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

Test staking #277

Merged
merged 20 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,46 @@ by any relay chain, but still runs the Moonbeam runtime logic.
./target/release/moonbase-standalone --dev
```

### Dev Addresses

Launching the node in devmode will prefund a list of dev addresses that are derived from
the canonical mnemonic: "bottom drive obey lake curtain smoke basket hold race lonely fit walk"

#### Alith:

- Address:0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac
- PrivKey:0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133

#### Baltathar:

- Address:0x3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0
- PrivKey:0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b

#### Charleth:

- Address:0x798d4Ba9baf0064Ec19eB4F0a1a45785ae9D6DFc
- PrivKey:0x0b6e18cafb6ed99687ec547bd28139cafdd2bffe70e6b688025de6b445aa5c5b

#### Dorothy:

- Address:0x773539d4Ac0e786233D90A233654ccEE26a613D9
- PrivKey:0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68

#### Ethan:

- Address:0xFf64d3F6efE2317EE2807d223a0Bdc4c0c49dfDB
- PrivKey:0x7dce9bc8babb68fec1409be38c8e1a52650206a7ed90ff956ae8a6d15eeaaef4

#### Faith:

- Address:0xC0F0f4ab324C46e55D02D0033343B4Be8A55532d
- PrivKey:0xb9d2ea9a615f3165812e8d44de0d24da9bbd164b65c4f0573e1ce2c8dbd9c8df

#### Gerald:

- Address:0x7BF369283338E12C90514468aa3868A551AB2929
- PrivKey:0x96b8a38e12e1a31dee1eab2fffdf9d9990045f5b37e44d8cc27766ef294acf18

### Docker image

An alternative to building locally is to use docker to run a pre-build binary.
Expand Down
8 changes: 4 additions & 4 deletions tools/config_moonbeam.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
"parachains": [
{
"bin": "../target/release/moonbeam",
"id": "200",
"id": "1000",
"rpcPort": 36846,
"wsPort": 36946,
"port": 36335,
"balance": "1000",
"chain": "local",
"chain": "staking-test-spec.json",
"flags": [
"--no-telemetry",
"--no-prometheus",
Expand All @@ -39,12 +39,12 @@
},
{
"bin": "../target/release/moonbeam",
"id": "200",
"id": "1000",
"rpcPort": 36847,
"wsPort": 36947,
"port": 36336,
"balance": "1000",
"chain": "local",
"chain": "staking-test-spec.json",
"flags": [
"--no-telemetry",
"--no-prometheus",
Expand Down
3 changes: 2 additions & 1 deletion tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"package-moon-key": "node_modules/.bin/tsc moon-key.ts; node_modules/.bin/pkg -t node14 moon-key.js; rm moon-key.js",
"build-moonbeam-launch": "yarn add PureStake/polkadot-launch#moonbeam-launch",
"moonbeam-launch": "yarn run build-moonbeam-launch && mkdir -p specFiles && ts-node moonbeam-launch.ts",
"moonbeam-test": "yarn run build-moonbeam-launch && mkdir -p specFiles && ts-node moonbeam-test.ts"
"moonbeam-test": "yarn run build-moonbeam-launch && mkdir -p specFiles && ts-node moonbeam-test.ts",
"test-staking": "yarn run build-moonbeam-launch && mkdir -p specFiles && ts-node test-staking.ts"
}
}
86 changes: 86 additions & 0 deletions tools/staking-test-spec.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions tools/test-alpha.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ApiPromise, WsProvider } from "@polkadot/api";
import Web3 from "web3";
import { typesBundle } from "../moonbeam-types-bundle";
import { FAITH } from "./test-constants";
const wsProviderUrl = `wss://wss.testnet.moonbeam.network`;
const GERALD = "0x6be02d1d3665660d22ff9624b7be0551ee1ac91b";
const FAITH = "0xC0F0f4ab324C46e55D02D0033343B4Be8A55532d";

export default async function test(ACC: string) {
const web3 = new Web3(wsProviderUrl);
Expand Down
14 changes: 14 additions & 0 deletions tools/test-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const GERALD = "0x6be02d1d3665660d22ff9624b7be0551ee1ac91b";
export const FAITH = "0xC0F0f4ab324C46e55D02D0033343B4Be8A55532d";
export const ETHAN_PRIVKEY = "0x7dce9bc8babb68fec1409be38c8e1a52650206a7ed90ff956ae8a6d15eeaaef4";
export const ETHAN = "0xFf64d3F6efE2317EE2807d223a0Bdc4c0c49dfDB";
export const ALITH_PRIVKEY = "0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133";
export const ALITH = "0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac";
export const STAKING_AMOUNT = "1.0000 kUnit";

export const GLMR = 1_000_000_000_000_000_000n;
export const MIN_GLMR_STAKING = 100_000n * GLMR;
export const MIN_GLMR_NOMINATOR = 5n * GLMR;
export const DEFAULT_GENESIS_BALANCE = 2n ** 80n;
export const DEFAULT_GENESIS_STAKING = 1_000n * GLMR;
export const GENESIS_ACCOUNT_BALANCE = DEFAULT_GENESIS_BALANCE - DEFAULT_GENESIS_STAKING;
124 changes: 124 additions & 0 deletions tools/test-staking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { ApiPromise, Keyring, WsProvider } from "@polkadot/api";
import { start } from "polkadot-launch";
import { typesBundle } from "../moonbeam-types-bundle";
import {
ALITH,
GERALD,
FAITH,
STAKING_AMOUNT,
ETHAN_PRIVKEY,
ETHAN,
GENESIS_ACCOUNT_BALANCE,
DEFAULT_GENESIS_BALANCE,
ALITH_PRIVKEY,
GLMR,
MIN_GLMR_NOMINATOR,
MIN_GLMR_STAKING,
} from "./test-constants";

function assert(condition: boolean, msg: string) {
if (!condition) throw new Error(msg);
}

async function wait(duration: number) {
console.log(`Waiting ${duration / 1000} seconds`);
return new Promise((res) => {
setTimeout(res, duration);
});
}

async function test() {
await start("config_moonbeam.json");
console.log("done");
const WS_PORT = 36946;
const wsProviderUrl = `ws://localhost:${WS_PORT}`;

const wsProvider = new WsProvider(wsProviderUrl);
const polkadotApi = await ApiPromise.create({
provider: wsProvider,
typesBundle: typesBundle as any,
});

// Balance
const account = await polkadotApi.query.system.account(ETHAN);
// assert(
// account.data.free.toString() === DEFAULT_GENESIS_BALANCE.toString(),
// "wrong balance for Ethan, dif: "+(Number(DEFAULT_GENESIS_BALANCE)-Number(account.data.free))
// );

// Nominators
const nominators = await polkadotApi.query.stake.nominators(GERALD);
assert(nominators.toHuman() === null, "there should be no nominator");

// Validators
const validators = await polkadotApi.query.stake.validators();
assert(validators.toHuman()[0].toLowerCase() === GERALD, "Gerald is not a validator");
assert(validators.toHuman()[1].toLowerCase() === FAITH.toLowerCase(), "Faith is not a validator");

// Candidates
const candidates = await polkadotApi.query.stake.candidatePool();
console.log("candidates", candidates.toHuman());
assert(candidates.toHuman()[0].owner.toLowerCase() === GERALD, "Gerald is not a candidates");
assert(
candidates.toHuman()[1].owner.toLowerCase() === FAITH.toLowerCase(),
"Faith is not a candidates"
);
assert(candidates.toHuman()[0].amount === STAKING_AMOUNT, "Gerald has wrong staking amount");
assert(candidates.toHuman()[1].amount === STAKING_AMOUNT, "Faith has wrong staking amount");

// Join Candidates
const keyring = new Keyring({ type: "ethereum" });
const testAccount = await keyring.addFromUri(ETHAN_PRIVKEY, null, "ethereum");
const unsub = await polkadotApi.tx.stake
.joinCandidates(0, MIN_GLMR_STAKING)
.signAndSend(testAccount, ({ events = [], status }) => {
console.log(`Current status is ${status.type}`);

if (status.isFinalized) {
console.log(`Transaction finalized at blockHash ${status.asFinalized}`);

// Loop through Vec<EventRecord> to display all events
events.forEach(({ phase, event: { data, method, section } }) => {
console.log(`\t' ${phase}: ${section}.${method}:: ${data}`);
});

unsub();
}
});
await wait(50000);
const candidatesAfter = await polkadotApi.query.stake.candidatePool();
console.log("candidatesAfter", candidatesAfter.toHuman());
assert(
(candidatesAfter.toHuman() as { owner: string; amount: string }[]).length === 3,
"new candidate should have been added"
);
const ethan = await polkadotApi.query.system.account(ETHAN);
console.log(ethan.data.free.toString());
console.log(DEFAULT_GENESIS_BALANCE.toString());

// Join Nominators
const keyringAlith = new Keyring({ type: "ethereum" });
const alith = await keyringAlith.addFromUri(ALITH_PRIVKEY, null, "ethereum");
const unsub2 = await polkadotApi.tx.stake
.joinNominators(GERALD, MIN_GLMR_NOMINATOR)
.signAndSend(alith, ({ events = [], status }) => {
console.log(`Current status is ${status.type}`);

if (status.isFinalized) {
console.log(`Transaction finalized at blockHash ${status.asFinalized}`);

// Loop through Vec<EventRecord> to display all events
events.forEach(({ phase, event: { data, method, section } }) => {
console.log(`\t' ${phase}: ${section}.${method}:: ${data}`);
});

unsub2();
}
});
await wait(50000);
const nominatorsAfter = await polkadotApi.query.stake.nominators(ALITH);
console.log("nominatorsAfter", nominatorsAfter.toHuman());

console.log("SUCCESS");
}
test();
2 changes: 1 addition & 1 deletion tools/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4112,7 +4112,7 @@ pkg@^4.4.9:

polkadot-launch@PureStake/polkadot-launch#moonbeam-launch:
version "1.0.12"
resolved "https://codeload.github.com/PureStake/polkadot-launch/tar.gz/fce3dec385af9a5ea4ed3ad8d4bc1d6db6b158e4"
resolved "https://codeload.github.com/PureStake/polkadot-launch/tar.gz/e0ab521ca814a1a2ec7af4914ea5b8087bb85c1c"
dependencies:
"@polkadot/api" "^3.11.1"
"@polkadot/util" "^5.9.2"
Expand Down