Skip to content

Commit f65c774

Browse files
committed
WETH integration test
1 parent 9181b3e commit f65c774

18 files changed

+493
-211
lines changed

integration-tests/chopsticks/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ To install dependencies:
66
bun install
77
```
88

9+
```bash
10+
bun papi
11+
```
12+
913
To start the chains:
1014

1115
```bash

integration-tests/chopsticks/bun.lockb

100644100755
32 Bytes
Binary file not shown.

integration-tests/chopsticks/overrides/polimec.ts

+81-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
import { INITIAL_BALANCES } from '@/constants';
2-
import { Accounts, Assets } from '@/types';
2+
import { Accounts, Asset, AssetLocation, AssetSourceRelation } from '@/types';
33

44
export const POLIMEC_WASM =
55
'../../target/release/wbuild/polimec-runtime/polimec_runtime.compact.compressed.wasm';
66

7+
const usdc_location = {
8+
parents: 1,
9+
interior: {
10+
x3: [{ parachain: 1000 }, { palletInstance: 50 }, { generalIndex: 1337 }],
11+
},
12+
};
13+
const usdt_location = {
14+
parents: 1,
15+
interior: {
16+
x3: [{ parachain: 1000 }, { palletInstance: 50 }, { generalIndex: 1984 }],
17+
},
18+
};
19+
const dot_location = {
20+
parents: 1,
21+
interior: {
22+
here: undefined,
23+
},
24+
};
25+
726
export const polimec_storage = {
827
System: {
928
Account: [
@@ -21,23 +40,81 @@ export const polimec_storage = {
2140
ForeignAssets: {
2241
Account: [
2342
[
24-
[Assets.USDC, Accounts.BOB],
43+
[usdc_location, Accounts.BOB],
2544
{
2645
balance: INITIAL_BALANCES.USDC,
2746
},
2847
],
2948
[
30-
[Assets.USDT, Accounts.BOB],
49+
[usdt_location, Accounts.BOB],
3150
{
3251
balance: INITIAL_BALANCES.USDT,
3352
},
3453
],
3554
[
36-
[Assets.DOT, Accounts.BOB],
55+
[dot_location, Accounts.BOB],
3756
{
3857
balance: INITIAL_BALANCES.DOT,
3958
},
4059
],
4160
],
61+
Asset: [
62+
[
63+
[usdc_location],
64+
{
65+
owner: Accounts.ALICE,
66+
issuer: Accounts.ALICE,
67+
admin: Accounts.ALICE,
68+
freezer: Accounts.ALICE,
69+
supply: INITIAL_BALANCES.USDC,
70+
deposit: 0n,
71+
min_balance: 70000n,
72+
is_sufficient: true,
73+
accounts: 1,
74+
sufficients: 1,
75+
approvals: 0,
76+
status: 'Live',
77+
},
78+
],
79+
[
80+
[usdt_location],
81+
{
82+
owner: Accounts.ALICE,
83+
issuer: Accounts.ALICE,
84+
admin: Accounts.ALICE,
85+
freezer: Accounts.ALICE,
86+
supply: INITIAL_BALANCES.USDT,
87+
deposit: 0n,
88+
min_balance: 70000n,
89+
is_sufficient: true,
90+
accounts: 1,
91+
sufficients: 1,
92+
approvals: 0,
93+
status: 'Live',
94+
},
95+
],
96+
[
97+
[dot_location],
98+
{
99+
owner: Accounts.ALICE,
100+
issuer: Accounts.ALICE,
101+
admin: Accounts.ALICE,
102+
freezer: Accounts.ALICE,
103+
supply: INITIAL_BALANCES.DOT,
104+
deposit: 0n,
105+
min_balance: 100000000n,
106+
is_sufficient: true,
107+
accounts: 1,
108+
sufficients: 1,
109+
approvals: 0,
110+
status: 'Live',
111+
},
112+
],
113+
],
114+
Metadata: [
115+
[[usdc_location], { symbol: 'USDC', name: 'USDC', decimals: 6, isFrozen: false }],
116+
[[usdt_location], { symbol: 'USDT', name: 'USDC', decimals: 6, isFrozen: false }],
117+
[[dot_location], { symbol: 'DOT', name: 'DOT', decimals: 10, isFrozen: false }],
118+
],
42119
},
43120
} as const;

integration-tests/chopsticks/overrides/polkadot-hub.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { INITIAL_BALANCES } from '@/constants';
2-
import { Accounts, Assets } from '@/types';
2+
import { Accounts, Asset } from '@/types';
33

44
export const polkadot_hub_storage = {
55
System: {
@@ -18,24 +18,17 @@ export const polkadot_hub_storage = {
1818
Assets: {
1919
Account: [
2020
[
21-
[Assets.USDT, Accounts.ALICE],
21+
[Asset.USDT, Accounts.ALICE],
2222
{
2323
balance: INITIAL_BALANCES.USDT,
2424
},
2525
],
2626
[
27-
[Assets.USDC, Accounts.ALICE],
27+
[Asset.USDC, Accounts.ALICE],
2828
{
2929
balance: INITIAL_BALANCES.USDC,
3030
},
3131
],
32-
[
33-
[Assets.UNKNOWN, Accounts.ALICE],
34-
{
35-
balance: INITIAL_BALANCES.USDT,
36-
},
37-
],
3832
],
3933
},
40-
// TODO: Add the foreignAssets storage to give to ALICE WETH = INITIAL_BALANCES.WETH
4134
} as const;

integration-tests/chopsticks/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dependencies": {
1919
"@polkadot-api/descriptors": "file:.papi/descriptors",
2020
"@polkadot/keyring": "13.2.3",
21+
"lodash": "^4.17.21",
2122
"polkadot-api": "^1.7.7"
2223
}
2324
}

integration-tests/chopsticks/src/managers/BaseManager.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { DERIVE_PATHS } from '@/constants';
2-
import type { Accounts, ChainClient, ChainToDefinition, Chains } from '@/types';
2+
import {
3+
type Accounts,
4+
type Asset,
5+
AssetLocation,
6+
type AssetSourceRelation,
7+
type ChainClient,
8+
type ChainToDefinition,
9+
type Chains,
10+
} from '@/types';
311
import { sr25519CreateDerive } from '@polkadot-labs/hdkd';
412
import { DEV_PHRASE, entropyToMiniSecret, mnemonicToEntropy } from '@polkadot-labs/hdkd-helpers';
513
import type { PolkadotSigner, TypedApi } from 'polkadot-api';
@@ -67,19 +75,32 @@ export abstract class BaseChainManager {
6775
return events[0]?.payload.actual_fee || 0n;
6876
}
6977

70-
async getNativeBalanceOf(account: Accounts) {
71-
const api = this.getApi(this.getChainType());
72-
const balance = await api.query.System.Account.getValue(account);
73-
return balance.data.free;
78+
// Make sure to override this in the other managers
79+
abstract getAssetSourceRelation(asset: Asset): AssetSourceRelation | undefined;
80+
81+
async getAssetBalanceOf(account: Accounts, asset: Asset): Promise<bigint> {
82+
const chain = this.getChainType();
83+
const api = this.getApi(chain);
84+
const asset_source_relation = this.getAssetSourceRelation(asset);
85+
const asset_location = AssetLocation(asset, asset_source_relation);
86+
const account_balances_result = await api.apis.FungiblesApi.query_account_balances(account);
87+
88+
if (account_balances_result.success === true && account_balances_result.value.type === 'V4') {
89+
const assets = account_balances_result.value.value;
90+
for (const asset of assets) {
91+
if (asset.id === asset_location && asset.fun.type === 'Fungible') {
92+
return asset.fun.value;
93+
}
94+
}
95+
}
96+
return 0n;
7497
}
7598

7699
// @ts-expect-error - TODO: Not sure which is the correct type for this
77100
abstract getXcmPallet();
78101

79102
abstract getChainType(): Chains;
80103

81-
abstract getAssetBalanceOf(account: Accounts, asset: number): Promise<bigint>;
82-
83104
abstract connect(): void;
84105

85106
abstract disconnect(): void;

integration-tests/chopsticks/src/managers/PolimecManager.ts

+39-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { type Accounts, type Assets, Chains } from '@/types';
1+
import { type Accounts, Asset, AssetLocation, AssetSourceRelation, Chains } from '@/types';
22
import { polimec } from '@polkadot-api/descriptors';
3+
import { isEqual } from 'lodash';
34
import { createClient } from 'polkadot-api';
45
import { getWsProvider } from 'polkadot-api/ws-provider/web';
56
import { BaseChainManager } from './BaseManager';
@@ -34,10 +35,44 @@ export class PolimecManager extends BaseChainManager {
3435
return '58kXueYKLr5b8yCeY3Gd1nLQX2zSJLXjfMzTAuksNq25CFEL' as Accounts;
3536
}
3637

37-
async getAssetBalanceOf(account: Accounts, asset: Assets) {
38+
getAssetSourceRelation(asset: Asset): AssetSourceRelation | undefined {
39+
switch (asset) {
40+
case Asset.DOT:
41+
return AssetSourceRelation.Parent;
42+
case Asset.USDT:
43+
return AssetSourceRelation.Sibling;
44+
case Asset.USDC:
45+
return AssetSourceRelation.Sibling;
46+
case Asset.WETH:
47+
return undefined;
48+
}
49+
}
50+
51+
async getAssetBalanceOf(account: Accounts, asset: Asset): Promise<bigint> {
3852
const api = this.getApi(Chains.Polimec);
39-
const balance = await api.query.ForeignAssets.Account.getValue(asset, account);
40-
return balance?.balance || 0n;
53+
const asset_source_relation = this.getAssetSourceRelation(asset);
54+
const asset_location = AssetLocation(asset, asset_source_relation).value;
55+
const account_balances_result = await api.apis.FungiblesApi.query_account_balances(account);
56+
console.log('Requested asset location in PolimecManager');
57+
console.log(asset_location);
58+
console.log('\n\n');
59+
if (account_balances_result.success === true && account_balances_result.value.type === 'V4') {
60+
const assets = account_balances_result.value.value;
61+
for (const asset of assets) {
62+
if (isEqual(asset.id, asset_location)) {
63+
console.log('Found asset. Balance is: ', asset.fun.value);
64+
console.dir(asset, { depth: null, colors: true });
65+
console.log('\n\n');
66+
return asset.fun.value;
67+
}
68+
console.log('Not it chief: \n');
69+
console.dir(asset, { depth: null, colors: true });
70+
console.log('\n\n');
71+
}
72+
}
73+
console.log('Asset not found');
74+
console.log('\n\n');
75+
return 0n;
4176
}
4277

4378
async getXcmFee() {

integration-tests/chopsticks/src/managers/PolkadotHubManager.ts

+39-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { type Accounts, type Assets, Chains } from '@/types';
2-
import { pah } from '@polkadot-api/descriptors';
1+
import { type Accounts, Asset, AssetLocation, AssetSourceRelation, Chains } from '@/types';
2+
import { type XcmVersionedAssets, type XcmVersionedLocation, pah } from '@polkadot-api/descriptors';
3+
import { isEqual } from 'lodash';
34
import { createClient } from 'polkadot-api';
45
import { getWsProvider } from 'polkadot-api/ws-provider/web';
56
import { BaseChainManager } from './BaseManager';
@@ -30,12 +31,44 @@ export class PolkadotHubManager extends BaseChainManager {
3031
return api.tx.PolkadotXcm;
3132
}
3233

33-
async getAssetBalanceOf(account: Accounts, asset: Assets) {
34+
getAssetSourceRelation(asset: Asset): AssetSourceRelation | undefined {
35+
switch (asset) {
36+
case Asset.DOT:
37+
return AssetSourceRelation.Parent;
38+
case Asset.USDT:
39+
return AssetSourceRelation.Self;
40+
case Asset.USDC:
41+
return AssetSourceRelation.Self;
42+
case Asset.WETH:
43+
return undefined;
44+
}
45+
}
46+
async getAssetBalanceOf(account: Accounts, asset: Asset): Promise<bigint> {
3447
const api = this.getApi(Chains.PolkadotHub);
35-
const balance = await api.query.Assets.Account.getValue(asset, account);
36-
return balance?.balance || 0n;
48+
const asset_source_relation = this.getAssetSourceRelation(asset);
49+
const asset_location = AssetLocation(asset, asset_source_relation).value;
50+
const account_balances_result = await api.apis.FungiblesApi.query_account_balances(account);
51+
console.log('Requested asset location in PolkadotHubManager');
52+
console.log(asset_location);
53+
console.log('\n\n');
54+
if (account_balances_result.success === true && account_balances_result.value.type === 'V4') {
55+
const assets = account_balances_result.value.value;
56+
for (const asset of assets) {
57+
if (isEqual(asset.id, asset_location)) {
58+
console.log('Found asset. Balance is: ', asset.fun.value);
59+
console.dir(asset, { depth: null, colors: true });
60+
console.log('\n\n');
61+
return asset.fun.value;
62+
}
63+
console.log('Not it chief: \n');
64+
console.dir(asset, { depth: null, colors: true });
65+
console.log('\n\n');
66+
}
67+
}
68+
console.log('Asset not found');
69+
console.log('\n\n');
70+
return 0n;
3771
}
38-
3972
async getSwapCredit() {
4073
const api = this.getApi(Chains.PolkadotHub);
4174
const events = await api.event.AssetConversion.SwapCreditExecuted.pull();

integration-tests/chopsticks/src/managers/PolkadotManager.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Accounts, Chains } from '@/types';
1+
import { type Accounts, Asset, AssetSourceRelation, Chains } from '@/types';
22
import { polkadot } from '@polkadot-api/descriptors';
33
import { createClient } from 'polkadot-api';
44
import { getWsProvider } from 'polkadot-api/ws-provider/web';
@@ -30,8 +30,26 @@ export class PolkadotManager extends BaseChainManager {
3030
return api.tx.XcmPallet;
3131
}
3232

33-
async getAssetBalanceOf(_account: Accounts, _asset: number): Promise<bigint> {
34-
throw new Error('Polkadot does not support assets');
33+
getAssetSourceRelation(asset: Asset): AssetSourceRelation | undefined {
34+
switch (asset) {
35+
case Asset.DOT:
36+
return AssetSourceRelation.Self;
37+
case Asset.USDT:
38+
return undefined;
39+
case Asset.USDC:
40+
return undefined;
41+
case Asset.WETH:
42+
return undefined;
43+
}
44+
}
45+
46+
async getAssetBalanceOf(account: Accounts, asset: Asset): Promise<bigint> {
47+
const api = this.getApi(this.getChainType());
48+
if (asset === Asset.DOT) {
49+
const balance = await api.query.System.Account.getValue(account);
50+
return balance.data.free;
51+
}
52+
return 0n;
3553
}
3654

3755
async getXcmFee() {

integration-tests/chopsticks/src/setup.ts

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export class ChainSetup {
8080
'wasm-override': POLIMEC_WASM,
8181
'import-storage': polimec_storage,
8282
'build-block-mode': BuildBlockMode.Instant,
83+
'runtime-log-level': 3,
8384
});
8485
}
8586

0 commit comments

Comments
 (0)