Skip to content

Commit

Permalink
fix fundingVote flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Jul 25, 2023
1 parent 68c8803 commit ee463b5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
18 changes: 10 additions & 8 deletions src/grant-fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,20 +334,22 @@ export function handleVoteCast(event: VoteCastEvent): void {
fundingVote.votesCast = wadToDecimal(event.params.weight)
fundingVote.blockNumber = voteCast.blockNumber

// update voter's distributionPeriodVote entity if it hasn't been recorded yet
if (distributionPeriodVote.initialFundingStageVotingPower === ZERO_BD) {
distributionPeriodVote.initialFundingStageVotingPower = getFundingStageVotingPower(event.address, bytesToBigInt(distributionId), Address.fromBytes(voter.id))
}
else {
distributionPeriodVote.remainingFundingStageVotingPower = getFundingStageVotingPower(event.address, bytesToBigInt(distributionId), Address.fromBytes(voter.id))
}

// add additional funding votes to voter's distributionPeriodVote entity
distributionPeriodVote.fundingVotes = distributionPeriodVote.fundingVotes.concat([fundingVote.id])

// FIXME: this isn't updating as expected
// calculate the voting power cost of this funding vote
fundingVote.votingPowerUsed = getFundingVotingPowerUsed(distributionPeriodVote, proposalId);

// update voter's distributionPeriodVote entity voting power tracking if it hasn't been recorded yet
if (distributionPeriodVote.initialFundingStageVotingPower.equals(ZERO_BD)) {
distributionPeriodVote.initialFundingStageVotingPower = getFundingStageVotingPower(event.address, bytesToBigInt(distributionId), Address.fromBytes(voter.id))
distributionPeriodVote.remainingFundingStageVotingPower = distributionPeriodVote.initialFundingStageVotingPower.minus(fundingVote.votingPowerUsed)
}
else {
distributionPeriodVote.remainingFundingStageVotingPower = getFundingStageVotingPower(event.address, bytesToBigInt(distributionId), Address.fromBytes(voter.id))
}

// save fundingVote to the store
fundingVote.save()
}
Expand Down
5 changes: 3 additions & 2 deletions src/utils/grants/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ export function getCurrentDistributionId(grantFundAddress: Address): BigInt {

// FIXME: update this for the latest version of the contract
export function getCurrentStage(currentBlockNumber: BigInt, distributionPeriod: DistributionPeriod): String {
log.info("getCurrentStage: {} {} {} {}", [currentBlockNumber.toString(), currentBlockNumber.ge(distributionPeriod.startBlock.plus(SCREENING_PERIOD_LENGTH)).toString(), currentBlockNumber.le(distributionPeriod.endBlock.minus(FUNDING_PERIOD_LENGTH.plus(CHALLENGE_PERIOD_LENGTH))).toString(), distributionPeriod.endBlock.minus(FUNDING_PERIOD_LENGTH.plus(CHALLENGE_PERIOD_LENGTH)).toString()])
if (currentBlockNumber.lt(distributionPeriod.startBlock.plus(SCREENING_PERIOD_LENGTH))) {
return "SCREENING"
} else if (
currentBlockNumber.gt(distributionPeriod.startBlock.plus(SCREENING_PERIOD_LENGTH))
currentBlockNumber.ge(distributionPeriod.startBlock.plus(SCREENING_PERIOD_LENGTH))
&&
currentBlockNumber.lt(distributionPeriod.endBlock.minus(FUNDING_PERIOD_LENGTH).minus(CHALLENGE_PERIOD_LENGTH))
currentBlockNumber.le(distributionPeriod.endBlock.minus(CHALLENGE_PERIOD_LENGTH))
) {
return "FUNDING"
} else {
Expand Down
18 changes: 12 additions & 6 deletions tests/grant-fund.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
ZERO_BI,
} from "../src/utils/constants";
import { addressToBytes, bigIntToBytes, wadToDecimal } from "../src/utils/convert";
import { mockGetDistributionId, mockGetVotesScreening } from "./utils/common";
import { mockGetDistributionId, mockGetVotesFunding, mockGetVotesScreening } from "./utils/common";
import { getDistributionPeriodVoteId } from "../src/utils/grants/voter";

// Tests structure (matchstick-as >=0.5.0)
Expand Down Expand Up @@ -365,7 +365,7 @@ describe("Grant Fund assertions", () => {
// mock contract calls
mockGetVotesScreening(grantFundAddress, distributionId, voter, votesCast);

const screeningVoteCastEvent = createVoteCastEvent(voter, proposalId, 1, votesCast, reason, startBlock);
const screeningVoteCastEvent = createVoteCastEvent(voter, proposalId, 1, votesCast, reason, startBlock, BigInt.fromI32(1));
handleVoteCast(screeningVoteCastEvent);

/********************/
Expand Down Expand Up @@ -464,7 +464,7 @@ describe("Grant Fund assertions", () => {
// mock contract calls
mockGetVotesScreening(grantFundAddress, distributionId, voter, votesCast);

const screeningVoteCastEvent = createVoteCastEvent(voter, proposalId, 1, votesCast, reason, startBlock);
const screeningVoteCastEvent = createVoteCastEvent(voter, proposalId, 1, votesCast, reason, startBlock, BigInt.fromI32(1));
handleVoteCast(screeningVoteCastEvent);

// TODO: advance state to funding stage
Expand All @@ -473,8 +473,13 @@ describe("Grant Fund assertions", () => {
/*** Funding Vote Proposal ***/
/*****************************/

// TODO: need to convert back from WAD
const fundingVotingPower = votesCast.times(votesCast);

mockGetVotesFunding(grantFundAddress, distributionId, voter, fundingVotingPower);

votesCast = BigInt.fromI32(-234);
const fundingVoteCastEvent = createVoteCastEvent(voter, proposalId, 0, votesCast, reason, startBlock.plus(SCREENING_PERIOD_LENGTH).plus(BigInt.fromI32(1)));
const fundingVoteCastEvent = createVoteCastEvent(voter, proposalId, 0, votesCast, reason, startBlock.plus(SCREENING_PERIOD_LENGTH).plus(BigInt.fromI32(1)), BigInt.fromI32(2));
handleVoteCast(fundingVoteCastEvent);

/********************/
Expand All @@ -487,8 +492,9 @@ describe("Grant Fund assertions", () => {
// check Proposal attributes
assert.entityCount("Proposal", 1);

// assert.entityCount("VoteCast", 2);
// assert.entityCount("ScreeningVote", 1);
assert.entityCount("VoteCast", 2);
assert.entityCount("FundingVote", 1);
assert.entityCount("ScreeningVote", 1);
assert.entityCount("DistributionPeriodVote", 1);

const distributionPeriodVoteId = getDistributionPeriodVoteId(bigIntToBytes(distributionId), addressToBytes(voter));
Expand Down
11 changes: 11 additions & 0 deletions tests/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,17 @@ export function mockGetVotesScreening(grantFund: Address, distributionId: BigInt
])
}

export function mockGetVotesFunding(grantFund: Address, distributionId: BigInt, voter: Address, expectedVotes: BigInt): void {
createMockedFunction(grantFund, 'getVotesFunding', 'getVotesFunding(uint24,address):(uint256)')
.withArgs([
ethereum.Value.fromUnsignedBigInt(distributionId),
ethereum.Value.fromAddress(voter)
])
.returns([
ethereum.Value.fromUnsignedBigInt(expectedVotes),
])
}

/*******************************/
/*** Position Mock Functions ***/
/*******************************/
Expand Down
5 changes: 4 additions & 1 deletion tests/utils/grant-fund-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ export function createVoteCastEvent(
support: i32,
weight: BigInt,
reason: string,
blockNumber: BigInt
blockNumber: BigInt,
logIndex: BigInt
): VoteCast {
let voteCastEvent = changetype<VoteCast>(newMockEvent())

Expand Down Expand Up @@ -239,7 +240,9 @@ export function createVoteCastEvent(
new ethereum.EventParam("reason", ethereum.Value.fromString(reason))
)

// override event logIndex as well to multiple voting in single test as well as moving between stages
voteCastEvent.block.number = blockNumber
voteCastEvent.logIndex = logIndex

return voteCastEvent
}

0 comments on commit ee463b5

Please sign in to comment.