-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.js
343 lines (309 loc) · 9.87 KB
/
app.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
const { ParaSwap } = require('paraswap');
const convert = require('ethereum-unit-converter')
const Web3 = require('web3');
require('dotenv').config();
const fs = require('fs')
const { GasPriceOracle } = require('gas-price-oracle');
const options = {
chainId: 1,
defaultRpc: 'https://gasstation-mainnet.matic.network/',
timeout: 10000,
defaultFallbackGasPrices: {
instant: 213,
fast: 22,
standard: 17,
low: 11,
},
};
const oracle = new GasPriceOracle(options);
const PROVIDER_URL = process.env.PROVIDER_URL;
const privateKey = process.env.PRIVATE_KEY;
const network = 137;
const paraSwap = new ParaSwap(network, undefined);
const provider = new Web3.providers.HttpProvider(PROVIDER_URL);
const web3 = new Web3(provider);
const LOOP_INTERVAL = 1000;
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
const walletAddress = account.address;
web3.eth.accounts.wallet.add(account);
// The minimum ABI required to get the ERC20 Token balance
const minABI = [
// balanceOf
{
constant: true,
inputs: [{ name: "_owner", type: "address" }],
name: "balanceOf",
outputs: [{ name: "balance", type: "uint256" }],
type: "function",
},
];
const USDC = '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174'; // USDC Polygon
const usdcContract = new web3.eth.Contract(minABI, USDC);
const USDC_DECIMALS = 6;
var usdcBalance = null;
const USDT = '0xc2132D05D31c914a87C6611C10748AEb04B58e8F';
const usdtContract = new web3.eth.Contract(minABI, USDT);
const USDT_DECIMALS = 6;
var usdtBalance = null;
const DAI = '0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063';
const daiContract = new web3.eth.Contract(minABI, DAI);
const DAI_DECIMALS = 18;
var daiBalance = null;
const PUSD = '0x9aF3b7DC29D3C4B1A5731408B6A9656fA7aC3b72';
const PUSD_DECIMALS = 18;
var pusdBalance = null;
const FRAX = '0x104592a158490a9228070e0a8e5343b499e125d0';
const FRAX_DECIMALS = 18;
var fraxBalance = null;
const MUSD = '0xe840b73e5287865eec17d250bfb1536704b43b21';
const MUSD_DECIMALS = 18;
const UST = '0x692597b009d13c4049a947cab2239b7d6517875f';
const UST_DECIMALS = 18;
const amUSDT = '0x60d55f02a771d515e077c9c2403a1ef324885cec';
const amUSDT_DECIMALS = 6;
const amUSDC = '0x1a13f4ca1d028320a707d99520abfefca3998b7f';
const amUSDC_DECIMALS = 6;
const amDAI = '0x27f8d03b3a2196956ed754badc28d73be8830a6e';
const amDAI_DECIMALS = 18;
var cointable = {};
var reserveAsset = null;
var reserveBalance = -100;
var txFinished = true;
var buys = 0;
var increaseNonce = false;
var nonce = 0;
function appendCoinToArb(address, decimals, symbol) {
cointable[address] = [decimals, 0, symbol];
}
function addCoins() {
//appendCoinToArb(USDT, USDT_DECIMALS, 'USDT');
//appendCoinToArb(USDC, USDC_DECIMALS, 'USDC');
//appendCoinToArb(DAI, DAI_DECIMALS, 'DAI');
//appendCoinToArb(UST, UST_DECIMALS, 'UST');
//appendCoinToArb(FRAX, FRAX_DECIMALS, 'FRAX');
appendCoinToArb(amUSDT, amUSDT_DECIMALS, 'amUSDT');
appendCoinToArb(amUSDC, amUSDC_DECIMALS, 'amUSDC');
appendCoinToArb(amDAI, amDAI_DECIMALS, 'amDAI');
//appendCoinToArb(PUSD, PUSD_DECIMALS, 'PUSD');
//appendCoinToArb(MUSD, MUSD_DECIMALS, 'MUSD');
}
async function start() {
addCoins();
var coins = await getAllBalances();
//appendCoinToArb(PUSD, PUSD_DECIMALS, 'PUSD');
console.log(coins);
nonce = await web3.eth.getTransactionCount(walletAddress);
loop();
}
async function getAllBalances() {
for (const [key, value] of Object.entries(cointable)) {
try{
var contract = new web3.eth.Contract(minABI, key);
var balance = await contract.methods.balanceOf(walletAddress).call();
if(cointable[key][0] == 6){
cointable[key][1] = balance - 10**3;
} else {
var bal = balance - (10 ** 9);
var rounded = bal.toLocaleString('fullwide', { useGrouping: false });
cointable[key][1] = rounded;
}
} catch(e){
console.log(`error: Failed to fetch balance of ${key}`);
return null;
}
}
return cointable;
}
async function getRate(reserve, reserveSupply, reserveDecimals, ticker, tickerDecimals){
try{
const priceRoute = await paraSwap.getRate(
reserve,
ticker,
reserveSupply,
walletAddress, //Change
'SELL', // Could not get buy side working
{},
reserveDecimals,
tickerDecimals
);
if(priceRoute.hasOwnProperty('hmac')){
return priceRoute;
} else {
console.log(priceRoute);
return null;
}
} catch(e){
console.log(`error: ${e}`);
}
}
async function buyCoinCallback(err, transactionHash ){
if (err) {
console.log(`Logged error: ${err}`);
if(err == 'Error: Returned error: replacement transaction underpriced'){
increaseNonce=true;
buys = buys+100;
}
//return this.setState({ error: err.toString(), loading: false });
return;
}
console.log(`transactionHash: ${transactionHash}`);
txFinished = true;
}
async function buyCoin(srcCoin, srcamt, destCoin, destamt, priceRoute, slippage){
/*
if(!txFinished){
console.log(`txfinished: ${txFinished}`);
return null
}
*/
const destamtWithSlippage = Math.floor(destamt * (1-slippage));
const txParams = await paraSwap.buildTx(
srcCoin, //srcToken,
destCoin, //destToken,
srcamt, //srcAmount,
destamtWithSlippage,//destAmount,
priceRoute, //priceRoute,
walletAddress, //senderAddress,
'capitalisnn',//referrer,
walletAddress //reciever
);
//console.log(`gasprice trippled: ${txParams.gasPrice * 4}`);
buys = buys + 1;
gas = instantGasPrice; //213500000000
if((txParams.gasPrice*4)<gas){
txParams.gasPrice = gas;
} else {
txParams.gasPrice = txParams.gasPrice*4;
}
if(increaseNonce){
increaseNonce = false;
nonce = await web3.eth.getTransactionCount(walletAddress);
nonce = nonce;
txParams['nonce'] = nonce;
}
console.log('txParams:');
console.log(txParams);
console.log('cointable:');
console.log(cointable);
try{
var txResult = web3.eth.sendTransaction(txParams, buyCoinCallback);
txFinished = false;
return txResult;
} catch (e) {
console.log(`SELL reverted reason: ${e}`);
console.log(priceRoute); // debug
return null
}
}
async function buyCheap(reserveAsset, reserveBalance) {
if(cointable[reserveAsset][2] == undefined){
return;
}
for (const [key, value] of Object.entries(cointable)) {
if(key == reserveAsset){
continue;
}
try{
var rate = await getRate(reserveAsset, reserveBalance, cointable[reserveAsset][0], key, cointable[key][0]);
const spent = rate.srcAmount / (10**cointable[reserveAsset][0]);
const bought = rate.destAmount / (10**cointable[key][0]);
const price = bought/spent; // We want more bought than spent
var currentdate = new Date();
var datetime = currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
console.log(`${datetime}| Calculated ${cointable[reserveAsset][2]}/${cointable[key][2]} price ${price}`);
var target = 1.00002;
if(price > target){
var profitMargin = price - target;
var slippage = profitMargin * 0.95;
console.log(`SELL @ ${price}`);
console.log(`slippage tolerance: ${slippage}`);
var result = buyCoin(rate.srcToken, rate.srcAmount, rate.destToken, rate.destAmount, rate, slippage);
const checkIfItsDone = async () => {
result
.then(ok => {})
.catch(err => {
console.log(err);
})
}
checkIfItsDone();
}
var content = `${Math.floor(currentdate.getTime()/1000)},${target},TARGET\n${Math.floor(currentdate.getTime()/1000)},${price},${cointable[reserveAsset][2]}/${cointable[key][2]}\n`;
fs.appendFile(`pricelog.csv`, content, err => {
if (err) {
console.error(err)
return
}
//done!
})
} catch(e) {
console.log(`failed to get ${cointable[reserveAsset][2]}/${cointable[key][2]} price`)
console.log(e);
}
}
}
var instantGasPrice = 0;
async function getGasPrice(){
var gasPrices = await oracle.gasPrices();
var instant = gasPrices['instant'];
var propriataryGasPrice = instant + 40 + 1.337;
instantGasPrice = convert(propriataryGasPrice, 'gwei', 'wei');
return propriataryGasPrice;
}
async function loop() {
/*
if(!txFinished){
setTimeout(loop, 2000);
return;
}
*/
getGasPrice();
try{
var coins = await getAllBalances();
if(coins == null){
throw(`couldn't fetch balances`);
}
for (const [key, value] of Object.entries(coins)) {
var bal = parseInt(value[1]);
if(cointable[key][0] == 18 ){
bal = convert(bal, 'wei', 'ether');
}
if(cointable[key][0] == 6){
bal = convert(bal, 'szabo', 'ether');
}
var resBal = 0;
if(reserveAsset != undefined && cointable[reserveAsset][0] == 18){
resBal = convert(reserveBalance, 'wei', 'ether');
}
if(reserveAsset != undefined && cointable[reserveAsset][0] == 6){
resBal = convert(reserveBalance, 'szabo', 'ether');
}
if(bal > resBal) {
if(value[1] < 0){
break;
}
reserveAsset = key;
reserveBalance = value[1];
console.log(`Reserve: ${cointable[key][2]} Balalance: ${value[1]}`);
}
}
if(reserveAsset == undefined|| reserveBalance == undefined || cointable[reserveAsset][2] == undefined || reserveBalance <= (10**7)){
console.log(`error: Reserve asset balance low! ${reserveBalance}`);
console.log(cointable)
setTimeout(loop, 500);
return;
}
await buyCheap(reserveAsset, reserveBalance);
} catch(e) {
console.log(e);
console.log(cointable);
await getAllBalances();
}
setTimeout(loop, 500);
return;
}
start();