Skip to content

Commit

Permalink
Adjustments after implementing mrl package in dapp (#401)
Browse files Browse the repository at this point in the history
* -wip- adjustments after integration

* add setIsAutomatic data initializer and exclude relayer fee from min if not automatic

* add statusCallback param

* fixes in getMoonChainData

* adjust configs

* more adjustments

* change fantom rpc endpoint
  • Loading branch information
mmaurello authored Dec 2, 2024
1 parent 160ef18 commit 8536459
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 66 deletions.
20 changes: 5 additions & 15 deletions examples/sdk-simple/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,24 @@ console.log(`Substrate address: ${pair.address}`);

export function logBalances(data: TransferData): void {
console.log(
`Balance on ${data.source.chain.name} ${data.source.balance.toDecimal()} ${
data.source.balance.symbol
}`,
`Balance on ${data.source.chain.name} ${data.source.balance.toDecimal()} ${data.source.balance.getSymbol()}`,
);
console.log(
`Balance on ${
data.destination.chain.name
} ${data.destination.balance.toDecimal()} ${
data.destination.balance.symbol
}`,
} ${data.destination.balance.toDecimal()} ${data.destination.balance.getSymbol()}`,
);
}

export function logTxDetails(data: TransferData): void {
console.log(
`\nYou can send min: ${data.min.toDecimal()} ${
data.min.symbol
} and max: ${data.max.toDecimal()} ${data.max.symbol} from ${
`\nYou can send min: ${data.min.toDecimal()} ${data.min.getSymbol()} and max: ${data.max.toDecimal()} ${data.max.getSymbol()} from ${
data.source.chain.name
} to ${
data.destination.chain.name
}. You will pay ${data.source.fee.toDecimal()} ${
data.source.fee.symbol
} fee on ${
}. You will pay ${data.source.fee.toDecimal()} ${data.source.fee.getSymbol()} fee on ${
data.source.chain.name
} and ${data.destination.fee.toDecimal()} ${
data.destination.fee.symbol
} fee on ${data.destination.chain.name}.`,
} and ${data.destination.fee.toDecimal()} ${data.destination.fee.getSymbol()} fee on ${data.destination.chain.name}.`,
);
}

Expand Down
7 changes: 6 additions & 1 deletion packages/config/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ export const fantomTestnet = new EvmChain({
key: 'fantom-testnet',
name: 'Fantom Testnet',
nativeAsset: ftm,
rpc: 'https://rpc.testnet.fantom.network',
rpc: 'https://fantom-testnet-rpc.publicnode.com',
wh: {
name: 'Fantom',
},
Expand Down Expand Up @@ -962,6 +962,7 @@ export const moonbaseAlpha = new EvmParachain({
}),
],
ecosystem: Ecosystem.AlphanetRelay,
explorer: 'https://moonbase.moonscan.io',
genesisHash:
'0x91bc6e169807aaa54802737e1c504b2577d4fafedd5a02c10293b1cd60e39527',
id: 1287,
Expand Down Expand Up @@ -1014,6 +1015,9 @@ export const moonbaseBeta = new EvmParachain({
}),
],
ecosystem: Ecosystem.AlphanetRelay,
explorer: getPolkadotAppsUrl(
'wss://moonbase-beta.api.moonbase.moonbeam.network',
),
genesisHash:
'0xeebb5d05763801e54d6a7a60a4b7998ac125c4d050dcec418dd07ea959a54464',
id: 1282,
Expand Down Expand Up @@ -1347,6 +1351,7 @@ export const moonbeam = new EvmParachain({
}),
],
ecosystem: Ecosystem.Polkadot,
explorer: 'https://moonbeam.moonscan.io',
genesisHash:
'0xfe58ea77779b7abda7da4ec526d14db9b1e9cd40a217c34892af80a9b332b76d',
id: 1284,
Expand Down
32 changes: 32 additions & 0 deletions packages/config/src/mrl-configs/fantomTestnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
moonbaseAlpha,
moonbaseBeta,
peaqAlphanet,
peaqEvmAlphanet,
} from '../chains';
import { MrlChainRoutes } from '../types/MrlChainRoutes';

Expand Down Expand Up @@ -42,6 +43,37 @@ export const fantomTestnetRoutes = new MrlChainRoutes({
},
},
},
{
source: {
asset: ftm,
balance: BalanceBuilder().evm().native(),
destinationFee: {
asset: ftm,
balance: BalanceBuilder().evm().native(),
},
},
destination: {
asset: ftmwh,
chain: peaqEvmAlphanet,
balance: BalanceBuilder().evm().erc20(),
fee: {
asset: ftmwh,
amount: 0.01,
},
},
mrl: {
isAutomaticPossible: false,
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: ftmwh,
fee: {
asset: dev,
amount: 0.1,
balance: BalanceBuilder().substrate().system().account(),
},
},
},
},
{
source: {
asset: agng,
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/mrl-configs/peaqAlphanet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const peaqAlphanetRoutes = new MrlChainRoutes({
destination: {
asset: agng,
chain: fantomTestnet,
balance: BalanceBuilder().evm().native(),
balance: BalanceBuilder().evm().erc20(),
fee: {
asset: agng,
amount: 0.2,
Expand Down
28 changes: 8 additions & 20 deletions packages/mrl/src/getTransferData/getMoonChainData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@ 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 { getMultilocationDerivedAddresses } from '@moonbeam-network/xcm-utils';
import type {
DestinationTransferData,
MoonChainTransferData,
} from '../mrl.interfaces';
import type { MoonChainTransferData } from '../mrl.interfaces';

interface GetMoonChainDataParams {
destinationData: DestinationTransferData;
route: MrlAssetRoute;
sourceAddress: string;
}

export async function getMoonChainData({
destinationData,
route,
sourceAddress,
}: GetMoonChainDataParams): Promise<MoonChainTransferData> {
Expand All @@ -25,16 +20,6 @@ export async function getMoonChainData({
}

const moonChain = getMoonChain(route.source.chain);
const asset = moonChain.getChainAsset(route.mrl.moonChain.asset);
const isDestinationMoonChain = route.destination.chain.isEqual(moonChain);

if (isDestinationMoonChain) {
return {
balance: destinationData.balance,
chain: destinationData.chain,
fee: destinationData.fee,
};
}

const fee = await getDestinationFee({
address: sourceAddress, // TODO not correct
Expand All @@ -46,7 +31,10 @@ export async function getMoonChainData({

let address = sourceAddress;

if (Parachain.is(route.source.chain)) {
if (
Parachain.is(route.source.chain) &&
!route.source.chain.isEqual(moonChain)
) {
const { address20 } = getMultilocationDerivedAddresses({
address: sourceAddress,
paraId: route.source.chain.parachainId,
Expand All @@ -56,15 +44,15 @@ export async function getMoonChainData({
address = address20;
}

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

return {
balance,
feeBalance,
chain: moonChain,
fee,
};
Expand Down
11 changes: 8 additions & 3 deletions packages/mrl/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
type EvmChain,
type EvmParachain,
} from '@moonbeam-network/xcm-types';
import { toBigInt } from '@moonbeam-network/xcm-utils';
import type { SourceTransferData } from '../mrl.interfaces';
import { WormholeService } from '../services/wormhole';
import {
Expand All @@ -30,13 +31,15 @@ import {
} from './getTransferData.utils';

interface GetSourceDataParams {
isAutomatic: boolean;
route: MrlAssetRoute;
destinationAddress: string;
destinationFee: AssetAmount;
sourceAddress: string;
}

export async function getSourceData({
isAutomatic,
route,
destinationAddress,
destinationFee,
Expand Down Expand Up @@ -94,7 +97,7 @@ export async function getSourceData({
asset: balance,
destinationAddress,
feeAsset: feeBalance,
isAutomatic: route.mrl.isAutomaticPossible,
isAutomatic,
route,
sourceAddress,
});
Expand All @@ -114,7 +117,7 @@ export async function getSourceData({
transfer,
asset: balance,
feeAsset: feeBalance,
isAutomatic: route.mrl.isAutomaticPossible,
isAutomatic,
destinationAddress,
route,
sourceAddress,
Expand Down Expand Up @@ -239,11 +242,13 @@ async function getWormholeFee({
config,
}: GetWormholeFeeParams): Promise<AssetAmount | undefined> {
if (WormholeConfig.is(config)) {
const safetyAmount = toBigInt(0.000001, asset.decimals);

const wh = WormholeService.create(chain as EvmChain | EvmParachain);
const fee = await wh.getFee(config);

return AssetAmount.fromChainAsset(chain.getChainAsset(asset), {
amount: fee.relayFee?.amount || 0n,
amount: fee.relayFee ? fee.relayFee.amount + safetyAmount : 0n,
});
}

Expand Down
10 changes: 8 additions & 2 deletions packages/mrl/src/getTransferData/getTransferData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ interface GetTransferDataParams {
route: MrlAssetRoute;
sourceAddress: string;
destinationAddress: string;
isAutomatic: boolean;
}

export async function getTransferData({
route,
sourceAddress,
destinationAddress,
isAutomatic,
}: GetTransferDataParams): Promise<TransferData> {
if (!route.mrl) {
throw new Error(
Expand All @@ -58,14 +60,14 @@ export async function getTransferData({
});

const sourceData = await getSourceData({
isAutomatic: route.mrl.isAutomaticPossible && isAutomatic,
route,
destinationAddress,
destinationFee,
sourceAddress,
});

const moonChainData = await getMoonChainData({
destinationData,
route,
sourceAddress,
});
Expand All @@ -87,12 +89,14 @@ export async function getTransferData({
.minus(
isSameAssetPayingDestinationFee ? destinationFee.toBig() : Big(0),
)
.minus(fee);
.minus(fee)
.minus(sourceData.relayerFee?.toBig() || Big(0));

return sourceData.balance.copyWith({
amount: result.lt(0) ? 0n : BigInt(result.toFixed()),
});
},
isAutomaticPossible: route.mrl.isAutomaticPossible,
max: sourceData.max,
min: getMrlMin({
destinationData,
Expand All @@ -105,6 +109,7 @@ export async function getTransferData({
amount,
isAutomatic,
{ evmSigner, polkadotSigner }: Partial<Signers>,
statusCallback,
): Promise<string[]> {
const source = route.source.chain;

Expand Down Expand Up @@ -152,6 +157,7 @@ export async function getTransferData({
sourceAddress,
transfer,
polkadotSigner,
statusCallback,
);

return [hash];
Expand Down
11 changes: 9 additions & 2 deletions packages/mrl/src/mrl.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
} from '@moonbeam-network/xcm-sdk';
import type { AnyChain, AssetAmount } from '@moonbeam-network/xcm-types';
import type { Signer } from '@polkadot/api/types';
import type { IKeyringPair } from '@polkadot/types/types';
import type { IKeyringPair, ISubmittableResult } from '@polkadot/types/types';
import type { TokenTransfer } from '@wormhole-foundation/sdk-connect';
import type { WalletClient } from 'viem';

Expand All @@ -16,6 +16,7 @@ export interface Signers {
export interface TransferData {
destination: DestinationTransferData;
getEstimate(amount: number | string): AssetAmount;
isAutomaticPossible: boolean;
max: AssetAmount;
min: AssetAmount;
moonChain: MoonChainTransferData;
Expand All @@ -24,6 +25,7 @@ export interface TransferData {
amount: bigint | number | string,
isAutomatic: boolean,
signers: Signers,
statusCallback?: (params: ISubmittableResult) => void,
): Promise<string[]>;
}

Expand All @@ -37,7 +39,12 @@ export interface SourceTransferData extends SourceChainTransferData {

export interface DestinationTransferData extends ChainTransferData {}

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

export interface ChainTransferData {
chain: AnyChain;
Expand Down
30 changes: 17 additions & 13 deletions packages/mrl/src/mrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,24 @@ export function Mrl(options?: MrlOptions) {
source,
destination,
});

return {
setAddresses({
sourceAddress,
destinationAddress,
}: {
sourceAddress: string;
destinationAddress: string;
}) {
return getTransferData({
route,
sourceAddress,
destinationAddress,
});
setIsAutomatic(isAutomatic: boolean) {
return {
setAddresses({
sourceAddress,
destinationAddress,
}: {
sourceAddress: string;
destinationAddress: string;
}) {
return getTransferData({
route,
sourceAddress,
destinationAddress,
isAutomatic,
});
},
};
},
};
},
Expand Down
Loading

0 comments on commit 8536459

Please sign in to comment.