Withdraw Liquidity
Example of code that withdraw liquidity from Aquarius pool
Executing Withdraw: Step by Step Guide
# Distributor's secret key (ensure this is kept secure)
user_secret_key = "S..."
# XLM/AQUA pool address
pool_address = "CCY2PXGMKNQHO7WNYXEWX76L2C5BH3JUW3RCATGUYKY7QQTRILBZIFWV"
# 1 share in stroops
share_amount = 1_0000000// Distributor's secret key (ensure this is kept secure)
const userSecretKey = "S...";
// XLM/AQUA pool address
const poolAddress = "CCY2PXGMKNQHO7WNYXEWX76L2C5BH3JUW3RCATGUYKY7QQTRILBZIFWV";
// 1 share in stroops
const shareAmount = 10000000;def execute_withdraw():
# 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 withdraw 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 withdraw
tx = tx_builder.append_invoke_contract_function_op(
contract_id=pool_address,
function_name="withdraw",
parameters=[
Address(keypair.public_key).to_xdr_sc_val(), # Distributor's address
scval.to_uint128(share_amount), # Amount of shares to redeem
scval.to_vec([
scval.to_uint128(1), # Minimum amount of XLM to receive
scval.to_uint128(1), # Minimum amount of AQUA to receive
]),
],
)
tx = server.prepare_transaction(tx.build())
tx.sign(keypair)
tx_response = horizon_server.submit_transaction(tx)
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
amount_a, amount_b = map(u128_to_int, [r.u128 for r in tx_result.vec.sc_vec])
print("Withdrawal successful!")
print(f"Received Amounts: Token A = {amount_a}, Token B = {amount_b}")Complete Code Examples
Last updated