Reading From External Tables

Zephyr tables are by default shareable across programs. Note that when pricing kicks off creators of certain tables will be able to manage how their tables are accessed with custom rules.

This functionality allows any program or Custom Dashboardsto access already indexer + live-updated data on Mercury!

This code returns all indexed Blend mainnet borrowed operations for a given pool:

use serde::{Deserialize, Serialize};
use zephyr_sdk::{prelude::*, EnvClient, DatabaseDerive};

#[derive(DatabaseDerive, Clone)]
pub struct Borrowed {
    pub id: i64,
    pub timestamp: u64,
    pub ledger: u32,
    pub pool: String,
    pub asset: String,
    pub borrowed: i128,
    pub delta: i128,
    pub source: String,

pub struct Request {
    pool: String,

pub struct ResponseObject {
    pub timestamp: u64,
    pub ledger: u32,
    pub asset: String,
    pub borrowed: String,
    pub delta: String,
    pub source: String,

pub extern "C" fn get_borrowed_by_pool() {
    let env = EnvClient::empty();
    let request: Request = env.read_request_body();
    let borrowed: Vec<Borrowed> = env.read_filter().column_equal_to("pool", request.pool).read().unwrap();
    let borrowed: Vec<ResponseObject> = borrowed.iter().map(|obj| {
        ResponseObject {
            timestamp: obj.timestamp,
            ledger: obj.ledger,
            asset: obj.asset.clone(),
            borrowed: (obj.borrowed as i64).to_string(),
            delta: ( as i64).to_string(),
            source: obj.source.clone()


The key here is the attribute


Which tells the compiler (and then the ZephyrVM) that you're reading from the borrow table created by id 8 (in this case, blend's mainnet deployment).

This function can be easily called through:

curl -X POST -H "Authorization: Bearer $MERCURY_JWT" -H 'Content-Type: application/json' -d '{"mode":{"Function": {"fname": "get_borrowed_by_pool", "arguments": "{\"pool\":\"CDVQVKOY2YSXS2IC7KN6MNASSHPAO7UN2UR2ON4OI2SKMFJNVAMDX6DP\"}"}}}'

Last updated