Execute trades seamlessly by building transaction data and sending it with your wallet signer. The SDK handles transaction building, gas estimation, and execution automatically.The
trade method combines transaction building and execution into a single, streamlined operation. This approach simplifies the trading process while maintaining full control over transaction parameters.
Before executing trades, tokens typically require approval to allow the DZap
contracts to spend them on your behalf. Learn more about gas-optimized
approval mechanisms in the Approval Mechanisms
section.
Rate Limits: Our API and SDK have rate limits in place to ensure fair
usage. If you need increased rate limits for your application, please reach
out to our team on Telegram.
Trade Execution
Execute a trade using the trade method:Parameters
Thetrade function expects an object with these parameters:
Required Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
request | TradeBuildTxnRequest | yes | The trade build request containing all trade details |
signer | Signer | WalletClient | yes | Ethers signer or viem wallet client for signing transactions |
txnData | TradeBuildTxnResponse | no | Pre-built transaction data (skips build step if provided) |
TradeBuildTxnRequest Structure
| Parameter | Type | Required | Description |
|---|---|---|---|
fromChain | number | yes | Source chain ID |
sender | HexString | yes | Wallet address sending the transaction |
refundee | HexString | yes | Address to receive refunds |
gasless | boolean | yes | Set to false for standard execution; true for gasless (if supported) |
data | TradeBuildTxnRequestData[] | yes | Array of trade data |
disableEstimation | boolean | no | Disable gas estimation (default: false) |
disablePricing | boolean | no | Disable pricing calculations for faster builds |
publicKey | string | no | Public key for some chains (e.g., Solana) |
TradeBuildTxnRequestData Structure
| Parameter | Type | Required | Description |
|---|---|---|---|
amount | string | yes | Amount to trade (in token units) |
srcToken | string | yes | Source token address |
destToken | string | yes | Destination token address |
toChain | number | yes | Destination chain ID |
protocol | string | yes | Protocol identifier from quotes |
recipient | string | yes | Address to receive destination tokens |
slippage | number | yes | Slippage tolerance percentage |
additionalInfo | AdditionalInfo | no | Additional route-specific data |
permitData | string | no | Pre-signed permit data |
TradeBuildTxnResponse (build response)
buildTradeTxn(request) returns an object with:
- status —
"success"when the build succeeds - txId — Unique build/transaction ID (hex string)
- chainId — Chain where the transaction is to be executed
- transaction — EVM payload:
{ data, to, from, value, gasLimit }(hex calldata, contract address, sender, value in wei, gas limit) - gasless —
falsefor standard EVM;truefor gasless/intent flows - quotes — Per-pair summary keyed by pair ID:
{ additionalInfo?: { requestId, bridgeProvider, srcSwapProvider }, provider, destAmount, minDestAmount }
txnData to trade({ request, signer, txnData }) to skip the build step. See Types reference for the full shape.
Complete Example with Quotes and Approvals
Here’s a full example that gets quotes, handles approvals, and executes a trade. ThegetAllowance response has data keyed by token address; each entry has approvalNeeded and signatureNeeded:
Viem/wagmi: If using wagmi v2 or viem v2, you may need to cast the wallet
client:
signer: walletClient as any when passing to approve() or
trade().Error handling and retry (TRY_ANOTHER_ROUTE)
When a trade fails, the response may includeaction: "TRY_ANOTHER_ROUTE". In that case, retry the trade using the fallback route (bestReturnSource from the quote) instead of recommendedSource:
Best Practices
- Always validate balances before execution
- Check allowances and execute approvals if needed
- Implement proper error handling with retry logic (including
TRY_ANOTHER_ROUTE) - Use appropriate slippage settings based on market conditions
- Monitor transaction status for cross-chain trades
Next Steps
After executing trades:- Track the trade status to monitor progress