Skip to content

Commit

Permalink
Foreign assets - Snowbridge Assets integration (#383)
Browse files Browse the repository at this point in the history
* add foreignAssets balance checking

* add assets configurations

* add extrinsic builders for foreign assets

* add wsethe and wbtce configurations

* add changeset

* add tests to new functions

* update acceptance snaps
  • Loading branch information
mmaurello authored Nov 22, 2024
1 parent b4e9992 commit 5d3e8b4
Show file tree
Hide file tree
Showing 16 changed files with 597 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/moody-snakes-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@moonbeam-network/xcm-builder': minor
'@moonbeam-network/xcm-config': patch
---

Add foreign assets configuration and Snowbridge assets tranfers between Asset Hub and Moonbeam
11 changes: 11 additions & 0 deletions packages/builder/fixtures/builderParamsMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,14 @@ export const buildParachainParamsMock: ExtrinsicConfigBuilderPrams = {
palletInstance: 10,
source: alphanetAssetHubMock,
};

export const buildParamsMockEth: ExtrinsicConfigBuilderPrams = {
address: '0xeF46c7649270C912704fB09B75097f6E32208b85',
amount: 99_000_000_000n,
asset: '0xeF46c7649270C912704fB09B75097f6E32208b85',
destination: moonbaseAlphaMock,
fee: 5_000_000_000n,
feeAsset: 'WETH.e',
palletInstance: 10,
source: alphanetAssetHubMock,
};
23 changes: 23 additions & 0 deletions packages/builder/src/asset-min/AssetMinBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,27 @@ describe('assetMinBuilder', () => {
});
});
});

describe('foreignAssets', () => {
describe('asset', () => {
const config = AssetMinBuilder()
.foreignAssets()
.asset()
.build({ asset: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' });

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

it('should transform correctly', async () => {
await expect(
config.transform({
unwrapOrDefault: () => ({
minBalance: balanceOf(999),
}),
}),
).resolves.toMatchSnapshot();
});
});
});
});
35 changes: 35 additions & 0 deletions packages/builder/src/asset-min/AssetMinBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import { Option } from '@polkadot/types';
import { PalletAssetsAssetDetails } from '@polkadot/types/lookup';
import { getExtrinsicAccount } from '../extrinsic/ExtrinsicBuilder.utils';
import { SubstrateQueryConfig } from '../types/substrate/SubstrateQueryConfig';
import { AssetMinConfigBuilder } from './AssetMinBuilder.interfaces';

export function AssetMinBuilder() {
return {
assetRegistry,
assets,
foreignAssets,
};
}

Expand Down Expand Up @@ -57,3 +59,36 @@ function assets() {
}),
};
}

function foreignAssets() {
return {
asset: (): AssetMinConfigBuilder => ({
build: ({ asset }) => {
const multilocation = {
parents: 2,
interior: {
X2: [
{
globalconsensus: {
ethereum: {
chainId: 1,
},
},
},
getExtrinsicAccount(asset as string),
],
},
};
return new SubstrateQueryConfig({
module: 'foreignAssets',
func: 'asset',
args: [multilocation],
transform: async (
response: Option<PalletAssetsAssetDetails>,
): Promise<bigint> =>
response.unwrapOrDefault().minBalance.toBigInt(),
});
},
}),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,38 @@ SubstrateQueryConfig {

exports[`assetMinBuilder > assets > asset > should transform correctly 1`] = `999n`;

exports[`assetMinBuilder > foreignAssets > asset > should be correct config 1`] = `
SubstrateQueryConfig {
"args": [
{
"interior": {
"X2": [
{
"globalconsensus": {
"ethereum": {
"chainId": 1,
},
},
},
{
"AccountKey20": {
"key": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
},
},
],
},
"parents": 2,
},
],
"func": "asset",
"module": "foreignAssets",
"transform": [Function],
"type": "Substrate",
}
`;

exports[`assetMinBuilder > foreignAssets > asset > should transform correctly 1`] = `999n`;

exports[`assetMinBuilder assetRegistry assetMetadatas should be correct config 1`] = `
SubstrateQueryConfig {
"args": [
Expand Down
25 changes: 25 additions & 0 deletions packages/builder/src/balance/BalanceBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ describe('balanceBuilder', () => {
});
});

describe('foreignAssets', () => {
describe('account', () => {
const config = BalanceBuilder()
.substrate()
.foreignAssets()
.account()
.build({
address: account,
asset: `0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2`,
}) as SubstrateQueryConfig;

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

it('should transform correctly', async () => {
await expect(
config.transform({
unwrapOrDefault: () => ({ balance: balanceOf(999) }),
}),
).resolves.toMatchSnapshot();
});
});
});

describe('system', () => {
describe('account', () => {
const config = BalanceBuilder()
Expand Down
29 changes: 29 additions & 0 deletions packages/builder/src/balance/BalanceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { isString } from '@polkadot/util';
import { evmToAddress } from '@polkadot/util-crypto';
import { ContractConfig } from '../contract';
import { getExtrinsicAccount } from '../extrinsic/ExtrinsicBuilder.utils';
import { SubstrateQueryConfig } from '../types/substrate/SubstrateQueryConfig';
import {
BalanceConfigBuilder,
Expand All @@ -34,6 +35,7 @@ export function evm() {
export function substrate() {
return {
assets,
foreignAssets,
system,
tokens,
};
Expand Down Expand Up @@ -72,6 +74,33 @@ function assets() {
};
}

function foreignAssets() {
return {
account: (): BalanceConfigBuilder => ({
build: ({ address, asset }) => {
const multilocation = {
parents: 2,
interior: {
X2: [
{ GlobalConsensus: { ethereum: { chainId: 1 } } },
getExtrinsicAccount(asset as string),
],
},
};

return new SubstrateQueryConfig({
module: 'foreignAssets',
func: 'account',
args: [multilocation, address],
transform: async (
response: Option<PalletAssetsAssetAccount>,
): Promise<bigint> => response.unwrapOrDefault().balance.toBigInt(),
});
},
}),
};
}

function system() {
return {
account: (): BalanceConfigBuilder => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,39 @@ SubstrateQueryConfig {

exports[`balanceBuilder > assets > account > should transform correctly 1`] = `999n`;

exports[`balanceBuilder > foreignAssets > account > should be correct config 1`] = `
SubstrateQueryConfig {
"args": [
{
"interior": {
"X2": [
{
"GlobalConsensus": {
"ethereum": {
"chainId": 1,
},
},
},
{
"AccountKey20": {
"key": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
},
},
],
},
"parents": 2,
},
"<ACCOUNT>",
],
"func": "account",
"module": "foreignAssets",
"transform": [Function],
"type": "Substrate",
}
`;

exports[`balanceBuilder > foreignAssets > account > should transform correctly 1`] = `999n`;

exports[`balanceBuilder > system > account > should be correct config 1`] = `
SubstrateQueryConfig {
"args": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,109 @@ exports[`polkadotXcm > limitedReserveWithdrawAssets > x2 > should get correct ar
]
`;

exports[`polkadotXcm > transferAssetsUsingTypeAndThen > x2 > should be correct config 1`] = `
ExtrinsicConfig {
"func": "transferAssetsUsingTypeAndThen",
"getArgs": [Function],
"module": "polkadotXcm",
"type": "Substrate",
}
`;

exports[`polkadotXcm > transferAssetsUsingTypeAndThen > x2 > should get correct arguments 1`] = `
[
{
"V4": {
"interior": {
"X1": [
{
"Parachain": 1000,
},
],
},
"parents": 1,
},
},
{
"V4": [
{
"fun": {
"fungible": 99000000000n,
},
"id": {
"interior": {
"X2": [
{
"globalConsensus": {
"Ethereum": {
"ChainId": 1,
},
},
},
{
"AccountKey20": {
"key": "0xeF46c7649270C912704fB09B75097f6E32208b85",
},
},
],
},
"parents": 2,
},
},
],
},
"LocalReserve",
{
"V4": {
"interior": {
"X2": [
{
"globalConsensus": {
"Ethereum": {
"ChainId": 1,
},
},
},
{
"AccountKey20": {
"key": "0xeF46c7649270C912704fB09B75097f6E32208b85",
},
},
],
},
"parents": 2,
},
},
"LocalReserve",
{
"V4": [
{
"DepositAsset": {
"assets": {
"Wild": {
"AllCounted": 1,
},
},
"beneficiary": {
"interior": {
"X1": [
{
"AccountKey20": {
"key": "0xeF46c7649270C912704fB09B75097f6E32208b85",
},
},
],
},
"parents": 0,
},
},
},
],
},
"Unlimited",
]
`;

exports[`polkadotXcm limitedReserveTransferAssets here should be correct config 1`] = `
ExtrinsicConfig {
"func": "limitedReserveTransferAssets",
Expand Down
Loading

0 comments on commit 5d3e8b4

Please sign in to comment.