Skip to content

Commit 6df4a88

Browse files
refactor(ledger): Extend class IcrcLedgerCanister with common IcrcCanister (#1288)
# Motivation Class `IcrcLedgerCanister` can actually extend abstract `IcrcCanister` (see PR #1285). And in fact, the method to fetch the balance is the exact same that is already developed in `IcrcCanister`. # Changes - Change the class extension of `IcrcLedgerCanister` from `Canister` to `IcrcCanister`. - Remove redundant `balance` method. # Tests No more tests needed, since the method `balance` is already tested for `IcrcCanister`.
1 parent a5cf8c3 commit 6df4a88

File tree

2 files changed

+3
-58
lines changed

2 files changed

+3
-58
lines changed

packages/ledger-icrc/src/ledger.canister.spec.ts

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { arrayOfNumberToUint8Array, toNullable } from "@dfinity/utils";
1+
import { toNullable } from "@dfinity/utils";
22
import type { ActorSubclass } from "@icp-sdk/core/agent";
33
import { Principal } from "@icp-sdk/core/principal";
44
import { mock } from "vitest-mock-extended";
@@ -87,47 +87,6 @@ describe("Ledger canister", () => {
8787
});
8888
});
8989

90-
describe("balance", () => {
91-
it("should return the balance of main account", async () => {
92-
const service = mock<ActorSubclass<IcrcLedgerService>>();
93-
const balance = BigInt(100);
94-
service.icrc1_balance_of.mockResolvedValue(balance);
95-
96-
const canister = IcrcLedgerCanister.create({
97-
canisterId: ledgerCanisterIdMock,
98-
certifiedServiceOverride: service,
99-
});
100-
101-
const owner = Principal.fromText("aaaaa-aa");
102-
const res = await canister.balance({
103-
owner,
104-
});
105-
106-
expect(service.icrc1_balance_of).toHaveBeenCalled();
107-
expect(res).toEqual(balance);
108-
});
109-
110-
it("should return the balance of subaccount", async () => {
111-
const service = mock<ActorSubclass<IcrcLedgerService>>();
112-
const balance = BigInt(100);
113-
service.icrc1_balance_of.mockResolvedValue(balance);
114-
115-
const canister = IcrcLedgerCanister.create({
116-
canisterId: ledgerCanisterIdMock,
117-
certifiedServiceOverride: service,
118-
});
119-
120-
const owner = Principal.fromText("aaaaa-aa");
121-
const subaccount = arrayOfNumberToUint8Array([0, 0, 1]);
122-
const res = await canister.balance({
123-
owner,
124-
subaccount,
125-
});
126-
127-
expect(res).toEqual(balance);
128-
});
129-
});
130-
13190
describe("transfer", () => {
13291
const transferParams: TransferParams = {
13392
to: {

packages/ledger-icrc/src/ledger.canister.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {
2-
Canister,
32
createServices,
4-
toNullable,
53
type Nullable,
64
type QueryParams,
75
} from "@dfinity/utils";
@@ -18,6 +16,7 @@ import type {
1816
} from "./candid/icrc_ledger";
1917
import { idlFactory as certifiedIdlFactory } from "./candid/icrc_ledger.certified.idl";
2018
import { idlFactory } from "./candid/icrc_ledger.idl";
19+
import { IcrcCanister } from "./canister";
2120
import {
2221
toApproveArgs,
2322
toIcrc21ConsentMessageArgs,
@@ -33,15 +32,14 @@ import type { IcrcLedgerCanisterOptions } from "./types/canister.options";
3332
import type {
3433
AllowanceParams,
3534
ApproveParams,
36-
BalanceParams,
3735
GetBlocksParams,
3836
Icrc21ConsentMessageParams,
3937
TransferFromParams,
4038
TransferParams,
4139
} from "./types/ledger.params";
4240
import type { IcrcTokenMetadataResponse } from "./types/ledger.responses";
4341

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

70-
/**
71-
* Returns the balance for a given account provided as owner and with optional subaccount.
72-
*
73-
* @param {BalanceParams} params The parameters to get the balance of an account.
74-
* @returns {Promise<Tokens>} The balance of the given account.
75-
*/
76-
balance = (params: BalanceParams): Promise<Tokens> =>
77-
this.caller({ certified: params.certified }).icrc1_balance_of({
78-
owner: params.owner,
79-
subaccount: toNullable(params.subaccount),
80-
});
81-
8268
/**
8369
* Transfers tokens from the sender to the given account.
8470
*

0 commit comments

Comments
 (0)