Files
bch-js/test/integration/chains/testnet/electrumx.js
T

367 lines
13 KiB
JavaScript
Raw Normal View History

2021-01-02 15:41:12 -08:00
const chai = require('chai')
2020-07-27 21:32:51 -07:00
const assert = chai.assert
2021-01-02 15:41:12 -08:00
const sinon = require('sinon')
2020-07-27 21:32:51 -07:00
const RESTURL = process.env.RESTURL
? process.env.RESTURL
2021-01-02 15:41:12 -08:00
: 'https://testnet3.fullstack.cash/v4/'
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
const BCHJS = require('../../../../src/bch-js')
2020-07-27 21:32:51 -07:00
const bchjs = new BCHJS({ restURL: RESTURL, apiToken: process.env.BCHJSTOKEN })
2021-01-02 15:41:12 -08:00
describe('#ElectrumX', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(async () => {
sandbox = sinon.createSandbox()
if (process.env.IS_USING_FREE_TIER) await sleep(1000)
})
2020-07-27 21:32:51 -07:00
afterEach(() => sandbox.restore())
2021-01-02 15:41:12 -08:00
describe('#utxo', () => {
it('should GET utxos for a single address', async () => {
const addr = 'bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2'
2020-07-27 21:32:51 -07:00
const result = await bchjs.Electrumx.utxo(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
2020-07-27 21:32:51 -07:00
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'utxos')
2020-07-27 21:32:51 -07:00
assert.isArray(result.utxos)
2021-01-02 15:41:12 -08:00
assert.property(result.utxos[0], 'height')
assert.property(result.utxos[0], 'tx_hash')
assert.property(result.utxos[0], 'tx_pos')
assert.property(result.utxos[0], 'value')
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
it('should POST utxo details for an array of addresses', async () => {
2020-07-27 21:32:51 -07:00
const addr = [
2021-01-02 15:41:12 -08:00
'bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2',
'bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2'
2020-07-27 21:32:51 -07:00
]
const result = await bchjs.Electrumx.utxo(addr)
2021-01-02 15:41:12 -08:00
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
2020-07-27 21:32:51 -07:00
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'utxos')
2020-07-27 21:32:51 -07:00
assert.isArray(result.utxos)
2021-01-02 15:41:12 -08:00
assert.property(result.utxos[0], 'utxos')
2020-07-27 21:32:51 -07:00
assert.isArray(result.utxos[0].utxos)
2021-01-02 15:41:12 -08:00
assert.property(result.utxos[0], 'address')
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
assert.property(result.utxos[0].utxos[0], 'height')
assert.property(result.utxos[0].utxos[0], 'tx_hash')
assert.property(result.utxos[0].utxos[0], 'tx_pos')
assert.property(result.utxos[0].utxos[0], 'value')
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
it('should throw error on array size rate limit', async () => {
2020-07-27 21:32:51 -07:00
try {
const addr = []
2021-01-02 15:41:12 -08:00
for (let i = 0; i < 25; i++) { addr.push('bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2') }
2020-07-27 21:32:51 -07:00
const result = await bchjs.Electrumx.utxo(addr)
2021-01-02 15:41:12 -08:00
// console.log(`result: ${util.inspect(result)}`)
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result!')
2020-07-27 21:32:51 -07:00
} catch (err) {
2021-01-02 15:41:12 -08:00
assert.hasAnyKeys(err, ['error'])
assert.include(err.error, 'Array too large')
2020-07-27 21:32:51 -07:00
}
})
})
2021-01-02 15:41:12 -08:00
describe('#balance', () => {
it('should GET balance for a single address', async () => {
const addr = 'bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2'
2020-07-27 21:32:51 -07:00
const result = await bchjs.Electrumx.balance(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
2020-07-27 21:32:51 -07:00
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'balance')
assert.property(result.balance, 'confirmed')
assert.property(result.balance, 'unconfirmed')
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
it('should POST request for balances for an array of addresses', async () => {
2020-07-27 21:32:51 -07:00
const addr = [
2021-01-02 15:41:12 -08:00
'bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2',
'bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2'
2020-07-27 21:32:51 -07:00
]
const result = await bchjs.Electrumx.balance(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
2020-07-27 21:32:51 -07:00
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'balances')
2020-07-27 21:32:51 -07:00
assert.isArray(result.balances)
2021-01-02 15:41:12 -08:00
assert.property(result.balances[0], 'address')
assert.property(result.balances[0], 'balance')
assert.property(result.balances[0].balance, 'confirmed')
assert.property(result.balances[0].balance, 'unconfirmed')
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
it('should throw error on array size rate limit', async () => {
2020-07-27 21:32:51 -07:00
try {
const addr = []
2021-01-02 15:41:12 -08:00
for (let i = 0; i < 25; i++) { addr.push('bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2') }
2020-07-27 21:32:51 -07:00
const result = await bchjs.Electrumx.balance(addr)
console.log(`result: ${util.inspect(result)}`)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result!')
2020-07-27 21:32:51 -07:00
} catch (err) {
2021-01-02 15:41:12 -08:00
assert.hasAnyKeys(err, ['error'])
assert.include(err.error, 'Array too large')
2020-07-27 21:32:51 -07:00
}
})
})
2021-01-02 15:41:12 -08:00
describe('#transactions', () => {
it('should GET transaction history for a single address', async () => {
const addr = 'bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2'
2020-07-27 21:32:51 -07:00
const result = await bchjs.Electrumx.transactions(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
2020-07-27 21:32:51 -07:00
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'transactions')
2020-07-27 21:32:51 -07:00
assert.isArray(result.transactions)
2021-01-02 15:41:12 -08:00
assert.property(result.transactions[0], 'height')
assert.property(result.transactions[0], 'tx_hash')
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
it('should POST request for transaction history for an array of addresses', async () => {
2020-07-27 21:32:51 -07:00
const addr = [
2021-01-02 15:41:12 -08:00
'bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2',
'bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2'
2020-07-27 21:32:51 -07:00
]
const result = await bchjs.Electrumx.transactions(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
2020-07-27 21:32:51 -07:00
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'transactions')
2020-07-27 21:32:51 -07:00
assert.isArray(result.transactions)
2021-01-02 15:41:12 -08:00
assert.property(result.transactions[0], 'address')
assert.property(result.transactions[0], 'transactions')
2020-07-27 21:32:51 -07:00
assert.isArray(result.transactions[0].transactions)
2021-01-02 15:41:12 -08:00
assert.property(result.transactions[0].transactions[0], 'height')
assert.property(result.transactions[0].transactions[0], 'tx_hash')
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
it('should throw error on array size rate limit', async () => {
2020-07-27 21:32:51 -07:00
try {
const addr = []
2021-01-02 15:41:12 -08:00
for (let i = 0; i < 25; i++) { addr.push('bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2') }
2020-07-27 21:32:51 -07:00
const result = await bchjs.Electrumx.transactions(addr)
console.log(`result: ${util.inspect(result)}`)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result!')
2020-07-27 21:32:51 -07:00
} catch (err) {
2021-01-02 15:41:12 -08:00
assert.hasAnyKeys(err, ['error'])
assert.include(err.error, 'Array too large')
2020-07-27 21:32:51 -07:00
}
})
})
2020-08-04 22:58:22 +09:00
2021-01-02 15:41:12 -08:00
describe('#unconfirmed', () => {
2020-08-05 08:54:54 -07:00
// These tests won't work because unconfirmed transactions are transient in nature.
/*
2020-08-04 22:58:22 +09:00
it(`should GET unconfirmed UTXOs (mempool) for a single address`, async () => {
const addr = "bchtest:qp25k20dgcljrz4hkdz43partam3j5httyprjp23qd"
const result = await bchjs.Electrumx.unconfirmed(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.property(result, "success")
assert.equal(result.success, true)
assert.property(result, "utxos")
assert.isArray(result.utxos)
assert.property(result.utxos[0], "height")
assert.property(result.utxos[0], "tx_hash")
assert.property(result.utxos[0], "fee")
})
it(`should POST unconfirmed UTXO details for an array of addresses`, async () => {
const addr = [
"bchtest:qp25k20dgcljrz4hkdz43partam3j5httyprjp23qd",
"bchtest:qpkl3xylrjx4jup6m66e7zg7whlaucsxeudxeqawdj"
]
const result = await bchjs.Electrumx.unconfirmed(addr)
//console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.property(result, "success")
assert.equal(result.success, true)
assert.property(result, "utxos")
assert.isArray(result.utxos)
assert.property(result.utxos[0], "utxos")
assert.isArray(result.utxos[0].utxos)
assert.property(result.utxos[0], "address")
assert.property(result.utxos[0].utxos[0], "height")
assert.property(result.utxos[0].utxos[0], "tx_hash")
assert.property(result.utxos[0].utxos[0], "fee")
})
2020-08-05 08:54:54 -07:00
*/
2020-08-04 22:58:22 +09:00
2021-01-02 15:41:12 -08:00
it('should throw error on array size rate limit', async () => {
2020-08-04 22:58:22 +09:00
try {
const addr = []
2021-01-02 15:41:12 -08:00
for (let i = 0; i < 25; i++) { addr.push('bchtest:qrvn2n228aa39xupcw9jw0d3fj8axxky656e4j62z2') }
2020-08-04 22:58:22 +09:00
2020-08-05 08:54:54 -07:00
await bchjs.Electrumx.unconfirmed(addr)
2021-01-02 15:41:12 -08:00
// console.log(`result: ${util.inspect(result)}`)
2020-08-04 22:58:22 +09:00
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result!')
2020-08-04 22:58:22 +09:00
} catch (err) {
2021-01-02 15:41:12 -08:00
assert.hasAnyKeys(err, ['error'])
assert.include(err.error, 'Array too large')
2020-08-04 22:58:22 +09:00
}
})
})
2021-01-02 15:41:12 -08:00
describe('#blockHeader', () => {
it('should GET block headers for given height and count', async () => {
const height = 42
const count = 2
const result = await bchjs.Electrumx.blockHeader(height, count)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'headers')
assert.isArray(result.headers)
assert.equal(result.headers.length, 2)
})
2021-01-02 15:41:12 -08:00
it('should GET block headers for given height with default count = 1', async () => {
const height = 42
const result = await bchjs.Electrumx.blockHeader(height)
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'headers')
assert.isArray(result.headers)
assert.equal(result.headers.length, 1)
})
})
2021-01-02 15:41:12 -08:00
describe('#txData', () => {
it('should GET details for a single transaction', async () => {
2020-10-30 15:10:55 -07:00
const txid =
2021-01-02 15:41:12 -08:00
'76d2ee0ebd8b978742f6478ff9a12f478c34e4cee0a2b919059101d961bd01ee'
const result = await bchjs.Electrumx.txData(txid)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'details')
assert.isObject(result.details)
2021-01-02 15:41:12 -08:00
assert.property(result.details, 'blockhash')
assert.property(result.details, 'hash')
assert.property(result.details, 'hex')
assert.property(result.details, 'vin')
assert.property(result.details, 'vout')
assert.equal(result.details.hash, txid)
})
2021-01-02 15:41:12 -08:00
it('should POST details for an array of transactions', async () => {
const txids = [
2021-01-02 15:41:12 -08:00
'76d2ee0ebd8b978742f6478ff9a12f478c34e4cee0a2b919059101d961bd01ee',
'76d2ee0ebd8b978742f6478ff9a12f478c34e4cee0a2b919059101d961bd01ee'
]
const result = await bchjs.Electrumx.txData(txids)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'transactions')
assert.isArray(result.transactions)
2021-01-02 15:41:12 -08:00
assert.property(result.transactions[0], 'txid')
assert.property(result.transactions[0], 'details')
2021-01-02 15:41:12 -08:00
assert.property(result.transactions[0].details, 'blockhash')
assert.property(result.transactions[0].details, 'hash')
assert.property(result.transactions[0].details, 'hex')
assert.property(result.transactions[0].details, 'vin')
assert.property(result.transactions[0].details, 'vout')
})
2021-01-02 15:41:12 -08:00
it('should throw error on array size rate limit', async () => {
try {
const txids = []
2020-10-30 15:10:55 -07:00
for (let i = 0; i < 25; i++) {
txids.push(
2021-01-02 15:41:12 -08:00
'76d2ee0ebd8b978742f6478ff9a12f478c34e4cee0a2b919059101d961bd01ee'
2020-10-30 15:10:55 -07:00
)
}
await bchjs.Electrumx.txData(txids)
console.log(`result: ${util.inspect(result)}`)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result!')
} catch (err) {
2021-01-02 15:41:12 -08:00
assert.hasAnyKeys(err, ['error'])
assert.include(err.error, 'Array too large')
}
})
})
2020-10-07 17:14:30 +09:00
2021-01-02 15:41:12 -08:00
describe('#broadcast', () => {
it('should broadcast a single transaction', async () => {
2020-10-30 15:10:55 -07:00
const txHex =
2021-01-02 15:41:12 -08:00
'020000000265d13ef402840c8a51f39779afb7ae4d49e4b0a3c24a3d0e7742038f2c679667010000006441dd1dd72770cadede1a7fd0363574846c48468a398ddfa41a9677c74cac8d2652b682743725a3b08c6c2021a629011e11a264d9036e9d5311e35b5f4937ca7b4e4121020797d8fd4d2fa6fd7cdeabe2526bfea2b90525d6e8ad506ec4ee3c53885aa309ffffffff65d13ef402840c8a51f39779afb7ae4d49e4b0a3c24a3d0e7742038f2c679667000000006441347d7f218c11c04487c1ad8baac28928fb10e5054cd4494b94d078cfa04ccf68e064fb188127ff656c0b98e9ce87f036d183925d0d0860605877d61e90375f774121028a53f95eb631b460854fc836b2e5d31cad16364b4dc3d970babfbdcc3f2e4954ffffffff035ac355000000000017a914189ce02e332548f4804bac65cba68202c9dbf822878dfd0800000000001976a914285bb350881b21ac89724c6fb6dc914d096cd53b88acf9ef3100000000001976a91445f1f1c4a9b9419a5088a3e9c24a293d7a150e6488ac00000000'
2020-10-07 17:14:30 +09:00
try {
await bchjs.Electrumx.broadcast(txHex)
} catch (err) {
2021-01-02 15:41:12 -08:00
assert.property(err, 'success')
2020-10-07 17:14:30 +09:00
assert.equal(err.success, false)
2020-10-30 15:10:55 -07:00
assert.include(
err.error,
2021-01-02 15:41:12 -08:00
'the transaction was rejected by network rules'
2020-10-30 15:10:55 -07:00
)
2020-10-07 17:14:30 +09:00
}
})
})
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}