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

Pass isAutomatic as parameter to transfer function #372

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
59 changes: 43 additions & 16 deletions examples/mrl-simple/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,25 @@ main()
.finally(() => process.exit());

async function main() {
// await fromFantomToPeaq(ftm, 0.011);
// await fromFantomToMoonbase(ftm, 0.01);
// await fromMoonbaseToFantom(dev, 0.01);
// await fromPeaqToFantom(ftmwh, 0.1);
await fromPeaqEvmToFantom(ftmwh, 0.12);
const isAutomatic = true;
// await fromFantomToPeaq(ftm, 0.011, isAutomatic);
// await fromFantomToMoonbase(ftm, 0.01, isAutomatic);
// await fromMoonbaseToFantom(ftmwh, 0.01, isAutomatic);
await fromPeaqToFantom(agng, 20, isAutomatic);
// await fromPeaqEvmToFantom(ftmwh, 1.5, isAutomatic);
}

async function fromFantomToPeaq(asset: Asset, amount: number) {
async function fromFantomToPeaq(
asset: Asset,
amount: number,
isAutomatic: boolean,
) {
const walletClient = createWalletClient({
account,
chain: fantomTestnet.getViemChain(),
transport: http(),
});

const data = await Mrl()
.setSource(fantomTestnet)
.setDestination(peaqAlphanet)
Expand All @@ -73,13 +84,17 @@ async function fromFantomToPeaq(asset: Asset, amount: number) {

console.log(data);

await data.transfer(amount, {
await data.transfer(amount, isAutomatic, {
polkadotSigner: pair,
evmSigner: walletClient,
});
}

async function fromFantomToMoonbase(asset: Asset, amount: number) {
async function fromFantomToMoonbase(
asset: Asset,
amount: number,
isAutomatic: boolean,
) {
const walletClient = createWalletClient({
account,
chain: fantomTestnet.getViemChain(),
Expand All @@ -89,21 +104,25 @@ async function fromFantomToMoonbase(asset: Asset, amount: number) {
const data = await Mrl()
.setSource(fantomTestnet)
.setDestination(moonbaseAlpha)
.setAsset(dev)
.setAsset(asset)
.setAddresses({
sourceAddress: account.address,
destinationAddress: account.address,
});

console.log(data);

await data.transfer(0.5, {
await data.transfer(amount, isAutomatic, {
polkadotSigner: pair,
evmSigner: walletClient,
});
}

async function fromMoonbaseToFantom(asset: Asset, amount: number) {
async function fromMoonbaseToFantom(
asset: Asset,
amount: number,
isAutomatic: boolean,
) {
const walletClient = createWalletClient({
account,
chain: moonbaseAlphaViem,
Expand All @@ -120,13 +139,17 @@ async function fromMoonbaseToFantom(asset: Asset, amount: number) {

console.log(data);

await data.transfer(amount, {
await data.transfer(amount, isAutomatic, {
polkadotSigner: pair,
evmSigner: walletClient,
});
}

async function fromPeaqToFantom(asset: Asset, amount: number) {
async function fromPeaqToFantom(
asset: Asset,
amount: number,
isAutomatic: boolean,
) {
const data = await Mrl()
.setSource(peaqAlphanet)
.setDestination(fantomTestnet)
Expand All @@ -138,12 +161,16 @@ async function fromPeaqToFantom(asset: Asset, amount: number) {

console.log(data);

await data.transfer(amount, {
await data.transfer(amount, isAutomatic, {
polkadotSigner: pair,
});
}

async function fromPeaqEvmToFantom(asset: Asset, amount: number) {
async function fromPeaqEvmToFantom(
asset: Asset,
amount: number,
isAutomatic: boolean,
) {
const walletClient = createWalletClient({
account,
chain: peaqEvmAlphanet.getViemChain(),
Expand All @@ -161,5 +188,5 @@ async function fromPeaqEvmToFantom(asset: Asset, amount: number) {

console.log(data);

await data.transfer(amount, { evmSigner: walletClient });
await data.transfer(amount, isAutomatic, { evmSigner: walletClient });
}
2 changes: 1 addition & 1 deletion packages/config/src/mrl-configs/fantomTestnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const fantomTestnetRoutes = new MrlChainRoutes({
},
},
mrl: {
isAutomaticPossible: false, // TODO
isAutomaticPossible: true,
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: ftmwh,
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 @@ -29,7 +29,7 @@ export const moonbaseAlphaRoutes = new MrlChainRoutes({
},
},
mrl: {
isAutomaticPossible: false, // TODO
isAutomaticPossible: true,
transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(),
moonChain: {
asset: ftmwh,
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/mrl-configs/peaqEvmAlphanet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const peaqEvmAlphanetRoutes = new MrlChainRoutes({
},
},
mrl: {
isAutomaticPossible: false, // TODO
isAutomaticPossible: true,
transfer: MrlBuilder()
.wormhole()
.contract()
Expand Down
5 changes: 5 additions & 0 deletions packages/mrl/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export async function getSourceData({
asset: balance,
destinationAddress,
feeAsset: feeBalance,
isAutomatic: route.mrl.isAutomaticPossible,
route,
sourceAddress,
});
Expand All @@ -113,6 +114,7 @@ export async function getSourceData({
transfer,
asset: balance,
feeAsset: feeBalance,
isAutomatic: route.mrl.isAutomaticPossible,
destinationAddress,
route,
sourceAddress,
Expand All @@ -128,6 +130,7 @@ export async function getSourceData({
return {
balance,
chain: source,
destinationFee,
destinationFeeBalance,
moonChainFeeBalance,
existentialDeposit,
Expand Down Expand Up @@ -202,6 +205,7 @@ export async function getRelayerFee({
chain,
destinationAddress,
feeAsset,
isAutomatic,
route,
sourceAddress,
transfer,
Expand All @@ -215,6 +219,7 @@ export async function getRelayerFee({
asset,
destinationAddress,
feeAsset,
isAutomatic,
route,
sourceAddress,
});
Expand Down
2 changes: 2 additions & 0 deletions packages/mrl/src/getTransferData/getTransferData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export async function getTransferData({
source: sourceData,
async transfer(
amount,
isAutomatic,
{ evmSigner, polkadotSigner }: Partial<Signers>,
): Promise<string[]> {
const source = route.source.chain;
Expand All @@ -122,6 +123,7 @@ export async function getTransferData({
asset,
destinationAddress,
feeAsset,
isAutomatic,
route,
sourceAddress,
});
Expand Down
9 changes: 8 additions & 1 deletion packages/mrl/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface BuildTransferParams {
asset: AssetAmount;
destinationAddress: string;
feeAsset: AssetAmount;
isAutomatic: boolean;
route: AssetRoute;
sourceAddress: string;
}
Expand All @@ -111,6 +112,11 @@ export async function buildTransfer(params: BuildTransferParams) {
`MrlConfigBuilder is not defined for source chain ${route.source.chain.name} and asset ${route.source.asset.originSymbol}`,
);
}
if (params.isAutomatic && !route.mrl.isAutomaticPossible) {
throw new Error(
`Route from source chain ${route.source.chain.name} and asset ${route.source.asset.originSymbol} does not allow the automatic option`,
);
}
const builderParams = await getMrlBuilderParams(params);

return route.mrl.transfer.build({
Expand All @@ -125,6 +131,7 @@ export async function getMrlBuilderParams({
asset,
destinationAddress,
feeAsset,
isAutomatic,
route,
sourceAddress,
}: BuildTransferParams): Promise<MrlBuilderParams> {
Expand All @@ -151,7 +158,7 @@ export async function getMrlBuilderParams({
destinationAddress,
destinationApi,
fee: feeAsset,
isAutomatic: route.mrl.isAutomaticPossible,
isAutomatic,
moonApi,
moonAsset: moonChain.nativeAsset,
moonChain,
Expand Down
1 change: 1 addition & 0 deletions packages/mrl/src/mrl.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface TransferData {
source: SourceTransferData;
transfer(
amount: bigint | number | string,
isAutomatic: boolean,
signers: Signers,
): Promise<string[]>;
}
Expand Down
70 changes: 69 additions & 1 deletion packages/sdk/tests/acceptance/__snapshots__/sdk.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,71 @@ exports[`sdk > getParachainBalances > on 'Moonbase Alpha' for address: '0x4E8214
]
`;

exports[`sdk > getParachainBalances > on 'Moonbase Beta' for address: '0x4E82143Af671Cc8201Bc7efCBbCED3A69e8…' > should get expected balances 1`] = `
[
_AssetAmount {
"address": undefined,
"amount": 100000000000000000n,
"decimals": 18,
"ids": {
"balanceId": "198801030527939140930753142903035039136",
"id": {
"ForeignAsset": "198801030527939140930753142903035039136",
},
},
"key": "ftmwh",
"min": undefined,
"originSymbol": "FTM.wh",
"symbol": undefined,
},
_AssetAmount {
"address": undefined,
"amount": 0n,
"decimals": 6,
"ids": {
"balanceId": "319794858556516669238969276945382613133",
"id": {
"ForeignAsset": "319794858556516669238969276945382613133",
},
},
"key": "usdcwh",
"min": undefined,
"originSymbol": "USDC.Wh",
"symbol": undefined,
},
_AssetAmount {
"address": undefined,
"amount": 1100000000000000000n,
"decimals": 18,
"ids": {
"balanceId": "85534404031760856987006367174489651085",
"id": {
"ForeignAsset": "85534404031760856987006367174489651085",
},
},
"key": "alan",
"min": undefined,
"originSymbol": "ALAN",
"symbol": undefined,
},
_AssetAmount {
"address": undefined,
"amount": 1100000000000000000n,
"decimals": 18,
"ids": {
"balanceId": "222902676330054289648817870329963141953",
"id": {
"ForeignAsset": "222902676330054289648817870329963141953",
},
},
"key": "dev",
"min": undefined,
"originSymbol": "DEV",
"symbol": undefined,
},
]
`;

exports[`sdk > getParachainBalances > on 'Moonbeam' for address: '0x4E82143Af671Cc8201Bc7efCBbCED3A69e8…' > should get expected balances 1`] = `
[
_AssetAmount {
Expand Down Expand Up @@ -788,9 +853,12 @@ exports[`sdk > getParachainBalances > on 'Moonbeam' for address: '0x4E82143Af671
"symbol": undefined,
},
_AssetAmount {
"address": "0xFfFffFFF18898CB5Fe1E88E668152B4f4052A947",
"address": "0xFfffffFfB3229c8E7657eABEA704d5e75246e544",
"amount": 0n,
"decimals": 12,
"ids": {
"id": "238111524681612888331172110363070489924",
},
"key": "neuro",
"min": undefined,
"originSymbol": "NEURO",
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/tests/acceptance/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ const config: { chain: AnyParachain; address: string }[] = [
{ chain: centrifuge, address: substrateAddress },
{ chain: hydrationAlphanet, address: hydrationAddress },
{ chain: hydrationAlphanet, address: substrateAddress },
// {
// chain: moonbaseBeta,
// address: '0x4E82143Af671Cc8201Bc7efCBbCED3A69e84405e',
// },
{
chain: moonbaseBeta,
address: '0x4E82143Af671Cc8201Bc7efCBbCED3A69e84405e',
},
{ chain: moonbaseAlpha, address: moonEvmAddress },
{ chain: peaqEvmAlphanet, address: moonEvmAddress },
{ chain: peaqAlphanet, address: substrateAddress },
Expand Down