Skip to content

Commit

Permalink
Region AssetTransactor (#100)
Browse files Browse the repository at this point in the history
* Region AssetTransactor

* workaround

* fix merge

* init tests

* progress

* sending xcm works | receving fails

* cross-chain region transfer works

* regionx region receival test

* switch to typescript

* update readme

* update package name

* remove unused file

* remove unused imports

* cargo fmt

* move region-transfer test

* transfering to coretime chain

* progress

* all good but unpaid execution?

* works :)) but messy

* merge fixes

* fixes

* small fixes

* format & refactor

* Update e2e_tests/xc-transfer/asset-transfer.ts

Co-authored-by: cuteolaf <OliverLim818@gmail.com>

* Update e2e_tests/common.ts

Co-authored-by: cuteolaf <OliverLim818@gmail.com>

* remove file

* trusted reserves

* fix asset-transfer test

* small fixes

---------

Co-authored-by: cuteolaf <oliverlim818@gmail.com>
  • Loading branch information
Szegoo and cuteolaf authored May 9, 2024
1 parent f04c8d0 commit 84023f4
Show file tree
Hide file tree
Showing 17 changed files with 746 additions and 72 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ bin/
bin/

node_modules
build/
**/package-lock.json
**/build
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
3. Get the polkadot binary:
```sh
zombienet-linux setup polkadot
zombienet-linux setup polkadot polkadot-parachain
Please add the dir to your $PATH by running the command:
export PATH=/home/<username>/RegionX-Node/:$PATH
Expand Down
52 changes: 48 additions & 4 deletions e2e_tests/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiPromise } from "@polkadot/api";
import { ApiPromise, Keyring } from "@polkadot/api";
import { SubmittableExtrinsic, SignerOptions } from "@polkadot/api/types";
import { KeyringPair } from "@polkadot/keyring/types";

Expand Down Expand Up @@ -30,7 +30,7 @@ async function submitExtrinsic(
async function setupRelayAsset(
api: ApiPromise,
signer: KeyringPair,
initialBalance: bigint = 10n ** 12n
initialBalance: bigint = 0n
) {
const assetMetadata = {
decimals: 12,
Expand Down Expand Up @@ -58,8 +58,52 @@ async function setupRelayAsset(
await submitExtrinsic(signer, sudoCall, {});
}

// Transfer the relay chain asset to the parachain specified by paraId.
// Receiver address is same as the sender's.
async function transferRelayAssetToPara(amount: bigint, paraId: number, relayApi: ApiPromise, signer: KeyringPair) {
const receiverKeypair = new Keyring();
receiverKeypair.addFromAddress(signer.address);

// If system parachain we use teleportation, otherwise we do a reserve transfer.
const transferKind = paraId < 2000 ? 'limitedTeleportAssets' : 'limitedReserveTransferAssets';

const feeAssetItem = 0;
const weightLimit = "Unlimited";
const reserveTransfer = relayApi.tx.xcmPallet[transferKind](
{ V3: { parents: 0, interior: { X1: { Parachain: paraId } } } }, //dest
{
V3: {
parents: 0,
interior: {
X1: {
AccountId32: {
chain: "Any",
id: receiverKeypair.pairs[0].publicKey,
},
},
},
},
}, //beneficiary
{
V3: [
{
id: {
Concrete: { parents: 0, interior: "Here" },
},
fun: {
Fungible: amount,
},
},
],
}, //asset
feeAssetItem,
weightLimit,
);
await submitExtrinsic(signer, reserveTransfer, {});
}

async function sleep(milliseconds: number) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
return new Promise(resolve => setTimeout(resolve, milliseconds));
}

export { submitExtrinsic, setupRelayAsset, sleep, RELAY_ASSET_ID };
export { submitExtrinsic, setupRelayAsset, transferRelayAssetToPara, sleep, RELAY_ASSET_ID }
19 changes: 19 additions & 0 deletions e2e_tests/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const UNIT = 10 ** 12; // ROC has 12 decimals

const INITIAL_PRICE = 50 * UNIT;
const CORE_COUNT = 10;
const TIMESLICE_PERIOD = 80;
const IDEAL_CORES_SOLD = 5;

const CONFIG = {
advance_notice: 20,
interlude_length: 0,
leadin_length: 10,
ideal_bulk_proportion: 0,
limit_cores_offered: 50,
region_length: 30,
renewal_bump: 10,
contribution_timeout: 5,
};

export {UNIT, INITIAL_PRICE, CORE_COUNT, TIMESLICE_PERIOD, IDEAL_CORES_SOLD, CONFIG};
1 change: 1 addition & 0 deletions e2e_tests/governance/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async function run(nodeName: string, networkInfo: any, _jsArgs: any) {
const keyring = new Keyring({ type: "sr25519" });
const alice = keyring.addFromUri("//Alice");

// relay asset is needed for storing the preimage.
await setupRelayAsset(api, alice);

const spendCallBytes = api.tx.treasury.spendLocal(10n ** 6n, alice.address).toU8a();
Expand Down
1 change: 1 addition & 0 deletions e2e_tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@polkadot/api": "^11.0.2",
"@polkadot/keyring": "^12.6.2",
"coretime-utils": "^0.3.2",
"typescript": "^5.4.5"
}
}
28 changes: 14 additions & 14 deletions e2e_tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"outDir": "build",
"target": "ES2020",
"strict": true,
"esModuleInterop": true
},
"include": [
"**/*.ts"
],
"exclude": [
"node_modules"
]
}
"compilerOptions": {
"module": "commonjs",
"outDir": "build",
"target": "ES2020",
"strict": true,
"esModuleInterop": true
},
"include": [
"**/*.ts"
],
"exclude": [
"node_modules"
]
}
55 changes: 11 additions & 44 deletions e2e_tests/xc-transfer/asset-transfer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiPromise, WsProvider, Keyring } from "@polkadot/api";
import { RELAY_ASSET_ID, setupRelayAsset, sleep, submitExtrinsic } from "../common";
import { RELAY_ASSET_ID, setupRelayAsset, sleep, submitExtrinsic, transferRelayAssetToPara } from "../common";

import assert from "node:assert";

Expand Down Expand Up @@ -27,58 +27,25 @@ async function run(nodeName: string, networkInfo: any, _jsArgs: any) {
const assertRegionXBalance = async (address: string, balance: bigint) => {
const { free } = (
await regionXApi.query.tokens.accounts(address, RELAY_ASSET_ID)
).toHuman() as any;
).toJSON() as any;

console.log(`RegionX: ${free}`);
assert(balance - BigInt(free.toString().replace(/,/g, "")) < TOLERANCE);
console.log(`RegionX: ${BigInt(free).toString()} | Expected: ${balance}`);
assert(balance - BigInt(free) < TOLERANCE);
};

const assertRococoBalance = async (address: string, balance: bigint) => {
const {
data: { free },
} = (await rococoApi.query.system.account(address)).toHuman() as any;
} = (await rococoApi.query.system.account(address)).toJSON() as any;

console.log(`Rococo: ${free}`);
assert(balance - BigInt(free.toString().replace(/,/g, "")) < TOLERANCE);
console.log(`Rococo: ${BigInt(free).toString()} | Expected: ${balance}`);
assert(balance - BigInt(free) < TOLERANCE);
};

await assertRegionXBalance(alice.address, 10n ** 12n);
await assertRococoBalance(alice.address, 10n ** 18n);

const feeAssetItem = 0;
const weightLimit = "Unlimited";
const rococoReserveTransfer = rococoApi.tx.xcmPallet.limitedReserveTransferAssets(
{ V3: { parents: 0, interior: { X1: { Parachain: 2000 } } } }, //dest
{
V3: {
parents: 0,
interior: {
X1: {
AccountId32: {
chain: "Any",
id: receiverKeypair.pairs[0].publicKey,
},
},
},
},
}, //beneficiary
{
V3: [
{
id: {
Concrete: { parents: 0, interior: "Here" },
},
fun: {
Fungible: 3n * 10n ** 12n,
},
},
],
}, //asset
feeAssetItem,
weightLimit
);
await submitExtrinsic(alice, rococoReserveTransfer, {});

await transferRelayAssetToPara(3n * 10n ** 12n, 2000, rococoApi, alice);
await sleep(5 * 1000);

await assertRegionXBalance(alice.address, 4n * 10n ** 12n);
Expand Down Expand Up @@ -111,15 +78,15 @@ async function run(nodeName: string, networkInfo: any, _jsArgs: any) {
},
],
}, //asset
feeAssetItem,
weightLimit
0,
"Unlimited"
);

await submitExtrinsic(alice, regionXReserveTransfer, {});

await sleep(5 * 1000);

await assertRegionXBalance(alice.address, 4n * 10n ** 12n);
await assertRegionXBalance(alice.address, 3n * 10n ** 12n);
await assertRococoBalance(alice.address, 10n ** 18n - 3n * 10n ** 12n);
}

Expand Down
Loading

0 comments on commit 84023f4

Please sign in to comment.