Accessing the Ledger
Learn how you can directly access the Stellar ledger from a Zephyr program.
Zephyr's host environment also provides functions to access the ledger maintained by the Stellar nodes that live on our server. This means that Zephyr programs have fast, first-class access to the current state of the ledger, allowing them to be aware of everything happening on-chain, not just during each ledger close.
There is currently only a limited set of data structures that can be retrieved from the Stellar ledger. All contract data and some Stellar classic entries are accessible, we are still developing the others.
Reading contract data entries
You can read contract data entries from the ledger using the following EnvClient
methods:
These functions are quite straightforward and are a good tool for Zephyr ingestion programs to fetch directly from the ledger's state, but are even more important when it comes to serverless functions that grant powerful on-request access to the ledger.
Understanding return types
Being the requested data taken directly from the ledger, the returned contract data entries will be ScVals. Learn more on how to deal with returned ScVals in the examples.
Examples
read_contract_entries()
read_contract_entries()
In this example taken from the Blend dashboard repository, we can see how to access the key of a specific entry (Contracts) for this factory contract, and then push the resulting addresses in a vector. The entry.key
object is and ScVal, while the resulting objects inside the vector have been converted to Address environment objects.
Here PoolFactoryDataKey::Contracts
is the only variable of the enum, so "address" can be defined as we did here:
But when the enum that holds the entry has many variants, we have to use a match
arm for each variant when accessing it. Here PoolDataKey::Positions
is one of the many variants of the enum:
See the complete code of this example here.
read_contract_instance
read_contract_instance
This example allows us to explore the structure of the contract instance we retrieve from the ledger. In this case, we are extracting (as a ScVal) the value of a specific entry in the instance by passing its key to the function str_
. Remember that here we're returning a ScVal. Learn more on how to deal with ScVals in the "work with data" section.
read_contract_entry_by_key()
read_contract_entry_by_key()
Here we are accessing a specific value for an entry key. The returned value will technically be a ScVal, but in this case, can be used as a regular integer. You can view the complete example here.
Last updated