giveApproval
The giveApproval
method approves Symphony to spend tokens on behalf of the user. This approval is necessary before executing swaps with non-native tokens. You can now override the approval amount for custom strategies.
Signature
async giveApproval(params?: {
variable?: Route | string;
signer?: Signer; // ethers/ethersV5
walletClient?: WalletClient; // viem
amount?: string | bigint;
options?: {
isRaw?: boolean;
wait?: number;
}
}): Promise<TransactionReceipt>
TypeScript Types
// Import for viem
import { Route } from "symphony-sdk/viem";
// Import for ethersV5
import { Route } from "symphony-sdk/ethersV5";
import { TransactionReceipt } from "ethers";
// Import for ethers v6
import { Route } from "symphony-sdk/ethers";
import { TransactionReceipt } from "ethers";
Parameters
variable
: Route object or token address (defaults tothis.route
)signer
/walletClient
: Transaction signer (defaults to Route's signer)amount
: Custom approval amount (optional - defaults to route's input amount)- If
isRaw=true
: Amount in wei units - If
isRaw=false
: Amount in formatted units (e.g., "1.5")
- If
options
: Configuration optionsisRaw
: If true, amount is in wei units (default:true
)wait
: Number of confirmations to wait (default:1
)
Returns
Returns a Promise that resolves to the transaction receipt of the approval transaction (already waited for confirmations).
Example Usage
const symphony = new Symphony();
// Get a route
const route = await symphony.getRoute(tokenIn, tokenOut, "1.0");
// Request approval for exact route amount
const approvalTx = await route.giveApproval();
console.log(`Approval transaction: ${approvalTx.transactionHash}`);
// After approval is confirmed, you can swap
const { swapReceipt } = await route.swap();
Custom Approval Amount
// Approve a larger amount to avoid future approvals
const approvalTx = await route.giveApproval({
amount: "100.0", // Approve 100 tokens instead of route's 1.0
options: {
isRaw: false, // Using formatted amount
wait: 2, // Wait for 2 confirmations
},
});
// Or approve max amount for infinite approval
const maxApproval = await route.giveApproval({
amount: MAX_UINT_256,
options: { isRaw: true },
});
TypeScript Example
import { Route } from "symphony-sdk/ethers";
import { TransactionReceipt } from "ethers";
const route: Route = await symphony.getRoute(tokenIn, tokenOut, "1.0");
// Type-safe approval
const receipt: TransactionReceipt = await route.giveApproval({
signer: ethersSigner,
amount: "10.0",
options: {
isRaw: false,
wait: 3,
},
});
console.log(`Approved in block ${receipt.blockNumber}`);
Notes
- A wallet client/signer must be connected to give approval
- Without custom amount, approves the exact amount needed for the swap
- Transaction receipts are returned after waiting for confirmations
- No approval is needed for native token swaps
- You can skip manual approval by using
swap
withskipApproval: true
- Custom approval amounts enable strategies like:
- Infinite approvals to save gas on future transactions
- Batch approvals for multiple swaps
- Precise control for security requirements
Error Handling
The method will throw an error if:
- No wallet client/signer is connected
- The token address is invalid
- The token is not listed on Symphony
- The approval transaction fails
- Amount is invalid or negative
- Network issues or RPC errors occur