Skip to content

Commit

Permalink
return computed origin account info in moonChainData for parachain to…
Browse files Browse the repository at this point in the history
… evm transactions
  • Loading branch information
mmaurello committed Dec 17, 2024
1 parent b094a24 commit 5906089
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 12 deletions.
7 changes: 7 additions & 0 deletions packages/config/src/mrl-configs/fantomTestnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const fantomTestnetRoutes = new MrlChainRoutes({
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: ftmwh,
balance: BalanceBuilder().evm().erc20(),
fee: {
asset: dev,
amount: 0.1,
Expand Down Expand Up @@ -66,6 +67,7 @@ export const fantomTestnetRoutes = new MrlChainRoutes({
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: ftmwh,
balance: BalanceBuilder().evm().erc20(),
fee: {
asset: dev,
amount: 0.1,
Expand Down Expand Up @@ -97,6 +99,8 @@ export const fantomTestnetRoutes = new MrlChainRoutes({
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: agng,
balance: BalanceBuilder().substrate().assets().account(),

fee: {
asset: dev,
amount: 0.1,
Expand Down Expand Up @@ -128,6 +132,7 @@ export const fantomTestnetRoutes = new MrlChainRoutes({
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: ftmwh,
balance: BalanceBuilder().evm().erc20(),
fee: {
asset: dev,
amount: 0.1,
Expand Down Expand Up @@ -159,6 +164,7 @@ export const fantomTestnetRoutes = new MrlChainRoutes({
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: ftmwh,
balance: BalanceBuilder().evm().erc20(),
fee: {
asset: dev,
amount: 0.1,
Expand Down Expand Up @@ -190,6 +196,7 @@ export const fantomTestnetRoutes = new MrlChainRoutes({
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: dev,
balance: BalanceBuilder().substrate().system().account(),
fee: {
asset: dev,
amount: 0.1,
Expand Down
4 changes: 3 additions & 1 deletion packages/config/src/mrl-configs/moonbaseAlpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ export const moonbaseAlphaRoutes = new MrlChainRoutes({
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: ftmwh,
balance: BalanceBuilder().evm().erc20(),
fee: {
asset: dev,
amount: 0.1, // TODO not really, it would be the source fee as source is moonChain
amount: 0.1,
balance: BalanceBuilder().substrate().system().account(),
},
},
Expand Down Expand Up @@ -64,6 +65,7 @@ export const moonbaseAlphaRoutes = new MrlChainRoutes({
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: dev,
balance: BalanceBuilder().evm().erc20(),
fee: {
asset: dev,
amount: 0,
Expand Down
2 changes: 2 additions & 0 deletions packages/config/src/mrl-configs/peaqAlphanet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const peaqAlphanetRoutes = new MrlChainRoutes({
transfer: MrlBuilder().wormhole().extrinsic().polkadotXcm().send(),
moonChain: {
asset: ftmwh,
balance: BalanceBuilder().evm().erc20(),
fee: {
asset: dev,
amount: 0.1,
Expand Down Expand Up @@ -72,6 +73,7 @@ export const peaqAlphanetRoutes = new MrlChainRoutes({
transfer: MrlBuilder().wormhole().extrinsic().polkadotXcm().send(),
moonChain: {
asset: agng,
balance: BalanceBuilder().substrate().assets().account(),
fee: {
asset: dev,
amount: 0.1,
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/mrl-configs/peaqEvmAlphanet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const peaqEvmAlphanetRoutes = new MrlChainRoutes({
.transferAssetsAndMessage(),
moonChain: {
asset: ftmwh,
balance: BalanceBuilder().evm().erc20(),
fee: {
asset: dev,
amount: 0.1,
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/types/AssetRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface DestinationFeeConfig

export interface MoonChainConfig {
asset: Asset;
balance: BalanceConfigBuilder;
fee: MoonChainFeeConfig;
}

Expand Down
1 change: 1 addition & 0 deletions packages/mrl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@polkadot/api-augment": "14.3.1",
"@polkadot/types": "14.3.1",
"@polkadot/util": "13.2.3",
"@polkadot/util-crypto": "13.2.3",
"@wormhole-foundation/sdk-connect": "^0.10.7",
"@wormhole-foundation/sdk-evm": "^0.10.7",
"viem": "^2.21.7"
Expand Down
29 changes: 24 additions & 5 deletions packages/mrl/src/getTransferData/getMoonChainData.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { type MrlAssetRoute, getMoonChain } from '@moonbeam-network/xcm-config';
import { getBalance, getDestinationFee } from '@moonbeam-network/xcm-sdk';
import { Parachain } from '@moonbeam-network/xcm-types';
import { EvmChain, EvmParachain, Parachain } from '@moonbeam-network/xcm-types';
import { getMultilocationDerivedAddresses } from '@moonbeam-network/xcm-utils';
import { evmToAddress } from '@polkadot/util-crypto';
import type { MoonChainTransferData } from '../mrl.interfaces';

interface GetMoonChainDataParams {
route: MrlAssetRoute;
sourceAddress: string;
destinationAddress: string;
}

export async function getMoonChainData({
route,
sourceAddress,
destinationAddress,
}: GetMoonChainDataParams): Promise<MoonChainTransferData> {
if (!route.mrl) {
throw new Error(
Expand All @@ -20,30 +23,44 @@ export async function getMoonChainData({
}

const moonChain = getMoonChain(route.source.chain);
const isDestinationMoonChain = moonChain.isEqual(route.destination.chain);
const isSourceEvmChain = EvmChain.is(route.source.chain);
let address =
isDestinationMoonChain || isSourceEvmChain
? destinationAddress
: sourceAddress;

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

let address = sourceAddress;

if (
Parachain.is(route.source.chain) &&
!route.source.chain.isEqual(moonChain)
) {
const addressToUse = EvmParachain.is(route.source.chain)
? evmToAddress(sourceAddress)
: address;
const { address20 } = getMultilocationDerivedAddresses({
address: sourceAddress,
address: addressToUse,
paraId: route.source.chain.parachainId,
isParents: true,
});

address = address20;
}

const balance = await getBalance({
address,
asset: moonChain.getChainAsset(route.mrl.moonChain.asset),
builder: route.mrl.moonChain.balance,
chain: moonChain,
});

const feeBalance = await getBalance({
address,
asset: moonChain.getChainAsset(route.mrl.moonChain.fee.asset),
Expand All @@ -52,6 +69,8 @@ export async function getMoonChainData({
});

return {
address,
balance,
feeBalance,
chain: moonChain,
fee,
Expand Down
1 change: 1 addition & 0 deletions packages/mrl/src/getTransferData/getTransferData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export async function getTransferData({
const moonChainData = await getMoonChainData({
route,
sourceAddress,
destinationAddress,
});

return {
Expand Down
6 changes: 2 additions & 4 deletions packages/mrl/src/mrl.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ export interface SourceTransferData extends SourceChainTransferData {

export interface DestinationTransferData extends ChainTransferData {}

export type MoonChainTransferData = Omit<
ChainTransferData,
'min' | 'balance'
> & {
export type MoonChainTransferData = Omit<ChainTransferData, 'min'> & {
address: string;
feeBalance: AssetAmount;
};

Expand Down
Loading

0 comments on commit 5906089

Please sign in to comment.