-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontractDefinition.js
171 lines (153 loc) · 3.88 KB
/
contractDefinition.js
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
const Web3 = require("web3");
let web3;
exports.initWeb3 = (web3Instance) => {
if (web3) {
return;
}
if (!web3Instance) {
web3 = new Web3(
new Web3.providers.HttpProvider("https://bsc-dataseed.binance.org/")
);
return;
}
web3 = web3Instance;
};
exports.antiSnipeContract = {
contractName: "AntiSnipe - SarabiChain",
contractAddress: "3975571f10ade161c9369651b105b4148cfbf8b9",
memorySlotsCount: 11,
memorySlotDefinitions: [
{
name: "AntiSnipe Enabled",
type: "struct",
fields: [
{ name: "enabled", type: "bool" },
{ name: "tokenContractAddress", type: "address" },
],
},
{
name: "LIQ Par Aaddress",
type: "address",
},
{ type: "map" },
{
name: "AntiSnipe Config",
type: "struct",
fields: [
{ name: "anti snipe enabled", type: "bool" },
{ name: "anti block enabled", type: "bool" },
{ name: "blacklist enabled", type: "bool" },
],
},
{ type: "map" },
{ type: "map" },
{ type: "map" },
{ name: "token launched", type: "bool" },
{
name: "Token Config",
type: "struct",
fields: [
{ name: "blockoffset", type: "uint8" },
{ name: "?", type: "uint16" },
{ name: "block number", type: "uint32" },
{ name: "block timestamp", type: "uint64" },
],
},
{ type: "map" },
{ name: "anti block finished", type: "bool" },
{ name: "bot count", type: "number" },
],
};
exports.parseMemorySlot = async (contract, slotIndex) => {
this.initWeb3();
memorySlotDefinition = contract.memorySlotDefinitions[slotIndex];
if (!memorySlotDefinition) {
return null;
}
if (memorySlotDefinition.type === "map") {
return ["Map is not supported yet"];
}
if (memorySlotDefinition.type == "struct") {
return await parseStruct(
contract.contractAddress,
memorySlotDefinition.fields,
slotIndex
);
}
return [
prettyPrintMemorySlot(
memorySlotDefinition,
await web3.eth.getStorageAt(contract.contractAddress, slotIndex)
),
];
};
parseStruct = async (contractAddress, structFields, slotIndex) => {
const result = [];
let slot = await web3.eth.getStorageAt(contractAddress, slotIndex);
let slot_i = 0;
for (let i = 0; i < structFields.length; i++) {
if (slot_i >= slot.length) {
break;
}
result.push(
prettyPrintMemorySlot(
structFields[i],
`0x${slot.substring(
slot.length - slot_i - getStructFieldLen(structFields[i].type),
slot.length - slot_i
)}`
)
);
slot_i += getStructFieldLen(structFields[i].type);
}
return result;
};
prettyPrintMemorySlot = (memorySlotDefinition, bytes) => {
return `${memorySlotDefinition.name}: ${parseBytes(
memorySlotDefinition.type,
bytes
)}`;
};
parseBytes = (type, bytes) => {
if (type.startsWith("uint")) {
type = "number";
}
switch (type) {
case "bool":
return web3.utils.hexToNumber(bytes) === 1;
case "address":
const addressBytes =
bytes.length === 40 || bytes.length == 42
? bytes
: bytesAddressToAddress(bytes);
return web3.utils.toChecksumAddress(addressBytes);
case "number":
return web3.utils.hexToNumber(bytes);
default:
return bytes;
}
};
bytesAddressToAddress = (bytes) => {
const lengthOf0x = 2;
const argLength = 64;
const totalLength = lengthOf0x + argLength;
const addressLength = 40;
if (bytes.length < totalLength) {
throw new Error("Invalid address length");
}
return `0x${bytes.substring(totalLength - addressLength)}`;
};
getStructFieldLen = (type) => {
if (type.startsWith("uint")) {
uintType = parseInt(type.substring(4));
return Math.ceil(uintType / 4);
}
switch (type) {
case "bool":
return 2;
case "address":
return 40;
default:
return 0;
}
};