|
1 |
| -import BigNum from 'bn.js' |
2 |
| -import fs from "fs" |
3 |
| -import fetch from 'node-fetch' |
4 |
| - |
5 | 1 | import {
|
6 |
| - serializeCV, |
7 |
| - deserializeCV, |
8 |
| - |
9 |
| - ClarityType, |
10 |
| - |
11 |
| - stringAsciiCV, |
12 |
| -} from '@stacks/transactions' |
13 |
| -import { |
14 |
| - ORACLE_STX, |
15 |
| - STACKS_API_URL, |
16 |
| -} from './src/config.js' |
17 |
| - |
18 |
| -async function getPrice(contract_name, source, symbol) { |
19 |
| - // console.log("getPrice", contract_name, source, symbol) |
20 |
| - const function_name = 'get-price' |
21 |
| - |
22 |
| - const sourceCV = serializeCV(stringAsciiCV(source)) |
23 |
| - const symbolCV = serializeCV(stringAsciiCV(symbol)) |
24 |
| - |
25 |
| - const options = { |
26 |
| - method: 'POST', |
27 |
| - headers: { |
28 |
| - 'Content-Type': 'application/json', |
29 |
| - }, |
30 |
| - body: `{"sender":"${ORACLE_STX}","arguments":["0x${sourceCV.toString("hex")}","0x${symbolCV.toString("hex")}"]}`, |
31 |
| - } |
32 |
| - // console.log("body", options.body) |
33 |
| - const response = await fetch(`${STACKS_API_URL}/v2/contracts/call-read/${ORACLE_STX}/${contract_name}/${function_name}`, options) |
34 |
| - |
35 |
| - if (response.ok) { |
36 |
| - const result = await response.json() |
37 |
| - if (result.okay) { |
38 |
| - const result_value = deserializeCV(Buffer.from(result.result.substr(2), "hex")) |
39 |
| - // console.log("result_value", result_value) |
40 |
| - // console.log("result_value", result_value.value.data) |
41 |
| - if (result_value.type === ClarityType.OptionalSome) { |
42 |
| - return { |
43 |
| - amount: result_value.value.data.amount.value.toString(), // TODO(psq): need decimal information to use toNumber() to avoid overflow (thanks ETH) |
44 |
| - height: result_value.value.data.height.value.toNumber(), |
45 |
| - timestamp: result_value.value.data.timestamp.value.toNumber(), |
46 |
| - } |
47 |
| - } else { |
48 |
| - return null |
49 |
| - } |
| 2 | + getPrice, |
| 3 | +} from './src/clients/oracle-client-tx.js' |
50 | 4 |
|
51 |
| - return result_data.value.value |
52 |
| - } else { |
53 |
| - console.log(result) |
54 |
| - } |
55 |
| - } else { |
56 |
| - console.log("not 200 response", response) |
57 |
| - } |
58 |
| -} |
59 |
| - |
60 |
| -async function checkPrice(contract, source, symbol) { |
61 |
| - const value = await getPrice(contract, source, symbol) |
| 5 | +async function checkPrice(source, symbol) { |
| 6 | + const value = await getPrice(source, symbol) |
62 | 7 | if (value) {
|
63 | 8 | console.log(source, symbol, value.amount, value.height, value.timestamp, new Date(value.timestamp * 1000))
|
64 | 9 | } else {
|
65 | 10 | console.log(source, symbol, "no result")
|
66 | 11 | }
|
67 | 12 | }
|
68 | 13 |
|
69 |
| -const contract = 'oracle-v0-0-2' |
70 |
| -await checkPrice(contract, 'artifix-okcoin', 'TEST') |
71 |
| - |
72 |
| -await checkPrice(contract, 'coinbase', 'BTC') |
73 |
| -await checkPrice(contract, 'coinbase', 'ETH') |
74 |
| -await checkPrice(contract, 'coinbase', 'LINK') |
75 |
| -await checkPrice(contract, 'coinbase', 'COMP') |
76 |
| -await checkPrice(contract, 'coinbase', 'UNI') |
77 |
| -await checkPrice(contract, 'coinbase', 'SNX') |
78 |
| - |
79 |
| -await checkPrice(contract, 'okcoin', 'BTC') |
80 |
| -await checkPrice(contract, 'okcoin', 'ETH') |
81 |
| - |
82 |
| -await checkPrice(contract, 'artifix-okcoin', 'BTC') |
83 |
| -await checkPrice(contract, 'artifix-okcoin', 'ETH') |
84 |
| -await checkPrice(contract, 'artifix-okcoin', 'LINK') |
85 |
| -await checkPrice(contract, 'artifix-okcoin', 'STX-BTC') |
86 |
| -await checkPrice(contract, 'artifix-okcoin', 'STX') |
87 |
| -await checkPrice(contract, 'artifix-okcoin', 'COMP') |
88 |
| -await checkPrice(contract, 'artifix-okcoin', 'LTC') |
89 |
| -await checkPrice(contract, 'artifix-okcoin', 'UNI') |
90 |
| - |
91 |
| -await checkPrice(contract, 'artifix-binance', 'ETH-BTC') |
92 |
| -await checkPrice(contract, 'artifix-binance', 'LINK-BTC') |
93 |
| -await checkPrice(contract, 'artifix-binance', 'LINK-ETH') |
94 |
| -await checkPrice(contract, 'artifix-binance', 'STX-BTC') |
95 |
| -await checkPrice(contract, 'artifix-binance', 'STX-USDT') |
96 |
| -await checkPrice(contract, 'artifix-binance', 'COMP-BTC') |
97 |
| -await checkPrice(contract, 'artifix-binance', 'LTC-BTC') |
98 |
| -await checkPrice(contract, 'artifix-binance', 'UNI-BTC') |
99 |
| -await checkPrice(contract, 'artifix-binance', 'AAVE-BTC') |
100 |
| -await checkPrice(contract, 'artifix-binance', 'SUSHI-BTC') |
101 |
| - |
| 14 | +await checkPrice('artifix-okcoin', 'TEST') |
| 15 | + |
| 16 | +await checkPrice('coinbase', 'BTC') |
| 17 | +await checkPrice('coinbase', 'ETH') |
| 18 | +await checkPrice('coinbase', 'LINK') |
| 19 | +await checkPrice('coinbase', 'COMP') |
| 20 | +await checkPrice('coinbase', 'UNI') |
| 21 | +await checkPrice('coinbase', 'SNX') |
| 22 | + |
| 23 | +await checkPrice('okcoin', 'BTC') |
| 24 | +await checkPrice('okcoin', 'ETH') |
| 25 | + |
| 26 | +await checkPrice('artifix-okcoin', 'BTC') |
| 27 | +await checkPrice('artifix-okcoin', 'ETH') |
| 28 | +await checkPrice('artifix-okcoin', 'LINK') |
| 29 | +await checkPrice('artifix-okcoin', 'STX-BTC') |
| 30 | +await checkPrice('artifix-okcoin', 'STX') |
| 31 | +await checkPrice('artifix-okcoin', 'COMP') |
| 32 | +await checkPrice('artifix-okcoin', 'LTC') |
| 33 | +await checkPrice('artifix-okcoin', 'UNI') |
| 34 | + |
| 35 | +await checkPrice('artifix-binance', 'ETH-BTC') |
| 36 | +await checkPrice('artifix-binance', 'LINK-BTC') |
| 37 | +await checkPrice('artifix-binance', 'LINK-ETH') |
| 38 | +await checkPrice('artifix-binance', 'STX-BTC') |
| 39 | +await checkPrice('artifix-binance', 'STX-USDT') |
| 40 | +await checkPrice('artifix-binance', 'COMP-BTC') |
| 41 | +await checkPrice('artifix-binance', 'LTC-BTC') |
| 42 | +await checkPrice('artifix-binance', 'UNI-BTC') |
| 43 | +await checkPrice('artifix-binance', 'AAVE-BTC') |
| 44 | +await checkPrice('artifix-binance', 'SUSHI-BTC') |
0 commit comments