Skip to content

Commit

Permalink
fix: XCM tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrshiposha committed Oct 24, 2023
1 parent e8a0ca9 commit b2db3c1
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 174 deletions.
7 changes: 0 additions & 7 deletions tests/src/util/playgrounds/types.xcm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,3 @@ export interface DemocracyStandardAccountVote {
conviction: number,
},
}

export interface IForeignAssetMetadata {
name?: number | Uint8Array,
symbol?: string,
decimals?: number,
minimalBalance?: bigint,
}
24 changes: 12 additions & 12 deletions tests/src/util/playgrounds/unique.xcm.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {ApiPromise, WsProvider} from '@polkadot/api';
import {IKeyringPair} from '@polkadot/types/types';
import {ChainHelperBase, EthereumBalanceGroup, HelperGroup, SubstrateBalanceGroup, UniqueHelper} from './unique';
import {ILogger, TSigner, TSubstrateAccount} from './types';
import {AcalaAssetMetadata, DemocracyStandardAccountVote, IForeignAssetMetadata, MoonbeamAssetInfo} from './types.xcm';
import {ILogger, TSigner} from './types';
import {AcalaAssetMetadata, DemocracyStandardAccountVote, MoonbeamAssetInfo} from './types.xcm';
import {CollectionMode} from '../../eth/util/playgrounds/types';


export class XcmChainHelper extends ChainHelperBase {
Expand Down Expand Up @@ -104,22 +105,17 @@ class PolkadexXcmHelperGroup<T extends ChainHelperBase> extends HelperGroup<T> {
}

export class ForeignAssetsGroup extends HelperGroup<UniqueHelper> {
async register(signer: TSigner, ownerAddress: TSubstrateAccount, location: any, metadata: IForeignAssetMetadata) {
async register(signer: TSigner, location: any, name: string, tokenPrefix: string, mode: 'NFT' | { Fungible: number }) {
await this.helper.executeExtrinsic(
signer,
'api.tx.foreignAssets.registerForeignAsset',
[ownerAddress, location, metadata],
'api.tx.foreignAssets.forceRegisterForeignAsset',
[location, this.helper.util.str2vec(name), tokenPrefix, mode],
true,
);
}

async update(signer: TSigner, foreignAssetId: number, location: any, metadata: IForeignAssetMetadata) {
await this.helper.executeExtrinsic(
signer,
'api.tx.foreignAssets.updateForeignAsset',
[foreignAssetId, location, metadata],
true,
);
async foreignCollectionId(location: any) {
return (await this.helper.callRpc('api.query.foreignAssets.foreignReserveLocationToCollection', [location])).toJSON();
}
}

Expand Down Expand Up @@ -256,6 +252,10 @@ export class AssetsGroup<T extends ChainHelperBase> extends HelperGroup<T> {
await this.helper.executeExtrinsic(signer, 'api.tx.assets.mint', [assetId, beneficiary, amount], true);
}

async assetInfo(assetId: number | bigint) {
return (await this.helper.callRpc('api.query.assets.asset', [assetId])).toJSON();
}

async account(assetId: string | number | bigint, address: string) {
const accountAsset = (
await this.helper.callRpc('api.query.assets.account', [assetId, address])
Expand Down
22 changes: 22 additions & 0 deletions tests/src/xcm/xcm.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,26 @@ export class XcmTestHelper {
expect(await helper.balance.getSubstrate(emptyAccount)).to.be.equal(0n);
});
}

async registerRelayNativeTokenOnUnique(alice: IKeyringPair) {
return await usingPlaygrounds(async (helper) => {
const relayLocation = {
parents: 1,
interior: 'Here',
};

const relayCollectionId = await helper.foreignAssets.foreignCollectionId(relayLocation);
if(relayCollectionId == null) {
const name = 'Relay Tokens';
const tokenPrefix = 'xDOT';
const decimals = 10;
await helper.getSudo().foreignAssets.register(alice, relayLocation, name, tokenPrefix, {Fungible: decimals});

return await helper.foreignAssets.foreignCollectionId(relayLocation);
} else {
console.log('Relay foreign collection is already registered');
return relayCollectionId;
}
});
}
}
123 changes: 69 additions & 54 deletions tests/src/xcm/xcmOpal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {IKeyringPair} from '@polkadot/types/types';
import config from '../config';
import {itSub, expect, describeXCM, usingPlaygrounds, usingWestmintPlaygrounds, usingRelayPlaygrounds} from '../util';
import {XcmTestHelper} from './xcm.types';

const STATEMINE_CHAIN = +(process.env.RELAY_WESTMINT_ID || 1000);
const UNIQUE_CHAIN = +(process.env.RELAY_OPAL_ID || 2095);
Expand All @@ -25,19 +26,21 @@ const relayUrl = config.relayUrl;
const westmintUrl = config.westmintUrl;

const STATEMINE_PALLET_INSTANCE = 50;
const ASSET_ID = 100;
const ASSET_METADATA_DECIMALS = 18;
const ASSET_METADATA_NAME = 'USDT';
const ASSET_METADATA_DESCRIPTION = 'USDT';
const ASSET_METADATA_MINIMAL_BALANCE = 1n;
const USDT_ASSET_ID = 100;
const USDT_ASSET_METADATA_DECIMALS = 18;
const USDT_ASSET_METADATA_NAME = 'USDT';
const USDT_ASSET_METADATA_DESCRIPTION = 'USDT';
const USDT_ASSET_METADATA_MINIMAL_BALANCE = 1n;

const RELAY_DECIMALS = 12;
const WESTMINT_DECIMALS = 12;

const TRANSFER_AMOUNT = 1_000_000_000_000_000_000n;

// 10,000.00 (ten thousands) USDT
const ASSET_AMOUNT = 1_000_000_000_000_000_000_000n;
const USDT_ASSET_AMOUNT = 1_000_000_000_000_000_000_000n;

const testHelper = new XcmTestHelper('opal');

describeXCM('[XCM] Integration test: Exchanging USDT with Westmint', () => {
let alice: IKeyringPair;
Expand All @@ -57,57 +60,78 @@ describeXCM('[XCM] Integration test: Exchanging USDT with Westmint', () => {
let balanceBobRelayTokenBefore: bigint;
let balanceBobRelayTokenAfter: bigint;

let usdtCollectionId: number;
let relayCollectionId: number;

before(async () => {
await usingPlaygrounds(async (_helper, privateKey) => {
alice = await privateKey('//Alice');
bob = await privateKey('//Bob'); // funds donor

relayCollectionId = await testHelper.registerRelayNativeTokenOnUnique(alice);
});

await usingWestmintPlaygrounds(westmintUrl, async (helper) => {
// 350.00 (three hundred fifty) DOT
const fundingAmount = 3_500_000_000_000n;
const assetInfo = await helper.assets.assetInfo(USDT_ASSET_ID);
if(assetInfo == null) {
await helper.assets.create(
alice,
USDT_ASSET_ID,
alice.address,
USDT_ASSET_METADATA_MINIMAL_BALANCE,
);
await helper.assets.setMetadata(
alice,
USDT_ASSET_ID,
USDT_ASSET_METADATA_NAME,
USDT_ASSET_METADATA_DESCRIPTION,
USDT_ASSET_METADATA_DECIMALS,
);
} else {
console.log('The USDT asset is already registered on AssetHub');
}

await helper.assets.mint(
alice,
USDT_ASSET_ID,
alice.address,
USDT_ASSET_AMOUNT,
);

await helper.assets.create(alice, ASSET_ID, alice.address, ASSET_METADATA_MINIMAL_BALANCE);
await helper.assets.setMetadata(alice, ASSET_ID, ASSET_METADATA_NAME, ASSET_METADATA_DESCRIPTION, ASSET_METADATA_DECIMALS);
await helper.assets.mint(alice, ASSET_ID, alice.address, ASSET_AMOUNT);
const sovereignFundingAmount = 3_500_000_000n;

// funding parachain sovereing account (Parachain: 2095)
const parachainSovereingAccount = helper.address.paraSiblingSovereignAccount(UNIQUE_CHAIN);
await helper.balance.transferToSubstrate(bob, parachainSovereingAccount, fundingAmount);
await helper.balance.transferToSubstrate(bob, parachainSovereingAccount, sovereignFundingAmount);
});


await usingPlaygrounds(async (helper) => {
const location = {
V2: {
parents: 1,
interior: {X3: [
{
Parachain: STATEMINE_CHAIN,
},
{
PalletInstance: STATEMINE_PALLET_INSTANCE,
},
{
GeneralIndex: ASSET_ID,
},
]},
},
parents: 1,
interior: {X3: [
{
Parachain: STATEMINE_CHAIN,
},
{
PalletInstance: STATEMINE_PALLET_INSTANCE,
},
{
GeneralIndex: USDT_ASSET_ID,
},
]},
};

const metadata =
{
name: ASSET_ID,
symbol: ASSET_METADATA_NAME,
decimals: ASSET_METADATA_DECIMALS,
minimalBalance: ASSET_METADATA_MINIMAL_BALANCE,
};
await helper.getSudo().foreignAssets.register(alice, alice.address, location, metadata);
if(await helper.foreignAssets.foreignCollectionId(location) == null) {
const tokenPrefix = USDT_ASSET_METADATA_NAME;
await helper.getSudo().foreignAssets.register(alice, location, USDT_ASSET_METADATA_NAME, tokenPrefix, {Fungible: USDT_ASSET_METADATA_DECIMALS});
} else {
console.log('Foreign collection is already registered on Opal');
}

balanceOpalBefore = await helper.balance.getSubstrate(alice.address);
usdtCollectionId = await helper.foreignAssets.foreignCollectionId(location);
});


// Providing the relay currency to the unique sender account
await usingRelayPlaygrounds(relayUrl, async (helper) => {
const destination = {
Expand Down Expand Up @@ -189,7 +213,7 @@ describeXCM('[XCM] Integration test: Exchanging USDT with Westmint', () => {
PalletInstance: STATEMINE_PALLET_INSTANCE,
},
{
GeneralIndex: ASSET_ID,
GeneralIndex: USDT_ASSET_ID,
},
]},
},
Expand Down Expand Up @@ -217,18 +241,16 @@ describeXCM('[XCM] Integration test: Exchanging USDT with Westmint', () => {

});


// ensure that asset has been delivered
await helper.wait.newBlocks(3);

// expext collection id will be with id 1
const free = await helper.ft.getBalance(1, {Substrate: alice.address});
const free = await helper.ft.getBalance(usdtCollectionId, {Substrate: alice.address});

balanceOpalAfter = await helper.balance.getSubstrate(alice.address);

console.log(
'[Westmint -> Opal] transaction fees on Opal: %s USDT',
helper.util.bigIntToDecimals(TRANSFER_AMOUNT - free, ASSET_METADATA_DECIMALS),
helper.util.bigIntToDecimals(TRANSFER_AMOUNT - free, USDT_ASSET_METADATA_DECIMALS),
);
console.log(
'[Westmint -> Opal] transaction fees on Opal: %s OPL',
Expand Down Expand Up @@ -261,16 +283,11 @@ describeXCM('[XCM] Integration test: Exchanging USDT with Westmint', () => {

const currencies: [any, bigint][] = [
[
{
ForeignAssetId: 0,
},
//10_000_000_000_000_000n,
usdtCollectionId,
TRANSFER_AMOUNT,
],
[
{
NativeAssetId: 'Parent',
},
relayCollectionId,
400_000_000_000_000n,
],
];
Expand All @@ -288,15 +305,15 @@ describeXCM('[XCM] Integration test: Exchanging USDT with Westmint', () => {

// The USDT token never paid fees. Its amount not changed from begin value.
// Also check that xcm transfer has been succeeded
expect((await helper.assets.account(ASSET_ID, alice.address))! == ASSET_AMOUNT).to.be.true;
expect((await helper.assets.account(USDT_ASSET_ID, alice.address))! == USDT_ASSET_AMOUNT).to.be.true;
});
});

itSub('Should connect and send Relay token to Unique', async ({helper}) => {
const TRANSFER_AMOUNT_RELAY = 50_000_000_000_000_000n;

balanceBobBefore = await helper.balance.getSubstrate(bob.address);
balanceBobRelayTokenBefore = await helper.tokens.accounts(bob.address, {NativeAssetId: 'Parent'});
balanceBobRelayTokenBefore = await helper.ft.getBalance(relayCollectionId, {Substrate: bob.address});

// Providing the relay currency to the unique sender account
await usingRelayPlaygrounds(relayUrl, async (helper) => {
Expand Down Expand Up @@ -345,7 +362,7 @@ describeXCM('[XCM] Integration test: Exchanging USDT with Westmint', () => {
await helper.wait.newBlocks(3);

balanceBobAfter = await helper.balance.getSubstrate(bob.address);
balanceBobRelayTokenAfter = await helper.tokens.accounts(bob.address, {NativeAssetId: 'Parent'});
balanceBobRelayTokenAfter = await helper.ft.getBalance(relayCollectionId, {Substrate: bob.address});

const wndFee = balanceBobRelayTokenAfter - TRANSFER_AMOUNT_RELAY - balanceBobRelayTokenBefore;
console.log(
Expand Down Expand Up @@ -383,9 +400,7 @@ describeXCM('[XCM] Integration test: Exchanging USDT with Westmint', () => {

const currencies: any = [
[
{
NativeAssetId: 'Parent',
},
relayCollectionId,
50_000_000_000_000_000n,
],
];
Expand Down
Loading

0 comments on commit b2db3c1

Please sign in to comment.