-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: created PayWithAsset provider and integrated with transfer form
- Loading branch information
1 parent
79a0e47
commit 76b650b
Showing
10 changed files
with
176 additions
and
37 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
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
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2017-2025 @polkadot/react-hooks authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { BN } from '@polkadot/util'; | ||
import type { AssetInfoComplete } from '../types.js'; | ||
import type { PayWithAsset } from './types.js'; | ||
|
||
import React, { useCallback, useMemo, useState } from 'react'; | ||
|
||
import { useApi, useAssetIds, useAssetInfos } from '@polkadot/react-hooks'; | ||
import { formatNumber } from '@polkadot/util'; | ||
|
||
import { ALLOWED_CHAINS } from '../constants.js'; | ||
|
||
interface Props { | ||
children?: React.ReactNode; | ||
} | ||
|
||
const EMPTY_STATE: PayWithAsset = { | ||
assetOptions: [], | ||
isDisabled: true, | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
onChange: () => {}, | ||
selectedFeeAsset: null | ||
}; | ||
|
||
export const PayWithAssetCtx = React.createContext<PayWithAsset>(EMPTY_STATE); | ||
|
||
export function PayWithAssetCtxRoot ({ children }: Props): React.ReactElement<Props> { | ||
const { api } = useApi(); | ||
const ids = useAssetIds(); | ||
const assetInfos = useAssetInfos(ids); | ||
const [selectedFeeAsset, setSelectedFeeAsset] = useState<AssetInfoComplete | null>(null); | ||
|
||
const nativeAsset = useMemo( | ||
() => api.registry.chainTokens[0], | ||
[api] | ||
); | ||
|
||
const completeInfos = useMemo( | ||
() => (assetInfos | ||
?.filter((i): i is AssetInfoComplete => | ||
!!(i.details && i.metadata) && !i.details.supply.isZero() && !!i.details?.toJSON().isSufficient) | ||
.sort((a, b) => a.id.cmp(b.id))) || [], | ||
[assetInfos] | ||
); | ||
|
||
const assetOptions = useMemo( | ||
() => [ | ||
{ text: `${nativeAsset} (Native)`, value: nativeAsset }, | ||
...completeInfos.map(({ id, metadata }) => ({ | ||
text: `${metadata.name.toUtf8()} (${formatNumber(id)})`, | ||
value: id.toString() | ||
}))], | ||
[completeInfos, nativeAsset] | ||
); | ||
|
||
const onChange = useCallback((assetId: BN, cb?: () => void) => { | ||
const selectedFeeAsset = completeInfos.find((a) => a.id.toString() === assetId.toString()); | ||
|
||
setSelectedFeeAsset(selectedFeeAsset ?? null); | ||
cb?.(); | ||
}, [completeInfos]); | ||
|
||
const isDisabled = useMemo(() => !ALLOWED_CHAINS.includes(api.genesisHash.toHex()) || completeInfos.length === 0, [api.genesisHash, completeInfos.length]); | ||
|
||
const values: PayWithAsset = useMemo(() => { | ||
return { | ||
assetOptions, | ||
isDisabled, | ||
onChange, | ||
selectedFeeAsset | ||
}; | ||
}, [assetOptions, isDisabled, onChange, selectedFeeAsset]); | ||
|
||
return ( | ||
<PayWithAssetCtx.Provider value={values}> | ||
{children} | ||
</PayWithAssetCtx.Provider> | ||
); | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2017-2025 @polkadot/react-hooks authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { PayWithAsset } from './ctx/types.js'; | ||
|
||
import { useContext } from 'react'; | ||
|
||
import { PayWithAssetCtx } from './ctx/PayWithAsset.js'; | ||
import { createNamedHook } from './createNamedHook.js'; | ||
|
||
function usePayWithAssetImpl (): PayWithAsset { | ||
return useContext(PayWithAssetCtx); | ||
} | ||
|
||
export const usePayWithAsset = createNamedHook('usePayWithAsset', usePayWithAssetImpl); |
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,37 @@ | ||
// Copyright 2017-2025 @polkadot/react-hooks authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { ApiPromise } from '@polkadot/api'; | ||
import type { AnyNumber } from '@polkadot/types-codec/types'; | ||
import type { AssetInfoComplete } from '../types.js'; | ||
|
||
import { ALLOWED_CHAINS } from '../constants.js'; | ||
|
||
export const getFeeAssetLocation = (api: ApiPromise, selectedFeeAsset: AssetInfoComplete | null): AnyNumber | object | undefined => { | ||
const genesis = api.genesisHash.toHex(); | ||
|
||
if (!ALLOWED_CHAINS.includes(genesis) || !selectedFeeAsset) { | ||
return undefined; | ||
} | ||
|
||
switch (genesis) { | ||
case ALLOWED_CHAINS[0]: { | ||
return { | ||
interior: { | ||
X2: [ | ||
{ | ||
PalletInstance: 50 | ||
}, | ||
{ | ||
GeneralIndex: selectedFeeAsset.id.toString() | ||
} | ||
] | ||
}, | ||
parents: 0 | ||
}; | ||
} | ||
|
||
default: | ||
return undefined; | ||
} | ||
}; |