1
1
import { BigInt } from "@graphprotocol/graph-ts" ;
2
2
import { Contribution } from "../../generated/DisputeKitClassic/DisputeKitClassic" ;
3
- import { ClassicRound } from "../../generated/schema" ;
4
- import { ONE , ZERO } from "../utils" ;
3
+ import { Answer , ClassicRound } from "../../generated/schema" ;
4
+ import { ZERO } from "../utils" ;
5
5
6
6
export function createClassicRound ( disputeID : string , numberOfChoices : BigInt , roundIndex : BigInt ) : void {
7
- const choicesLength = numberOfChoices . plus ( ONE ) ;
8
7
const localDisputeID = `1-${ disputeID } ` ;
9
8
const id = `${ localDisputeID } -${ roundIndex . toString ( ) } ` ;
10
9
const classicRound = new ClassicRound ( id ) ;
11
10
classicRound . localDispute = localDisputeID ;
12
11
classicRound . winningChoice = ZERO ;
13
- classicRound . counts = new Array < BigInt > ( choicesLength . toI32 ( ) ) . fill ( ZERO ) ;
14
12
classicRound . tied = true ;
15
13
classicRound . totalVoted = ZERO ;
16
14
classicRound . totalCommited = ZERO ;
17
- classicRound . paidFees = new Array < BigInt > ( choicesLength . toI32 ( ) ) . fill ( ZERO ) ;
18
15
classicRound . feeRewards = ZERO ;
19
16
classicRound . appealFeesDispersed = false ;
20
17
classicRound . totalFeeDispersed = ZERO ;
@@ -27,21 +24,31 @@ class CurrentRulingInfo {
27
24
tied : boolean ;
28
25
}
29
26
27
+ export function ensureAnswer ( localRoundId : string , answerId : BigInt ) : Answer {
28
+ const id = `${ localRoundId } -${ answerId } ` ;
29
+ let answer = Answer . load ( id ) ;
30
+ if ( answer ) return answer ;
31
+ answer = new Answer ( id ) ;
32
+ answer . answerId = answerId ;
33
+ answer . count = ZERO ;
34
+ answer . paidFee = ZERO ;
35
+ answer . funded = false ;
36
+ answer . localRound = localRoundId ;
37
+ return answer ;
38
+ }
39
+
30
40
export function updateCountsAndGetCurrentRuling ( id : string , choice : BigInt , delta : BigInt ) : CurrentRulingInfo {
31
41
const round = ClassicRound . load ( id ) ;
32
42
if ( ! round ) return { ruling : ZERO , tied : false } ;
33
- const choiceNum = choice . toI32 ( ) ;
34
- const newChoiceCount = round . counts [ choiceNum ] . plus ( delta ) ;
35
- let newCounts : BigInt [ ] = [ ] ;
36
- for ( let i = 0 ; i < round . counts . length ; i ++ ) {
37
- if ( BigInt . fromI32 ( i ) . equals ( choice ) ) {
38
- newCounts . push ( newChoiceCount ) ;
39
- } else {
40
- newCounts . push ( round . counts [ i ] ) ;
41
- }
42
- }
43
- round . counts = newCounts ;
44
- const currentWinningCount = round . counts [ round . winningChoice . toI32 ( ) ] ;
43
+ const answer = ensureAnswer ( id , choice ) ;
44
+
45
+ answer . count = answer . count . plus ( delta ) ;
46
+
47
+ const newChoiceCount = answer . count ;
48
+
49
+ const winningAnswer = ensureAnswer ( id , round . winningChoice ) ;
50
+ const currentWinningCount = winningAnswer . count ;
51
+
45
52
if ( choice . equals ( round . winningChoice ) ) {
46
53
if ( round . tied ) round . tied = false ;
47
54
} else {
@@ -53,6 +60,8 @@ export function updateCountsAndGetCurrentRuling(id: string, choice: BigInt, delt
53
60
}
54
61
}
55
62
round . totalVoted = round . totalVoted . plus ( delta ) ;
63
+
64
+ answer . save ( ) ;
56
65
round . save ( ) ;
57
66
return { ruling : round . winningChoice , tied : round . tied } ;
58
67
}
@@ -68,15 +77,9 @@ export function updateChoiceFundingFromContributionEvent(event: Contribution): v
68
77
69
78
const choice = event . params . _choice ;
70
79
const amount = event . params . _amount ;
71
- const currentPaidFees = classicRound . paidFees [ choice . toI32 ( ) ] ;
72
- let newPaidFees : BigInt [ ] = [ ] ;
73
- for ( let i = 0 ; i < classicRound . paidFees . length ; i ++ ) {
74
- if ( BigInt . fromI32 ( i ) . equals ( choice ) ) {
75
- newPaidFees . push ( currentPaidFees . plus ( amount ) ) ;
76
- } else {
77
- newPaidFees . push ( classicRound . paidFees [ i ] ) ;
78
- }
79
- }
80
- classicRound . paidFees = newPaidFees ;
80
+ const answer = ensureAnswer ( roundID , choice ) ;
81
+ answer . paidFee = answer . paidFee . plus ( amount ) ;
82
+
83
+ answer . save ( ) ;
81
84
classicRound . save ( ) ;
82
85
}
0 commit comments