Skip to content

Commit 64a5ccb

Browse files
committed
add feed-oracle_price, trim down symbol to reduce execution cost
1 parent 26b0d9a commit 64a5ccb

9 files changed

+134
-69
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Will probably add a CoinGecko feed next with STX, STX-BTC, CoinMarketCap does no
7474

7575
Suggestions welcome!
7676

77-
If you'd like to contribute, open a PR, and small STX donations are welcome to `SPZ0RAC1EFTH949T4W2SYY6YBHJRMAF4ECT5A7DD` to help pay for keeping the feed alive (unless I figure out a way to use microblocks, at the current block frequency, it will cost less than 1 STX per day, at least I think)
77+
If you'd like to contribute, open a PR, and small STX donations are welcome to `SPZ0RAC1EFTH949T4W2SYY6YBHJRMAF4ECT5A7DD` to help pay for keeping the feed alive (unless I figure out a way to use microblocks), at the current block frequency, it will cost less than 1 STX per day, at least I think, for 14 data points, 26 overflows the block, which is an other topic for an other day)
7878

7979
## Credits
8080
Extra credits to @jcnelson for sharing an approach to manipulate buffers (https://gist.github.com/jcnelson/76c44b4209c29a19d2dbc06a0e7b446e)

check-prices.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ await checkPrice('artifix-okcoin', 'TEST')
1515

1616
await checkPrice('coinbase', 'BTC')
1717
await checkPrice('coinbase', 'ETH')
18-
await checkPrice('coinbase', 'LINK')
19-
await checkPrice('coinbase', 'COMP')
18+
// await checkPrice('coinbase', 'LINK')
19+
// await checkPrice('coinbase', 'COMP')
2020
await checkPrice('coinbase', 'UNI')
2121
await checkPrice('coinbase', 'SNX')
2222

@@ -25,20 +25,20 @@ await checkPrice('okcoin', 'ETH')
2525

2626
await checkPrice('artifix-okcoin', 'BTC')
2727
await checkPrice('artifix-okcoin', 'ETH')
28-
await checkPrice('artifix-okcoin', 'LINK')
28+
// await checkPrice('artifix-okcoin', 'LINK')
2929
await checkPrice('artifix-okcoin', 'STX-BTC')
3030
await checkPrice('artifix-okcoin', 'STX')
31-
await checkPrice('artifix-okcoin', 'COMP')
32-
await checkPrice('artifix-okcoin', 'LTC')
31+
// await checkPrice('artifix-okcoin', 'COMP')
32+
// await checkPrice('artifix-okcoin', 'LTC')
3333
await checkPrice('artifix-okcoin', 'UNI')
3434

35-
await checkPrice('artifix-binance', 'ETH-BTC')
36-
await checkPrice('artifix-binance', 'LINK-BTC')
37-
await checkPrice('artifix-binance', 'LINK-ETH')
35+
// await checkPrice('artifix-binance', 'ETH-BTC')
36+
// await checkPrice('artifix-binance', 'LINK-BTC')
37+
// await checkPrice('artifix-binance', 'LINK-ETH')
3838
await checkPrice('artifix-binance', 'STX-BTC')
3939
await checkPrice('artifix-binance', 'STX-USDT')
40-
await checkPrice('artifix-binance', 'COMP-BTC')
41-
await checkPrice('artifix-binance', 'LTC-BTC')
40+
// await checkPrice('artifix-binance', 'COMP-BTC')
41+
// await checkPrice('artifix-binance', 'LTC-BTC')
4242
await checkPrice('artifix-binance', 'UNI-BTC')
43-
await checkPrice('artifix-binance', 'AAVE-BTC')
44-
await checkPrice('artifix-binance', 'SUSHI-BTC')
43+
// await checkPrice('artifix-binance', 'AAVE-BTC')
44+
// await checkPrice('artifix-binance', 'SUSHI-BTC')

feed-oracle-prices.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {
2+
getNonce,
3+
timeout,
4+
} from './src/tx-utils.js'
5+
import {
6+
addPrices,
7+
} from './src/clients/oracle-client-tx.js'
8+
9+
import { retrieveCoinbaseOracleFeed } from './src/feeds/coinbase-oracle.js'
10+
import { retrieveOKCoinOracleFeed } from './src/feeds/okcoin-oracle.js'
11+
import { retrieveBinanceFeed } from './src/feeds/binance.js'
12+
import { retrieveOKCoinFeed } from './src/feeds/okcoin.js'
13+
14+
// TODO(psq): need a way to restore safely, store nonce maybe, and read at the beginning?
15+
16+
let nonce = await getNonce()
17+
while (true) {
18+
const coinbase_oracle_feed = await retrieveCoinbaseOracleFeed()
19+
const okcoin_oracle_feed = await retrieveOKCoinOracleFeed()
20+
const binance_feed = await retrieveBinanceFeed()
21+
const okcoin_feed = await retrieveOKCoinFeed()
22+
23+
const feed = coinbase_oracle_feed.concat(okcoin_oracle_feed.concat(binance_feed.concat(okcoin_feed)))
24+
25+
console.log("feed", feed.length)
26+
const result = await addPrices(feed)
27+
let next_nonce = await getNonce()
28+
while (next_nonce === nonce) {
29+
await timeout(1000 * 60 * 2)
30+
next_nonce = await getNonce()
31+
}
32+
nonce = next_nonce
33+
console.log("new nonce", nonce)
34+
}
35+
36+
37+
// const processed = await processing(result, 0, 25)

src/clients/oracle-client-tx.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export async function deployContract(contract_file) {
6868
)
6969
}
7070
}
71-
const processed = await processing(result, 0)
71+
const processed = await processing(result, 0, 25)
7272
if (!processed) {
7373
throw new Error(`failed to deploy ${CONTRACT_NAME}: transaction not found`)
7474
}
@@ -96,6 +96,9 @@ export async function addPrices(prices) {
9696
functionArgs: [buildPriceList(prices)],
9797
senderKey: ORACLE_SK,
9898
network,
99+
// helpful to recover botched tx, overbig...
100+
// nonce: new BigNum(1),
101+
// fee: new BigNum(20000),
99102
postConditionMode: PostConditionMode.Allow,
100103
postConditions: [
101104
],
@@ -110,10 +113,7 @@ export async function addPrices(prices) {
110113
console.log(result.reason)
111114
throw new Error(`transaction failed`)
112115
}
113-
const processed = await processing(result, 0)
114-
if (!processed) {
115-
throw new Error(`failed to execute add-prices`)
116-
}
116+
return result
117117
}
118118

119119
export async function getPrice(source, symbol) {
@@ -180,7 +180,7 @@ export async function addSource(source, key) {
180180
console.log(result.reason)
181181
throw new Error(`transaction failed`)
182182
}
183-
const processed = await processing(result, 0)
183+
const processed = await processing(result, 0, 25)
184184
if (!processed) {
185185
throw new Error(`failed to execute add-prices`)
186186
}
@@ -208,7 +208,7 @@ export async function revokeSource(source) {
208208
console.log(result.reason)
209209
throw new Error(`transaction failed`)
210210
}
211-
const processed = await processing(result, 0)
211+
const processed = await processing(result, 0, 25)
212212
if (!processed) {
213213
throw new Error(`failed to execute add-prices`)
214214
}

src/feeds/binance.js

+28-28
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ export async function retrieveBinanceFeed() {
1616
const timestamp = Math.floor(Date.now() / 1000)
1717

1818
const filter = {
19-
'ETHBTC': {
20-
symbol: 'ETH-BTC',
21-
decimals: 100_000_000,
22-
},
23-
'LINKBTC': {
24-
symbol: 'LINK-BTC',
25-
decimals: 100_000_000,
26-
},
27-
'LINKETH': {
28-
symbol: 'LINK-ETH',
29-
decimals: 1_000_000_000_000_000_000,
30-
},
19+
// 'ETHBTC': {
20+
// symbol: 'ETH-BTC',
21+
// decimals: 100_000_000,
22+
// },
23+
// 'LINKBTC': {
24+
// symbol: 'LINK-BTC',
25+
// decimals: 100_000_000,
26+
// },
27+
// 'LINKETH': {
28+
// symbol: 'LINK-ETH',
29+
// decimals: 1_000_000_000_000_000_000,
30+
// },
3131
'STXBTC': {
3232
symbol: 'STX-BTC',
3333
decimals: 100_000_000,
@@ -36,26 +36,26 @@ export async function retrieveBinanceFeed() {
3636
symbol: 'STX-USDT',
3737
decimals: 1_000_000,
3838
},
39-
'COMPBTC': {
40-
symbol: 'COMP-BTC',
41-
decimals: 100_000_000,
42-
},
43-
'LTCBTC': {
44-
symbol: 'LTC-BTC',
45-
decimals: 100_000_000,
46-
},
39+
// 'COMPBTC': {
40+
// symbol: 'COMP-BTC',
41+
// decimals: 100_000_000,
42+
// },
43+
// 'LTCBTC': {
44+
// symbol: 'LTC-BTC',
45+
// decimals: 100_000_000,
46+
// },
4747
'UNIBTC': {
4848
symbol: 'UNI-BTC',
4949
decimals: 100_000_000,
5050
},
51-
'AAVEBTC': {
52-
symbol: 'AAVE-BTC',
53-
decimals: 100_000_000,
54-
},
55-
'SUSHIBTC': {
56-
symbol: 'SUSHI-BTC',
57-
decimals: 100_000_000,
58-
},
51+
// 'AAVEBTC': {
52+
// symbol: 'AAVE-BTC',
53+
// decimals: 100_000_000,
54+
// },
55+
// 'SUSHIBTC': {
56+
// symbol: 'SUSHI-BTC',
57+
// decimals: 100_000_000,
58+
// },
5959
}
6060

6161
const feed = []

src/feeds/coinbase-oracle.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import {
1010
COINBASE_PASSPHRASE,
1111
} from '../config.js'
1212

13-
const filter = ['BTC', 'ETH', 'LINK', 'COMP', 'UNI', 'SNX']
13+
const filter = [
14+
'BTC',
15+
'ETH',
16+
// 'LINK',
17+
// 'COMP',
18+
'UNI',
19+
'SNX',
20+
]
1421

1522
const client = new Client({
1623
'apiKey': COINBASE_KEY,

src/feeds/okcoin.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ export async function retrieveOKCoinFeed() {
102102
symbol: 'ETH',
103103
decimals: 1_000_000,
104104
},
105-
'LINK-USD': {
106-
symbol: 'LINK',
107-
decimals: 1_000_000,
108-
},
105+
// 'LINK-USD': {
106+
// symbol: 'LINK',
107+
// decimals: 1_000_000,
108+
// },
109109
'STX-BTC': {
110110
symbol: 'STX-BTC',
111111
decimals: 100_000_000,
@@ -114,14 +114,14 @@ export async function retrieveOKCoinFeed() {
114114
symbol: 'STX',
115115
decimals: 1_000_000,
116116
},
117-
'COMP-USD': {
118-
symbol: 'COMP',
119-
decimals: 1_000_000,
120-
},
121-
'LTC-USD': {
122-
symbol: 'LTC',
123-
decimals: 1_000_000,
124-
},
117+
// // 'COMP-USD': {
118+
// // symbol: 'COMP',
119+
// // decimals: 1_000_000,
120+
// // },
121+
// 'LTC-USD': {
122+
// symbol: 'LTC',
123+
// decimals: 1_000_000,
124+
// },
125125
'UNI-USD': {
126126
symbol: 'UNI',
127127
decimals: 1_000_000,

src/tx-utils.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import {
2+
ORACLE_STX,
23
STACKS_API_URL,
34
} from './config.js'
45

5-
export async function processing(tx, count = 0) {
6+
export async function processing(tx, count, max) {
67
console.log("processing", tx, count)
7-
var result = await fetch(
8+
const result = await fetch(
89
`${STACKS_API_URL}/extended/v1/tx/${tx}`
910
)
10-
var value = await result.json()
11+
const value = await result.json()
1112
if (value.tx_status === "success") {
1213
console.log(`transaction ${tx} processed`)
1314
// console.log(value)
@@ -16,15 +17,25 @@ export async function processing(tx, count = 0) {
1617
// if (value.tx_status === "pending") {
1718
// console.log("pending" /*, value*/)
1819
// }
19-
if (count > 10) {
20-
console.log("failed after 2 attempts", value)
20+
if (count > max) {
21+
console.log(`failed after ${max} attempts`, value)
2122
return false
2223
}
2324

2425
await timeout(30000)
25-
return processing(tx, count + 1)
26+
return processing(tx, count + 1, max)
2627
}
2728

28-
function timeout(ms) {
29+
export function timeout(ms) {
2930
return new Promise((resolve) => setTimeout(resolve, ms))
3031
}
32+
33+
export async function getNonce() {
34+
console.log("getNonce for", ORACLE_STX)
35+
const result = await fetch(
36+
`${STACKS_API_URL}/v2/accounts/${ORACLE_STX}?proof=0`
37+
)
38+
const value = await result.json()
39+
console.log("value", value)
40+
41+
}

update-prices.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import {
2+
processing,
3+
} from './src/tx-utils.js'
14
import {
25
addPrices,
36
} from './src/clients/oracle-client-tx.js'
@@ -14,4 +17,11 @@ const okcoin_feed = await retrieveOKCoinFeed()
1417

1518
const feed = coinbase_oracle_feed.concat(okcoin_oracle_feed.concat(binance_feed.concat(okcoin_feed)))
1619

17-
await addPrices(feed)
20+
console.log("feed", feed.length)
21+
const result = await addPrices(feed)
22+
23+
const processed = await processing(result, 0, 25)
24+
if (!processed) {
25+
console.log(`failed to execute addPrices after over 12 minutes`)
26+
}
27+

0 commit comments

Comments
 (0)