Skip to content

Commit

Permalink
feat(jellyfish-api-core): change refundHtlcAll to return string arr
Browse files Browse the repository at this point in the history
  • Loading branch information
cwkang1998 committed Apr 8, 2022
1 parent 9178d8e commit 2b2d9de
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/node/CATEGORIES/14-spv.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
refundHtlcAll (destinationAddress: string, options: SpvDefaultOptions = { feeRate: new BigNumber('10000') }): Promise<string[]>
}

interface SpvDefaultOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,7 +51,7 @@ describe('Spv', () => {
address: htlc1.address,
confirms: 11,
spent: {
txid: result,
txid: results[0],
confirms: 1
}
})
Expand All @@ -59,7 +62,7 @@ describe('Spv', () => {
address: htlc2.address,
confirms: 11,
spent: {
txid: result,
txid: results[0],
confirms: 1
}
})
Expand All @@ -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 () => {
Expand All @@ -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
Expand All @@ -104,7 +110,7 @@ describe('Spv', () => {
address: htlc1.address,
confirms: 11,
spent: {
txid: result,
txid: results[0],
confirms: 1
}
})
Expand All @@ -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 () => {
Expand All @@ -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
Expand All @@ -153,7 +162,7 @@ describe('Spv', () => {
address: htlc.address,
confirms: 11,
spent: {
txid: result,
txid: results[0],
confirms: 1
}
})
Expand All @@ -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 () => {
Expand Down
11 changes: 5 additions & 6 deletions packages/jellyfish-api-core/src/category/spv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} txid
* @return {Promise<string[]>} array of txid
*/
async refundHtlcAll (destinationAddress: string, options: SpvDefaultOptions = { feeRate: new BigNumber('10000') }): Promise<string> {
async refundHtlcAll (destinationAddress: string, options: SpvDefaultOptions = { feeRate: new BigNumber('10000') }): Promise<string[]> {
/**
* 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<string[]>('spv_refundhtlcall', [destinationAddress, options.feeRate], 'number')
return result[0]
return await this.client.call<string[]>('spv_refundhtlcall', [destinationAddress, options.feeRate], 'number')
}

/**
Expand Down

0 comments on commit 2b2d9de

Please sign in to comment.