Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FOLLOW-244: Add notifyTopUpCanister to CMC API #5852

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
});
});
});
});