-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest.js
83 lines (76 loc) · 2.8 KB
/
test.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
require("dotenv").config()
const snxData = require('synthetix-data');
const Discord = require("discord.js")
const client = new Discord.Client();
const fetch = require('node-fetch');
const w3utils = require('web3-utils');
client.login(process.env.BOT_TOKEN);
let l2trades = null;
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
client.channels.fetch('871713566225485834').then(c => {
l2trades = c;
getl2Exchanges();
});
})
async function getl2Exchanges() {
try {
const ts = Math.floor(Date.now() / 1e3);
const oneDayAgo = ts - 60*60;
const body = JSON.stringify({
query: `{
synthExchanges(
orderBy:timestamp,
orderDirection:desc,
where:{timestamp_gt: ${oneDayAgo}}
)
{
fromAmount
fromAmountInUSD
fromCurrencyKey
toCurrencyKey
block
timestamp
toAddress
toAmount
toAmountInUSD
feesInUSD
}
}`,
variables: null,
});
const response = await fetch('https://api.thegraph.com/subgraphs/name/killerbyte/optimism-exchanges', {
method: 'POST',
body,
});
const json = await response.json();
const {synthExchanges} = json.data;
synthExchanges.forEach(r => {
try {
console.log("Exchanged " + r.fromAmount + " " + fromBytes32(r.fromCurrencyKey).substring(0, 4) + " to " + r.toAmount + " " + fromBytes32(r.toCurrencyKey).substring(0, 4));
console.log("Exchanged amount in sUSD was:" + r.toAmountInUSD );
const exampleEmbed = new Discord.MessageEmbed();
exampleEmbed.setColor("ff0000");
exampleEmbed.setTitle("New trade");
exampleEmbed.setURL("https://optimistic.etherscan.io/address/" + r.toAddress);
exampleEmbed.addField("Wallet",
'[' + r.toAddress + '](https://optimistic.etherscan.io/address/' + r.toAddress + ')');
exampleEmbed.addField("From",
numberWithCommas((r.fromAmount*1.0 ).toFixed(2)) + " " + fromBytes32(r.fromCurrencyKey).substring(0, 4));
exampleEmbed.addField("To",
numberWithCommas((r.toAmount*1.0 ).toFixed(2)) + " " + fromBytes32(r.toCurrencyKey).substring(0, 4));
exampleEmbed.addField("Value",
numberWithCommas((r.fromAmountInUSD*1.0).toFixed(2)) + " sUSD");
l2trades.send(exampleEmbed);
} catch (e) {
console.log(e);
}
});
} catch (e) {
console.log(e);
}
};
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
const fromBytes32 = key => w3utils.hexToAscii(key);