Livy Documentation

Price feed

To create your price feed services, you use any API provider to get the price of a token. With Livy you can easily make it verifiable too.

For this example, we will use Coinbase's API.

Before you start

Create your Livy account or create one. Then, create your verifiable application.

Get a price for a token

Chose "Write Inline" to directly write the code in the console.

Create service page

Write your code

For this example, we will use the Binance API:

main.rs
use reqwest;
use serde::Deserialize;
use clap::Parser;

#[derive(Deserialize)]
struct CoinbaseData {
    amount: String,
}
#[derive(Deserialize)]
struct CoinbaseResponse {
    data: CoinbaseData,
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
    #[arg(short, long)]
    symbol: String, // ETH-USD or BTC-USD
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Args::parse();
    
    let url = format!("https://api.coinbase.com/v2/prices/{}/spot", args.symbol);
    let response = reqwest::get(&url).await?;
    let coinbase_response: CoinbaseResponse = response.json().await?;
    println!("{}", coinbase_response.data.amount);
    Ok(())
}

Information

By using clap crate, Livy can parse the arguments passed to the application and autofills the execution arguments.

Add the "Cargo.toml" file:

Cargo.toml
[package]
name = "price-feed"
version = "0.1.0"
edition = "2021"

[dependencies]
reqwest = { version = "0.12", features = ["json", "rustls-tls"], default-features = false }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.0", features = ["full"] }
clap = { version = "4.0", features = ["derive"] } 

Deploy your application

Deploy your application by clicking the "Create Service" button.