Skip to content

Commit

Permalink
fix: cardano decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
paulclindo committed Sep 19, 2023
1 parent dfc4a91 commit f209833
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
35 changes: 35 additions & 0 deletions packages/milkomeda-wsc/src/CardanoPendingManger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,27 @@ export interface MempoolTx {
}>;
}

export type CardanoAssetDetail = {
asset: string;
policy_id: string;
asset_name: string;
fingerprint: string;
quantity: string;
initial_mint_tx_hash: string;
mint_or_burn_count: number;
onchain_metadata: null;
onchain_metadata_standard: null;
onchain_metadata_extra: null;
metadata?: {
name: string;
description: string;
ticker: string;
url: string;
logo: string;
decimals: number;
};
};

class CardanoPendingManager extends PendingManager implements IPendingManager {
blockfrost: Blockfrost;

Expand Down Expand Up @@ -359,6 +380,20 @@ class CardanoPendingManager extends PendingManager implements IPendingManager {
const tx: MempoolTx = await response.json();
return tx;
}

async fetchCardanoAssetDetails(assetId: string): Promise<CardanoAssetDetail> {
const url = this.blockfrost.url + "/assets/" + assetId;
const response = await fetch(url, {
headers: {
project_id: this.blockfrost.projectId,
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const tokenInfo = await response.json();
return tokenInfo;
}
}

export default CardanoPendingManager;
15 changes: 12 additions & 3 deletions packages/milkomeda-wsc/src/wscLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ export class WSCLib {
const stargateObj: ADAStargateApiResponse = await response.json();
const assets = stargateObj.assets;

const userL1Address = await this.origin_getAddress();
const evmAddress = await this.eth_getAccount();
const pendingMngr = new CardanoPendingManager(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.blockfrost!,
this.network,
userL1Address,
evmAddress
);

for (const asset of assets) {
asset.fingerprint = await getFingerprintFromBridge(asset.idCardano);
}
Expand All @@ -380,9 +390,8 @@ export class WSCLib {
token.fingerprint = await getFingerprintFromBlockfrost(token.unit);
token.assetName = await assetNameFromBlockfrostId(token.unit);
token.bridgeAllowed = assets.some((asset) => token.fingerprint === asset.fingerprint);
token.decimals = assets.filter(
(asset) => token.fingerprint === asset.fingerprint
)[0]?.milkomedaDecimals;
token.decimals =
(await pendingMngr.fetchCardanoAssetDetails(token.unit)).metadata?.decimals ?? 0;
}

return tokens;
Expand Down

0 comments on commit f209833

Please sign in to comment.