Crates.io | rustlink |
lib.rs | rustlink |
version | 0.0.4 |
source | src |
created_at | 2024-05-16 16:12:25.286881 |
updated_at | 2024-09-18 12:28:18.553012 |
description | A lightweight and easy-to-use library for periodically retrieving data from the Chainlink decentralized data feed. |
homepage | |
repository | https://github.com/starkbamse/rustlink |
max_upload_size | |
id | 1242253 |
size | 29,359 |
rustlink
- Decentralized Cryptocurrency Price FeedA lightweight rust library for periodically retrieving cryptocurrency prices from the ChainLink decentralized price feed. With rustlink
, you can easily retrieve the latest price of any cryptocurrency supported by ChainLink.
rustlink
?The core principle I have maintained while developing this library is robustness and simplicity. As Web3-developers, payment operators we often need to retrieve cryptocurrency prices. However, most of the libraries available use centralized exchange APIs that have shown to be unreliable and are affected by local exchange fluctuations. Using the ChainLink dencetralized data feed we can retrieve reliable data that is provided by many independent oracles.
The library is made to be WASM-compatible, so you can use it in your web applications as well. This allows you to receive decentralized price updates in real-time in your web and desktop apps, without having to rely on centralized price feeds or APIs.
To see which cryptocurrencies are supported by ChainLink, you can visit the ChainLink data feeds list.
WebAssembly (WASM) is a binary instruction format for a stack-based virtual machine. WASM is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications. WASM is supported by all major browsers, including Chrome, Firefox, Safari, and Edge.
This means that you can run rust code in your web applications, and use rustlink
to retrieve cryptocurrency prices in real-time in your web apps.
To use rustlink
in your project, add the following to your Cargo.toml
file:
[dependencies]
rustlink = "0.0.3"
To build the library for WASM, you need to have the wasm-pack
tool installed. You can install it by running the following command:
cargo install wasm-pack
You also need the wasm32-unknown-unknown target installed. You can install it by running the following command:
rustup target add wasm32-unknown-unknown
You can build the library either for the browser or for Node.js.
For the browser:
wasm-pack build --target web --out-name rustlink --out-dir <directory>
For Node.js:
wasm-pack build --target nodejs --out-name rustlink --out-dir <directory>
Here is a simple example of how you can use rustlink
to retrieve the latest price of a cryptocurrency:
use async_std::channel::unbounded;
use rustlink::core::{Reflector, Rustlink};
#[tokio::main]
async fn main(){
let mut contracts: Vec<(String, String)> = Vec::new();
contracts.push((
"ETH".to_string(),
"0x9ef1B8c0E4F7dc8bF5719Ea496883DC6401d5b2e".to_string(),
));
let (sender, receiver) = unbounded();
let rustlink = Rustlink::try_new(
"https://bsc-dataseed1.binance.org/",
1,
Reflector::Sender(sender),
contracts,
std::time::Duration::from_secs(10),
)
.unwrap();
rustlink.start();
let round_data = receiver.recv().await.unwrap();
println!("Received data: {:#?}", round_data);
}
You can also loop through the receiver
to get the latest price updates in real-time by putting the receiver in a loop:
loop {
let round_data = receiver.recv().await.unwrap();
println!("Received data: {:#?}", round_data);
}
import init, { RustlinkJS } from '../web/rustlink.js';
async function runWasm() {
await init(); // Initialize the wasm module
// Example data
const rpcUrl = "https://bsc-dataseed1.binance.org/";
const fetchIntervalSeconds = BigInt(1);
const contracts = [
["ETH", "0x9ef1B8c0E4F7dc8bF5719Ea496883DC6401d5b2e"],
["1INCH", "0x9a177Bb9f5b6083E962f9e62bD21d4b5660Aeb03"],
];
async function callback(roundData) {
console.log("Callback received:", roundData);
}
let rustlink = new RustlinkJS(rpcUrl, fetchIntervalSeconds, contracts, callback, 10);
rustlink.start();
console.log("Stopping after 5 seconds");
setTimeout(() => {
rustlink.stop();
}, 5000);
}
runWasm();
This library is licensed under the MIT license. See the LICENSE file for more details.