Liquidity Pool Management
The Liquidity Service provides API endpoints for managing liquidity pools, adding and removing liquidity, and executing token swaps. This guide walks you through the common operations with examples.
Overview
Liquidity pools allow users to provide liquidity (deposit pairs of tokens) to enable decentralized trading through automated market makers (AMMs). In return for providing liquidity, users receive LP (Liquidity Provider) tokens representing their share of the pool.
Key Features
- Create and manage liquidity pools
- Add and remove liquidity
- Swap tokens with predictable pricing
- View user positions and transaction history
- Administrator controls for fees and rates
Prerequisites
Before using the Liquidity Service:
- You need a valid API key with appropriate permissions
- Understand the token pairs you want to work with
- Have wallets set up with sufficient balances
Creating a Liquidity Pool
To create a new liquidity pool, you need to specify the two tokens that will form the pair, the initial exchange rate, and the fee structure.
const createPool = async () => { const response = await fetch('https://api.fsco.io/v2/liquidity/pool', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ token0ContractDeploymentId: "TOKEN_0_CONTRACT_ID", token1ContractDeploymentId: "TOKEN_1_CONTRACT_ID", fee: 300, // 0.3% fee in basis points rate: "1800.25", // Initial exchange rate walletId: "YOUR_WALLET_ID", name: "USDC-ETH Pool", description: "Liquidity pool for USDC and ETH trading" }) });
const data = await response.json(); console.log('Pool created with queryId:', data.queryId);};
import requestsimport json
def create_pool(): url = "https://api.fsco.io/v2/liquidity/pool" headers = { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY" } payload = { "token0ContractDeploymentId": "TOKEN_0_CONTRACT_ID", "token1ContractDeploymentId": "TOKEN_1_CONTRACT_ID", "fee": 300, # 0.3% fee in basis points "rate": "1800.25", # Initial exchange rate "walletId": "YOUR_WALLET_ID", "name": "USDC-ETH Pool", "description": "Liquidity pool for USDC and ETH trading" }
response = requests.post(url, headers=headers, json=payload) data = response.json() print(f"Pool created with queryId: {data['queryId']}")
curl -X POST "https://api.fsco.io/v2/liquidity/pool" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "token0ContractDeploymentId": "TOKEN_0_CONTRACT_ID", "token1ContractDeploymentId": "TOKEN_1_CONTRACT_ID", "fee": 300, "rate": "1800.25", "walletId": "YOUR_WALLET_ID", "name": "USDC-ETH Pool", "description": "Liquidity pool for USDC and ETH trading" }'
Pool Parameters
- token0ContractDeploymentId/token1ContractDeploymentId: The contract addresses of the tokens in the pair
- fee: Trading fee in basis points (e.g., 300 = 0.3%)
- rate: Initial exchange rate (how many units of token1 you get for 1 unit of token0)
- walletId: Your wallet ID that will be the initial administrator
- name/description: Human-readable identifiers for the pool
Viewing Liquidity Pools
You can retrieve all available liquidity pools or get details about a specific pool.
List All Pools
const getPools = async () => { const response = await fetch('https://api.fsco.io/v2/liquidity/pools', { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } });
const pools = await response.json(); console.log('Available pools:', pools);};
import requests
def get_pools(): url = "https://api.fsco.io/v2/liquidity/pools" headers = { "Authorization": "Bearer YOUR_API_KEY" }
response = requests.get(url, headers=headers) pools = response.json() print("Available pools:", pools)
curl -X GET "https://api.fsco.io/v2/liquidity/pools" \ -H "Authorization: Bearer YOUR_API_KEY"
Get Pool Details
const getPoolDetails = async (poolId) => { const response = await fetch(`https://api.fsco.io/v2/liquidity/pools/${poolId}`, { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } });
const pool = await response.json(); console.log('Pool details:', pool);};
import requests
def get_pool_details(pool_id): url = f"https://api.fsco.io/v2/liquidity/pools/{pool_id}" headers = { "Authorization": "Bearer YOUR_API_KEY" }
response = requests.get(url, headers=headers) pool = response.json() print("Pool details:", pool)
curl -X GET "https://api.fsco.io/v2/liquidity/pools/{POOL_ID}" \ -H "Authorization: Bearer YOUR_API_KEY"
Adding Liquidity
Adding liquidity involves depositing a balanced amount of both tokens in the pool. Before adding liquidity, you can get a quote to see how many LP tokens you’ll receive.
Get Add Liquidity Quote
const getAddLiquidityQuote = async (poolId, token0Amount, token1Amount) => { const response = await fetch( `https://api.fsco.io/v2/liquidity/quote/add?poolId=${poolId}&token0Amount=${token0Amount}&token1Amount=${token1Amount}`, { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } } );
const quote = await response.json(); console.log('Add liquidity quote:', quote); return quote;};
import requests
def get_add_liquidity_quote(pool_id, token0_amount, token1_amount): url = f"https://api.fsco.io/v2/liquidity/quote/add" headers = { "Authorization": "Bearer YOUR_API_KEY" } params = { "poolId": pool_id, "token0Amount": token0_amount, "token1Amount": token1_amount }
response = requests.get(url, headers=headers, params=params) quote = response.json() print("Add liquidity quote:", quote) return quote
curl -X GET "https://api.fsco.io/v2/liquidity/quote/add?poolId={POOL_ID}&token0Amount={AMOUNT}&token1Amount={AMOUNT}" \ -H "Authorization: Bearer YOUR_API_KEY"
Add Liquidity to Pool
const addLiquidity = async (poolId, walletId, token0Amount, token1Amount) => { const response = await fetch('https://api.fsco.io/v2/liquidity/add', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ poolId, walletId, token0Amount, token1Amount }) });
const result = await response.json(); console.log('Added liquidity:', result); return result;};
import requestsimport json
def add_liquidity(pool_id, wallet_id, token0_amount, token1_amount): url = "https://api.fsco.io/v2/liquidity/add" headers = { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY" } payload = { "poolId": pool_id, "walletId": wallet_id, "token0Amount": token0_amount, "token1Amount": token1_amount }
response = requests.post(url, headers=headers, json=payload) result = response.json() print("Added liquidity:", result) return result
curl -X POST "https://api.fsco.io/v2/liquidity/add" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "poolId": "POOL_ID", "walletId": "WALLET_ID", "token0Amount": "1000", "token1Amount": "0.5" }'
Removing Liquidity
When removing liquidity, you burn your LP tokens and receive back both tokens in the pair.
Get Remove Liquidity Quote
const getRemoveLiquidityQuote = async (poolId, lpTokenAmount, walletId) => { const response = await fetch( `https://api.fsco.io/v2/liquidity/quote/remove?poolId=${poolId}&lpTokenAmount=${lpTokenAmount}&walletId=${walletId}`, { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } } );
const quote = await response.json(); console.log('Remove liquidity quote:', quote); return quote;};
import requests
def get_remove_liquidity_quote(pool_id, lp_token_amount, wallet_id): url = f"https://api.fsco.io/v2/liquidity/quote/remove" headers = { "Authorization": "Bearer YOUR_API_KEY" } params = { "poolId": pool_id, "lpTokenAmount": lp_token_amount, "walletId": wallet_id }
response = requests.get(url, headers=headers, params=params) quote = response.json() print("Remove liquidity quote:", quote) return quote
curl -X GET "https://api.fsco.io/v2/liquidity/quote/remove?poolId={POOL_ID}&lpTokenAmount={AMOUNT}&walletId={WALLET_ID}" \ -H "Authorization: Bearer YOUR_API_KEY"
Remove Liquidity
const removeLiquidity = async (poolId, walletId, lpTokenAmount) => { const response = await fetch('https://api.fsco.io/v2/liquidity/remove', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ poolId, walletId, lpTokenAmount }) });
const result = await response.json(); console.log('Removed liquidity:', result); return result;};
import requestsimport json
def remove_liquidity(pool_id, wallet_id, lp_token_amount): url = "https://api.fsco.io/v2/liquidity/remove" headers = { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY" } payload = { "poolId": pool_id, "walletId": wallet_id, "lpTokenAmount": lp_token_amount }
response = requests.post(url, headers=headers, json=payload) result = response.json() print("Removed liquidity:", result) return result
curl -X POST "https://api.fsco.io/v2/liquidity/remove" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "poolId": "POOL_ID", "walletId": "WALLET_ID", "lpTokenAmount": "100" }'
Swapping Tokens
Swapping allows users to trade one token for another using the liquidity in the pool.
Get Swap Quote
const getSwapQuote = async (poolId, inputTokenAmount, inputTokenIsToken0) => { const response = await fetch( `https://api.fsco.io/v2/liquidity/quote/swap?poolId=${poolId}&inputTokenAmount=${inputTokenAmount}&inputTokenIsToken0=${inputTokenIsToken0}`, { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } } );
const quote = await response.json(); console.log('Swap quote:', quote); return quote;};
import requests
def get_swap_quote(pool_id, input_token_amount, input_token_is_token0): url = f"https://api.fsco.io/v2/liquidity/quote/swap" headers = { "Authorization": "Bearer YOUR_API_KEY" } params = { "poolId": pool_id, "inputTokenAmount": input_token_amount, "inputTokenIsToken0": str(input_token_is_token0).lower() }
response = requests.get(url, headers=headers, params=params) quote = response.json() print("Swap quote:", quote) return quote
curl -X GET "https://api.fsco.io/v2/liquidity/quote/swap?poolId={POOL_ID}&inputTokenAmount={AMOUNT}&inputTokenIsToken0=true" \ -H "Authorization: Bearer YOUR_API_KEY"
Execute Swap
const swap = async (poolId, walletId, amountIn, isToken0ToToken1, slippageBps = 50) => { const response = await fetch('https://api.fsco.io/v2/liquidity/swap', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ poolId, walletId, amountIn, isToken0ToToken1, slippageBps }) });
const result = await response.json(); console.log('Swap executed:', result); return result;};
import requestsimport json
def swap(pool_id, wallet_id, amount_in, is_token0_to_token1, slippage_bps=50): url = "https://api.fsco.io/v2/liquidity/swap" headers = { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY" } payload = { "poolId": pool_id, "walletId": wallet_id, "amountIn": amount_in, "isToken0ToToken1": is_token0_to_token1, "slippageBps": slippage_bps }
response = requests.post(url, headers=headers, json=payload) result = response.json() print("Swap executed:", result) return result
curl -X POST "https://api.fsco.io/v2/liquidity/swap" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "poolId": "POOL_ID", "walletId": "WALLET_ID", "amountIn": "100", "isToken0ToToken1": true, "slippageBps": 50 }'
Viewing User Positions
You can check how much liquidity a user has provided to a specific pool.
const getUserPosition = async (poolId, walletId) => { const response = await fetch( `https://api.fsco.io/v2/liquidity/pools/${poolId}/position/${walletId}`, { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } } );
const position = await response.json(); console.log('User position:', position); return position;};
import requests
def get_user_position(pool_id, wallet_id): url = f"https://api.fsco.io/v2/liquidity/pools/{pool_id}/position/{wallet_id}" headers = { "Authorization": "Bearer YOUR_API_KEY" }
response = requests.get(url, headers=headers) position = response.json() print("User position:", position) return position
curl -X GET "https://api.fsco.io/v2/liquidity/pools/{POOL_ID}/position/{WALLET_ID}" \ -H "Authorization: Bearer YOUR_API_KEY"
Transaction History
You can query transaction history for liquidity operations as well as swaps.
Liquidity Transactions
const getLiquidityTransactions = async (walletId = null, poolId = null, page = 1, pageSize = 10) => { let url = `https://api.fsco.io/v2/liquidity/transactions?page=${page}&pageSize=${pageSize}`;
if (walletId) url += `&walletId=${walletId}`; if (poolId) url += `&poolId=${poolId}`;
const response = await fetch(url, { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } });
const transactions = await response.json(); console.log('Liquidity transactions:', transactions); return transactions;};
import requests
def get_liquidity_transactions(wallet_id=None, pool_id=None, page=1, page_size=10): url = f"https://api.fsco.io/v2/liquidity/transactions" headers = { "Authorization": "Bearer YOUR_API_KEY" } params = { "page": page, "pageSize": page_size }
if wallet_id: params["walletId"] = wallet_id if pool_id: params["poolId"] = pool_id
response = requests.get(url, headers=headers, params=params) transactions = response.json() print("Liquidity transactions:", transactions) return transactions
curl -X GET "https://api.fsco.io/v2/liquidity/transactions?page=1&pageSize=10&walletId={WALLET_ID}&poolId={POOL_ID}" \ -H "Authorization: Bearer YOUR_API_KEY"
Swap Transactions
const getSwapTransactions = async (walletId = null, poolId = null, status = null, page = 1, pageSize = 10) => { let url = `https://api.fsco.io/v2/liquidity/swaps?page=${page}&pageSize=${pageSize}`;
if (walletId) url += `&walletId=${walletId}`; if (poolId) url += `&poolId=${poolId}`; if (status) url += `&status=${status}`;
const response = await fetch(url, { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } });
const transactions = await response.json(); console.log('Swap transactions:', transactions); return transactions;};
import requests
def get_swap_transactions(wallet_id=None, pool_id=None, status=None, page=1, page_size=10): url = f"https://api.fsco.io/v2/liquidity/swaps" headers = { "Authorization": "Bearer YOUR_API_KEY" } params = { "page": page, "pageSize": page_size }
if wallet_id: params["walletId"] = wallet_id if pool_id: params["poolId"] = pool_id if status: params["status"] = status
response = requests.get(url, headers=headers, params=params) transactions = response.json() print("Swap transactions:", transactions) return transactions
curl -X GET "https://api.fsco.io/v2/liquidity/swaps?page=1&pageSize=10&walletId={WALLET_ID}&poolId={POOL_ID}&status=completed" \ -H "Authorization: Bearer YOUR_API_KEY"
Administrator Controls
Pool administrators have additional capabilities to manage the pool.
Update Pool Fees
const updatePoolFees = async (poolId, fee, walletId) => { const response = await fetch('https://api.fsco.io/v2/liquidity/pool/fees', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ poolId, fee, walletId }) });
const result = await response.json(); console.log('Updated pool fees:', result); return result;};
import requestsimport json
def update_pool_fees(pool_id, fee, wallet_id): url = "https://api.fsco.io/v2/liquidity/pool/fees" headers = { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY" } payload = { "poolId": pool_id, "fee": fee, "walletId": wallet_id }
response = requests.post(url, headers=headers, json=payload) result = response.json() print("Updated pool fees:", result) return result
curl -X POST "https://api.fsco.io/v2/liquidity/pool/fees" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "poolId": "POOL_ID", "fee": 250, "walletId": "ADMIN_WALLET_ID" }'
Update Pool Rate
const updatePoolRate = async (poolId, rate, walletId) => { const response = await fetch('https://api.fsco.io/v2/liquidity/pool/rate', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ poolId, rate, walletId }) });
const result = await response.json(); console.log('Updated pool rate:', result); return result;};
import requestsimport json
def update_pool_rate(pool_id, rate, wallet_id): url = "https://api.fsco.io/v2/liquidity/pool/rate" headers = { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY" } payload = { "poolId": pool_id, "rate": rate, "walletId": wallet_id }
response = requests.post(url, headers=headers, json=payload) result = response.json() print("Updated pool rate:", result) return result
curl -X POST "https://api.fsco.io/v2/liquidity/pool/rate" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "poolId": "POOL_ID", "rate": "1850.75", "walletId": "ADMIN_WALLET_ID" }'
Update Pool Status
Administrators can pause or resume trading on a pool.
const updatePoolStatus = async (poolId, status, walletId) => { const response = await fetch('https://api.fsco.io/v2/liquidity/pool/status', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ poolId, status, // 'active' or 'paused' walletId }) });
const result = await response.json(); console.log('Updated pool status:', result); return result;};
import requestsimport json
def update_pool_status(pool_id, status, wallet_id): url = "https://api.fsco.io/v2/liquidity/pool/status" headers = { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY" } payload = { "poolId": pool_id, "status": status, # 'active' or 'paused' "walletId": wallet_id }
response = requests.post(url, headers=headers, json=payload) result = response.json() print("Updated pool status:", result) return result
curl -X POST "https://api.fsco.io/v2/liquidity/pool/status" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "poolId": "POOL_ID", "status": "paused", "walletId": "ADMIN_WALLET_ID" }'
Admin Fee Management
Administrators can collect fees and adjust fee sharing parameters.
Update Admin Fee Share
const updateAdminFeeShare = async (poolId, adminFeeShare, walletId) => { const response = await fetch('https://api.fsco.io/v2/liquidity/pool/admin-fee-share', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ poolId, adminFeeShare, // in basis points (e.g., 1000 = 10%) walletId }) });
const result = await response.json(); console.log('Updated admin fee share:', result); return result;};
import requestsimport json
def update_admin_fee_share(pool_id, admin_fee_share, wallet_id): url = "https://api.fsco.io/v2/liquidity/pool/admin-fee-share" headers = { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY" } payload = { "poolId": pool_id, "adminFeeShare": admin_fee_share, # in basis points (e.g., 1000 = 10%) "walletId": wallet_id }
response = requests.post(url, headers=headers, json=payload) result = response.json() print("Updated admin fee share:", result) return result
curl -X POST "https://api.fsco.io/v2/liquidity/pool/admin-fee-share" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "poolId": "POOL_ID", "adminFeeShare": 1000, "walletId": "ADMIN_WALLET_ID" }'
Collect Admin Fees
const collectAdminFees = async (poolId, walletId) => { const response = await fetch('https://api.fsco.io/v2/liquidity/pool/admin-fees/collect', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ poolId, walletId }) });
const result = await response.json(); console.log('Collected admin fees:', result); return result;};
import requestsimport json
def collect_admin_fees(pool_id, wallet_id): url = "https://api.fsco.io/v2/liquidity/pool/admin-fees/collect" headers = { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY" } payload = { "poolId": pool_id, "walletId": wallet_id }
response = requests.post(url, headers=headers, json=payload) result = response.json() print("Collected admin fees:", result) return result
curl -X POST "https://api.fsco.io/v2/liquidity/pool/admin-fees/collect" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "poolId": "POOL_ID", "walletId": "ADMIN_WALLET_ID" }'
Best Practices
When working with liquidity pools, consider these best practices:
-
Get a quote before executing actions - Always get quotes for add liquidity, remove liquidity, and swap operations to estimate the outcome.
-
Use slippage protection - When swapping tokens, set a reasonable slippage parameter (usually 0.5-1%) to protect against price movements.
-
Start with small amounts - When first interacting with a pool, start with small amounts to ensure everything works as expected.
-
Monitor pool status - Check if pools are active before attempting to add liquidity or swap tokens.
-
Check transaction history - Regularly review your transaction history to confirm all operations completed successfully.
-
Be aware of fees - Understand the fee structure of each pool before providing liquidity or swapping tokens.
-
Consider price impact - Large swaps or liquidity provisions can significantly impact price. Check the price impact in quotes.
Troubleshooting
Common Issues
Issue | Possible Causes | Solutions |
---|---|---|
Transaction fails | Insufficient balance, pool paused | Check token balances, verify pool status |
High slippage | Low liquidity, price volatility | Reduce trade size, adjust slippage tolerance |
Cannot collect fees | Not an admin, no fees to collect | Verify admin permissions, check fee accumulation |
Rate mismatch | Outdated rate information | Get a fresh quote before executing actions |
API requests failing | Authentication issues | Check API key permissions and expiration |
Error Handling
Always include proper error handling in your code:
try { // API call here} catch (error) { console.error("Error:", error.message); // Handle specific error types if (error.response && error.response.status === 400) { console.error("Bad request:", await error.response.json()); }}
Next Steps
Now that you understand how to manage liquidity pools, consider exploring:
- Token Management for creating and managing tokens
- Transaction Signing for more details on signing transactions
- Asset Management for broader asset functionality