Ethereum Transaction RevertInstructionError: Transaction has been reverted by the EVM
I encountered an error when trying to use addLiquidity with Uniswap V2 on SEOlia testnet using web3.js in Truffle. The error is caused by a transaction that has been reverted by the Ethereum Virtual Machine (EVM). Here is a detailed explanation of the issue and possible solutions.
The issue:
When attempting to run addLiquidity
on the Uniswap V2 interface, the EVM reverts the transaction. This can happen for various reasons, such as:
- Insufficient liquidity in the pool.
- Incorrect token balances or addresses.
- Unvalidated external calls (e.g. calling functions from external contracts).
- Incorrect gas limits.
The code:
Here is an example of a Truffle contract and a web3.js code snippet that reproduces the problem:
// seolia-truffle-contracts.js
const Web3 = require('web3');
const truffle = require('truffle');
const Web3 = new Web3(new Web3.providers.HttpProvider("
module.exports = {
providers: [], // No provider specified, causing EVM to cancel the transaction
networks: {
development: {
host: 'localhost',
port: 8545,
gas: 2000000
},
seolia: { network_id: 1 }
},
contract: {
AddLiquidity: require('./AddLiquidity.json')
}
};
// addLiquidity.js
const Web3 = require('web3');
const contract = new Web3.eth.Contract(require.resolve('./AddLiquidity'), '0x...');
contract.methods.addLiquidity(address, amountIn, amountOut, price).send({ from: '0x...', value: '0.1' })
.then((result) => {
console.log(result.status);
if (result.status === 'success') {
contract.functions.getBalance.call(address)
.then((balance) => {
console.log(Balances: ${balance}
);
});
} else {
throw new Error('Error');
}
})
.catch((error) => {
console.error(error);
throw error;
});
Possible solutions:
To fix this issue, make sure to:
- Specify a provider: Add a
providers
array with the URL of the EVM provider (for example,in your Truffle configuration.
// seolia-truffle-contracts.js
const Web3 = require('web3');
const truffle = require('truffle');
const Web3 = new Web3(new Web3.providers.HttpProvider("
module.exports = {
providers: [],
networks: {
development: {
host: 'localhost',
port: 8545,
gas: 2000000
},
seolia: { network_id: 1 }
},
contract: {
AddLiquidity: require('./AddLiquidity.json')
}
};
- External Call Validation: Make sure that the externalCall
function is implemented in your contract to validate external transactions.
Balances: ${balance}
// addLiquidity.js
const Web3 = require('web3');
const contract = new Web3.eth.Contract(require.resolve('./AddLiquidity'), '0x...');
contract.methods.addLiquidity(address, amountIn, amountOut, price)
.call({ from: '0x...' })
.then((result) => {
console.log(result.status);
if (result.status === 'success') {
contract.functions.getBalance.call(address)
.then((balance) => {
console.log(
);
});
} else {
throw new Error('Error');
}
})
.catch((error) => {
console.error(error);
throw error;
});
- Increase gas limits: Set higher gas limits to prevent EVM from being reset due to insufficient funds or transaction complexity.
“javascript
// seolia-truffle-contracts.js
const Web3 = require(‘web3’);
const truffle = require(‘truffle’);
const Web3 = new Web3(new Web3.providers.HttpProvider(“
module.
Leave a Reply