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
  • Contract Events Subscription
  • Contract Data Ledger Entries Subscription
  • Ledger Entries Expiration Subscription
  • Account Subscriptions
  1. Mercury "Classic"
  2. Subscriptions

API Definition

Description of the subscriptions API, learn how to subscribe to data.

PreviousSubscriptionsNextQueries

Last updated 5 months ago

All the JS examples in this definition are made using . We thank the Paltalabs team for providing and maintaining this SDK!

Before you use those examples, you need to define mercuryInstance:

import { Mercury } from "mercury-sdk";

const mercuryInstance = new Mercury({
  backendEndpoint: process.env.MERCURY_BACKEND_ENDPOINT,
  graphqlEndpoint: process.env.MERCURY_GRAPHQL_ENDPOINT,
  email: process.env.MERCURY_TESTER_EMAIL,
  password: process.env.MERCURY_TESTER_PASSWORD,
});

Contract Events Subscription

Contract event subscriptions allow you to start indexing smart contract events.

curl -X POST \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer $JWT_TOKEN" \
     -d '{
          "contract_id": "CONTRACT_ID",
          "topic1": "SOME_SCVAL_XDR",
          "topic2": "SOME_SCVAL_XDR",
          "topic3": "SOME_SCVAL_XDR",
          "topic4": "SOME_SCVAL_XDR"
         }' \
     MERCURY_ENDPOINT/event
mercuryInstance.subscribeToContractEvents({
  contractId: "CONTRACT_ID",
  topic1: "SOME_SCVAL_XDR",
  topic2: "SOME_SCVAL_XDR",
  topic3: "SOME_SCVAL_XDR",
  topic4: "SOME_SCVAL_XDR",
});

Contract Data Ledger Entries Subscription

Contract data ledger entries subscriptions allow you to index all changes to certain contract data. For example, imagine that it could keep track of balances.

curl -X POST \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer $JWT_TOKEN" \
     -d '{
          "contract_id": "CONTRACT_ID",
          "key_xdr": "SOME_SCVAL_XDR",
          "durability": "persistent"
          "hydrate": true
         }' \
     MERCURY_ENDPOINT/entry
const args = {
    contractId: "CONTRACT_ID",
    keyXdr: "SOME_SCVAL_XDR", // For example AAAAFA== is contract instance.
    durability: "persistent",
    hydrate: true,
}
const subscribe = await mercuryInstance.subscribeToLedgerEntries(args).catch((err) => {
    console.error(err)
})

Note that the hydrate: bool flag specifies whether you want to make a light upon subscribing. This means the current value in the ledger will be queriable after the subscription.

Ledger Entries Expiration Subscription

Tracks the current lifetime of a certain ledger entry.

curl -X POST \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer $JWT_TOKEN" \
     -d '{
          "hash_xdr": "HASHED_LEDGER_KEY_XDR_BASE64",
         }' \
     MERCURY_ENDPOINT/expiration

Where hash_xdr is a base64 encoded XDR object Hash which wraps the SHA256 of a LedgerKey XDR object.

Generating the correct hash_xdr can be non-trivial without guidance, so we will include here a brief Rust snippet that you can use to calculate the param:

// Make sure to import sha2::{Digest, Sha256} and all the XDR structures used first.
#[test]
fn to_bytes() {
    let key = LedgerKey::ContractData(LedgerKeyContractData {
        contract: ScAddress::Contract(Hash(stellar_strkey::Contract::from_string("CDRBYAW4UHXWVBCM4MIYZNL2EAW4TEKLQKUS7SXO67BG4IWAHXIDW63A").unwrap().0)),
        key: ScVal::LedgerKeyContractInstance,
        durability: ContractDataDurability::Persistent
    });
    let mut hasher = Sha256::new();
    hasher.update(key.to_xdr(Limits::none()).unwrap());
    let result = hasher.finalize().as_slice().try_into().unwrap();
    println!("Your hash_xdr is {}", Hash(result).to_xdr_base64(Limits::none()).unwrap());
}

Account Subscriptions

Account subscriptions allow you to start indexing operations, balances, and the account object of a certain Stellar account. These are mostly used for the Stellar "classic" side.

curl -X POST \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer $JWT_TOKEN" \
     -d '{
          "publickey": "PUBLIC_KEY",
          "hydrate": true
         }' \
     MERCURY_ENDPOINT/account
mercuryInstance.subscribeToFullAccount({
  address: "PUBLIC_KEY",
});

Note that the hydrate: bool flag specifies whether you want to make a light catchup upon subscribing. This means that the latest operations and current balances of the public key will be queriable right after the subscription, even if these were emitted before the time of the subscription.

When specifying hydrate: true, if no account exists on the network with that public key, the backend will return an error.

PaltaLabs' Mercury SDK