Skip to content

Commit

Permalink
Merge pull request #61 from Psychedelic/fix/getTokens-params
Browse files Browse the repository at this point in the history
fix: getTokens receive an objet
  • Loading branch information
0xAaCE authored Feb 15, 2022
2 parents a38b3cc + e52370c commit d4e5758
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@psychedelic/dab-js",
"version": "1.0.1",
"version": "1.0.2",
"description": "JS adapter for DAB",
"main": "dist/index.js",
"repository": {
Expand Down
46 changes: 28 additions & 18 deletions src/registries/token_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,46 @@ const CANISTER_ID = 'b7hhy-tyaaa-aaaah-abbja-cai';

const DEFAULT_AGENT = new HttpAgent({ fetch, host: IC_HOST });

export const TOKEN_STANDARDS = Object.values(TOKEN)
export const TOKEN_STANDARDS = Object.values(TOKEN);

interface GetTokenActorParams {
canisterId: string,
standard: string,
agent: HttpAgent
canisterId: string;
standard: string;
agent: HttpAgent;
}

export const getTokenActor = <T = {}>(
{ canisterId,
agent,
standard }: GetTokenActorParams
) => {
if (!(TOKEN_STANDARDS.includes(standard))) {
export const getTokenActor = <T = {}>({
canisterId,
agent,
standard,
}: GetTokenActorParams) => {
if (!TOKEN_STANDARDS.includes(standard)) {
console.error(`Standard ${standard} is not implemented`);
throw new Error(`standard is not supported: ${standard}`);
}
return createTokenActor<T>(canisterId, agent, standard)
return createTokenActor<T>(canisterId, agent, standard);
};

export class TokenRegistry extends Registry {
constructor(agent?: HttpAgent) {
super(CANISTER_ID, agent);
this.actor = generateActor({ agent: agent || DEFAULT_AGENT, canisterId: CANISTER_ID, IDL });
this.actor = generateActor({
agent: agent || DEFAULT_AGENT,
canisterId: CANISTER_ID,
IDL,
});
}
public getAll = async (): Promise<FormattedMetadata[]> => {
const tokenCanistersMetadata = await (this.actor as ActorSubclass<TokenRegistryInterface>).get_all();
const tokenCanistersMetadata = await (
this.actor as ActorSubclass<TokenRegistryInterface>
).get_all();
return tokenCanistersMetadata.map(formatMetadata);
}
};
}

export const getTokens = async (agent = DEFAULT_AGENT): Promise<Token[]> => {
export const getTokens = async ({ agent = DEFAULT_AGENT } = {}): Promise<
Token[]
> => {
const tokenRegistry = new TokenRegistry(agent);
const tokenCanisters = await tokenRegistry.getAll();
return tokenCanisters.map((token) => ({
Expand All @@ -59,15 +67,17 @@ export const getTokens = async (agent = DEFAULT_AGENT): Promise<Token[]> => {
website: token.frontend.length ? token.frontend[0] : '',
principal_id: token.principal_id,
standard: token.details.standard as string,
total_supply : [token.details.total_supply as bigint],
total_supply: [token.details.total_supply as bigint],
symbol: token.details.symbol as string,
}));
};

export default {
getTokenActor,
getTokens,
addToken: async ({ agent, tokenInfo }) => new TokenRegistry(agent).add(tokenInfo),
addToken: async ({ agent, tokenInfo }) =>
new TokenRegistry(agent).add(tokenInfo),
// editToken: async ({ agent, tokenInfo }) => new TokenRegistry(agent).edit(tokenInfo),
removeToken: async ({ agent, canisterId }) => new TokenRegistry(agent).remove(canisterId),
removeToken: async ({ agent, canisterId }) =>
new TokenRegistry(agent).remove(canisterId),
};

0 comments on commit d4e5758

Please sign in to comment.