generateCalldata
The generateCalldata
method encodes the transaction data for a swap without executing it. This is useful for transaction simulation, gas estimation, or integrating with other smart contracts.
Signature
async generateCalldata({
from?: string | WalletClient | Signer,
route?: Route,
includesNative?: boolean,
slippage?: {
slippageAmount?: string,
isRaw?: boolean,
isBps?: boolean,
}
} = {}): Promise<{
from: string,
to: string,
data: string,
value: string
}>
Parameters
from
(optional): The sender's address or wallet client. Defaults to the current signer/wallet.route
(optional): The route to use for the swap. Defaults to the route instance's route.includesNative
(optional): Whether the swap includes native token. Defaults to the route instance's value.slippage
(optional): Slippage configuration:slippageAmount
: Slippage tolerance. Defaults to config value.isRaw
: If true, slippage is in raw units. Defaults to true.isBps
: If true, slippage is in basis points. Defaults to false.
Returns
Returns a Promise that resolves to a transaction object containing:
from
: The validated sender addressto
: The Symphony contract addressdata
: The encoded calldata for the swapvalue
: Amount of native tokens (if applicable)
Example Usage
Viem
const symphony = new Symphony();
// Get a route
const route = await symphony.getRoute(tokenIn, tokenOut, "1.0");
// Generate calldata
const tx = await route.generateCalldata({
from: "0xa43aed657D44e432121843B8784eFc3dE3Cf3Aa2",
});
// Simulate the transaction
const result = await publicClient.call(tx);
console.log(`Simulation result: ${result}`);
//Example output
{
"account": "0xa43aed657D44e432121843B8784eFc3dE3Cf3Aa2",
"to": "0x4b5e80Ff7e93B4055E3c4482F7567491E274004C",
"data": "0xdc1a.............00000000bb8",
"value": "100000000000000000"
}
Notes
- The method validates the
from
address, whether it's provided as a string or through a wallet client. - If
from
argument is not provided, function falls back to existing wallet connected to Symphony SDK if any. - For native token swaps, the
value
field will contain the amount of native tokens to send. - The generated calldata includes slippage protection based on the provided configuration.
- The transaction sender is labeled as
from
on Ethers V5 and Ethers V6 implementations. On Viem, transaction sender is labeled asaccount
as Viem expects transaction sender address underaccount
field on followup interactions.