Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spike] Update PaymentInfo to use Assets pallet for fee asset if applicable #8324

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/apps-config/src/settings/feeAssets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2017-2022 @polkadot/apps-config authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { ChainSpecName, FeeAsset } from './types';

// A mapping of chains to their fee asset if different to the native token
export const feeAssets: Record<ChainSpecName, FeeAsset> = {
root: {
assetId: 2,
decimals: 6,
symbol: 'XRP'
}
};
1 change: 1 addition & 0 deletions packages/apps-config/src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

export * from './ethereumChains';
export * from './feeAssets';
export * from './languages';
export * from './ss58';
7 changes: 7 additions & 0 deletions packages/apps-config/src/settings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ export interface LinkOption extends Option {
paraId?: number;
textBy: string;
}

export type ChainSpecName = string;
export interface FeeAsset {
assetId: number;
symbol: string;
decimals: number;
}
1 change: 1 addition & 0 deletions packages/react-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export { useEventChanges } from './useEventChanges';
export { useEventTrigger } from './useEventTrigger';
export { useExtrinsicTrigger } from './useExtrinsicTrigger';
export { useFavorites } from './useFavorites';
export { useFeeAssetBalance } from './useFeeAssetBalance';
export { useFormField } from './useFormField';
export { useIncrement } from './useIncrement';
export { useInflation } from './useInflation';
Expand Down
40 changes: 40 additions & 0 deletions packages/react-hooks/src/useFeeAssetBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2017-2022 @polkadot/react-signer authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { FeeAsset } from '@polkadot/apps-config/settings/types';
import type { Option } from '@polkadot/types';
import type { PalletAssetsAssetAccount } from '@polkadot/types/lookup';
import type { BN } from '@polkadot/util';

import { useEffect, useState } from 'react';

import { feeAssets } from '@polkadot/apps-config';
import { useApi, useCall } from '@polkadot/react-hooks';

export const useFeeAssetBalance = (accountId: string | null): [
FeeAsset,
BN | null
] => {
const { api } = useApi();
const [feeBalance, setFeeBalance] = useState<BN | null>(null);
const feeAsset = feeAssets[api.runtimeVersion.specName.toString()];
const assetsAccount = useCall<Option<PalletAssetsAssetAccount>>(api.query.assets?.account, [feeAsset?.assetId, accountId]);

useEffect(() => {
if (!assetsAccount) {
return;
}

if (assetsAccount.isSome) {
const { balance } = assetsAccount.unwrap();

setFeeBalance(balance);
}

if (assetsAccount.isNone) {
setFeeBalance(api.registry.createType('Balance', 0));
}
}, [api, assetsAccount]);

return [feeAsset, feeBalance];
};
11 changes: 7 additions & 4 deletions packages/react-signer/src/PaymentInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React, { useEffect, useState } from 'react';
import { Trans } from 'react-i18next';

import { Expander, MarkWarning } from '@polkadot/react-components';
import { useApi, useCall, useIsMountedRef } from '@polkadot/react-hooks';
import { useApi, useCall, useFeeAssetBalance, useIsMountedRef } from '@polkadot/react-hooks';
import { formatBalance, nextTick } from '@polkadot/util';

import { useTranslation } from './translate';
Expand All @@ -27,6 +27,7 @@ interface Props {
function PaymentInfo ({ accountId, className = '', extrinsic }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { api } = useApi();
const [feeAsset, feeAssetBalance] = useFeeAssetBalance(accountId);
const [dispatchInfo, setDispatchInfo] = useState<RuntimeDispatchInfo | null>(null);
const balances = useCall<DeriveBalancesAll>(api.derive.balances?.all, [accountId]);
const mountedRef = useIsMountedRef();
Expand All @@ -48,22 +49,24 @@ function PaymentInfo ({ accountId, className = '', extrinsic }: Props): React.Re
return null;
}

const isFeeError = api.consts.balances && !api.tx.balances?.transfer.is(extrinsic) && balances?.accountId.eq(accountId) && (
const isFeeError = !feeAsset && api.consts.balances && !api.tx.balances?.transfer.is(extrinsic) && balances?.accountId.eq(accountId) && (
balances.availableBalance.lte(dispatchInfo.partialFee) ||
balances.freeBalance.sub(dispatchInfo.partialFee).lte(api.consts.balances.existentialDeposit)
);

const isFeeAssetError = feeAsset && feeAssetBalance?.lte(dispatchInfo.partialFee);

return (
<>
<Expander
className={className}
summary={
<Trans i18nKey='feesForSubmission'>
Fees of <span className='highlight'>{formatBalance(dispatchInfo.partialFee, { withSiFull: true })}</span> will be applied to the submission
Fees of <span className='highlight'>{formatBalance(dispatchInfo.partialFee, { decimals: feeAsset?.decimals, withSiFull: true, withUnit: feeAsset?.symbol })}</span> will be applied to the submission
</Trans>
}
/>
{isFeeError && (
{(isFeeError || isFeeAssetError) && (
<MarkWarning content={t<string>('The account does not have enough free funds (excluding locked/bonded/reserved) available to cover the transaction fees without dropping the balance below the account existential amount.')} />
)}
</>
Expand Down