From 79675d0cc8a0896ca3cbd850a59529360e713dcf Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Tue, 27 Aug 2024 10:01:58 +0200 Subject: [PATCH] feat: ckBTC min_confirmations renamed to minConfirmations (#703) # Motivation As discussed in #701, let's rename `min_confirmations` parameter of `getUtxos` in ckBTC library to `minConfirmations`. # Changes - Parameter and mapping updated accordingly. --------- Signed-off-by: David Dal Busco --- CHANGELOG.md | 1 + packages/ckbtc/src/bitcoin.canister.spec.ts | 50 ++++++++++++++++++++- packages/ckbtc/src/types/bitcoin.params.ts | 14 ++++-- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1510b2d..f0fa9dfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Rename values of enum Topic and NnsFunction to match the backend values. - Use different request/response types for NNS Governance proposals, and different fields for `InstallCode` proposals. +- The `getUtxos` parameter `filter.min_confirmations` has been renamed to `filter.minConfirmations` for consistency with the general naming conventions used in `@dfinity/ckbtc`. ## Features diff --git a/packages/ckbtc/src/bitcoin.canister.spec.ts b/packages/ckbtc/src/bitcoin.canister.spec.ts index 04144ab9..2cd289dc 100644 --- a/packages/ckbtc/src/bitcoin.canister.spec.ts +++ b/packages/ckbtc/src/bitcoin.canister.spec.ts @@ -26,7 +26,7 @@ describe("BitcoinCanister", () => { describe("bitcoinGetUtxos", () => { const params: Omit = { network: "testnet", - filter: { min_confirmations: 2 }, + filter: { minConfirmations: 2 }, address: bitcoinAddressMock, }; @@ -80,6 +80,54 @@ describe("BitcoinCanister", () => { expect(service.bitcoin_get_utxos_query).not.toHaveBeenCalled(); }); + it("call get utxos with min_confirmations", async () => { + const certifiedService = mock>(); + certifiedService.bitcoin_get_utxos.mockResolvedValue(response); + + const { getUtxos } = await createBitcoinCanister({ + certifiedServiceOverride: certifiedService, + }); + + await getUtxos({ + ...params, + certified: true, + }); + + expect(certifiedService.bitcoin_get_utxos).toHaveBeenCalledWith({ + network: { testnet: null }, + filter: [{ min_confirmations: 2 }], + address: bitcoinAddressMock, + }); + }); + + it("call get utxos with page", async () => { + const certifiedService = mock>(); + certifiedService.bitcoin_get_utxos.mockResolvedValue(response); + + const { getUtxos } = await createBitcoinCanister({ + certifiedServiceOverride: certifiedService, + }); + + const page = [1, 2, 3]; + const pageParams: Omit = { + ...params, + filter: { + page, + }, + }; + + await getUtxos({ + ...pageParams, + certified: true, + }); + + expect(certifiedService.bitcoin_get_utxos).toHaveBeenCalledWith({ + network: { testnet: null }, + filter: [{ page }], + address: bitcoinAddressMock, + }); + }); + it("throws Error", async () => { const error = new Error("Test"); const certifiedService = mock>(); diff --git a/packages/ckbtc/src/types/bitcoin.params.ts b/packages/ckbtc/src/types/bitcoin.params.ts index 7a4d4104..162ae2fc 100644 --- a/packages/ckbtc/src/types/bitcoin.params.ts +++ b/packages/ckbtc/src/types/bitcoin.params.ts @@ -1,4 +1,4 @@ -import { toNullable, type QueryParams } from "@dfinity/utils"; +import { nonNullish, toNullable, type QueryParams } from "@dfinity/utils"; import type { get_balance_request, get_utxos_request, @@ -12,7 +12,7 @@ const mapBitcoinNetwork = (network: BitcoinNetwork): network => export type GetUtxosParams = Omit & { network: BitcoinNetwork; - filter?: { page: Uint8Array | number[] } | { min_confirmations: number }; + filter?: { page: Uint8Array | number[] } | { minConfirmations: number }; } & QueryParams; export const toGetUtxosParams = ({ @@ -20,8 +20,14 @@ export const toGetUtxosParams = ({ filter, ...rest }: GetUtxosParams): get_utxos_request => ({ - filter: toNullable(filter), - network: mapBitcoinNetwork(network), + filter: nonNullish(filter) + ? toNullable( + "minConfirmations" in filter + ? { min_confirmations: filter.minConfirmations } + : { page: filter.page }, + ) + : toNullable(), + network: network === "testnet" ? { testnet: null } : { mainnet: null }, ...rest, });