-
Notifications
You must be signed in to change notification settings - Fork 0
/
geth_liquidity_web3.mjs
172 lines (127 loc) · 4.19 KB
/
geth_liquidity_web3.mjs
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
// ganache --miner.blockTime 1 --fork.url wss://mainnet.infura.io/ws/v3/[your-own-infura-access-url-required]@13266287 --wallet.unlockedAccounts 0xE2313Ab106fFb9196b29f5B8880Ab474355deb90
// 13266287 is last ETH block before GETH contract hack - first fake mint was https://etherscan.io/tx/0x78f67582fde638a94f3ad7869eef8d8a3518e5c4f4a349f19c80d4afde17b7f8
import Web3 from 'web3'
import ERC20ABI from './ERC20.json' assert { type: "json" }
import INonfungiblePositionManagerABI from './INonfungiblePositionManager.json' assert { type: "json" }
const WALLET_ADDRESS = "0xE2313Ab106fFb9196b29f5B8880Ab474355deb90"
const Old_GETH_Token_addr = "0x9ad03c34aab604a9e0fde41dbf8e383e11c416c4"
const WETH9_Token = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' // wrapped ETH
const positionManagerAddress= "0xC36442b4a4522E871399CD717aBDD847Ab11FE88" // Uniswap NonfungiblePositionManager
const UNISWAP_NFT_TOKENS = [
129737,
125244,
123797,
120979,
115678,
73830,
72603,
72560,
68838,
65359,
21531,
]
async function main()
{
const web3 = new Web3("http://127.0.0.1:8545");
async function getETHBalanceForAddress(for_whom)
{
let balanceInWei = await web3.eth.getBalance(for_whom)
console.log(for_whom, "ETH balance", web3.utils.fromWei(balanceInWei) )
}
async function getTokenBalanceForAddress(for_whom, TOKEN_ADDRESS)
{
const TOKEN = new web3.eth.Contract(
ERC20ABI,
TOKEN_ADDRESS,
{ from: WALLET_ADDRESS, gasLimit: 1000000 }
)
const token_balance = await TOKEN.methods.balanceOf(for_whom).call();
console.log(for_whom, "token", TOKEN_ADDRESS,"balance:", web3.utils.fromWei(token_balance) )
}
async function check_wallet_balances(for_wallet)
{
return new Promise(async (resolve)=>
{
await await_next_block_and_exec(()=>
{
console.log("check_wallet_balances")
getTokenBalanceForAddress(for_wallet, Old_GETH_Token_addr)
getTokenBalanceForAddress(for_wallet, WETH9_Token)
getETHBalanceForAddress(for_wallet)
})
resolve()
})
}
async function await_next_block_and_exec(x, b)
{
return new Promise(async resolve =>
{
let prev_block = 0
if (typeof b === 'undefined')
prev_block = await web3.eth.getBlockNumber()
else
prev_block = b
const cur_block = await web3.eth.getBlockNumber()
console.log("prev_block", prev_block, "cur_block", cur_block)
if (cur_block > prev_block)
{
await x()
resolve()
}
else
setTimeout( ()=>await_next_block_and_exec(async()=>{await x(); resolve();}, cur_block), 2000)
})
}
const networkId = await web3.eth.net.getId();
const nonFungiblePositionManagerContract = new web3.eth.Contract(
INonfungiblePositionManagerABI.abi,
positionManagerAddress,
{ from: WALLET_ADDRESS, gasLimit: 500000 }
)
await check_wallet_balances(WALLET_ADDRESS)
for(let i=0; i < UNISWAP_NFT_TOKENS.length; i++)
{
console.log("")
console.log("")
let nft_token_id = UNISWAP_NFT_TOKENS[i]
console.log("i", i, "of", UNISWAP_NFT_TOKENS.length, "token", nft_token_id)
try {
let res = await nonFungiblePositionManagerContract.methods.positions( String( nft_token_id ) ).call()
const totalLiquidity = res.liquidity
console.log(nft_token_id, "nft token, totalLiquidity", totalLiquidity.toString())
if (totalLiquidity == 0)
continue;
const params = {
tokenId: nft_token_id,
liquidity: totalLiquidity,
amount0Min: 0,
amount1Min: 0,
deadline: Math.floor(Date.now() / 1000) + (60 * 10),
}
await await_next_block_and_exec(async()=>
{
let res2 = await nonFungiblePositionManagerContract.methods.decreaseLiquidity(params).send()
console.log("res2", res2)
})
let collect_params = {
tokenId: nft_token_id,
amount0Max: "100000000000000000000",
amount1Max: "100000000000000000000",
recipient: WALLET_ADDRESS,
}
await await_next_block_and_exec(async()=>
{
let res3 = await nonFungiblePositionManagerContract.methods.collect(collect_params).send()
console.log("res3", res3)
})
}
catch(e)
{
console.log(e)
}
console.log("")
console.log("")
}
await check_wallet_balances(WALLET_ADDRESS)
}
main()