Skip to content

Commit

Permalink
Transferable Balance Test (#2512)
Browse files Browse the repository at this point in the history
* use referenda for testing transferable balance

---------

Co-authored-by: Andrea Giacobino <[email protected]>
  • Loading branch information
ahmadkaouk and noandrea authored Dec 11, 2023
1 parent 9014c50 commit 2b9982e
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 12 deletions.
19 changes: 14 additions & 5 deletions test/helpers/voting.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import { DevModeContext } from "@moonwall/cli";
import { ALITH_ADDRESS, alith } from "@moonwall/util";
import { alith } from "@moonwall/util";
import { expectSubstrateEvent } from "./expect.js";
import { KeyringPair } from "@polkadot/keyring/types";

export async function createProposal(context: DevModeContext, track = "root") {
let nonce = (await context.polkadotJs().rpc.system.accountNextIndex(ALITH_ADDRESS)).toNumber();
export async function createProposal({
context,
track = "root",
from = alith,
}: {
context: DevModeContext;
track?: string;
from?: KeyringPair;
}) {
let nonce = (await context.polkadotJs().rpc.system.accountNextIndex(from.address)).toNumber();
const call = context.polkadotJs().tx.identity.setIdentity({ display: { raw: "Me" } });
const block = await context.createBlock([
await context
.polkadotJs()
.tx.preimage.notePreimage(call.toHex())
.signAsync(alith, { nonce: nonce++ }),
.signAsync(from, { nonce: nonce++ }),
await context
.polkadotJs()
.tx.referenda.submit(
track == "root" ? { system: "root" } : { Origins: track },
{ Lookup: { Hash: call.hash.toHex(), len: call.length } },
{ After: 1 }
)
.signAsync(alith, { nonce: nonce++ }),
.signAsync(from, { nonce: nonce++ }),
]);
return expectSubstrateEvent(block, "referenda", "Submitted").data[0].toNumber();
}
Expand Down
101 changes: 101 additions & 0 deletions test/suites/dev/test-balance/test-balance-transferable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import "@moonbeam-network/api-augment";
import { beforeAll, describeSuite, expect } from "@moonwall/cli";
import { ALITH_ADDRESS, GLMR, baltathar, checkBalance, generateKeyringPair } from "@moonwall/util";
import { createProposal } from "../../../helpers/voting.ts";

describeSuite({
id: "D4005",
title: "Balance - Transferable",
foundationMethods: "dev",
testCases: ({ context, it }) => {
const randomAccount = generateKeyringPair();
const randomAddress = randomAccount.address as `0x${string}`;

beforeAll(async function () {
await context.createBlock(
context.polkadotJs().tx.balances.transfer(randomAccount.address, 38n * GLMR),
{ allowFailures: false }
);
});

it({
id: "T01",
title: "Consistent Transferable Balance Computation for Fee Calculation and Actual Balance",
test: async function () {
{
// In this test, the following actions are performed:
// 1. A new account is created with an initial balance of 38 GLMR.
// 2. An identity proposal is submitted by randomAccount (~15 GLMR)
// 3. 20 GLMR are delegated to Alith from randomAccount.
// 4. 5 GLMR are transferred to Balthazar from randomAccount.
// 5. A second transfer of 2 GLMR to Balthazar is performed from randomAccount.

// Delegate to Alith
const { result: res } = await context.createBlock(
context
.polkadotJs()
.tx.parachainStaking.delegate(ALITH_ADDRESS, 20n * GLMR, 10, 10)
.signAsync(randomAccount)
);
expect(res!.successful).to.be.true;

// Create a proposal
const propNum = await createProposal({ context, from: randomAccount });
expect(propNum).toBe(0);

// Balance after proposal
const balanceAfter = (
await context.polkadotJs().query.system.account(randomAccount.address)
).data.free.toBigInt();

// Check the balance of randomAccount before tranfer
const balanceBeforeTransfer = await checkBalance(context, randomAddress);
expect(balanceBeforeTransfer).toBeGreaterThan(9n * GLMR);

// Get fee for transfer
const fee = await context
.polkadotJs()
.tx.balances.transfer(randomAccount.address, 5n * GLMR)
.paymentInfo(randomAccount.address);

// Transfer 5 GLMR to Balthazar
const { result: res3 } = await context.createBlock(
context
.polkadotJs()
.tx.balances.transfer(baltathar.address, 5n * GLMR)
.signAsync(randomAccount)
);

expect(res3!.successful).to.be.true;
expect(await checkBalance(context, randomAddress)).toBe(
balanceBeforeTransfer - 5n * GLMR - fee.partialFee.toBigInt()
);
expect(await checkBalance(context, randomAddress)).toBeGreaterThan(4n * GLMR);

// Do a second transfer of 2 GLMR to Balthazar
// const { result: res2 } = await context.createBlock(
// context
// .polkadotJs()
// .tx.balances.transfer(baltathar.address, 2n * GLMR)
// .signAsync(randomAccount)
// );
// expect(res2!.successful).to.be.true;

// TODO Change this check once the transferable balance is fixed
// Check Ticket MOON-2598: https://opslayer.atlassian.net/browse/MOON-2598
expect(
async () =>
await context.createBlock(
context
.polkadotJs()
.tx.balances.transfer(baltathar.address, 2n * GLMR)
.signAsync(randomAccount)
)
).rejects.toThrowError(
"1010: Invalid Transaction: Inability to pay some fees , e.g. account balance too low"
);
}
},
});
},
});
2 changes: 1 addition & 1 deletion test/suites/dev/test-locks/test-locks-multiple-locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describeSuite({
await context.createBlock(
createRawTransfer(context, randomAddress, MIN_GLMR_DELEGATOR + GLMR)
);
proposalIndex = await createProposal(context);
proposalIndex = await createProposal({ context });
});

it({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describeSuite({

beforeEach(async function () {
convictionVoting = new ConvictionVoting(context);
proposalIndex = await createProposal(context);
proposalIndex = await createProposal({ context });
});

it({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describeSuite({

beforeEach(async function () {
convictionVoting = new ConvictionVoting(context);
proposalIndex = await createProposal(context);
proposalIndex = await createProposal({ context });
});

for (const conviction of CONVICTION_VALUES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describeSuite({
});

beforeEach(async function () {
proposalIndex = await createProposal(context);
proposalIndex = await createProposal({ context });

const block = await convictionVoting.voteYes(proposalIndex, 1n * 10n ** 18n, 1n).block();
// Verifies the setup is correct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describeSuite({

beforeEach(async function () {
convictionVoting = new ConvictionVoting(context);
proposalIndex = await createProposal(context, "generaladmin");
proposalIndex = await createProposal({ context, track: "generaladmin" });

const block = await convictionVoting.voteYes(proposalIndex, GLMR, 1n).block();
// Verifies the setup is correct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describeSuite({

beforeAll(async function () {
// Whitelist caller is track 3
proposalIndex = await createProposal(context, "whitelistedcaller");
proposalIndex = await createProposal({ context, track: "whitelistedcaller" });
await context.createBlock(
context.polkadotJs().tx.referenda.placeDecisionDeposit(proposalIndex),
{ allowFailures: false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describeSuite({
let convictionVoting: ConvictionVoting;

beforeAll(async function () {
proposalIndex = await createProposal(context);
proposalIndex = await createProposal({ context });

convictionVoting = new ConvictionVoting(context);
const blockAlith_1 = await convictionVoting.voteYes(proposalIndex, GLMR, 1n).block();
Expand Down

0 comments on commit 2b9982e

Please sign in to comment.