2020-07-27 21:32:51 -07:00
|
|
|
/*
|
|
|
|
|
Integration tests for the bchjs covering SLP tokens.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-11-16 13:37:04 -08:00
|
|
|
import chai from 'chai'
|
|
|
|
|
// if (process.env.RESTURL) RESTURL = process.env.RESTURL
|
|
|
|
|
|
|
|
|
|
import BCHJS from '../../../../src/bch-js.js'
|
|
|
|
|
|
|
|
|
|
// Inspect utility used for debugging.
|
|
|
|
|
import util from 'util'
|
2020-07-27 21:32:51 -07:00
|
|
|
const assert = chai.assert
|
|
|
|
|
|
|
|
|
|
const RESTURL = process.env.RESTURL
|
|
|
|
|
? process.env.RESTURL
|
2021-06-16 17:00:08 -07:00
|
|
|
: 'https://testnet3.fullstack.cash/v5/'
|
|
|
|
|
// const bchjs = new BCHJS({ restURL: `https://testnet.bchjs.cash/v5/` })
|
2020-07-27 21:32:51 -07:00
|
|
|
const bchjs = new BCHJS({ restURL: RESTURL, apiToken: process.env.BCHJSTOKEN })
|
|
|
|
|
util.inspect.defaultOptions = {
|
|
|
|
|
showHidden: true,
|
|
|
|
|
colors: true,
|
|
|
|
|
depth: 1
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#SLP', () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
beforeEach(async () => {
|
|
|
|
|
// Introduce a delay so that the BVT doesn't trip the rate limits.
|
2021-08-09 14:54:49 -07:00
|
|
|
if (process.env.IS_USING_FREE_TIER) await sleep(1500)
|
2020-07-27 21:32:51 -07:00
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#util', () => {
|
|
|
|
|
it('should get information on the Oasis token', async () => {
|
2021-01-02 16:39:44 -08:00
|
|
|
const tokenId =
|
|
|
|
|
'a371e9934c7695d08a5eb7f31d3bceb4f3644860cc67520cda1e149423b9ec39'
|
2020-07-27 21:32:51 -07:00
|
|
|
|
|
|
|
|
const result = await bchjs.SLP.Utils.list(tokenId)
|
2021-01-02 15:41:12 -08:00
|
|
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
2020-07-27 21:32:51 -07:00
|
|
|
|
|
|
|
|
assert.hasAnyKeys(result, [
|
2021-01-02 15:41:12 -08:00
|
|
|
'decimals',
|
|
|
|
|
'timestamp',
|
|
|
|
|
'timestamp_unix',
|
|
|
|
|
'versionType',
|
|
|
|
|
'documentUri',
|
|
|
|
|
'symbol',
|
|
|
|
|
'name',
|
|
|
|
|
'containsBaton',
|
|
|
|
|
'id',
|
|
|
|
|
'documentHash',
|
|
|
|
|
'initialTokenQty',
|
|
|
|
|
'blockCreated',
|
|
|
|
|
'blockLastActiveSend',
|
|
|
|
|
'blockLastActiveMint',
|
|
|
|
|
'txnsSinceGenesis',
|
|
|
|
|
'validAddress',
|
|
|
|
|
'totalMinted',
|
|
|
|
|
'totalBurned',
|
|
|
|
|
'circulatingSupply',
|
|
|
|
|
'mintingBatonStatus'
|
2020-07-27 21:32:51 -07:00
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#decodeOpReturn', () => {
|
|
|
|
|
it('should decode the OP_RETURN for a SEND txid', async () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
const txid =
|
2021-01-02 15:41:12 -08:00
|
|
|
'ad28116e0818339342dddfc5f58ca8a5379ceb9679b4e4cbd72f4de905415ec1'
|
2020-07-27 21:32:51 -07:00
|
|
|
|
|
|
|
|
const result = await bchjs.SLP.Utils.decodeOpReturn(txid)
|
|
|
|
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
assert.hasAllKeys(result, ['amounts', 'tokenType', 'tokenId', 'txType'])
|
2020-07-27 21:32:51 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#balancesForAddress', () => {
|
|
|
|
|
it('should fetch all balances for address: slptest:qz0kc67pm4emjyr3gaaa2wstdaykg9m4yqwlzpj3w9', async () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
const balances = await bchjs.SLP.Utils.balancesForAddress(
|
2021-01-02 15:41:12 -08:00
|
|
|
'slptest:qz0kc67pm4emjyr3gaaa2wstdaykg9m4yqwlzpj3w9'
|
2020-07-27 21:32:51 -07:00
|
|
|
)
|
|
|
|
|
// console.log(`balances: ${JSON.stringify(balances, null, 2)}`)
|
|
|
|
|
|
|
|
|
|
assert.isArray(balances)
|
|
|
|
|
assert.hasAllKeys(balances[0], [
|
2021-01-02 15:41:12 -08:00
|
|
|
'tokenId',
|
|
|
|
|
'balanceString',
|
|
|
|
|
'balance',
|
|
|
|
|
'decimalCount',
|
|
|
|
|
'slpAddress'
|
2020-07-27 21:32:51 -07:00
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should fetch balances for multiple addresses', async () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
const addresses = [
|
2021-01-02 15:41:12 -08:00
|
|
|
'slptest:qz0kc67pm4emjyr3gaaa2wstdaykg9m4yqwlzpj3w9',
|
|
|
|
|
'slptest:qr5uy4h8ysyhwkp9s245scckdnc7teyjqc5ft2z43h'
|
2020-07-27 21:32:51 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
const balances = await bchjs.SLP.Utils.balancesForAddress(addresses)
|
|
|
|
|
// console.log(`balances: ${JSON.stringify(balances, null, 2)}`)
|
|
|
|
|
|
|
|
|
|
assert.isArray(balances)
|
|
|
|
|
assert.isArray(balances[0])
|
|
|
|
|
assert.hasAllKeys(balances[0][0], [
|
2021-01-02 15:41:12 -08:00
|
|
|
'tokenId',
|
|
|
|
|
'balanceString',
|
|
|
|
|
'balance',
|
|
|
|
|
'decimalCount',
|
|
|
|
|
'slpAddress'
|
2020-07-27 21:32:51 -07:00
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
})
|
2020-10-16 22:22:18 -07:00
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#tokenUtxoDetails', () => {
|
|
|
|
|
it('should hydrate UTXOs', async () => {
|
2020-10-16 22:22:18 -07:00
|
|
|
const bchAddr = bchjs.SLP.Address.toCashAddress(
|
2021-01-02 15:41:12 -08:00
|
|
|
'slptest:qz0kc67pm4emjyr3gaaa2wstdaykg9m4yqwlzpj3w9'
|
2020-10-16 22:22:18 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const utxos = await bchjs.Electrumx.utxo([bchAddr])
|
|
|
|
|
// console.log(`utxos: ${JSON.stringify(utxos, null, 2)}`)
|
|
|
|
|
|
|
|
|
|
const utxoInfo = await bchjs.SLP.Utils.tokenUtxoDetails(
|
|
|
|
|
utxos.utxos[0].utxos
|
|
|
|
|
)
|
|
|
|
|
// console.log(`utxoInfo: ${JSON.stringify(utxoInfo, null, 2)}`)
|
|
|
|
|
|
|
|
|
|
assert.isArray(utxoInfo)
|
|
|
|
|
|
|
|
|
|
// first UTXO should be a PSF test token.
|
|
|
|
|
assert.equal(utxoInfo[0].isValid, true)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#hydrateUtxos', () => {
|
2020-10-16 22:22:18 -07:00
|
|
|
// This test will error out if the LOCAL_RESTURL settings is not set properly
|
|
|
|
|
// in bch-api.
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should hydrate UTXOs', async () => {
|
2020-10-16 22:22:18 -07:00
|
|
|
const bchAddr = bchjs.SLP.Address.toCashAddress(
|
2021-01-02 15:41:12 -08:00
|
|
|
'slptest:qz0kc67pm4emjyr3gaaa2wstdaykg9m4yqwlzpj3w9'
|
2020-10-16 22:22:18 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const utxos = await bchjs.Electrumx.utxo([bchAddr])
|
|
|
|
|
// console.log(`utxos: ${JSON.stringify(utxos, null, 2)}`)
|
|
|
|
|
|
|
|
|
|
const utxoInfo = await bchjs.SLP.Utils.hydrateUtxos(utxos.utxos)
|
|
|
|
|
// console.log(`utxoInfo: ${JSON.stringify(utxoInfo, null, 2)}`)
|
|
|
|
|
|
|
|
|
|
assert.isArray(utxoInfo.slpUtxos[0].utxos)
|
|
|
|
|
|
|
|
|
|
// first UTXO should be a PSF test token.
|
|
|
|
|
assert.equal(utxoInfo.slpUtxos[0].utxos[0].isValid, true)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#validateTxid2', () => {
|
|
|
|
|
it('should validate a token txid', async () => {
|
2020-10-16 22:22:18 -07:00
|
|
|
const txid =
|
2021-01-02 15:41:12 -08:00
|
|
|
'ad28116e0818339342dddfc5f58ca8a5379ceb9679b4e4cbd72f4de905415ec1'
|
2020-10-16 22:22:18 -07:00
|
|
|
|
|
|
|
|
const validated = await bchjs.SLP.Utils.validateTxid(txid)
|
|
|
|
|
// console.log(validated)
|
|
|
|
|
|
|
|
|
|
assert.equal(validated[0].valid, true)
|
|
|
|
|
})
|
|
|
|
|
})
|
2020-07-27 21:32:51 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Promise-based sleep function
|
2021-01-02 15:41:12 -08:00
|
|
|
function sleep (ms) {
|
2020-07-27 21:32:51 -07:00
|
|
|
return new Promise(resolve => setTimeout(resolve, ms))
|
|
|
|
|
}
|