-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelphi-oracle.js
88 lines (69 loc) · 2.16 KB
/
delphi-oracle.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
require('./config.js');
const fetch = require('node-fetch');
const { Api, JsonRpc, RpcError } = require('eosjs');
const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig');
function get_api(){
const signatureProvider = new JsSignatureProvider([EOS_KEY]);
const rpc = new JsonRpc( EOS_PROTOCOL + "://" + EOS_HOST + ":" + EOS_PORT, { fetch });
const api = new Api({
rpc,
signatureProvider,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder()
});
return api;
}
async function get_price(currency = 'USD'){
const res = await fetch(`${API_URL}?key=${API_KEY}&ids=EOS&interval=1d&convert=${currency}`);
const data = (await res.json())[0];
if (!data || !data.id){
throw new Error(`Invalid data from API`);
}
if (data.id != 'EOS'){
throw new Error(`Invalid id from API ${data.id}`);
}
const price = parseFloat(data.price);
if (!price || isNaN(price)){
throw new Error(`Invalid price received from API ${data.price}`);
}
return price;
}
async function get_quote_data(currency){
const price = await get_price(currency);
const pair = `eos${currency}`.toLowerCase();
return {"value": parseInt(Math.round(price * 10000)), pair};
}
async function get_action(currencies = ['USD', 'GBP', 'CNY']){
const promises = [];
currencies.forEach((currency) => {
promises.push(get_quote_data(currency));
});
const quotes = await Promise.all(promises);
return {
account: CONTRACT,
name: 'write',
authorization: [{
actor: ORACLE,
permission: ORACLE_PERMISSION
}],
data: {
owner: ORACLE,
quotes
},
};
}
async function write() {
const eos = get_api();
try {
const actions = [ await get_action(CURRENCIES) ];
const res = await eos.transact({
actions
}, { blocksBehind: 3, expireSeconds: 30 });
console.log(`Feed published for ${CURRENCIES.join(', ')} with txid : ${res.transaction_id}`);
}
catch (e){
console.error(`ERROR : ${e.message}`, e);
}
}
write();
setInterval(write, FREQ);