Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xc setup #44

Merged
merged 10 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[submodule "polkadot-sdk"]
path = polkadot-sdk
url = https://github.com/paritytech/polkadot-sdk.git
branch = "release-polkadot-v1.6.0"
url = https://github.com/Szegoo/polkadot-sdk.git
branch = "release-polkadot-v1.7.0-patch"
[submodule "Astar"]
path = Astar
url = https://github.com/RegionX-Labs/Astar.git
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ This repo provides an init program which will based on the selected options set

- Description: Specify an account on the coretime chain. When specified the program will transfer a mock region to this account.

7. `--mintXcRegions <string>`:

- Description: Mints a couple of mock xc-regions. Convenient for testing the market functionality.

**Example: Testing contracts related stuff only:**

Expand Down
2 changes: 1 addition & 1 deletion polkadot-sdk
Submodule polkadot-sdk updated 1583 files
37 changes: 26 additions & 11 deletions src/contracts/index.init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import Keyring from "@polkadot/keyring";
import { CoreMask, Id, Region } from "coretime-utils";
import { log, setBalance } from "../utils";
import { deployMarket, deployXcRegions, initXcRegion, transferWrappedRegion } from "./contract";
import { approveTransfer, createRegionCollection, mintRegion } from "./uniques";
import { approveTransfer, createRegionCollection, mintRegion, registerXcRegionAsset } from "./uniques";
import * as consts from "../consts";

const keyring = new Keyring({ type: "sr25519" });

export async function contractsInit(contractsEndpoint: string, account: string, contractsPath: string) {
export async function contractsInit(contractsEndpoint: string, account: string, contractsPath: string, mint: boolean) {
const alice = keyring.addFromUri("//Alice");

const contractsProvider = new WsProvider(contractsEndpoint);
Expand All @@ -21,17 +21,32 @@ export async function contractsInit(contractsEndpoint: string, account: string,
log(`Market address: ${marketAddress}`);

await createRegionCollection(contractsApi);
await registerXcRegionAsset(contractsApi);

const mockRegion = new Region(
{ begin: 30, core: 0, mask: CoreMask.fromChunk(0, 40) },
{ end: 60, owner: alice.address, paid: null }
);

await mintRegion(contractsApi, mockRegion);
await approveTransfer(contractsApi, mockRegion, xcRegionsAddress);
await initXcRegion(contractsApi, xcRegionsAddress, mockRegion, contractsPath);
if (account) {
await setBalance(contractsApi, account, (10 ** 8 * consts.UNIT).toString());
await transferWrappedRegion(contractsApi, xcRegionsAddress, mockRegion, account, contractsPath);
}

if (mint) {
const mockRegions = [
new Region(
{ begin: 30, core: 0, mask: CoreMask.fromChunk(0, 40) },
{ end: 60, owner: alice.address, paid: null }
),
new Region({ begin: 5, core: 1, mask: CoreMask.completeMask() }, { end: 30, owner: alice.address, paid: null }),
new Region(
{ begin: 5, core: 1, mask: CoreMask.fromChunk(40, 60) },
{ end: 30, owner: alice.address, paid: null }
),
];

for await (const mockRegion of mockRegions) {
await mintRegion(contractsApi, mockRegion);
await approveTransfer(contractsApi, mockRegion, xcRegionsAddress);
await initXcRegion(contractsApi, xcRegionsAddress, mockRegion, contractsPath);
if (account) {
await transferWrappedRegion(contractsApi, xcRegionsAddress, mockRegion, account, contractsPath);
}
}
}
}
20 changes: 19 additions & 1 deletion src/contracts/uniques.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiPromise } from "@polkadot/api";
import { log } from "../utils";
import { force, log } from "../utils";
import { keyring } from "../utils";
import { Region } from "coretime-utils";

Expand All @@ -13,7 +13,7 @@
const createCollectionCall = contractsApi.tx.uniques.create(REGION_COLLECTION_ID, alice.address);

const callTx = async (resolve: () => void) => {
const unsub = await createCollectionCall.signAndSend(alice, (result: any) => {

Check warning on line 16 in src/contracts/uniques.ts

View workflow job for this annotation

GitHub Actions / build-and-test

Unexpected any. Specify a different type
if (result.status.isInBlock) {
unsub();
resolve();
Expand All @@ -24,6 +24,24 @@
return new Promise(callTx);
}

export async function registerXcRegionAsset(contractsApi: ApiPromise): Promise<void> {
log(`Registering xc-region asset`);

const registerCall = contractsApi.tx.xcAssetConfig.registerAssetLocation(
{
V3: {
parents: 1,
interior: {
X2: [{ Parachain: 1005 }, { PalletInstance: 50 }],
},
},
},
REGION_COLLECTION_ID
);

return force(contractsApi, registerCall);
}

export async function mintRegion(contractsApi: ApiPromise, region: Region): Promise<void> {
log(`Minting a region`);

Expand All @@ -32,7 +50,7 @@
const mintCall = contractsApi.tx.uniques.mint(REGION_COLLECTION_ID, rawRegionId, alice.address);

const callTx = async (resolve: () => void) => {
const unsub = await mintCall.signAndSend(alice, (result: any) => {

Check warning on line 53 in src/contracts/uniques.ts

View workflow job for this annotation

GitHub Actions / build-and-test

Unexpected any. Specify a different type
if (result.status.isInBlock) {
unsub();
resolve();
Expand All @@ -51,7 +69,7 @@
const approveCall = contractsApi.tx.uniques.approveTransfer(REGION_COLLECTION_ID, rawRegionId, delegate);

const callTx = async (resolve: () => void) => {
const unsub = await approveCall.signAndSend(alice, (result: any) => {

Check warning on line 72 in src/contracts/uniques.ts

View workflow job for this annotation

GitHub Actions / build-and-test

Unexpected any. Specify a different type
if (result.status.isInBlock) {
unsub();
resolve();
Expand Down
9 changes: 9 additions & 0 deletions src/coretime/index.init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
const coretimeWsProvider = new WsProvider(coretimeEndpoint);
const coretimeApi = await ApiPromise.create({ provider: coretimeWsProvider });

await forceSafeXCMVersion(coretimeApi);
await configureBroker(coretimeApi);
await startSales(coretimeApi);

Expand All @@ -18,6 +19,7 @@
const regionId = await purchaseRegion(coretimeApi, alice);

if (coretimeAccount) {
await setBalance(coretimeApi, coretimeAccount, (1000 * consts.UNIT).toString());
await transferRegion(coretimeApi, alice, coretimeAccount, regionId);
}
}
Expand All @@ -36,12 +38,19 @@
return force(coretimeApi, startSaleCall);
}

async function forceSafeXCMVersion(coretimeApi: ApiPromise): Promise<void> {
log(`Setting the safe XCM version to V3`);

const setVersionCall = coretimeApi.tx.polkadotXcm.forceDefaultXcmVersion([3]);
return force(coretimeApi, setVersionCall);
}

export async function purchaseRegion(coretimeApi: ApiPromise, buyer: KeyringPair): Promise<RegionId> {
log(`Purchasing a region.`);

const callTx = async (resolve: (regionId: RegionId) => void) => {
const purchase = coretimeApi.tx.broker.purchase(consts.INITIAL_PRICE * 2);
const unsub = await purchase.signAndSend(buyer, async (result: any) => {

Check warning on line 53 in src/coretime/index.init.ts

View workflow job for this annotation

GitHub Actions / build-and-test

Unexpected any. Specify a different type
if (result.status.isInBlock) {
const regionId = await getRegionId(coretimeApi);
unsub();
Expand All @@ -63,7 +72,7 @@

const callTx = async (resolve: () => void) => {
const transfer = coretimeApi.tx.broker.transfer(regionId, receiver);
const unsub = await transfer.signAndSend(sender, (result: any) => {

Check warning on line 75 in src/coretime/index.init.ts

View workflow job for this annotation

GitHub Actions / build-and-test

Unexpected any. Specify a different type
if (result.status.isInBlock) {
unsub();
resolve();
Expand Down
7 changes: 4 additions & 3 deletions src/relay/index.init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export async function relayInit(relayEndpoint: string, coretimeParaId: number, c
const relayWsProvider = new WsProvider(relayEndpoint);
const relayApi = await ApiPromise.create({ provider: relayWsProvider });

openHrmpChannel(relayApi, coretimeParaId, contractsParaId);
await openHrmpChannel(relayApi, coretimeParaId, contractsParaId);
await openHrmpChannel(relayApi, contractsParaId, coretimeParaId);
}

async function openHrmpChannel(relayApi: ApiPromise, sender: number, recipient: number): Promise<void> {
Expand All @@ -15,12 +16,12 @@ async function openHrmpChannel(relayApi: ApiPromise, sender: number, recipient:
sender,
recipient,
8, // Max capacity
512, // Max message size
102400, // Max message size
];

const alice = keyring.addFromUri("//Alice");

const openHrmp = relayApi.tx.hrmp.forceOpenHrmpChannel(...newHrmpChannel);
const openHrmp = relayApi.tx.parasSudoWrapper.sudoEstablishHrmpChannel(...newHrmpChannel);
const sudoCall = relayApi.tx.sudo.sudo(openHrmp);

const callTx = async (resolve: () => void) => {
Expand Down
10 changes: 8 additions & 2 deletions src/zombienet.init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ program
.option("--contractsInit")
.option("--contractsPath <string>")
.option("--contractsAccount <string>")
.option("--coretimeAccount <string>");
.option("--coretimeAccount <string>")
.option("--mintXcRegions");

program.parse(process.argv);

Expand All @@ -39,7 +40,12 @@ async function init() {
throw new Error("--contractsPath must be specified");
}
const contractsPath = normalizePath(program.opts().contractsPath);
await contractsInit(CONTRACTS_ENDPOINT, program.opts().contractsAccount || "", contractsPath);
await contractsInit(
CONTRACTS_ENDPOINT,
program.opts().contractsAccount || "",
contractsPath,
program.opts().mintXcRegions
);
}
}

Expand Down
Loading