-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadd-rate.js
73 lines (60 loc) · 1.72 KB
/
add-rate.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
const Dash = require('dash');
const axios = require('axios')
const secrets = require('./secrets.js');
const clientOpts = {
network: 'evonet',
apps: {
ratesContract: {
contractId: 'CeSNCJDJD5qHziBjSGWfvsd8LLeeBzrSrYpvYN5e3Qoy'
}
},
wallet: {
mnemonic: secrets.mnemonic
}
};
console.log(clientOpts);
const client = new Dash.Client(clientOpts);
let identity;
const submitRateDocument = async function () {
const platform = client.platform;
try {
identity = await platform.identities.get('5Qhw9CyVjzdbDi4PhSTYQf4LHvThRmeVVjPnarmToJEt');
let response = await axios.get('https://rates2.dashretail.org/rates?source=dashretail&symbol=dashbtc,dashusd,dasheur,dashgbp,dashves,dashngn,dashthb')
//console.log('got api response:', response);
/*
// sample data
const docProperties = {
"baseCurrency": "DASH",
"price": 91.0243,
"quoteCurrency": "USD",
"retrieved": "2020-08-19T13:24:37.433863587Z",
"source": "dashretail-average",
"symbol": "DASHUSD"
}
*/
const asyncRes = await Promise.all(response.data.map(async (i) => {
i.price = parseFloat(i.price);
// Create the rate document
const rateDocument = await platform.documents.create(
'ratesContract.rate',
identity,
//docProperties,
i,
);
return rateDocument;
}));
console.log('results:', asyncRes);
const documentBatch = {
create: asyncRes,
replace: [],
delete: [],
}
// Sign and submit the document(s)
await platform.documents.broadcast(documentBatch, identity);
} catch (e) {
console.error('Something went wrong:', e);
} finally {
client.disconnect();
}
};
submitRateDocument();