Skip to content

Commit 11c05e3

Browse files
committed
DRYer code, tx source add/revoke/check
1 parent 0ffc583 commit 11c05e3

12 files changed

+367
-285
lines changed

.env.sample

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ MODE=mainnet
1212
ORACLE_SK=
1313
ORACLE_PK=
1414
ORACLE_STX=
15+
16+
CONTRACT_NAME=oracle-v1

add-sources.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {
2+
addSource,
3+
} from './src/clients/oracle-client-tx.js'
4+
5+
await addSource('test1', '0x032ac03dbc2d291023459f14b1f94324bd072baf5d3914a83d81edd655dcc2b2ed')
6+
await addSource('test2', '0x032ac03dbc2d291023459f14b1f94324bd072baf5d3914a83d81edd655dcc2b2ed')

check-prices.js

+35-92
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,44 @@
1-
import BigNum from 'bn.js'
2-
import fs from "fs"
3-
import fetch from 'node-fetch'
4-
51
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'
504

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)
627
if (value) {
638
console.log(source, symbol, value.amount, value.height, value.timestamp, new Date(value.timestamp * 1000))
649
} else {
6510
console.log(source, symbol, "no result")
6611
}
6712
}
6813

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')

check-sources.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {
2+
checkSource,
3+
} from './src/clients/oracle-client-tx.js'
4+
5+
const coinbase = await checkSource('coinbase')
6+
const okcoin = await checkSource('okcoin')
7+
const artifix_okcoin = await checkSource('artifix-okcoin')
8+
const artifix_binance = await checkSource('artifix-binance')
9+
const test1 = await checkSource('test1')
10+
const test2 = await checkSource('test2')
11+
12+
console.log("coinbase", coinbase)
13+
console.log("okcoin", okcoin)
14+
console.log("artifix_okcoin", artifix_okcoin)
15+
console.log("artifix_binance", artifix_binance)
16+
console.log("test1", test1)
17+
console.log("test2", test2)
18+

contracts/oracle.clar

+5
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@
204204
err-not-owner
205205
)
206206
)
207+
208+
(define-read-only (check-source (source (string-ascii 16)))
209+
(map-get? sources { source: source})
210+
)
211+
207212
;; preseed with trusted sources
208213
;; need compressed version of public keys
209214
(map-set sources {source: "coinbase"} {public-key: 0x034170a2083dccbc2be253885a8d0e9f7ce859eb370d0c5cae3b6994af4cb9d666}) ;; Eth: 0xfCEAdAFab14d46e20144F48824d0C09B1a03F2BC

deploy-contracts.js

+5-84
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,9 @@
1-
import BigNum from 'bn.js'
2-
import fs from "fs"
3-
import fetch from 'node-fetch'
41
import {
5-
broadcastTransaction,
6-
makeContractDeploy,
7-
} from '@stacks/transactions'
8-
9-
import {
10-
StacksMainnet,
11-
StacksTestnet,
12-
StacksMocknet,
13-
} from '@stacks/network'
14-
15-
import {
16-
MODE,
17-
ORACLE_PK,
18-
ORACLE_SK,
19-
ORACLE_STX,
20-
STACKS_API_URL,
2+
CONTRACT_NAME,
213
} from './src/config.js'
224

23-
console.log("mode", MODE)
24-
console.log("api", STACKS_API_URL)
25-
26-
const network = MODE === 'mainnet' ? new StacksMainnet() : MODE === 'testnet' ? new StacksTestnet() : new StacksMocknet()
27-
network.coreApiUrl = STACKS_API_URL // Is this needed except in case of custom node?
28-
29-
async function deployContract(contract_file, contract_name) {
30-
console.log(`deploying ${contract_name}`)
31-
const body = fs.readFileSync(`./contracts/${contract_file}.clar`).toString()
32-
const codeBody = body
33-
.replace('SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR', ORACLE_STX)
34-
.replace('0367b2946150dfab1862457da80beb522440be5737ea51ba14cf8018a12911128f', ORACLE_PK)
35-
.replace('ST31HHVBKYCYQQJ5AQ25ZHA6W2A548ZADDQ6S16GP', ORACLE_STX)
36-
37-
const transaction = await makeContractDeploy({
38-
contractName: contract_name,
39-
codeBody,
40-
senderKey: ORACLE_SK,
41-
network,
42-
})
43-
44-
const result = await broadcastTransaction(transaction, network)
45-
if (result.error) {
46-
if (result.reason === "ContractAlreadyExists") {
47-
console.log(`${contract_name} already deployed`)
48-
return result
49-
} else {
50-
throw new Error(
51-
`failed to deploy ${contract_name}: ${JSON.stringify(result)}`
52-
)
53-
}
54-
}
55-
const processed = await processing(result, 0)
56-
if (!processed) {
57-
throw new Error(`failed to deploy ${contract_name}: transaction not found`)
58-
}
59-
return result
60-
}
61-
62-
function timeout(ms) {
63-
return new Promise((resolve) => setTimeout(resolve, ms))
64-
}
65-
66-
async function processing(tx, count) {
67-
console.log("processing", tx)
68-
var result = await fetch(
69-
`${STACKS_API_URL}/extended/v1/tx/${tx}`
70-
)
71-
var value = await result.json()
72-
if (value.tx_status === "success") {
73-
console.log(`transaction ${tx} processed`)
74-
// console.log(value)
75-
return true
76-
}
77-
if (count > 20) {
78-
console.log("failed after 20 attempts", value)
79-
return false
80-
}
81-
82-
await timeout(30000)
83-
return processing(tx, count + 1)
84-
}
5+
import {
6+
deployContract,
7+
} from './src/clients/oracle-client-tx.js'
858

86-
(async () => {
87-
await deployContract('oracle', 'oracle-v0-0-2')
88-
})()
9+
await deployContract('oracle', CONTRACT_NAME)

revoke-source.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {
2+
revokeSource,
3+
} from './src/clients/oracle-client-tx.js'
4+
5+
await revokeSource('test1')

0 commit comments

Comments
 (0)