Skip to content

Commit

Permalink
Add relayer fee to min (#363)
Browse files Browse the repository at this point in the history
* add relayer fee to min and rename interface

* fix lint
  • Loading branch information
mmaurello authored Oct 7, 2024
1 parent 7b48f4f commit e4630c8
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 34 deletions.
12 changes: 6 additions & 6 deletions packages/builder/fixtures/builderParamsMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Parachain,
} from '@moonbeam-network/xcm-types';
import { vi } from 'vitest';
import type { BuilderPrams, MrlBuilderParams } from '../src';
import type { BuilderParams, MrlBuilderParams } from '../src';

export const apiMock = {
tx: {
Expand Down Expand Up @@ -98,7 +98,7 @@ export const fantomTestnet = new EvmChain({
},
});

export const buildParamsMock: BuilderPrams = {
export const buildParamsMock: BuilderParams = {
asset: testAssetAmount,
destination: moonbaseAlphaMock,
destinationAddress: '0xeF46c7649270C912704fB09B75097f6E32208b85',
Expand All @@ -109,7 +109,7 @@ export const buildParamsMock: BuilderPrams = {
sourceApi: apiMock,
};

export const buildParamsSameAssetMock: BuilderPrams = {
export const buildParamsSameAssetMock: BuilderParams = {
asset: testAssetAmount,
destination: moonbaseAlphaMock,
destinationAddress: '0xeF46c7649270C912704fB09B75097f6E32208b85',
Expand All @@ -120,7 +120,7 @@ export const buildParamsSameAssetMock: BuilderPrams = {
sourceApi: apiMock,
};

export const buildParachainParamsMock: BuilderPrams = {
export const buildParachainParamsMock: BuilderParams = {
asset: testAssetAmount,
destination: interlayTestnetMock,
/* cspell:disable-next-line */
Expand Down Expand Up @@ -164,7 +164,7 @@ export const mrlBuildParamsMock2: MrlBuilderParams = {
},
};

export const wormholeConfigBuilderPrams: MrlBuilderParams = {
export const wormholeConfigBuilderParams: MrlBuilderParams = {
asset: testAssetAmount,
destination: alphanetAssetHubMock,
destinationAddress: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
Expand All @@ -180,7 +180,7 @@ export const wormholeConfigBuilderPrams: MrlBuilderParams = {
sourceApi: apiMock,
};

export const wormholeToMoonchainConfigBuilderPrams: MrlBuilderParams = {
export const wormholeToMoonchainConfigBuilderParams: MrlBuilderParams = {
asset: testAssetAmount,
destination: moonbaseAlphaMock,
destinationAddress: '0x98891e5FD24Ef33A488A47101F65D212Ff6E650E',
Expand Down
4 changes: 2 additions & 2 deletions packages/builder/src/asset-min/AssetMinBuilder.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import type { SubstrateQueryConfig } from '../types/substrate/SubstrateQueryConf

export type AssetMinConfigBuilder = ConfigBuilder<
SubstrateQueryConfig,
AssetMinConfigBuilderPrams
AssetMinConfigBuilderParams
>;

export interface AssetMinConfigBuilderPrams {
export interface AssetMinConfigBuilderParams {
asset: ChainAssetId;
}
4 changes: 2 additions & 2 deletions packages/builder/src/balance/BalanceBuilder.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import type { SubstrateQueryConfig } from '../types/substrate/SubstrateQueryConf

export type BalanceConfigBuilder = ConfigBuilder<
ContractConfig | SubstrateQueryConfig | EvmQueryConfig,
BalanceBuilderPrams
BalanceBuilderParams
>;

export interface BalanceBuilderPrams {
export interface BalanceBuilderParams {
address: string;
asset: ChainAsset;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/builder/src/builder.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import type {
} from '@moonbeam-network/xcm-types';
import type { ApiPromise } from '@polkadot/api';

export interface ConfigBuilder<Config, Params = BuilderPrams> {
export interface ConfigBuilder<Config, Params = BuilderParams> {
build: (params: Params) => Config;
}

export interface BuilderPrams<IChain extends Chain = AnyParachain> {
export interface BuilderParams<IChain extends Chain = AnyParachain> {
asset: AssetAmount;
destination: IChain;
destinationAddress: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { SubmittableExtrinsicFunction } from '@polkadot/api/types';
import type { BuilderPrams } from '../../../builder.interfaces';
import type { BuilderParams } from '../../../builder.interfaces';
import type { Parents } from '../../ExtrinsicBuilder.interfaces';
import {
getExtrinsicAccount,
getExtrinsicArgumentVersion,
} from '../../ExtrinsicBuilder.utils';

export interface GetExtrinsicParams extends BuilderPrams {
export interface GetExtrinsicParams extends BuilderParams {
// biome-ignore lint/suspicious/noExplicitAny: not sure how to fix this
asset: any;
func?: SubmittableExtrinsicFunction<'promise'>;
Expand Down Expand Up @@ -55,7 +55,7 @@ export function getPolkadotXcmExtrinsicArgs({
export function shouldFeeAssetPrecedeAsset({
asset,
fee,
}: BuilderPrams): boolean {
}: BuilderParams): boolean {
const assetIdNumber = Number(asset.getAssetId());
const feeAssetIdNumber = Number(fee.getAssetId());

Expand Down
4 changes: 2 additions & 2 deletions packages/builder/src/fee/FeeBuilder.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import type { SubstrateCallConfig } from '../types/substrate/SubstrateCallConfig

export type FeeConfigBuilder = ConfigBuilder<
SubstrateCallConfig,
FeeConfigBuilderPrams
FeeConfigBuilderParams
>;

export interface FeeConfigBuilderPrams {
export interface FeeConfigBuilderParams {
address: string;
api: ApiPromise;
asset: ChainAsset;
Expand Down
4 changes: 2 additions & 2 deletions packages/builder/src/fee/FeeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { SubstrateCallConfig } from '../types/substrate/SubstrateCallConfig';
import type {
FeeConfigBuilder,
FeeConfigBuilderPrams,
FeeConfigBuilderParams,
XcmPaymentFeeProps,
} from './FeeBuilder.interfaces';
import {
Expand Down Expand Up @@ -36,7 +36,7 @@ function xcmPaymentApi() {
asset,
destination,
feeAsset,
}: FeeConfigBuilderPrams) =>
}: FeeConfigBuilderParams) =>
new SubstrateCallConfig({
api,
call: async (): Promise<bigint> => {
Expand Down
4 changes: 2 additions & 2 deletions packages/builder/src/mrl/MrlBuilder.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
} from '@moonbeam-network/xcm-types';
import type { ApiPromise } from '@polkadot/api';
import type { HexString } from '@polkadot/util/types';
import type { BuilderPrams, ConfigBuilder } from '../builder.interfaces';
import type { BuilderParams, ConfigBuilder } from '../builder.interfaces';
import type { ContractConfig } from '../contract';
import type { ExtrinsicConfig } from '../extrinsic';
import type { WormholeConfig } from './providers/wormhole/wormhole';
Expand All @@ -15,7 +15,7 @@ export type MrlConfigBuilder = ConfigBuilder<
MrlBuilderParams
>;

export interface MrlBuilderParams extends BuilderPrams<AnyChain> {
export interface MrlBuilderParams extends BuilderParams<AnyChain> {
isAutomatic: boolean;
moonApi: ApiPromise;
moonAsset: ChainAsset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { describe, expect, it } from 'vitest';

import { getPolkadotApi } from '@moonbeam-network/xcm-utils';
import {
wormholeConfigBuilderPrams,
wormholeToMoonchainConfigBuilderPrams,
wormholeConfigBuilderParams,
wormholeToMoonchainConfigBuilderParams,
} from '../../../../../fixtures';
import { wormhole } from './wormhole';

Expand All @@ -18,7 +18,7 @@ describe('wormhole', async () => {
it('should be correct config', () => {
expect(
transfer.build({
...wormholeConfigBuilderPrams,
...wormholeConfigBuilderParams,
moonApi,
isAutomatic: true,
}),
Expand All @@ -32,7 +32,7 @@ describe('wormhole', async () => {
it('should be correct config to moon chain', () => {
expect(
transfer.build({
...wormholeToMoonchainConfigBuilderPrams,
...wormholeToMoonchainConfigBuilderParams,
moonApi,
isAutomatic: false,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ export function wormhole() {
source,
sourceAddress,
}): WormholeConfig => {
const isNativeAsset = asset.isSame(source.nativeAsset);
const isDestinationMoonChain = destination.isEqual(moonChain);
const isDestinationEvmChain = EvmChain.is(destination);
const isNativeAsset = asset.isSame(
isDestinationEvmChain ? moonChain.nativeAsset : source.nativeAsset,
);
const tokenAddress = isNativeAsset
? 'native'
: isDestinationEvmChain
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/mrl-configs/moonbaseAlpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const moonbaseAlphaRoutes = new ChainRoutes({
asset: dev,
fee: {
asset: dev,
amount: 0.1,
amount: 0,
balance: BalanceBuilder().substrate().system().account(),
},
},
Expand Down
25 changes: 18 additions & 7 deletions packages/mrl/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import {
import {
type DestinationChainTransferData,
PolkadotService,
type SourceChainTransferData,
convertToChainDecimals,
getMin,
} from '@moonbeam-network/xcm-sdk';
import { type AssetAmount, EvmParachain } from '@moonbeam-network/xcm-types';
import { AssetAmount, EvmParachain } from '@moonbeam-network/xcm-types';
import {
getMultilocationDerivedAddresses,
getPolkadotApi,
Expand All @@ -33,7 +32,10 @@ import {
createPublicClient,
encodeFunctionData,
} from 'viem';
import type { MoonChainTransferData } from '../mrl.interfaces';
import type {
MoonChainTransferData,
SourceTransferData,
} from '../mrl.interfaces';

const MOON_CHAIN_AUTOMATIC_GAS_ESTIMATION = {
[moonbeam.key]: 1273110n,
Expand All @@ -43,7 +45,7 @@ const MOON_CHAIN_AUTOMATIC_GAS_ESTIMATION = {
export interface DataParams {
destinationData: DestinationChainTransferData;
moonChainData: MoonChainTransferData;
sourceData: SourceChainTransferData;
sourceData: SourceTransferData;
}

export function getMoonChainFeeValueOnSource({
Expand Down Expand Up @@ -73,15 +75,24 @@ export function getMrlMin({
moonChainData,
sourceData,
}: DataParams): AssetAmount {
const min = getMin(destinationData);
const fee = getMoonChainFeeValueOnSource({
const minInDestination = getMin(destinationData);
const min = AssetAmount.fromChainAsset(
sourceData.chain.getChainAsset(sourceData.balance),
{
amount: minInDestination.amount,
},
);
const moonChainFee = getMoonChainFeeValueOnSource({
destinationData,
moonChainData,
sourceData,
});
const relayerFee = sourceData.relayerFee?.amount
? sourceData.relayerFee.toBig()
: Big(0);

return min.copyWith({
amount: BigInt(min.toBig().add(fee).toFixed()),
amount: BigInt(min.toBig().add(moonChainFee).add(relayerFee).toFixed()),
});
}

Expand Down

0 comments on commit e4630c8

Please sign in to comment.