-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
185 lines (171 loc) · 6.13 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
const express = require('express');
const bp = require('body-parser');
const morgan = require('morgan');
const request = require('request-promise');
const async = require('async');
const currencies = require('./currencies');
const app = express();
const fetch = require("node-fetch");
const {getETHToTokenRate} = require('./testKyberRates');
// middleware
app.set("view engine", "ejs");
app.set("views", __dirname + "/views");
app.use(bp.json());
app.use(morgan('dev'));
app.use(express.static("public"));
app.get('/home', (req, res) => {
res.render('index', {title: 'welcome to slipswap!!'});
})
// app.get('/form', (req, res) => {
// res.redirect('/redirect');
// });
app.get('/:id', async (req, res) => {
var endpoint = process.env.KYBER_API;
let tokenid = `${currencies[req.params.id]}`;
let name = req.params.id;
endpoint += `sell_rate?id=`+tokenid;
// console.log(endpoint);
async function getCoinBaseFeed(name){
let req = await getETHToTokenRate('eth',name,1);
// let info = await req.json();
// var a = parseInt(info.data.rates[name]);
console.log('coinbase',req);
return parseInt(req);
}
// const from = req.query.from;
// const to = req.query.to;
// const amount = parseInt(req.query.amt);
// if(from.toLowerCase() === 'eth') process.env.factor = 1;
// else process.env.factor = await getETHToTokenRate(to, from, 1);
process.env.factor = await getCoinBaseFeed(name);
console.log(process.env.factor);
const originalAmt = parseInt(req.query.amt);
console.log(originalAmt);
const amt = parseInt(req.query.amt / process.env.factor);
console.log(amt);
const rem = req.query.amt % process.env.factor;
var result = await request(process.env.ETHGASSTATION_API);
result = JSON.parse(result);
console.log(result);
var gasPrice = result["safeLow"];
console.log('gasprice', gasPrice);
function createObject(i, amount) {
return callback => {
var start = (i-1)*5 + 1;
const r = Array();
var ep = endpoint;
for(var j = start;j <= Math.min(amount, start+5-1);j++) {
ep += `&qty=${j * process.env.factor}`;
}
console.log('ep',ep);
request(ep, (error, response, body) => {
body = JSON.parse(body);
if(!body.error) {
callback(null, body.data[0]);
} else {
callback(true, {});
}
});
}
}
function createObjectArray(amount) {
var ans = {};
console.log(amount);
// amount = amount / process.env.factor;
var qty = Math.floor((amount+5-1) / 5);
// console.log(amount, qty);
for(var i = 1;i <= qty;i++) {
console.log('i=',i);
ans[i] = createObject(i, amount);
}
console.log('create obj',ans);
return ans;
}
// res.status(200).json(endpoint);
// request(endpoint, (error, response, body) => {
// console.log(error, response, body);
// res.json(JSON.parse(body));
// });
async.parallel(
createObjectArray(parseInt(amt))
,async (err, result) => {
if(err) return;
console.log('parallel', result);
var withoutSplits = Array();
for(var i = 0;i < amt;i++) withoutSplits.push(0);
Object.keys(result).forEach(x => {
var obj = result[x];
console.log('obj',obj);
obj['dst_qty'].forEach((dst, index) => {
// console.log(dst, index);
withoutSplits[obj['src_qty'][index] / process.env.factor -1] = dst * 1000000;
});
})
// return;
// res.json(withoutSplits);
console.log('ws',withoutSplits);
var optimalAns = Number.MIN_VALUE;
var dp = Array();
var dpList = Array();
var seq = Array();
for(var i = 0;i <= amt;i++) dp[i] = 0;
for(var i = 0;i <= amt;i++) seq[i] = 0;
seq[0] = -1;
seq[1] = 1;
dp[1] = withoutSplits[1];
for(var i = 1;i <= amt;i++) {
var maxval = -1;
for(var j = 0;j < i;j++) {
var curr_val = withoutSplits[j] + dp[i-j - 1] - (gasPrice/10);
// console.log(curr_val);
if(curr_val > maxval) {
maxval = curr_val;
seq[i] = j +1;
}
}
dp[i] = maxval;
}
var x = amt;
console.log(seq);
while(x > 0) {
dpList.push(seq[x] * process.env.factor);
x -= seq[x];
}
if(rem != 0){
dpList[0] += rem;
}
let req = await getETHToTokenRate(name,'eth',rem);
console.log('req',typeof req, dp[amt]);
// console.log(d)
dp[amt] += parseFloat(req * 1000000);
req = await getETHToTokenRate(name,'eth',originalAmt);
withoutSplits[amt-1] = parseFloat(req);
console.log(dp);
console.log(withoutSplits[amt-1], dp[amt]);
request('https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD', (error, request, body) => {
if(error) {
console.log(error);
return res.json('error');
}
body = JSON.parse(body);
var usd = parseFloat(body["USD"]);
const amountSaved = (usd * (dp[amt] / 1000000.0 - withoutSplits[amt-1]) ) ;
console.log(usd);
dpList.sort((a,b) => {return b-a});
res.json({
amount: originalAmt,
fromCurrency: name,
toCurrency: "ETH",
optimalPrice: dp[amt] / 1000000.0,
originalPrice: withoutSplits[amt-1],
sequence: dpList,
gasRequired: (dpList.length * (gasPrice / 10)),
gasSpeed: "safeLow",
amountSaved: Math.max(0,amountSaved),
conversionRate: process.env.factor
});
})
})
})
const port = process.env.PORT || 3000 ;
app.listen(port, () => console.log(`listening at port ${port}`));