diff --git a/packages/ledger-icrc/README.md b/packages/ledger-icrc/README.md index b27dd447..b1a3a220 100644 --- a/packages/ledger-icrc/README.md +++ b/packages/ledger-icrc/README.md @@ -275,12 +275,13 @@ Parameters: ### :factory: IcrcIndexCanister -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ledger-icrc/src/index.canister.ts#L13) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ledger-icrc/src/index.canister.ts#L14) #### Methods - [create](#gear-create) - [getTransactions](#gear-gettransactions) +- [ledgerId](#gear-ledgerid) ##### :gear: create @@ -288,7 +289,7 @@ Parameters: | -------- | --------------------------------------------------------------------- | | `create` | `(options: IcrcLedgerCanisterOptions<_SERVICE>) => IcrcIndexCanister` | -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ledger-icrc/src/index.canister.ts#L14) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ledger-icrc/src/index.canister.ts#L15) ##### :gear: getTransactions @@ -303,7 +304,17 @@ Index Canister only holds the transactions ids in state, not the whole transacti | ----------------- | -------------------------------------------------------------------- | | `getTransactions` | `(params: GetAccountTransactionsParams) => Promise` | -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ledger-icrc/src/index.canister.ts#L33) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ledger-icrc/src/index.canister.ts#L34) + +##### :gear: ledgerId + +Returns the ledger canister ID related to the index canister. + +| Method | Type | +| ---------- | --------------------------------------------- | +| `ledgerId` | `(params: QueryParams) => Promise` | + +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ledger-icrc/src/index.canister.ts#L51) ### :factory: IcrcIndexNgCanister diff --git a/packages/ledger-icrc/src/index.canister.spec.ts b/packages/ledger-icrc/src/index.canister.spec.ts index 1d87311b..f07d9faa 100644 --- a/packages/ledger-icrc/src/index.canister.spec.ts +++ b/packages/ledger-icrc/src/index.canister.spec.ts @@ -8,7 +8,7 @@ import type { } from "../candid/icrc_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", () => { @@ -91,4 +91,22 @@ describe("Index canister", () => { expect(call).rejects.toThrowError(IndexError); }); }); + + describe("ledgerId", () => { + it("should return ledger id", async () => { + const service = mock>(); + service.ledger_id.mockResolvedValue(ledgerCanisterIdMock); + + const canister = IcrcIndexCanister.create({ + canisterId: indexCanisterIdMock, + certifiedServiceOverride: service, + }); + + const result = await canister.ledgerId({ + certified: true, + }); + + expect(result).toEqual(ledgerCanisterIdMock); + }); + }); }); diff --git a/packages/ledger-icrc/src/index.canister.ts b/packages/ledger-icrc/src/index.canister.ts index 895829de..619ff389 100644 --- a/packages/ledger-icrc/src/index.canister.ts +++ b/packages/ledger-icrc/src/index.canister.ts @@ -1,4 +1,5 @@ -import { Canister, createServices } from "@dfinity/utils"; +import type { Principal } from "@dfinity/principal"; +import { Canister, createServices, type QueryParams } from "@dfinity/utils"; import type { GetTransactions, _SERVICE as IcrcIndexService, @@ -43,4 +44,12 @@ export class IcrcIndexCanister extends Canister { return response.Ok; }; + + /** + * Returns the ledger canister ID related to the index canister. + */ + ledgerId = (params: QueryParams): Promise => { + const { ledger_id } = this.caller(params); + return ledger_id(); + }; }