IntegrationsQuick StartV3
Swap with V3
Be sure to respect all the necessary requirements present in V3
Be sure to respect all the necessary requirements present in V3
Swapping Tokens
Use the provided swapTokens function to swap ETH for USDC:
-
Open the index.js file in your editor.
-
Replace the placeholder values in the main() function:
async function main() {
try {
console.log('Initializing wallet...');
// Swap example
const swapParams = {
tokenIn: process.env.IN_ADDRESS,
tokenOut: process.env.OUT_ADDRESS,
recipient: wallet.address,
amountIn: ethers.utils.parseUnits("0.001", 18),
amountOutMinimum: ethers.utils.parseUnits("0", 8), // Minimum USDC expected,
};
console.log('Swapping tokens...');
const receipt = await exactInputSingleSwap(provider, wallet, swapParams.amountIn, swapParams.recipient, swapParams.amountOutMinimum, swapParams.tokenIn, swapParams.tokenOut);
console.log('Swap completed! Transaction hash:', receipt.transactionHash);
} catch (error) {
console.error('An error occurred:', error);
}
}Note that in order to execute the swap correctly, you need to provide the decimals of each token you wish to swap
- Run the script:
node index.js