generated from ora-io/zkgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.ts
310 lines (280 loc) · 9.64 KB
/
parser.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// @ts-ignore
import { require } from "@hyperoracle/zkgraph-lib";
import { Bytes, BigInt } from "@hyperoracle/zkgraph-lib";
const OFFERS_OFFSET = BigInt.fromU64(0x80);
const CONSIDERATIONS_OFFSET = BigInt.fromU64(0x120);
const SINGLE_OFFER_DATA_SIZE = BigInt.fromU64(32).times(4);
const SINGLE_CONSIDERATION_DATA_SIZE = BigInt.fromU64(32).times(5);
const SEAPORT_FEE_RECIPIENT = Bytes.fromHexString(
"0x0000a26b00c1f0df003000390027140000faa719",
);
// reference link:
// https://etherscan.io/address/0x00000000000000adc04c56bf30ac9d3c0aaf14dc#code#F9#L115
enum ItemType {
NATIVE,
ERC20,
ERC721,
ERC1155,
ERC721_WITH_CRITERIA,
ERC1155_WITH_CRITERIA,
}
/**
* Retrieves the itemType from a byte array.
*
* @param {Bytes} bytes - The byte array from which to retrieve the item type.
* @return {ItemType} The corresponding item type based on the byte array.
*/
function itemTypeFromBytes(bytes: Bytes): ItemType {
const itemType = BigInt.fromBytesBigEndian(bytes).toU32();
switch (itemType) {
case 0:
return ItemType.NATIVE;
case 1:
return ItemType.ERC20;
case 2:
return ItemType.ERC721;
case 3:
return ItemType.ERC1155;
case 4:
return ItemType.ERC721_WITH_CRITERIA;
case 5:
return ItemType.ERC1155_WITH_CRITERIA;
default:
// Other item types are not allowed at the moment.
// If an uncommon item type is found,
// it should be understood as a parsing error in the event structure.
require(false);
return ItemType.NATIVE;
}
}
/** reference link:
* https://etherscan.io/address/0x00000000000000adc04c56bf30ac9d3c0aaf14dc#code#F7#L49
* In the source code, there are variables called endAmount and startAmount,
* but in the actual blockchain events, there is only a variable called amount.
*/
class OfferItem {
public itemType: ItemType;
public token: Bytes;
public identifierOrCriteria: BigInt;
public amount: BigInt;
public constructor(
itemType: ItemType,
token: Bytes,
identifierOrCriteria: BigInt,
amount: BigInt,
) {
this.itemType = itemType;
this.token = token;
this.identifierOrCriteria = identifierOrCriteria;
this.amount = amount;
}
/**
* Creates an instance of OfferItem from the given offer data.
*
* @param {Bytes} data - The offer data.
* @return {OfferItem} The created OfferItem instance.
*/
static fromOfferData(data: Bytes): OfferItem {
const itemTypeSlice = data.slice(0, 32);
const token = data.slice(32, 64);
const identifierOrCriteriaSlice = data.slice(64, 96);
const amountSlice = data.slice(96, 128);
const itemType: ItemType = itemTypeFromBytes(itemTypeSlice);
const identifierOrCriteria = BigInt.fromBytesBigEndian(
identifierOrCriteriaSlice,
);
const amount = BigInt.fromBytesBigEndian(amountSlice);
return new OfferItem(itemType, token, identifierOrCriteria, amount);
}
}
/**
* Generates a list of `OfferItem` objects from the given `data`.
*
* @param {Bytes} data - The data from offers slice in `Event.data`.
* @return {OfferItem[]} - An array of `OfferItem` objects.
*/
function offerItemsFromData(data: Bytes): OfferItem[] {
const offersLengthSlice = data.slice(0, 32);
const offersLength = BigInt.fromBytesBigEndian(offersLengthSlice);
const offersDataSize = SINGLE_OFFER_DATA_SIZE.times(offersLength);
const offersData = data.slice(32, offersDataSize.plus(32).toI32());
// TODO: Do we need to add a check here
// for offersOffset + offersDataSize + 32 == considerationsOffset
let offers: OfferItem[] = [];
for (let i = 0; i < offersLength.toI32(); i++) {
const offerDataStartOffset = i * SINGLE_OFFER_DATA_SIZE.toI32();
const offerDataEndOffset = (i + 1) * SINGLE_OFFER_DATA_SIZE.toI32();
const offerData = offersData.slice(
offerDataStartOffset,
offerDataEndOffset,
);
const offer = OfferItem.fromOfferData(offerData);
offers.push(offer);
}
return offers;
}
// reference link:
// https://etherscan.io/address/0x00000000000000adc04c56bf30ac9d3c0aaf14dc#code#F7#L62
// The Consideration and OfferItem are the same, only has the amount,
// without distinguishing between endAmount and startAmount.
class Consideration {
public itemType: ItemType;
public token: Bytes;
public identifierOrCriteria: BigInt;
public amount: BigInt;
public recipient: Bytes;
public constructor(
itemType: ItemType,
token: Bytes,
identifierOrCriteria: BigInt,
amount: BigInt,
receipient: Bytes,
) {
this.itemType = itemType;
this.token = token;
this.identifierOrCriteria = identifierOrCriteria;
this.amount = amount;
this.recipient = receipient;
}
/**
* Creates a `Consideration` instance from the given consideration data.
*
* @param {Bytes} data - The consideration data to create the instance from.
* @return {Consideration} The created `Consideration` instance.
*/
static fromConsiderationData(data: Bytes): Consideration {
const itemTypeSlice = data.slice(0, 32);
const token = data.slice(32, 64);
const identifierOrCriteriaSlice = data.slice(64, 96);
const amountSlice = data.slice(96, 128);
const recipient = data.slice(128, 160);
const itemType: ItemType = itemTypeFromBytes(itemTypeSlice);
const identifierOrCriteria = BigInt.fromBytesBigEndian(
identifierOrCriteriaSlice,
);
const amount = BigInt.fromBytesBigEndian(amountSlice);
return new Consideration(
itemType,
token,
identifierOrCriteria,
amount,
recipient,
);
}
}
/**
* Generates the array of considerations from the given data.
*
* @param {Bytes} data - The considerations slice in `Event.data`.
* @return {Consideration[]} An array of considerations.
*/
function considerationsFromData(data: Bytes): Consideration[] {
const considerationsLengthSlice = data.slice(0, 32);
const considerationsLength = BigInt.fromBytesBigEndian(
considerationsLengthSlice,
);
const considerationsDataSize =
SINGLE_CONSIDERATION_DATA_SIZE.times(considerationsLength);
const considerationsData = data.slice(
32,
considerationsDataSize.plus(32).toI32(),
);
let considerations: Consideration[] = [];
for (let i = 0; i < considerationsLength.toI32(); i++) {
const considerationDataStartOffset =
i * SINGLE_CONSIDERATION_DATA_SIZE.toI32();
const considerationDataEndOffset =
(i + 1) * SINGLE_CONSIDERATION_DATA_SIZE.toI32();
const considerationData = considerationsData.slice(
considerationDataStartOffset,
considerationDataEndOffset,
);
const consideration =
Consideration.fromConsiderationData(considerationData);
considerations.push(consideration);
}
return considerations;
}
export class OrderFulfilled {
public orderHash: Bytes;
public recipient: Bytes;
public offers: OfferItem[];
public offersLength: i32;
public considerations: Consideration[];
public considerationsLength: i32;
public constructor(
orderHash: Bytes,
recipient: Bytes,
offers: OfferItem[],
considerations: Consideration[],
) {
this.orderHash = orderHash;
this.recipient = recipient;
this.offers = offers;
this.offersLength = offers.length;
this.considerations = considerations;
this.considerationsLength = considerations.length;
}
/**
* Converts the given event data into an OrderFulfilled object.
*
* @param {Bytes} data - The event.data to convert.
* @return {OrderFulfilled | null} - The converted OrderFulfilled object, or null if the conversion fails.
*/
static fromEventData(data: Bytes): OrderFulfilled {
const orderHash = data.slice(0, 32);
const recipient = data.slice(32, 32 + 32);
const offersOffsetSlice = data.slice(64, 64 + 32);
const considerationsOffsetSlice = data.slice(96, 96 + 32);
const offersOffset = BigInt.fromBytesBigEndian(offersOffsetSlice);
const considerationsOffset = BigInt.fromBytesBigEndian(
considerationsOffsetSlice,
);
/*
* The fields before "offers" are all of static type,
* and the assertion here should always be true.
*/
require(offersOffset === OFFERS_OFFSET);
/* The offset of the "consideration" field depends on the length of the offer.
* Currently, all encountered event offers have a length of 1.
* Consider scenarios where the length of the offer is greater than one.
*/
require(considerationsOffset === CONSIDERATIONS_OFFSET);
const offersData = data.slice(
offersOffset.toU32(),
considerationsOffset.toU32(),
);
const offers: OfferItem[] = offerItemsFromData(offersData);
const considerationsData = data.slice(considerationsOffset.toU32());
const considerations: Consideration[] =
considerationsFromData(considerationsData);
return new OrderFulfilled(orderHash, recipient, offers, considerations);
}
/**
* Calculates the total NFT trade amount.
*
* @return {BigInt} The total NFT trade amount.
*/
getTotalNftTradeAmount(): BigInt {
let totalNftTradeAmount = BigInt.zero();
for (let i = 0; i < this.offersLength; i++) {
const NftTradeAmount = this.offers[i].amount;
totalNftTradeAmount = totalNftTradeAmount.plus(NftTradeAmount);
}
return totalNftTradeAmount;
}
/**
* Calculates the total Ethereum trade amount.
*
* @return {BigInt} The total Ethereum trade amount.
*/
getTotalEthTradeAmount(): BigInt {
let totalEthTradeAmount = BigInt.zero();
for (let i = 0; i < this.considerationsLength; i++) {
if (this.considerations[i].recipient === SEAPORT_FEE_RECIPIENT) continue;
const EthTradeAmount = this.considerations[i].amount;
totalEthTradeAmount = totalEthTradeAmount.plus(EthTradeAmount);
}
return totalEthTradeAmount;
}
}