so i have this code const { ethers } = require('ethers');
const { Token, CurrencyAmount, Percent, TradeType } = require('@uniswap/sdk-core');
const { Pool, Route, Trade } = require('@uniswap/v3-sdk');
const { abi: IUniswapV3PoolABI } = require('@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json');
const { SwapRouter } = require('@uniswap/v3-periphery/artifacts/contracts/SwapRouter.sol/SwapRouter.json');
const { abi: Quoter } = require('@uniswap/v3-periphery/artifacts/contracts/lens/Quoter.sol/Quoter.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 getPoolInfo(tokenIn, tokenOut, fees) {
// Fixed from here
const currentPoolAddress = Pool.getAddress(tokenIn, tokenOut, fees);
const poolContract = new ethers.Contract(currentPoolAddress, IUniswapV3PoolABI, provider);
const [token0, token1, fee, tickSpacing, liquidity, slot0] = await Promise.all([
poolContract.token0(),
poolContract.token1(),
poolContract.fee(),
poolContract.tickSpacing(),
poolContract.liquidity(),
poolContract.slot0(),
]);
return {
token0,
token1,
fee,
tickSpacing,
liquidity,
sqrtPriceX96: slot0[0],
tick: slot0[1],
};
}
async function getOutputQuote(tokenIn, tokenOut, fees, amountIn) {
const quoterContract = new ethers.Contract(QUOTER_CONTRACT_ADDRESS, Quoter, provider);
const quotedAmountOut = await quoterContract.callStatic.quoteExactInputSingle(tokenIn.address, tokenOut.address, fees, amountIn, 0);
return quotedAmountOut;
}
function fromReadableAmount(amount, decimals) {
const value = ethers.utils.parseUnits(amount, decimals);
return value.toString();
}
async function getTokenTransferApproval(tokenAddress) {
try {
const wallet = new ethers.Wallet(WALLET_SECRET, provider);
const tokenContract = new ethers.Contract(tokenAddress, Token.abi, wallet);
const spender = SWAP_ROUTER_ADDRESS;
const approvalAmount = ethers.constants.MaxUint256;
const connectedTokenContract = tokenContract.connect(wallet);
// Approve the spender (swap router) to spend the tokens on behalf of the user
const approvalTx = await connectedTokenContract.approve(spender, approvalAmount);
// Wait for the approval transaction to be mined
const approvalReceipt = await approvalTx.wait();
console.log('here');
// Check if the approval transaction was successful
if (approvalReceipt.status !== 1) {
throw new Error('Token transfer approval failed');
}
// Return the approval transaction hash or any other relevant information
return approvalReceipt.transactionHash;
} catch (e) {
console.log(e);
}
}
async function sendTransaction(tx, wallet) {
// Sign the transaction with the wallet's private key
const signedTx = await wallet.signTransaction(tx);
// Send the signed transaction
const txResponse = await provider.sendTransaction(signedTx);
// Wait for the transaction to be mined
const txReceipt = await txResponse.wait();
// Check if the transaction was successful
if (txReceipt.status !== 1) {
throw new Error('Transaction failed');
}
// Return the transaction hash or any other relevant information
return txReceipt.transactionHash;
}
async function main(tokenIn, tokenOut, fees) {
const poolInfo = await getPoolInfo(tokenIn, tokenOut, fees);
const pool = new Pool(tokenIn, tokenOut, fees, poolInfo.sqrtPriceX96.toString(), poolInfo.liquidity.toString(), poolInfo.tick);
const swapRoute = new Route([pool], tokenIn, tokenOut);
const amountIn = fromReadableAmount('0.001', 18);
const quotedAmountOut = await getOutputQuote(tokenIn, tokenOut, fees, amountIn);
const uncheckedTrade = Trade.createUncheckedTrade({
route: swapRoute,
inputAmount: CurrencyAmount.fromRawAmount(tokenIn, amountIn),
outputAmount: CurrencyAmount.fromRawAmount(tokenOut, quotedAmountOut.toString()),
tradeType: TradeType.EXACT_INPUT,
});
await getTokenTransferApproval(tokenIn.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);
}
main(token0, token1, fee); and this error Node.js v18.16.0
PS C:\Users\lidor\Desktop\Trade Bot> node index.js
TypeError: Cannot read properties of undefined (reading 'map')
at new Interface (C:\Users\lidor\Desktop\Trade Bot\node_modules@ethersproject\abi\lib\interface.js:100:65)
at BaseContract.getInterface (C:\Users\lidor\Desktop\Trade Bot\node_modules@ethersproject\contracts\lib\index.js:764:16)
at Contract.BaseContract (C:\Users\lidor\Desktop\Trade Bot\node_modules@ethersproject\contracts\lib\index.js:616:116)
at new Contract (C:\Users\lidor\Desktop\Trade Bot\node_modules@ethersproject\contracts\lib\index.js:1053:42)
at getTokenTransferApproval (C:\Users\lidor\Desktop\Trade Bot\index.js:65:27)
at main (C:\Users\lidor\Desktop\Trade Bot\index.js:122:9)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
C:\Users\lidor\Desktop\Trade Bot\index.js:129
const methodParameters = SwapRouter.swapCallParameters(uncheckedTrade, options);
^
TypeError: Cannot read properties of undefined (reading 'swapCallParameters')
at main (C:\Users\lidor\Desktop\Trade Bot\index.js:129:39)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v18.16.0
PS C:\Users\lidor\Desktop\Trade Bot>