Skip to content
This repository was archived by the owner on Nov 9, 2022. It is now read-only.

Commit 990a87d

Browse files
committed
chore: map appeal funding in subgraph
1 parent f74acb7 commit 990a87d

File tree

3 files changed

+95
-24
lines changed

3 files changed

+95
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
- chore: generate first changelog ([2e41de7](https://github.com/kleros/governor-web/commit/2e41de7))
55
- chore: map governance functions in subgraph ([360d007](https://github.com/kleros/governor-web/commit/360d007))
66
- chore: map list submissions in subgraph ([8cbbe41](https://github.com/kleros/governor-web/commit/8cbbe41))
7+
- chore: map the withdrawal and execution of submissions in subgraph ([f74acb7](https://github.com/kleros/governor-web/commit/f74acb7))
78
- chore: set up project and architecture ([d28fb74](https://github.com/kleros/governor-web/commit/d28fb74))

subgraph/src/mapping.ts

Lines changed: 92 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,34 @@ import {
1111
ChangeWinnerMultiplierCall,
1212
ChangeWithdrawTimeoutCall,
1313
ExecuteSubmissionsCall,
14+
FundAppealCall,
1415
Governor,
1516
SetMetaEvidenceCall,
1617
SubmitListCall,
1718
WithdrawTransactionListCall,
1819
} from "../generated/Governor/Governor";
1920
import {
2021
Contract,
22+
Contribution,
2123
MetaEvidence,
2224
Round,
2325
Session,
2426
Submission,
2527
Transaction,
2628
} from "../generated/schema";
2729

28-
function getStatus(status: number): string {
29-
if (status == 0) return "NoDispute";
30-
if (status == 1) return "DisputeCreated";
31-
if (status == 2) return "Resolved";
32-
return "Error";
33-
}
34-
3530
function concatByteArrays(a: ByteArray, b: ByteArray): ByteArray {
3631
let out = new Uint8Array(a.length + b.length);
3732
for (let i = 0; i < a.length; i++) out[i] = a[i];
3833
for (let j = 0; j < b.length; j++) out[a.length + j] = b[j];
3934
return out as ByteArray;
4035
}
4136

42-
function newSession(contract: Contract, creationTime: BigInt): Session {
43-
let session = new Session(contract.sessionsLength.toHexString());
44-
session.creationTime = creationTime;
45-
session.sumDeposit = BigInt.fromI32(0);
46-
session.status = getStatus(0);
47-
session.durationOffset = BigInt.fromI32(0);
48-
session.submissionsLength = BigInt.fromI32(0);
49-
session.roundsLength = BigInt.fromI32(0);
50-
session.save();
51-
52-
contract.sessionsLength.plus(BigInt.fromI32(1));
53-
contract.save();
54-
55-
// Something is broken with The Graph's null type guards so we need these explicit casts in some places.
56-
return session as Session;
37+
function getStatus(status: number): string {
38+
if (status == 0) return "NoDispute";
39+
if (status == 1) return "DisputeCreated";
40+
if (status == 2) return "Resolved";
41+
return "Error";
5742
}
5843

5944
function initializeContract(
@@ -89,12 +74,29 @@ function initializeContract(
8974
return contract as Contract;
9075
}
9176

77+
function newSession(contract: Contract, creationTime: BigInt): Session {
78+
let session = new Session(contract.sessionsLength.toHexString());
79+
session.creationTime = creationTime;
80+
session.sumDeposit = BigInt.fromI32(0);
81+
session.status = getStatus(0);
82+
session.durationOffset = BigInt.fromI32(0);
83+
session.submissionsLength = BigInt.fromI32(0);
84+
session.roundsLength = BigInt.fromI32(0);
85+
session.save();
86+
87+
contract.sessionsLength.plus(BigInt.fromI32(1));
88+
contract.save();
89+
90+
// Something is broken with The Graph's null type guards so we need these explicit casts in some places.
91+
return session as Session;
92+
}
93+
9294
function newRound(session: Session, creationTime: BigInt): Round {
9395
let round = new Round(
9496
crypto
9597
.keccak256(
9698
concatByteArrays(
97-
ByteArray.fromUTF8(session.id),
99+
ByteArray.fromHexString(session.id),
98100
ByteArray.fromUTF8(session.roundsLength.toString())
99101
)
100102
)
@@ -115,6 +117,49 @@ function newRound(session: Session, creationTime: BigInt): Round {
115117
return round as Round;
116118
}
117119

120+
function updateContribution(
121+
governor: Governor,
122+
sessionID: BigInt,
123+
roundIndex: BigInt,
124+
contributor: Address,
125+
time: BigInt
126+
): Contribution {
127+
let roundInfo = governor.getRoundInfo(sessionID, roundIndex);
128+
let contributions = governor.getContributions(
129+
sessionID,
130+
roundIndex,
131+
contributor
132+
);
133+
134+
let roundID = crypto.keccak256(
135+
concatByteArrays(
136+
ByteArray.fromUTF8(sessionID.toString()),
137+
ByteArray.fromUTF8(roundIndex.toString())
138+
)
139+
);
140+
let round = Round.load(roundID.toHexString());
141+
round.paidFees = roundInfo.value0;
142+
round.hasPaid = roundInfo.value1;
143+
round.feeRewards = roundInfo.value2;
144+
round.successfullyPaid = roundInfo.value3;
145+
round.save();
146+
147+
let contributionID = crypto
148+
.keccak256(concatByteArrays(roundID, contributor))
149+
.toHexString();
150+
let contribution = Contribution.load(contributionID);
151+
if (contribution == null) {
152+
contribution = new Contribution(contributionID);
153+
contribution.creationTime = time;
154+
contribution.round = round.id;
155+
contribution.contributor = contributor;
156+
}
157+
contribution.values = contributions;
158+
contribution.save();
159+
160+
return contribution as Contribution;
161+
}
162+
118163
export function setMetaEvidence(call: SetMetaEvidenceCall): void {
119164
let contract = initializeContract(call.to, call.from, call.block.timestamp);
120165

@@ -215,7 +260,7 @@ export function submitList(call: SubmitListCall): void {
215260
for (let i = 0; i < call.inputs._target.length; i++) {
216261
let transaction = new Transaction(
217262
concatByteArrays(
218-
crypto.keccak256(ByteArray.fromUTF8(submission.id)),
263+
crypto.keccak256(ByteArray.fromHexString(submission.id)),
219264
ByteArray.fromUTF8(i.toString())
220265
).toHexString()
221266
);
@@ -309,3 +354,26 @@ export function executeSubmissions(call: ExecuteSubmissionsCall): void {
309354
newRound(session as Session, call.block.timestamp);
310355
}
311356
}
357+
358+
export function fundAppeal(call: FundAppealCall): void {
359+
let governor = Governor.bind(call.to);
360+
let contract = initializeContract(call.to, call.from, call.block.timestamp);
361+
362+
let sessionID = contract.sessionsLength.minus(BigInt.fromI32(1));
363+
let session = Session.load(sessionID.toHexString());
364+
updateContribution(
365+
governor,
366+
sessionID,
367+
session.roundsLength.minus(BigInt.fromI32(1)),
368+
call.from,
369+
call.block.timestamp
370+
);
371+
372+
contract.reservedETH = governor.reservedETH();
373+
contract.shadowWinner = governor.shadowWinner().toHexString();
374+
contract.save();
375+
376+
let sessionRoundsNumber = governor.getSessionRoundsNumber(sessionID);
377+
if (sessionRoundsNumber.gt(session.roundsLength))
378+
newRound(session as Session, call.block.timestamp);
379+
}

subgraph/subgraph.template.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ dataSources:
4545
handler: withdrawTransactionList
4646
- function: executeSubmissions()
4747
handler: executeSubmissions
48+
- function: fundAppeal(uint256)
49+
handler: fundAppeal
4850
file: ./src/mapping.ts

0 commit comments

Comments
 (0)