Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return balance and address in MoonChainData #408

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions packages/config/src/mrl-configs/moonbaseBeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const moonbaseBetaRoutes = new MrlChainRoutes({
transfer: MrlBuilder().wormhole().extrinsic().polkadotXcm().send(),
moonChain: {
asset: ftmwh,
balance: BalanceBuilder().substrate().assets().account(),
fee: {
asset: dev,
amount: 0.1,
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
69 changes: 52 additions & 17 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 { 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,40 +23,72 @@ export async function getMoonChainData({
}

const moonChain = getMoonChain(route.source.chain);
const moonChainAddress = getMoonChainAddress({
route,
sourceAddress,
destinationAddress,
});

const fee = await getDestinationFee({
address: sourceAddress, // TODO not correct
address: moonChainAddress,
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 { address20 } = getMultilocationDerivedAddresses({
address: sourceAddress,
paraId: route.source.chain.parachainId,
isParents: true,
});

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

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

return {
address: moonChainAddress,
balance,
feeBalance,
chain: moonChain,
fee,
};
}

function getMoonChainAddress({
mmaurello marked this conversation as resolved.
Show resolved Hide resolved
route: { source, destination },
sourceAddress,
destinationAddress,
}: GetMoonChainDataParams): string {
const moonChain = getMoonChain(source.chain);
const isDestinationMoonChain = moonChain.isEqual(destination.chain);
const isSourceMoonChain = moonChain.isEqual(source.chain);

let moonChainAddress = isDestinationMoonChain
? destinationAddress
: sourceAddress;

// for Parachain to EVM transactions, we use the computed origin account in the moonchain
if (Parachain.is(source.chain) && !isSourceMoonChain) {
const isSourceEvmSigner =
EvmParachain.is(source.chain) && source.chain.isEvmSigner;

const { address20: computedOriginAccount } =
getMultilocationDerivedAddresses({
address: isSourceEvmSigner
? evmToAddress(sourceAddress)
: sourceAddress,
paraId: source.chain.parachainId,
isParents: true,
});

moonChainAddress = computedOriginAccount;
}

return moonChainAddress;
}
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
Loading