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 11, 2024
1 parent 094babf commit 7666f70
Show file tree
Hide file tree
Showing 20 changed files with 365 additions and 220 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;
34 changes: 27 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,31 @@ 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 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;
}

// @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
17 changes: 12 additions & 5 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, 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,17 @@ export class PolimecManager extends BaseChainManager {
return '58kXueYKLr5b8yCeY3Gd1nLQX2zSJLXjfMzTAuksNq25CFEL' as Accounts;
}

async getAssetBalanceOf(account: Accounts, asset: Assets) {
const api = this.getApi(Chains.Polimec);
const balance = await api.query.ForeignAssets.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.Sibling;
case Asset.USDC:
return AssetSourceRelation.Sibling;
case Asset.WETH:
return undefined;
}
}

async getXcmFee() {
Expand Down
18 changes: 13 additions & 5 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 { type Accounts, Asset, AssetLocation, AssetSourceRelation, Chains } from '@/types';
import { pah } from '@polkadot-api/descriptors';
import { XcmV3MultiassetFungibility } from '@polkadot-api/descriptors/dist/common-types';
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws-provider/web';
import { BaseChainManager } from './BaseManager';
Expand Down Expand Up @@ -30,10 +31,17 @@ 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 getSwapCredit() {
Expand Down
15 changes: 12 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,17 @@ 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 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 },
// );
});
53 changes: 34 additions & 19 deletions integration-tests/chopsticks/src/tests/polimec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createChainManager } from '@/managers/Factory';
import { polimec_storage } from '@/polimec';
import { ChainSetup } from '@/setup';
import { PolimecToHubTransfer } from '@/transfers/PolimecToHub';
import { Accounts, Assets, Chains } from '@/types';
import { Accounts, Asset, AssetSourceRelation, Chains } from '@/types';

describe('Polimec -> Hub Transfer Tests', () => {
const sourceManager = createChainManager(Chains.Polimec);
Expand All @@ -19,24 +19,39 @@ describe('Polimec -> Hub Transfer Tests', () => {
});
afterAll(async () => await chainSetup.cleanup());

test('Send USDC to Hub', () =>
transferTest.testTransfer({
amount: TRANSFER_AMOUNTS.TOKENS,
account: Accounts.BOB,
asset: Assets.USDC,
}));
test(
'Send USDC to Hub',
() =>
transferTest.testTransfer({
amount: TRANSFER_AMOUNTS.TOKENS,
account: Accounts.BOB,
asset: Asset.USDC,
assetSourceRelation: AssetSourceRelation.Sibling,
}),
{ timeout: 25000 },
);

test('Send USDt to Hub', () =>
transferTest.testTransfer({
amount: TRANSFER_AMOUNTS.TOKENS,
account: Accounts.BOB,
asset: Assets.USDT,
}));
test(
'Send USDT to Hub',
() =>
transferTest.testTransfer({
amount: TRANSFER_AMOUNTS.TOKENS,
account: Accounts.BOB,
asset: Asset.USDT,
assetSourceRelation: AssetSourceRelation.Sibling,
}),
{ timeout: 25000 },
);

test('Send DOT to Hub', () =>
transferTest.testTransfer({
amount: TRANSFER_AMOUNTS.NATIVE,
account: Accounts.BOB,
asset: Assets.DOT,
}));
test(
'Send DOT to Hub',
() =>
transferTest.testTransfer({
amount: TRANSFER_AMOUNTS.NATIVE,
account: Accounts.BOB,
asset: Asset.DOT,
assetSourceRelation: AssetSourceRelation.Parent,
}),
{ timeout: 25000 },
);
});
18 changes: 11 additions & 7 deletions integration-tests/chopsticks/src/tests/polkadot.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 { PolkadotToPolimecTransfer } from '@/transfers/PolkadotToPolimec';
import { Accounts, Assets, Chains } from '@/types';
import { Accounts, Asset, Chains } from '@/types';

describe('Polkadot -> Polimec Transfer Tests', () => {
const chainSetup = new ChainSetup();
Expand All @@ -19,10 +19,14 @@ describe('Polkadot -> 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 DOT to Polimec',
() =>
transferTest.testTransfer({
amount: TRANSFER_AMOUNTS.NATIVE,
account: Accounts.ALICE,
asset: Asset.DOT,
}),
{ timeout: 25000 },
);
});
Loading

0 comments on commit 7666f70

Please sign in to comment.