RUST library for integrating RedStone Oracles with NEAR Smart Contracts and dApps.
RedStone is a data ecosystem that delivers frequently updated, reliable and diverse data on-chain.
To learn more about RedStone oracles use the following links:
Add redstone-near-connector-rs dependency to your Cargo.toml file in your smart contract crate
Now you can use the get_oracle_value
function in your smart contract code in the following way:
```rust use nearsdk::{log, nearbindgen}; use redstonenearconnectorrs::{getoraclevalue, decodehex};
const BTCBYTES32HEXSTR: &str = "4254430000000000000000000000000000000000000000000000000000000000";
const REDSTONEMAINDEMOSIGNERPUBKEYHEX: &str = "009dd87eb41d96ce8ad94aa22ea8b0ba4ac20c45e42f71726d6b180f93c3f298e333ae7591fe1c9d88234575639be9e81e35ba2fe5ad2c2260f07db49ccb9d0d";
fn getpubkey(hexpubkey: &str) -> [u8; 64] { let pubkeyvec = decodehex(hexpubkey).unwrap(); pubkeyvec.tryinto().unwrap() }
pub struct YourContract { ... }
impl YourContract { ...
pub fn yourcontractmethod(&mut self, redstonepayloadstr: String) { ...
// 32 bytes identifier of the data feed
let data_feed_id_vec = decode_hex(BTC_BYTES_32_HEX_STR).unwrap();
let data_feed_id: [u8; 32] = data_feed_id_vec.try_into().unwrap();
// Required min number of unique signers for the requested data feed
let unique_signers_threshold = 1;
// Vector, containing public keys of trusted signers
// Trusted public keys can be found here: https://github.com/redstone-finance/redstone-oracles-monorepo/blob/main/packages/oracles-smartweave-contracts/src/contracts/redstone-oracle-registry/initial-state.json
let authorised_signers: Vec<[u8; 64]> =
vec![get_pub_key(REDSTONE_MAIN_DEMO_SIGNER_PUB_KEY_HEX)];
let current_timestamp_milliseconds = u128::from(near_sdk::env::block_timestamp() / 1_000_000);
// Signer oracle data, efficiently serialized to bytes
let redstone_payload = decode_hex(&redstone_payload).unwrap();
// `get_oracle_value` function will:
// - parse redstone payload,
// - go through each signed data package,
// - verify each signature,
// - count unique signers for the requested data feed,
// - after passing all checks, return the aggregated median value
let oracle_value = get_oracle_value(
&data_feed_id,
unique_signers_threshold,
&authorised_signers,
current_timestamp_milliseconds,
&redstone_payload,
);
...
} }
```
You probably noticed, that in the first step your smart contract function requires an additional String argument (redstone_payload
). You can get it in your front-end or your tests code using redstone-sdk.
Firstly, install it in your frontend JS or TS project
```sh
npm install redstone-sdk
yarn add redstone-sdk ```
Then you can request the redstone payload in the following way:
```js import redstoneSDK from "redstone-sdk";
const redstoneDataGateways = [ "https://cache-service-direct-1.b.redstone.finance", "https://d33trozg86ya9x.cloudfront.net", ];
const redstonePayloadHex = await redstoneSDK.requestRedstonePayload( { dataServiceId: "redstone-main-demo", uniqueSignersCount: 1, dataFeeds: ["BTC"], }, redstoneDataGateways );
// Then you can pass the redstonePayloadHex
as an argument to the smart contract call, e.g.
const outcome = await wallet.signAndSendTransaction({
...
actions: [
{
type: "FunctionCall",
params: {
methodName: "yourcontractmethod",
args: {
redstone_payload: redstonePayloadHex,
},
...
},
},
],
});
```
You can check out a simple example NEAR dApp in RUST powered by RedStone oracles here.
The main logic is located in the src/lib.rs file. Tests are located in the tests folder.
cargo test -- --nocapture
Please feel free to contact RedStone team on Discord or send email to core@redstone.finance
MIT