Skip to content

Commit

Permalink
add treasury council test
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Jan 15, 2025
1 parent 9427412 commit 4a46c92
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 14 deletions.
29 changes: 29 additions & 0 deletions test/scripts/print-failed-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.resolve(path.dirname(__filename), "..");

["Moonbeam", "Moonriver", "Moonbase"].forEach((runtime) => {
const outputPath = `${__dirname}/tmp/testResults${runtime}.json`;
if (!fs.existsSync(outputPath)) {
return;
}

const fileContent = fs.readFileSync(outputPath, { encoding: "utf-8" });
const { testResults } = JSON.parse(fileContent);

const failedSuites = testResults.filter((suite) => suite.status !== "passed");
const failedTests = failedSuites
.flatMap((suite) => {
const file = suite.name.replaceAll(__dirname, "");
const fails = suite.assertionResults.map((test) => `\n\t\t> ${test.title.trim()}`).join("");
return `\t- ${file}${fails}`;
})
.join("\n");

if (failedTests) {
console.log(`Failed tests for ${runtime}:\n`, failedTests);
}
});
87 changes: 73 additions & 14 deletions test/suites/dev/moonbase/test-treasury/test-treasury-pallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@ import { beforeAll, describeSuite, expect } from "@moonwall/cli";
import type { ApiPromise } from "@polkadot/api";
import { alith, baltathar, ethan } from "@moonwall/util";
import { expectSubstrateEvent } from "../../../../helpers";
import type { FrameSupportPalletId } from "@polkadot/types/lookup";

describeSuite({
id: "D013801",
title: "Treasury pallet tests",
foundationMethods: "dev",
testCases: ({ context, log, it }) => {
let treasuryPalletId: FrameSupportPalletId;
let treasuryAddress: string;
let api: ApiPromise;

beforeAll(async function () {
api = context.polkadotJs();
treasuryPalletId = api.consts.treasury.palletId;
treasuryAddress = `0x6d6f646C${treasuryPalletId.toString().slice(2)}0000000000000000`;
});

it({
id: "T01",
title: "Non root cannot spend (local)",
title: "Origins that are not Root or members of treasury council cannot spend treasury funds",
test: async function () {
expect((await api.query.treasury.spendCount()).toNumber()).to.equal(0);

// Creates a proposal
const proposal_value = 1000000000n;
const tx = api.tx.treasury.spendLocal(proposal_value, ethan.address);
const signedTx = await tx.signAsync(baltathar);
const blockResult = await context.createBlock(signedTx);

expectSubstrateEvent(blockResult, "system", "ExtrinsicFailed");
await context.createBlock(signedTx, {
expectEvents: [api.events.system.ExtrinsicFailed],
});

expect((await api.query.treasury.proposalCount()).toNumber()).to.equal(0);
expect((await api.query.treasury.spendCount()).toNumber()).to.equal(0);
Expand All @@ -36,22 +41,76 @@ describeSuite({

it({
id: "T02",
title: "Root should be able to spend (local) and approve a proposal",
timeout: -1,
title: "Root can spend treasury funds",
test: async function () {
const spendPeriod = api.consts.treasury.spendPeriod.toNumber();
// Fund treasury
const treasuryPot = 2_000_000_000_000_000n;
await context.createBlock(
await api.tx.balances.transferAllowDeath(treasuryAddress, treasuryPot),
{ allowFailures: false }
);
await context.createBlock();
expect((await api.query.treasury.deactivated()).toBigInt()).toBeGreaterThan(treasuryPot);

expect((await api.query.treasury.spendCount()).toNumber()).to.equal(0);
// Creates a proposal
// Value needs to be higher than the transaction fee paid by ethan,
// but lower than the total treasury pot
const proposal_value = 1000000000n;
// Pre-checks
expect((await api.query.treasury.proposalCount()).toNumber()).to.equal(0);
expect((await api.query.treasury.approvals()).length).to.equal(0);

// Approve treasury spend to Ethan
const proposal_value = 1_000_000_000_000_000n;
const tx = api.tx.treasury.spendLocal(proposal_value, ethan.address);
const signedTx = await api.tx.sudo.sudo(tx).signAsync(alith);
const blockResult = await context.createBlock(signedTx, { allowFailures: false });
await context.createBlock(signedTx, {
allowFailures: false,
expectEvents: [api.events.treasury.SpendApproved],
});

// Spending was successfully approved
expect((await api.query.treasury.proposalCount()).toNumber()).to.equal(1);
expect((await api.query.treasury.approvals()).length).to.equal(1);
},
});

it({
id: "T03",
title: "Members of the treasury council can spend treasury funds",
test: async function () {
// Set Alith as member of the treasury council
const setMembersTX = api.tx.treasuryCouncilCollective.setMembers(
[alith.address],
alith.address,
3
);
await context.createBlock(await api.tx.sudo.sudo(setMembersTX).signAsync(alith), {
allowFailures: false,
});

// Fund treasury
const treasuryPot = 2_000_000_000_000_000n;
await context.createBlock(
await api.tx.balances.transferAllowDeath(treasuryAddress, treasuryPot),
{ allowFailures: false }
);
await context.createBlock();
expect((await api.query.treasury.deactivated()).toBigInt()).toBeGreaterThan(treasuryPot);

// Pre-checks
expect((await api.query.treasury.proposalCount()).toNumber()).to.equal(0);
expect((await api.query.treasury.approvals()).length).to.equal(0);

// Approve treasury spend to Ethan
const proposal_value = 1_000_000_000_000_000n;
const tx = api.tx.treasury.spendLocal(proposal_value, ethan.address);
const signedTx = api.tx.treasuryCouncilCollective.propose(1, tx, 1_000).signAsync(alith);

await context.createBlock(signedTx, {
allowFailures: false,
expectEvents: [api.events.treasury.SpendApproved],
});

expectSubstrateEvent(blockResult, "treasury", "SpendApproved");
// Spending was successfully approved
expect((await api.query.treasury.proposalCount()).toNumber()).to.equal(1);
expect((await api.query.treasury.approvals()).length).to.equal(1);
},
});
},
Expand Down

0 comments on commit 4a46c92

Please sign in to comment.