Skip to content

Commit d730ac1

Browse files
committed
Fix quadratic totals per round
1 parent 1bf8f6a commit d730ac1

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

src/XAllocationVoting.ts

+36-17
Original file line numberDiff line numberDiff line change
@@ -22,55 +22,74 @@ export function handleVoteCast(event: AllocationVoteCastEvent): void {
2222
veDelegateStats.voters = veDelegateStats.voters.plus(constants.BIGINT_ONE)
2323
}
2424

25+
let totalQFVotesAdjustment = constants.BIGINT_ZERO
26+
let totalQFVotesAdjustmentVD = constants.BIGINT_ZERO
2527
for (let index = 0; index < appCount; index += 1) {
2628
const app = event.params.appsIds[index]
2729
const appId = app.toHexString()
2830
const id = (event.block.number.toI64() * 10000000) + (event.transaction.index.toI64() * 10000) + (event.transactionLogIndex.toI64() * 100) + index
2931
const vote = new AllocationVote(id.toString())
3032
const votesCast = event.params.voteWeights[index]
31-
const qfWeight = votesCast.sqrt()
33+
const newQFVotes = votesCast.gt(bigInt.fromString("1000000000000000000")) ? votesCast.sqrt() : votesCast.div(bigInt.fromString("1000000000"))
3234
vote.voter = fetchAccount(event.params.voter).id
3335
vote.round = roundId
3436
vote.app = app;
35-
vote.weight = decimals.toDecimals(votesCast, 18)
3637
vote.weightExact = votesCast
38+
vote.weight = decimals.toDecimals(vote.weightExact, 18)
3739

38-
vote.qfWeightExact = qfWeight
39-
vote.qfWeight = decimals.toDecimals(qfWeight, 18)
40-
vote.timestamp = event.block.timestamp.toI64()
41-
vote.transaction = transactions.log(event).id
42-
vote.save()
43-
40+
// Get the current sum of the square roots of individual votes for the given project
4441
const allocation = AllocationResult.load([roundId, appId].join('/'))!
45-
allocation.voters = allocation.voters.plus(constants.BIGINT_ONE)
42+
const qfAppVotesPreVote = allocation.weightExact
43+
44+
// Calculate the new sum of the square roots of individual votes for the given project
45+
const qfAppVotesPostVote = qfAppVotesPreVote.plus(newQFVotes)
46+
47+
// Calculate the adjustment to the quadratic funding value for the given app
48+
totalQFVotesAdjustment = totalQFVotesAdjustment.plus(qfAppVotesPostVote.times(qfAppVotesPostVote).minus(qfAppVotesPreVote.times(qfAppVotesPreVote)))
49+
50+
// Update the quadratic funding votes received for the given app
51+
allocation.weightExact = qfAppVotesPostVote
52+
allocation.weight = decimals.toDecimals(qfAppVotesPostVote, 9)
4653
allocation.votesCastExact = allocation.votesCastExact.plus(votesCast)
4754
allocation.votesCast = decimals.toDecimals(allocation.votesCastExact, 18)
48-
allocation.weightExact = allocation.weightExact.plus(qfWeight)
49-
allocation.weight = decimals.toDecimals(allocation.weightExact, 18)
55+
allocation.voters = allocation.voters.plus(constants.BIGINT_ONE)
5056
allocation.save()
5157

58+
vote.qfWeightExact = newQFVotes
59+
vote.qfWeight = decimals.toDecimals(newQFVotes, 9)
60+
vote.timestamp = event.block.timestamp.toI64()
61+
vote.transaction = transactions.log(event).id
62+
vote.save()
63+
5264
stats.votesCastExact = stats.votesCastExact.plus(votesCast)
5365
stats.votesCast = decimals.toDecimals(stats.votesCastExact, 18)
54-
stats.weightExact = stats.weightExact.plus(qfWeight)
55-
stats.weight = decimals.toDecimals(stats.weightExact, 18)
5666

5767
if (veAccount != null) {
5868
const veDelegateAllocation = AllocationResult.load([roundId, appId, 'vedelegate'].join('/'))!
5969
veDelegateAllocation.voters = veDelegateAllocation.voters.plus(constants.BIGINT_ONE)
60-
veDelegateAllocation.weightExact = veDelegateAllocation.weightExact.plus(qfWeight)
61-
veDelegateAllocation.weight = decimals.toDecimals(veDelegateAllocation.weightExact, 18)
70+
veDelegateAllocation.weightExact = veDelegateAllocation.weightExact.plus(newQFVotes)
71+
veDelegateAllocation.weight = decimals.toDecimals(veDelegateAllocation.weightExact, 9)
6272
veDelegateAllocation.votesCastExact = veDelegateAllocation.votesCastExact.plus(votesCast)
6373
veDelegateAllocation.votesCast = decimals.toDecimals(veDelegateAllocation.votesCastExact, 18)
6474
veDelegateAllocation.save()
6575

6676
veDelegateStats.votesCastExact = veDelegateStats.votesCastExact.plus(votesCast)
6777
veDelegateStats.votesCast = decimals.toDecimals(veDelegateStats.votesCastExact, 18)
6878

69-
veDelegateStats.weightExact = veDelegateStats.weightExact.plus(qfWeight)
70-
veDelegateStats.weight = decimals.toDecimals(veDelegateStats.weightExact, 18)
79+
// Calculate the adjustment to the quadratic funding value for the given app for veDelegate
80+
totalQFVotesAdjustmentVD = totalQFVotesAdjustmentVD.plus(qfAppVotesPostVote.times(qfAppVotesPostVote).minus(qfAppVotesPreVote.times(qfAppVotesPreVote)))
7181
}
7282
}
83+
84+
stats.weightExact = stats.weightExact.plus(totalQFVotesAdjustment)
85+
stats.weight = decimals.toDecimals(stats.weightExact, 18)
7386
stats.save()
87+
88+
if (veAccount) {
89+
veDelegateStats.weightExact = veDelegateStats.weightExact.plus(totalQFVotesAdjustmentVD)
90+
veDelegateStats.weight = decimals.toDecimals(veDelegateStats.weightExact, 18)
91+
}
92+
7493
veDelegateStats.save()
7594
}
7695

0 commit comments

Comments
 (0)