-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (46 loc) · 1.7 KB
/
index.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
const {ethers} = require('ethers');
const WETHABI=require('./contract_json/WETH.json');
require("dotenv").config({ path: ".env" });
//mode
const WETHAddress="0x4200000000000000000000000000000000000006";
//设置时间多久重复执行
// 定时器间隔(单位:毫秒)
const interval = 25000; //25 s
const provider = new ethers.providers.JsonRpcProvider(
process.env.RPC_KEY
);
const signer = new ethers.Wallet(
process.env.PRIVATE_KEY,
provider
);
const WETH=new ethers.Contract(WETHAddress,WETHABI,signer);
async function swap(){
const currentAddress=await signer.getAddress();
console.log("currentAddress:",currentAddress);
//执行weth存入
const currentBalance=await provider.getBalance(currentAddress);
console.log("balance:",ethers.utils.formatEther(currentBalance));
//80%
const swapAmount=ethers.utils.formatEther(currentBalance)*0.8;
const swapAmountString=swapAmount.toString();
console.log("交易数量:",swapAmountString);
const depositETH=await WETH.deposit({value: ethers.utils.parseEther(swapAmountString)});
const depositETHTx=await depositETH.wait();
if(depositETHTx.status===1){
console.log("存入成功");
const thisBalance=await WETH.balanceOf(currentAddress);
console.log("提取的WETH数量",ethers.utils.formatEther(thisBalance));
const withdrawETH=await WETH.withdraw(thisBalance);
const withdrawETHTx=await withdrawETH.wait();
if(withdrawETHTx.status===1){
console.log("提取成功");
}else{
console.log("提取失败");
}
}else{
console.log("存入失败");
}
}
// swap();
// 启动定时器
setInterval(swap, interval);