Skip to content

Commit

Permalink
feat: get balance for Index canister as well
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Jul 5, 2023
1 parent 3515fc2 commit 73ec769
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 18 deletions.
25 changes: 25 additions & 0 deletions packages/ledger/src/canister.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Canister } from "@dfinity/utils";
import { toNullable } from "@dfinity/utils/src";
import type { _SERVICE as IcrcIndexService } from "../candid/icrc1_index";
import type {
Tokens,
_SERVICE as IcrcLedgerService,
} from "../candid/icrc1_ledger";
import type { BalanceParams } from "./types/ledger.params";

export abstract class IcrcCanister<
T extends IcrcLedgerService | IcrcIndexService
> extends Canister<T> {

/**
* 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),
});
}
42 changes: 41 additions & 1 deletion packages/ledger/src/index.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ActorSubclass } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { arrayOfNumberToUint8Array } from "@dfinity/utils/src";
import { mock } from "jest-mock-extended";
import type {
Account,
Expand All @@ -8,7 +9,7 @@ import type {
} from "../candid/icrc1_index";
import { IndexError } from "./errors/index.errors";
import { IcrcIndexCanister } from "./index.canister";
import { indexCanisterIdMock } from "./mocks/ledger.mock";
import { indexCanisterIdMock, ledgerCanisterIdMock } from "./mocks/ledger.mock";
import { IcrcAccount } from "./types/ledger.responses";

describe("Index canister", () => {
Expand Down Expand Up @@ -85,4 +86,43 @@ describe("Index canister", () => {
expect(call).rejects.toThrowError(IndexError);
});
});

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

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

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

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

const canister = IcrcIndexCanister.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);
});
});
});
19 changes: 17 additions & 2 deletions packages/ledger/src/index.canister.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Canister, createServices } from "@dfinity/utils";
import { createServices, toNullable } from "@dfinity/utils";
import type {
GetTransactions,
Tokens,
_SERVICE as IcrcIndexService,
} from "../candid/icrc1_index";
import { idlFactory as certifiedIdlFactory } from "../candid/icrc1_index.certified.idl";
import { idlFactory } from "../candid/icrc1_index.idl";
import { IcrcCanister } from "./canister";
import { toGetTransactionsArgs } from "./converters/index.converters";
import { IndexError } from "./errors/index.errors";
import type { IcrcLedgerCanisterOptions } from "./types/canister.options";
import type { GetAccountTransactionsParams } from "./types/index.params";
import type { BalanceParams } from "./types/ledger.params";

export class IcrcIndexCanister extends Canister<IcrcIndexService> {
export class IcrcIndexCanister extends IcrcCanister<IcrcIndexService> {
static create(options: IcrcLedgerCanisterOptions<IcrcIndexService>) {
const { service, certifiedService, canisterId } =
createServices<IcrcIndexService>({
Expand Down Expand Up @@ -42,4 +45,16 @@ export class IcrcIndexCanister extends Canister<IcrcIndexService> {

return response.Ok;
};

/**
* 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),
});
}
19 changes: 4 additions & 15 deletions packages/ledger/src/ledger.canister.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import type { QueryParams } from "@dfinity/utils";
import { Canister, createServices, toNullable } from "@dfinity/utils";
import { createServices } from "@dfinity/utils";
import type {
BlockIndex,
Tokens,
_SERVICE as IcrcLedgerService,
} from "../candid/icrc1_ledger";
import { idlFactory as certifiedIdlFactory } from "../candid/icrc1_ledger.certified.idl";
import { idlFactory } from "../candid/icrc1_ledger.idl";
import { IcrcCanister } from "./canister";
import { toTransferArg } from "./converters/ledger.converters";
import { IcrcTransferError } from "./errors/ledger.errors";
import type { IcrcLedgerCanisterOptions } from "./types/canister.options";
import type { BalanceParams, TransferParams } from "./types/ledger.params";
import type { 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 @@ -39,18 +40,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

0 comments on commit 73ec769

Please sign in to comment.