Skip to content

Commit

Permalink
feat: ckBTC min_confirmations renamed to minConfirmations (#703)
Browse files Browse the repository at this point in the history
# 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 <david.dalbusco@dfinity.org>
  • Loading branch information
peterpeterparker authored Aug 27, 2024
1 parent b2f76c9 commit 79675d0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 49 additions & 1 deletion packages/ckbtc/src/bitcoin.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("BitcoinCanister", () => {
describe("bitcoinGetUtxos", () => {
const params: Omit<GetUtxosParams, "certified"> = {
network: "testnet",
filter: { min_confirmations: 2 },
filter: { minConfirmations: 2 },
address: bitcoinAddressMock,
};

Expand Down Expand Up @@ -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<ActorSubclass<BitcoinService>>();
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<ActorSubclass<BitcoinService>>();
certifiedService.bitcoin_get_utxos.mockResolvedValue(response);

const { getUtxos } = await createBitcoinCanister({
certifiedServiceOverride: certifiedService,
});

const page = [1, 2, 3];
const pageParams: Omit<GetUtxosParams, "certified"> = {
...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<ActorSubclass<BitcoinService>>();
Expand Down
14 changes: 10 additions & 4 deletions packages/ckbtc/src/types/bitcoin.params.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -12,16 +12,22 @@ const mapBitcoinNetwork = (network: BitcoinNetwork): network =>

export type GetUtxosParams = Omit<get_utxos_request, "network" | "filter"> & {
network: BitcoinNetwork;
filter?: { page: Uint8Array | number[] } | { min_confirmations: number };
filter?: { page: Uint8Array | number[] } | { minConfirmations: number };
} & QueryParams;

export const toGetUtxosParams = ({
network,
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,
});

Expand Down

0 comments on commit 79675d0

Please sign in to comment.