Skip to content

Commit

Permalink
remove screeningStageVotingPower; rename fundingStage votingPower att…
Browse files Browse the repository at this point in the history
…ributes
  • Loading branch information
Mike committed Jul 27, 2023
1 parent fdf5c6e commit f46a9eb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 43 deletions.
5 changes: 2 additions & 3 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,8 @@ type DistributionPeriodVote @entity {
id: Bytes! # $accountId + '|'' + $distributionId
voter: Account!
distribution: DistributionPeriod! # uint256
screeningStageVotingPower: BigDecimal! # uint256
initialFundingStageVotingPower: BigDecimal! # uint256
remainingFundingStageVotingPower: BigDecimal! # uint256
estimatedInitialFundingStageVotingPowerForCalculatingRewards: BigDecimal! # uint256
estimatedRemainingFundingStageVotingPowerForCalculatingRewards: BigDecimal! # uint256
screeningVotes: [ScreeningVote!]! # ScreeningVote[]
fundingVotes: [FundingVote!]! # FundingVote[]
}
Expand Down
29 changes: 4 additions & 25 deletions src/grant-fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,6 @@ export function handleDistributionPeriodStarted(
distributionPeriod.startBlock = distributionStarted.startBlock
distributionPeriod.endBlock = distributionStarted.endBlock

// loop through DistributionPeriodVotes of the previous period, create new entities, and set their screening stage voting power
if (event.params.distributionId != ONE_BI) {
const prevDistributionPeriod = loadOrCreateDistributionPeriod(distributionId)
const votes = prevDistributionPeriod.votes
for (var i=0; i<votes.length; ++i) {
const prevVote = DistributionPeriodVote.load(votes[i])!
const prevVoter = prevVote.voter
const newDistributionPeriodVote = loadOrCreateDistributionPeriodVote(distributionPeriod.id, prevVoter)
newDistributionPeriodVote.screeningStageVotingPower = getScreeningStageVotingPower(event.address, bytesToBigInt(distributionId), Address.fromBytes(prevVoter))
newDistributionPeriodVote.save()
}
}
else {
distributionPeriod.votes = []
}

// update GrantFund entity
const grantFund = loadOrCreateGrantFund(event.address)
const treasury = getTreasury(event.address)
Expand Down Expand Up @@ -336,11 +320,6 @@ export function handleVoteCast(event: VoteCastEvent): void {
screeningVote.votesCast = screeningVotesCast
screeningVote.blockNumber = voteCast.blockNumber

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

// add additional screening votes to voter's distributionPeriodVote entity
distributionPeriodVote.screeningVotes = distributionPeriodVote.screeningVotes.concat([screeningVote.id])

Expand Down Expand Up @@ -368,12 +347,12 @@ export function handleVoteCast(event: VoteCastEvent): void {
distributionPeriod.fundingVotePowerUsed = distributionPeriod.fundingVotePowerUsed.plus(fundingVote.votingPowerUsed)

// 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)
if (distributionPeriodVote.estimatedInitialFundingStageVotingPowerForCalculatingRewards.equals(ZERO_BD)) {
distributionPeriodVote.estimatedInitialFundingStageVotingPowerForCalculatingRewards = getFundingStageVotingPower(event.address, bytesToBigInt(distributionId), Address.fromBytes(voter.id))
distributionPeriodVote.estimatedRemainingFundingStageVotingPowerForCalculatingRewards = distributionPeriodVote.estimatedInitialFundingStageVotingPowerForCalculatingRewards.minus(fundingVote.votingPowerUsed)
}
else {
distributionPeriodVote.remainingFundingStageVotingPower = getFundingStageVotingPower(event.address, bytesToBigInt(distributionId), Address.fromBytes(voter.id))
distributionPeriodVote.estimatedRemainingFundingStageVotingPowerForCalculatingRewards = getFundingStageVotingPower(event.address, bytesToBigInt(distributionId), Address.fromBytes(voter.id))
}

// save fundingVote to the store
Expand Down
5 changes: 2 additions & 3 deletions src/utils/grants/voter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ export function loadOrCreateDistributionPeriodVote(distributionPeriodId: Bytes,
distributionPeriodVotes = new DistributionPeriodVote(distributionPeriodVotesId) as DistributionPeriodVote
distributionPeriodVotes.voter = voterId
distributionPeriodVotes.distribution = distributionPeriodId
distributionPeriodVotes.screeningStageVotingPower = ZERO_BD
distributionPeriodVotes.initialFundingStageVotingPower = ZERO_BD
distributionPeriodVotes.remainingFundingStageVotingPower = ZERO_BD
distributionPeriodVotes.estimatedInitialFundingStageVotingPowerForCalculatingRewards = ZERO_BD
distributionPeriodVotes.estimatedRemainingFundingStageVotingPowerForCalculatingRewards = ZERO_BD
distributionPeriodVotes.screeningVotes = []
distributionPeriodVotes.fundingVotes = []

Expand Down
30 changes: 18 additions & 12 deletions tests/grant-fund.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
} from "../src/utils/constants";
import { addressToBytes, bigIntToBytes, decimalToWad, wadToDecimal } from "../src/utils/convert";
import { mockGetDistributionId, mockGetTreasury, mockGetVotesFunding, mockGetVotesScreening } from "./utils/common";
import { getDistributionPeriodVoteId, getFundingVoteId } from "../src/utils/grants/voter";
import { getDistributionPeriodVoteId, getFundingVoteId, getScreeningVoteId } from "../src/utils/grants/voter";

// Tests structure (matchstick-as >=0.5.0)
// https://thegraph.com/docs/en/developer/matchstick/#tests-structure-0-5-0
Expand Down Expand Up @@ -395,20 +395,23 @@ describe("Grant Fund assertions", () => {
assert.entityCount("ScreeningVote", 1);

const distributionPeriodVoteId = getDistributionPeriodVoteId(bigIntToBytes(distributionId), addressToBytes(voter));
const screeningVoteId = getScreeningVoteId(bigIntToBytes(proposalId), addressToBytes(voter), BigInt.fromI32(1));

assert.fieldEquals(
"DistributionPeriodVote",
`${distributionPeriodVoteId.toHexString()}`,
"screeningStageVotingPower",
`${wadToDecimal(votesCast)}`
"distribution",
`${bigIntToBytes(distributionId).toHexString()}`
);
});

test("getFundingVotingPowerUsed", () => {

assert.fieldEquals(
"ScreeningVote",
`${screeningVoteId.toHexString()}`,
"votesCast",
`${wadToDecimal(votesCast)}`
);
});


test("FundingVote", () => {
/***********************/
/*** Submit Proposal ***/
Expand Down Expand Up @@ -511,6 +514,7 @@ describe("Grant Fund assertions", () => {

const distributionPeriodVoteId = getDistributionPeriodVoteId(bigIntToBytes(distributionId), addressToBytes(voter));
const fundingVoteId = getFundingVoteId(bigIntToBytes(proposalId), addressToBytes(voter), BigInt.fromI32(2));
const screeningVoteId = getScreeningVoteId(bigIntToBytes(proposalId), addressToBytes(voter), BigInt.fromI32(1));
const expectedDistributionId = bigIntToBytes(distributionId).toHexString();
const expectedVotingPowerUsed = wadToDecimal(votesCast.times(votesCast));

Expand All @@ -521,24 +525,26 @@ describe("Grant Fund assertions", () => {
`${expectedDistributionId}`
);

// access ScreeningVote entity and attributes
assert.fieldEquals(
"DistributionPeriodVote",
`${distributionPeriodVoteId.toHexString()}`,
"screeningStageVotingPower",
"ScreeningVote",
`${screeningVoteId.toHexString()}`,
"votesCast",
`${wadToDecimal(votesCast.times(BigInt.fromI32(-1)))}`
);

// check DistributionPeriodVote attributes
assert.fieldEquals(
"DistributionPeriodVote",
`${distributionPeriodVoteId.toHexString()}`,
"initialFundingStageVotingPower",
"estimatedInitialFundingStageVotingPowerForCalculatingRewards",
`${expectedVotingPowerUsed}`
);

assert.fieldEquals(
"DistributionPeriodVote",
`${distributionPeriodVoteId.toHexString()}`,
"remainingFundingStageVotingPower",
"estimatedRemainingFundingStageVotingPowerForCalculatingRewards",
`${0}`
);

Expand Down

0 comments on commit f46a9eb

Please sign in to comment.