♒
Aquarius Guide
  • 👋Welcome to Aquarius
  • Developers
    • Integrating with Aquarius
    • Aquarius Soroban Functions
    • Code Examples
      • Prerequisites & Basics
      • Executing Swaps Through Optimal Path
      • Executing Swaps Through Specific Pool
      • Deposit Liquidity
      • Withdraw Liquidity
      • Get Pools Info
      • Claim LP Rewards
      • Add Fees To Swap
        • Deploying a New Fee Collector
        • Executing Swaps with Provider Fees
        • Claiming & Swapping Accumulated Fees
  • Ecosystem Overview
    • 🌐What is Stellar?
      • What are Lumens (XLM)?
      • What are Anchors?
      • What are Trustlines?
      • How much are network fees on Stellar?
      • What are network reserves?
      • Where to trade Stellar assets?
    • 🧮What is Soroban?
  • AQUA tokens
    • ♒What are AQUA tokens?
      • AQUAnomics
      • AQUA Wallets
      • Where can I buy AQUA?
  • ICE
    • 🧊ICE tokens: locking AQUA and getting benefits
    • ICE boosts - how to maximize LP rewards
  • Aquarius AMMs
    • 💱What are Aquarius AMMs?
      • Pools
        • Creating a Pool
        • Deposit & Withdraw Liquidity
      • Swap
      • System limitations
        • Aquarius AMM: Limitations in Support for Fee-on-Transfer, Rebasing, and Deflationary Tokens
        • Aquarius AMM: Token Address Migration Limitations and Mitigation Strategy
  • My Aquarius
    • 👤My Aquarius
      • Main Overview
      • Balances
      • My Liquidity
      • SDEX Rewards
      • Liquidity Votes
      • Governance Votes
      • Airdrop #2
      • ICE Locks
      • Payments History
  • Aquarius AQUA Rewards
    • 🗳️Aquarius voting
      • Aquarius voting: asset Flag Restrictions
    • 🪙SDEX Rewards
    • 🤖Aquarius AMM Rewards
  • Bribes
    • 🎁What are bribes?
      • What are the advantages of protocol level bribes?
  • Aquarius Governance
    • 🧑‍⚖️Aquarius Governance: Community-Led Decision Making
  • Airdrops
    • 1️⃣The Initial Airdrop
      • Am I Eligible For the Initial Airdrop?
      • How can I see if I am eligible?
      • What are Claimable Balances?
      • How is the Initial airdrop distributed?
      • Where can I find more information?
    • 🌠Airdrop #2
      • How could I have been eligible for Airdrop #2?
      • How can I see if I am eligible?
      • When was the Airdrop #2 snapshot?
      • Were there any CEX's taking part?
      • How big was Airdrop #2?
      • How will the airdrop be distributed and for how long?
      • Could I have increased my potential reward?
      • Where can I find more information?
  • Signers Guild
    • 📜What is the signers guild?
      • What percentage of the AQUA supply will be controlled by the Signers Guild?
      • Who will be in the Signers Guild?
      • How does the Signing process work?
      • What will be expected from a guild member?
      • How can I sign up for this position?
      • What are wallets that Guild members will manage?
      • How can I learn more about this?
  • Guides
    • ❔How to use AQUA Locker tool and get ICE tokens
    • ❔How to vote for markets on Aquarius
    • How to create bribes
    • ❔How to use Aquarius Governance
      • How to make a governance vote
      • How to create a proposal
    • ❔How to earn SDEX rewards
    • ❔How to earn AMM rewards
  • Technical Documents
    • 📜Audits
    • 🪲Bug Bounties
    • 🛄Claimable Balances
    • 🗳️The Aquarius Voting Mechanism
    • 🎁SDEX v2 proposal & algorithm
    • ⏩ICE Boost Formula
  • Useful Links
    • Aquarius Home
    • Liquidity Voting
    • Liquidity Rewards
    • Aquarius Bribes
    • ICE locker
    • Aquarius Governance
    • Airdrop #2
Powered by GitBook
On this page
  • Prerequisites
  • Constants
  • Helper Functions
  1. Developers
  2. Code Examples

Prerequisites & Basics

Dependencies, commonly used URLs, contract addresses and useful methods

Prerequisites

Before executing scripts, ensure you have Stellar SDK installed:

  • Python 3.7+: The script is written in Python and requires Python version 3.7 or higher.

  • Stellar SDK: Install via pip:

pip install stellar-sdk
  • Node 18+: The script is written in JavaScript and requires Node version 18 or higher.

  • Stellar SDK: Install via npm or yarn.

npm install @stellar/stellar-sdk

or

yarn add @stellar/stellar-sdk

Constants

Below are some API endpoints and contract addresses that will be used in other code examples.

For Mainnet

# The contract ID of the Aquarius AMM contract
router_contract_id = "CBQDHNBFBZYE4MKPWBSJOPIYLW4SFSXAXUTSXJN76GNKYVYPCKWC6QUK"
# Soroban RPC server address
soroban_rpc_server = "https://mainnet.sorobanrpc.com"
# Horizon server address
horizon_server = "https://horizon.stellar.org"
# Aquarius backend API URL
base_api = "https://amm-api.aqua.network/api/external/v1"
// The contract ID of the Aquarius AMM contract
const routerContractId = "CBQDHNBFBZYE4MKPWBSJOPIYLW4SFSXAXUTSXJN76GNKYVYPCKWC6QUK";
// Soroban RPC server address
const sorobanRpcServer = "https://mainnet.sorobanrpc.com";
// Horizon server address
const horizonServer = "https://horizon.stellar.org";
// Aquarius backend API URL
const baseApi = "https://amm-api.aqua.network/api/external/v1";

For Testnet

# The contract ID of the Aquarius AMM contract
# This updates quarterly, due to testnet reset. 
# Address updated on December 2024.
router_contract_id = "CDGX6Q3ZZIDSX2N3SHBORWUIEG2ZZEBAAMYARAXTT7M5L6IXKNJMT3GB"
# Soroban RPC server address
soroban_rpc_server = "https://soroban-testnet.stellar.org:443"
# Horizon server address
horizon_server = "https://horizon-testnet.stellar.org"
# Aquarius backend API URL
base_api = "https://amm-api-testnet.aqua.network/api/external/v1"
// The contract ID of the Aquarius AMM contract
// This updates quarterly, due to testnet reset. 
// Address updated on December 2024.
const routerContractId = "CDGX6Q3ZZIDSX2N3SHBORWUIEG2ZZEBAAMYARAXTT7M5L6IXKNJMT3GB";
// Soroban RPC server address
const sorobanRpcServer = "https://soroban-testnet.stellar.org:443";
// Horizon server address
const horizonServer = "https://horizon-testnet.stellar.org";
// Aquarius backend API URL
const baseApi = "https://amm-api-testnet.aqua.network/api/external/v1";

Helper Functions

Common utility methods that will be used in other code examples.

Get Asset Contract Id

To interact with any Soroban assets, you need their smart contract address.

This code snippet demonstrates how to retrieve the contract address of an asset on the Stellar network. The code uses the PUBLIC network by default, but you can switch to the desired network (e.g. TESTNET).

from stellar_sdk import Asset, Network

# Create the native asset
asset = Asset.native()
# Or create a custom asset
# asset = Asset("AQUA", "GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA")

# Retrieve the contract ID for the PUBLIC network
contract_id = asset.contract_id(Network.PUBLIC_NETWORK_PASSPHRASE)

print(contract_id)
# Example output:
# "CAS3J7GYLGXMF6TDJBBYYSE3HQ6BBSMLNUQ34T6TZMYMW2EVH34XOWMA"
const StellarSdk = require('@stellar/stellar-sdk');

// Create the native asset
const asset = StellarSdk.Asset.native();
// Or create a custom asset
// const asset = new StellarSdk.Asset('AQUA', 'GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA');

// Retrieve the contract ID for the PUBLIC network
const contractId = asset.contractId(StellarSdk.Networks.PUBLIC);

console.log(contractId);
// console.log example
// "CAS3J7GYLGXMF6TDJBBYYSE3HQ6BBSMLNUQ34T6TZMYMW2EVH34XOWMA"

Get Pool Contract ID and Pool Hash

To interact with an Aquarius pool (e.g. make deposits or direct swaps), you need the address of the pool contract.

Order Tokens IDs

Most of the time, if an array of token IDs needs to be passed as arguments in a contract call, they should be sorted.

def order_token_ids(tokens: List[xdr.SCVal]) -> List[xdr.SCVal]:
   """
   Orders token IDs based on their contract ID to maintain consistency.

   Args:
       tokens (List[xdr.SCVal]): List of token addresses as SCVal objects.

   Returns:
       List[xdr.SCVal]: Ordered list of token SCVal objects.
   """
   return sorted(tokens, key=lambda token: int(token.address.contract_id.hash.hex(), 16))
function orderTokensIds(tokensIds) {
    /**
     * Orders token IDs based on their contract ID to maintain consistency.
     *
     * @param {Array} tokensIds - List of token addresses as SCVal objects.
     * @returns {Array} Ordered list of token SCVal objects.
     */
    return tokensIds.sort((a, b) => {
        const aHash = BigInt('0x' + a.address().contractId().toString('hex'));
        const bHash = BigInt('0x' + b.address().contractId().toString('hex'));

        // Compare BigInts directly without converting to number
        if (aHash < bHash) return -1;
        if (aHash > bHash) return 1;
        return 0;
    });
}

Data Conversion Utilities

Here are methods and code snippets for converting data between basic types and ScVal (Smart Contract Value).

from stellar_sdk import Address, scval

# ================
# Basic => ScVal
# ================

# Contract Id To ScVal
contract_id = "C..."
contract_id_scval = scval.to_address(contract_id)

# Array To ScVal
scval.to_vec([contract_id_scv, contract_id_scv])

# Public Key To ScVal
public_key = "G..."
public_key_scval = scval.to_address(public_key)

# Number To Uint32 ScVal
number_u32_scval = scval.to_uint32(1_000_000)


# Number To Uint128 ScVal
number_u128_scval = scval.to_uint128(1_000_000)

# Hash To ScVal 
pool_hash = "a1b2...."
pool_hash_scval = scval.to_bytes(bytes.fromhex(pool_hash))

# ================
#  ScVal => Basic
# ================

def u128_to_int(value: UInt128Parts) -> int:
   """
   Converts UInt128Parts from Stellar's XDR to a Python integer.

   Args:
       value (UInt128Parts): UInt128Parts object from Stellar SDK.

   Returns:
       int: Corresponding Python integer.
   """
   return int(value.hi.uint64 << 64) + value.lo.uint64
const StellarSdk = require('@stellar/stellar-sdk');
const binascii = require('binascii');
const { Address, StrKey, XdrLargeInt, xdr } = StellarSdk;

// ================
// Basic => ScVal
// ================

function contractIdToScVal(contractId) {
    /**
     * Converts a contract ID to a Stellar SCVal (Smart Contract Value) format.
     *
     * @param {string} contractId - The contract ID to convert.
     * @returns {StellarSdk.xdr.ScVal} - The SCVal representation of the contract ID.
     *
     * @throws {Error} Throws an error if the contract ID is invalid or cannot be decoded.
     */
    return Address.contract(StrKey.decodeContract(contractId)).toScVal();
}

function arrayToScVal(array) {
    /**
     * Converts an array of ScVal objects into a single ScVal vector.
     *
     * @param {StellarSdk.xdr.ScVal[]} array - An array of ScVal objects to be converted into a vector.
     * @returns {StellarSdk.xdr.ScVal} A ScVal object representing the vector.
    */
    return xdr.ScVal.scvVec(array);
}


function publicKeyToScVal(pubkey) {
    /*
     * Converts a Stellar public key string into an ScVal address.
     *
     * @param {string} pubkey - The Stellar public key to be converted.
     * @returns {StellarSdk.xdr.ScVal} An ScVal object representing the address derived from the public key.
    */
    return xdr.ScVal.scvAddress(Address.fromString(pubkey).toScAddress());
}

function numberToUint32(number) {
    /**
     * Converts a number to an ScVal representing a 32-bit unsigned integer (Uint32).
     *
     * @param {number} number - The number to be converted. Must be a non-negative integer within the range of Uint32 (0 to 2^32 - 1).
     * @returns {StellarSdk.xdr.ScVal} An ScVal object representing the 32-bit unsigned integer.
    */
    return xdr.ScVal.scvU32(number);
}

function numberToUint128(number) {
    /**
     * Converts a number to an ScVal representing a 128-bit unsigned integer (Uint128).
     *
     * @param {number} number - The number to be converted. Must be a non-negative value that fits within the range of Uint128.
     * @returns {StellarSdk.xdr.ScVal} An ScVal object representing the 128-bit unsigned integer.
    */
    return new XdrLargeInt(
        'u128',
         number.toFixed(),
    ).toU128();
}

function bufferToScVal(buffer) {
    /**
     * Converts a Buffer object into an ScVal representing a byte array.
     *
     * @param {Buffer} buffer - The Buffer object containing the byte data to be converted.
     * @returns {StellarSdk.xdr.ScVal} An ScVal object representing the byte array.
    */
    return xdr.ScVal.scvBytes(buffer);
}


hashToScVal(hash) {
    /**
     * Converts a hexadecimal hash string into an ScVal representing a byte array.
     *
     * @param {string} hash - The hash string in hexadecimal format to be converted.
     * @returns {StellarSdk.xdr.ScVal} An ScVal object representing the byte array derived from the hash.
    */
    return xdr.ScVal.scvBytes(Buffer.from(binascii.unhexlify(hash), 'ascii'));
}

// ================
// SvVal => Basic
// ================

function u128ToInt(value) {
    /**
     * Converts UInt128Parts from Stellar's XDR to a JavaScript number.
     *
     * @param {Object} value - UInt128Parts object from Stellar SDK, with `hi` and `lo` properties.
     * @returns {number|null} Corresponding JavaScript number, or null if the number is too large.
     */
    const result = (BigInt(value.hi()._value) << 64n) + BigInt(value.lo()._value);

    // Check if the result is within the safe integer range for JavaScript numbers
    if (result <= BigInt(Number.MAX_SAFE_INTEGER)) {
        return Number(result);
    } else {
        console.warn("Value exceeds JavaScript's safe integer range");
        return null;
    }
}
PreviousCode ExamplesNextExecuting Swaps Through Optimal Path

Last updated 5 months ago

One option to find the pool address is to refer to the on Aquarius website, please see below:

In order to receive this information programmatically, please refer to detailed description in the .

pool page
"Get Pools Info" section
Example of pool contract address, click to copy.