read.cash Log in
@JonathanSilverblood edited

Beta Price Oracle: Requesting current price

Introduction We at generalprotocols are running a public beta of a Price Oracle that is used for our AnyHedge product. This article will show you how to use the `@generalprotocols/price-oracle` **NPM** library to connect with the price oracle, request the latest price message, verify the message signature and finally parse the price message to get to the price data. https://www.generalprotocols.com/ https://read.cash/@GeneralProtocols/anyhedge-product-overview-0b4e9379 https://www.anyhedge.com Setting up Install the library so that it is available in your project: npm install @generalprotocols/price-oracle Include the library parts you will be using, and choose which oracle service provider to use (our beta is hosted at `oracles.generalprotocols.com`) and which oracle you want to get data for (our beta oracle uses the public key below): // Import the Price Oracle library. const { OracleData, OracleNetwork, OracleProtocol } = require('@generalprotocols/price-oracle'); // Choose which oracle to use, and what service provider to use to get the oracle data. const oracleAddress = 'oracles.generalprotocols.com'; const oraclePublicKey = '0273ee49099f0a09be514cbb45756bf49ad256d0a6de993107e98c89c16b6fa84e'; Requesting the current price To get the most recently issued price message, first form a request structure, then send the request to the oracle service provider: // Create a request for the most recently issued price message, for the given oracle. const requestString = { action: 'latest', public_key: oraclePublicKey }; // Send the request to the oracle service provider, indexing the given oracle data. const response = await OracleNetwork.request(requestString, oracleAddress); *The response is an object that looks like this:* message: <Buffer df 77 00 00 42 e1 09 00 00 00 00 00 00 00 00 00 ...>, public_key: <Buffer 02 73 ee 49 09 9f 0a 09 be 51 4c bb 45 75 6b f4 ...>, signature: <Buffer 31 9f ee dd 7a 19 45 57 dc 3b c0 1d 7f 74 c4 92 ...> Validating message signature When you have the response, you should validate that it is signed by the oracle that you requested. Simply call the library function and pass in the message and signature you got in the response, but use the same oracle public key as before so you can be sure the data provided is for the oracle you expect: // Verify that the signature provided matches the oracle message and public key. const validity = await OracleData.verifyMessageSignature(response.message, response.signature, Buffer.from(oraclePublicKey, 'hex')); *The value returned will be a boolean indicating the validity of the signature.* true Parsing price message After you've made sure that the message is properly signed, you can parse it to get the data stored within the message which you can then use in your application: // Parse an oracle price message into parts. const messageParts = await OracleData.parsePriceMessage(response.message); *The returned value is an object with the following properties:*  price: 30311,  blockHeight: 647491,  blockHash: <Buffer 00 00 00 00 00 00 00 00 00 a9 d6 af a6 bc 32 ...>,  blockSequence: 1,  oracleSequence: 60427,  timestamp: 1596903497

$1.96

4 comments

Log in to join in Reading is open to everyone. Replying needs an account.