-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.js
More file actions
56 lines (49 loc) · 1.49 KB
/
decoder.js
File metadata and controls
56 lines (49 loc) · 1.49 KB
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
/**
* OPNet event raw_data decoder.
* All fields in OPNet events are 32-byte big-endian chunks (Base64-encoded).
*
* Known layouts (from OIP-0020):
* Transferred 128 bytes: operator(32) + from(32) + to(32) + amount(32)
* Minted 64 bytes: to(32) + amount(32)
* Burned 64 bytes: from(32) + amount(32)
* SwapCreated variable: parsed as generic named chunks
*/
function addr(buf, offset) {
return '0x' + buf.subarray(offset, offset + 32).toString('hex');
}
function uint256(buf, offset) {
return BigInt('0x' + buf.subarray(offset, offset + 32).toString('hex')).toString();
}
export function decodeEvent(eventType, rawDataB64) {
if (!rawDataB64) return null;
try {
const buf = Buffer.from(rawDataB64, 'base64');
switch (eventType) {
case 'Transferred':
if (buf.length < 128) return null;
return {
operator: addr(buf, 0),
from: addr(buf, 32),
to: addr(buf, 64),
amount: uint256(buf, 96),
};
case 'Minted':
if (buf.length < 64) return null;
return {
to: addr(buf, 0),
amount: uint256(buf, 32),
};
case 'Burned':
if (buf.length < 64) return null;
return {
from: addr(buf, 0),
amount: uint256(buf, 32),
};
default:
// For unknown event types store byte length so callers know something is there
return { bytes: buf.length };
}
} catch {
return null;
}
}