Created
July 20, 2020 09:47
-
-
Save tansawit/f89850ae020b3bbb07982b8499daf712 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Obi } = require('@bandprotocol/obi.js'); | |
const BandChain = require('@bandprotocol/bandchain.js'); | |
// BandChain devnet endpoint URL | |
const endpoint = 'http://guanyu-devnet.bandchain.org/rest'; | |
// Mnemonic of the account to make the query from. | |
const mnemonic = | |
'ask jar coast prison educate decide elephant find pigeon truth reason double figure enroll scheme melt soldier damage debris recall brief jeans million essence'; | |
// Request parameters | |
const oracleScriptID = 1; | |
const minCount = 1; | |
const askCount = 2; | |
const gasAmount = 100; | |
const gasLimit = 300000; | |
(async () => { | |
// Instantiating BandChain with REST endpoint | |
const bandchain = new BandChain(endpoint); | |
// Create an instance of OracleScript with the script ID | |
const oracleScript = await bandchain.getOracleScript(oracleScriptID); | |
// Initiate obi object to be used for decoding the result later | |
const obi = new Obi(oracleScript.schema); | |
// Create a new request, which will block into the tx is confirmed | |
try { | |
const requestID = await bandchain.submitRequestTx( | |
oracleScript, | |
{ symbol: 'BTC', multiplier: 10000 }, | |
{ minCount, askCount }, | |
mnemonic, | |
gasAmount, | |
gasLimit | |
); | |
// Get request result | |
const responseStruct = await bandchain.getRequestResult(requestID); | |
const encodedOutput = responseStruct.ResponsePacketData['result']; | |
const decodedOutput = obi.decodeOutput(Buffer.from(encodedOutput, 'base64')); | |
console.log(decodedOutput); | |
} catch (e) { | |
// Something went wrong (e.g. specified time is in the future) | |
console.error('Data request failed with reason: ', e); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment