From 3ebe3fc3d209fab5e3729cc9a0b12b2a5afe84a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kihlberg?= Date: Tue, 29 Aug 2023 14:41:13 +0200 Subject: [PATCH 1/3] flake gonna flake --- flake.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flake.lock b/flake.lock index c88d867b..982ea36e 100644 --- a/flake.lock +++ b/flake.lock @@ -36,11 +36,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1692523566, - "narHash": "sha256-VDJDihK6jNebVw9y3qKCVD6+6QaC/x8kxZzL4MaIPPY=", + "lastModified": 1692793255, + "narHash": "sha256-yVyj0AE280JkccDHuG1XO9oGxN6bW8ksr/xttXcXzK0=", "owner": "numtide", "repo": "devshell", - "rev": "d208c58e2f7afef838add5f18a9936b12a71d695", + "rev": "2aa26972b951bc05c3632d4e5ae683cb6771a7c6", "type": "github" }, "original": { From 90135fe05b6110a6f76d4300706313f0d1a1a0e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kihlberg?= Date: Tue, 29 Aug 2023 14:41:47 +0200 Subject: [PATCH 2/3] software craftmanship --- packages/token-metadata-client/src/index.ts | 75 ++++++++++++++++----- 1 file changed, 57 insertions(+), 18 deletions(-) diff --git a/packages/token-metadata-client/src/index.ts b/packages/token-metadata-client/src/index.ts index 58689d93..aab5f323 100644 --- a/packages/token-metadata-client/src/index.ts +++ b/packages/token-metadata-client/src/index.ts @@ -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 => - 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 => { + 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}`; From 2d5e07f99a9e7e0a8645206193d84a06244e9aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kihlberg?= Date: Tue, 29 Aug 2023 15:19:47 +0200 Subject: [PATCH 3/3] add to build references --- tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 9726e96d..a5a31f70 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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" } ] } \ No newline at end of file