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 17, 2024
1 parent 583f9e4 commit e0ad95d
Show file tree
Hide file tree
Showing 17 changed files with 387 additions and 210 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
Empty file modified integration-tests/chopsticks/bun.lockb
100644 → 100755
Empty file.
8 changes: 4 additions & 4 deletions integration-tests/chopsticks/overrides/polimec.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 POLIMEC_WASM =
'../../target/release/wbuild/polimec-runtime/polimec_runtime.compact.compressed.wasm';
Expand All @@ -21,19 +21,19 @@ export const polimec_storage = {
ForeignAssets: {
Account: [
[
[Assets.USDC, Accounts.BOB],
[Asset.USDC, Accounts.BOB],
{
balance: INITIAL_BALANCES.USDC,
},
],
[
[Assets.USDT, Accounts.BOB],
[Asset.USDT, Accounts.BOB],
{
balance: INITIAL_BALANCES.USDT,
},
],
[
[Assets.DOT, Accounts.BOB],
[Asset.DOT, Accounts.BOB],
{
balance: INITIAL_BALANCES.DOT,
},
Expand Down
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;
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,
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
32 changes: 28 additions & 4 deletions integration-tests/chopsticks/src/managers/PolimecManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Accounts, type Assets, Chains } from '@/types';
import { type Accounts, Asset, AssetLocation, AssetSourceRelation, Chains } from '@/types';
import { polimec } from '@polkadot-api/descriptors';
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws-provider/web';
Expand Down Expand Up @@ -34,10 +34,34 @@ 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);
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;
}

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

async getAssetBalanceOf(account: Accounts, asset: Assets) {
const api = this.getApi(Chains.PolkadotHub);
const balance = await api.query.Assets.Account.getValue(asset, account);
return balance?.balance || 0n;
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 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;
}
async getSwapCredit() {
const api = this.getApi(Chains.PolkadotHub);
const events = await api.event.AssetConversion.SwapCreditExecuted.pull();
Expand Down
25 changes: 22 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,27 @@ 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;
} else {
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
64 changes: 37 additions & 27 deletions integration-tests/chopsticks/src/tests/hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TRANSFER_AMOUNTS } from '@/constants';
import { createChainManager } from '@/managers/Factory';
import { ChainSetup } from '@/setup';
import { HubToPolimecTransfer } from '@/transfers/HubToPolimec';
import { Accounts, Assets, Chains } from '@/types';
import { Accounts, Asset, AssetSourceRelation, Chains } from '@/types';

describe('Polkadot Hub -> Polimec Transfer Tests', () => {
const sourceManager = createChainManager(Chains.PolkadotHub);
Expand All @@ -18,33 +18,43 @@ describe('Polkadot Hub -> Polimec Transfer Tests', () => {
});
afterAll(async () => await chainSetup.cleanup());

test('Send DOT to Polimec', () =>
transferTest.testTransfer({
amount: TRANSFER_AMOUNTS.NATIVE,
account: Accounts.ALICE,
asset: Assets.DOT,
}));

test('Send USDt to Polimec', () =>
transferTest.testTransfer({
amount: TRANSFER_AMOUNTS.TOKENS,
account: Accounts.ALICE,
asset: Assets.USDT,
}));

test('Send USDC to Polimec', () =>
transferTest.testTransfer({
amount: TRANSFER_AMOUNTS.TOKENS,
account: Accounts.ALICE,
asset: Assets.USDC,
}));

test('Send Unknown Asset to Polimec', () =>
expect(() =>
test(
'Send DOT to Polimec',
() =>
transferTest.testTransfer({
amount: TRANSFER_AMOUNTS.TOKENS,
amount: TRANSFER_AMOUNTS.NATIVE,
account: Accounts.ALICE,
asset: Assets.UNKNOWN,
asset: Asset.DOT,
assetSourceRelation: AssetSourceRelation.Parent,
}),
).toThrow());
{ timeout: 25000 },
);
//
// test('Send USDT to Polimec', () =>
// transferTest.testTransfer({
// amount: TRANSFER_AMOUNTS.TOKENS,
// account: Accounts.ALICE,
// asset: Asset.USDT,
// assetSourceRelation: AssetSourceRelation.Self,
// }), {timeout: 25000});
//
// test('Send USDC to Polimec', () =>
// transferTest.testTransfer({
// amount: TRANSFER_AMOUNTS.TOKENS,
// account: Accounts.ALICE,
// asset: Asset.USDC,
// assetSourceRelation: AssetSourceRelation.Self,
// }), {timeout: 25000});

// test(
// 'Send WETH to Polimec',
// () =>
// transferTest.testTransfer({
// amount: TRANSFER_AMOUNTS.BRIDGED,
// account: Accounts.ALICE,
// asset: Asset.WETH,
// assetSourceRelation: undefined,
// }),
// { timeout: 25000 },
// );
});
Loading

0 comments on commit e0ad95d

Please sign in to comment.