-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliquidity_actions.js
394 lines (319 loc) · 14 KB
/
liquidity_actions.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
const redis = require("redis");
const configloader = require('./config')
const loggerLoader = require('./config/logger')
const { Util, Gwei, ContractWrapper } = require('./app/util.js')
const { ContractApp } = require('./app/index')
const BN = require('bignumber.js')
const { toWei, numberToHex, leftPad } = require('web3-utils')
// ==================================================================================== |
// PASSWORD='CsOkEX!23$' ENV=okex SUBMIT=N node liquidity_actions
// --------------------- Config Params ------------------------------------------------ +
const MyAddress = '0x82deec6f97572b4a1d457778328a45aa72cbf9f2'
const SwapRouter = 'CherrySwap_SwapRouter'
const MasterChef = 'CherrySwap_MasrerChef'
const TokenA = 'CHE'
const TokenB = 'USDT'
const LP_TokenAddr = '0x089dedbfd12f2ad990c55a2f1061b8ad986bff88'
const GasPriceGwei = 0.1
// ------------------------------------------------------------------------------------ |
const gasPrice = GasPriceGwei * Gwei
class BaseApp extends ContractApp {
constructor(util, config, contractName, gasPrice) {
super(util, config)
console.log(contractName)
this.owner = config.contracts[contractName].creator
this.address = config.contracts[contractName].address
this.gasPrice = gasPrice
const JsonData = require(config.contracts[contractName].jsonPath)
const contractWrapper = new ContractWrapper(util.eth, JsonData.abi, config.contracts[contractName].address)
this.setContractWrapper(contractWrapper)
this.apps = {}
this.tokens = {}
}
async transferOwnership(owner) {
await this.methodSend('transferOwnership', [owner], this.owner, { gasPrice: this.gasPrice, gas: 3000000 })
}
async openWorkerMode() {
await this.methodSend(
'resetWorkerModeEnable',
[true],
this.owner,
{ gasPrice: this.gasPrice, gas: 60000 })
}
async addWorkers(workers) {
await this.methodSend('addWorkers', [workers], this.owner, { gasPrice: this.gasPrice, gas: 300000 * workers.length })
}
async printWorkers(workers) {
for(const worker of workers) {
const result = await this.methodCall('isWorker', [worker])
console.log(worker, 'is_worker ?', result)
}
}
async deposit(from, amount) {
await this.methodSend(
'deposit',
['0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE', toWei(String(amount), 'ether')],
from,
{ value: toWei(String(amount), 'ether'), gasPrice: this.gasPrice, gas: 100000 })
}
async withdraw(amount, to) {
await this.methodSend(
'withdraw',
[toWei(String(amount), 'ether'), to],
this.owner,
{ value: 0, gasPrice: this.gasPrice, gas: 100000 })
}
async erc20Transfer(token, amount, to) {
await this.methodSend(
'transfer',
[token, toWei(String(amount), 'ether'), to],
this.owner,
{ value: 0, gasPrice: this.gasPrice, gas: 100000 })
}
async approve(token, spender, amount) {
await this.methodSend(
'approve',
[token, spender, toWei(String(amount), 'ether')],
this.owner,
{ value: 0, gasPrice: this.gasPrice, gas: 200000 })
}
async ethTtransfer(from, to, amount) {
await this.transfer(from, to, toWei(String(amount), 'Ether'), { gasPrice: this.gasPrice, gas: 100000 })
}
}
class Bee extends BaseApp {
async xLog() {
await this.methodSend(
'xLog',
[Date.now()],
this.owner,
{ value: 0, gasPrice, gas: 3000000 })
}
loadContractFromConfig(contractName, key) {
const app = new ContractApp(this.util, this.config)
const appConf = this.config.contracts[contractName]
const AbiJsonData = require(appConf.jsonPath)
const contractWrapper = new ContractWrapper(this.util.eth, AbiJsonData.abi, appConf.address)
app.setContractWrapper(contractWrapper)
app.setAddress(appConf.address)
this.apps[key] = app
}
loadTokenFromConfig(tokenName) {
const app = new ContractApp(this.util, this.config)
const tokenConf = this.config.tokens[tokenName]
const AbiJsonData = require(tokenConf.jsonPath)
const contractWrapper = new ContractWrapper(this.util.eth, AbiJsonData.abi, tokenConf.address)
app.setContractWrapper(contractWrapper)
app.setAddress(tokenConf.address)
this.tokens[tokenName] = app
}
loadToken(address) {
const app = new ContractApp(this.util, this.config)
const JsonData = require(`./abis/IERC20.json`)
const contractWrapper = new ContractWrapper(this.util.eth, JsonData.abi, address)
app.setContractWrapper(contractWrapper)
app.setAddress(address)
this.tokens[String(address).toLocaleLowerCase()] = app
}
// 提前步骤
// = 合约 approve X Token to swapPool
// = 合约 approve USDT to swapPool
// = 1 stake
// = 1.1 AddLiquidity to swapPool
// = 1.2 approve LP Token to LiquidityPool
// = 1.3 deposit(amount)
// = 2 withdraw and sell
// = 2.1 withdraw LP Token and reward from LiquidityPool
// = 2.2 remove Liquidity from swapPool
// = 2.4 swap X token to USDT
// = 1 stake
// 0xe2bbb1580000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000054377455623f8a6
async methodEncode(appKey, name, parmas) {
const app = this.apps[appKey]
return await app.funcEncode(name, [...parmas])
}
// async methodEncode(targetAddress, name, parmas) {
// const addr = String(targetAddress).toLocaleLowerCase()
// const app = this.apps[addr]
// return await app.funcEncode(name, [...parmas])
// }
async methodEncodeToken(targetAddress, name, parmas) {
const addr = String(targetAddress).toLocaleLowerCase()
const app = this.tokens[addr]
return await app.funcEncode(name, [...parmas])
}
async approve(tokenAddress, spender, amount='0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') {
return await this.methodSend(
'approve',
[tokenAddress, spender, amount],
this.owner,
{ value: 0, gasPrice, gas: 3000000 })
}
async deposit(lpTokenAddress, pid) {
const tokenLP = this.tokens[lpTokenAddress]
const balance = await tokenLP.methodCall('balanceOf', [this.address])
console.log(balance)
const depositRaw = await this.methodEncode('MasterChef', 'deposit', [pid, balance])
console.log(depositRaw)
const steps = [
[this.apps['MasterChef'].address, depositRaw, 0], // deposit
]
return await this.methodSend(
'xProxySteps',
[steps],
this.owner,
{ value: 0, gasPrice, gas: 3000000 })
}
async havest(lpTokenAddress, pid, gasPrice1) {
const tokenLP = this.tokens[lpTokenAddress]
const appMasterChef = this.apps['MasterChef']
const userInfo = await appMasterChef.methodCall('userInfo', [pid, this.address])
console.log(userInfo)
const amount = userInfo.amount
// rewardDebt: '1360865814891197234',
// accKstAmount: '975901324112876'
const withdrawRaw = await this.methodEncode('MasterChef', 'withdraw', [pid, amount])
// console.log(withdrawRaw)
// const trfRaw = await this.funcEncode('transferERC20From', [lpTokenAddress, MyAddress, this.address, amount])
// const balance = await tokenLP.methodCall('balanceOf', [this.address])
const removeLiquidityRaw = await this.funcEncode('xRemoveLiquidity', [
this.apps['SwapRouter'].address,
this.tokens[TokenA].address,
this.tokens[TokenB].address,
amount,
// toWei('10', 'ether'),
0,
0,
lpTokenAddress
])
const swapRaw = await this.funcEncode('xSwapExactTokensForTokens',
[
this.apps['SwapRouter'].address,
[this.tokens[TokenA].address, this.tokens[TokenB].address],
0,
0,
this.address,
parseInt(Date.now()/1000 + 60)
])
const steps = [
[this.apps['MasterChef'].address, withdrawRaw, 0], //
// [this.address, trfRaw, 0],
[this.address, removeLiquidityRaw, 0], // removeLiquidity
[this.address, swapRaw, 0],
]
const gp = gasPrice1 ? parseFloat(gasPrice1) * Gwei : gasPrice
return await this.methodSend(
'xProxySteps',
[steps],
this.owner,
{ value: 0, gasPrice: gp, gas: 30000000 })
}
async xProxySteps(poolAddress) {
const steps = [
[poolAddress, '0x3d18b912', 0], // getReward
[poolAddress, '0xe9fad8ee', 0], // exit
]
return await this.methodSend(
'xProxySteps',
[steps],
this.owner,
{ value: 0, gasPrice, gas: 8000000 })
}
async transferERC20From() {
await this.methodSend(
'transferERC20From',
['0x84ee6a98990010fe87d2c79822763fca584418e9', MyAddress, this.address, toWei('59.2775833973907', 'ether')],
this.owner,
{ value: 0, gasPrice: this.gasPrice, gas: 8000000 })
}
async erc20Transfer(token, amount, to) {
await this.methodSend(
'transferERC20',
[token, amount, to],
this.owner,
{ value: 0, gasPrice: this.gasPrice, gas: 1000000 })
}
async xRemoveLiquidity(routerAddress, lpTokenAddress) {
await this.methodSend(
'xRemoveLiquidity',
[routerAddress, '0xab0d1578216a545532882e420a8c61ea07b00b12', '0x382bB369d343125BfB2117af9c149795C6C65C50', toWei('59.2775833973907', 'ether'), 0, 0, lpTokenAddress],
this.owner,
{ value: 0, gasPrice: this.gasPrice, gas: 30000000 })
}
async xSwapExactTokensForTokens(routerAddress) {
await this.methodSend(
'xSwapExactTokensForTokens',
[routerAddress, ['0xab0d1578216a545532882e420a8c61ea07b00b12', '0x382bB369d343125BfB2117af9c149795C6C65C50'], 0, 0, this.address, parseInt(Date.now()/1000 + 60)],
this.owner,
{ value: 0, gasPrice: this.gasPrice, gas: 30000000 })
}
async approveLpToken(lpTokenAddress) {
const tokenLP = this.tokens[lpTokenAddress]
await tokenLP.methodSend('approve', [this.address, '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'], this.owner, { value: 0, gasPrice, gas: 3000000 })
}
async msgHandle(message) {
const msgData = JSON.parse(message)
try {
if (msgData.function) {
console.log("===>", msgData.symbol, msgData.function, msgData.gasPrice)
} else {
console.log("===>", msgData.name, msgData.pid, msgData.gasPrice, msgData.tx)
if (msgData.name === 'removeLiquidity' && parseInt(msgData.pid) === 2) {
// if (msgData.name === 'addLiquidity' && parseInt(msgData.pid) === 2) {
await this.havest(LP_TokenAddr, 2, msgData.gasPrice)
} else if (msgData.name === 'removeLiquidityWithPermit' && parseInt(msgData.pid) === 2) {
await this.havest(LP_TokenAddr, 2, msgData.gasPrice)
}
}
} catch (e) {
console.log(e)
}
}
async subscribe(channel) {
const that = this
const subscriber = redis.createClient({
host: 'localhost',
port: 6379,
db: 0,
});
subscriber.on("subscribe", function(channel, count) {
console.log('subscribe')
});
subscriber.on("message", async function(channel, message) {
console.log("Subscriber received message in channel '" + channel + "': " + message);
// await app.step2(OKEX_lpTokenAddr, OKEX_LiquidityPool, 6, OKEX_kswapRouterAddr, OKEX_TokenKstAddr, OKEX_TokenUSDTAddr)
await that.msgHandle(message)
});
subscriber.subscribe(channel);
}
}
async function run() {
try {
const env = process.env.ENV
const config = configloader({ mode: env, homeDir: __dirname })
loggerLoader({ mode: env, homeDir: __dirname })
const util = new Util(config.url)
const app = new Bee(util, config, 'MultiAction', gasPrice)
app.loadContractFromConfig(SwapRouter, 'SwapRouter')
app.loadContractFromConfig(MasterChef, 'MasterChef')
app.loadTokenFromConfig(TokenA)
app.loadTokenFromConfig(TokenB)
app.loadToken(LP_TokenAddr)
await app.subscribe('pending')
// await app.approve(app.tokens[TokenA].address, app.apps['SwapRouter'].address)
// await app.approve(app.tokens[TokenB].address, app.apps['SwapRouter'].address)
// await app.approve(LP_TokenAddr, app.apps['SwapRouter'].address)
// await app.approve(LP_TokenAddr, app.apps['MasterChef'].address)
// await app.deposit(LP_TokenAddr, 2)
// ls await app.havest(LP_TokenAddr, 2)
// await app.xSwapExactTokensForTokens(OKEX_kswapRouterAddr)
// await app.erc20Transfer(config.tokens['USDT'].address, toWei('30', 'ether'), '0x82deec6f97572b4a1d457778328a45aa72cbf9f2')
// await app.approveLpToken(OKEX_lpTokenAddr)
// await app.xRemoveLiquidity(OKEX_kswapRouterAddr, OKEX_lpTokenAddr)
// await app.transferOwnership('0x91F402f532498b52069d73C4fA672DD97f8d1b80')
// await app.addWorkers(['0x82deec6f97572b4a1d457778328a45aa72cbf9f2'])
} catch (e) {
console.error("deploy contract fail:", e)
}
}
run()