Skip to content

Commit

Permalink
Implement Relay Fee (#362)
Browse files Browse the repository at this point in the history
* standarize transferAsset and feeAsset

* -wip- implement relay fee to transfers coming from parachains

* clean

* unify wh builder args

* revert change in extrinsic builder and add relay fee calculation in source data withj provider checking

* revert deconstructing not needed

* address TODOs

* remove provider enum and apply fixes

* change property name
  • Loading branch information
mmaurello authored Oct 7, 2024
1 parent 7531461 commit 7b48f4f
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export function polkadotXcm() {
build: ({
asset,
destination,
destinationAddress,
destinationApi,
fee,
moonAsset,
moonChain,
Expand Down
22 changes: 18 additions & 4 deletions packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EvmChain, EvmParachain } from '@moonbeam-network/xcm-types';
import { getMultilocationDerivedAddresses } from '@moonbeam-network/xcm-utils';
import { evmToAddress } from '@polkadot/util-crypto/address';
import { Wormhole } from '@wormhole-foundation/sdk-connect';
import { getExtrinsicAccount } from '../../../../extrinsic/ExtrinsicBuilder.utils';
Expand All @@ -24,25 +25,38 @@ export function wormhole() {
moonChain,
source,
sourceAddress,
}) => {
}): WormholeConfig => {
const isNativeAsset = asset.isSame(source.nativeAsset);
const isDestinationMoonChain = destination.isEqual(moonChain);
const isDestinationEvmChain = EvmChain.is(destination);
const tokenAddress = isNativeAsset ? 'native' : asset.address;
const tokenAddress = isNativeAsset
? 'native'
: isDestinationEvmChain
? moonChain.getChainAsset(asset).address
: asset.address;

const { address20: computedOriginAccount } =
getMultilocationDerivedAddresses({
address: sourceAddress,
paraId: moonChain.parachainId,
isParents: true,
});

if (!tokenAddress) {
throw new Error(`Asset ${asset.key} has no address`);
}

const wh = wormholeFactory(source);
const whSource = wh.getChain(source.getWormholeName());
const whSource = isDestinationEvmChain
? wh.getChain(moonChain.getWormholeName())
: wh.getChain(source.getWormholeName());
const whDestination = isDestinationEvmChain
? wh.getChain(destination.getWormholeName())
: wh.getChain(moonChain.getWormholeName());
const whAsset = Wormhole.tokenId(whSource.chain, tokenAddress);
const whSourceAddress = Wormhole.chainAddress(
whSource.chain,
sourceAddress,
isDestinationEvmChain ? computedOriginAccount : sourceAddress,
);
const whDestinationAddress = Wormhole.chainAddress(
whDestination.chain,
Expand Down
6 changes: 5 additions & 1 deletion packages/config/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,15 @@ export const fantomTestnet = new EvmChain({
ChainAsset.fromAsset(ftm, {
decimals: 18,
}),
// TODO should be WGLMR
// TODO should be WGLMR ?
ChainAsset.fromAsset(dev, {
address: '0x41E3CFDFC255A4bF3C8D3560Bc8D3D9b5080338e',
decimals: 18,
}),
ChainAsset.fromAsset(agng, {
address: '0xBb4D53C75654D28f69470546414401A2b31b586c',
decimals: 18,
}),
],
ecosystem: Ecosystem.AlphanetRelay,
explorer: 'https://testnet.ftmscan.com',
Expand Down
44 changes: 40 additions & 4 deletions packages/config/src/mrl-configs/fantomTestnet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { BalanceBuilder, MrlBuilder } from '@moonbeam-network/xcm-builder';
import { dev, ftm, ftmwh } from '../assets';
import { fantomTestnet, moonbaseAlpha, peaqAlphanet } from '../chains';
import {
fantomTestnet,
moonbaseAlpha,
moonbaseBeta,
peaqAlphanet,
} from '../chains';
import { ChainRoutes } from '../types/ChainRoutes';

export const fantomTestnetRoutes = new ChainRoutes({
Expand All @@ -25,7 +30,38 @@ export const fantomTestnetRoutes = new ChainRoutes({
},
},
mrl: {
isAutomatic: false, // TODO should be isAutomaticPossible
isAutomaticPossible: false,
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: ftmwh,
fee: {
asset: dev,
amount: 0.1,
balance: BalanceBuilder().substrate().system().account(),
},
},
},
},
{
source: {
asset: ftm,
balance: BalanceBuilder().evm().native(),
destinationFee: {
asset: ftm,
balance: BalanceBuilder().evm().native(),
},
},
destination: {
asset: ftmwh,
chain: moonbaseBeta,
balance: BalanceBuilder().substrate().assets().account(),
fee: {
asset: ftmwh,
amount: 0.06,
},
},
mrl: {
isAutomaticPossible: false,
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: ftmwh,
Expand Down Expand Up @@ -56,7 +92,7 @@ export const fantomTestnetRoutes = new ChainRoutes({
},
},
mrl: {
isAutomatic: true,
isAutomaticPossible: true,
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: ftmwh,
Expand Down Expand Up @@ -87,7 +123,7 @@ export const fantomTestnetRoutes = new ChainRoutes({
},
},
mrl: {
isAutomatic: false,
isAutomaticPossible: true,
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: dev,
Expand Down
2 changes: 2 additions & 0 deletions packages/config/src/mrl-configs/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { ChainRoutes } from '../types/ChainRoutes';
import { fantomTestnetRoutes } from './fantomTestnet';
import { moonbaseAlphaRoutes } from './moonbaseAlpha';
import { moonbaseBetaRoutes } from './moonbaseBeta';
import { peaqAlphanetRoutes } from './peaqAlphanet';

export const mrlRoutesList: ChainRoutes[] = [
fantomTestnetRoutes,
moonbaseAlphaRoutes,
moonbaseBetaRoutes,
peaqAlphanetRoutes,
];

Expand Down
8 changes: 6 additions & 2 deletions packages/config/src/mrl-configs/moonbaseAlpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const moonbaseAlphaRoutes = new ChainRoutes({
asset: ftmwh,
balance: BalanceBuilder().evm().erc20(),
},
fee: {
asset: dev,
balance: BalanceBuilder().substrate().system().account(),
},
},
destination: {
asset: ftm,
Expand All @@ -25,7 +29,7 @@ export const moonbaseAlphaRoutes = new ChainRoutes({
},
},
mrl: {
isAutomatic: true,
isAutomaticPossible: true,
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: ftmwh,
Expand Down Expand Up @@ -56,7 +60,7 @@ export const moonbaseAlphaRoutes = new ChainRoutes({
},
},
mrl: {
isAutomatic: true,
isAutomaticPossible: true,
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: dev,
Expand Down
45 changes: 45 additions & 0 deletions packages/config/src/mrl-configs/moonbaseBeta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { BalanceBuilder, MrlBuilder } from '@moonbeam-network/xcm-builder';
import { betaDEV, dev, ftm, ftmwh } from '../assets';
import { fantomTestnet, moonbaseBeta } from '../chains';
import { ChainRoutes } from '../types/ChainRoutes';

export const moonbaseBetaRoutes = new ChainRoutes({
chain: moonbaseBeta,
routes: [
{
source: {
asset: ftmwh,
balance: BalanceBuilder().substrate().assets().account(),
destinationFee: {
asset: ftmwh,
balance: BalanceBuilder().substrate().assets().account(),
},
fee: {
asset: betaDEV,
balance: BalanceBuilder().substrate().system().account(),
},
},
destination: {
asset: ftm,
chain: fantomTestnet,
balance: BalanceBuilder().evm().native(),
fee: {
asset: ftm,
amount: 0,
},
},
mrl: {
isAutomaticPossible: true,
transfer: MrlBuilder().wormhole().extrinsic().polkadotXcm().send(),
moonChain: {
asset: ftmwh,
fee: {
asset: dev,
amount: 0.1,
balance: BalanceBuilder().substrate().system().account(),
},
},
},
},
],
});
12 changes: 6 additions & 6 deletions packages/config/src/mrl-configs/peaqAlphanet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const peaqAlphanetRoutes = new ChainRoutes({
},
},
mrl: {
isAutomatic: true,
isAutomaticPossible: true,
transfer: MrlBuilder().wormhole().extrinsic().polkadotXcm().send(),
moonChain: {
asset: ftmwh,
Expand All @@ -51,19 +51,19 @@ export const peaqAlphanetRoutes = new ChainRoutes({
},
},
destination: {
asset: ftm,
asset: agng,
chain: fantomTestnet,
balance: BalanceBuilder().evm().native(),
fee: {
asset: ftm,
amount: 0,
asset: agng,
amount: 0.2,
},
},
mrl: {
isAutomatic: true,
isAutomaticPossible: true,
transfer: MrlBuilder().wormhole().extrinsic().polkadotXcm().send(),
moonChain: {
asset: ftmwh,
asset: agng,
fee: {
asset: dev,
amount: 0.1,
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/types/AssetRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface FeeConfig {
}

export interface MrlConfig {
isAutomatic: boolean;
isAutomaticPossible: boolean;
transfer: MrlConfigBuilder;
moonChain: MoonChainConfig;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/mrl/src/getTransferData/getMoonChainData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export async function getMoonChainData({

const fee = await getDestinationFee({
address: sourceAddress, // TODO not correct
asset: route.mrl.moonChain.fee.asset,
asset: route.source.asset,
destination: moonChain,
fee: route.mrl.moonChain.fee.amount,
transferAsset: route.source.asset,
feeAsset: route.mrl.moonChain.fee.asset,
});

let address = sourceAddress;
Expand Down
Loading

0 comments on commit 7b48f4f

Please sign in to comment.