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;
}
}
addEmployee
andupdateEmployeeOrder
start with arequire
statement. Have you made sure that this statement is fulfilled (i.e., that the expression inside it evaluates totrue
)? You would obviously get your transaction "always failing" otherwise.departmentEmployees
is a dynamic array, the assignmentdepartmentEmployees[_employeeId] = _employeeId
would naturally revert. You should either change that variable to amapping
, or usedepartmentEmployees.push(_employeeId)
instead of what you're doing now.