Skip to content

Commit

Permalink
WETH integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Dec 20, 2024
1 parent 9181b3e commit f65c774
Show file tree
Hide file tree
Showing 18 changed files with 493 additions and 211 deletions.
4 changes: 4 additions & 0 deletions integration-tests/chopsticks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ To install dependencies:
bun install
```

```bash
bun papi
```

To start the chains:

```bash
Expand Down
Binary file modified integration-tests/chopsticks/bun.lockb
100644 → 100755
Binary file not shown.
85 changes: 81 additions & 4 deletions integration-tests/chopsticks/overrides/polimec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
import { INITIAL_BALANCES } from '@/constants';
import { Accounts, Assets } from '@/types';
import { Accounts, Asset, AssetLocation, AssetSourceRelation } from '@/types';

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

const usdc_location = {
parents: 1,
interior: {
x3: [{ parachain: 1000 }, { palletInstance: 50 }, { generalIndex: 1337 }],
},
};
const usdt_location = {
parents: 1,
interior: {
x3: [{ parachain: 1000 }, { palletInstance: 50 }, { generalIndex: 1984 }],
},
};
const dot_location = {
parents: 1,
interior: {
here: undefined,
},
};

export const polimec_storage = {
System: {
Account: [
Expand All @@ -21,23 +40,81 @@ export const polimec_storage = {
ForeignAssets: {
Account: [
[
[Assets.USDC, Accounts.BOB],
[usdc_location, Accounts.BOB],
{
balance: INITIAL_BALANCES.USDC,
},
],
[
[Assets.USDT, Accounts.BOB],
[usdt_location, Accounts.BOB],
{
balance: INITIAL_BALANCES.USDT,
},
],
[
[Assets.DOT, Accounts.BOB],
[dot_location, Accounts.BOB],
{
balance: INITIAL_BALANCES.DOT,
},
],
],
Asset: [
[
[usdc_location],
{
owner: Accounts.ALICE,
issuer: Accounts.ALICE,
admin: Accounts.ALICE,
freezer: Accounts.ALICE,
supply: INITIAL_BALANCES.USDC,
deposit: 0n,
min_balance: 70000n,
is_sufficient: true,
accounts: 1,
sufficients: 1,
approvals: 0,
status: 'Live',
},
],
[
[usdt_location],
{
owner: Accounts.ALICE,
issuer: Accounts.ALICE,
admin: Accounts.ALICE,
freezer: Accounts.ALICE,
supply: INITIAL_BALANCES.USDT,
deposit: 0n,
min_balance: 70000n,
is_sufficient: true,
accounts: 1,
sufficients: 1,
approvals: 0,
status: 'Live',
},
],
[
[dot_location],
{
owner: Accounts.ALICE,
issuer: Accounts.ALICE,
admin: Accounts.ALICE,
freezer: Accounts.ALICE,
supply: INITIAL_BALANCES.DOT,
deposit: 0n,
min_balance: 100000000n,
is_sufficient: true,
accounts: 1,
sufficients: 1,
approvals: 0,
status: 'Live',
},
],
],
Metadata: [
[[usdc_location], { symbol: 'USDC', name: 'USDC', decimals: 6, isFrozen: false }],
[[usdt_location], { symbol: 'USDT', name: 'USDC', decimals: 6, isFrozen: false }],
[[dot_location], { symbol: 'DOT', name: 'DOT', decimals: 10, isFrozen: false }],
],
},
} as const;
13 changes: 3 additions & 10 deletions integration-tests/chopsticks/overrides/polkadot-hub.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { INITIAL_BALANCES } from '@/constants';
import { Accounts, Assets } from '@/types';
import { Accounts, Asset } from '@/types';

export const polkadot_hub_storage = {
System: {
Expand All @@ -18,24 +18,17 @@ export const polkadot_hub_storage = {
Assets: {
Account: [
[
[Assets.USDT, Accounts.ALICE],
[Asset.USDT, Accounts.ALICE],
{
balance: INITIAL_BALANCES.USDT,
},
],
[
[Assets.USDC, Accounts.ALICE],
[Asset.USDC, Accounts.ALICE],
{
balance: INITIAL_BALANCES.USDC,
},
],
[
[Assets.UNKNOWN, Accounts.ALICE],
{
balance: INITIAL_BALANCES.USDT,
},
],
],
},
// TODO: Add the foreignAssets storage to give to ALICE WETH = INITIAL_BALANCES.WETH
} as const;
1 change: 1 addition & 0 deletions integration-tests/chopsticks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@polkadot-api/descriptors": "file:.papi/descriptors",
"@polkadot/keyring": "13.2.3",
"lodash": "^4.17.21",
"polkadot-api": "^1.7.7"
}
}
35 changes: 28 additions & 7 deletions integration-tests/chopsticks/src/managers/BaseManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { DERIVE_PATHS } from '@/constants';
import type { Accounts, ChainClient, ChainToDefinition, Chains } from '@/types';
import {
type Accounts,
type Asset,
AssetLocation,
type AssetSourceRelation,
type ChainClient,
type ChainToDefinition,
type Chains,
} from '@/types';
import { sr25519CreateDerive } from '@polkadot-labs/hdkd';
import { DEV_PHRASE, entropyToMiniSecret, mnemonicToEntropy } from '@polkadot-labs/hdkd-helpers';
import type { PolkadotSigner, TypedApi } from 'polkadot-api';
Expand Down Expand Up @@ -67,19 +75,32 @@ export abstract class BaseChainManager {
return events[0]?.payload.actual_fee || 0n;
}

async getNativeBalanceOf(account: Accounts) {
const api = this.getApi(this.getChainType());
const balance = await api.query.System.Account.getValue(account);
return balance.data.free;
// Make sure to override this in the other managers
abstract getAssetSourceRelation(asset: Asset): AssetSourceRelation | undefined;

async getAssetBalanceOf(account: Accounts, asset: Asset): Promise<bigint> {
const chain = this.getChainType();
const api = this.getApi(chain);
const asset_source_relation = this.getAssetSourceRelation(asset);
const asset_location = AssetLocation(asset, asset_source_relation);
const account_balances_result = await api.apis.FungiblesApi.query_account_balances(account);

if (account_balances_result.success === true && account_balances_result.value.type === 'V4') {
const assets = account_balances_result.value.value;
for (const asset of assets) {
if (asset.id === asset_location && asset.fun.type === 'Fungible') {
return asset.fun.value;
}
}
}
return 0n;
}

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

abstract getChainType(): Chains;

abstract getAssetBalanceOf(account: Accounts, asset: number): Promise<bigint>;

abstract connect(): void;

abstract disconnect(): void;
Expand Down
43 changes: 39 additions & 4 deletions integration-tests/chopsticks/src/managers/PolimecManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Accounts, type Assets, Chains } from '@/types';
import { type Accounts, Asset, AssetLocation, AssetSourceRelation, Chains } from '@/types';
import { polimec } from '@polkadot-api/descriptors';
import { isEqual } from 'lodash';
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws-provider/web';
import { BaseChainManager } from './BaseManager';
Expand Down Expand Up @@ -34,10 +35,44 @@ export class PolimecManager extends BaseChainManager {
return '58kXueYKLr5b8yCeY3Gd1nLQX2zSJLXjfMzTAuksNq25CFEL' as Accounts;
}

async getAssetBalanceOf(account: Accounts, asset: Assets) {
getAssetSourceRelation(asset: Asset): AssetSourceRelation | undefined {
switch (asset) {
case Asset.DOT:
return AssetSourceRelation.Parent;
case Asset.USDT:
return AssetSourceRelation.Sibling;
case Asset.USDC:
return AssetSourceRelation.Sibling;
case Asset.WETH:
return undefined;
}
}

async getAssetBalanceOf(account: Accounts, asset: Asset): Promise<bigint> {
const api = this.getApi(Chains.Polimec);
const balance = await api.query.ForeignAssets.Account.getValue(asset, account);
return balance?.balance || 0n;
const asset_source_relation = this.getAssetSourceRelation(asset);
const asset_location = AssetLocation(asset, asset_source_relation).value;
const account_balances_result = await api.apis.FungiblesApi.query_account_balances(account);
console.log('Requested asset location in PolimecManager');
console.log(asset_location);
console.log('\n\n');
if (account_balances_result.success === true && account_balances_result.value.type === 'V4') {
const assets = account_balances_result.value.value;
for (const asset of assets) {
if (isEqual(asset.id, asset_location)) {
console.log('Found asset. Balance is: ', asset.fun.value);
console.dir(asset, { depth: null, colors: true });
console.log('\n\n');
return asset.fun.value;
}
console.log('Not it chief: \n');
console.dir(asset, { depth: null, colors: true });
console.log('\n\n');
}
}
console.log('Asset not found');
console.log('\n\n');
return 0n;
}

async getXcmFee() {
Expand Down
45 changes: 39 additions & 6 deletions integration-tests/chopsticks/src/managers/PolkadotHubManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Accounts, type Assets, Chains } from '@/types';
import { pah } from '@polkadot-api/descriptors';
import { type Accounts, Asset, AssetLocation, AssetSourceRelation, Chains } from '@/types';
import { type XcmVersionedAssets, type XcmVersionedLocation, pah } from '@polkadot-api/descriptors';
import { isEqual } from 'lodash';
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws-provider/web';
import { BaseChainManager } from './BaseManager';
Expand Down Expand Up @@ -30,12 +31,44 @@ export class PolkadotHubManager extends BaseChainManager {
return api.tx.PolkadotXcm;
}

async getAssetBalanceOf(account: Accounts, asset: Assets) {
getAssetSourceRelation(asset: Asset): AssetSourceRelation | undefined {
switch (asset) {
case Asset.DOT:
return AssetSourceRelation.Parent;
case Asset.USDT:
return AssetSourceRelation.Self;
case Asset.USDC:
return AssetSourceRelation.Self;
case Asset.WETH:
return undefined;
}
}
async getAssetBalanceOf(account: Accounts, asset: Asset): Promise<bigint> {
const api = this.getApi(Chains.PolkadotHub);
const balance = await api.query.Assets.Account.getValue(asset, account);
return balance?.balance || 0n;
const asset_source_relation = this.getAssetSourceRelation(asset);
const asset_location = AssetLocation(asset, asset_source_relation).value;
const account_balances_result = await api.apis.FungiblesApi.query_account_balances(account);
console.log('Requested asset location in PolkadotHubManager');
console.log(asset_location);
console.log('\n\n');
if (account_balances_result.success === true && account_balances_result.value.type === 'V4') {
const assets = account_balances_result.value.value;
for (const asset of assets) {
if (isEqual(asset.id, asset_location)) {
console.log('Found asset. Balance is: ', asset.fun.value);
console.dir(asset, { depth: null, colors: true });
console.log('\n\n');
return asset.fun.value;
}
console.log('Not it chief: \n');
console.dir(asset, { depth: null, colors: true });
console.log('\n\n');
}
}
console.log('Asset not found');
console.log('\n\n');
return 0n;
}

async getSwapCredit() {
const api = this.getApi(Chains.PolkadotHub);
const events = await api.event.AssetConversion.SwapCreditExecuted.pull();
Expand Down
24 changes: 21 additions & 3 deletions integration-tests/chopsticks/src/managers/PolkadotManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Accounts, Chains } from '@/types';
import { type Accounts, Asset, AssetSourceRelation, Chains } from '@/types';
import { polkadot } from '@polkadot-api/descriptors';
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws-provider/web';
Expand Down Expand Up @@ -30,8 +30,26 @@ export class PolkadotManager extends BaseChainManager {
return api.tx.XcmPallet;
}

async getAssetBalanceOf(_account: Accounts, _asset: number): Promise<bigint> {
throw new Error('Polkadot does not support assets');
getAssetSourceRelation(asset: Asset): AssetSourceRelation | undefined {
switch (asset) {
case Asset.DOT:
return AssetSourceRelation.Self;
case Asset.USDT:
return undefined;
case Asset.USDC:
return undefined;
case Asset.WETH:
return undefined;
}
}

async getAssetBalanceOf(account: Accounts, asset: Asset): Promise<bigint> {
const api = this.getApi(this.getChainType());
if (asset === Asset.DOT) {
const balance = await api.query.System.Account.getValue(account);
return balance.data.free;
}
return 0n;
}

async getXcmFee() {
Expand Down
1 change: 1 addition & 0 deletions integration-tests/chopsticks/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class ChainSetup {
'wasm-override': POLIMEC_WASM,
'import-storage': polimec_storage,
'build-block-mode': BuildBlockMode.Instant,
'runtime-log-level': 3,
});
}

Expand Down
Loading

0 comments on commit f65c774

Please sign in to comment.