Skip to content

Commit

Permalink
icrc index getLedgerId (#693)
Browse files Browse the repository at this point in the history
# Motivation

For the import token functionality, we need to ensure that the provided
by users index canister is correctly associated with the same token and
not mistakenly linked to a different ledger canister. To achieve this,
we going to use the `ledger_id` API of the index canister, which returns
the corresponding ledger canister ID. This PR introduces a function that
makes the `ledger_id` request.

# Changes

- Add getLedgerId function.

# Tests

- Test for getLedgerId.
- Tested manually in nns-dapp against localhost.

# Todos

- [ ] Add entry to changelog (if necessary).
Not necessary.

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
mstrasinskis and github-actions[bot] authored Aug 10, 2024
1 parent 706bf1c commit d8c72d2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
17 changes: 14 additions & 3 deletions packages/ledger-icrc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,21 @@ 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

| Method | Type |
| -------- | --------------------------------------------------------------------- |
| `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

Expand All @@ -303,7 +304,17 @@ Index Canister only holds the transactions ids in state, not the whole transacti
| ----------------- | -------------------------------------------------------------------- |
| `getTransactions` | `(params: GetAccountTransactionsParams) => Promise<GetTransactions>` |

[: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<Principal>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ledger-icrc/src/index.canister.ts#L51)

### :factory: IcrcIndexNgCanister

Expand Down
20 changes: 19 additions & 1 deletion packages/ledger-icrc/src/index.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -91,4 +91,22 @@ describe("Index canister", () => {
expect(call).rejects.toThrowError(IndexError);
});
});

describe("ledgerId", () => {
it("should return ledger id", async () => {
const service = mock<ActorSubclass<IcrcIndexService>>();
service.ledger_id.mockResolvedValue(ledgerCanisterIdMock);

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

const result = await canister.ledgerId({
certified: true,
});

expect(result).toEqual(ledgerCanisterIdMock);
});
});
});
11 changes: 10 additions & 1 deletion packages/ledger-icrc/src/index.canister.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -43,4 +44,12 @@ export class IcrcIndexCanister extends Canister<IcrcIndexService> {

return response.Ok;
};

/**
* Returns the ledger canister ID related to the index canister.
*/
ledgerId = (params: QueryParams): Promise<Principal> => {
const { ledger_id } = this.caller(params);
return ledger_id();
};
}

0 comments on commit d8c72d2

Please sign in to comment.