-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpancakeswapv2.html
242 lines (217 loc) · 8.04 KB
/
pancakeswapv2.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PancakeSwap v2 Price Extract</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: sans-serif;
font-size: 16px;
}
input {
display: block;
width: 64ch;
}
label {
display: block;
padding-bottom: 1rem;
}
textarea {
width: 100%;
resize: vertical;
overflow-x: scroll;
}
</style>
</head>
<body>
<h1>PancakeSwap v2 Price Extract</h1>
<form id="form">
<label>
Token A
<input id="token-a" name="token" type="text" pattern="0x[a-fA-F0-9]{40}"
placeholder="0x0000000000000000000000000000000000000000" required>
</label>
<label>
Token B
<input id="token-b" name="token" type="text" pattern="0x[a-fA-F0-9]{40}"
placeholder="0x0000000000000000000000000000000000000000" required>
</label>
<label>
Start Block Number
<input type="number" name="start-block" min="1" step="1" required>
</label>
<label>
End Block Number (optional, last block by default)
<input type="number" name="end-block" min="1" step="1">
</label>
<button type="submit" id="submit">Extract</button>
</form>
<hr>
<div id="status">Loading...</div>
<textarea readonly id="output" rows="10"></textarea>
<script type="module">
import { ethers, Contract, JsonRpcProvider, formatUnits } from "https://cdnjs.cloudflare.com/ajax/libs/ethers/6.7.0/ethers.min.js";
const FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
const BLOCKS = 3000n;
const status = document.getElementById("status");
const output = document.getElementById("output");
const form = document.getElementById("form");
const submit = document.getElementById("submit");
const tokenA = document.getElementById("token-a");
const tokenB = document.getElementById("token-b");
const ERC20ABI = [{
constant: true,
inputs: [],
name: "decimals",
outputs: [
{
internalType: "uint8",
name: "",
type: "uint8"
}
],
payable: false,
stateMutability: "view",
type: "function"
}, {
constant: true,
inputs: [],
name: "symbol",
outputs: [
{
name: "",
type: "string"
}
],
payable: false,
stateMutability: "view",
type: "function"
}];
const FactoryABI = [{
constant: true,
inputs: [
{
internalType: "address",
name: "",
type: "address"
},
{
internalType: "address",
name: "",
type: "address"
}
],
name: "getPair",
outputs: [
{
internalType: "address",
name: "",
type: "address"
}
],
payable: false,
stateMutability: "view",
type: "function"
}];
const PairABI = [{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "uint112",
name: "reserve0",
type: "uint112"
},
{
indexed: false,
internalType: "uint112",
name: "reserve1",
type: "uint112"
}
],
name: "Sync",
type: "event"
}, {
constant: true,
inputs: [],
name: "token0",
outputs: [
{
internalType: "address",
name: "",
type: "address"
}
],
payable: false,
stateMutability: "view",
type: "function"
}, {
constant: true,
inputs: [],
name: "token1",
outputs: [
{
internalType: "address",
name: "",
type: "address"
}
],
payable: false,
stateMutability: "view",
type: "function"
}];
const provider = new JsonRpcProvider("https://bscrpc.com");
const blockTimestamps = new Map();
async function getBlockTimestamp(blockNumber) {
if (blockTimestamps.has(blockNumber)) return blockTimestamps.get(blockNumber);
const { timestamp } = await provider.getBlock(blockNumber);
blockTimestamps.set(blockNumber, timestamp * 1000);
return timestamp * 1000;
}
async function extract(tokens, startBlock, endBlock) {
console.log(tokens, startBlock, endBlock);
endBlock = BigInt(endBlock || await provider.getBlockNumber());
console.log(tokens, startBlock, endBlock);
const factory = await new Contract(FACTORY_ADDRESS, FactoryABI, provider);
const pair = new Contract(await factory.getPair(...tokens), PairABI, provider);
const token0Address = await pair.token0();
const token1Address = await pair.token1();
const token0 = new Contract(token0Address, ERC20ABI, provider);
const token1 = new Contract(token1Address, ERC20ABI, provider);
const token0Decimals = Number(await token0.decimals());
const token1Decimals = Number(await token1.decimals());
const token0Symbol = await token0.symbol();
const token1Symbol = await token1.symbol();
tokenA.value = `(${token0Address}) ${token0Symbol}`;
tokenB.value = `(${token1Address}) ${token1Symbol}`;
output.value = `date\tblock_number\tlog_index\ttransaction_index\ttransaction_hash\treserves (${token0Symbol})\treserves (${token1Symbol})\tapprox_price`;
for (let fromBlock = startBlock; fromBlock < endBlock; fromBlock += BLOCKS) {
const toBlock = fromBlock + BLOCKS;
status.innerText = `Extracting blocks ${fromBlock}-${toBlock}/${endBlock}...`;
const reserves = await pair.queryFilter("Sync", fromBlock, toBlock);
for (const { blockNumber, index, transactionIndex, transactionHash, args } of reserves)
output.value += `\n${new Date(await getBlockTimestamp(blockNumber)).toISOString()}\t${blockNumber}\t${index}\t${transactionIndex}\t${transactionHash}\t${formatUnits(args.reserve0, token0Decimals)}\t${formatUnits(args.reserve1, token1Decimals)}\t${(Number(args.reserve1) / Number(args.reserve0) / Math.pow(10, token1Decimals - token0Decimals))}`
}
}
form.addEventListener("submit", event => {
event.preventDefault();
status.innerText = "Extracting...";
const data = new FormData(event.currentTarget);
const tokens = data.getAll("token");
const startBlock = BigInt(data.get("start-block"));
const endBlock = data.get("end-block");
extract(tokens, startBlock, endBlock).then(() => {
status.innerText = "Done!"
}).catch(error => {
console.log("extract", error);
status.innerText = "Error, check console for more details";
});
});
status.innerText = "Ready";
</script>
</body>
</html>