Skip to content

Commit

Permalink
Add imported tokens api
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrasinskis committed Jul 19, 2024
1 parent 837ad64 commit 1e4b8b4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
32 changes: 32 additions & 0 deletions frontend/src/lib/api/canisters.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ 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 @@ -376,3 +378,33 @@ 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.");
};
28 changes: 27 additions & 1 deletion frontend/src/lib/canisters/nns-dapp/nns-dapp.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {
AccountDetails,
CanisterDetails,
CreateSubAccountResponse,
GetAccountResponse,
GetAccountResponse, ImportedToken, ImportedTokens,
RegisterHardwareWalletRequest,
RegisterHardwareWalletResponse,
RenameSubAccountRequest,
Expand Down Expand Up @@ -325,4 +325,30 @@ export class NNSDappCanister {
errorText ?? (nonNullish(response) ? JSON.stringify(response) : undefined)
);
}

public getImportedTokens = async (): Promise<ImportedTokens> => {
const response = await this.certifiedService.get_imported_tokens();
if ("Ok" in response) {
return response.Ok;
}
if ("AccountNotFound" in response) {
throw new AccountNotFoundError("error__account.not_found");
}
// Edge case
throw new Error(`Error getting imported tokens ${JSON.stringify(response)}`);
};

public setImportedTokens = async (importedTokens: Array<ImportedToken>): Promise<void> => {
const response = await this.certifiedService.set_imported_tokens(
{ 'imported_tokens' : importedTokens }
);
if ("Ok" in response) {
return;
}
if ("AccountNotFound" in response) {
throw new AccountNotFoundError("error__account.not_found");
}
// Edge case
throw new Error(`Error setting imported tokens ${JSON.stringify(response)}`);
};
}

0 comments on commit 1e4b8b4

Please sign in to comment.