# Reading From Indexes/External Tables

## Understanding Indexes

Zephyr tables are by default shareable across programs.&#x20;

{% hint style="info" %}
Note that when pricing kicks off creators of certain tables will be able to manage how their tables are accessed with custom rules.
{% endhint %}

This functionality allows any program or [custom-dashboards](https://docs.mercurydata.app/zephyr-full-customization/learn/custom-dashboards "mention") to access already indexed + live-updated data on Mercury. As seen in [Zephyr.toml Extentions](https://docs.mercurydata.app/zephyr-full-customization/learn/zephyr.toml-extensions-indexes-and-dashboard), creators can share their tables with the community and significantly simplify the work for othr users. All the needed data to access an already existing public table can be found in the [Community Indexes](https://main.mercurydata.app/indexes) section of the app.&#x20;

<figure><img src="https://511343000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fsu8zswTJAtj7BtnAACVf%2Fuploads%2F08mMmo2tMhjdrIn0aPOH%2FScreenshot%202024-10-07%20at%2017.50.37.png?alt=media&#x26;token=7d710b95-3947-4c71-b2b0-bbe6bb007393" alt=""><figcaption></figcaption></figure>

By selecting the desired index, it's possible to copy the table struct to paste in the program that needs to access it. &#x20;

## Accessing a public Index

Let's make things practical and jump straight to an example. This code returns all indexed Blend Mainnet borrowed operations for a given pool:

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

// importing the public table
#[derive(DatabaseDerive, Clone)]
#[with_name("borrowed")]
#[external("35")]    // notice that! Below you have the full explanation
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,
}

#[derive(Deserialize)]
pub struct Request {
    pool: String,
}

#[derive(Serialize)]
pub struct ResponseObject {
    pub timestamp: u64,
    pub ledger: u32,
    pub asset: String,
    pub borrowed: String,
    pub delta: String,
    pub source: String,
}

#[no_mangle]
pub extern "C" fn get_borrowed_by_pool() {
    let env = EnvClient::empty();
    let request: Request = env.read_request_body();
    // starting from the publicly indexed "borrowed" table we filter through the Borrowed objects
    // for the ones whose ".pool" field matches our request body. 
    let borrowed: Vec<Borrowed> = env.read_filter().column_equal_to("pool", request.pool).read().unwrap();
    // return those as a Vec of objects of type "ResponseObject"
    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: (obj.delta as i64).to_string(),
            source: obj.source.clone()
        }
    }).collect();

    env.conclude(&borrowed)
}
```

The key here is the attribute

```
#[external("35")]
```

Which tells the compiler (and then the ZephyrVM) that you're reading from the `borrowed` table created by id 35 (in this case, blend's Mainnet deployment).&#x20;

This function can be easily called through:

```
curl -X POST https://mainnet.mercurydata.app/zephyr/execute -H "Authorization: Bearer $MERCURY_JWT" -H 'Content-Type: application/json' -d '{"project_name":"zephyr-hello-world", "mode":{"Function": {"fname": "get_borrowed_by_pool", "arguments": "{\"pool\":\"CDVQVKOY2YSXS2IC7KN6MNASSHPAO7UN2UR2ON4OI2SKMFJNVAMDX6DP\"}"}}}'
```

The result will be a list of all the borrows for a certain pool, each borrow displaying the specified fields.

<figure><img src="https://511343000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fsu8zswTJAtj7BtnAACVf%2Fuploads%2F5aOyecyyW6nOjwQjx8Wl%2FScreenshot%202024-10-07%20at%2018.28.10.png?alt=media&#x26;token=149b45b7-75ba-42c0-9b32-14d18eba95d6" alt=""><figcaption></figcaption></figure>

As you can see here, we are accessing some data that we have not indexed ourselves quite easily, and this can be done from every point of your Zephyr program.&#x20;
