Skip to content

Commit

Permalink
Fixed GMP message builder (#326)
Browse files Browse the repository at this point in the history
* Fixed GMP message builder

* Changed to import type
  • Loading branch information
ekenigs authored Aug 19, 2024
1 parent 1a61f6a commit 27d9ccb
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 206 deletions.
2 changes: 2 additions & 0 deletions packages/builder/fixtures/builderParamsMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export const wormholeConfigBuilderPrams: WormholeConfigBuilderPrams = {
asset: testAssetAmount,
destination: alphanetAssetHubMock,
destinationAddress: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
moonApi: {} as any,
moonChain: moonbaseAlphaMock,
source: fantomTestnet,
sourceAddress: '0xeF46c7649270C912704fB09B75097f6E32208b85',
Expand All @@ -180,6 +181,7 @@ export const wormholeToMoonchainConfigBuilderPrams: WormholeConfigBuilderPrams =
asset: testAssetAmount,
destination: moonbaseAlphaMock,
destinationAddress: '0x98891e5FD24Ef33A488A47101F65D212Ff6E650E',
moonApi: {} as any,
moonChain: moonbaseAlphaMock,
source: fantomTestnet,
sourceAddress: '0xeF46c7649270C912704fB09B75097f6E32208b85',
Expand Down
2 changes: 2 additions & 0 deletions packages/builder/src/wormhole/WormholeBuilder.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AssetAmount,
EvmParachain,
} from '@moonbeam-network/xcm-types';
import type { ApiPromise } from '@polkadot/api';
import { WormholeConfig } from './WormholeConfig';

export interface WormholeConfigBuilder {
Expand All @@ -13,6 +14,7 @@ export interface WormholeConfigBuilderPrams {
asset: AssetAmount;
destination: AnyChain;
destinationAddress: string;
moonApi: ApiPromise;
moonChain: EvmParachain;
source: AnyChain;
sourceAddress: string;
Expand Down
13 changes: 10 additions & 3 deletions packages/builder/src/wormhole/WormholeBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { describe, expect, it } from 'vitest';

import { getPolkadotApi } from '@moonbeam-network/xcm-utils';
import {
wormholeConfigBuilderPrams,
wormholeToMoonchainConfigBuilderPrams,
} from '../../fixtures';
import { WormholeBuilder } from './WormholeBuilder';

describe('wormholeBuilder', () => {
describe('wormholeBuilder', async () => {
const moonApi = await getPolkadotApi(
'wss://wss.api.moonbase.moonbeam.network',
);

describe('tokenTransfer with isAutomatic=true', () => {
const transfer = WormholeBuilder().tokenTransfer({ isAutomatic: true });

it('should be correct config', () => {
expect(transfer.build(wormholeConfigBuilderPrams)).toMatchSnapshot();
expect(
transfer.build({ ...wormholeConfigBuilderPrams, moonApi }),
).toMatchSnapshot();
});
});

Expand All @@ -22,7 +29,7 @@ describe('wormholeBuilder', () => {

it('should be correct config to moon chain', () => {
expect(
transfer.build(wormholeToMoonchainConfigBuilderPrams),
transfer.build({ ...wormholeToMoonchainConfigBuilderPrams, moonApi }),
).toMatchSnapshot();
});
});
Expand Down
57 changes: 32 additions & 25 deletions packages/builder/src/wormhole/WormholeBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import { Wormhole } from '@wormhole-foundation/sdk-connect';
import { EvmParachain, Parachain } from '@moonbeam-network/xcm-types';
import { stringify, stringToU8a } from '@polkadot/util';
import { evmToAddress } from '@polkadot/util-crypto/address';
import {
WormholeConfigBuilder,
Expand All @@ -25,6 +24,7 @@ export function WormholeBuilder() {
asset,
destination,
destinationAddress,
moonApi,
moonChain,
source,
sourceAddress,
Expand Down Expand Up @@ -57,7 +57,7 @@ export function WormholeBuilder() {
isAutomatic,
isDestinationMoonChain
? undefined
: getPayload({ destination, destinationAddress }),
: getPayload({ destination, destinationAddress, moonApi }),
],
func: 'tokenTransfer',
});
Expand All @@ -66,12 +66,18 @@ export function WormholeBuilder() {
};
}

/*
* Extrinsic to GMP precompile
* https://docs.moonbeam.network/builders/ethereum/precompiles/interoperability/gmp/
*/
export function getPayload({
moonApi,
destination,
destinationAddress,
}: Pick<WormholeConfigBuilderPrams, 'destination' | 'destinationAddress'>):
| Uint8Array
| undefined {
}: Pick<
WormholeConfigBuilderPrams,
'destination' | 'destinationAddress' | 'moonApi'
>): Uint8Array | undefined {
if (!Parachain.is(destination) && !EvmParachain.is(destination)) {
throw new Error(
`Destination ${destination.name} is not a Parachain or EvmParachain`,
Expand All @@ -82,26 +88,27 @@ export function getPayload({
const isPeaqEvm =
destination.key === 'peaq-evm-Alphanet' || destination.key === 'peaq-evm';

// TODO: this is not working I will need to use API to create type
return stringToU8a(
stringify({
destination: {
V3: {
parents: 1,
interior: {
X2: [
{
Parachain: destination.parachainId,
},
getExtrinsicAccount(
isPeaqEvm
? evmToAddress(destinationAddress)
: destinationAddress,
),
],
const multilocation = moonApi.createType('XcmVersionedLocation', {
V3: {
parents: 1,
interior: {
X2: [
{
Parachain: destination.parachainId,
},
},
getExtrinsicAccount(
isPeaqEvm ? evmToAddress(destinationAddress) : destinationAddress,
),
],
},
}),
);
},
});
const action = moonApi.createType('XcmRoutingUserAction', {
destination: multilocation,
});
const versioned = moonApi.createType('VersionedUserAction', {
V1: action,
});

return versioned.toU8a();
}
215 changes: 37 additions & 178 deletions packages/builder/src/wormhole/__snapshots__/WormholeBuilder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,187 +59,46 @@ WormholeConfig {
},
true,
Uint8Array [
123,
34,
100,
101,
115,
116,
105,
110,
97,
116,
105,
111,
110,
34,
58,
123,
34,
86,
51,
34,
58,
123,
34,
112,
97,
114,
101,
110,
116,
115,
34,
58,
49,
44,
34,
105,
110,
116,
101,
114,
105,
111,
114,
34,
58,
123,
34,
88,
50,
34,
58,
91,
123,
34,
80,
97,
114,
97,
99,
104,
97,
105,
110,
34,
58,
49,
48,
48,
49,
125,
44,
123,
34,
65,
99,
99,
111,
117,
110,
116,
73,
100,
51,
50,
34,
58,
123,
34,
105,
100,
34,
58,
34,
48,
120,
100,
52,
51,
53,
57,
51,
99,
55,
49,
0,
3,
1,
2,
0,
165,
15,
1,
0,
212,
53,
102,
100,
100,
51,
49,
99,
54,
49,
49,
52,
49,
147,
199,
21,
253,
211,
28,
97,
98,
100,
48,
52,
97,
57,
57,
102,
100,
54,
56,
50,
50,
99,
56,
53,
53,
56,
56,
53,
52,
99,
99,
100,
101,
51,
57,
97,
53,
54,
56,
52,
101,
55,
97,
53,
54,
100,
97,
50,
55,
100,
34,
20,
26,
189,
4,
169,
159,
214,
130,
44,
34,
110,
101,
116,
119,
111,
114,
107,
34,
58,
110,
117,
108,
108,
125,
125,
93,
125,
125,
125,
133,
88,
133,
76,
205,
227,
154,
86,
132,
231,
165,
109,
162,
125,
],
],
Expand Down

0 comments on commit 27d9ccb

Please sign in to comment.