Skip to content

Commit

Permalink
Fixed destination fee on source chain
Browse files Browse the repository at this point in the history
  • Loading branch information
ekenigs committed Jul 5, 2024
1 parent bea2122 commit 3999e0c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 33 deletions.
1 change: 1 addition & 0 deletions packages/builder/src/balance/BalanceBuilder.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface BalanceConfigBuilder {
export interface BalanceConfigBuilderPrams {
address: string;
asset: ChainAssetId;
constractAddress?: string;
}

export interface PalletBalancesAccountDataOld extends Struct {
Expand Down
24 changes: 12 additions & 12 deletions packages/builder/src/balance/BalanceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,15 @@ export function evm() {
};
}

export function substrate() {
return {
assets,
system,
tokens,
};
}

function erc20(): BalanceConfigBuilder {
return {
build: ({ address, asset }) => {
if (!asset || !isString(asset)) {
throw new Error(`Invalid contract address: ${asset}`);
build: ({ address, constractAddress }) => {
if (!constractAddress || !isString(constractAddress)) {
throw new Error(`Invalid contract address: ${constractAddress}`);
}

return new ContractConfig({
address: asset,
address: constractAddress,
args: [address],
func: 'balanceOf',
module: 'Erc20',
Expand All @@ -56,6 +48,14 @@ function erc20(): BalanceConfigBuilder {
};
}

export function substrate() {
return {
assets,
system,
tokens,
};
}

function assets() {
return {
account: (): BalanceConfigBuilder => ({
Expand Down
16 changes: 0 additions & 16 deletions packages/builder/src/contract/contracts/erc20/Erc20.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/builder/src/contract/contracts/erc20/index.ts

This file was deleted.

7 changes: 5 additions & 2 deletions packages/sdk/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export async function getSourceData({
});

const fee = await getFee({
balance,
chain,
contract,
extrinsic,
Expand Down Expand Up @@ -117,6 +118,7 @@ export async function getSourceData({
}

export interface GetFeeParams {
balance: AssetAmount;
feeBalance: AssetAmount;
contract?: ContractConfig;
chain: AnyChain;
Expand All @@ -127,6 +129,7 @@ export interface GetFeeParams {
}

export async function getFee({
balance,
feeBalance,
chain,
contract: contractConfig,
Expand All @@ -144,13 +147,13 @@ export async function getFee({
chain,
contractConfig,
) as TransferContractInterface;
const fee = await contract.getFee(feeBalance.amount, sourceAddress);
const fee = await contract.getFee(balance.amount, sourceAddress);

return feeBalance.copyWith({ amount: fee });
}

const fee = await getExtrinsicFee(
feeBalance,
balance,
extrinsic as ExtrinsicConfig,
polkadot,
sourceAddress,
Expand Down
10 changes: 8 additions & 2 deletions packages/sdk/src/getTransferData/getTransferData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { IConfigService, TransferConfig } from '@moonbeam-network/xcm-config';
import { toBigInt } from '@moonbeam-network/xcm-utils';
import Big from 'big.js';
import { AssetAmount } from '@moonbeam-network/xcm-types';
import { TransferContractInterface, createContract } from '../contract';
import { PolkadotService } from '../polkadot';
import {
Expand Down Expand Up @@ -38,9 +39,14 @@ export async function getTransferData({
transferConfig,
});

const destinationFee = destination.fee.convertDecimals(
transferConfig.source.chain.getChainAsset(destination.fee).decimals,
// Here we need to convert the fee on the destination chain to an asset on source chain.
const destinationFeeAsset = transferConfig.source.chain.getChainAsset(
destination.fee,
);
const destinationFee = AssetAmount.fromChainAsset(destinationFeeAsset, {
amount: destination.fee.convertDecimals(destinationFeeAsset.decimals)
.amount,
});

const source = await getSourceData({
destinationAddress,
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export async function getBalance({
const cfg = builder.build({
address,
asset: asset.getBalanceAssetId(),
constractAddress: asset.address,
});
const amount = AssetAmount.fromChainAsset(asset, { amount: 0n });

Expand Down

0 comments on commit 3999e0c

Please sign in to comment.