Skip to content

Commit

Permalink
fix Hydra decimals as Options (#220)
Browse files Browse the repository at this point in the history
* fix Hydra decimals as Options

* add changeset
  • Loading branch information
mmaurello authored Mar 19, 2024
1 parent 201d603 commit bf0e917
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-bikes-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moonbeam-network/xcm-sdk': patch
---

Fix decimal fetching for HydraDX
4 changes: 2 additions & 2 deletions packages/sdk/src/polkadot/PolkadotService.interfaces.ts
Original file line number Diff line number Diff line change
@@ -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<u8>;
}
17 changes: 13 additions & 4 deletions packages/sdk/src/polkadot/PolkadotService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ export class PolkadotService {

const data = (await fn(asset)) as AssetMetadata | Option<AssetMetadata>;
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(),
};
}
Expand All @@ -149,12 +153,17 @@ export class PolkadotService {
async getAssetDecimals(asset: Asset): Promise<number> {
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<bigint> {
Expand Down

0 comments on commit bf0e917

Please sign in to comment.