From 3ef842b02a1673947c2d11c9f5b9a8f5aa2eda7c Mon Sep 17 00:00:00 2001 From: David de Kloet Date: Thu, 14 Dec 2023 09:29:56 +0100 Subject: [PATCH 1/6] Add retrieveBtcStatusV2ByAccount to ckbtc canister --- packages/ckbtc/src/minter.canister.spec.ts | 77 +++++++++++++++++++- packages/ckbtc/src/minter.canister.ts | 22 ++++++ packages/ckbtc/src/types/minter.responses.ts | 6 ++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/packages/ckbtc/src/minter.canister.spec.ts b/packages/ckbtc/src/minter.canister.spec.ts index 387ded72..91616c50 100644 --- a/packages/ckbtc/src/minter.canister.spec.ts +++ b/packages/ckbtc/src/minter.canister.spec.ts @@ -2,7 +2,11 @@ import { ActorSubclass } from "@dfinity/agent"; import { Principal } from "@dfinity/principal"; import { arrayOfNumberToUint8Array, toNullable } from "@dfinity/utils"; import { mock } from "jest-mock-extended"; -import type { Account, _SERVICE as CkBTCMinterService } from "../candid/minter"; +import type { + Account, + _SERVICE as CkBTCMinterService, + RetrieveBtcStatusV2, +} from "../candid/minter"; import { RetrieveBtcError, RetrieveBtcOk, @@ -689,6 +693,77 @@ describe("ckBTC minter canister", () => { }); }); + describe("Retrieve BTC status V2 by account", () => { + const owner = Principal.fromHex("4321"); + const status1 = { + Submitted: { txid: new Uint8Array([3, 2, 6]) }, + } as RetrieveBtcStatusV2; + const status2 = { + Reimbursed: { + account: { owner, subaccount: [] }, + mint_block_index: 103n, + amount: 123_000n, + reason: { + CallFailed: null, + }, + }, + } as RetrieveBtcStatusV2; + const response = [ + { + block_index: 101n, + status_v2: [status1], + }, + { + block_index: 102n, + status_v2: [status2], + }, + ] as { + block_index: bigint; + status_v2: [] | [RetrieveBtcStatusV2]; + }[]; + + const expectedResponse = [ + { + id: 101n, + status: status1, + }, + { + id: 102n, + status: status2, + }, + ]; + + it("should return statuses", async () => { + const service = mock>(); + service.retrieve_btc_status_v2_by_account.mockResolvedValue(response); + + const canister = minter(service); + + const res = await canister.retrieveBtcStatusV2ByAccount({ + certified: true, + }); + + expect(service.retrieve_btc_status_v2_by_account).toBeCalledTimes(1); + expect(service.retrieve_btc_status_v2_by_account).toBeCalledWith([]); + expect(res).toEqual(expectedResponse); + }); + + it("should use non-certified service", async () => { + const service = mock>(); + service.retrieve_btc_status_v2_by_account.mockResolvedValue(response); + + const canister = nonCertifiedMinter(service); + + const res = await canister.retrieveBtcStatusV2ByAccount({ + certified: false, + }); + + expect(service.retrieve_btc_status_v2_by_account).toBeCalledTimes(1); + expect(service.retrieve_btc_status_v2_by_account).toBeCalledWith([]); + expect(res).toEqual(expectedResponse); + }); + }); + describe("Estimate Withdrawal Fee", () => { it("should return estimated fee", async () => { const result = { minter_fee: 123n, bitcoin_fee: 456n }; diff --git a/packages/ckbtc/src/minter.canister.ts b/packages/ckbtc/src/minter.canister.ts index 178e5c19..20e129f4 100644 --- a/packages/ckbtc/src/minter.canister.ts +++ b/packages/ckbtc/src/minter.canister.ts @@ -1,6 +1,7 @@ import { Canister, createServices, + fromNullable, toNullable, type QueryParams, } from "@dfinity/utils"; @@ -28,6 +29,7 @@ import type { import type { EstimateWithdrawalFee, RetrieveBtcResponse, + RetrieveBtcStatusV2WithId, RetrieveBtcWithApprovalResponse, UpdateBalanceOk, UpdateBalanceResponse, @@ -188,6 +190,26 @@ export class CkBTCMinterCanister extends Canister { certified, }).retrieve_btc_status({ block_index: transactionId }); + /** + * Returns the status of all BTC withdrawals for the user's main account. + * + * @param {bigint} transactionId The ID of the corresponding burn transaction. + * @param {boolean} certified query or update call + */ + retrieveBtcStatusV2ByAccount = async ({ + certified, + }: { + certified: boolean; + }): Promise => { + const statuses = await this.caller({ + certified, + }).retrieve_btc_status_v2_by_account([]); + return statuses.map(({ block_index, status_v2 }) => ({ + id: block_index, + status: fromNullable(status_v2), + })); + }; + /** * Returns an estimation of the user's fee (in Satoshi) for a retrieve_btc request based on the current status of the Bitcoin network and the fee related to the minter. * diff --git a/packages/ckbtc/src/types/minter.responses.ts b/packages/ckbtc/src/types/minter.responses.ts index 31d4c682..0aa859d6 100644 --- a/packages/ckbtc/src/types/minter.responses.ts +++ b/packages/ckbtc/src/types/minter.responses.ts @@ -1,6 +1,7 @@ import type { RetrieveBtcError, RetrieveBtcOk, + RetrieveBtcStatusV2, RetrieveBtcWithApprovalError, UpdateBalanceError, UtxoStatus, @@ -21,3 +22,8 @@ export type RetrieveBtcWithApprovalResponse = | { Err: RetrieveBtcWithApprovalError }; export type EstimateWithdrawalFee = { minter_fee: bigint; bitcoin_fee: bigint }; + +export type RetrieveBtcStatusV2WithId = { + id: bigint; + status: RetrieveBtcStatusV2 | undefined; +}; From cba6501acbe0c436d9a6b94917f8b2448c5709b1 Mon Sep 17 00:00:00 2001 From: David de Kloet Date: Thu, 14 Dec 2023 09:30:34 +0100 Subject: [PATCH 2/6] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5989c6fc..fdacff4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Substitute `?` fields with `Option` fields in the converters related to NNS proposals. - Add retrieveBtcStatus to ckbtc minter canister. +- Add retrieveBtcStatusV2ByAccount to ckbtc minter canister. - Make uint8ArrayToHexString also accept `number[]` as input. - Add a new type TokenAmountV2 which supports `decimals !== 8`. - Fix issue when converting token amount from small numbers with `TokenAmountV2`. From ec445978cc0016a57b0773523b4d3a37d9bfdba7 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:31:52 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=A4=96=20Documentation=20auto-update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ckbtc/README.md | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/ckbtc/README.md b/packages/ckbtc/README.md index f736642c..3c42bbe2 100644 --- a/packages/ckbtc/README.md +++ b/packages/ckbtc/README.md @@ -78,7 +78,7 @@ Parameters: ### :factory: CkBTCMinterCanister -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L36) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L38) #### Methods @@ -89,6 +89,7 @@ Parameters: - [retrieveBtc](#gear-retrievebtc) - [retrieveBtcWithApproval](#gear-retrievebtcwithapproval) - [retrieveBtcStatus](#gear-retrievebtcstatus) +- [retrieveBtcStatusV2ByAccount](#gear-retrievebtcstatusv2byaccount) - [estimateWithdrawalFee](#gear-estimatewithdrawalfee) - [getMinterInfo](#gear-getminterinfo) @@ -98,7 +99,7 @@ Parameters: | -------- | ------------------------------------------------------------------------ | | `create` | `(options: CkBTCMinterCanisterOptions<_SERVICE>) => CkBTCMinterCanister` | -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L37) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L39) ##### :gear: getBtcAddress @@ -116,7 +117,7 @@ Parameters: - `params.owner`: The owner for which the BTC address should be generated. If not provided, the `caller` will be use instead. - `params.subaccount`: An optional subaccount to compute the address. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L58) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L60) ##### :gear: updateBalance @@ -134,7 +135,7 @@ Parameters: - `params.owner`: The owner of the address. If not provided, the `caller` will be use instead. - `params.subaccount`: An optional subaccount of the address. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L77) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L79) ##### :gear: getWithdrawalAccount @@ -144,7 +145,7 @@ Returns the account to which the caller should deposit ckBTC before withdrawing | ---------------------- | ------------------------ | | `getWithdrawalAccount` | `() => Promise` | -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L100) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L102) ##### :gear: retrieveBtc @@ -168,7 +169,7 @@ Parameters: - `params.address`: The bitcoin address. - `params.amount`: The ckBTC amount. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L119) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L121) ##### :gear: retrieveBtcWithApproval @@ -194,7 +195,7 @@ Parameters: - `params.fromSubaccount`: An optional subaccount from which the ckBTC should be transferred. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L149) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L151) ##### :gear: retrieveBtcStatus @@ -210,7 +211,22 @@ Parameters: - `transactionId`: The ID of the corresponding burn transaction. - `certified`: query or update call -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L180) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L182) + +##### :gear: retrieveBtcStatusV2ByAccount + +Returns the status of all BTC withdrawals for the user's main account. + +| Method | Type | +| ------------------------------ | ----------------------------------------------------------------------------------- | +| `retrieveBtcStatusV2ByAccount` | `({ certified, }: { certified: boolean; }) => Promise` | + +Parameters: + +- `transactionId`: The ID of the corresponding burn transaction. +- `certified`: query or update call + +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L199) ##### :gear: estimateWithdrawalFee @@ -226,7 +242,7 @@ Parameters: - `params.certified`: query or update call - `params.amount`: The optional amount for which the fee should be estimated. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L198) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L220) ##### :gear: getMinterInfo @@ -241,7 +257,7 @@ Parameters: - `params`: The parameters to get the deposit fee. - `params.certified`: query or update call -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L212) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L234) From 36a69dc52c3d76a339e2f57c2684dc11c6b583fa Mon Sep 17 00:00:00 2001 From: David de Kloet Date: Thu, 14 Dec 2023 10:19:31 +0100 Subject: [PATCH 4/6] Add return type --- packages/ckbtc/src/minter.canister.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/ckbtc/src/minter.canister.ts b/packages/ckbtc/src/minter.canister.ts index 20e129f4..1a84b2f1 100644 --- a/packages/ckbtc/src/minter.canister.ts +++ b/packages/ckbtc/src/minter.canister.ts @@ -178,6 +178,7 @@ export class CkBTCMinterCanister extends Canister { * * @param {bigint} transactionId The ID of the corresponding burn transaction. * @param {boolean} certified query or update call + * @returns {Promise} The status of the BTC retrieval request. */ retrieveBtcStatus = async ({ transactionId, @@ -193,8 +194,8 @@ export class CkBTCMinterCanister extends Canister { /** * Returns the status of all BTC withdrawals for the user's main account. * - * @param {bigint} transactionId The ID of the corresponding burn transaction. * @param {boolean} certified query or update call + * @returns {Promise} The statuses of the BTC retrieval requests. */ retrieveBtcStatusV2ByAccount = async ({ certified, From dbacecedbc2a0461959121bb204d3c6b7dc300c3 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 14 Dec 2023 09:20:53 +0000 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=A4=96=20Documentation=20auto-update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ckbtc/README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/ckbtc/README.md b/packages/ckbtc/README.md index 3c42bbe2..b9891d33 100644 --- a/packages/ckbtc/README.md +++ b/packages/ckbtc/README.md @@ -211,7 +211,7 @@ Parameters: - `transactionId`: The ID of the corresponding burn transaction. - `certified`: query or update call -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L182) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L183) ##### :gear: retrieveBtcStatusV2ByAccount @@ -223,10 +223,9 @@ Returns the status of all BTC withdrawals for the user's main account. Parameters: -- `transactionId`: The ID of the corresponding burn transaction. - `certified`: query or update call -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L199) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L200) ##### :gear: estimateWithdrawalFee @@ -242,7 +241,7 @@ Parameters: - `params.certified`: query or update call - `params.amount`: The optional amount for which the fee should be estimated. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L220) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L221) ##### :gear: getMinterInfo @@ -257,7 +256,7 @@ Parameters: - `params`: The parameters to get the deposit fee. - `params.certified`: query or update call -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L234) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ckbtc/src/minter.canister.ts#L235) From a461c3ed688cfd1c22d15b0fea67d16a8076f976 Mon Sep 17 00:00:00 2001 From: David de Kloet Date: Thu, 14 Dec 2023 10:24:16 +0100 Subject: [PATCH 6/6] empty