Skip to content

Instantly share code, notes, and snippets.

@tansawit
Created July 20, 2020 09:47
Show Gist options
  • Save tansawit/f89850ae020b3bbb07982b8499daf712 to your computer and use it in GitHub Desktop.
Save tansawit/f89850ae020b3bbb07982b8499daf712 to your computer and use it in GitHub Desktop.
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