|
| 1 | +import { json, JSONValueKind, log } from "@graphprotocol/graph-ts"; |
1 | 2 | import { Evidence as EvidenceEvent } from "../generated/EvidenceModule/EvidenceModule";
|
2 | 3 | import { ClassicEvidence } from "../generated/schema";
|
3 | 4 | import { ensureClassicEvidenceGroup } from "./entities/ClassicEvidenceGroup";
|
4 | 5 | import { ensureUser } from "./entities/User";
|
5 | 6 | import { ONE } from "./utils";
|
| 7 | +import { JSONValueToMaybeString } from "../../utils"; |
6 | 8 |
|
7 | 9 | export function handleEvidenceEvent(event: EvidenceEvent): void {
|
8 | 10 | const evidenceGroupID = event.params._externalDisputeID.toString();
|
9 | 11 | const evidenceGroup = ensureClassicEvidenceGroup(evidenceGroupID);
|
10 | 12 | const evidenceIndex = evidenceGroup.nextEvidenceIndex;
|
11 | 13 | evidenceGroup.nextEvidenceIndex = evidenceGroup.nextEvidenceIndex.plus(ONE);
|
12 | 14 | evidenceGroup.save();
|
13 |
| - const evidence = new ClassicEvidence(`${evidenceGroupID}-${evidenceIndex.toString()}`); |
| 15 | + const evidenceId = `${evidenceGroupID}-${evidenceIndex.toString()}`; |
| 16 | + const evidence = new ClassicEvidence(evidenceId); |
| 17 | + evidence.evidenceIndex = evidenceIndex.plus(ONE).toString(); |
14 | 18 | const userId = event.params._party.toHexString();
|
15 | 19 | evidence.timestamp = event.block.timestamp;
|
16 | 20 | evidence.evidence = event.params._evidence;
|
17 | 21 | evidence.evidenceGroup = evidenceGroupID.toString();
|
18 | 22 | evidence.sender = userId;
|
| 23 | + evidence.senderAddress = userId; |
19 | 24 | ensureUser(userId);
|
| 25 | + |
| 26 | + let jsonObjValueAndSuccess = json.try_fromString(event.params._evidence); |
| 27 | + if (!jsonObjValueAndSuccess.isOk || jsonObjValueAndSuccess.isError) { |
| 28 | + log.error(`Error getting json object for evidenceId {}`, [evidenceId]); |
| 29 | + evidence.save(); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + if (jsonObjValueAndSuccess.value.isNull() || jsonObjValueAndSuccess.value.kind !== JSONValueKind.OBJECT) { |
| 34 | + log.error(`Encountered invalid parsed value for evidenceId {}`, [evidenceId]); |
| 35 | + evidence.save(); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + let jsonObj = jsonObjValueAndSuccess.value.toObject(); |
| 40 | + if (!jsonObj) { |
| 41 | + log.error(`Error converting json object for evidenceId {}`, [evidenceId]); |
| 42 | + evidence.save(); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + let name = jsonObj.get("name"); |
| 47 | + let description = jsonObj.get("description"); |
| 48 | + let fileURI = jsonObj.get("fileURI"); |
| 49 | + let fileTypeExtension = jsonObj.get("fileTypeExtension"); |
| 50 | + |
| 51 | + evidence.name = JSONValueToMaybeString(name); |
| 52 | + evidence.description = JSONValueToMaybeString(description); |
| 53 | + |
| 54 | + if (fileURI) { |
| 55 | + evidence.fileURI = JSONValueToMaybeString(fileURI); |
| 56 | + } |
| 57 | + |
| 58 | + if (fileTypeExtension) { |
| 59 | + evidence.fileTypeExtension = JSONValueToMaybeString(fileTypeExtension); |
| 60 | + } |
| 61 | + |
20 | 62 | evidence.save();
|
21 | 63 | }
|
0 commit comments