1
pragma solidity ^0.4.17;

contract supplyChain {
    address public state;
    uint public unitCost = 10;
    uint public totalAmount = 0;

    function supplyChain() payable public{
        state = msg.sender;
    }

    function add_money() public payable {
        require(msg.value == totalAmount && totalAmount>0);
    }

    struct School{
        address schoolId;
        string name;
        string district;
        uint students;
        uint amountRequired;
    }

    function getbalance() external view returns(uint){
        return address(this).balance;
    }

    School[] public Schools;

    function addSchool(address schoolId,string memory name,string memory district,uint students) public {
        require(state == msg.sender);
        Schools.push(School(schoolId,name,district,students,students*unitCost));
    }

    function getTotalAmountToPay() public returns(uint){
        uint total = 0;
        for(uint i=0;i<Schools.length;i++){
            total+=Schools[i].amountRequired;
        }
        totalAmount = total;
        return total;
    }

    function allocateMoneyToSchools() public{
        for(uint i=0;i<Schools.length;i++){
            Schools[i].schoolId.transfer(Schools[i].amountRequired);   
        }
    }

}


**Error generated**

> transact to supplyChain.add_money errored: VM error: revert.
revert  The transaction has been reverted to the initial state.
Note: The constructor should be payable if you send value.


Even though the method is payable, still it shows the same error.
1

It works perfectly fine for me.

The error stems from require(msg.value == totalAmount && totalAmount>0). I think you are trying to fund the contract using add_money while the totalAmount is 0. In the current state of your contract, it will always fail as long as you don't add at least one school, call getTotalAmountToPay and set the transaction value when calling add_money to the value of totalAmount.

You can set uint public totalAmount = 2 ether; and then call add_money with a value of 2 ether and it will work.

0

This error is misleading.

Your require statement is reverting because one or more of the conditions is not true.

Make sure totalValue is > 0 and msg.value matches exactly.

Hope it helps.

  • both condition individually works fine. But together they are giving error – Mukul Juneja Apr 12 at 5:52
  • I would concentrate on your testing methodology. You have to set up the right conditions or it will fail, by design. – Rob Hitchens - B9lab Apr 12 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.