Deposit Liquidity

Example of code that deposits liquidity into Aquarius pool

On this page, we demonstrate how to deposit 0.1 XLM and 25 AQUA into a specific AQUA/XLM pool. Scroll here to see the complete code.

Executing Deposit: Step by Step Guide

To perform a deposit, you need to follow these steps:

  1. Specify user secret key, amounts for deposit and pool address: To deposit into a pool, you need your user secret key, the amounts to deposit (specified in stroops) and the pool address. The amounts must be passed to the contract call in the same order as they are indexed in the pool (an example of token sorting can be found here).

# Distributor's secret key (ensure this is kept secure)
user_secret_key = "S..."
# XLM/AQUA pool address 
pool_address = "CCY2PXGMKNQHO7WNYXEWX76L2C5BH3JUW3RCATGUYKY7QQTRILBZIFWV"
# 0.1 XLM in stroops
amount_a = 1_000000
# 25 AQUA in stroops
amount_b = 25_0000000
  1. Make a contract call to the pool's "deposit" method.

def execute_deposit():
   # Initialize Soroban and Horizon servers
   server = SorobanServer(SOROBAN_SERVER_RPC)
   horizon_server = Server(HORIZON_SERVER)

   # Load distributor's keypair using the secret key
   keypair = Keypair.from_secret(user_secret_key)

   # Load the distributor's account information from Soroban server
   account = server.load_account(keypair.public_key)

   # Build the deposit transaction
   tx_builder = TransactionBuilder(
       source_account=account,
       network_passphrase=NETWORK_PASSPHRASE,
       base_fee=1000000  # Set base fee; adjust as necessary
   ).set_timeout(3600)  # Set transaction timeout

   # Append the invoke_contract_function operation for deposit
   tx = tx_builder.append_invoke_contract_function_op(
       contract_id=pool_address,
       function_name="deposit",
       parameters=[
           Address(keypair.public_key).to_xdr_sc_val(),  # Distributor's address
           scval.to_vec([
               scval.to_uint128(amount_a),  # Amount of XLM to deposit
               scval.to_uint128(amount_b),  # Amount of AQUA to deposit
           ]),
           scval.to_uint128(0),  # Additional parameter (e.g., minimum shares)
       ],
   ).build()

   # Prepare and sign the transaction
   prepared_tx = server.prepare_transaction(tx)
   prepared_tx.sign(keypair)

   # Submit the transaction to the Horizon server
   tx_response = horizon_server.submit_transaction(prepared_tx)

   # Parse the transaction metadata to extract results
   transaction_meta = xdr.TransactionMeta.from_xdr(tx_response['result_meta_xdr'])
   tx_result = transaction_meta.v3.soroban_meta.return_value
   if not tx_result:
       raise RuntimeError("Transaction did not return a result. Deposit failed.")

   # Extract deposited amounts and shares from the transaction result
   deposit_a, deposit_b = map(u128_to_int, [r.u128 for r in tx_result.vec.sc_vec[0].vec.sc_vec])
   shares_amount = u128_to_int(tx_result.vec.sc_vec[1].u128)

   print("Deposit successful!")
   print(f"Deposited Amounts: Token A = {deposit_a}, Token B = {deposit_b}")
   print(f"Received Shares: {shares_amount}")

Complete Code Examples

This code deposits 0.1 XLM and 25 AQUA to the pool with Aquarius AMM on mainnet.

To successfully execute the code, provide the secret key of a Stellar account with at least 5 XLM and 25 AQUA.

Copy the full code Python
Copy the full code JavaScript

Last updated