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

PLT-7211: Connecting to token metadata registry #22

Merged
merged 3 commits into from
Aug 29, 2023
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
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 57 additions & 18 deletions packages/token-metadata-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,65 @@
export interface TokenMetadata {
precision: number;
symbol: string;
/** A human-readable name for the subject, suitable for use in an interface. */
name: string;
network: "mainnet" | "preview" | "preprod";
/** A human-readable description for the subject, suitable for use in an interface. */
description: string;
/** The base16-encoded CBOR representation of the monetary policy script, used to verify ownership. Optional in the case of Plutus scripts as verification is handled elsewhere. */
policy?: string;
/** A human-readable ticker name for the subject, suitable for use in an interface. */
ticker?: string;
/** A HTTPS URL (web page relating to the token). */
url?: string;
/** A PNG image file as a byte string. */
logo?: string;
/** How many decimals to the token. */
decimals?: number;
}

export const lookupTokenMetadata = (
interface TokenMetadataJSONResponse {
name: { value: string };
description: { value: string };
policy?: string;
ticker?: { value: string };
url?: { value: string };
logo?: { value: string };
decimals?: { value: number };
}

const cardano_foundation_server_url =
"https://raw.githubusercontent.com/cardano-foundation/cardano-token-registry/master/mappings/";
const iohk_server_url =
"https://raw.githubusercontent.com/input-output-hk/metadata-registry-testnet/master/registry/";

export const lookupTokenMetadata = async (
policyId: string,
tokenName: string
): Promise<TokenMetadata> =>
new Promise((resolve, reject) => {
if (policyId === "" && tokenName === "")
return resolve({
precision: 1e-6,
symbol: "₳",
name: "Ada",
network: "mainnet",
});
throw reject("not found");
});
assetName: string,
network: "mainnet" | "preview" | "preprod"
): Promise<TokenMetadata> => {
if (policyId === "" && assetName === "") {
return {
decimals: 6,
ticker: network === "mainnet" ? "₳" : "t₳",
name: "Ada",
description: "Cardano ADA",
};
} else {
const server_url =
network === "mainnet" ? cardano_foundation_server_url : iohk_server_url;
const response = await fetch(`${server_url}/${policyId + assetName}.json`);
const json: TokenMetadataJSONResponse = await response.json();
return {
name: json.name.value,
description: json.description.value,
policy: json.policy,
ticker: json.ticker?.value,
url: json.url?.value,
logo: json.logo?.value,
decimals: json.decimals?.value,
};
}
};

export const formatToken = (
{ precision, symbol }: TokenMetadata,
{ decimals, ticker, name }: TokenMetadata,
value: number
): string => `${value * precision} ${symbol}`;
): string => `${value * 10 ** -(decimals || 0)} ${ticker || name}`;
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{ "path": "./packages/wallet/src" },
{ "path": "./packages/runtime/client/rest/src" },
{ "path": "./packages/runtime/core/src" },
{ "path": "./packages/runtime/api/src" }
{ "path": "./packages/runtime/api/src" },
{ "path": "./packages/token-metadata-client/src" }
]
}