PS C:\Users\lidor\Desktop\Trade Bot> node index.js TypeError: tokenA.sortsBefore is not…
- User
PS C:\Users\lidor\Desktop\Trade Bot> node index.js TypeError: tokenA.sortsBefore is not a function at computePoolAddress (C:\Users\lidor\Desktop\Trade Bot\node_modules@uniswap\v3-sdk\dist\v3-sdk.cjs.development.js:946:22) at test (C:\Users\lidor\Desktop\Trade Bot\index.js:33:32) at Object. (C:\Users\lidor\Desktop\Trade Bot\index.js:53:1) at Module._compile (node:internal/modules/cjs/loader:1254:14) 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 this error here const ethers = require('ethers'); const { Pool, computePoolAddress } = require('@uniswap/v3-sdk'); const { Token, Percent } = require('@uniswap/sdk-core');
// Ethereum network configuration const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545/');
const privateKey = 'ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
// 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;
// Token configuration const tokenToSwapAddress = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'; const tokenToReceiveAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7';
const tokenToSwapAmount = ethers.parseUnits('100', 'ether');
// Connect to wallet const wallet = new ethers.Wallet(privateKey, provider);
// Load the Uniswap V3 contract const uniswapV3SwapRouterContract = new ethers.Contract(uniswapV3SwapRouterAddress, uniswapV3SwapRouterABI, wallet); const customGasPrice = ethers.parseUnits('50', 'gwei');
const test = async () => { try { const currentPoolAddress = computePoolAddress({ factoryAddress: PoolFactoryContractAddress, tokenA: tokenToSwapAddress, tokenB: tokenToReceiveAddress, fee: 3000, });
const poolContract = new ethers.Contract(currentPoolAddress, uniswapV3PoolABI, provider); const [token0, token1, fee] = await Promise.all([poolContract.token0(), poolContract.token1(), poolContract.fee()]); console.log(token0, token1); const quoterContract = new ethers.Contract(quoterContractAddress, uniswapV3QuoterAbi, provider); const quotedAmountOut = await quoterContract.callStatic.quoteExactInputSingle(token0, token1, fee, fromReadableAmount(tokenToSwapAmount, 18).toString(), 0); console.log(quotedAmountOut); - Assistant
The issue seems to be with the
computePoolAddressfunction arguments.tokenAandtokenBshould be instances of theTokenclass, not just their addresses.Please update your code as follows:
// Token configuration const tokenToSwapAddress = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'; const tokenToReceiveAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7'; // Convert token addresses to Token instances const tokenToSwap = new Token(1, tokenToSwapAddress, 18, 'WETH', 'Wrapped Ether'); const tokenToReceive = new Token(1, tokenToReceiveAddress, 6, 'USDT', 'Tether USD'); const tokenToSwapAmount = ethers.parseUnits('100', 'ether'); // ... const test = async () => { try { const currentPoolAddress = computePoolAddress({ factoryAddress: PoolFactoryContractAddress, tokenA: tokenToSwap, // Use the Token instance tokenB: tokenToReceive, // Use the Token instance fee: 3000, }); // ... } catch (e) { console.log(e); } }; test();With this change, your code should work correctly as it now uses
Tokeninstances fortokenAandtokenBin thecomputePoolAddressfunction. - User
eferenceError: fromReadableAmount is not defined at test (C:\Users\lidor\Desktop\Trade Bot\index.js:53:88) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
- Assistant
I apologize for the confusion. I made an incorrect assumption about the
fromReadableAmountfunction in your code. Since it's not defined, you can use theethers.utils.parseUnitsfunction to achieve the same result. Replace this line:const quotedAmountOut = await quoterContract.callStatic.quoteExactInputSingle(token0, token1, fee, fromReadableAmount(tokenToSwapAmount, 18).toString(), 0);with:
const quotedAmountOut = await quoterContract.callStatic.quoteExactInputSingle(token0, token1, fee, tokenToSwapAmount.toString(), 0);This should work as expected, since
tokenToSwapAmountwas already converted to the desired format withethers.parseUnits('100', 'ether'). - User
Error: contract runner does not support sending transactions (operation="sendTransaction", code=UNSUPPORTED_OPERATION, version=6.4.1) at makeError (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\utils\errors.js:125:21) at assert (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\utils\errors.js:142:15) at send (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\contract\contract.js:203:31) at Proxy.quoteExactInputSingle (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\contract\contract.js:237:22)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: 'UNSUPPORTED_OPERATION', operation: 'sendTransaction' } Token spend approval transaction confirmed. Sending swap transaction… Waiting for swap transaction confirmation… Swap transaction confirmed! PS C:\Users\lidor\Desktop\Trade Bot> node index.js Approving token spend by SwapRouter… Token spend approval transaction confirmed. Sending swap transaction… Swap transaction hash: 0x548a22721eef5c81d14a56e0eef64d4d933806431f6915ab18864e8ee3a5e5d5 Waiting for swap transaction confirmation… Swap transaction confirmed! PS C:\Users\lidor\Desktop\Trade Bot> node index.js Approving token spend by SwapRouter… 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 0xdAC17F958D2ee523a2206206994597C13D831ec7 Error: contract runner does not support sending transactions (operation="sendTransaction", code=UNSUPPORTED_OPERATION, version=6.4.1) at makeError (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\utils\errors.js:125:21) at assert (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\utils\errors.js:142:15) at send (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\contract\contract.js:203:31) at Proxy.quoteExactInputSingle (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\contract\contract.js:237:22) at test (C:\Users\lidor\Desktop\Trade Bot\index.js:51:50) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: 'UNSUPPORTED_OPERATION', operation: 'sendTransaction' } Token spend approval trans - Assistant
It seems you are trying to send a transaction using a contract function marked as
callStatic. This is not supported, ascallStaticfunctions are meant for read-only operations and do not send transactions.Instead of using
callStatic.quoteExactInputSingle, you should use the appropriate function for the Uniswap V3 Quoter contract to obtain a quote for the swap.Please change the line:
const quotedAmountOut = await quoterContract.callStatic.quoteExactInputSingle(token0, token1, fee, tokenToSwapAmount.toString(), 0);to:
const quotedAmountOut = await quoterContract.estimateAmountOut(token0, token1, fee, tokenToSwapAmount.toString(), 0);This will use the correct function to estimate the output amount without attempting to perform a transaction.
Make sure the
estimateAmountOutfunction is available in the Quoter contract's ABI. If it's not, you can try using thequoteExactInputSinglefunction without thecallStaticpart, like this: - User
estimate isn't available i tried the single this is the eror Error: contract runner does not support sending transactions (operation="sendTransaction", code=UNSUPPORTED_OPERATION, version=6.4.1) at makeError (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\utils\errors.js:125:21) at assert (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\utils\errors.js:142:15) at send (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\contract\contract.js:203:31) at Proxy.quoteExactInputSingle (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\contract\contract.js:237:22)
at test (C:\Users\lidor\Desktop\Trade Bot\index.js:51:50) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: 'UNSUPPORTED_OPERATION', operation: 'sendTransaction' } - Assistant
Apologies for the confusion. Since
estimateAmountOutis not available, let's stick withcallStaticto make a read-only call toquoteExactInputSingle, but separately instantiate the quoterContract with a readonly provider.Please change your code as follows:
- Update your
quoterContractinstantiation to use a separate instance with the default provider, like this:
const quoterContract = new ethers.Contract(quoterContractAddress, uniswapV3QuoterAbi, provider); const quoterContractStatic = new ethers.Contract(quoterContractAddress, uniswapV3QuoterAbi, ethers.getDefaultProvider());- Replace the previous callStatic line with this:
const quotedAmountOut = await quoterContractStatic.callStatic.quoteExactInputSingle(token0, token1, fee, tokenToSwapAmount.toString(), 0);Now you should have two instances of the quoter contract - one for making transactions and other for making static calls. The error should be resolved.
- Update your
- User
PS C:\Users\lidor\Desktop\Trade Bot> node index.js Approving token spend by SwapRouter… 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 0xdAC17F958D2ee523a2206206994597C13D831ec7 TypeError: no matching function (argument="key", value="callStatic", code=INVALID_ARGUMENT, version=6.4.1) at makeError (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\utils\errors.js:118:21) at assert (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\utils\errors.js:142:15) at assertArgument (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\utils\errors.js:154:5) at Interface.getFunctionName (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\abi\interface.js:433:39) at buildWrappedMethod (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\contract\contract.js:240:34) at Contract.getFunction (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\contract\contract.js:688:22) at Object.get (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\contract\contract.js:598:39) at test (C:\Users\lidor\Desktop\Trade Bot\index.js:51:50) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: 'INVALID_ARGUMENT', argument: 'key', value: 'callStatic' }
- Assistant
Apologies for the confusion. It appears that
callStaticis not available as a direct method on the contract. We will need to make the static call manually usingeth_call. Here's how to do it:- Replace the previous callStatic line with this:
const data = quoterContract.interface.encodeFunctionData('quoteExactInputSingle', [token0, token1, fee, tokenToSwapAmount.toString(), 0]); const quotedAmountOut = await provider.call({ to: quoterContract.address, data });This will manually create an
eth_callrequest to the quoter contract'squoteExactInputSinglefunction, which should return the expected output amount without sending a transaction.Please update your code accordingly and let me know if this resolves the issue.
- User
TypeError: no matching function (argument="key", value="address", code=INVALID_ARGUMENT, version=6.4.1) at makeError (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\utils\errors.js:118:21) at assert (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\utils\errors.js:142:15) at assertArgument (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\utils\errors.js:154:5) at Interface.getFunctionName (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\abi\interface.js:433:39) at buildWrappedMethod (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\contract\contract.js:240:34) at Contract.getFunction (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\contract\contract.js:688:22) at Object.get (C:\Users\lidor\Desktop\Trade Bot\node_modules\ethers\lib.commonjs\contract\contract.js:598:39) at test (C:\Users\lidor\Desktop\Trade Bot\index.js:52:70) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: 'INVALID_ARGUMENT', argument: 'key', value: 'address' }
- Assistant
Apologies for the error in the previous response. I've realized that I made a mistake in accessing the contract properties. Here's the corrected version of making the static call:
- Replace the previous callStatic line with this:
const data = quoterContract.interface.encodeFunctionData('quoteExactInputSingle', [token0, token1, fee, tokenToSwapAmount.toString(), 0]); const quotedAmountOut = await provider.call({ to: quoterContractAddress, data });This will manually create an
eth_callrequest to the quoter contract'squoteExactInputSinglefunction. Please update your code accordingly and let me know if this resolves the issue. - User
i receive this output ? 0x0000000000000000000000000000000000000000000000000000002a7de26424 what does that mean
- Assistant
The output you received,
0x0000000000000000000000000000000000000000000000000000002a7de26424, is the estimated amount of tokens you will receive (in hexadecimal string format) for the given input amount after the swap.To convert this to a human-readable number, you can use the
ethers.utilslibrary as follows:const humanReadableAmountOut = ethers.utils.formatUnits(quotedAmountOut, 6); // here, the second argument should be the decimal count of the token you will receive console.log('Estimated amount out:', humanReadableAmountOut);Replace the
6with the actual number of decimals for the token you are expecting to receive. ThehumanReadableAmountOutwill contain the estimated output amount as a string in human-readable format. - User
how to get const tokenToSwap = new Token(1, tokenToSwapAddress, 18, 'WETH', 'Wrapped Ether'); const tokenToReceive = new Token(1, tokenToReceiveAddress, 6, 'USDT', 'Tether USD'); the whole token properties by the address
- Assistant
To get the token properties from the token address, you first need to interact with the ERC20 contract at that address. Here's a breakdown of the steps to get the token properties:
- Define the ERC20 token ABI:
const erc20ABI = [ "function name() view returns (string)", "function symbol() view returns (string)", "function decimals() view returns (uint8)", ];- Create a function to fetch token properties:
async function getTokenProperties(tokenAddress) { const tokenContract = new ethers.Contract(tokenAddress, erc20ABI, provider); const [name, symbol, decimals] = await Promise.all([ tokenContract.name(), tokenContract.symbol(), tokenContract.decimals(), ]); return { name, symbol, decimals, }; } - User
how to make this dynamic const tokenToSwapAmount = ethers.parseUnits('100', 'ether');
- Assistant
To make the
tokenToSwapAmountdynamic, you can pass the amount and decimals as variables. Here's an example:const amountToSwap = '100'; const tokenToSwapDecimals = tokenToSwapProperties.decimals; // assuming you've fetched the token properties as shown in the previous response const tokenToSwapAmount = ethers.utils.parseUnits(amountToSwap, tokenToSwapDecimals);This code creates the
tokenToSwapAmountusing the providedamountToSwapandtokenToSwapDecimals. Replace100andtokenToSwapProperties.decimalswith the desired values depending on your use case.