11import { BigInt } from "@graphprotocol/graph-ts" ;
22import { 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" ;
55
66export function createClassicRound ( disputeID : string , numberOfChoices : BigInt , roundIndex : BigInt ) : void {
7- const choicesLength = numberOfChoices . plus ( ONE ) ;
87 const localDisputeID = `1-${ disputeID } ` ;
98 const id = `${ localDisputeID } -${ roundIndex . toString ( ) } ` ;
109 const classicRound = new ClassicRound ( id ) ;
1110 classicRound . localDispute = localDisputeID ;
1211 classicRound . winningChoice = ZERO ;
13- classicRound . counts = new Array < BigInt > ( choicesLength . toI32 ( ) ) . fill ( ZERO ) ;
1412 classicRound . tied = true ;
1513 classicRound . totalVoted = ZERO ;
1614 classicRound . totalCommited = ZERO ;
17- classicRound . paidFees = new Array < BigInt > ( choicesLength . toI32 ( ) ) . fill ( ZERO ) ;
1815 classicRound . feeRewards = ZERO ;
1916 classicRound . appealFeesDispersed = false ;
2017 classicRound . totalFeeDispersed = ZERO ;
@@ -27,21 +24,31 @@ class CurrentRulingInfo {
2724 tied : boolean ;
2825}
2926
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+
3040export function updateCountsAndGetCurrentRuling ( id : string , choice : BigInt , delta : BigInt ) : CurrentRulingInfo {
3141 const round = ClassicRound . load ( id ) ;
3242 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+
4552 if ( choice . equals ( round . winningChoice ) ) {
4653 if ( round . tied ) round . tied = false ;
4754 } else {
@@ -53,6 +60,8 @@ export function updateCountsAndGetCurrentRuling(id: string, choice: BigInt, delt
5360 }
5461 }
5562 round . totalVoted = round . totalVoted . plus ( delta ) ;
63+
64+ answer . save ( ) ;
5665 round . save ( ) ;
5766 return { ruling : round . winningChoice , tied : round . tied } ;
5867}
@@ -68,15 +77,9 @@ export function updateChoiceFundingFromContributionEvent(event: Contribution): v
6877
6978 const choice = event . params . _choice ;
7079 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 ( ) ;
8184 classicRound . save ( ) ;
8285}
0 commit comments