-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpragma_indexer.ts
151 lines (136 loc) · 3.95 KB
/
pragma_indexer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import type {
Block,
Filter,
} from "https://esm.sh/@apibara/[email protected]/starknet";
import type { Config } from "https://esm.sh/@apibara/[email protected]";
import { pairIdToToken } from "./constants.ts";
// 01 Jan 2024 HKT 00:00:45
const JAN_01_2024_BLOCK = 489712;
export const PRAGMA_V0_END_BLOCK = 524957;
const PRAGMA_V0_CONTRACT =
`0x0346c57f094d641ad94e43468628d8e9c574dcb2803ec372576ccc60a40be2c4`;
const PRAGMA_V1_CONTRACT =
`0x2a85bd616f912537c50a49a4076db02c00b29b2cdc8a197ce92ed1837fa875b`;
const SUBMITTED_SPOT_ENTRY_SELECTOR =
`0x0280bb2099800026f90c334a3a23888ffe718a2920ffbbf4f44c6d3d5efb613c`;
const SUBMITTED_SPOT_ENTRY_EVENT_DATA_LENGTH = 6;
export interface SubmittedSpotEntry {
block_timestamp: number;
source_timestamp: number;
event_index: number;
source: `0x${string}`;
publisher: `0x${string}`;
token_symbol: typeof pairIdToToken[keyof typeof pairIdToToken];
price: string;
volume: `0x${string}`;
}
const filter: Filter = {
header: {},
events: [
{
fromAddress: PRAGMA_V0_CONTRACT,
keys: [SUBMITTED_SPOT_ENTRY_SELECTOR],
includeReceipt: true,
includeTransaction: false,
},
{
fromAddress: PRAGMA_V1_CONTRACT,
keys: [SUBMITTED_SPOT_ENTRY_SELECTOR],
includeReceipt: true,
includeTransaction: false,
},
],
};
export const config: Config = {
streamUrl: "https://mainnet.starknet.a5a.ch",
startingBlock: JAN_01_2024_BLOCK,
network: "starknet",
finality: "DATA_STATUS_ACCEPTED",
filter,
sinkType: "postgres",
sinkOptions: {
tableName: "submitted_spot_entries",
},
};
export default function transform({ header, events }: Block) {
if (!events || events.length === 0 || !header) {
return [];
}
const submittedSpotEntryEvents: SubmittedSpotEntry[] = [];
let eventIndex = 0;
for (
const {
event,
receipt,
} of events
) {
const block_number = header.blockNumber;
const block_timestamp = header.timestamp;
const transaction_hash = receipt.transactionHash;
const fromAddress = event.fromAddress;
if (
!event.data ||
event.data.length !== SUBMITTED_SPOT_ENTRY_EVENT_DATA_LENGTH ||
!block_number || !block_timestamp || !transaction_hash || !fromAddress
) {
continue;
}
if (
PRAGMA_V0_END_BLOCK >= Number(block_number) &&
BigInt(fromAddress) === BigInt(PRAGMA_V0_CONTRACT)
) {
const [
source_timestamp,
source,
publisher,
pairId,
price,
volume,
] = event.data;
if (!(Number(pairId) in pairIdToToken)) {
continue;
}
const submittedSpotEntryEvent: SubmittedSpotEntry = {
block_timestamp: new Date(block_timestamp).getTime() / 1000,
source_timestamp: Number(source_timestamp),
event_index: eventIndex,
source,
publisher,
token_symbol: pairIdToToken[Number(pairId)],
price: Math.floor(Number(price)).toFixed(0),
volume,
};
submittedSpotEntryEvents.push(submittedSpotEntryEvent);
} else if (
PRAGMA_V0_END_BLOCK < Number(block_number) &&
BigInt(fromAddress) === BigInt(PRAGMA_V1_CONTRACT)
) {
const [
source_timestamp,
source,
publisher,
// Pragma V1 has a different order of event data
price,
pairId,
volume,
] = event.data;
if (!(Number(pairId) in pairIdToToken)) {
continue;
}
const submittedSpotEntryEvent: SubmittedSpotEntry = {
block_timestamp: new Date(block_timestamp).getTime() / 1000,
source_timestamp: Number(source_timestamp),
event_index: eventIndex,
source,
publisher,
token_symbol: pairIdToToken[Number(pairId)],
price: Math.floor(Number(price)).toFixed(0),
volume,
};
submittedSpotEntryEvents.push(submittedSpotEntryEvent);
}
eventIndex += 1;
}
console.log(submittedSpotEntryEvents);
return submittedSpotEntryEvents;
}