forked from paraswap/paraswap-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (78 loc) · 3.35 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
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
require("dotenv").config();
const Web3 = require("web3");
const BN = require("bigNumber.js");
const HDWalletProvider = require("truffle-hdwallet-provider");
const {ParaSwap} = require("paraswap");
const PROVIDER_URL = process.env.PROVIDER_URL;
const TEST_PK = process.env.TEST_PK;
const TEST_USER_ADDRESS = process.env.TEST_USER_ADDRESS;
const TEST_REFERRER = process.env.TEST_REFERRER;
class ParaSwapper {
constructor(){
this.web3Provider = new Web3(PROVIDER_URL);
this.paraSwap = new ParaSwap();
}
async getTokens(){
return this.paraSwap.getTokens();
}
async getRate(from, to, amount){
return this.paraSwap.getRate(from.address, to.address, amount);
}
async buildSwap(from, to, srcAmount, minAmount, priceRoute) {
return this.paraSwap.buildTx(from.address, to.address, srcAmount, minAmount, priceRoute, TEST_USER_ADDRESS, TEST_REFERRER);
}
async swap(txParams){
try {
const provider = new Web3(new HDWalletProvider(TEST_PK, PROVIDER_URL));
const result = await provider.eth.sendTransaction(txParams, async (err, transactionHash) => {
if (err) {
return console.error('Tx_Error', txParams.from, err);
}
console.log(txParams.from, `https://etherscan.io/tx/${transactionHash}`)
});
console.log('result', result);
} catch (e) {
console.error('Tx_Error', txParams.from, e);
}
}
amountOf(amount, token){
return new BN(amount).times(10 ** token.decimals).toFixed(0);
}
rateOf(amount, token){
return new BN(amount).dividedBy(10 ** token.decimals).toFixed(9);
}
display(_amount, from, to, priceRoute) {
return `rate of ${_amount} ${from.symbol} = ${this.rateOf(priceRoute.amount, to)} ${to.symbol}`;
}
}
async function swap(_amount, _from, _to, execute) {
const ps = new ParaSwapper();
const tokens = await ps.getTokens();
const from = tokens.find(({symbol}) => symbol === _from);
const to = tokens.find(({symbol}) => symbol === _to);
const amount = ps.amountOf(_amount, from);
const priceRoute = await ps.getRate(from, to, amount);
console.log("swap", ps.display(_amount, from, to, priceRoute));
const minAmount = new BN(priceRoute.amount).times(0.995).toFixed(0);
const transaction = await ps.buildSwap(from, to, amount, minAmount, priceRoute);
console.log(transaction);
if(execute) ps.swap(transaction);
}
async function multiSwap(_amount, _tokens, execute) {
const ps = new ParaSwapper();
const tokens = await ps.getTokens();
const from = tokens.find(({symbol}) => symbol === _tokens[0]);
const to = tokens.find(({symbol}) => symbol === _tokens[3]);
const amount = ps.amountOf(_amount, from);
const priceRoute = await ps.paraSwap.getRateByRoute(_tokens, amount);
console.log("multiSwqap", ps.display(_amount, from, to, priceRoute));
if(new BN(priceRoute.amount).isLessThanOrEqualTo(amount)){
return console.log("No profit :(");
}
const minAmount = new BN(priceRoute.amount).times(0.995).toFixed(0);
const transaction = await ps.buildSwap(from, to, amount, minAmount, priceRoute);
console.log(transaction);
if(execute) ps.swap(transaction);
}
//multiSwap(0.05, ["ETH", "DAI", "USDC", "ETH"]);
//swap(0.05, "ETH", "DAI");