Quickstart
Get a quote and executable transaction with one allowance request, then confirm and submit.
This Direct swap uses your application's existing signer to supply tokens, submit the transaction and receive the output. For separate payer and executor accounts, see Delegated Swaps.
Call swapAllowance() once to obtain the quote, selected paths and validated transaction. Review its terms and submit the same result after confirmation.
Install and initialize
npm install @masterpeach/aggregator-sdk@latest ethers@^6Initialize the client with BSC Mainnet defaults and use an existing ethers v6 signer on chain ID 56. See Overview for production configuration and service compatibility.
import { ethers } from "ethers";
import { BSC_MAINNET_CONFIG, PeachClient } from "@masterpeach/aggregator-sdk";
const client = new PeachClient(BSC_MAINNET_CONFIG);Get the quote and transaction
Use the application's tokenIn, tokenOut, input amount and outputDecimals. Validate the decimal input and convert it with ethers.parseUnits(value, inputDecimals). amount is a positive bigint in input-token base units. Obtain output decimals from token metadata; native BNB uses 18.
Prepare and review
swapAllowance() requests fresh transaction data and a signed execution plan from the Peach API. The SDK validates them; your signer later signs and submits the transaction.
const executor = await signer.getAddress();
const prepared = await client.swapAllowance({
from: tokenIn, target: tokenOut, amount, executor,
slippageBps: 50, // 0.5%; 100 bps = 1%
deadlineSecs: 300,
});
if (prepared.amountIn !== amount) throw new Error("Prepared input differs from the order");
console.log("Expected output:", ethers.formatUnits(prepared.expectAmountOut, outputDecimals));
console.log("Minimum received:", ethers.formatUnits(prepared.tx.amountOutMin, outputDecimals));Render the logged expected output and minimum received in your confirmation view. Wait for the user to accept these fresh terms before approval or submission. Logging them alone is not confirmation.
prepared.expectAmountOut is net recipient output; prepared.tx.amountOutMin is the signed minimum. Display both with output-token decimals without deducting fees or slippage again. Use the user's chosen slippageBps; the returned signed deadline determines validity.
Approve and execute
Run this step only for the accepted plan. Do not call swapAllowance() again in the submit handler.
The approval helper skips native BNB and sufficient existing allowance. For ERC20 approval, the helper requests unlimited allowance to the validated Execution Router; disclose this in the wallet flow. Wait for approval confirmation before sending the swap.
Optional simulation fits after approval and before executeSwap(): call client.simulateSwap(prepared, prepared.executor). Its userAmountOut is a current-state estimate, not the final settled amount. See Direct Swaps.
When to prepare again
After order, wallet or network changes, or if approval consumes the plan's validity, prepare and review fresh terms. Keep confirmed allowance where sufficient. Never edit signed fields, and disable duplicate submission while the trade is active.
If a send times out, inspect the transaction hash or wallet nonce before retrying; the transaction may already have been sent. See Client Configuration for error handling.
Next steps
See Quotes and Routing for prepared quotes and route display, Slippage and Fees for trade settings, and Transaction Safety for validation and expiry. For upgrades, see Migration Guide.
Last updated
Was this helpful?