Skip to content

Commit

Permalink
fix: delivery fee (#2878)
Browse files Browse the repository at this point in the history
  • Loading branch information
Asmadek authored Dec 19, 2024
1 parent 652e683 commit bdda454
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 54 deletions.
10 changes: 4 additions & 6 deletions src/renderer/entities/transaction/ui/XcmFee/XcmFee.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ export const XcmFee = memo(
return;
}

const originChainId = transaction.chainId;
const destinationChainId = transaction.args.destinationChain;
const configChain = config.chains.find((c) => c.chainId === toLocalChainId(originChainId));
const originChainId = toLocalChainId(transaction.chainId);
const destinationChainId = toLocalChainId(transaction.args.destinationChain);
const configChain = config.chains.find((c) => c.chainId === originChainId);
const configAsset = configChain?.assets.find((a) => a.assetId === asset.assetId);
const configXcmTransfer = configAsset?.xcmTransfers.find(
(t) => t.destination.chainId === toLocalChainId(destinationChainId),
);
const configXcmTransfer = configAsset?.xcmTransfers.find((t) => t.destination.chainId === destinationChainId);

if (originChainId && configXcmTransfer && configAsset) {
xcmService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export const TransferRules = {
incorrectRecipient: {
name: 'incorrectRecipient',
errorText: 'transfer.incorrectRecipientError',
validator: validateAddress,
// Second argument for validator is form data, but we need chain
validator: (destination: string) => validateAddress(destination),
},
},
amount: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export const Confirmation = ({ api, tx, account, chain, signAccount, feeTx, onSi
});

useEffect(() => {
xcmTransferModel.events.xcmConfigLoaded();
}, []);
xcmTransferModel.events.xcmStarted({ chain, asset });
}, [chain, asset]);

return (
<div className="flex flex-col items-center gap-y-3 px-5 pb-4">
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/pages/Operations/components/Operation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const Operation = ({ tx, account }: Props) => {
const approvals = events?.filter((e) => e.status === 'SIGNED') || [];
const initEvent = approvals.find((e) => e.accountId === tx.depositor);
const date = new Date(tx.dateCreated || initEvent?.dateCreated || Date.now());
const asset =
tx.transaction && getAssetById(tx.transaction.args.asset, chainsService.getChainById(tx.chainId)?.assets);
const assetId = tx.transaction && (tx.transaction.args.assetId || tx.transaction.args.asset);
const asset = getAssetById(assetId, chainsService.getChainById(tx.chainId)?.assets);
const amount = tx.transaction && getTransactionAmount(tx.transaction);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ type Props = {
};

export const TransactionAmount = ({ tx, className }: Props) => {
const asset = tx && getAssetById(tx.args.asset, chainsService.getChainById(tx.chainId)?.assets);
const assetId = tx?.args.assetId || tx?.args.asset;
const asset = getAssetById(assetId, chainsService.getChainById(tx.chainId)?.assets);
const value = getTransactionAmount(tx);

if (!asset || !value) {
Expand Down
18 changes: 9 additions & 9 deletions src/renderer/shared/api/xcm/service/xcmService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type ApiPromise } from '@polkadot/api';
import { type SubmittableExtrinsic } from '@polkadot/api/types';
import { BN, BN_TEN } from '@polkadot/util';
import { camelCase, get } from 'lodash';

Expand Down Expand Up @@ -339,20 +340,19 @@ function getParentChain(chain: Chain, chains: Record<ChainId, Chain>) {
async function getDeliveryFeeFromConfig({
config,
originChain,
destinationChain,
originApi,
parentApi,
destinationChainId,
txBytesLength,
extrinsic,
}: {
config: XcmConfig;
originChain: string;
destinationChain: Chain;
originApi: ApiPromise;
parentApi: ApiPromise;
destinationChainId: number;
txBytesLength: number;
extrinsic: SubmittableExtrinsic<'promise'>;
}): Promise<BN> {
const RELAYCHAINS = [1000, 2000];
const direction = RELAYCHAINS.includes(destinationChainId) ? 'toParent' : 'toParachain';
const direction = destinationChain.parentId ? 'toParachain' : 'toParent';

const deliveryFeeConfig = config.networkDeliveryFee[originChain]?.[direction];

Expand All @@ -362,17 +362,17 @@ async function getDeliveryFeeFromConfig({

if (direction === 'toParent') {
deliveryFactor = (
await parentApi.query[camelCase(deliveryFeeConfig.factorPallet)].upwardDeliveryFeeFactor()
await originApi.query[camelCase(deliveryFeeConfig.factorPallet)].upwardDeliveryFeeFactor()
).toString();
} else {
deliveryFactor = (
await originApi.query[camelCase(deliveryFeeConfig.factorPallet)].deliveryFeeFactor(destinationChainId)
).toString();
}

const weight = new BN(txBytesLength).add(SET_TOPIC_SIZE);
const weight = new BN(extrinsic.length).add(SET_TOPIC_SIZE);
const feeSize = new BN(deliveryFeeConfig.sizeBase).add(weight.mul(new BN(deliveryFeeConfig.sizeFactor)));
const deliveryFee = new BN(deliveryFactor).div(new BN(BN_TEN).pow(FACTOR_MULTIPLIER)).mul(feeSize);
const deliveryFee = feeSize.mul(new BN(deliveryFactor)).div(BN_TEN.pow(FACTOR_MULTIPLIER));

return deliveryFee;
}
24 changes: 18 additions & 6 deletions src/renderer/widgets/Transfer/model/form-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,28 +432,40 @@ const $extrinsic = combine(
},
);

const $xcmChain = combine(
{
chains: networkModel.$chains,
xcmChainId: xcmTransferModel.$xcmChainId,
},
({ chains, xcmChainId }) => {
if (!xcmChainId) return null;

return chains[xcmChainId] ?? null;
},
);

const getDeliveryFeeFx = createEffect(
async ({
config,
parachainId,
api,
parentApi,
extrinsic,
destinationChain,
}: {
config: XcmConfig | null;
parachainId: number | null;
api: ApiPromise | null;
parentApi: ApiPromise | null;
extrinsic?: SubmittableExtrinsic<'promise'> | null;
destinationChain: Chain | null;
}) => {
if (config && api && parentApi && parachainId && extrinsic) {
if (config && api && parachainId && extrinsic && destinationChain) {
return xcmService.getDeliveryFeeFromConfig({
config,
originChain: toLocalChainId(api.genesisHash.toHex()) || '',
originApi: api,
parentApi,
destinationChainId: parachainId,
txBytesLength: extrinsic.encodedLength,
extrinsic,
destinationChain,
});
} else {
return BN_ZERO;
Expand Down Expand Up @@ -678,10 +690,10 @@ sample({
clock: $extrinsic,
source: {
api: $api,
parentApi: xcmTransferModel.$parentChainApi,
parachainId: xcmTransferModel.$xcmParaId,
config: xcmTransferModel.$config,
extrinsic: $extrinsic,
destinationChain: $xcmChain,
},
target: getDeliveryFeeFx,
});
Expand Down
37 changes: 10 additions & 27 deletions src/renderer/widgets/Transfer/model/xcm-transfer-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const destinationChanged = createEvent<AccountId>();

const $config = createStore<XcmConfig | null>(null);
const $networkStore = restore(xcmStarted, null);
const $xcmChain = restore(xcmChainSelected, null);
const $xcmChainId = restore(xcmChainSelected, null);
const $xcmFee = restore(xcmFeeChanged, '0');
const $isXcmFeeLoading = restore(isXcmFeeLoadingChanged, true);
const $xcmParaId = createStore<number | null>(null);
Expand Down Expand Up @@ -70,15 +70,13 @@ const $transferDirections = combine(

const $transferDirection = combine(
{
xcmChain: $xcmChain,
xcmChainId: $xcmChainId,
xcmAsset: $xcmAsset,
},
({ xcmChain, xcmAsset }) => {
if (!xcmChain || !xcmAsset) return undefined;
({ xcmChainId, xcmAsset }) => {
if (!xcmChainId || !xcmAsset) return undefined;

const xcmChainId = toLocalChainId(xcmChain);

return xcmAsset?.xcmTransfers.find((t) => t.destination.chainId === xcmChainId);
return xcmAsset?.xcmTransfers.find((t) => t.destination.chainId === toLocalChainId(xcmChainId));
},
{ skipVoid: false },
);
Expand Down Expand Up @@ -209,40 +207,25 @@ const $xcmData = combine(
api: $api,
xcmFee: $xcmFee,
xcmAsset: $txAsset,
xcmChain: $xcmChain,
xcmChainId: $xcmChainId,
xcmWeight: $xcmWeight,
xcmDest: $txDestination,
xcmBeneficiary: $txBeneficiary,
transferDirection: $transferDirection,
},
({ api, xcmChain, transferDirection, ...rest }) => {
if (!api || !transferDirection || !xcmChain) return undefined;
({ api, xcmChainId, transferDirection, ...rest }) => {
if (!api || !transferDirection || !xcmChainId) return undefined;

const transactionType = xcmTransferUtils.getXcmTransferType(api, transferDirection.type);

return {
transactionType,
args: { destinationChain: xcmChain, ...rest },
args: { destinationChain: xcmChainId, ...rest },
};
},
{ skipVoid: false },
);

const $parentChainApi = combine(
{
network: $networkStore,
chains: networkModel.$chains,
apis: networkModel.$apis,
},
({ network, chains, apis }) => {
if (!chains || !apis || !network) return null;

const parentChain = xcmService.getParentChain(network.chain, chains);

return apis[parentChain.chainId] ?? null;
},
);

sample({
clock: xcmStarted,
target: xcmConfigLoaded,
Expand Down Expand Up @@ -281,12 +264,12 @@ sample({
export const xcmTransferModel = {
$config,
$apiDestination,
$parentChainApi,
$xcmData,
$xcmFee,
$isXcmFeeLoading,
$transferDirections,
$xcmParaId,
$xcmChainId,

events: {
xcmStarted,
Expand Down

0 comments on commit bdda454

Please sign in to comment.