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 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();
codeconst 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); const gasEstimate = await provider.estimateGas({ to: methodParameters.address, data: methodParameters.calldata, value: methodParameters.value, }); const wallet = new ethers.Wallet(WALLET_SECRET, provider); const tx = { data: methodParameters.calldata, to: methodParameters.address, value: methodParameters.value, gasLimit: gasEstimate, gasPrice: await provider.getGasPrice(), }; const res = await sendTransaction(tx, wallet); console.log(res); - Assistant
The error is due to a variable naming conflict. In the
getPoolInfo()function, you are defining variables with the same names as thetoken0andtoken1constants declared at the beginning of your code. This causes a reference error, making thetoken0andtoken1constants inaccessible.To fix this issue, you can change the variables names inside the
getPoolInfo()function to avoid conflicts, like this:javascriptasync function getPoolInfo() { try { // Fixed from here const currentPoolAddress = Pool.getAddress(token0, token1, fee); const poolContract = new ethers.Contract(currentPoolAddress, IUniswapV3PoolABI, provider); const [poolToken0, poolToken1, poolFee, tickSpacing, liquidity, slot0] = await Promise.all([ poolContract.token0(), poolContract.token1(), poolContract.fee(), poolContract.tickSpacing(), poolContract.liquidity(), poolContract.slot0(), ]); return { token0: poolToken0, token1: poolToken1, fee: poolFee, tickSpacing, liquidity, sqrtPriceX96: slot0[0], tick: slot0[1], }; } catch (e) { console.log(e); } } - User
now i ge tthis PS C:\Users\lidor\Desktop\Trade Bot> node . Error: invalid address or ENS name (argument="name", value={"chainId":1,"decimals":18,"symbol":"WETH","name":"Wrapped Ether","isNative":false,"isToken":true,"address":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"}, code=INVALID_ARGUMENT, version=contracts/5.7.0) at Logger.makeError (C:\Users\lidor\Desktop\Trade Bot\node_modules@ethersproject\logger\lib\index.js:238:21) at Logger.throwError (C:\Users\lidor\Desktop\Trade Bot\node_modules@ethersproject\logger\lib\index.js:247:20) at Logger.throwArgumentError (C:\Users\lidor\Desktop\Trade Bot\node_modules@ethersproject\logger\lib\index.js:250:21) at C:\Users\lidor\Desktop\Trade Bot\node_modules@ethersproject\contracts\lib\index.js:94:32 at step (C:\Users\lidor\Desktop\Trade Bot\node_modules@ethersproject\contracts\lib\index.js:48:23) at Object.next (C:\Users\lidor\Desktop\Trade Bot\node_modules@ethersproject\contracts\lib\index.js:29:53) at fulfilled (C:\Users\lidor\Desktop\Trade Bot\node_modules@ethersproject\contracts\lib\index.js:20:58) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { reason: 'invalid address or ENS name', code: 'INVALID_ARGUMENT', argument: 'name', value: Token { chainId: 1, decimals: 18, symbol: 'WETH', name: 'Wrapped Ether', isNative: false, isToken: true, address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' } } TypeError: Cannot read properties of undefined (reading 'toString') at main (C:\Users\lidor\Desktop\Trade Bot\index.js:36:74) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) PS C:\Users\lidor\Desktop\Trade Bot>
- Assistant
The error is caused by passing the
token0object instead oftoken0.addresswhen creating the quoterContract object in thegetOutputQuote()function. You need to pass the token address rather than the token object itself. Update thegetOutputQuote()function like this:javascriptasync function getOutputQuote(amountIn) { try { const quoterContract = new ethers.Contract(QUOTER_CONTRACT_ADDRESS, Quoter, provider); const quotedAmountOut = await quoterContract.callStatic.quoteExactInputSingle(token0.address, token1.address, fee, amountIn, 0); return quotedAmountOut; } catch (e) { console.log(e); } }This should fix the error.
- User
- Assistant
The error is caused by the unresolved promises in the
txobject. You need to useawaitto get the values of the unresolved promises. Update themainfunction like this:javascript - User
C:\Users\lidor\Desktop\Trade Bot\index.js:30 const amountIn = fromReadableAmount(‘0.001’, 18);
SyntaxError: Invalid or unexpected token at internalCompileFunction (node:internal/vm:73:18) at wrapSafe (node:internal/modules/cjs/loader:1176:20) at Module._compile (node:internal/modules/cjs/loader:1218:27) at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Module._load (node:internal/modules/cjs/loader:958:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47
Node.js v18.16.0 PS C:\Users\lidor\Desktop\Trade Bot>
- Assistant
It looks like there is a syntax error in your code caused by the usage of non-standard quotes (‘’). You should replace the non-standard quotes with the standard single quotes (
'') or double quotes (""). Update the line like this:javascriptconst amountIn = fromReadableAmount('0.001', 18);This should fix the syntax error.