Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: utilize ICP Index accountBalance and getTransactions with account identifier hex directly #501

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
- Add a new type TokenAmountV2 which supports `decimals !== 8`.
- Fix issue when converting token amount from small numbers with `TokenAmountV2`.

## Fix

- Utilize ICP Index `accountBalance` and `getTransactions` with account identifier hex directly, eliminating the need for conversion with Buffer and resolving usage in non-polyfilled environments.

## Operations

- Add a cron job to periodically update IC candid files and typescript bindings.
Expand Down
6 changes: 3 additions & 3 deletions packages/ledger-icp/src/index.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { MAINNET_INDEX_CANISTER_ID } from "./constants/canister_ids";
import { IndexError } from "./errors/index.errors";
import type { GetTransactionsParams } from "./types/index.params";
import type { AccountBalanceParams } from "./types/ledger.params";
import { paramToAccountIdentifier } from "./utils/params.utils";
import { paramToAccountIdentifierHex } from "./utils/params.utils";

export class IndexCanister extends Canister<IndexService> {
static create({
Expand Down Expand Up @@ -47,7 +47,7 @@ export class IndexCanister extends Canister<IndexService> {
accountIdentifier,
}: AccountBalanceParams): Promise<bigint> =>
this.caller({ certified }).get_account_identifier_balance(
paramToAccountIdentifier(accountIdentifier).toHex(),
paramToAccountIdentifierHex(accountIdentifier),
);

/**
Expand All @@ -70,7 +70,7 @@ export class IndexCanister extends Canister<IndexService> {
const response = await this.caller({
certified,
}).get_account_identifier_transactions({
account_identifier: paramToAccountIdentifier(accountIdentifier).toHex(),
account_identifier: paramToAccountIdentifierHex(accountIdentifier),
start: toNullable(start),
max_results,
});
Expand Down
6 changes: 6 additions & 0 deletions packages/ledger-icp/src/utils/params.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { AccountIdentifier } from "../account_identifier";
import type { AccountIdentifierHex } from "../types/common";
import type { AccountIdentifierParam } from "../types/ledger.params";

export const paramToAccountIdentifier = (
param: AccountIdentifierParam,
): AccountIdentifier =>
param instanceof AccountIdentifier ? param : AccountIdentifier.fromHex(param);

export const paramToAccountIdentifierHex = (
param: AccountIdentifierParam,
): AccountIdentifierHex =>
param instanceof AccountIdentifier ? param.toHex() : param;
Loading