Infura Server response error / transaction underpriced

Hi! I’m a new Infura user (actually with Polygon) and I have developed a piece of code that worked fine in Mumbai Polygon Testnet, but now in Polygon Mainnet I can’t make it work. That’s is full code:

const { ethers, } = require(‘ethers’)
const { CurrencyAmount, Token,TradeType, Percent } = require(’@uniswap/sdk-core’)
const { Pool, Position, nearestUsableTick, TickMath, FullMath } = require(’@uniswap/v3-sdk’)
const { abi: IUniswapV3PoolABI } = require("@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json")
const { abi: INonfungiblePositionManagerABI } = require(’@uniswap/v3-periphery/artifacts/contracts/interfaces/INonfungiblePositionManager.sol/INonfungiblePositionManager.json’)
const JSBI =require(‘jsbi’)
const ERC20ABI = require(’./abi.json’)

require(‘dotenv’).config()
const INFURA_URL_MAINNET = process.env.INFURA_URL_MAINNET
const WALLET_ADDRESS_MAINNET = process.env.WALLET_ADDRESS_MAINNET
const WALLET_SECRET_MAINNET = process.env.WALLET_SECRET_MAINNET

const poolAddress = ‘0xa374094527e1673a86de625aa59517c5de346d32’ // MATIC - USDC on Polygon Mainnet
const positionManagerAddress = ‘0xc36442b4a4522e871399cd717abdd847ab11fe88’ // NonfungiblePositionManager

const name0 = ‘Wrapped Matic’
const symbol0 = ‘WMATIC’
const decimals0 = 18
const address0 = ‘0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270’

const name1 = ‘USD Coin (PoS)’
const symbol1 = ‘USDC’
const decimals1 = 6
const address1 = ‘0x2791bca1f2de4661ed88a30c99a7a9449aa84174’

const provider = new ethers.providers.JsonRpcProvider(INFURA_URL_MAINNET)
const chainId = 137 // Polygon Mainnet
const MaticToken = new Token(chainId, address0, decimals0, symbol0, name0)
const UsdcToken = new Token(chainId, address1, decimals1, symbol1, name1)

const nonfungiblePositionManagerContract = new ethers.Contract(
positionManagerAddress,
INonfungiblePositionManagerABI,
provider
)
const poolContract = new ethers.Contract(
poolAddress,
IUniswapV3PoolABI,
provider
)

async function getPoolData(poolContract) {
const [tickSpacing, fee, liquidity, slot0] = await Promise.all([
poolContract.tickSpacing(),
poolContract.fee(),
poolContract.liquidity(),
poolContract.slot0(),
])

return {
tickSpacing: tickSpacing,
fee: fee,
liquidity: liquidity,
sqrtPriceX96: slot0[0],
tick: slot0[1],
}
}

//CREATE NEW POOL
async function mainNewPool() {

const poolData = await getPoolData(poolContract)

const MATIC_USDC_POOL = new Pool(
MaticToken,
UsdcToken,
poolData.fee,
poolData.sqrtPriceX96.toString(),
poolData.liquidity.toString(),
poolData.tick
)

//TO MODIFY LIQUIDITY
const liq = 0.02
const approvalAmountLiq = liq * 2

const position = new Position({

pool: MATIC_USDC_POOL,
liquidity: ethers.utils.parseUnits(liq.toString(),18),
tickLower: nearestUsableTick(poolData.tick, poolData.tickSpacing) - poolData.tickSpacing * 10,
tickUpper: nearestUsableTick(poolData.tick, poolData.tickSpacing) + poolData.tickSpacing * 10,

})

const wallet = new ethers.Wallet(WALLET_SECRET_MAINNET)
const connectedWallet = wallet.connect(provider)

const approvalAmount0 = ethers.utils.parseUnits(approvalAmountLiq.toString(), decimals0)
console.log(approvalAmount0.toString())
const approvalAmount1 = ethers.utils.parseUnits(approvalAmountLiq.toString(), decimals1)
console.log(approvalAmount1.toString())

const tokenContract0 = new ethers.Contract(address0, ERC20ABI, provider)
const tokenC0 = await tokenContract0.connect(connectedWallet).approve(
positionManagerAddress,
approvalAmount0.toString()
)

const tokenContract1 = new ethers.Contract(address1, ERC20ABI, provider)
const tokenC1 = await tokenContract1.connect(connectedWallet).approve(
positionManagerAddress,
approvalAmount1.toString()
)

const { amount0: amount0Desired, amount1: amount1Desired} = position.mintAmounts
// mintAmountsWithSlippage

params = {
token0: address0,
token1: address1,
fee: poolData.fee,
tickLower: nearestUsableTick(poolData.tick, poolData.tickSpacing) - poolData.tickSpacing * 10,
tickUpper: nearestUsableTick(poolData.tick, poolData.tickSpacing) + poolData.tickSpacing * 10,
amount0Desired: amount0Desired.toString(),
amount1Desired: amount1Desired.toString(),
amount0Min: amount0Desired.toString(),
amount1Min: amount1Desired.toString(),
recipient: WALLET_ADDRESS_MAINNET,
deadline: Math.floor(Date.now() / 1000) + (60 * 10)
}

await nonfungiblePositionManagerContract.connect(connectedWallet).mint(
params,
{ gasLimit: ethers.utils.hexlify(300000) }
).then((res) => {
console.log(res)
})
}
mainNewPool()

As you can see, it’s a simple code for creating a Uniswap pool on Polygon Mainnet, but there is an error at ‘mainNewPool()’ function:

    var error = new Error(message);
                ^

Error: processing response error (body="{“jsonrpc”:“2.0”,“id”:58,“error”:{“code”:-32000,“message”:“transaction underpriced”}}", error={“code”:-32000}, requestBody="{“method”:“eth_sendRawTransaction”,“params”:[“0x02f8b18189058459682f0085018c41101082b3f9940d500b1d8e8ef31e21c99d1db9a6444d3adf127080b844095ea7b3000000000000000000000000c36442b4a4522e871399cd717abdd847ab11fe88000000000000000000000000000000000000000000000000008e1bc9bf040000c001a0e00713e805e844c56ddb213bbc1707a6f81375c771cba0eee4ea537db045ef7da0781b818051e30f94f5e8e08256288f4fd78d309f4c360e3edcfed3f95202682e”],“id”:58,“jsonrpc”:“2.0”}", requestMethod=“POST”, url=“https://polygon-mainnet.infura.io/v3/864606511d3147dabe0e17d331970b29”, code=SERVER_ERROR, version=web/5.7.1)
at Logger.makeError (/Users/fran_vidal/Documents/Uniswap_Auto_Pools/node_modules/@ethersproject/logger/lib/index.js:238:21)
at Logger.throwError (/Users/fran_vidal/Documents/Uniswap_Auto_Pools/node_modules/@ethersproject/logger/lib/index.js:247:20)
at /Users/fran_vidal/Documents/Uniswap_Auto_Pools/node_modules/@ethersproject/web/lib/index.js:313:32
at step (/Users/fran_vidal/Documents/Uniswap_Auto_Pools/node_modules/@ethersproject/web/lib/index.js:33:23)
at Object.next (/Users/fran_vidal/Documents/Uniswap_Auto_Pools/node_modules/@ethersproject/web/lib/index.js:14:53)
at fulfilled (/Users/fran_vidal/Documents/Uniswap_Auto_Pools/node_modules/@ethersproject/web/lib/index.js:5:58)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
reason: ‘processing response error’,
code: ‘SERVER_ERROR’,
body: ‘{“jsonrpc”:“2.0”,“id”:58,“error”:{“code”:-32000,“message”:“transaction underpriced”}}’,
error: Error: transaction underpriced
at getResult (/Users/fran_vidal/Documents/Uniswap_Auto_Pools/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:191:21)
at processJsonFunc (/Users/fran_vidal/Documents/Uniswap_Auto_Pools/node_modules/@ethersproject/web/lib/index.js:356:22)
at /Users/fran_vidal/Documents/Uniswap_Auto_Pools/node_modules/@ethersproject/web/lib/index.js:288:46
at step (/Users/fran_vidal/Documents/Uniswap_Auto_Pools/node_modules/@ethersproject/web/lib/index.js:33:23)
at Object.next (/Users/fran_vidal/Documents/Uniswap_Auto_Pools/node_modules/@ethersproject/web/lib/index.js:14:53)
at fulfilled (/Users/fran_vidal/Documents/Uniswap_Auto_Pools/node_modules/@ethersproject/web/lib/index.js:5:58)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: -32000,
data: undefined
},
requestBody: ‘{“method”:“eth_sendRawTransaction”,“params”:[“0x02f8b18189058459682f0085018c41101082b3f9940d500b1d8e8ef31e21c99d1db9a6444d3adf127080b844095ea7b3000000000000000000000000c36442b4a4522e871399cd717abdd847ab11fe88000000000000000000000000000000000000000000000000008e1bc9bf040000c001a0e00713e805e844c56ddb213bbc1707a6f81375c771cba0eee4ea537db045ef7da0781b818051e30f94f5e8e08256288f4fd78d309f4c360e3edcfed3f95202682e”],“id”:58,“jsonrpc”:“2.0”}’,
requestMethod: ‘POST’,
url: ‘https://polygon-mainnet.infura.io/v3/864606511d3147dabe0e17d331970b29’,
transaction: {
type: 2,
chainId: 137,
nonce: 5,
maxPriorityFeePerGas: BigNumber { _hex: ‘0x59682f00’, _isBigNumber: true },
maxFeePerGas: BigNumber { _hex: ‘0x018c411010’, _isBigNumber: true },
gasPrice: null,
gasLimit: BigNumber { _hex: ‘0xb3f9’, _isBigNumber: true },
to: ‘0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270’,
value: BigNumber { _hex: ‘0x00’, _isBigNumber: true },
data: ‘0x095ea7b3000000000000000000000000c36442b4a4522e871399cd717abdd847ab11fe88000000000000000000000000000000000000000000000000008e1bc9bf040000’,
accessList: [],
hash: ‘0xd714f4079fea522cf20cf3b68fed1a96d7d46022d42882a93e00a4b8bf954263’,
v: 1,
r: ‘0xe00713e805e844c56ddb213bbc1707a6f81375c771cba0eee4ea537db045ef7d’,
s: ‘0x781b818051e30f94f5e8e08256288f4fd78d309f4c360e3edcfed3f95202682e’,
from: ‘0x79BA030fE10B5bb6a31F5faa0Fa1d05bc23C5dc8’,
confirmations: 0
},
transactionHash: ‘0xd714f4079fea522cf20cf3b68fed1a96d7d46022d42882a93e00a4b8bf954263’
}

I think that could be an error with the communication with Infura, but I can’t find it. Anybody could help me? I’m a little desperate with that…

Hi @fran_vidal26, wellcome!

The error: transaction underpriced can happen because you send a replacement transaction (i.e. with the same nonce as one already sent and waiting in the mempool, but with a gas price too low. The explains it better than I can:
https://stackoverflow.com/questions/67972674/transaction-underpriced-in-bep-20-token-transaction

I see you have a nonce of 5 above but can’t see where you are setting it, perhaps ethers is setting it for you, not sure.

I hope this helps you solve your issue, I’d be grateful if you can let us know the outcome.

Warm regards,
Chris | Infura | ConsenSys