solidity 员工薪酬案例
时间: 2023-07-07 19:27:33 浏览: 114
contract-utils
以下是一个使用 Solidity 编写的员工薪酬合约的案例:
```
pragma solidity ^0.8.0;
contract EmployeeSalaryContract {
address public owner;
address public employee;
uint public salary;
uint public lastPayday;
constructor(address _employee, uint _salary) {
owner = msg.sender;
employee = _employee;
salary = _salary * 1 ether; // 将薪资转换为以 wei 为单位的整数
lastPayday = block.timestamp;
}
// 发放薪资
function paySalary() public {
require(msg.sender == owner, "Only owner can pay salary.");
uint nextPayday = lastPayday + 30 days;
require(nextPayday < block.timestamp, "It's not time to pay salary yet.");
uint payments = (block.timestamp - lastPayday) / 30 days;
uint amount = payments * salary;
lastPayday = nextPayday;
payable(employee).transfer(amount);
}
// 修改员工地址
function changeEmployee(address newEmployee) public {
require(msg.sender == owner, "Only owner can change employee.");
employee = newEmployee;
}
// 修改员工薪资
function changeSalary(uint newSalary) public {
require(msg.sender == owner, "Only owner can change salary.");
salary = newSalary * 1 ether;
}
// 查询合约余额
function getBalance() public view returns(uint) {
return address(this).balance;
}
}
```
该合约包含了发放薪资、修改员工地址、修改员工薪资和查询合约余额等功能。在合约创建时需要指定员工地址和薪资,薪资会以以 wei 为单位的整数存储,每月发放一次,发放前需要检查是否到了发薪日,并计算应发的薪资金额。
阅读全文