Skip to content

Commit

Permalink
feat: add utilities and first action proposal test
Browse files Browse the repository at this point in the history
  • Loading branch information
whoabuddy committed Jan 15, 2025
1 parent da183e2 commit 9e181ef
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
26 changes: 16 additions & 10 deletions tests/dao/extensions/actions/aibtc-action-send-message.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Cl, cvToValue } from "@stacks/transactions";
import { describe, expect, it } from "vitest";
import {
actionProposalsContractName,
constructDao,
getDaoTokens,
passActionProposal,
} from "../../../test-utilities";
import { ActionErrCode } from "../../../error-codes";

const accounts = simnet.getAccounts();
const deployer = accounts.get("deployer")!;
const address1 = accounts.get("address1")!;
const address2 = accounts.get("address2")!;
const address1 = accounts.get("wallet_1")!;
const address2 = accounts.get("wallet_2")!;

const contractName = "aibtc-action-send-message";
const contractAddress = `${deployer}.${contractName}`;
Expand All @@ -27,21 +27,23 @@ describe(`action extension: ${contractName}`, () => {
});

it("run() fails if called directly", () => {
const message = "hello world";
const receipt = simnet.callPublicFn(
contractAddress,
"run",
[Cl.principal(deployer)],
[Cl.buffer(Cl.serialize(Cl.stringAscii(message)))],
deployer
);
expect(receipt.result).toBeErr(Cl.uint(0));
expect(receipt.result).toBeErr(Cl.uint(ActionErrCode.ERR_UNAUTHORIZED));
});

it("run() succeeds if called as a DAO action proposal", () => {
const message = "hello world";
// fund accounts for creating and voting on proposals
const getDaoTokensReceipts = [
getDaoTokens(deployer, deployer, 1000000000), // 1000 STX
getDaoTokens(deployer, address1, 500000000), // 500 STX
getDaoTokens(deployer, address2, 250000000), // 250 STX
getDaoTokens(deployer, deployer, 100000000), // 100 STX
getDaoTokens(deployer, address1, 50000000), // 50 STX
getDaoTokens(deployer, address2, 25000000), // 25 STX
];
const getAddressBalances = [
simnet.callReadOnlyFn(
Expand All @@ -67,7 +69,7 @@ describe(`action extension: ${contractName}`, () => {
const expectedBalance = parseInt(
cvToValue(getAddressBalances[i].result).value
);
console.log(`expectedBalance: ${expectedBalance}`);
// console.log(`expectedBalance: ${expectedBalance}`);
expect(getDaoTokensReceipts[i].result).toBeOk(Cl.uint(expectedBalance));
}

Expand All @@ -81,19 +83,22 @@ describe(`action extension: ${contractName}`, () => {
// pass action proposal
const concludeProposalReceipt = passActionProposal(
contractAddress,
Cl.stringAscii(message),
deployer,
deployer,
[deployer, address1, address2]
);

/*
console.log("===========================");
console.log("concludeProposalReceipt");
console.log(concludeProposalReceipt);
console.log("events:");
for (const event of concludeProposalReceipt.events) {
const eventValue = cvToValue(event.data.value!);
// if event value is an object stringify it
console.log(
`- event: ${
`${
typeof eventValue === "object"
? JSON.stringify(eventValue)
: eventValue
Expand All @@ -111,6 +116,7 @@ describe(`action extension: ${contractName}`, () => {
console.log("===========================");
console.log("proposalDetails");
console.log(cvToValue(proposalDetails.result).value);
*/

expect(concludeProposalReceipt.result).toBeOk(Cl.bool(true));
});
Expand Down
11 changes: 7 additions & 4 deletions tests/dao/extensions/aibtc-onchain-messaging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ describe(`extension: ${contractName}`, () => {

// fund accounts for creating and voting on proposals
const getDaoTokensReceipts = [
getDaoTokens(deployer, deployer, 1000000000), // 1000 STX
getDaoTokens(deployer, address1, 500000000), // 500 STX
getDaoTokens(deployer, address2, 250000000), // 250 STX
getDaoTokens(deployer, deployer, 100000000), // 100 STX
getDaoTokens(deployer, address1, 50000000), // 50 STX
getDaoTokens(deployer, address2, 25000000), // 25 STX
];
const getAddressBalances = [
simnet.callReadOnlyFn(
Expand All @@ -84,7 +84,7 @@ describe(`extension: ${contractName}`, () => {
const expectedBalance = parseInt(
cvToValue(getAddressBalances[i].result).value
);
console.log(`expectedBalance: ${expectedBalance}`);
// console.log(`expectedBalance: ${expectedBalance}`);
expect(getDaoTokensReceipts[i].result).toBeOk(Cl.uint(expectedBalance));
}

Expand All @@ -102,6 +102,7 @@ describe(`extension: ${contractName}`, () => {
[deployer, address1, address2]
);

/*
console.log("===========================");
console.log("concludeProposalReceipt");
console.log(concludeProposalReceipt);
Expand All @@ -117,6 +118,7 @@ describe(`extension: ${contractName}`, () => {
);
}
const proposalDetails = simnet.callReadOnlyFn(
`${deployer}.aibtc-core-proposals`,
"get-proposal",
Expand All @@ -127,6 +129,7 @@ describe(`extension: ${contractName}`, () => {
console.log("===========================");
console.log("proposalDetails");
console.log(cvToValue(proposalDetails.result).value);
*/

expect(concludeProposalReceipt.result).toBeOk(Cl.bool(true));
});
Expand Down
5 changes: 5 additions & 0 deletions tests/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export enum ActionProposalsErrCode {
ERR_INVALID_ACTION,
}

export enum ActionErrCode {
ERR_UNAUTHORIZED = 10001,
ERR_INVALID_PARAMS,
}

export enum BankAccountErrCode {
ERR_INVALID = 2000,
ERR_UNAUTHORIZED,
Expand Down
18 changes: 13 additions & 5 deletions tests/test-utilities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cl } from "@stacks/transactions";
import { Cl, ClarityValue } from "@stacks/transactions";
import { expect } from "vitest";

export const actionProposalsContractName = "aibtc-action-proposals";
Expand All @@ -24,7 +24,7 @@ export function getDaoTokens(
const getDaoTokensReceipt = simnet.callPublicFn(
tokenDexContractAddress,
"buy",
[Cl.principal(tokenContractAddress), Cl.uint(stxAmount)], // 1000 STX buy test
[Cl.principal(tokenContractAddress), Cl.uint(stxAmount)],
address
);

Expand Down Expand Up @@ -85,15 +85,21 @@ export function passCoreProposal(

export function passActionProposal(
proposalContractAddress: string,
proposalParams: ClarityValue,
deployer: string,
sender: string,
voters: string[]
) {
// TODO: hardcoded
const proposalId = 1;
// propose-action
const proposeActionReceipt = simnet.callPublicFn(
`${deployer}.${actionProposalsContractName}`,
"propose-action",
[Cl.principal(proposalContractAddress)],
[
Cl.principal(proposalContractAddress),
Cl.buffer(Cl.serialize(proposalParams)),
],
sender
);
expect(proposeActionReceipt.result).toBeOk(Cl.bool(true));
Expand All @@ -102,16 +108,18 @@ export function passActionProposal(
const voteReceipt = simnet.callPublicFn(
`${deployer}.${actionProposalsContractName}`,
"vote-on-proposal",
[Cl.principal(proposalContractAddress), Cl.bool(true)],
[Cl.uint(proposalId), Cl.bool(true)],
voter
);
expect(voteReceipt.result).toBeOk(Cl.bool(true));
}
// progress past the end block
simnet.mineEmptyBlocks(votingPeriod);
// conclude-proposal
const concludeProposalReceipt = simnet.callPublicFn(
`${deployer}.${actionProposalsContractName}`,
"conclude-proposal",
[Cl.principal(proposalContractAddress)],
[Cl.uint(proposalId), Cl.principal(proposalContractAddress)],
deployer
);
// return final receipt for processing
Expand Down

0 comments on commit 9e181ef

Please sign in to comment.