Skip to content

Commit

Permalink
add AccountId32 support (#161)
Browse files Browse the repository at this point in the history
* add AccountId32 support

* Changeset and removed ethers

---------

Co-authored-by: Elmar Kenigs <[email protected]>
  • Loading branch information
duxiaofeng and ekenigs authored Nov 9, 2023
1 parent 043d4e6 commit df29a0b
Show file tree
Hide file tree
Showing 17 changed files with 550 additions and 147 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-tomatoes-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moonbeam-network/xcm-builder': patch
---

Add AccountId32 support
82 changes: 2 additions & 80 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions packages/builder/fixtures/builderParamsMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ import {
} from '@moonbeam-network/xcm-types';
import { ExtrinsicConfigBuilderPrams } from '../src/extrinsic';

export const interlayTestnetMock = new Parachain({
ecosystem: Ecosystem.AlphanetRelay,
genesisHash:
'0x418ae94c9fce02b1ab3b5bc211cd2f2133426f2861d97482bbdfdac1bbb0fb92',
isTestChain: true,
key: 'interlay-testnet',
name: 'Interlay Testnet',
parachainId: 2032,
ss58Format: 2032,
ws: 'wss://interlay-moonbeam-alphanet.interlay.io/',
});

export const alphanetAssetHubMock = new Parachain({
ecosystem: Ecosystem.AlphanetRelay,
genesisHash:
Expand Down Expand Up @@ -41,3 +53,14 @@ export const buildParamsMock: ExtrinsicConfigBuilderPrams = {
palletInstance: 10,
source: alphanetAssetHubMock,
};

export const buildParachainParamsMock: ExtrinsicConfigBuilderPrams = {
address: 'wd84XqsQ4LVzhmTBVd4s5ApGt9sBnnk8K7Q5PhBwwhxwqgm1u',
amount: 99_000_000_000n,
asset: 'USDT',
destination: interlayTestnetMock,
fee: 5_000_000_000n,
feeAsset: 'RMRK',
palletInstance: 10,
source: alphanetAssetHubMock,
};
1 change: 0 additions & 1 deletion packages/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"@moonbeam-network/xcm-types": "1.0.1",
"@moonbeam-network/xcm-utils": "1.0.3",
"big.js": "^6.2.1",
"ethers": "^5.7.2",
"type-fest": "^3.8.0"
},
"peerDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions packages/builder/src/extrinsic/ExtrinsicBuilder.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { SubmittableExtrinsicFunction } from '@polkadot/api/types';
import { getTypeDef } from '@polkadot/types';
import { u8aToHex } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
import { XcmVersion } from './ExtrinsicBuilder.interfaces';

export function getExtrinsicArgumentVersion(
Expand Down Expand Up @@ -34,3 +36,21 @@ export function getExtrinsicArgumentVersion(

throw new Error("Can't find Xcm version");
}

export function getExtrinsicAccount(address: string) {
const isEthAddress = address.length === 42;

return isEthAddress
? {
AccountKey20: {
key: address,
network: 'Any',
},
}
: {
AccountId32: {
id: u8aToHex(decodeAddress(address)),
network: null,
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,44 @@ exports[`eqBalances transferXcm should get correct arguments 1`] = `
{
"AccountKey20": {
"key": "0xeF46c7649270C912704fB09B75097f6E32208b85",
"network": "Any",
},
},
],
},
"parents": 1,
},
]
`;

exports[`eqBalances transferXcm to parachain should be correct config 1`] = `
ExtrinsicConfig {
"func": "transferXcm",
"getArgs": [Function],
"module": "eqBalances",
"type": "Substrate",
}
`;

exports[`eqBalances transferXcm to parachain should get correct arguments 1`] = `
[
[
"USDT",
99000000000n,
],
[
"RMRK",
5000000000n,
],
{
"interior": {
"X2": [
{
"Parachain": 2032,
},
{
"AccountId32": {
"id": "0x18e6c7ad45fbab18778710642072fdd057470b76d4d568b44dea6accd5b0c423",
"network": null,
},
},
Expand Down Expand Up @@ -60,6 +98,39 @@ exports[`eqBalances xcmTransfer should get correct arguments 1`] = `
{
"AccountKey20": {
"key": "0xeF46c7649270C912704fB09B75097f6E32208b85",
"network": "Any",
},
},
],
},
"parents": 1,
},
"ThisAccWillPay",
]
`;

exports[`eqBalances xcmTransfer to parachain should be correct config 1`] = `
ExtrinsicConfig {
"func": "xcmTransfer",
"getArgs": [Function],
"module": "eqBalances",
"type": "Substrate",
}
`;

exports[`eqBalances xcmTransfer to parachain should get correct arguments 1`] = `
[
"USDT",
99000000000n,
{
"interior": {
"X2": [
{
"Parachain": 2032,
},
{
"AccountId32": {
"id": "0x18e6c7ad45fbab18778710642072fdd057470b76d4d568b44dea6accd5b0c423",
"network": null,
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { buildParamsMock } from '../../../../fixtures';
import {
buildParachainParamsMock,
buildParamsMock,
} from '../../../../fixtures';
import { eqBalances } from './eqBalances';

describe('eqBalances', () => {
Expand All @@ -25,4 +28,32 @@ describe('eqBalances', () => {
expect(extrinsic.getArgs({} as any)).toMatchSnapshot();
});
});

describe('xcmTransfer to parachain', () => {
const extrinsic = eqBalances()
.xcmTransfer()
.build(buildParachainParamsMock);

it('should be correct config', () => {
expect(extrinsic).toMatchSnapshot();
});

it('should get correct arguments', () => {
expect(extrinsic.getArgs({} as any)).toMatchSnapshot();
});
});

describe('transferXcm to parachain', () => {
const extrinsic = eqBalances()
.transferXcm()
.build(buildParachainParamsMock);

it('should be correct config', () => {
expect(extrinsic).toMatchSnapshot();
});

it('should get correct arguments', () => {
expect(extrinsic.getArgs({} as any)).toMatchSnapshot();
});
});
});
15 changes: 3 additions & 12 deletions packages/builder/src/extrinsic/pallets/eqBalances/eqBalances.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable sort-keys */
import { ExtrinsicConfigBuilder } from '../../ExtrinsicBuilder.interfaces';
import { getExtrinsicAccount } from '../../ExtrinsicBuilder.utils';
import { ExtrinsicConfig } from '../../ExtrinsicConfig';

export enum EqBalancesFee {
Expand Down Expand Up @@ -27,12 +28,7 @@ export function eqBalances() {
{
Parachain: destination.parachainId,
},
{
AccountKey20: {
network: null,
key: address,
},
},
getExtrinsicAccount(address),
],
},
},
Expand All @@ -58,12 +54,7 @@ export function eqBalances() {
{
Parachain: destination.parachainId,
},
{
AccountKey20: {
network: null,
key: address,
},
},
getExtrinsicAccount(address),
],
},
},
Expand Down
Loading

0 comments on commit df29a0b

Please sign in to comment.