From dacfaad08305cdd1096c511c4d7217da8fe3c0cf Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Tue, 19 Mar 2024 23:55:13 +1300 Subject: [PATCH 1/2] fix Hydra decimals as Options --- .../src/polkadot/PolkadotService.interfaces.ts | 4 ++-- packages/sdk/src/polkadot/PolkadotService.ts | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) 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 { From eb9b0c8c72f1ae1cf16e0c7791a341e550c413d9 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Wed, 20 Mar 2024 00:33:32 +1300 Subject: [PATCH 2/2] add changeset --- .changeset/rare-bikes-return.md | 5 +++++ 1 file changed, 5 insertions(+) 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