-
Notifications
You must be signed in to change notification settings - Fork 3
/
update-price.js
261 lines (236 loc) · 9.02 KB
/
update-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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
require('dotenv').config();
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
const CONTRACT_NAME = 'arkadiko-oracle-v1-1';
const FUNCTION_NAME = 'update-price';
const rp = require('request-promise');
const tx = require('@stacks/transactions');
const BN = require('bn.js');
const utils = require('./utils');
const network = utils.resolveNetwork();
const getPrice = async (symbol) => {
const fetchedPrice = await tx.callReadOnlyFunction({
contractAddress: CONTRACT_ADDRESS,
contractName: "arkadiko-oracle-v1-1",
functionName: "get-price",
functionArgs: [tx.stringAsciiCV(symbol || 'STX')],
senderAddress: CONTRACT_ADDRESS,
network: network,
});
const json = tx.cvToJSON(fetchedPrice);
return json.value['last-price'].value;
};
const fetchUsdaPrice = async () => {
let details = await tx.callReadOnlyFunction({
contractAddress: 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9',
contractName: 'amm-swap-pool',
functionName: 'get-oracle-instant',
functionArgs: [
tx.contractPrincipalCV('SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9', 'token-wusda'),
tx.contractPrincipalCV('SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9', 'token-wxusd'),
tx.uintCV(500000)
],
senderAddress: CONTRACT_ADDRESS,
network: network,
});
return tx.cvToJSON(details);
};
const setPrice = async (stxPrice, btcPrice, atAlexPrice) => {
let nonce = await utils.getNonce('SP17BSF329AQEY7YA3CWQHN3KGQYTYYP7208CQH4G');
const priceWithDecimals = stxPrice.toFixed(4) * 1000000;
const txOptions = {
contractAddress: CONTRACT_ADDRESS,
contractName: CONTRACT_NAME,
functionName: FUNCTION_NAME,
functionArgs: [tx.stringAsciiCV('STX'), tx.uintCV(new BN(priceWithDecimals)), tx.uintCV(1000000)],
senderKey: process.env.STACKS_PRIVATE_KEY,
nonce: new BN(nonce),
postConditionMode: 1,
network
};
const transaction = await tx.makeContractCall(txOptions);
const result = tx.broadcastTransaction(transaction, network);
await utils.processing(result, transaction.txid(), 0);
const xTxOptions = {
contractAddress: CONTRACT_ADDRESS,
contractName: CONTRACT_NAME,
functionName: FUNCTION_NAME,
functionArgs: [tx.stringAsciiCV('xSTX'), tx.uintCV(new BN(priceWithDecimals)), tx.uintCV(1000000)],
senderKey: process.env.STACKS_PRIVATE_KEY,
nonce: new BN(nonce + 1),
fee: new BN(10000, 10),
postConditionMode: 1,
network
};
const transaction2 = await tx.makeContractCall(xTxOptions);
const result2 = tx.broadcastTransaction(transaction2, network);
await utils.processing(result2, transaction2.txid(), 0);
const xBtcTxOptions = {
contractAddress: CONTRACT_ADDRESS,
contractName: CONTRACT_NAME,
functionName: FUNCTION_NAME,
functionArgs: [tx.stringAsciiCV('xBTC'), tx.uintCV(new BN(btcPrice.toFixed(0) * 1000000)), tx.uintCV(100000000)],
senderKey: process.env.STACKS_PRIVATE_KEY,
nonce: new BN(nonce + 2),
fee: new BN(10000, 10),
postConditionMode: 1,
network
};
const transaction3 = await tx.makeContractCall(xBtcTxOptions);
const result3 = tx.broadcastTransaction(transaction3, network);
await utils.processing(result3, transaction3.txid(), 0);
const fetchPair = async () => {
let details = await tx.callReadOnlyFunction({
contractAddress: CONTRACT_ADDRESS,
contractName: "arkadiko-swap-v2-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);
};
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 dikoTxOptions = {
contractAddress: CONTRACT_ADDRESS,
contractName: CONTRACT_NAME,
functionName: FUNCTION_NAME,
functionArgs: [tx.stringAsciiCV('DIKO'), tx.uintCV(new BN(dikoPrice * 1000000)), tx.uintCV(1000000)],
senderKey: process.env.STACKS_PRIVATE_KEY,
nonce: new BN(nonce + 3),
fee: new BN(10000, 10),
postConditionMode: 1,
network
};
const transaction4 = await tx.makeContractCall(dikoTxOptions);
const result4 = tx.broadcastTransaction(transaction4, network);
await utils.processing(result4, transaction4.txid(), 0);
}
const usdaPriceResult = await fetchUsdaPrice();
if (usdaPriceResult.success) {
const usdaPrice = usdaPriceResult.value.value;
const usdaTxOptions = {
contractAddress: CONTRACT_ADDRESS,
contractName: CONTRACT_NAME,
functionName: FUNCTION_NAME,
functionArgs: [tx.stringAsciiCV('USDA'), tx.uintCV(new BN(usdaPrice)), tx.uintCV(10000000000)],
senderKey: process.env.STACKS_PRIVATE_KEY,
nonce: new BN(nonce + 4),
fee: new BN(10000, 10),
postConditionMode: 1,
network
};
const transaction5 = await tx.makeContractCall(usdaTxOptions);
const result5 = tx.broadcastTransaction(transaction5, network);
await utils.processing(result5, transaction5.txid(), 0);
}
const atAlexTxOptions = {
contractAddress: CONTRACT_ADDRESS,
contractName: CONTRACT_NAME,
functionName: FUNCTION_NAME,
functionArgs: [tx.stringAsciiCV('auto-alex'), tx.uintCV(new BN(atAlexPrice * 100000000)), tx.uintCV(10000000000)],
senderKey: process.env.STACKS_PRIVATE_KEY,
nonce: new BN(nonce + 5),
fee: new BN(10000, 10),
postConditionMode: 1,
network
};
const transaction6 = await tx.makeContractCall(atAlexTxOptions);
const result6 = tx.broadcastTransaction(transaction6, network);
await utils.processing(result6, transaction6.txid(), 0);
const fetchPairStxUsda = async () => {
let details = await tx.callReadOnlyFunction({
contractAddress: CONTRACT_ADDRESS,
contractName: "arkadiko-swap-v2-1",
functionName: "get-pair-details",
functionArgs: [
tx.contractPrincipalCV(CONTRACT_ADDRESS, 'wrapped-stx-token'),
tx.contractPrincipalCV(CONTRACT_ADDRESS, 'usda-token')
],
senderAddress: CONTRACT_ADDRESS,
network: network,
});
return tx.cvToJSON(details);
};
const stxUsda = await fetchPairStxUsda();
if (stxUsda.success) {
const stxUsdaDetails = stxUsda.value.value.value;
const stxAMMPrice = (stxUsdaDetails['balance-y'].value / stxUsdaDetails['balance-x'].value).toFixed(4);
const usdaPrice = (stxPrice/stxAMMPrice).toFixed(4);
const usdaTxOptions = {
contractAddress: CONTRACT_ADDRESS,
contractName: CONTRACT_NAME,
functionName: FUNCTION_NAME,
functionArgs: [tx.stringAsciiCV('STX/USDA'), tx.uintCV(new BN(usdaPrice * 1000000)), tx.uintCV(1000000)],
senderKey: process.env.STACKS_PRIVATE_KEY,
nonce: new BN(nonce + 6),
fee: new BN(10000, 10),
postConditionMode: 1,
network
};
const transaction5 = await tx.makeContractCall(usdaTxOptions);
const result5 = tx.broadcastTransaction(transaction5, network);
await utils.processing(result5, transaction5.txid(), 0);
}
};
const requestOptions = {
method: 'GET',
uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest',
qs: {
'id': '4847,1',
'convert': 'USD'
},
headers: {
'X-CMC_PRO_API_KEY': process.env.CMC_API_KEY
},
json: true,
gzip: true
};
const requestOptions2 = {
method: 'GET',
uri: 'https://laozi1.bandchain.org/api/oracle/v1/request_prices?ask_count=16&min_count=10&symbols=STX&symbols=BTC',
json: true,
gzip: true
}
const requestOptions3 = {
method: 'GET',
uri: 'https://hasura-console.alexlab.co/api/rest/oracle_price/autoalex',
json: true,
gzip: true
}
const getPrices = async () => {
const res = await rp(requestOptions);
const stxPrice = res['data']['4847']['quote']['USD']['price'];
const btcPrice = res['data']['1']['quote']['USD']['price'];
getPrice('STX').then(async (prevPrice) => {
prevPrice = prevPrice / 1000000;
const diff = stxPrice / prevPrice;
if (diff < 1.3 && diff > 0.7) {
getPrice('xBTC').then(async (prevBtcPrice) => {
prevBtcPrice = prevBtcPrice / 1000000;
const btcDiff = btcPrice / prevBtcPrice;
// console.log(bandBtcPrice, prevBtcPrice, btcDiff);
if (btcDiff < 1.3 && btcDiff > 0.7) {
rp(requestOptions3).then(async (res2) => {
const atAlexPrice = res2['laplace_current_token_price']['0']['avg_price_usd'];
const atAlexName = res2['laplace_current_token_price']['0']['token'];
getPrice('auto-alex').then(async (prevAtAlexPrice) => {
prevAtAlexPrice = prevAtAlexPrice / 100000000;
const alexDiff = atAlexPrice / prevAtAlexPrice;
if (atAlexName === 'auto-alex' && alexDiff < 1.3 && alexDiff > 0.7) {
console.log('publishing new STX price', stxPrice, 'and BTC price', btcPrice, 'and atALEX price', atAlexPrice);
await setPrice(stxPrice, btcPrice, atAlexPrice);
}
});
});
}
});
}
});
};
getPrices();