-
Notifications
You must be signed in to change notification settings - Fork 98
/
monitor.js
133 lines (105 loc) · 4.27 KB
/
monitor.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
// Swap Tokens With 0x API
// https://0x.org/docs/guides/swap-tokens-with-0x-api
const fetch = require("node-fetch"); // npm install node-fetch
const qs = require("qs");// npm install qs
function tableHeader(){
//console.log("| DEX Source | Token Pair | DEX Price | Estimated Gas | Gas Price |");
//writeRow("DEX Source");
console.log('\n-----------------------------------------------------------------------------------------------------------------------------------');
writeRow("Buy Token",10);
writeRow("Sell Token",10);
writeRow("UniswapV2", 30);
writeRow("SushiSwap", 30);
writeRow("Difference");
writeRow("Estimated Gas");
writeRow("Gas Price");
console.log('\n-----------------------------------------------------------------------------------------------------------------------------------');
}
async function writeRow(value, size = 15){
let fill = 0;
process.stdout.write("|");
process.stdout.write(value);
fill = size - value.length;
for (var i = 0; i < fill; i++) {
process.stdout.write(" ");
}
//process.stdout.write("|");
}
async function start(_buyToken,_sellToken, _DEX1, _DEX2) {
const params1 = {
buyToken: _buyToken, //WETH
sellToken: _sellToken, //DAI
buyAmount: '1000000000000000000000',
includedSources: _DEX1, // 'Uniswap',
}
const response1 = await fetch(`https://api.0x.org/swap/v1/quote?${qs.stringify(params1)}`);
//console.log(await response.json());
//console.log(< in JSON at position 0);
var DEX1 = await response1.json();
const params2 = {
buyToken: _buyToken, //WETH
sellToken: _sellToken, //DAI
buyAmount: '1000000000000000000000',
includedSources: _DEX2, // 'Uniswap',
}
const response2 = await fetch(`https://api.0x.org/swap/v1/quote?${qs.stringify(params2)}`);
//console.log(await response.json());
var DEX2 = await response2.json();
// writeRow(params1.includedSources, 20);
writeRow(params1.buyToken, 10);
writeRow(params1.sellToken, 10 );
if (((typeof DEX1["price"]) === 'undefined') || ((typeof DEX2["price"]) === 'undefined')) {
writeRow('N/A', 30);
writeRow('N/A', 30);
writeRow('N/A');
writeRow('N/A');
writeRow('N/A');
}else{
writeRow(DEX1["price"].toString(), 30);
writeRow(DEX2["price"].toString(), 30);
let price1 = parseFloat(DEX1["price"].toString()).toFixed(8);
let price2 = parseFloat(DEX2["price"].toString()).toFixed(8);
// Calculate difference in prices
let rawDiff = (price2 - price1).toFixed(8);
// let rawDiff = price1 - price2; //- parseFloat(DEX2["price"].toString());
writeRow(rawDiff);
// https://0x.org/api
// Send to ethereum with your favorite Web3 Library
// window.web3.eth.sendTransaction(quote, (err, txId) => {
// console.log('Success!', txId);
// });
// https://codepen.io/kimpers/pen/zYqMdxE
writeRow(DEX1["estimatedGas"].toString());
writeRow(DEX1["gasPrice"].toString());
}
console.log(""); //\n
}
async function monitor(){
tableHeader();
//start("WETH", "DAI", "0x");
//start("WETH", "DAI", "Uniswap");
start("WETH", "DAI", "UniswapV2");
//start("WETH", "DAI", "Kyber");
//start("WETH", "DAI", "Bancor");
start("REP", "DAI", "UniswapV2", "SushiSwap");
start("ZRX", "DAI", "UniswapV2", "SushiSwap");
start("USDC", "DAI", "UniswapV2", "SushiSwap");
start("BAT", "DAI", "UniswapV2", "SushiSwap");
start("MKR", "DAI", "UniswapV2", "SushiSwap");
start("WBTC", "DAI", "UniswapV2", "SushiSwap");
start("SNX", "DAI", "UniswapV2", "SushiSwap");
start("SUSD", "DAI", "UniswapV2", "SushiSwap");
start("KNC", "DAI", "UniswapV2", "SushiSwap");
// start("WETH", "DAI", "SushiSwap");
// start("REP", "DAI", "SushiSwap");
// start("ZRX", "DAI", "SushiSwap");
// start("USDC", "DAI", "SushiSwap");
// start("BAT", "DAI", "SushiSwap");
// start("MKR", "DAI", "SushiSwap");
// start("WBTC", "DAI", "SushiSwap");
// start("SNX", "DAI", "SushiSwap");
// start("SUSD", "DAI", "SushiSwap");
// start("KNC", "DAI", "SushiSwap");
}
// Infinite loop
setInterval(monitor, 7000);