how to calculate minimum amount out here const ethers = require('ethers');
const { computePoolAddress } = require('@uniswap/v3-sdk');
const { Token } = require('@uniswap/sdk-core');
// Ethereum network configuration
const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545/');

// Uniswap V3 contract configuration
const uniswapV3SwapRouterAddress = '0xE592427A0AEce92De3Edee1F18E0157C05861564';
const PoolFactoryContractAddress = '0x1F98431c8aD98523631AE4a59f267346ea31F984';
const quoterContractAddress = '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6';
const uniswapV3SwapRouterABI = require('@uniswap/v3-periphery/artifacts/contracts/SwapRouter.sol/SwapRouter.json').abi;
const uniswapV3PoolABI =
  require('@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json').abi;
const uniswapV3QuoterAbi = require('@uniswap/v3-periphery/artifacts/contracts/lens/Quoter.sol/Quoter.json').abi;

const privateKey1 = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
const token0Address1 = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2';
const token1Address1 = '0xdAC17F958D2ee523a2206206994597C13D831ec7';

// Load the Uniswap V3 contract
const maxFeePerGas = ethers.parseUnits('50', 'gwei');
const maxPriorityFeePerGas = ethers.parseUnits('2', 'gwei');

exports.executeTrade = async function executeTrade(token0Address, token1Address, privateKey, amount) {
  try {
    const wallet = new ethers.Wallet(privateKey, provider);
    const uniswapV3SwapRouterContract = new ethers.Contract(uniswapV3SwapRouterAddress, uniswapV3SwapRouterABI, wallet);
    const token0Properties = await getTokenProperties(token0Address);
    const token1Properties = await getTokenProperties(token1Address);

    const swapAmount = ethers.parseUnits(amount.toString(), token0Properties.decimals);

    // Approve Uniswap V3 SwapRouter to spend token0Instance
    console.log('Approving token spend by SwapRouter…');
    const tokenToSwapContract = new ethers.Contract(
      token0Address,
      ['function approve(address spender, uint256 amount) external returns (bool)'],
      wallet
    );
    const approvalTx = await tokenToSwapContract.approve(uniswapV3SwapRouterAddress, swapAmount, {
      maxFeePerGas,
      maxPriorityFeePerGas,
      gasLimit: 25000000,
    });

    await approvalTx.wait();
    console.log('Token spend approval transaction confirmed.');

    const slippageTolerance = 0.01; // 1%, for example. You can adjust this value according to your needs.
    const minimumAmountOut =
      (BigInt(quotedAmountOut) * BigInt(Math.floor((1 - slippageTolerance) * 10 ** 18))) / BigInt(10 ** 18);

    const swapParams = {
      tokenIn: token0Address,
      tokenOut: token1Address,
      fee: 3000,
      recipient: wallet.address,
      deadline: Math.floor(Date.now() / 1000) + 60 * 20,
      amountIn: swapAmount,
      amountOutMinimum: minimumAmountOut,
      sqrtPriceLimitX96: 0,
    };

    console.log('Sending swap transaction…');
    const swapTx = await uniswapV3SwapRouterContract.exactInputSingle(swapParams, {
      maxFeePerGas: maxFeePerGas,
      maxPriorityFeePerGas: maxPriorityFeePerGas,
      gasLimit: 25000000,
    });

    console.log('Swap transaction hash:', swapTx.hash);

    // Wait for transaction confirmation
    console.log('Waiting for swap transaction confirmation…');
    await swapTx.wait();
    console.log('Swap transaction confirmed!');
    return `Swapped ${ethers.formatUnits(swapAmount, token0Properties.decimals)} ${token0Properties.name} for ${
      token1Properties.name
    }`;
  } catch (e) {
    console.log(e);
    return null;
  }
};