-
Notifications
You must be signed in to change notification settings - Fork 0
Payroll src and tests #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
808f0fc
add src and tests
nourharidy 58e7292
add withdraw uint amount
nourharidy 348efd0
reproduce bug test
nourharidy 8b51919
bug fix
nourharidy f603bac
remove unused event
nourharidy 42f236e
rename dola to asset
nourharidy c2413ee
require 18 decimal tokens
nourharidy 23c9556
index event recipients
nourharidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "lib/forge-std": { | ||
| "tag": { | ||
| "name": "v1.10.0", | ||
| "rev": "8bbcf6e3f8f62f419e5429a0bd89331c85c37824" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
nourharidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| emit AmountWithdrawn(msg.sender, withdrawAmount); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.