Skip to content

Commit

Permalink
updated liquid assets script to open hrmp channels
Browse files Browse the repository at this point in the history
  • Loading branch information
bee344 committed Nov 9, 2023
1 parent 7e10c93 commit 802fb61
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
32 changes: 27 additions & 5 deletions scripts/testNetworkLiquidAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { KeyringPair } from '@polkadot/keyring/types';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import chalk from 'chalk';

import { KUSAMA_ASSET_HUB_WS_URL } from './consts';
import { awaitBlockProduction, delay, logWithDate } from './util';
import { KUSAMA_ASSET_HUB_WS_URL, ROCOCO_ALICE_WS_URL } from './consts';
import { awaitBlockProduction, awaitEpochChange, delay, logWithDate } from './util';

const ASSET_ID = 1;
const ASSET_NAME = 'Testy';
Expand Down Expand Up @@ -57,6 +57,10 @@ const transferLPTokensCall = (api: ApiPromise, token: number, amount: number, to
return api.tx.poolAssets.transferKeepAlive(token, to.address, amount);
};

const openHrmpChannels = (api: ApiPromise, sender: number, receiver: number) => {
return api.tx.hrmp.forceOpenHrmpChannel(sender, receiver, Number(8), Number(512));
};

const main = async () => {
logWithDate(chalk.yellow('Initializing script to create a liquidity pool on Kusama Asset Hub'));
await cryptoWaitReady();
Expand All @@ -65,6 +69,24 @@ const main = async () => {
const alice = keyring.addFromUri('//Alice');
const bob = keyring.addFromUri('//Bob');

const relayApi = await ApiPromise.create({
provider: new WsProvider(ROCOCO_ALICE_WS_URL),
noInitWarn: true,
});

await relayApi.isReady;

logWithDate(chalk.blue('Opening HRMP Channels'));

let hrmpChannelCalls = [];

hrmpChannelCalls.push(openHrmpChannels(relayApi, Number(1000), Number(1836)));
hrmpChannelCalls.push(openHrmpChannels(relayApi, Number(1836), Number(1000)));

await relayApi.tx.sudo.sudo(relayApi.tx.utility.batchAll(hrmpChannelCalls)).signAndSend(alice);

await awaitEpochChange(relayApi);

const api = await ApiPromise.create({
provider: new WsProvider(KUSAMA_ASSET_HUB_WS_URL),
noInitWarn: true,
Expand All @@ -87,15 +109,15 @@ const main = async () => {
txs.push(createPool);
txs.push(addLiquidity);

await api.tx.utility.batch(txs).signAndSend(alice);
await api.tx.utility.batch(txs).signAndSend(alice, { nonce: -1 });

await delay(24000);

const nextLpToken = await api.query.assetConversion.nextPoolAssetId();

const lpToken = Number(nextLpToken) - 1;

logWithDate(chalk.yellow('Asset and Liquidity Pool created'));
logWithDate(chalk.yellow('Asset and Liquidit Pool created'));

logWithDate(chalk.green(`Liquidity Pool Token ID: ${lpToken}`));

Expand All @@ -122,7 +144,7 @@ const main = async () => {

logWithDate(chalk.magenta('Sending 1,000,000,000,000 lpTokens from Alice to Bob on Kusama Asset Hub'));

await transferLPTokensCall(api, 0, 1000000000000, bob).signAndSend(alice);
await transferLPTokensCall(api, 0, 1000000000000, bob).signAndSend(alice, { nonce: -1 });

await delay(24000);

Expand Down
22 changes: 22 additions & 0 deletions scripts/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,25 @@ export const awaitBlockProduction = async (wsUrl: string) => {
logWithDate(chalk.blue('Polkadot-js successfully disconnected'));
});
};

export const awaitEpochChange = async (api: ApiPromise) => {
const currentEpoch = await api.call.babeApi.currentEpoch();
const currentEpochIndex = currentEpoch.epochIndex;
let counter = 1;
let changedEpoch = false;
while (!changedEpoch) {
const { epochIndex } = await api.call.babeApi.currentEpoch();

if (epochIndex.toNumber() > currentEpochIndex.toNumber() + 1) {
changedEpoch = true;
}
await delay(1000);

counter += 1;
process.stdout.clearLine(0);
process.stdout.write(`\rWaiting for Epoch change${'.'.repeat((counter % 3) + 1)}`);
}

process.stdout.clearLine(0);
logWithDate(chalk.magenta('Epoch changed, HRMP channels opened'), true);
};

0 comments on commit 802fb61

Please sign in to comment.