forked from arkadiko-dao/keeper-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape-diko-price.js
60 lines (54 loc) · 1.74 KB
/
scrape-diko-price.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
require('dotenv').config();
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
const CONTRACT_NAME = 'arkadiko-oracle-v1-1';
const tx = require('@stacks/transactions');
const fs = require('fs');
const utils = require('./utils');
const network = utils.resolveNetwork();
const AWS = require('aws-sdk');
const BUCKET_NAME = 'arkadiko-prices';
const axios = require('axios');
const fetchPair = async () => {
let details = await tx.callReadOnlyFunction({
contractAddress: CONTRACT_ADDRESS,
contractName: "arkadiko-swap-v1-1",
functionName: "get-pair-details",
functionArgs: [
tx.contractPrincipalCV(CONTRACT_ADDRESS, 'arkadiko-token'),
tx.contractPrincipalCV(CONTRACT_ADDRESS, 'usda-token')
],
senderAddress: CONTRACT_ADDRESS,
network: network,
});
return tx.cvToJSON(details);
};
(async () => {
const pair = await fetchPair();
if (pair.success) {
const pairDetails = pair.value.value.value;
const dikoPrice = (pairDetails['balance-y'].value / pairDetails['balance-x'].value).toFixed(4);
const timestamp = Date.now();
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_ACCESS_KEY_SECRET
});
const url = 'https://arkadiko-prices.s3.eu-central-1.amazonaws.com/prices.json';
const response = await axios.get(url);
let obj = response.data;
obj[timestamp] = {};
obj[timestamp]['diko-usda'] = dikoPrice;
json = JSON.stringify(obj);
const params = {
ACL: "public-read",
Bucket: BUCKET_NAME,
Key: 'prices.json',
Body: json
};
s3.upload(params, function(err, data) {
if (err) {
throw err;
}
console.log(`File uploaded successfully. ${data.Location}`);
});
}
})();