Here is my code…
require(‘dotenv’).config();
const {ethers} = require(‘ethers’)
const { abi: IUniswapV3PoolABI } = require(‘@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json’)
const { abi: SwapRouterABI} = require(‘@uniswap/v3-periphery/artifacts/contracts/interfaces/ISwapRouter.sol/ISwapRouter.json’)
const { getPoolImmutables, getPoolState } = require(‘./helpers’)
const ERC20ABI = require(‘./abi.json’)
const INFURA_URL_TESTNET = process.env.INFURA_URL_TESTNET
const WALLET_ADDRESS = process.env.WALLET_ADDRESS
const WALLET_SECRET = process.env.WALLET_SECRET
const provider = new ethers.providers.JsonRpcProvider(INFURA_URL_TESTNET) // Goerli
const poolAddress = ‘0xA374094527e1673A86dE625aa59517c5dE346d32’
const swapRouterAddress = ‘0xE592427A0AEce92De3Edee1F18E0157C05861564’;
const name0 = ‘Matic’
const symbol0 = ‘MATIC’
const decimals0 = 18;
const address0 = ‘0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270’;
const name1 = ‘Token USD Coin’
const symbol1 = ‘USDC’
const decimals1 = 6;
const address1 = ‘0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174’;
async function getGasPrice() {
const price = await provider.getGasPrice();
return price;
}
async function main() {
const poolContract = new ethers.Contract(
poolAddress,
IUniswapV3PoolABI,
provider
)
const immutables = await getPoolImmutables(poolContract)
const state = await getPoolState(poolContract)
const wallet = new ethers.Wallet(WALLET_SECRET)
const connectedWallet = wallet.connect(provider)
const swapRouterContract = new ethers.Contract(
swapRouterAddress,
SwapRouterABI,
provider
)
const amountIn = ethers.utils.parseUnits(“5”, decimals0);
const approvalAmount = amountIn.mul(ethers.BigNumber.from(100000)); // Using BigNumber multiplication
const tokenContract0 = new ethers.Contract(
address0,
ERC20ABI,
provider
);
const approvalResponse = await tokenContract0.connect(connectedWallet).approve(
swapRouterAddress,
approvalAmount
);
const params = {
tokenIn: address0, // WMATIC
tokenOut: address1, // USDC
fee: immutables.fee,
recipient: WALLET_ADDRESS,
deadline: Math.floor(Date.now() / 1000) + 600, // Adding 10 minutes to current timestamp
amountIn: amountIn,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0,
};
const maxPriorityFeePerGas = ethers.BigNumber.from(80000000000)
const maxFeePerGas = ethers.BigNumber.from(80000000000)
const transaction = await swapRouterContract.connect(connectedWallet).exactInputSingle(
params,
{
gasLimit: ethers.utils.hexlify(3000000),
maxPriorityFeePerGas: maxPriorityFeePerGas,
maxFeePerGas: maxFeePerGas
}
);
console.log(transaction);
}
main();
error —>>
Error: processing response error (body=“{"jsonrpc":"2.0","id":59,"error":{"code":-32000,"message":"transaction underpriced"}}”, error={“code”:-32000}, requestBody=“{"method":"eth_sendRawTransaction","params":["0x02f8b18189018459682f00850e5f8be36c82b41d940d500b1d8e8ef31e21c99d1db9a6444d3adf127080b844095ea7b3000000000000000000000000e592427a0aece92de3edee1f18e0157c058615640000000000000000000000000000000000000000000069e10de76676d0800000c080a0b50e39522f672ea4c042cc4f2d58722c8caadd0c87e89e6f220598ac2bdde189a070750b907517323646be1a33b21cdf62fc2a1d8be6cafc6f8e33e465ea4e007d"],"id":59,"jsonrpc":"2.0"}”, requestMethod=“POST”, url=“https://polygon-mainnet.infura.io/v3/xxx”, code=SERVER_ERROR, version=web/5.7.1)
at Logger.makeError (c:\Projetos_Flutter\unipoly\node_modules@ethersproject\logger\lib\index.js:238:21)
at Logger.throwError (c:\Projetos_Flutter\unipoly\node_modules@ethersproject\logger\lib\index.js:247:20)
at c:\Projetos_Flutter\unipoly\node_modules@ethersproject\web\lib\index.js:313:32
at step (c:\Projetos_Flutter\unipoly\node_modules@ethersproject\web\lib\index.js:33:23)
at Object.next (c:\Projetos_Flutter\unipoly\node_modules@ethersproject\web\lib\index.js:14:53)
at fulfilled (c:\Projetos_Flutter\unipoly\node_modules@ethersproject\web\lib\index.js:5:58)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
reason: ‘processing response error’,
code: ‘SERVER_ERROR’,
body: ‘{“jsonrpc”:“2.0”,“id”:59,“error”:{“code”:-32000,“message”:“transaction underpriced”}}’,
error: Error: transaction underpriced
at getResult (c:\Projetos_Flutter\unipoly\node_modules\ethers\node_modules@ethersproject\providers\lib\json-rpc-provider.js:191:21)
at processJsonFunc (c:\Projetos_Flutter\unipoly\node_modules@ethersproject\web\lib\index.js:356:22)
at c:\Projetos_Flutter\unipoly\node_modules@ethersproject\web\lib\index.js:288:46
at step (c:\Projetos_Flutter\unipoly\node_modules@ethersproject\web\lib\index.js:33:23)
at Object.next (c:\Projetos_Flutter\unipoly\node_modules@ethersproject\web\lib\index.js:14:53)
at fulfilled (c:\Projetos_Flutter\unipoly\node_modules@ethersproject\web\lib\index.js:5:58)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: -32000,
data: undefined
},
requestBody: ‘{“method”:“eth_sendRawTransaction”,“params”:[“0x02f8b18189018459682f00850e5f8be36c82b41d940d500b1d8e8ef31e21c99d1db9a6444d3adf127080b844095ea7b3000000000000000000000000e592427a0aece92de3edee1f18e0157c058615640000000000000000000000000000000000000000000069e10de76676d0800000c080a0b50e39522f672ea4c042cc4f2d58722c8caadd0c87e89e6f220598ac2bdde189a070750b907517323646be1a33b21cdf62fc2a1d8be6cafc6f8e33e465ea4e007d”],“id”:59,“jsonrpc”:“2.0”}’,
requestMethod: ‘POST’,
url: ‘https://polygon-mainnet.infura.io/v3/xx’,
transaction: {
type: 2,
chainId: 137,
nonce: 1,
maxPriorityFeePerGas: BigNumber { _hex: ‘0x59682f00’, _isBigNumber: true },
maxFeePerGas: BigNumber { _hex: ‘0x0e5f8be36c’, _isBigNumber: true },
gasPrice: null,
gasLimit: BigNumber { _hex: ‘0xb41d’, _isBigNumber: true },
to: ‘0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270’,
value: BigNumber { _hex: ‘0x00’, _isBigNumber: true },
data: ‘0x095ea7b3000000000000000000000000e592427a0aece92de3edee1f18e0157c058615640000000000000000000000000000000000000000000069e10de76676d0800000’,
accessList: ,
hash: ‘0x0c12dfe7496e091b73fcbc8572c5b353d6d46de6d43a4f1d75e336c5661071d7’,
v: 0,
r: ‘0xb50e39522f672ea4c042cc4f2d58722c8caadd0c87e89e6f220598ac2bdde189’,
s: ‘0x70750b907517323646be1a33b21cdf62fc2a1d8be6cafc6f8e33e465ea4e007d’,
from: ‘0xC5e543b37aCD47C12035CDE2dd7B997d2b5B2c81’,
confirmations: 0
},
transactionHash: ‘0x0c12dfe7496e091b73fcbc8572c5b353d6d46de6d43a4f1d75e336c5661071d7’
}