From 2b2d9decfff7ddbe8e75d8a106d79ba5d38cbcc2 Mon Sep 17 00:00:00 2001 From: chen <23054115+cwkang1998@users.noreply.github.com> Date: Fri, 8 Apr 2022 14:11:01 +0800 Subject: [PATCH] feat(jellyfish-api-core): change refundHtlcAll to return string arr --- docs/node/CATEGORIES/14-spv.md | 2 +- .../category/spv/refundHtlcAll.test.ts | 41 +++++++++++-------- .../jellyfish-api-core/src/category/spv.ts | 11 +++-- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/docs/node/CATEGORIES/14-spv.md b/docs/node/CATEGORIES/14-spv.md index 72f26c6c90..046fc3c8ad 100644 --- a/docs/node/CATEGORIES/14-spv.md +++ b/docs/node/CATEGORIES/14-spv.md @@ -164,7 +164,7 @@ Gets all HTLC contracts stored in wallet and creates refunds transactions for al ```ts title="client.spv.refundHtlcAll()" interface spv { - refundHtlcAll (destinationAddress: string, options: SpvDefaultOptions = { feeRate: new BigNumber('10000') }): Promise + refundHtlcAll (destinationAddress: string, options: SpvDefaultOptions = { feeRate: new BigNumber('10000') }): Promise } interface SpvDefaultOptions { diff --git a/packages/jellyfish-api-core/__tests__/category/spv/refundHtlcAll.test.ts b/packages/jellyfish-api-core/__tests__/category/spv/refundHtlcAll.test.ts index 8fb3815af7..87a7a05ce6 100644 --- a/packages/jellyfish-api-core/__tests__/category/spv/refundHtlcAll.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/spv/refundHtlcAll.test.ts @@ -28,9 +28,12 @@ describe('Spv', () => { await container.spv.increaseSpvHeight(10) const destinationAddress = await container.call('spv_getnewaddress') - const result = await testing.rpc.spv.refundHtlcAll(destinationAddress) // This refund should only happen after timeout threshold set in createHtlc. See https://en.bitcoin.it/wiki/BIP_0199 - expect(typeof result).toStrictEqual('string') - expect(result.length).toStrictEqual(64) + const results = await testing.rpc.spv.refundHtlcAll(destinationAddress) // This refund should only happen after timeout threshold set in createHtlc. See https://en.bitcoin.it/wiki/BIP_0199 + expect(results.length).toStrictEqual(1) + for (const txid of results) { + expect(typeof txid).toStrictEqual('string') + expect(txid.length).toStrictEqual(64) + } /** * Assert that refund happened @@ -48,7 +51,7 @@ describe('Spv', () => { address: htlc1.address, confirms: 11, spent: { - txid: result, + txid: results[0], confirms: 1 } }) @@ -59,7 +62,7 @@ describe('Spv', () => { address: htlc2.address, confirms: 11, spent: { - txid: result, + txid: results[0], confirms: 1 } }) @@ -70,7 +73,7 @@ describe('Spv', () => { const listReceivingAddresses: any[] = await container.call('spv_listreceivedbyaddress') const receivingAddress = listReceivingAddresses.find(({ address }: { address: string }) => address === destinationAddress) expect(receivingAddress.address).toStrictEqual(destinationAddress) - expect(receivingAddress.txids.some((txid: string) => txid === result)).toStrictEqual(true) + expect(receivingAddress.txids.some((txid: string) => txid === results[0])).toStrictEqual(true) }) it('should refundHtlcAll for expired only', async () => { @@ -84,9 +87,12 @@ describe('Spv', () => { await container.spv.increaseSpvHeight(10) const destinationAddress = await container.call('spv_getnewaddress') - const result = await testing.rpc.spv.refundHtlcAll(destinationAddress) // This refund should only happen after timeout threshold set in createHtlc. See https://en.bitcoin.it/wiki/BIP_0199 - expect(typeof result).toStrictEqual('string') - expect(result.length).toStrictEqual(64) + const results = await testing.rpc.spv.refundHtlcAll(destinationAddress) // This refund should only happen after timeout threshold set in createHtlc. See https://en.bitcoin.it/wiki/BIP_0199 + expect(results.length).toStrictEqual(1) + for (const txid of results) { + expect(typeof txid).toStrictEqual('string') + expect(txid.length).toStrictEqual(64) + } /** * Assert that refund happened @@ -104,7 +110,7 @@ describe('Spv', () => { address: htlc1.address, confirms: 11, spent: { - txid: result, + txid: results[0], confirms: 1 } }) @@ -122,7 +128,7 @@ describe('Spv', () => { const listReceivingAddresses: any[] = await container.call('spv_listreceivedbyaddress') const receivingAddress = listReceivingAddresses.find(({ address }: { address: string }) => address === destinationAddress) expect(receivingAddress.address).toStrictEqual(destinationAddress) - expect(receivingAddress.txids.some((txid: string) => txid === result)).toStrictEqual(true) + expect(receivingAddress.txids.some((txid: string) => txid === results[0])).toStrictEqual(true) }) it('should refundHtlcAll with custom feeRate', async () => { @@ -134,9 +140,12 @@ describe('Spv', () => { await container.spv.increaseSpvHeight() const destinationAddress = await container.call('spv_getnewaddress') - const result = await testing.rpc.spv.refundHtlcAll(destinationAddress, { feeRate: new BigNumber('20000') }) // This refund should only happen after timeout threshold set in createHtlc. See https://en.bitcoin.it/wiki/BIP_0199 - expect(typeof result).toStrictEqual('string') - expect(result.length).toStrictEqual(64) + const results = await testing.rpc.spv.refundHtlcAll(destinationAddress, { feeRate: new BigNumber('20000') }) // This refund should only happen after timeout threshold set in createHtlc. See https://en.bitcoin.it/wiki/BIP_0199 + expect(results.length).toStrictEqual(1) + for (const txid of results) { + expect(typeof txid).toStrictEqual('string') + expect(txid.length).toStrictEqual(64) + } /** * Assert that refund happened @@ -153,7 +162,7 @@ describe('Spv', () => { address: htlc.address, confirms: 11, spent: { - txid: result, + txid: results[0], confirms: 1 } }) @@ -164,7 +173,7 @@ describe('Spv', () => { const listReceivingAddresses = await container.call('spv_listreceivedbyaddress') const receivingAddress = listReceivingAddresses.find(({ address }: { address: string }) => address === destinationAddress) expect(receivingAddress.address).toStrictEqual(destinationAddress) - expect(receivingAddress.txids.some((txid: string) => txid === result)).toStrictEqual(true) + expect(receivingAddress.txids.some((txid: string) => txid === results[0])).toStrictEqual(true) }) it('should not refundHtlcAll when no unspent HTLC outputs found', async () => { diff --git a/packages/jellyfish-api-core/src/category/spv.ts b/packages/jellyfish-api-core/src/category/spv.ts index f3de11b01e..3d09a62597 100644 --- a/packages/jellyfish-api-core/src/category/spv.ts +++ b/packages/jellyfish-api-core/src/category/spv.ts @@ -126,16 +126,15 @@ export class Spv { * @param {string} destinationAddress Destination for funds in the HTLC * @param {SpvDefaultOptions} [options] * @param {BigNumber} [options.feeRate=10000] Fee rate in satoshis per KB. Minimum is 1000. - * @return {Promise} txid + * @return {Promise} array of txid */ - async refundHtlcAll (destinationAddress: string, options: SpvDefaultOptions = { feeRate: new BigNumber('10000') }): Promise { + async refundHtlcAll (destinationAddress: string, options: SpvDefaultOptions = { feeRate: new BigNumber('10000') }): Promise { /** * Looking at ain, its returning an array of txid containing only 1 txid. - * https://github.com/DeFiCh/ain/blob/86f554f2454edc8c16684cac6f7332b437a991e0/src/spv/spv_wrapper.cpp#L1354-L1379 - * For now, extract the txid from the array according to docs from rpc. + * Considering some factors, this implementation is different from the rpc docs + * on ain side. Refer to PR https://github.com/DeFiCh/jellyfish/pull/1324 */ - const result = await this.client.call('spv_refundhtlcall', [destinationAddress, options.feeRate], 'number') - return result[0] + return await this.client.call('spv_refundhtlcall', [destinationAddress, options.feeRate], 'number') } /**