-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
346 lines (307 loc) · 15.6 KB
/
server.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
344
345
346
const express = require("express"); // defacto server framework for NodeJS
const cors = require("cors"); // allows other sites to visit our endpoints
require("dotenv").config(); // for process.env to work
const axios = require("axios"); // make API requests (for telegram)
const ccxt = require("ccxt"); // library of unified CEX APIs
const { google } = require("googleapis"); // library to connect to google services
// web3
const { ethers } = require("ethers"); // library of Ethereum JSON-RPC API endpoints
const { matchaTargetAddresses, matchaEndpoints, rpcUrls } = require("./constants/constants"); // needed constants for swapping on matcha
const ERC20ABI = require("./ERC20ABI.json"); // ABI of ERC20 contract
const qs = require("qs"); // to combine params into a query string
const app = express();
app.listen(process.env.PORT, () => {
console.log(`listening on port ${process.env.PORT}`); // Heroku overrides process.env.PORT with own value, so it won't be 8080
});
app.use(express.json()); // middleware to recognize incoming req.body as a JSON Object
app.use(express.urlencoded({ extended: true })); // middleware to regonize incoming req.body as html or strings/arrays
app.use(cors()); // allows other sites to visit our endpoints
app.post("/", async (req, res) => {
const data = req.body; // "data" = Trading View Alert JSON message
// exits App if wrong password
if (data.password != process.env.TV_PASSWORD) {
console.log("wrong password");
return;
}
var ERROR = false; // initialize global ERROR variable. Search this document to see how "ERROR" is used.
/************ EXECUTE TRADE ************/
try {
// BINANCE TESTNET
if (data.exchange === "binancetest") {
const binance = new ccxt.binance({
enableRateLimit: true,
apiKey: process.env.BINANCE_TESTNET_API,
secret: process.env.BINANCE_TESTNET_SECRET,
});
binance.setSandboxMode(true); // set to testnet
var ticker = data.ticker; // use "ticker" instead of "data.ticker"
var order = await binance.createMarketOrder(ticker, data.side, data.amount); // execute market order and log it
console.log(`${data.side} ${order.filled} ${data.ticker} at ${order.average} (fee: ${order.fees[0].cost} USD)`); // log message
var tokenBalance = await binance.fetchBalance().then((balances) => balances.info.balances.find((i) => i.asset === data.ticker.split("USDT")[0]).free); // get balance of the base token
var uBalance = await binance.fetchBalance().then((balances) => balances.info.balances.find((i) => i.asset === "USDT").free); // get balance of USDT (u is alias for usdt or usd)
}
// BINANCE
if (data.exchange === "binance") {
const binance = new ccxt.binance({
enableRateLimit: true,
apiKey: process.env.BINANCE_API,
secret: process.env.BINANCE_SECRET,
});
var ticker = data.ticker; // use "ticker" instead of "data.ticker"
var order = await binance.createMarketOrder(ticker, data.side, data.amount); // execute market order and log it
console.log(`${data.side} ${order.filled} ${ticker} at ${order.average} (fee: ${order.fees[0].cost} USD)`); // log message
var tokenBalance = await binance.fetchBalance().then((balances) => balances.info.balances.find((i) => i.asset === ticker.split("USDT")[0]).free); // get balance of the base token
var uBalance = await binance.fetchBalance().then((balances) => balances.info.balances.find((i) => i.asset === "USDT").free); // get balance of USDT (u is alias for usdt or usd)
}
// COINBASE
if (data.exchange === "coinbase") {
const coinbase = new ccxt.coinbase({
enableRateLimit: true,
apiKey: process.env.COINBASE_KEY,
secret: process.env.COINBASE_SECRET,
options: { createMarketBuyOrderRequiresPrice: false }, // needed specifically for Coinbase
});
var ticker = data.ticker.slice(0, -3) + "/USD"; // reformat ticker to <token>/USD
const orderReceipt = await coinbase.createMarketOrder(ticker, data.side, data.amount); // execute market order
var order = await coinbase.fetchOrder(orderReceipt.id); // coinbase different to binance, need this extra step
console.log(`${data.side} ${order.filled} ${ticker} at ${order.average} (fee: ${order.fees[0].cost} USD)`); // log message
var tokenBalance = await coinbase.fetchBalance().then((balances) => balances.total[ticker]); // get balance of the base token
var uBalance = await coinbase.fetchBalance().then((balances) => balances.total["USD"]); // get balance of USDT (u is alias for usdt or usd)
}
// MACTHA AGGREGATOR
if (data.exchange === "matcha") {
// create wallet instance
const provider = new ethers.JsonRpcProvider(rpcUrls[data.network]);
const signer = new ethers.Wallet(process.env.MY_PRIVATE_KEY, provider);
// create token contract instances
const sellTokenContract = new ethers.Contract(data.sellToken, ERC20ABI, signer);
const buyTokenContract = new ethers.Contract(data.buyToken, ERC20ABI, signer);
// fetches the "decimals" of the tokens using the Ethereum API
const sellTokenDecimals = Number(await sellTokenContract.decimals());
const buyTokenDecimals = Number(await buyTokenContract.decimals());
// define Matcha API params, see https://0x.org/docs/0x-swap-api/api-references/get-swap-v1-quote
const params = {
sellToken: data.sellToken,
buyToken: data.buyToken,
sellAmount: ethers.parseUnits(data.amount, sellTokenDecimals),
takerAddress: process.env.MY_ADDRESS, // address that will make the trade
slippagePercentage: data.slippage2, // in decimals
};
const headers = { "0x-api-key": process.env.ZEROEX_KEY }; // the "header" is sent on every request to the Matcha API
// this function checks of you have sufficieint approval of the sellToken
const checkApprove = async () => {
const approvedAmount = ethers.formatUnits(await sellTokenContract.allowance(signer.address, matchaTargetAddresses[data.network]), sellTokenDecimals);
if (approvedAmount >= data.amount) {
console.log(`Sufficient tokens approved (approvedAmount = ${approvedAmount})`);
return true;
} else {
console.log("Error: Not enough tokens approved");
return false;
}
};
// this function sets approval of the token to a very big number
const approve = async () => {
console.log("Approving sellToken...");
const txResponse = await sellTokenContract.approve(matchaTargetAddresses[data.network], BigInt("1000000000000000000000000000000"));
const txReceipt = await txResponse.wait();
console.log("Approve hash:", txReceipt.hash);
};
// this function gets a "soft" price. If it does not exceed slippage1, it then proceeeds to get a "firm price", and executes the swap
const getQuoteAndSwap = async () => {
// gets "soft" price from Matcha API
const priceAPIResponse = await fetch(`${endpoints[data.network]}swap/v1/price?${qs.stringify(params)}`, { headers });
const priceAPIObject = await priceAPIResponse.json();
// If "soft" price (compared to "firm price") does not exceed slippage1, then get the "firm" price.
const priceDiff = (priceAPIObject.price - data.price) / data.price;
if (priceDiff <= data.slippage1) {
// Gets the "firm" and "gauranteed" price from the Matcha API. If the "firm price" does not exceed " slippage2 (which is in "params"), then the txn will not go through
const quoteAPIResponse = await fetch(`${matchaEndpoints[data.network]}swap/v1/quote?${qs.stringify(params)}`, { headers });
const quoteAPIObject = await quoteAPIResponse.json();
// send transaction
const txResponse = await signer.sendTransaction({
gasLimit: quoteAPIObject.gas,
gasPrice: quoteAPIObject.gasPrice,
to: quoteAPIObject.to,
data: quoteAPIObject.data,
value: quoteAPIObject.value,
chainId: quoteAPIObject.chainId,
});
const hash = (await txResponse.wait()).hash; // gets the txn hash
// convert sellAmount to human-readable format (should be equal to data.amount)
const sellAmount = ethers.formatUnits(quoteAPIObject.sellAmount, sellTokenDecimals);
// get buyToken amount from hash
const interface = new ethers.Interface(ERC20ABI);
const txReceipt = await provider.getTransactionReceipt(hash);
let amountReceivedBigInt = BigInt(0);
for (const log of txReceipt.logs) {
if (log.address == data.buyToken) {
let parsedLog = interface.parseLog(log); // parsedLog.args[1] = toAddress, parsedLog.args[2] = amount
if (parsedLog.args[1] == process.env.MY_ADDRESS) {
amountReceivedBigInt = amountReceivedBigInt + parsedLog.args[2];
}
}
}
const amountReceived = ethers.formatUnits(amountReceivedBigInt, sellTokenDecimals);
const buyTokenBalance = ethers.formatUnits(await buyTokenContract.balanceOf(signer.address), buyTokenDecimals); // get buyToken balance
const sellTokenBalance = ethers.formatUnits(await sellTokenContract.balanceOf(signer.address), buyTokenDecimals); // get sellToken balance
const fee = ethers.formatUnits(quoteAPIObject.fees.zeroExFee.feeAmount, sellTokenDecimals); // get fee, fee is in buyToken
// return all the above info
return { order: { average: amountReceived / sellAmount, filled: amountReceived, cost: sellAmount }, buyTokenBalance, sellTokenBalance };
} else {
console.log(
`Trade did not execute.
Price difference (${(priceDiff * 100).toFixed(2)}%) exceeded slippage1 (${(data.slippage1 * 100).toFixed(2)}%)
Trading View Alert Price: ${data.price}
Matcha Price: ${priceAPIObject.price}`
);
}
};
// approve and swap
const isApproved = await checkApprove();
if (isApproved) {
var { order, buyTokenBalance, sellTokenBalance } = await getQuoteAndSwap();
} else {
await approve();
var { order, buyTokenBalance, sellTokenBalance } = await getQuoteAndSwap();
}
}
} catch (e) {
ERROR = e.message;
console.log("transaction failed", ERROR);
}
/************ LOG TO GOOGLE SHEETS ************/
try {
const auth = new google.auth.GoogleAuth(
process.env.DYNO // this is true in Heroku
? {
scopes: "https://www.googleapis.com/auth/spreadsheets",
keyFile: "googlekey.json",
}
: { scopes: "https://www.googleapis.com/auth/spreadsheets" } // if google cloude does not need keyFile
);
const authClientObject = await auth.getClient(); // create the authenticated client
const googleSheetsInstance = google.sheets({ version: "v4", auth: authClientObject }); // create an instance using the client
// define requests, depending on cex or dex. "requests" is an array of actions. We have 2 actions: 1) insert a blank row and 2) write to the cells
if (data.exchange === "matcha") {
var requests = [
{
insertRange: {
range: {
sheetId: 0,
startRowIndex: 1,
endRowIndex: 2,
startColumnIndex: 0,
endColumnIndex: 20,
},
shiftDimension: "ROWS",
},
},
{
updateCells: {
range: {
sheetId: 0,
startRowIndex: 1,
endRowIndex: 2,
startColumnIndex: 0,
endColumnIndex: 10,
},
rows: [
{
values: [
{ userEnteredValue: { stringValue: new Date().toLocaleString() } },
{ userEnteredValue: { stringValue: data.ticker } },
{ userEnteredValue: { stringValue: data.side } },
{ userEnteredValue: { numberValue: ERROR ? data.amount : order.filled } },
{ userEnteredValue: { numberValue: ERROR ? 0 : order.average } },
{ userEnteredValue: { numberValue: ERROR ? 0 : order.cost } },
{ userEnteredValue: { numberValue: ERROR ? 0 : buyTokenBalance } },
{ userEnteredValue: { numberValue: ERROR ? 0 : sellTokenBalance } },
{ userEnteredValue: { stringValue: data.alertName } },
{ userEnteredValue: { stringValue: ERROR ? "FAILED: " + ERROR : "" } },
],
},
],
fields: "userEnteredValue",
},
},
];
} else {
var requests = [
{
insertRange: {
range: {
sheetId: 0,
startRowIndex: 1,
endRowIndex: 2,
startColumnIndex: 0,
endColumnIndex: 20,
},
shiftDimension: "ROWS",
},
},
{
updateCells: {
range: {
sheetId: 0,
startRowIndex: 1,
endRowIndex: 2,
startColumnIndex: 0,
endColumnIndex: 10,
},
rows: [
{
values: [
{ userEnteredValue: { stringValue: new Date().toLocaleString() } },
{ userEnteredValue: { stringValue: ticker } },
{ userEnteredValue: { stringValue: data.side } },
{ userEnteredValue: { numberValue: ERROR ? data.amount : order.filled } },
{ userEnteredValue: { numberValue: ERROR ? 0 : order.average } },
{ userEnteredValue: { numberValue: ERROR ? 0 : order.cost } },
{ userEnteredValue: { numberValue: ERROR ? 0 : tokenBalance } },
{ userEnteredValue: { numberValue: ERROR ? 0 : uBalance } },
{ userEnteredValue: { stringValue: data.alertName } },
{ userEnteredValue: { stringValue: ERROR ? "FAILED: " + ERROR : "" } },
],
},
],
fields: "userEnteredValue",
},
},
];
}
// write to google sheet
await googleSheetsInstance.spreadsheets.batchUpdate({
auth,
spreadsheetId: process.env.GOOGLE_SHEET_ID,
resource: {
requests: requests,
},
});
} catch (e) {
console.log("failed logging ot google sheets", e.message);
}
/************ SEND TELEGRAM NOTIFICATION ************/
try {
// define text
if (data.exchange === "matcha") {
if (ERROR) {
var text = `FAILED TRANSACTION \nReason: ${ERROR} \nTrading View alert: ${data.exchange} ${data.side} ${data.amount} ${ticker} ${data.alertName}`;
} else {
var text = `Transaction completed on ${data.exchange}. ${order.filled} ${ticker} ${data.side} at ${order.average} \nTotal cost: ${order.cost}) \nBuyToken Balance: ${buyTokenBalance} \nSellToken Balance: ${sellTokenBalance}`;
}
} else {
if (ERROR) {
var text = `FAILED TRANSACTION \nReason: ${ERROR} \nTrading View alert: ${data.exchange} ${data.side} ${data.amount} ${ticker} ${data.alertName}`;
} else {
var text = `Transaction completed on ${data.exchange}. ${order.filled} ${ticker} ${data.side} at ${order.average} \nTotal cost: ${order.cost}) \nToken balance: ${tokenBalance} \nUSDT/USD balance: ${uBalance}`;
}
}
// send text
axios.post(`https://api.telegram.org/bot${process.env.TG_BOT_TOKEN}/sendMessage`, {
chat_id: process.env.TG_CHAT_ID,
text: text,
});
} catch (e) {
console.log("failed sending telegram message", e.message);
}
});