0

I am running private block chain using geth, Here is my geth command :

geth
 --datadir $ethereum_home/chaindata
 --networkid 45683
 --verbosity 3
 --rpc
 --port 30304
 --rpcport 8545
 --rpcaddr "0.0.0.0"
 --rpcapi "web3,eth,personal,net"
 --rpccorsdomain https://remix.ethereum.org
 --nodiscover
 --gcmode archive
 --allow-insecure-unlock console 2

I am trying to run following contract in remix ide, I have compiled the contract using compiler version 0.4.24 and I am able to deploy my contract successfully on my blockchain.

But when I am trying to call methods like addEmployee, updateEmployeeOrder etc. then I am getting this error: Returned error: gas required exceeds allowance (8000000) or always failing transaction I tried to optimize my contract but still I am getting this error

pragma solidity >=0.4.17 <0.7.0;
contract EmployeeOrder{

    struct Employee{
        string employeeName;
        uint128 order;
    }

    uint128 departmentId;
    uint128 goal;
    uint32 startDate;
    uint32 endDate;
    mapping (uint128 => Employee) public employees;
    uint128[] public departmentEmployees;
    address public owner;
    uint128 minimumOrder;
    uint128 public totalOrder;

    //This will call when we deploy contract for the first time
    constructor(uint128 _departmentId, uint128 _expectedOrder, uint32 _startDate, uint32 _endDate) public {
        owner = msg.sender;
        minimumOrder = 1;
        totalOrder = 0;
        departmentId = _departmentId;
        goal = _expectedOrder;
        startDate = _startDate;
        endDate = _endDate;
    }

    //This will tackle order history of all the employees
    event logEmployeeData(uint256 _employeeId, string _name, uint _order, uint256 _time);

    //It will restrict access of the contract to owner only
    modifier restricted() {
        require(msg.sender == owner);
        _;
    }

    //It will not allow to update data after endDate of department
    modifier beforeEndDate() {
        require(now < endDate);
        _;
    }

    //This will add new employees data on the block chain
    function addEmployee(uint128 _employeeId, string memory _name, uint128 _order) public {
        require(_order >= minimumOrder);
        Employee storage employee = employees[_employeeId];
        employee.employeeName = _name;
        employee.order = _order;
        totalOrder = totalOrder + _order;
        departmentEmployees[_employeeId] = _employeeId;
        emit logEmployeeData(_employeeId, _name, _order, now);
    }

    //This will update order of existing employees on the block chain
    function updateEmployeeOrder(uint128 _employeeId,  uint128 _order) public {
        require(_order >= minimumOrder);
        employees[_employeeId].order = employees[_employeeId].order + _order;
        totalOrder = totalOrder + _order;
        emit logEmployeeData(_employeeId, employees[_employeeId].employeeName, _order, now);
    }

    //This will change end date with new date
    function extendEndDate(uint32 _newEndDate) public {
        endDate = _newEndDate;
    }

    //This will return all the employee ids
    function getAllEmployees() public view returns (uint128[] memory) {
        return departmentEmployees;
    }

    //This will return true if goal achieved
    function isGoalAchieved() public view returns (bool) {
        if(totalOrder>= goal) return true;
        else return false;
    }
}
4
  • Both addEmployee and updateEmployeeOrder start with a require statement. Have you made sure that this statement is fulfilled (i.e., that the expression inside it evaluates to true)? You would obviously get your transaction "always failing" otherwise. Commented Mar 25, 2020 at 14:51
  • 1
    Also, being that departmentEmployees is a dynamic array, the assignment departmentEmployees[_employeeId] = _employeeId would naturally revert. You should either change that variable to a mapping, or use departmentEmployees.push(_employeeId) instead of what you're doing now. Commented Mar 25, 2020 at 14:54
  • @goodvibration: I used departmentEmployees.push(_employeeId) and it worked!! Thanks for your help!!
    – Ruchita
    Commented Mar 25, 2020 at 16:21
  • Added as an answer then... Commented Mar 25, 2020 at 16:47

1 Answer 1

1

Being that departmentEmployees is a dynamic array, this assignment naturally reverts:

departmentEmployees[_employeeId] = _employeeId;

You can change that variable to a mapping, but then, what's the point in mapping a key to its own value (in your case, _employeeId to _employeeId)?

So you probably want to leave that variable as is, and simply change your assignment to:

departmentEmployees.push(_employeeId);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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