-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·121 lines (97 loc) · 3.2 KB
/
index.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
#!/usr/bin/env node
const { Api, JsonRpc } = require('eosjs');
const { TextDecoder, TextEncoder } = require('text-encoding');
const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig');
const fetch = require("node-fetch");
const config = require('./config');
const extract_pairs = require('./extract_pairs');
const extract_gateio_pair = require("./extract_gateio_pair");
const rpc = new JsonRpc(config.endpoint, { fetch });
const signatureProvider = new JsSignatureProvider([config.private_key]);
const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });
const required_pairs = ['waxpbtc', 'waxpeth', 'waxpusd'];
const get_pairs_from_delphi_contract = async () => {
try {
const res = await rpc.get_table_rows({
json: true,
code: 'delphioracle',
scope: 'delphioracle',
table: 'pairs'
});
if (res.rows.length) {
const pairs = {};
res.rows.forEach((row) => {
if (row.active) {
pairs[row.name] = row;
}
});
return pairs
}
}
catch (e) {
throw new Error('Error fetching pairs from delphioracle: ' + e.message)
}
};
const get_bittrex_quotes = async () => {
const url = 'https://api.bittrex.com/v3/markets/summaries';
const res = await fetch(url);
const json = await res.json();
const wax_markets = json.filter((q) => {
return q.symbol.includes('WAXP');
});
return wax_markets;
};
const get_gateio_quotes = async () => {
const url = 'https://api.cryptowat.ch/markets/gateio/waxpeth/price';
const res = await fetch(url);
const json = await res.json();
return extract_gateio_pair(json);
};
const push_quotes_to_contract = async (push_quotes) => {
try {
const actions = [{
account: 'delphioracle',
name: 'write',
authorization: [{
actor: config.account,
permission: config.permission
}],
data: {
owner: config.account,
quotes: push_quotes
}
}];
const push_res = await api.transact({
actions
}, {
blocksBehind: 3,
expireSeconds: 30,
});
return push_res;
} catch (e) {
throw new Error('Error pushing pairs to delphioracle contract: ' + e.message)
}
};
const send_quotes = async () => {
try {
const bittrex = await get_bittrex_quotes();
const pairs = await get_pairs_from_delphi_contract();
// { "pair": "waxpbtc", "value": 436 }[],
const quotes = extract_pairs(bittrex, pairs, required_pairs)
const waxpeth = await get_gateio_quotes();
if (waxpeth) {
quotes.push(waxpeth);
}
const response = await push_quotes_to_contract(quotes);
console.log(`Pushed transaction ${response.transaction_id}`);
}
catch (e) {
console.log(e.message)
}
};
const run = async () => {
send_quotes();
const interval = config.interval * 1000 || 60 * 2 * 1000;
setInterval(send_quotes, interval);
};
run();