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.

Last updated