Skip to content
Merged
43 changes: 1 addition & 42 deletions packages/ledger-icrc/src/ledger.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { arrayOfNumberToUint8Array, toNullable } from "@dfinity/utils";
import { toNullable } from "@dfinity/utils";
import type { ActorSubclass } from "@icp-sdk/core/agent";
import { Principal } from "@icp-sdk/core/principal";
import { mock } from "vitest-mock-extended";
Expand Down Expand Up @@ -87,47 +87,6 @@ describe("Ledger canister", () => {
});
});

describe("balance", () => {
it("should return the balance of main account", async () => {
const service = mock<ActorSubclass<IcrcLedgerService>>();
const balance = BigInt(100);
service.icrc1_balance_of.mockResolvedValue(balance);

const canister = IcrcLedgerCanister.create({
canisterId: ledgerCanisterIdMock,
certifiedServiceOverride: service,
});

const owner = Principal.fromText("aaaaa-aa");
const res = await canister.balance({
owner,
});

expect(service.icrc1_balance_of).toHaveBeenCalled();
expect(res).toEqual(balance);
});

it("should return the balance of subaccount", async () => {
const service = mock<ActorSubclass<IcrcLedgerService>>();
const balance = BigInt(100);
service.icrc1_balance_of.mockResolvedValue(balance);

const canister = IcrcLedgerCanister.create({
canisterId: ledgerCanisterIdMock,
certifiedServiceOverride: service,
});

const owner = Principal.fromText("aaaaa-aa");
const subaccount = arrayOfNumberToUint8Array([0, 0, 1]);
const res = await canister.balance({
owner,
subaccount,
});

expect(res).toEqual(balance);
});
});

describe("transfer", () => {
const transferParams: TransferParams = {
to: {
Expand Down
18 changes: 2 additions & 16 deletions packages/ledger-icrc/src/ledger.canister.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
Canister,
createServices,
toNullable,
type Nullable,
type QueryParams,
} from "@dfinity/utils";
Expand All @@ -18,6 +16,7 @@ import type {
} from "./candid/icrc_ledger";
import { idlFactory as certifiedIdlFactory } from "./candid/icrc_ledger.certified.idl";
import { idlFactory } from "./candid/icrc_ledger.idl";
import { IcrcCanister } from "./canister";
import {
toApproveArgs,
toIcrc21ConsentMessageArgs,
Expand All @@ -33,15 +32,14 @@ import type { IcrcLedgerCanisterOptions } from "./types/canister.options";
import type {
AllowanceParams,
ApproveParams,
BalanceParams,
GetBlocksParams,
Icrc21ConsentMessageParams,
TransferFromParams,
TransferParams,
} from "./types/ledger.params";
import type { IcrcTokenMetadataResponse } from "./types/ledger.responses";

export class IcrcLedgerCanister extends Canister<IcrcLedgerService> {
export class IcrcLedgerCanister extends IcrcCanister<IcrcLedgerService> {
static create(options: IcrcLedgerCanisterOptions<IcrcLedgerService>) {
const { service, certifiedService, canisterId } =
createServices<IcrcLedgerService>({
Expand All @@ -67,18 +65,6 @@ export class IcrcLedgerCanister extends Canister<IcrcLedgerService> {
transactionFee = (params: QueryParams): Promise<Tokens> =>
this.caller(params).icrc1_fee();

/**
* Returns the balance for a given account provided as owner and with optional subaccount.
*
* @param {BalanceParams} params The parameters to get the balance of an account.
* @returns {Promise<Tokens>} The balance of the given account.
*/
balance = (params: BalanceParams): Promise<Tokens> =>
this.caller({ certified: params.certified }).icrc1_balance_of({
owner: params.owner,
subaccount: toNullable(params.subaccount),
});

/**
* Transfers tokens from the sender to the given account.
*
Expand Down