Skip to content

Commit

Permalink
Add redeeming functionalities for Womrhole
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaurello committed Oct 24, 2024
1 parent 5b4c2a1 commit 458d6d8
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 14 deletions.
56 changes: 47 additions & 9 deletions examples/mrl-simple/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { cryptoWaitReady } from '@polkadot/util-crypto';
import { http, type Address, createWalletClient } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { moonbaseAlpha as moonbaseAlphaViem } from 'viem/chains';
import type { EvmSigner } from '../../packages/sdk/build';

// disable unnecessary warning logs
console.warn = () => null;
Expand Down Expand Up @@ -54,12 +55,46 @@ main()
.finally(() => process.exit());

async function main() {
const isAutomatic = true;
// await fromFantomToPeaq(ftm, 0.011, isAutomatic);
// await fromFantomToMoonbase(ftm, 0.01, isAutomatic);
const isAutomatic = false;

// await fromFantomToPeaq(agng, 10, isAutomatic);
// await fromFantomToMoonbase(dev, 1.23, isAutomatic);
// await fromMoonbaseToFantom(ftmwh, 0.01, isAutomatic);
await fromPeaqToFantom(agng, 20, isAutomatic);
// await fromPeaqToFantom(ftmwh, 0.0121, isAutomatic);
// await fromPeaqEvmToFantom(ftmwh, 1.5, isAutomatic);

// await redeemInMoonbaseAlpha();
await redeemInFantomTestnet();
}

async function redeemInMoonbaseAlpha() {
const tx =
'0x7c2771b74211420284c44116fe436003df31713a7ffefb034b2ce21ef3711cef';
const walletClient = createWalletClient({
account,
chain: moonbaseAlphaViem,
transport: http(),
});

const data = await Mrl().setTxHashToRedeem(tx).setRedeemChain(moonbaseAlpha);
console.log('data', data);

await data.redeem(walletClient as EvmSigner);
}

async function redeemInFantomTestnet() {
const tx =
'0xa0032ff270885f7278a42d4d974fceab9e4feb039263db35b09beafe57bd6683';
const walletClient = createWalletClient({
account,
chain: fantomTestnet.getViemChain(),
transport: http(),
});

const data = await Mrl().setTxHashToRedeem(tx).setRedeemChain(fantomTestnet);
console.log('data', data);

await data.redeem(walletClient as EvmSigner);
}

async function fromFantomToPeaq(
Expand All @@ -86,7 +121,7 @@ async function fromFantomToPeaq(

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

Expand All @@ -112,10 +147,11 @@ async function fromFantomToMoonbase(

console.log(data);

await data.transfer(amount, isAutomatic, {
const hash = await data.transfer(amount, isAutomatic, {
polkadotSigner: pair,
evmSigner: walletClient,
evmSigner: walletClient as EvmSigner,
});
console.log('hash', hash);
}

async function fromMoonbaseToFantom(
Expand All @@ -141,7 +177,7 @@ async function fromMoonbaseToFantom(

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

Expand Down Expand Up @@ -188,5 +224,7 @@ async function fromPeaqEvmToFantom(

console.log(data);

await data.transfer(amount, isAutomatic, { evmSigner: walletClient });
await data.transfer(amount, isAutomatic, {
evmSigner: walletClient as EvmSigner,
});
}
9 changes: 9 additions & 0 deletions packages/builder/src/mrl/MrlBuilder.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export type MrlConfigBuilder = ConfigBuilder<
MrlBuilderParams
>;

export type MrlRedeemConfigBuilder = ConfigBuilder<
ContractConfig,
MrlRedeemBuilderParams
>;

export interface MrlBuilderParams extends BuilderParams<AnyChain> {
isAutomatic: boolean;
moonApi: ApiPromise;
Expand All @@ -24,6 +29,10 @@ export interface MrlBuilderParams extends BuilderParams<AnyChain> {
transact?: Transact;
}

export interface MrlRedeemBuilderParams {
bytes?: Uint8Array;
}

export interface Transact {
call: HexString;
txWeight: {
Expand Down
28 changes: 28 additions & 0 deletions packages/builder/src/mrl/providers/wormhole/contract/Gmp/Gmp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Address } from 'viem';
import { ContractConfig } from '../../../../../contract';
import type { MrlRedeemConfigBuilder } from '../../../../MrlBuilder.interfaces';
import { GMP_ABI } from './GmpAbi';

const module = 'GMP';

export const GMP_CONTRACT_ADDRESS =
'0x0000000000000000000000000000000000000816';

export function Gmp() {
return {
wormholeTransferERC20: (): MrlRedeemConfigBuilder => ({
build: ({ bytes }) => {
const hex =
`0x${Buffer.from(bytes as Uint8Array).toString('hex')}` as Address;

return new ContractConfig({
address: GMP_CONTRACT_ADDRESS,
abi: GMP_ABI,
args: [hex],
func: 'wormholeTransferERC20',
module,
});
},
}),
};
}
15 changes: 15 additions & 0 deletions packages/builder/src/mrl/providers/wormhole/contract/Gmp/GmpAbi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const GMP_ABI = [
{
inputs: [
{
internalType: 'bytes',
name: 'vaa',
type: 'bytes',
},
],
name: 'wormholeTransferERC20',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
] as const;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Gmp';
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Batch } from './Batch';
import { Gmp } from './Gmp';
import { TokenBridge } from './TokenBridge';
import { TokenBridgeRelayer } from './TokenBridgeRelayer';

export function contract() {
return { Batch, TokenBridge, TokenBridgeRelayer };
return { Batch, Gmp, TokenBridge, TokenBridgeRelayer };
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import type {
MrlBuilderParams,
MrlConfigBuilder,
} from '../../../MrlBuilder.interfaces';
import { GMP_CONTRACT_ADDRESS } from '../contract/Gmp';
import { WormholeConfig } from './WormholeConfig';
import { wormholeFactory } from './wormholeFactory';

export const GMP_CONTRACT_ADDRESS =
'0x0000000000000000000000000000000000000816';

export function wormhole() {
return {
tokenTransfer: (): MrlConfigBuilder => ({
Expand Down
49 changes: 49 additions & 0 deletions packages/mrl/src/getTransferData/getRedeemData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { type ContractConfig, MrlBuilder } from '@moonbeam-network/xcm-builder';
import { EvmService, type EvmSigner } from '@moonbeam-network/xcm-sdk';
import type { EvmChain, EvmParachain } from '@moonbeam-network/xcm-types';
import { WormholeService } from '../services/wormhole';

export interface GetRedeemDataParams {
txId: string;
chain: EvmChain | EvmParachain;
}

export async function getRedeemData({ txId, chain }: GetRedeemDataParams) {
// TODO this is just for wormhole
const wh = WormholeService.create(chain);

const vaa = await wh.getVaa(txId);
const tokenTransfer = await wh.getTokenTransfer(vaa, txId);

const isXcm = vaa.payloadName === 'TransferWithPayload';

return {
vaa,
tokenTransfer,
async redeem(signer: EvmSigner) {
const isComplete = await wh.isComplete(vaa, tokenTransfer);

if (isComplete) {
throw new Error('This transaction is already finalized in Wormhole');
}

if (isXcm) {
const bytes = await wh.getVaaBytes(vaa);

const contract = MrlBuilder()
.wormhole()
.contract()
.Gmp()
.wormholeTransferERC20()
.build({ bytes }) as ContractConfig;

const evm = EvmService.create(chain);
const hash = await evm.transfer(signer, contract);

return hash;
}

return await wh.completeTransfer(tokenTransfer, signer);
},
};
}
13 changes: 12 additions & 1 deletion packages/mrl/src/mrl.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { SourceChainTransferData } from '@moonbeam-network/xcm-sdk';
import type {
EvmSigner,
SourceChainTransferData,
} 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 { TokenTransfer } from '@wormhole-foundation/sdk-connect';
import type { WalletClient } from 'viem';

export interface Signers {
Expand Down Expand Up @@ -41,3 +45,10 @@ export interface ChainTransferData {
fee: AssetAmount;
min: AssetAmount;
}

// TODO this is just for Wormhole
export type RedeemData = {
vaa: TokenTransfer.VAA;
tokenTransfer: TokenTransfer;
transfer(signer: EvmSigner): Promise<string[]>;
};
10 changes: 10 additions & 0 deletions packages/mrl/src/mrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import type {
AnyAsset,
AnyChain,
Ecosystem,
EvmChain,
EvmParachain,
} from '@moonbeam-network/xcm-types';
import { getRedeemData } from './getTransferData/getRedeemData';
import { getTransferData } from './getTransferData/getTransferData';

const DEFAULT_SERVICE = new ConfigService({ routes: mrlRoutesMap });
Expand Down Expand Up @@ -56,5 +59,12 @@ export function Mrl(options?: MrlOptions) {
},
};
},
setTxHashToRedeem(txId: string) {
return {
setRedeemChain(chain: EvmChain | EvmParachain) {
return getRedeemData({ txId, chain });
},
};
},
};
}
40 changes: 40 additions & 0 deletions packages/mrl/src/services/wormhole/WormholeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,44 @@ export class WormholeService {

return xfer.initiateTransfer(new WormholeWagmiSigner(this.chain, signer));
}

async getVaa(txId: string) {
return await TokenTransfer.getTransferVaa(this.#wh, txId);
}

async getVaaBytes(vaa: TokenTransfer.VAA) {
return (
(await this.#wh.getVaaBytes({
chain: vaa.emitterChain,
emitter: vaa.emitterAddress,
sequence: vaa.sequence,
})) || undefined
);
}

async getTokenTransfer(vaa: TokenTransfer.VAA, txId: string) {
return await TokenTransfer.from(this.#wh, {
chain: vaa.emitterChain,
txid: txId,
});
}

async isComplete(vaa: TokenTransfer.VAA, tokenTransfer: TokenTransfer) {
const isComplete = await TokenTransfer.isTransferComplete(
tokenTransfer.toChain,
vaa,
);
return isComplete;
}

async completeTransfer(
tokenTransfer: TokenTransfer,
signer: EvmSigner,
): Promise<string> {
const txIds = await tokenTransfer.completeTransfer(
new WormholeWagmiSigner(this.chain, signer),
);

return txIds[0];
}
}

0 comments on commit 458d6d8

Please sign in to comment.