From d0a3b276824a0bd443b767895b1b0b405bfed16f Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 11 Jul 2022 07:09:44 -0700 Subject: [PATCH] fix(ecash): Adding token support for ecash --- src/address.js | 6 +-- src/electrumx.js | 5 +++ src/utxo.js | 7 +++- .../chains/abc/psf-slp-indexer-integration.js | 38 +++++++++++++++++++ .../chains/abc/utxo-integration.js | 26 ++++++------- .../bchn/psf-slp-indexer.integration.js | 5 ++- 6 files changed, 67 insertions(+), 20 deletions(-) create mode 100644 test/integration/chains/abc/psf-slp-indexer-integration.js diff --git a/src/address.js b/src/address.js index 11ea291..f27a02f 100644 --- a/src/address.js +++ b/src/address.js @@ -166,11 +166,11 @@ class Address { * * @apiExample Example usage: * // mainnet - * bchjs.Address.toEcashAddress('bitcoincash:qq50d800hgunr8u4trz3uuppspk3mds0dy9978plt2') - * // ecash:qq50d800hgunr8u4trz3uuppspk3mds0dyug2v69da + * bchjs.Address.toEtokenAddress('bitcoincash:qq50d800hgunr8u4trz3uuppspk3mds0dy9978plt2') + * // etoken:qq50d800hgunr8u4trz3uuppspk3mds0dyug2v69da * * // mainnet no prefix - * bchjs.Address.toEcashAddress('bitcoincash:qq50d800hgunr8u4trz3uuppspk3mds0dy9978plt2', false) + * bchjs.Address.toEtokenAddress('bitcoincash:qq50d800hgunr8u4trz3uuppspk3mds0dy9978plt2', false) * // qq50d800hgunr8u4trz3uuppspk3mds0dyug2v69da * */ diff --git a/src/electrumx.js b/src/electrumx.js index 1d88dc4..5bdc3d8 100644 --- a/src/electrumx.js +++ b/src/electrumx.js @@ -7,6 +7,7 @@ const axios = require('axios') // Local libraries. const Blockchain = require('./blockchain') +// const Address = require('./address') // let _this @@ -32,7 +33,9 @@ class ElectrumX { } } + // Encapsulate dependencies this.blockchain = new Blockchain(config) + // this.address = new Address(config) // _this = this } @@ -104,6 +107,8 @@ class ElectrumX { */ async utxo (address) { try { + // console.log(`electrumx.js/utxo() restURL: ${this.restURL}`) + // Handle single address. if (typeof address === 'string') { const response = await axios.get( diff --git a/src/utxo.js b/src/utxo.js index c2a50c6..0a0ec59 100644 --- a/src/utxo.js +++ b/src/utxo.js @@ -82,7 +82,10 @@ class UTXO { } // Ensure the address is a BCH address. - const addr = this.slp.Address.toCashAddress(address) + let addr = address + if (!addr.includes('ecash')) { + addr = this.slp.Address.toCashAddress(address) + } // Get the UTXOs associated with the address. const utxoData = await this.electrumx.utxo(addr) @@ -209,7 +212,7 @@ class UTXO { return outObj } catch (err) { - console.error('Error in bchjs.Utxo.get()') + console.error('Error in bchjs.Utxo.get(): ', err) if (err.error) throw new Error(err.error) throw err diff --git a/test/integration/chains/abc/psf-slp-indexer-integration.js b/test/integration/chains/abc/psf-slp-indexer-integration.js new file mode 100644 index 0000000..40a1a0b --- /dev/null +++ b/test/integration/chains/abc/psf-slp-indexer-integration.js @@ -0,0 +1,38 @@ +/* + Integration tests for the psf-slp-indexer.js library, specific to the eCash + blockchain. +*/ + +// Global npm libraries +const assert = require('chai').assert + +// Local libraries +const BCHJS = require('../../../../src/bch-js') +let bchjs + +describe('#psf-slp-indexer', () => { + beforeEach(async () => { + // Introduce a delay so that the BVT doesn't trip the rate limits. + if (process.env.IS_USING_FREE_TIER) await sleep(3000) + + bchjs = new BCHJS() + }) + + describe('#balance', () => { + it('should get token balance for an ecash address', async () => { + const addr = 'ecash:qr5c4hfy52zn87484cucvzle5pljz0gtr5vhtw9z09' + + const result = await bchjs.PsfSlpIndexer.balance(addr) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.property(result.balance, 'utxos') + assert.property(result.balance, 'txs') + assert.property(result.balance, 'balances') + }) + }) +}) + +// Promise-based sleep function +function sleep (ms) { + return new Promise(resolve => setTimeout(resolve, ms)) +} diff --git a/test/integration/chains/abc/utxo-integration.js b/test/integration/chains/abc/utxo-integration.js index c387c7b..2589ede 100644 --- a/test/integration/chains/abc/utxo-integration.js +++ b/test/integration/chains/abc/utxo-integration.js @@ -2,10 +2,10 @@ Integration tests for the utxo.js library. */ -// const assert = require('chai').assert +const assert = require('chai').assert -// const BCHJS = require('../../../../src/bch-js') -// const bchjs = new BCHJS() +const BCHJS = require('../../../../src/bch-js') +const bchjs = new BCHJS() describe('#UTXO', () => { beforeEach(async () => { @@ -14,25 +14,23 @@ describe('#UTXO', () => { if (process.env.IS_USING_FREE_TIER) await sleep(1500) }) - /* describe('#get', () => { it('should get hydrated and filtered UTXOs for an address', async () => { - // const addr = 'bitcoincash:qqh793x9au6ehvh7r2zflzguanlme760wuzehgzjh9' - const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9' + const addr = 'ecash:qr5c4hfy52zn87484cucvzle5pljz0gtr5vhtw9z09' + // const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9' const result = await bchjs.Utxo.get(addr) // console.log(`result: ${JSON.stringify(result, null, 2)}`) - assert.isArray(result) - assert.property(result[0], 'address') - assert.property(result[0], 'bchUtxos') - assert.property(result[0], 'nullUtxos') - assert.property(result[0], 'slpUtxos') - assert.isArray(result[0].bchUtxos) - assert.isArray(result[0].nullUtxos) + // assert.isArray(result) + assert.property(result, 'address') + assert.property(result, 'bchUtxos') + assert.property(result, 'nullUtxos') + assert.property(result, 'slpUtxos') + assert.isArray(result.bchUtxos) + assert.isArray(result.nullUtxos) }) }) - */ }) function sleep (ms) { diff --git a/test/integration/chains/bchn/psf-slp-indexer.integration.js b/test/integration/chains/bchn/psf-slp-indexer.integration.js index 479e6cc..ba0a317 100644 --- a/test/integration/chains/bchn/psf-slp-indexer.integration.js +++ b/test/integration/chains/bchn/psf-slp-indexer.integration.js @@ -1,9 +1,12 @@ /* - Integration tests for the psf-slp-indexer.js library + Integration tests for the psf-slp-indexer.js library, specific to the BCH + blockchain. */ +// Global npm libraries const assert = require('chai').assert +// Local libraries const BCHJS = require('../../../../src/bch-js') let bchjs