Mercury Documentation
  • Get Started with Mercury
    • Pricing
    • Endpoints
    • Authentication
  • The Definitive Guide to Understanding The Mercury's Stack Vision
  • Retroshades
    • Introduction to Retroshades
    • Get Started
      • Importing the Retroshades SDK
      • Writing and Emitting Retroshades
      • Deploying to Mercury Retroshades
      • Querying Retroshades
  • Zephyr: Full Customization
    • Introduction
    • General concepts
      • Zephyr Programs
      • Accessing the Ledger: Contract Entries
      • Accessing the Ledger Meta: Contract Events
      • Database Interactions
      • Create Custom Callable APIs: Serverless Functions
      • Catchups
      • Using Soroban inside Zephyr
    • Quickstart
    • Learn
      • Introduction to Mercury's Cloud and the Zephyr Stack
      • Get Started: Set Up and Manage the Project
        • Create the Project
        • Writing the Program
        • Local Testing
        • Deploy
        • Data catchups/backfill
        • Monitor Execution
        • Querying
      • Database Interactions
        • Zephyr.toml: Define the Tables Structure
        • Understanding Database Interactions
        • Database Operations
      • Accessing the Ledger
      • Accessing Ledger Transition: Soroban Events
      • Working with Contract Custom Types
      • Working with Soroban SDK Types
      • Web Requests, Automation and Alerts.
      • Zephyr.toml Extensions: Indexes And Dashboard
      • Reading From Indexes/External Tables
      • Custom APIs - Serverless Functions
        • General Concepts
        • Custom RPC-alike Endpoints
        • Querying APIs for Composable Data
      • Data Catchups/Backfill
      • Custom Dashboards
        • Creating the Dashboard
        • Plotting: Simple
        • Complex Plotting
    • Support
  • Mercury "Classic"
    • Subscriptions
      • API Definition
    • Queries
      • Contract Events
      • Contract Data Entry Updates
      • Stellar Operations, Balances, and Account Objects
  • TUTORIALS
    • Zephyr
      • Self-hosting Zephyr
      • No-RPC Dapp
      • Indexing a DeFi liquidity pool (Blend)
      • Building a Secure DeFi Real-Time Bot Through Smart Accounts
      • Monitoring Large Deposits with Zephyr and Sending Web Alerts
    • Mercury Classic
      • Index and query contract events
Powered by GitBook
On this page
  • Defining Retroshades
  • Emitting Retroshades
  1. Retroshades
  2. Get Started

Writing and Emitting Retroshades

As promised, this section is very short, because there's little to know about Retroshades: it's all Soroban code!

The only concept that you need to keep in mind is that the retroshade logic within your contract is not metered on chain, and you are not paying any extra fees by adding Retroshade logic to your code. In fact, you can write retroshades for contracts that are already deployed and you don't need to perform any kind of action on-chain.

The contracts compiled with the mercury feature can be directly deployed to Mercury's retroshade network and work out of the box.

Defining Retroshades

Defining retroshades means defining rust structures. Keep in mind that:

  • The fields will be translated to database columns. So if you have data that you want to be able to efficiently query, include them as top-level field, rather than in a nested structure.

  • The types are transformed to their corresponding native database type.

#[cfg(feature = "mercury")]
mod retroshade {
    use retroshade_sdk::Retroshade;
    use soroban_sdk::{contracttype, Address, Symbol};

    #[derive(Retroshade)]
    #[contracttype]
    pub struct LiquidityEvent {
        pub from: Address,
        pub kind: Symbol,
        pub amount: i128,
        pub at_fee_per_share_universal: i128,
        pub at_fee_per_share_particular: i128,
        pub at_shares: i128,
        pub new_shares_minted: i128,
        pub ledger: u32,
        pub timestamp: u64,
    }
}

Emitting Retroshades

Emitting a retroshade is as simple as declaring the retroshade structure and emit it in your target code block:

#[cfg(feature = "mercury")]
retroshade::LiquidityEvent {
    from,
    amount,
    at_fee_per_share_particular,
    at_fee_per_share_universal,
    at_shares,
    new_shares_minted: amount,
    kind: symbol_short!("deposit"),
    ledger: env.ledger().sequence(),
    timestamp: env.ledger().timestamp(),
}
.emit(&env);

Keep in mind that you can add additional soroban code under the mercury flag to retrieve the data you need!

#[cfg(feature = "mercury")]
let (at_fee_per_share_particular, at_fee_per_share_universal, at_shares) = (
    read_fee_per_share_particular(&env, from.clone()),
    get_fee_per_share_universal(&env),
    get_tot_supply(&env),
);

This code won't have any impact when the binary is compiled with the mercury flag.

Tip: beware that .clone() are just compiler stubs in Soroban. So if you end up having to clone Val don't worry, because it won't change the resulting binary.

PreviousImporting the Retroshades SDKNextDeploying to Mercury Retroshades

Last updated 7 months ago