How to Solve Transaction Error in Solidity?

In this article, I will explain how to solve transaction errors in Solidity. This guide aims to provide examples of how to solve transaction errors in Solidity. So, let us get this guide started.

The Transaction Error in Solidity

A transaction error is usually a result when there is an error with a function marked as payable in the smart contract. So, let us look at the error.

Transactionr Error Solidity

The transaction has been reverted to the initial state.

Note: The called function should be payable if you send value and the value you send should be less than your current balance.

There might be a lot of causes for this error. Let us discuss a few of them below.

Causes of Transaction Error in Solidity

Let us discuss a few possible causes that lead to this error in Remix.

1. Value conditions not met

The very first possible reason for this error could be that either the value is 0 or the provided value doesn’t meet specifications set by you.

For example, you have set the minimum ether to pay to enter the lottery as 1 but you pass the value as 0.1 while calling the function, then you will see a transaction error printed out for you.

2. Not enough balance in account

One another possible reason for this error could be the unavailability of balance to process the transaction. In Remix, you get 100 ethers on every account on the JavaScript VM environment.

I suppose you try to use more than the available balance per account, then you will be thrown the transaction error.

3. Not marked as payable

To be able to send or receive funds via a call of a function, then that function must be marked as payable. In case you fail to do so, be sure to see a nice transaction error printed out for you in the Remix terminal.

4. Wrong unit selected

This is one cause of the transaction error to occur that most of us become prey to. You probably specified the amount to be sent in Wei but you mistakenly send it in Finney or some other unit.

Again, you will be presented with this error!

While the list of causes can be many, we are not going to cover each every one here in this guide today. Let us now discuss a few possible solutions to this error with a sample smart contract.

How to Solve Transaction Error in Solidity

We will now move further to discuss the possible solutions that must be taken into consideration. As mentioned above, I will be using a sample smart contract to extract real-world solutions.

Lotter.sol

pragma solidity ^0.4.17;

contract Lottery {
    address public manager;
    address[] public players;

    function Lottery() public {
        manager = msg.sender;
    }

    function enter() public payable {
        require(msg.value > .01 ether);
        players.push(msg.sender);
    }

    function random() private view returns (uint256) {
        return uint256(keccak256(block.difficulty, now, players));
    }

    function pickWinner() public restricted {
        uint256 index = random() % players.length;
        players[index].transfer(this.balance);
        players = new address[](0);
    }

      modifier restricted(){
          require(msg.sender == manager);
          _;
      }

    function getPlayers() public view returns (address[]) {
        return players;
    }
}

Very well. Let us draw some possible solutions now based on the above contract.

Solving Transaction Error in Solidity Remix

Let’s figure out how to solve the transaction errors in Solidity Remix now.

1. Pass value above set limits

In the lottery contract, we have mentioned that people must pay at least 0.01 ether to enter into the lottery and become its players. We will then have to pass any value that is greater than 0.01.

2. Make sure contract is deployed

Make sure the contract you are working on in Remix is deployed and is the latest one. Moreover, delete the ones that you are sure you will no longer need.

3. Make sure account has enough balance to send transaction

Before you proceed to test out your transaction in Remix, make sure you have an ample number of ethers in the account to execute the transaction.

4. Make sure compiler version is correct

The most crucial and possible solution to the transaction error is to set the right Solidity compiler version in Remix.

5. Make sure all addresses are correct

When deploying, we usually mess up the contract addresses. Make it a point to check the addresses before you deploy your contracts.

Conclusion

Learn to solve the transaction error in Solidity.

Noteworthy References

https://stackoverflow.com/questions/61388743/solidity-transaction-error-the-called-function-should-be-payable-if-you-send-va

Aneesha S
Aneesha S
Articles: 172