Polygon Infura uniswap transaction uderpriced error and STF ERROR

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’
}

1 Like

Hi there,

To fix this issue, you need to increase the gas price used for your transaction. In your code, you have defined two variables: maxPriorityFeePerGas and maxFeePerGas. The sum of these two values determines the actual gas price for the transaction.

Here are some steps you can take to resolve the problem:

  1. Check Gas Price: Find out the current gas prices on the Ethereum network. You can use a tool like 85.9-79.7 Gwei | Polygon Gas Tracker | PolygonScan to get an estimate of reasonable gas prices.
  2. Set Gas Price: Update the values of maxPriorityFeePerGas and maxFeePerGas to appropriate values based on the current network conditions. You may need to increase them to ensure your transaction gets processed quickly.

For example, if you find that the current average gas price is 100 gwei, you can set a higher value like 150 gwei for both maxPriorityFeePerGas and maxFeePerGas.

Replace these lines:

const maxPriorityFeePerGas = ethers.BigNumber.from(80000000000)
const maxFeePerGas = ethers.BigNumber.from(80000000000)

with new values:

const maxPriorityFeePerGas = ethers.BigNumber.from(150000000000) // Replace with your desired gas price (in wei)
const maxFeePerGas = ethers.BigNumber.from(150000000000) // Replace with your desired gas price (in wei)

Remember that gas prices fluctuate based on network demand, so it’s essential to keep them up to date.

After making these adjustments, try running your code again. If the gas price is sufficient, your transaction should be processed without encountering the “transaction underpriced” error.

2 Likes

thanks! now my problem is other. I rebuild the code, and now i can approve but when its done, i receive the STF error… And cant fix it! here is the entire code:
*** POLYGON NETWORK***

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 = ‘0x4CcD010148379ea531D6C587CfDd60180196F9b1’
const swapRouterAddress = ‘0xE592427A0AEce92De3Edee1F18E0157C05861564’;

const name0 = ‘USDT’
const symbol0 = ‘USDT’
const decimals0 = 6;
const address0 = ‘0xc2132D05D31c914a87C6611C10748AEb04B58e8F’;

const name1 = ‘ETH’
const symbol1 = ‘WETH’
const decimals1 = 18;
const address1 = ‘0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619’;

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(“2”, decimals0);
const approvalAmount = ethers.utils.parseUnits(“2”, decimals0);//amountIn.mul(ethers.BigNumber.from(100000)); // Using BigNumber multiplication

const tokenContract0 = new ethers.Contract(
address0,
ERC20ABI,
provider
);

const transactionRequest = await tokenContract0.populateTransaction.approve(
swapRouterAddress,
ethers.utils.parseUnits(‘12.0’,decimals0), // A quantidade que você deseja aprovar
);

// Agora, definir os parâmetros de gás
transactionRequest.maxPriorityFeePerGas = ethers.utils.parseUnits(‘200’, ‘gwei’);
transactionRequest.maxFeePerGas = ethers.utils.parseUnits(‘200’, ‘gwei’);
transactionRequest.gasLimit = 100000;

// Agora, enviar a transação
const txResponse = await connectedWallet.sendTransaction(transactionRequest);

// Aguardar pela confirmação da transação
const txReceipt = await txResponse.wait();

const transaction = await swapRouterContract.connect(connectedWallet).exactInputSingle({
tokenIn: immutables.token0,
tokenOut: immutables.token1,
fee: immutables.fee,
recipient: WALLET_ADDRESS,
deadline: Math.floor(Date.now() / 1000) + (60 * 10), // 3 minutes from the current Unix time
amountIn: amountIn,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0,
}, {
maxPriorityFeePerGas: ethers.utils.parseUnits(‘200’, ‘gwei’), // And here
maxFeePerGas: ethers.utils.parseUnits(‘200’, ‘gwei’), // And here
gasLimit: ethers.utils.hexlify(100000) // And here
}).then(transaction => {
console.log(transaction)
})

}

main();

1 Like

Hi there,

The “STF error” you mentioned is likely referring to an “Insufficient Output Amount” error. This error occurs when the trade cannot be executed because the amount of output tokens (ETH in this case) that the swap would yield is too low to meet the minimum specified (amountOutMinimum). The swap fails to protect users from losing a significant portion of their funds due to slippage.

To resolve this issue, you need to adjust the amountOutMinimum parameter to ensure that the swap can be executed even if the output amount is slightly lower than expected due to slippage. Additionally, make sure you are providing enough input tokens (amountIn) to cover the swap and the associated fees.

Try increasing the amountOutMinimum value to a more suitable amount

1 Like

but my amountOutMinimum: 0… to increase the amountOut I will need to calculate it someway

1 Like

INo success!

Function: exactInputSingle((address,address,uint24,address,uint256,uint256,uint256,uint160))

# Name Type Data
0 params.tokenIn address [0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619]
0 params.tokenOut address [0xc2132D05D31c914a87C6611C10748AEb04B58e8F]
0 params.fee uint24 3000
0 params.recipient address [0xC5e543b37aCD47C12035CDE2dd7B997d2b5B2c81]
0 params.deadline uint256 1690382594
0 params.amountIn uint256 2000000
0 params.amountOutMinimum uint256 1050000000000000
0 params.sqrtPriceLimitX96 uint160 0

address: 0xC5e543b37aCD47C12035CDE2dd7B997d2b5B2c81

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.