Payroll Smart Contract on Ethereum

N F
Coinmonks

--

In this post we’ll go over a Payroll smart contract written in Solidity using Truffle and Open Zeppelin. You can find the repo here.

Let’s make sure you have Truffle installed globally:

$ npm install -g truffle

Clone my repo to your local machine and install:

$ npm install

You can now use Truffle develop to compile, migrate, and run the tests:

$ truffle develop
truffle(develop)> compile
truffle(develop)> migrate
truffle(develop)> test

Ok, enough of that, let’s look at some code.

The contract follows an interface approach similar to Zeppelin’s ERC20 standard in which an interface contract is defined. In our case the Payroll interface is given by:

You can see that we have all the functions necessary to manage employees and payments. Now, let’s look at the main contract: Payroll.sol.

Payroll.sol has three imports:

import './InterfacePayroll.sol';
import './EmployeeStorage.sol';
import './EmployeeToken.sol';

which allow to set the Storage variables:

/*
* Storage
*/
InterfaceEmployeeStorage public employeeStorage;
EmployeeToken public employeeToken;

EmployeeStorage.sol defines the employee structure:

/*
* Storage
*/
struct Employee {
bool exists;
uint256 id;
address accountAddress;
address tokenContractAddress;
uint256 latestPayday;
uint256 monthlySalary;
}

EmployeeToken.sol is a simple ERC20 standard contract:

The constructor for Payroll.sol is given by:

/*
* Public functions
*/
/// @dev Contract constructor sets storage and token.
function Payroll()
public
{
setEmployeeStorage(new EmployeeStorage());
setEmployeeToken(new EmployeeToken());
}

which simply sets the employee storage and the employee token. The contract owner can now manage the employees through any of the functions defined in InterfacePayroll.sol. Employees can request to be payed for a given pay period by calling:

/// @dev allows employee to get payed.
/// @param _payPeriod of requested payment.
function getPay (uint256 _payPeriod)
public
employeePayPeriodExists(_payPeriod)
enoughTokensLeft()
{
uint256 employeeSalary = employeeStorage.getMonthlySalary(msg.sender);
employeeToken.transfer(msg.sender, employeeSalary);
uint256 newPayday = employeeStorage.getLatestPayday(msg.sender);
newPayday++;
employeeStorage.setlatestPayday(msg.sender, newPayday);
EmployeePayed(msg.sender, newPayday--);
}

Payroll.sol also has several events for dapp watching:

/*
* Events
*/
event Deposit(address indexed sender, uint value);
event NewEmployee(address indexed newEmployee);
event EmployeeAddressChange(address indexed newAddress);
event EmployeePaydayChange(address indexed employeeAddress, uint indexed newPayday);event EmployeeSalaryChange(address indexed employeeAddress, uint indexed newSalary);event EmployeeRemoved(address indexed employeeAddress, uint indexed_id);event EmployeePayed(address indexed employeeAddress, uint payPeriod);

Right now the pay period is set to be simply an uint , but it could easily be set to an actual time frame as specified by the owner.

That’s it! If you would rather work on this contract in Remix, I set up a Gist you can use.

Get Best Software Deals Directly In Your Inbox

Thank you very much for reading this post! If you found this post useful please clap on this article and make sure to follow me for more regular content, also check out my Github as I regularly post sample code and projects. If you have any questions feel free to reply below this post or shoot me an email.

Happy coding!

--

--