From bf0e917e64372b7a9bb7782ff8bc8ea69149bd36 Mon Sep 17 00:00:00 2001 From: mmaurello <93129175+mmaurello@users.noreply.github.com> Date: Wed, 20 Mar 2024 02:03:48 +1300 Subject: [PATCH] fix Hydra decimals as Options (#220) * fix Hydra decimals as Options * add changeset --- .changeset/rare-bikes-return.md | 5 +++++ .../src/polkadot/PolkadotService.interfaces.ts | 4 ++-- packages/sdk/src/polkadot/PolkadotService.ts | 17 +++++++++++++---- 3 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 .changeset/rare-bikes-return.md diff --git a/.changeset/rare-bikes-return.md b/.changeset/rare-bikes-return.md new file mode 100644 index 00000000..e5288dd2 --- /dev/null +++ b/.changeset/rare-bikes-return.md @@ -0,0 +1,5 @@ +--- +'@moonbeam-network/xcm-sdk': patch +--- + +Fix decimal fetching for HydraDX diff --git a/packages/sdk/src/polkadot/PolkadotService.interfaces.ts b/packages/sdk/src/polkadot/PolkadotService.interfaces.ts index f8d5044f..d0e24a1a 100644 --- a/packages/sdk/src/polkadot/PolkadotService.interfaces.ts +++ b/packages/sdk/src/polkadot/PolkadotService.interfaces.ts @@ -1,7 +1,7 @@ -import { Bytes, Struct, u8 } from '@polkadot/types'; +import { Bytes, Option, Struct, u8 } from '@polkadot/types'; export interface AssetMetadata extends Struct { readonly name: Bytes; readonly symbol: Bytes; - readonly decimals: u8; + readonly decimals: u8 | Option; } diff --git a/packages/sdk/src/polkadot/PolkadotService.ts b/packages/sdk/src/polkadot/PolkadotService.ts index 837f5846..7a1ba476 100644 --- a/packages/sdk/src/polkadot/PolkadotService.ts +++ b/packages/sdk/src/polkadot/PolkadotService.ts @@ -125,9 +125,13 @@ export class PolkadotService { const data = (await fn(asset)) as AssetMetadata | Option; const unwrapped = 'unwrapOrDefault' in data ? data.unwrapOrDefault() : data; + const decimals = + 'unwrapOrDefault' in unwrapped.decimals + ? unwrapped.decimals.unwrapOrDefault() + : unwrapped.decimals; return { - decimals: unwrapped.decimals.toNumber(), + decimals: decimals.toNumber(), symbol: unwrapped.symbol.toString(), }; } @@ -149,12 +153,17 @@ export class PolkadotService { async getAssetDecimals(asset: Asset): Promise { const metaId = this.chain.getMetadataAssetId(asset); - return ( + const decimals = this.chain.getAssetDecimals(asset) || (await this.getAssetDecimalsFromQuery(metaId)) || (await this.getAssetMeta(metaId))?.decimals || - this.decimals - ); + this.decimals; // TODO remove this and handle it separately only for native assets + + if (!decimals) { + throw new Error(`No decimals found for asset ${asset.originSymbol}`); + } + + return decimals; } async query(config: SubstrateQueryConfig): Promise {