const { ethers } = require('ethers'); const { Token, CurrencyAmount, Percent, TradeType…
- User
const { ethers } = require('ethers'); const { Token, CurrencyAmount, Percent, TradeType } = require('@uniswap/sdk-core'); const { Pool, Route, Trade, SwapRouter } = require('@uniswap/v3-sdk'); const IUniswapV3PoolABI = require('@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json').abi; const Quoter = require('@uniswap/v3-periphery/artifacts/contracts/lens/Quoter.sol/Quoter.json').abi; const IERC20 = require('@uniswap/v2-periphery/build/IERC20.json');
const SWAP_ROUTER_ADDRESS = '0xe592427a0aece92de3edee1f18e0157c05861564'; const WALLET_ADDRESS = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'; const WALLET_SECRET = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'; const INFURA_TEST_URL = 'https://mainnet.infura.io/v3/2d5bc62bb8d748cebfc64763e719cb4f'; const POOL_FACTORY_CONTRACT_ADDRESS = '0x1F98431c8aD98523631AE4a59f267346ea31F984'; const QUOTER_CONTRACT_ADDRESS = '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6';
const provider = new ethers.providers.JsonRpcProvider(INFURA_TEST_URL); const chainId = 1;
const wallet = new ethers.Wallet(WALLET_SECRET, provider);
const token0 = new Token(chainId, '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', 18, 'WETH', 'Wrapped Ether'); const token1 = new Token(chainId, '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984', 18, 'UNI', 'Uniswap Token');
const fee = 3000; // Change the fee tier as required (500 for low, 3000 for medium, and 10000 for high)
async function main() { try { const poolInfo = await getPoolInfo();
const pool = new Pool(token0, token1, fee, poolInfo.sqrtPriceX96.toString(), poolInfo.liquidity.toString(), poolInfo.tick); const swapRoute = new Route([pool], token0, token1); const amountIn = fromReadableAmount('0.001', 18); const quotedAmountOut = await getOutputQuote(amountIn); const uncheckedTrade = Trade.createUncheckedTrade({ route: swapRoute, inputAmount: CurrencyAmount.fromRawAmount(token0, amountIn), outputAmount: CurrencyAmount.fromRawAmount(token1, quotedAmountOut.toString()), tradeType: TradeType.EXACT_INPUT, }); await getTokenTransferApproval(token0.address); const options = { slippageTolerance: new Percent(50, 10_000), // 50 bips, or 0.50% deadline: Math.floor(Date.now() / 1000) + 60 * 20, // 20 minutes from the current Unix time }; const methodParameters = SwapRouter.swapCallParameters(uncheckedTrade, options); console.log('Address to validate:', methodParameters.address); const tx = { data: methodParameters.calldata, to: methodParameters.address, value: methodParameters.value, gasLimit: ethers.BigNumber.from('100000'), // Add a suitable value for the gas limit gasPrice: await provider.getGasPrice(), }; const res = await sendTransaction(tx); console.log(res);} catch (e) { console.log(e); } } main();
async function getPoolInfo() { try { // Fixed from here const currentPoolAddress = Pool.getAddress(token0, token1, fee);
- Assistant
It seems that there are two errors that occurred while running your code. Let's address each one of them:
-
Gas estimation error The first error occurs during the gas estimation (eth_estimateGas). The error message - "gas required exceeds allowance (0)" indicates that the available gas is insufficient.
To resolve this error, instead of estimating the gas, you can simply provide a reasonable gas limit while preparing the transaction. You can adjust the gasLimit value in the
txobject in themain()function:const tx = { data: methodParameters.calldata, to: methodParameters.address, value: methodParameters.value, gasLimit: ethers.BigNumber.from('1000000'), // increase this value accordingly gasPrice: await provider.getGasPrice() }; -
Invalid address error The second error occurs when the code tries to call method. The error message - "undefined is not a valid address", indicates that the address passed to the function is not valid.
-
- User
still the error
- Assistant
I apologize for the confusion. Let's try to resolve the issues step by step.
- Set a specific gas limit instead of estimating gas.
Change this line:
gasLimit: ethers.BigNumber.from('100000'), // Add a suitable value for the gas limitTo:
gasLimit: ethers.BigNumber.from('1000000'),- Update the
SWAP_ROUTER_ADDRESS.
Change this line:
const SWAP_ROUTER_ADDRESS = '0xe592427a0aece92de3edee1f18e0157c05861564';To:
const SWAP_ROUTER_ADDRESS = '0xE592427A0AEce92De3Edee1F18E0157C05861564';- Before calling
SwapRouter.swapCallParameters(uncheckedTrade, options);, let's double-check if theuncheckedTradeobject contains valid addresses.
Add the following lines just before calling
SwapRouter.swapCallParametersin the function: - User
- Assistant
I apologize for the confusion earlier. The error seems to be related to the
SwapRouternot being initialized properly.