Skip to content

Commit

Permalink
move imported tokens to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrasinskis committed Jul 25, 2024
1 parent d914ee4 commit 2d4c3be
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 38 deletions.
32 changes: 0 additions & 32 deletions frontend/src/lib/api/canisters.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
} from "$lib/canisters/nns-dapp/nns-dapp.errors";
import type {
CanisterDetails as CanisterInfo,
ImportedToken,
ImportedTokens,
SubAccountArray,
} from "$lib/canisters/nns-dapp/nns-dapp.types";
import {
Expand Down Expand Up @@ -378,33 +376,3 @@ const canisters = async (

return { cmc, icMgt, nnsDapp };
};

export const getImportedTokens = async ({
identity,
}: {
identity: Identity;
}): Promise<ImportedTokens> => {
logWithTimestamp("Getting imported tokens call...");
const { nnsDapp } = await canisters(identity);

const importedTokens = await nnsDapp.getImportedTokens();

logWithTimestamp("Getting imported tokens call complete.");

return importedTokens;
};

export const setImportedTokens = async ({
identity,
importedTokens,
}: {
identity: Identity;
importedTokens: Array<ImportedToken>;
}): Promise<void> => {
logWithTimestamp("Setting imported tokens call...");
const { nnsDapp } = await canisters(identity);

await nnsDapp.setImportedTokens(importedTokens);

logWithTimestamp("Setting imported tokens call complete.");
};
39 changes: 39 additions & 0 deletions frontend/src/lib/api/imported-tokens.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { nnsDappCanister } from "$lib/api/nns-dapp.api";
import type {
ImportedToken,
ImportedTokens,
} from "$lib/canisters/nns-dapp/nns-dapp.types";
import { logWithTimestamp } from "$lib/utils/dev.utils";
import type { Identity } from "@dfinity/agent";

export const getImportedTokens = async ({
identity,
certified,
}: {
identity: Identity;
certified: boolean;
}): Promise<ImportedTokens> => {
logWithTimestamp(`Getting imported tokens:${certified} call...`);

const { canister: nnsDapp } = await nnsDappCanister({ identity });
const response = await nnsDapp.getImportedTokens({ certified });

logWithTimestamp(`Getting imported tokens:${certified} complete`);

return response;
};

export const setImportedTokens = async ({
identity,
importedTokens,
}: {
identity: Identity;
importedTokens: Array<ImportedToken>;
}): Promise<void> => {
logWithTimestamp("Setting imported tokens call...");

const { canister: nnsDapp } = await nnsDappCanister({ identity });
await nnsDapp.setImportedTokens(importedTokens);

logWithTimestamp("Setting imported tokens call complete.");
};
9 changes: 7 additions & 2 deletions frontend/src/lib/canisters/nns-dapp/nns-dapp.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,13 @@ export class NNSDappCanister {
);
}

public getImportedTokens = async (): Promise<ImportedTokens> => {
const response = await this.certifiedService.get_imported_tokens();
public getImportedTokens = async ({
certified,
}: {
certified: boolean;
}): Promise<ImportedTokens> => {
const response =
await this.getNNSDappService(certified).get_imported_tokens();
if ("Ok" in response) {
return response.Ok;
}
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/tests/lib/api/canisters.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,13 @@ describe("canisters-api", () => {
expect(mockNNSDappCanister.getImportedTokens).not.toBeCalled();
await getImportedTokens({
identity: mockIdentity,
certified: true,
});

expect(mockNNSDappCanister.getImportedTokens).toBeCalledTimes(1);
expect(mockNNSDappCanister.getImportedTokens).toBeCalledWith({
certified: true,
});
});
});

Expand Down
11 changes: 7 additions & 4 deletions frontend/src/tests/lib/canisters/nns-dapp.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,12 @@ describe("NNSDapp", () => {

expect(service.get_imported_tokens).not.toBeCalled();

await nnsDapp.getImportedTokens();
await nnsDapp.getImportedTokens({ certified: true });

expect(service.get_imported_tokens).toBeCalledTimes(1);
expect(service.get_imported_tokens).toBeCalledWith({
certified: true,
});
});

it("should return imported tokens", async () => {
Expand All @@ -502,7 +505,7 @@ describe("NNSDapp", () => {
},
});
const nnsDapp = await createNnsDapp(service);
const result = await nnsDapp.getImportedTokens();
const result = await nnsDapp.getImportedTokens({ certified: true });

expect(result).toEqual({
imported_tokens: [mockImportedToken],
Expand All @@ -518,7 +521,7 @@ describe("NNSDapp", () => {

const nnsDapp = await createNnsDapp(service);

const call = async () => nnsDapp.getImportedTokens();
const call = async () => nnsDapp.getImportedTokens({ certified: true });

await expect(call).rejects.toThrow(AccountNotFoundError);
});
Expand All @@ -534,7 +537,7 @@ describe("NNSDapp", () => {

const nnsDapp = await createNnsDapp(service);

const call = async () => nnsDapp.getImportedTokens();
const call = async () => nnsDapp.getImportedTokens({ certified: true });

await expect(call).rejects.toThrow(
'Error getting imported tokens {"UnexpectedError":"message"}'
Expand Down

0 comments on commit 2d4c3be

Please sign in to comment.