forked from arkadiko-dao/keeper-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-tvl.js
65 lines (55 loc) · 2.73 KB
/
update-tvl.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
require('dotenv').config();
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
const API_KEY = process.env.API_KEY;
const axios = require('axios');
const tx = require('@stacks/transactions');
const utils = require('./utils');
const network = utils.resolveNetwork();
const tvlUrl = 'https://arkadiko-api.herokuapp.com/api/v1/blockchains/1';
// const tvlUrl = 'http://localhost:3000/api/v1/blockchains/1';
const fetchVaultsTvl = async (prices) => {
let url = `https://stacks-node-api.mainnet.stacks.co/extended/v1/address/SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR.arkadiko-vaults-pool-active-v1-1/balances`;
let response = await fetch(url, { credentials: 'omit' });
let data = await response.json();
let sum = (data['fungible_tokens']['SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR.wstx-token::wstx']['balance'] / 1000000) * (prices['stx'] / 1000000);
sum += (data['fungible_tokens']['SP3DX3H4FEYZJZ586MFBS25ZW3HZDMEW92260R2PR.Wrapped-Bitcoin::wrapped-bitcoin']['balance'] / 100000000) * (prices['xbtc'] / 100000000);
sum = (data['fungible_tokens']['SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.ststx-token::ststx']['balance'] / 1000000) * (1.00506 * prices['stx'] / 1000000);
return sum;
};
const fetchSwapTvl = async (prices) => {
const url = `https://stacks-node-api.mainnet.stacks.co/extended/v1/address/SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR.arkadiko-swap-v2-1/balances`;
const response = await fetch(url, { credentials: 'omit' });
const data = await response.json();
let sum = 0;
sum += (data['stx']['balance'] / 1000000) * (prices['stx'] / 1000000);
sum += (data['fungible_tokens']['SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR.arkadiko-token::diko']['balance'] / 1000000) * (prices['diko'] / 1000000);
sum += (data['fungible_tokens']['SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR.usda-token::usda']['balance'] / 1000000);
sum += (data['fungible_tokens']['SP3DX3H4FEYZJZ586MFBS25ZW3HZDMEW92260R2PR.Wrapped-Bitcoin::wrapped-bitcoin']['balance'] / 100000000) * (prices['xbtc'] / 100000000);
return sum;
};
const fetchPrices = async () => {
const url = 'https://arkadiko-api.herokuapp.com/api/v1/pages/oracle';
const response = await fetch(url, { credentials: 'omit' });
const data = await response.json();
const hsh = {
'stx': data['wstx']['last_price'],
'diko': data['diko']['last_price'],
'usda': data['usda']['last_price'],
'xbtc': data['xbtc']['last_price']
};
return hsh;
};
(async () => {
const prices = await fetchPrices();
const vaultsTvl = await fetchVaultsTvl(prices);
const swapTvl = await fetchSwapTvl(prices);
axios({
method: 'PATCH',
url: tvlUrl + `?key=${API_KEY}`,
data: {
vaults_tvl: vaultsTvl,
swap_tvl: swapTvl
},
headers: { 'Content-Type': 'application/json' },
});
})();