-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Fetch currencies from blockchain instead of using hardcoded me…
…tadata * fetch currencies from blockchain instead of using harcoded metadata * handle undefined blockchain data for /gas * implement useAssetRegistryMetadata hook
- Loading branch information
1 parent
7413027
commit 063e0d7
Showing
6 changed files
with
147 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useState, useEffect, useCallback } from 'preact/hooks'; | ||
import { useNodeInfoState } from '../NodeInfoProvider'; | ||
import { AssetId, OrmlTraitsAssetRegistryAssetMetadata } from './useBuyout/types'; | ||
import { StorageKey } from '@polkadot/types'; | ||
import { Codec } from '@polkadot/types-codec/types'; | ||
|
||
type CurrencyMetadataType = { | ||
decimals: string; | ||
name: string; | ||
symbol: string; | ||
// There are more coming, but are not used in this context | ||
}; | ||
|
||
interface UseAssetRegistryMetadata { | ||
getAssetMetadata: (assetId: AssetId) => Promise<OrmlTraitsAssetRegistryAssetMetadata | undefined>; | ||
getNativeAssetMetadata: () => Promise<OrmlTraitsAssetRegistryAssetMetadata | undefined>; | ||
} | ||
|
||
function convertToOrmlAssetRegistryAssetMetadata(metadata: [StorageKey, Codec]): OrmlTraitsAssetRegistryAssetMetadata { | ||
const { decimals, name, symbol } = metadata[1].toHuman() as CurrencyMetadataType; | ||
const assetId = (metadata[0].toHuman() as string[])[0] as string; | ||
|
||
return { | ||
metadata: { decimals: Number(decimals), name, symbol }, | ||
assetId, | ||
}; | ||
} | ||
|
||
export const useAssetRegistryMetadata = (): UseAssetRegistryMetadata => { | ||
const { api } = useNodeInfoState().state; | ||
const [metadataCache, setMetadataCache] = useState<OrmlTraitsAssetRegistryAssetMetadata[]>([]); | ||
|
||
const fetchMetadata = useCallback(async () => { | ||
if (api) { | ||
const fetchedMetadata = await api.query.assetRegistry.metadata.entries(); | ||
setMetadataCache(fetchedMetadata.map((m) => convertToOrmlAssetRegistryAssetMetadata(m))); | ||
} | ||
}, [api]); | ||
|
||
useEffect(() => { | ||
fetchMetadata().catch(console.error); | ||
}, [fetchMetadata]); | ||
|
||
const getAssetMetadata = useCallback( | ||
async (assetId: AssetId): Promise<OrmlTraitsAssetRegistryAssetMetadata | undefined> => | ||
metadataCache.find( | ||
// When JSON.stringify we check for the same object structure as OrmlTraitsAssetRegistryAssetMetadata.assetId's are really different | ||
(m: OrmlTraitsAssetRegistryAssetMetadata) => JSON.stringify(m.assetId) === JSON.stringify(assetId), | ||
), | ||
[metadataCache], | ||
); | ||
|
||
const getNativeAssetMetadata = useCallback(async () => { | ||
return getAssetMetadata('Native'); | ||
}, [getAssetMetadata]); | ||
|
||
return { getAssetMetadata, getNativeAssetMetadata }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import { SpacewalkPrimitivesAsset } from '@pendulum-chain/types/interfaces'; | ||
|
||
export type AssetId = | ||
| { | ||
XCM: number; | ||
} | ||
| string | ||
| SpacewalkPrimitivesAsset; | ||
|
||
export interface OrmlTraitsAssetRegistryAssetMetadata { | ||
metadata: { | ||
decimals: number; | ||
name: string; | ||
symbol: string; | ||
existentialDeposit: number; | ||
}; | ||
assetId: | ||
| { | ||
XCM: number; | ||
} | ||
| string; | ||
assetId: AssetId; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Skeleton } from '../../components/Skeleton'; | ||
|
||
export const GasSkeleton = () => ( | ||
<div className="w-full h-full flex justify-center items-center"> | ||
<div className="w-3/5"> | ||
{Array.from({ length: 12 }).map((_, index) => ( | ||
<Skeleton className="w-full h-8 mb-2" isLoading={true} key={index} /> | ||
))} | ||
</div> | ||
</div> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters