@@ -22,55 +22,74 @@ export function handleVoteCast(event: AllocationVoteCastEvent): void {
22
22
veDelegateStats . voters = veDelegateStats . voters . plus ( constants . BIGINT_ONE )
23
23
}
24
24
25
+ let totalQFVotesAdjustment = constants . BIGINT_ZERO
26
+ let totalQFVotesAdjustmentVD = constants . BIGINT_ZERO
25
27
for ( let index = 0 ; index < appCount ; index += 1 ) {
26
28
const app = event . params . appsIds [ index ]
27
29
const appId = app . toHexString ( )
28
30
const id = ( event . block . number . toI64 ( ) * 10000000 ) + ( event . transaction . index . toI64 ( ) * 10000 ) + ( event . transactionLogIndex . toI64 ( ) * 100 ) + index
29
31
const vote = new AllocationVote ( id . toString ( ) )
30
32
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" ) )
32
34
vote . voter = fetchAccount ( event . params . voter ) . id
33
35
vote . round = roundId
34
36
vote . app = app ;
35
- vote . weight = decimals . toDecimals ( votesCast , 18 )
36
37
vote . weightExact = votesCast
38
+ vote . weight = decimals . toDecimals ( vote . weightExact , 18 )
37
39
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
44
41
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 )
46
53
allocation . votesCastExact = allocation . votesCastExact . plus ( votesCast )
47
54
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 )
50
56
allocation . save ( )
51
57
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
+
52
64
stats . votesCastExact = stats . votesCastExact . plus ( votesCast )
53
65
stats . votesCast = decimals . toDecimals ( stats . votesCastExact , 18 )
54
- stats . weightExact = stats . weightExact . plus ( qfWeight )
55
- stats . weight = decimals . toDecimals ( stats . weightExact , 18 )
56
66
57
67
if ( veAccount != null ) {
58
68
const veDelegateAllocation = AllocationResult . load ( [ roundId , appId , 'vedelegate' ] . join ( '/' ) ) !
59
69
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 )
62
72
veDelegateAllocation . votesCastExact = veDelegateAllocation . votesCastExact . plus ( votesCast )
63
73
veDelegateAllocation . votesCast = decimals . toDecimals ( veDelegateAllocation . votesCastExact , 18 )
64
74
veDelegateAllocation . save ( )
65
75
66
76
veDelegateStats . votesCastExact = veDelegateStats . votesCastExact . plus ( votesCast )
67
77
veDelegateStats . votesCast = decimals . toDecimals ( veDelegateStats . votesCastExact , 18 )
68
78
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 ) ) )
71
81
}
72
82
}
83
+
84
+ stats . weightExact = stats . weightExact . plus ( totalQFVotesAdjustment )
85
+ stats . weight = decimals . toDecimals ( stats . weightExact , 18 )
73
86
stats . save ( )
87
+
88
+ if ( veAccount ) {
89
+ veDelegateStats . weightExact = veDelegateStats . weightExact . plus ( totalQFVotesAdjustmentVD )
90
+ veDelegateStats . weight = decimals . toDecimals ( veDelegateStats . weightExact , 18 )
91
+ }
92
+
74
93
veDelegateStats . save ( )
75
94
}
76
95
0 commit comments