From 806837549e891b770e6dc5cdda45fd7d7a3f2dbc Mon Sep 17 00:00:00 2001 From: Jason Schrader Date: Wed, 15 Jan 2025 00:03:14 -0700 Subject: [PATCH] refactor: generalize voter funding with other proposal steps --- .../actions/aibtc-action-send-message.test.ts | 34 ++---------------- .../aibtc-onchain-messaging.test.ts | 35 ++----------------- tests/test-utilities.ts | 17 ++++++++- 3 files changed, 20 insertions(+), 66 deletions(-) diff --git a/tests/dao/extensions/actions/aibtc-action-send-message.test.ts b/tests/dao/extensions/actions/aibtc-action-send-message.test.ts index e4919af..920c501 100644 --- a/tests/dao/extensions/actions/aibtc-action-send-message.test.ts +++ b/tests/dao/extensions/actions/aibtc-action-send-message.test.ts @@ -2,6 +2,7 @@ import { Cl, cvToValue } from "@stacks/transactions"; import { describe, expect, it } from "vitest"; import { constructDao, + fundVoters, getDaoTokens, passActionProposal, } from "../../../test-utilities"; @@ -40,38 +41,7 @@ describe(`action extension: ${contractName}`, () => { 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, 100000000), // 100 STX - getDaoTokens(deployer, address1, 50000000), // 50 STX - getDaoTokens(deployer, address2, 25000000), // 25 STX - ]; - const getAddressBalances = [ - simnet.callReadOnlyFn( - `${deployer}.aibtc-token`, - "get-balance", - [Cl.principal(deployer)], - deployer - ), - simnet.callReadOnlyFn( - `${deployer}.aibtc-token`, - "get-balance", - [Cl.principal(address1)], - deployer - ), - simnet.callReadOnlyFn( - `${deployer}.aibtc-token`, - "get-balance", - [Cl.principal(address2)], - deployer - ), - ]; - for (let i = 0; i < getDaoTokensReceipts.length; i++) { - const expectedBalance = parseInt( - cvToValue(getAddressBalances[i].result).value - ); - // console.log(`expectedBalance: ${expectedBalance}`); - expect(getDaoTokensReceipts[i].result).toBeOk(Cl.uint(expectedBalance)); - } + fundVoters(deployer, [deployer, address1, address2]); // construct DAO const constructReceipt = constructDao(deployer); diff --git a/tests/dao/extensions/aibtc-onchain-messaging.test.ts b/tests/dao/extensions/aibtc-onchain-messaging.test.ts index 45040f8..2d6c38f 100644 --- a/tests/dao/extensions/aibtc-onchain-messaging.test.ts +++ b/tests/dao/extensions/aibtc-onchain-messaging.test.ts @@ -2,6 +2,7 @@ import { Cl, cvToValue } from "@stacks/transactions"; import { describe, expect, it } from "vitest"; import { constructDao, + fundVoters, getDaoTokens, passCoreProposal, } from "../../test-utilities"; @@ -55,39 +56,7 @@ describe(`extension: ${contractName}`, () => { const proposalContractAddress = `${deployer}.${proposalContractName}`; // fund accounts for creating and voting on proposals - // TODO: consolidate as fundVoters() - const getDaoTokensReceipts = [ - getDaoTokens(deployer, deployer, 100000000), // 100 STX - getDaoTokens(deployer, address1, 50000000), // 50 STX - getDaoTokens(deployer, address2, 25000000), // 25 STX - ]; - const getAddressBalances = [ - simnet.callReadOnlyFn( - `${deployer}.aibtc-token`, - "get-balance", - [Cl.principal(deployer)], - deployer - ), - simnet.callReadOnlyFn( - `${deployer}.aibtc-token`, - "get-balance", - [Cl.principal(address1)], - deployer - ), - simnet.callReadOnlyFn( - `${deployer}.aibtc-token`, - "get-balance", - [Cl.principal(address2)], - deployer - ), - ]; - for (let i = 0; i < getDaoTokensReceipts.length; i++) { - const expectedBalance = parseInt( - cvToValue(getAddressBalances[i].result).value - ); - // console.log(`expectedBalance: ${expectedBalance}`); - expect(getDaoTokensReceipts[i].result).toBeOk(Cl.uint(expectedBalance)); - } + fundVoters(deployer, [deployer, address1, address2]); // construct DAO const constructReceipt = constructDao(deployer); diff --git a/tests/test-utilities.ts b/tests/test-utilities.ts index be45c68..8a9e5b6 100644 --- a/tests/test-utilities.ts +++ b/tests/test-utilities.ts @@ -1,4 +1,4 @@ -import { Cl, ClarityValue } from "@stacks/transactions"; +import { Cl, ClarityValue, cvToValue } from "@stacks/transactions"; import { expect } from "vitest"; export const actionProposalsContractName = "aibtc-action-proposals"; @@ -11,6 +11,21 @@ function getPercentageOfSupply(amount: number, totalSupply: number) { return percentage; } +export function fundVoters(deployer: string, voters: string[]) { + for (const voter of voters) { + const stxAmount = Math.floor(Math.random() * 500000000) + 1000000; + const getDaoTokensReceipt = getDaoTokens(deployer, voter, stxAmount); + const getAddressBalanceResult = simnet.callReadOnlyFn( + `${deployer}.aibtc-token`, + "get-balance", + [Cl.principal(voter)], + deployer + ).result; + const expectedBalance = parseInt(cvToValue(getAddressBalanceResult).value); + expect(getDaoTokensReceipt.result).toBeOk(Cl.uint(expectedBalance)); + } +} + export function getDaoTokens( deployer: string, address: string,