|
| 1 | +import { Address, BigInt } from "@graphprotocol/graph-ts"; |
1 | 2 | import { TokenAndETHShift as TokenAndETHShiftEvent } from "../../generated/KlerosCore/KlerosCore";
|
2 |
| -import { TokenAndETHShift } from "../../generated/schema"; |
| 3 | +import { Court, Dispute, TokenAndETHShift } from "../../generated/schema"; |
| 4 | +import { updatePaidETH, updateRedistributedPNK } from "../datapoint"; |
3 | 5 | import { ZERO } from "../utils";
|
| 6 | +import { convertTokenAmountToEth, updateFeeTokenPaid } from "./FeeToken"; |
4 | 7 | import { resolveUserDispute } from "./User";
|
5 | 8 |
|
6 | 9 | export function updateTokenAndEthShiftFromEvent(event: TokenAndETHShiftEvent): void {
|
7 |
| - const jurorAddress = event.params._account.toHexString(); |
8 |
| - const disputeID = event.params._disputeID.toString(); |
9 |
| - const shiftID = `${jurorAddress}-${disputeID}`; |
10 |
| - const shift = TokenAndETHShift.load(shiftID); |
11 |
| - |
12 |
| - if (!shift) { |
13 |
| - createTokenAndEthShiftFromEvent(event); |
14 |
| - resolveUserDispute(jurorAddress, ZERO, event.params._feeAmount, disputeID); |
15 |
| - return; |
| 10 | + const jurorAddress = event.params._account; |
| 11 | + const disputeID = event.params._disputeID; |
| 12 | + const dispute = Dispute.load(disputeID.toString()); |
| 13 | + if (!dispute) return; |
| 14 | + const court = Court.load(dispute.court); |
| 15 | + if (!court) return; |
| 16 | + const roundIndex = event.params._roundID; |
| 17 | + const feeTokenAddress = event.params._feeToken; |
| 18 | + let shift = ensureTokenAndEthShift(jurorAddress, disputeID, roundIndex, feeTokenAddress); |
| 19 | + const feeAmount = event.params._feeAmount; |
| 20 | + const pnkAmount = event.params._pnkAmount; |
| 21 | + let ethAmount: BigInt; |
| 22 | + if (feeTokenAddress.toHexString() === "0x0000000000000000000000000000000000000000") { |
| 23 | + updateFeeTokenPaid(feeTokenAddress, event.address, feeAmount); |
| 24 | + ethAmount = convertTokenAmountToEth(feeTokenAddress, feeAmount, event.address); |
| 25 | + shift.feeTokenAmount = shift.feeTokenAmount.plus(feeAmount); |
| 26 | + } else { |
| 27 | + ethAmount = feeAmount; |
16 | 28 | }
|
17 |
| - |
18 |
| - shift.tokenAmount = shift.tokenAmount.plus(event.params._pnkAmount); |
19 |
| - const previousFeeAmount = shift.ethAmount; |
20 |
| - const newFeeAmount = shift.ethAmount.plus(event.params._feeAmount); |
21 |
| - shift.ethAmount = newFeeAmount; |
| 29 | + const previousEthAmount = shift.ethAmount; |
| 30 | + const newEthAmount = previousEthAmount.plus(ethAmount); |
| 31 | + shift.ethAmount = newEthAmount; |
| 32 | + resolveUserDispute(jurorAddress.toHexString(), previousEthAmount, newEthAmount, disputeID.toString()); |
| 33 | + court.paidETH = court.paidETH.plus(ethAmount); |
| 34 | + updatePaidETH(ethAmount, event.block.timestamp); |
| 35 | + if (pnkAmount.gt(ZERO)) { |
| 36 | + court.paidPNK = court.paidPNK.plus(pnkAmount); |
| 37 | + updateRedistributedPNK(pnkAmount, event.block.timestamp); |
| 38 | + } |
| 39 | + shift.pnkAmount = shift.pnkAmount.plus(pnkAmount); |
22 | 40 | shift.save();
|
23 |
| - resolveUserDispute(jurorAddress, previousFeeAmount, newFeeAmount, disputeID); |
| 41 | + court.save(); |
24 | 42 | }
|
25 | 43 |
|
26 |
| -export function createTokenAndEthShiftFromEvent(event: TokenAndETHShiftEvent): void { |
27 |
| - const jurorAddress = event.params._account.toHexString(); |
28 |
| - const disputeID = event.params._disputeID.toString(); |
29 |
| - const shiftID = `${jurorAddress}-${disputeID}`; |
30 |
| - const shift = new TokenAndETHShift(shiftID); |
31 |
| - shift.juror = jurorAddress; |
32 |
| - shift.dispute = disputeID; |
33 |
| - shift.tokenAmount = event.params._pnkAmount; |
34 |
| - shift.ethAmount = event.params._feeAmount; |
35 |
| - shift.save(); |
| 44 | +export function ensureTokenAndEthShift( |
| 45 | + jurorAddress: Address, |
| 46 | + disputeID: BigInt, |
| 47 | + roundIndex: BigInt, |
| 48 | + feeTokenAddress: Address |
| 49 | +): TokenAndETHShift { |
| 50 | + const shiftID = `${jurorAddress.toHexString()}-${disputeID.toString()}-${roundIndex.toString()}`; |
| 51 | + let shift = TokenAndETHShift.load(shiftID); |
| 52 | + if (!shift) { |
| 53 | + shift = new TokenAndETHShift(shiftID); |
| 54 | + if (feeTokenAddress !== Address.fromI32(0)) { |
| 55 | + shift.isNativeCurrency = false; |
| 56 | + shift.feeToken = feeTokenAddress.toHexString(); |
| 57 | + } else { |
| 58 | + shift.isNativeCurrency = true; |
| 59 | + } |
| 60 | + shift.feeTokenAmount = ZERO; |
| 61 | + shift.ethAmount = ZERO; |
| 62 | + shift.juror = jurorAddress.toHexString(); |
| 63 | + shift.dispute = disputeID.toString(); |
| 64 | + shift.pnkAmount = ZERO; |
| 65 | + shift.save(); |
| 66 | + } |
| 67 | + return shift; |
36 | 68 | }
|
0 commit comments