Skip to content

Commit

Permalink
FOLLOW-244: Add notifyTopUpCanister to CMC API
Browse files Browse the repository at this point in the history
  • Loading branch information
dskloetd committed Nov 25, 2024
1 parent 5922fae commit 35b4a1f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
27 changes: 27 additions & 0 deletions frontend/src/lib/api/canisters.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,33 @@ export const topUpCanister = async ({
logWithTimestamp(`Topping up canister ${canisterId.toText()} complete.`);
};

// Returns the number of cycles that were topped up. But if called again, it
// will return the same number. So this can't be used to tell if the top-up
// wasn't done before.
export const notifyTopUpCanister = async ({
identity,
blockHeight,
canisterId,
}: {
identity: Identity;
blockHeight: bigint;
canisterId: Principal;
}): Promise<bigint> => {
logWithTimestamp(`Notifying canister topup ${canisterId.toText()} call...`);

const { cmc } = await canisters(identity);
try {
return cmc.notifyTopUp({
canister_id: canisterId,
block_index: blockHeight,
});
} finally {
logWithTimestamp(
`Notifying canister topup ${canisterId.toText()} complete.`
);
}
};

const canisters = async (
identity: Identity
): Promise<{
Expand Down
29 changes: 28 additions & 1 deletion frontend/src/tests/lib/api/canisters.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
createCanister,
detachCanister,
getIcpToCyclesExchangeRate,
notifyTopUpCanister,
queryCanisterDetails,
queryCanisters,
renameCanister,
Expand Down Expand Up @@ -31,6 +32,7 @@ import {
LedgerCanister,
SubAccount,
} from "@dfinity/ledger-icp";
import { Principal } from "@dfinity/principal";
import * as dfinityUtils from "@dfinity/utils";
import { principalToSubAccount } from "@dfinity/utils";
import { mock } from "vitest-mock-extended";
Expand Down Expand Up @@ -422,7 +424,6 @@ describe("canisters-api", () => {
});

it("should not notify if transfer fails", async () => {
vi.spyOn(console, "error").mockImplementation(() => undefined);
mockLedgerCanister.transfer.mockRejectedValue(new Error());

const call = () =>
Expand All @@ -436,4 +437,30 @@ describe("canisters-api", () => {
expect(mockCMCCanister.notifyTopUp).not.toBeCalled();
});
});

describe("notifyTopUpCanister", () => {
const canisterId = Principal.fromText("mkam6-f4aaa-aaaaa-qablq-cai");

it("should call cmc.notifyTopUp", async () => {
const cycles = 3_000_000_000_000n;
const blockHeight = 14545n;

mockCMCCanister.notifyTopUp.mockResolvedValue(cycles);

const call = () =>
notifyTopUpCanister({
identity: mockIdentity,
blockHeight,
canisterId,
});

await expect(call()).resolves.toBe(cycles);

expect(mockCMCCanister.notifyTopUp).toBeCalledTimes(1);
expect(mockCMCCanister.notifyTopUp).toBeCalledWith({
canister_id: canisterId,
block_index: blockHeight,
});
});
});
});

0 comments on commit 35b4a1f

Please sign in to comment.