Skip to content

Commit

Permalink
Support withdraw to chains with Ethereum accounts (#65)
Browse files Browse the repository at this point in the history
* pangoro initial configuration

* Support withdraw to chains with Ethereum accounts

* Updated param name

* Updated tests and snapshots

* Review

---------

Co-authored-by: Mario J Maurello <[email protected]>
  • Loading branch information
fgamundi and mmaurello authored Mar 17, 2023
1 parent 7b751bf commit f6028be
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 27 deletions.
37 changes: 37 additions & 0 deletions packages/config/src/config/moonbase/assets/paring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { AssetSymbol, ChainKey } from '../../../constants';
import { PolkadotXcmExtrinsicSuccessEvent } from '../../../extrinsic';
import {
assets,
balance,
chains,
extrinsic,
withdraw,
} from '../moonbase.common';
import { MoonbaseXcmConfig } from '../moonbase.interfaces';

const asset = assets[AssetSymbol.PARING];
const origin = chains[ChainKey.DarwiniaPangoro];

export const PARING: MoonbaseXcmConfig = {
asset,
origin,
deposit: {
[origin.key]: {
source: origin,
balance: balance.system(),
extrinsic: extrinsic
.polkadotXcm()
.limitedReserveTransferAssets()
.successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted)
.V1V2()
.X1(),
},
},
withdraw: {
[origin.key]: withdraw.xTokens({
balance: balance.system(),
destination: origin,
feePerWeight: 1_000_000_000,
}),
},
};
14 changes: 10 additions & 4 deletions packages/config/src/config/moonbase/moonbase.assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const MOONBASE_ASSETS = <const>[
AssetSymbol.DEV,
AssetSymbol.LIT,
AssetSymbol.NEER,
AssetSymbol.PARING,
AssetSymbol.TT1,
AssetSymbol.UNIT,
];
Expand Down Expand Up @@ -39,10 +40,10 @@ export const MOONBASE_ASSETS_MAP: AssetsMap<MoonbaseAssets> = {
erc20Id: '0xffffffff2754f0bdf7eb215503c69204ccd61c5d',
originSymbol: AssetSymbol.NEER,
},
[AssetSymbol.UNIT]: {
id: '42259045809535163221576417993425387648',
erc20Id: '0xffffffff1fcacbd218edc0eba20fc2308c778080',
originSymbol: AssetSymbol.UNIT,
[AssetSymbol.PARING]: {
id: '173481220575862801646329923366065693029',
erc20Id: '0xffffffff8283448b3cb519ca4732f2dddc6a6165',
originSymbol: AssetSymbol.PARING,
},
[AssetSymbol.TT1]: {
id: '156305701417244550631956600137082963628',
Expand All @@ -52,4 +53,9 @@ export const MOONBASE_ASSETS_MAP: AssetsMap<MoonbaseAssets> = {
[ChainKey.StatemineAlphanet]: 2,
},
},
[AssetSymbol.UNIT]: {
id: '42259045809535163221576417993425387648',
erc20Id: '0xffffffff1fcacbd218edc0eba20fc2308c778080',
originSymbol: AssetSymbol.UNIT,
},
};
11 changes: 11 additions & 0 deletions packages/config/src/config/moonbase/moonbase.chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const MOONBASE_CHAINS = <const>[
ChainKey.AlphanetRelay,
ChainKey.LitentryAlphanet,
ChainKey.BitCountryPioneer,
ChainKey.DarwiniaPangoro,
ChainKey.MoonbaseBeta,
ChainKey.StatemineAlphanet,
ChainKey.UniqueAlpha,
Expand Down Expand Up @@ -33,6 +34,16 @@ export const MOONBASE_CHAINS_MAP: ChainsMap<MoonbaseChains> = {
genesisHash:
'0xb27da7332d3a229f0d5f2a83f711b3f74a70f22b68021e92c37817057de58e74',
},
[ChainKey.DarwiniaPangoro]: {
key: ChainKey.DarwiniaPangoro,
name: 'Pangoro',
ws: 'wss://pangoro-rpc.darwinia.network',
weight: 1_000_000_000,
parachainId: 2105,
usesEthereumAccounts: true,
genesisHash:
'0xaaa8b33b723b30b44e45e4e6c01936cc92e7559b4184fb0cee2853d55610fcbf',
},
[ChainKey.LitentryAlphanet]: {
key: ChainKey.LitentryAlphanet,
name: 'Litentry Alphanet',
Expand Down
2 changes: 2 additions & 0 deletions packages/config/src/config/moonbase/moonbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BIT } from './assets/bit';
import { DEV } from './assets/dev';
import { LIT } from './assets/lit';
import { NEER } from './assets/neer';
import { PARING } from './assets/paring';
import { TT1 } from './assets/tt1';
import { UNIT } from './assets/unit';

Expand All @@ -15,6 +16,7 @@ export const MOONBASE_CONFIGS: MoonbaseXcmConfigs = {
[AssetSymbol.DEV]: DEV,
[AssetSymbol.LIT]: LIT,
[AssetSymbol.NEER]: NEER,
[AssetSymbol.PARING]: PARING,
[AssetSymbol.TT1]: TT1,
[AssetSymbol.UNIT]: UNIT,
};
1 change: 1 addition & 0 deletions packages/config/src/constants/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export enum AssetSymbol {
MOVR = 'MOVR',
NEER = 'NEER',
PARA = 'PARA',
PARING = 'PARING',
PHA = 'PHA',
RING = 'RING',
RMRK = 'RMRK',
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/constants/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export enum ChainKey {
Crab = 'Crab',
CrustShadow = 'CrustShadow',
Darwinia = 'Darwinia',
DarwiniaPangoro = 'DarwiniaPangoro',
Integritee = 'Integritee',
Interlay = 'Interlay',
Karura = 'Karura',
Expand Down
3 changes: 2 additions & 1 deletion packages/config/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export interface Chain<ChainKeys extends ChainKey = ChainKey>
* unitsPerSecond = weightPerSecond * baseExtrinsicCost / baseExtrinsicWeight
*/
unitsPerSecond?: bigint;
ss58Format: number;
ss58Format?: number;
usesEthereumAccounts?: boolean;
genesisHash: string;
}

Expand Down
31 changes: 25 additions & 6 deletions packages/config/src/withdraw/__snapshots__/withdraw.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`withdraw xTokens eth account should get params with parachain id 1`] = `
[
1,
[
"0x000000064",
"0x03ef46c7649270c912704fb09b75097f6e32208b8500",
],
]
`;

exports[`withdraw xTokens eth account should get params without parachain id 1`] = `
[
1,
[
"0x03ef46c7649270c912704fb09b75097f6e32208b8500",
],
]
`;

exports[`withdraw xTokens should be correct withdraw config 1`] = `
{
"balance": "<BALANCE>",
"destination": {
"parachainId": 1000,
"parachainId": 100,
},
"feePerWeight": 8,
"getParams": [Function],
Expand All @@ -14,21 +33,21 @@ exports[`withdraw xTokens should be correct withdraw config 1`] = `
}
`;

exports[`withdraw xTokens should get params with parachain id 1`] = `
exports[`withdraw xTokens substrate account should get params with parachain id 1`] = `
[
1,
[
"0x00000003e8",
"0x01ef46c7649270c912704fb09b75097f6e32208b8500",
"0x000000064",
"0x01c4db7bcb733e117c0b34ac96354b10d47e84a006b9e7e66a229d174e8ff2a06300",
],
]
`;

exports[`withdraw xTokens should get params without parachain id 1`] = `
exports[`withdraw xTokens substrate account should get params without parachain id 1`] = `
[
1,
[
"0x01ef46c7649270c912704fb09b75097f6e32208b8500",
"0x01c4db7bcb733e117c0b34ac96354b10d47e84a006b9e7e66a229d174e8ff2a06300",
],
]
`;
5 changes: 4 additions & 1 deletion packages/config/src/withdraw/withdraw.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export interface WithdrawXTokensConfig<
sourceMinBalance?: MinBalanceConfig;
xcmFeeAsset?: WithdrawXcmFeeAsset<Symbols>;
weight: number;
getParams: (account: string) => WithdrawXTokensParams;
getParams: (
account: string,
usesEthereumAccounts: boolean | undefined,
) => WithdrawXTokensParams;
}

export type WithdrawXTokensParams = [
Expand Down
40 changes: 30 additions & 10 deletions packages/config/src/withdraw/withdraw.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
import { createWithdrawBuilder } from './withdraw';

describe('withdraw', () => {
const account = '0xeF46c7649270C912704fB09B75097f6E32208b85';
const withdraw = createWithdrawBuilder();

describe('xTokens', () => {
const cfg = withdraw.xTokens({
balance: '<BALANCE>' as any,
destination: { parachainId: 1000 } as any,
destination: { parachainId: 100 } as any,
feePerWeight: 8,
});

it('should be correct withdraw config', () => {
expect(cfg).toMatchSnapshot();
});
describe('substrate account', () => {
const account = '5GWpSdqkkKGZmdKQ9nkSF7TmHp6JWt28BMGQNuG4MXtSvq3e';

it('should get params with parachain id', () => {
expect(cfg.getParams(account)).toMatchSnapshot();
it('should get params with parachain id', () => {
expect(cfg.getParams(account, false)).toMatchSnapshot();
});

it('should get params without parachain id', () => {
const cfg2 = withdraw.xTokens({
balance: '<BALANCE>' as any,
destination: {} as any,
feePerWeight: 8,
});

expect(cfg2.getParams(account, false)).toMatchSnapshot();
});
});

it('should get params without parachain id', () => {
const cfg2 = withdraw.xTokens({
balance: '<BALANCE>' as any,
destination: {} as any,
feePerWeight: 8,
describe('eth account', () => {
const account = '0xeF46c7649270C912704fB09B75097f6E32208b85';

it('should get params with parachain id', () => {
expect(cfg.getParams(account, true)).toMatchSnapshot();
});

expect(cfg2.getParams(account)).toMatchSnapshot();
it('should get params without parachain id', () => {
const cfg2 = withdraw.xTokens({
balance: '<BALANCE>' as any,
destination: {} as any,
feePerWeight: 8,
});

expect(cfg2.getParams(account, true)).toMatchSnapshot();
});
});
});
});
13 changes: 11 additions & 2 deletions packages/config/src/withdraw/withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@ function xTokens<Symbols extends AssetSymbol = AssetSymbol>({
sourceMinBalance,
xcmFeeAsset,
weight,
getParams: (account: string) => {
getParams: (account: string, usesEthereumAccounts = false) => {
const { parachainId } = destination;
const acc = `0x01${u8aToHex(decodeAddress(account), -1, false)}00`;
// 01: AccountId32
// 03: AccountKey20
// https://docs.moonbeam.network/builders/interoperability/xcm/xc20/xtokens/#building-the-precompile-multilocation
const accountType = usesEthereumAccounts ? '03' : '01';

const acc = `0x${accountType}${u8aToHex(
decodeAddress(account),
-1,
false,
)}00`;

return [
1,
Expand Down
10 changes: 7 additions & 3 deletions packages/sdk/src/contracts/XTokensContract/XTokensContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ export class XTokensContract<Symbols extends AssetSymbol = AssetSymbol> {
config: WithdrawXTokensConfig<Symbols>,
minAmount: bigint,
): Promise<TransactionResponse> {
const { usesEthereumAccounts } = config.destination;
const params = config.getParams(account, usesEthereumAccounts);

if (config.xcmFeeAsset) {
return this.#contract.transferMultiCurrencies(
[
[config.xcmFeeAsset.asset.erc20Id, minAmount],
[asset.erc20Id, amount],
],
0,
config.getParams(account),
params,
config.weight,
);
}

return this.#contract.transfer(
asset.erc20Id,
amount,
config.getParams(account),
params,
config.weight,
);
}
Expand All @@ -56,7 +60,7 @@ export class XTokensContract<Symbols extends AssetSymbol = AssetSymbol> {
await this.#contract.estimateGas.transfer(
asset.erc20Id,
amount,
config.getParams(account),
config.getParams(account, config.destination.usesEthereumAccounts),
config.weight,
)
).toBigInt();
Expand Down

0 comments on commit f6028be

Please sign in to comment.