Skip to content

Commit f8cfd8a

Browse files
lorcan-codesgharbi-bdr
authored andcommitted
Code cleanup and make parsing more reliable
1 parent d0317a8 commit f8cfd8a

File tree

5 files changed

+26
-239
lines changed

5 files changed

+26
-239
lines changed

indexer/src/indexer.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { requiredEnv } from "./utils/envs";
33
import {
44
GetTransactionReceiptResponse,
55
ParsedEvent,
6-
ParsedEvents,
76
} from "starknet";
87
import { Event } from "./entity/event";
98
import { Transaction } from "./entity/transaction";
@@ -18,6 +17,7 @@ import {
1817
} from "./utils/contract";
1918
import { IsNull, Not } from "typeorm";
2019
import assert from "assert";
20+
import { eventNameMap } from "./utils/const";
2121

2222
type ReturnedTransactionStatus = {
2323
tx_status: string;
@@ -30,19 +30,31 @@ const contractAddress = requiredEnv("CONTRACT_ADDRESS");
3030

3131
function parseEvent(emittedEvent: EMITTED_EVENT): ParsedEvent {
3232
let parsedEvent: ParsedEvent | undefined = undefined;
33+
const shouldUseNewContract = emittedEvent.block_number == null || emittedEvent.block_number > OLD_CONTRACT_BLOCK_END;
3334

34-
if (emittedEvent.block_number == null || emittedEvent.block_number >= OLD_CONTRACT_BLOCK_END) {
35-
parsedEvent = contract
36-
.parseEvents({
37-
events: [emittedEvent],
38-
} as GetTransactionReceiptResponse)
39-
.at(0);
40-
} else {
41-
parsedEvent = oldContract
35+
try {
36+
const parsingContract = shouldUseNewContract ? contract : oldContract;
37+
parsedEvent = parsingContract
4238
.parseEvents({
4339
events: [emittedEvent],
4440
} as GetTransactionReceiptResponse)
4541
.at(0);
42+
} catch (e) {
43+
console.error("Error parsing event with default parser", e);
44+
}
45+
46+
if (parsedEvent == undefined) {
47+
console.debug("Parsing with fallback parser");
48+
const parsingContract = shouldUseNewContract ? oldContract : contract;
49+
try {
50+
parsedEvent = parsingContract
51+
.parseEvents({
52+
events: [emittedEvent],
53+
} as GetTransactionReceiptResponse)
54+
.at(0);
55+
} catch (e) {
56+
console.error("Error parsing event with fallback parser", e);
57+
}
4658
}
4759

4860
assert(parsedEvent != null, "Parsed event is null.");
@@ -178,12 +190,6 @@ async function pullEvents() {
178190
}
179191
}
180192

181-
const eventNameMap = {
182-
GameCreated: "game_created",
183-
GameEvolved: "game_evolved",
184-
CellRevived: "cell_revived",
185-
} as { [key: string]: string };
186-
187193
type MaterializedViewName = "balance" | "creator" | "infinite";
188194

189195
async function refreshMaterializedView(name: MaterializedViewName) {

indexer/src/utils/const.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ export const indexerApp = "indexer";
44
export const viewRefresherApp = "viewRefresher";
55
export const generator = "generator";
66
export const apps = [indexerApp, viewRefresherApp, generator];
7+
export const eventNameMap = {
8+
GameCreated: "game_created",
9+
GameEvolved: "game_evolved",
10+
CellRevived: "cell_revived",
11+
} as { [key: string]: string };
712

813
export const abi = [
914
{

indexer/src/utils/events-old.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

indexer/src/utils/events.ts

Lines changed: 0 additions & 122 deletions
This file was deleted.

indexer/src/utils/parser.ts

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
// import { contractOldAbiPromise, contractPromise } from "./contract";
2-
// import { requiredEnv } from "./envs";
3-
41
const uint256Shift = BigInt("0x100000000000000000000000000000000");
52

6-
// const OLD_CONTRACT_BLOCK_END = requiredEnv("OLD_CONTRACT_BLOCK_END");
7-
83
const isObject = (value: any): boolean => {
94
return Object.prototype.toString.call(value) === '[object Object]'
105
}
@@ -27,46 +22,4 @@ export const deserializeContent = (parsedEvent: any): any => {
2722

2823
return eventContent;
2924
};
30-
31-
// export const getParsedEvent = async (emittedEvent: any) => {
32-
// const contractWithNewAbi = await contractPromise;
33-
// const contractWithOldAbi = await contractOldAbiPromise;
34-
// let parsedEvent: any = null;
35-
// let failed = 0
36-
// let parser = 'new'
37-
38-
// if(OLD_CONTRACT_BLOCK_END && emittedEvent.block_number <= OLD_CONTRACT_BLOCK_END){
39-
// parser = 'old'
40-
// }
41-
42-
// try {
43-
// const contract = parser === 'new' ? contractWithNewAbi : contractWithOldAbi
44-
// const [ParsedEvent] = contract.parseEvents({
45-
// events: [emittedEvent],
46-
// } as any);
47-
// parsedEvent = ParsedEvent;
48-
// } catch (e) {
49-
// failed++;
50-
// console.log(`Could not parse with ${parser} contract`, e);
51-
// }
52-
53-
// if (!parsedEvent || parsedEvent?.length === 0) {
54-
// const fallbackContract = parser === 'new' ? contractWithOldAbi : contractWithNewAbi
55-
// try {
56-
// const [ParsedEvent] = fallbackContract.parseEvents({
57-
// events: [emittedEvent],
58-
// } as any);
59-
// parsedEvent = ParsedEvent;
60-
// } catch (e) {
61-
// failed++;
62-
// console.log("Could not parse with fallback contract", e);
63-
// }
64-
// }
65-
66-
// if(failed === 2){
67-
// console.error("Failed to parse event in both parsers", emittedEvent)
68-
// }
69-
70-
// return parsedEvent
71-
// }
7225

0 commit comments

Comments
 (0)