Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI

on:
push:
pull_request:
workflow_dispatch:

env:
FOUNDRY_PROFILE: ci

jobs:
check:
name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1

- name: Show Forge version
run: |
forge --version
- name: Run Forge fmt
run: |
forge fmt --check
id: fmt

- name: Run Forge build
run: |
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
id: test
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
# payroll
## Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Foundry consists of:

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.

## Documentation

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```

### Test

```shell
$ forge test
```

### Format

```shell
$ forge fmt
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```

### Deploy

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
8 changes: 8 additions & 0 deletions foundry.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"lib/forge-std": {
"tag": {
"name": "v1.10.0",
"rev": "8bbcf6e3f8f62f419e5429a0bd89331c85c37824"
}
}
}
9 changes: 9 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

[lint]
severity = ["high"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at 8bbcf6
19 changes: 19 additions & 0 deletions script/Payroll.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Script} from "forge-std/Script.sol";
import {Payroll} from "../src/Payroll.sol";

contract CounterScript is Script {
Payroll public payroll;

function setUp() public {}

function run() public {
vm.startBroadcast();

//payroll = new Payroll();

vm.stopBroadcast();
}
}
81 changes: 81 additions & 0 deletions src/Payroll.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
pragma solidity 0.8.13;

interface IERC20 {
function transferFrom(address from, address to, uint256 amount) external returns (bool);
function decimals() external view returns (uint8);
}

// WARNING: THIS CONTRACT IS NOT COMPATIBLE WITH NON-STANDARD ERC20 TOKENS (e.g. USDT)
contract Payroll {

mapping(address => Recipient) public recipients;
mapping(address => uint256) public unclaimed;

address public immutable treasuryAddress;
address public immutable governance;
IERC20 public immutable asset;

uint256 public constant SECONDS_PER_YEAR = 365 days;

struct Recipient {
uint256 lastClaim;
uint256 ratePerSecond;
uint256 endTime;
}

event SetRecipient(address indexed recipient, uint256 amount, uint256 endTime);
event AmountWithdrawn(address indexed recipient, uint256 amount);

constructor(address _treasuryAddress, address _governance, address _asset) {
require(IERC20(_asset).decimals() == 18, "Payroll::constructor: asset must have 18 decimals");
treasuryAddress = _treasuryAddress;
governance = _governance;
asset = IERC20(_asset);
}

function balanceOf(address _recipient) public view returns (uint256 bal) {
bal = unclaimed[_recipient];
Recipient memory recipient = recipients[_recipient];
uint256 accrualEnd = block.timestamp < recipient.endTime ? block.timestamp : recipient.endTime;
uint256 accrualStart = recipient.lastClaim < accrualEnd ? recipient.lastClaim : accrualEnd;
bal += recipient.ratePerSecond * (accrualEnd - accrualStart);
}

function updateRecipient(address recipient) internal {
unclaimed[recipient] = balanceOf(recipient);
recipients[recipient].lastClaim = block.timestamp;
}

function setRecipient(address _recipient, uint256 _yearlyAmount, uint256 _endTime) external {
updateRecipient(_recipient);
require(msg.sender == governance, "Payroll::setRecipient: only governance");
require(_recipient != address(0), "Payroll::setRecipient: zero address!");

// endTime cannot be in the past
if(_endTime < block.timestamp) {
_endTime = block.timestamp;
}

recipients[_recipient] = Recipient({
lastClaim: block.timestamp,
ratePerSecond: _yearlyAmount / SECONDS_PER_YEAR,
endTime: _endTime
});

emit SetRecipient(_recipient, _yearlyAmount, _endTime);
}

/**
* @notice withdraw salary
*/
function withdraw(uint256 amount) external {
updateRecipient(msg.sender);

uint256 withdrawAmount = unclaimed[msg.sender] > amount ? amount : unclaimed[msg.sender];
unclaimed[msg.sender] -= withdrawAmount;
require(asset.transferFrom(treasuryAddress, msg.sender, withdrawAmount), "Payroll::withdraw: transfer failed");

emit AmountWithdrawn(msg.sender, withdrawAmount);
}

}
Loading
Loading