├── .env.example ├── remappings.txt ├── .gitignore ├── src ├── test │ ├── mocks │ │ ├── TestERC20.sol │ │ └── TestERC721.sol │ ├── base │ │ └── BaseTest.sol │ ├── StakingPoolFactory.t.sol │ ├── xERC20.t.sol │ ├── ERC20StakingPool.t.sol │ └── ERC721StakingPool.t.sol ├── lib │ ├── Multicall.sol │ ├── SelfPermit.sol │ ├── Ownable.sol │ ├── ERC20.sol │ └── FullMath.sol ├── StakingPoolFactory.sol ├── xERC20.sol ├── ERC20StakingPool.sol └── ERC721StakingPool.sol ├── foundry.toml ├── .gitmodules ├── .github └── workflows │ └── test.yml ├── README.md ├── script ├── base │ └── CREATE3Script.sol └── Deploy.s.sol └── LICENSE /.env.example: -------------------------------------------------------------------------------- 1 | # Network and account info 2 | RPC_URL_MAINNET= 3 | RPC_URL_GOERLI= 4 | 5 | PRIVATE_KEY= 6 | 7 | ETHERSCAN_KEY= 8 | 9 | # Deploy configs 10 | VERSION="1.0.0" -------------------------------------------------------------------------------- /remappings.txt: -------------------------------------------------------------------------------- 1 | ds-test/=lib/forge-std/lib/ds-test/src/ 2 | forge-std/=lib/forge-std/src/ 3 | @clones/=lib/clones-with-immutable-args/src/ 4 | create3-factory/=lib/create3-factory/src/ -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiler files 2 | cache/ 3 | out/ 4 | 5 | # Ignores development broadcast logs 6 | !/broadcast 7 | /broadcast/*/31337/ 8 | /broadcast/**/dry-run/ 9 | 10 | # Dotenv file 11 | .env 12 | 13 | .vscode -------------------------------------------------------------------------------- /src/test/mocks/TestERC20.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0 2 | 3 | pragma solidity ^0.8.11; 4 | 5 | import {ERC20} from "solmate/tokens/ERC20.sol"; 6 | 7 | contract TestERC20 is ERC20("", "", 18) { 8 | function mint(address to, uint256 amount) external { 9 | _mint(to, amount); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /foundry.toml: -------------------------------------------------------------------------------- 1 | [profile.default] 2 | optimizer_runs = 1000000 3 | verbosity = 1 4 | 5 | # Extreme Fuzzing CI Profile :P 6 | [profile.ci] 7 | fuzz_runs = 100_000 8 | verbosity = 4 9 | 10 | [rpc_endpoints] 11 | goerli = "${RPC_URL_GOERLI}" 12 | mainnet = "${RPC_URL_MAINNET}" 13 | 14 | [etherscan] 15 | goerli = {key = "${ETHERSCAN_KEY}", url = "https://api-goerli.etherscan.io/api"} 16 | mainnet = {key = "${ETHERSCAN_KEY}"} 17 | -------------------------------------------------------------------------------- /src/test/mocks/TestERC721.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0 2 | pragma solidity ^0.8.0; 3 | 4 | import {ERC721} from "solmate/tokens/ERC721.sol"; 5 | 6 | contract TestERC721 is ERC721 { 7 | constructor() ERC721("Test721", "T721") {} 8 | 9 | function tokenURI(uint256) public pure override returns (string memory) { 10 | return ""; 11 | } 12 | 13 | function safeMint(address to, uint256 id) public { 14 | _safeMint(to, id); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "lib/solmate"] 2 | path = lib/solmate 3 | url = https://github.com/rari-capital/solmate 4 | [submodule "lib/clones-with-immutable-args"] 5 | path = lib/clones-with-immutable-args 6 | url = https://github.com/wighawag/clones-with-immutable-args 7 | [submodule "lib/forge-std"] 8 | path = lib/forge-std 9 | url = https://github.com/foundry-rs/forge-std 10 | branch = v1.3.0 11 | [submodule "lib/create3-factory"] 12 | path = lib/create3-factory 13 | url = https://github.com/zeframlou/create3-factory 14 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: workflow_dispatch 4 | 5 | env: 6 | FOUNDRY_PROFILE: ci 7 | 8 | jobs: 9 | check: 10 | strategy: 11 | fail-fast: true 12 | 13 | name: Foundry project 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v3 17 | with: 18 | submodules: recursive 19 | 20 | - name: Install Foundry 21 | uses: foundry-rs/foundry-toolchain@v1 22 | with: 23 | version: nightly 24 | 25 | - name: Run Forge build 26 | run: | 27 | forge --version 28 | forge build --sizes 29 | id: build 30 | 31 | - name: Run Forge tests 32 | run: | 33 | forge test -vvv 34 | id: test 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Modern ERC20 & ERC721 Staking Foundry 2 | 3 | This project is a set of modern, gas optimized staking pool contracts including ERC20 and ERC721. 4 | 5 | ## Features 6 | 7 | - Support for both ERC20 staking and ERC721 staking. 8 | - Can start new reward period after the current one is over. 9 | - Minimized error in reward computation (<10^-8) by using higher precision. 10 | - Well commented with NatSpec comments. 11 | - Fuzz tests powered by [Foundry](https://github.com/gakonst/foundry). 12 | - Gas optimized. 13 | - Cheap deployment using `ClonesWithCallData` (~81.7k gas). 14 | 15 | ## Development 16 | 17 | This project uses [Foundry](https://github.com/gakonst/foundry) as the development framework. 18 | 19 | ### Dependencies 20 | 21 | ``` 22 | forge install 23 | ``` 24 | 25 | ### Compilation 26 | 27 | ``` 28 | forge build 29 | ``` 30 | 31 | ### Testing 32 | 33 | ``` 34 | forge test 35 | ``` 36 | -------------------------------------------------------------------------------- /src/lib/Multicall.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0 2 | pragma solidity ^0.8.4; 3 | 4 | /// @title Multicall 5 | /// @notice Enables calling multiple methods in a single call to the contract 6 | abstract contract Multicall { 7 | function multicall(bytes[] calldata data) external payable returns (bytes[] memory results) { 8 | results = new bytes[](data.length); 9 | for (uint256 i = 0; i < data.length; i++) { 10 | (bool success, bytes memory result) = address(this).delegatecall(data[i]); 11 | 12 | if (!success) { 13 | // Next 5 lines from https://ethereum.stackexchange.com/a/83577 14 | if (result.length < 68) revert(); 15 | assembly { 16 | result := add(result, 0x04) 17 | } 18 | revert(abi.decode(result, (string))); 19 | } 20 | 21 | results[i] = result; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/lib/SelfPermit.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0 2 | pragma solidity >=0.5.0; 3 | 4 | import {ERC20} from "solmate/tokens/ERC20.sol"; 5 | 6 | /// @title Self Permit 7 | /// @notice Functionality to call permit on any EIP-2612-compliant token for use in the route 8 | /// @dev These functions are expected to be embedded in multicalls to allow EOAs to approve a contract and call a function 9 | /// that requires an approval in a single transaction. 10 | abstract contract SelfPermit { 11 | function selfPermit(ERC20 token, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public payable { 12 | token.permit(msg.sender, address(this), value, deadline, v, r, s); 13 | } 14 | 15 | function selfPermitIfNecessary(ERC20 token, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) 16 | external 17 | payable 18 | { 19 | if (token.allowance(msg.sender, address(this)) < value) { 20 | selfPermit(token, value, deadline, v, r, s); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /script/base/CREATE3Script.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0 2 | pragma solidity ^0.8.13; 3 | 4 | import {CREATE3Factory} from "create3-factory/CREATE3Factory.sol"; 5 | 6 | import "forge-std/Script.sol"; 7 | 8 | abstract contract CREATE3Script is Script { 9 | CREATE3Factory internal constant create3 = CREATE3Factory(0x9fBB3DF7C40Da2e5A0dE984fFE2CCB7C47cd0ABf); 10 | 11 | string internal version; 12 | 13 | constructor(string memory version_) { 14 | version = version_; 15 | } 16 | 17 | function getCreate3Contract(string memory name) internal view virtual returns (address) { 18 | uint256 deployerPrivateKey = uint256(vm.envBytes32("PRIVATE_KEY")); 19 | address deployer = vm.addr(deployerPrivateKey); 20 | 21 | return create3.getDeployed(deployer, getCreate3ContractSalt(name)); 22 | } 23 | 24 | function getCreate3ContractSalt(string memory name) internal view virtual returns (bytes32) { 25 | return keccak256(bytes(string.concat(name, "-v", version))); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/base/BaseTest.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Unlicense 2 | pragma solidity ^0.8.4; 3 | 4 | import "forge-std/Test.sol"; 5 | 6 | contract BaseTest is Test { 7 | function assertEqEpsilonBelow(uint256 a, uint256 b, uint256 epsilonInv) internal { 8 | assertLe(a, b); 9 | assertGe(a, b - b / epsilonInv); 10 | } 11 | 12 | function assertEqEpsilonAround(uint256 a, uint256 b, uint256 epsilonInv) internal { 13 | assertLe(a, b + b / epsilonInv); 14 | assertGe(a, b - b / epsilonInv); 15 | } 16 | 17 | function assertEqDecimalEpsilonBelow(uint256 a, uint256 b, uint256 decimals, uint256 epsilonInv) internal { 18 | assertLeDecimal(a, b, decimals); 19 | assertGeDecimal(a, b - b / epsilonInv, decimals); 20 | } 21 | 22 | function assertEqDecimalEpsilonAround(uint256 a, uint256 b, uint256 decimals, uint256 epsilonInv) internal { 23 | if (a == 0) a = 1; 24 | if (b == 0) b = 1; 25 | assertLeDecimal(a, b + b / epsilonInv, decimals); 26 | assertGeDecimal(a, b - b / epsilonInv, decimals); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /script/Deploy.s.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0 2 | pragma solidity ^0.8.13; 3 | 4 | import {CREATE3Script} from "./base/CREATE3Script.sol"; 5 | import "../src/StakingPoolFactory.sol"; 6 | 7 | contract DeployScript is CREATE3Script { 8 | constructor() CREATE3Script(vm.envString("VERSION")) {} 9 | 10 | function run() external returns (StakingPoolFactory factory) { 11 | uint256 deployerPrivateKey = uint256(vm.envBytes32("PRIVATE_KEY")); 12 | 13 | vm.startBroadcast(deployerPrivateKey); 14 | 15 | xERC20 xerc20Template = new xERC20(); 16 | ERC20StakingPool erc20Template = new ERC20StakingPool(); 17 | ERC721StakingPool erc721Template = new ERC721StakingPool(); 18 | factory = StakingPoolFactory( 19 | create3.deploy( 20 | getCreate3ContractSalt("StakingPoolFactory"), 21 | bytes.concat( 22 | type(StakingPoolFactory).creationCode, abi.encode(xerc20Template, erc20Template, erc721Template) 23 | ) 24 | ) 25 | ); 26 | 27 | vm.stopBroadcast(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/lib/Ownable.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0 2 | 3 | pragma solidity ^0.8.4; 4 | 5 | abstract contract Ownable { 6 | error Ownable_NotOwner(); 7 | error Ownable_NewOwnerZeroAddress(); 8 | 9 | address private _owner; 10 | 11 | event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); 12 | 13 | /// @dev Returns the address of the current owner. 14 | function owner() public view virtual returns (address) { 15 | return _owner; 16 | } 17 | 18 | /// @dev Throws if called by any account other than the owner. 19 | modifier onlyOwner() { 20 | if (owner() != msg.sender) revert Ownable_NotOwner(); 21 | _; 22 | } 23 | 24 | /// @dev Transfers ownership of the contract to a new account (`newOwner`). 25 | /// Can only be called by the current owner. 26 | function transferOwnership(address newOwner) public virtual onlyOwner { 27 | if (newOwner == address(0)) revert Ownable_NewOwnerZeroAddress(); 28 | _transferOwnership(newOwner); 29 | } 30 | 31 | /// @dev Transfers ownership of the contract to a new account (`newOwner`). 32 | /// Internal function without access restriction. 33 | function _transferOwnership(address newOwner) internal virtual { 34 | address oldOwner = _owner; 35 | _owner = newOwner; 36 | emit OwnershipTransferred(oldOwner, newOwner); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/StakingPoolFactory.t.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Unlicense 2 | pragma solidity ^0.8.4; 3 | 4 | import {BaseTest, console} from "./base/BaseTest.sol"; 5 | 6 | import {xERC20} from "../xERC20.sol"; 7 | import {TestERC20} from "./mocks/TestERC20.sol"; 8 | import {TestERC721} from "./mocks/TestERC721.sol"; 9 | import {ERC20StakingPool} from "../ERC20StakingPool.sol"; 10 | import {ERC721StakingPool} from "../ERC721StakingPool.sol"; 11 | import {StakingPoolFactory} from "../StakingPoolFactory.sol"; 12 | 13 | contract StakingPoolFactoryTest is BaseTest { 14 | StakingPoolFactory factory; 15 | TestERC20 rewardToken; 16 | TestERC20 stakeToken; 17 | TestERC721 stakeNFT; 18 | 19 | function setUp() public { 20 | xERC20 xERC20Implementation = new xERC20(); 21 | ERC20StakingPool erc20StakingPoolImplementation = new ERC20StakingPool(); 22 | ERC721StakingPool erc721StakingPoolImplementation = new ERC721StakingPool(); 23 | factory = new StakingPoolFactory( 24 | xERC20Implementation, 25 | erc20StakingPoolImplementation, 26 | erc721StakingPoolImplementation 27 | ); 28 | 29 | rewardToken = new TestERC20(); 30 | stakeToken = new TestERC20(); 31 | stakeNFT = new TestERC721(); 32 | } 33 | 34 | /// ------------------------------------------------------------------- 35 | /// Gas benchmarking 36 | /// ------------------------------------------------------------------- 37 | 38 | function testGas_createXERC20(bytes32 name, bytes32 symbol, uint8 decimals, uint64 DURATION) public { 39 | factory.createXERC20(name, symbol, decimals, stakeToken, DURATION); 40 | } 41 | 42 | function testGas_createERC20StakingPool(uint64 DURATION) public { 43 | factory.createERC20StakingPool(rewardToken, stakeToken, DURATION); 44 | } 45 | 46 | function testGas_createERC721StakingPool(uint64 DURATION) public { 47 | factory.createERC721StakingPool(rewardToken, stakeNFT, DURATION); 48 | } 49 | 50 | /// ------------------------------------------------------------------- 51 | /// Correctness tests 52 | /// ------------------------------------------------------------------- 53 | 54 | function testCorrectness_createXERC20(bytes32 name, bytes32 symbol, uint8 decimals, uint64 DURATION) public { 55 | xERC20 stakingPool = factory.createXERC20(name, symbol, decimals, stakeToken, DURATION); 56 | 57 | assertEq(stakingPool.name(), string(abi.encodePacked(name))); 58 | assertEq(stakingPool.symbol(), string(abi.encodePacked(symbol))); 59 | assertEq(stakingPool.decimals(), decimals); 60 | assertEq(address(stakingPool.stakeToken()), address(stakeToken)); 61 | assertEq(uint256(stakingPool.DURATION()), uint256(DURATION)); 62 | } 63 | 64 | function testCorrectness_createERC20StakingPool(uint64 DURATION) public { 65 | ERC20StakingPool stakingPool = factory.createERC20StakingPool(rewardToken, stakeToken, DURATION); 66 | 67 | assertEq(address(stakingPool.rewardToken()), address(rewardToken)); 68 | assertEq(address(stakingPool.stakeToken()), address(stakeToken)); 69 | assertEq(stakingPool.DURATION(), DURATION); 70 | } 71 | 72 | function testCorrectness_createERC721StakingPool(uint64 DURATION) public { 73 | ERC721StakingPool stakingPool = factory.createERC721StakingPool(rewardToken, stakeNFT, DURATION); 74 | 75 | assertEq(address(stakingPool.rewardToken()), address(rewardToken)); 76 | assertEq(address(stakingPool.stakeToken()), address(stakeNFT)); 77 | assertEq(stakingPool.DURATION(), DURATION); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/lib/ERC20.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-only 2 | pragma solidity >=0.8.0; 3 | 4 | import {Clone} from "@clones/Clone.sol"; 5 | 6 | /// @notice Modern and gas efficient ERC20 + EIP-2612 implementation. 7 | /// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/tokens/ERC20.sol) 8 | /// @author Modified from Uniswap (https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol) 9 | /// @dev Do not manually set balances without updating totalSupply, as the sum of all user balances must not exceed it. 10 | abstract contract ERC20 is Clone { 11 | /*/////////////////////////////////////////////////////////////// 12 | EVENTS 13 | //////////////////////////////////////////////////////////////*/ 14 | 15 | event Transfer(address indexed from, address indexed to, uint256 amount); 16 | 17 | event Approval(address indexed owner, address indexed spender, uint256 amount); 18 | 19 | /*/////////////////////////////////////////////////////////////// 20 | ERC20 STORAGE 21 | //////////////////////////////////////////////////////////////*/ 22 | 23 | uint256 public totalSupply; 24 | 25 | mapping(address => uint256) public balanceOf; 26 | 27 | mapping(address => mapping(address => uint256)) public allowance; 28 | 29 | /*/////////////////////////////////////////////////////////////// 30 | METADATA 31 | //////////////////////////////////////////////////////////////*/ 32 | 33 | function name() external pure returns (string memory) { 34 | return string(abi.encodePacked(_getArgUint256(0))); 35 | } 36 | 37 | function symbol() external pure returns (string memory) { 38 | return string(abi.encodePacked(_getArgUint256(0x20))); 39 | } 40 | 41 | function decimals() external pure returns (uint8) { 42 | return _getArgUint8(0x40); 43 | } 44 | 45 | /*/////////////////////////////////////////////////////////////// 46 | ERC20 LOGIC 47 | //////////////////////////////////////////////////////////////*/ 48 | 49 | function approve(address spender, uint256 amount) public virtual returns (bool) { 50 | allowance[msg.sender][spender] = amount; 51 | 52 | emit Approval(msg.sender, spender, amount); 53 | 54 | return true; 55 | } 56 | 57 | function transfer(address to, uint256 amount) public virtual returns (bool) { 58 | balanceOf[msg.sender] -= amount; 59 | 60 | // Cannot overflow because the sum of all user 61 | // balances can't exceed the max uint256 value. 62 | unchecked { 63 | balanceOf[to] += amount; 64 | } 65 | 66 | emit Transfer(msg.sender, to, amount); 67 | 68 | return true; 69 | } 70 | 71 | function transferFrom(address from, address to, uint256 amount) public virtual returns (bool) { 72 | uint256 allowed = allowance[from][msg.sender]; // Saves gas for limited approvals. 73 | 74 | if (allowed != type(uint256).max) { 75 | allowance[from][msg.sender] = allowed - amount; 76 | } 77 | 78 | balanceOf[from] -= amount; 79 | 80 | // Cannot overflow because the sum of all user 81 | // balances can't exceed the max uint256 value. 82 | unchecked { 83 | balanceOf[to] += amount; 84 | } 85 | 86 | emit Transfer(from, to, amount); 87 | 88 | return true; 89 | } 90 | 91 | /*/////////////////////////////////////////////////////////////// 92 | INTERNAL LOGIC 93 | //////////////////////////////////////////////////////////////*/ 94 | 95 | function _mint(address to, uint256 amount) internal virtual { 96 | totalSupply += amount; 97 | 98 | // Cannot overflow because the sum of all user 99 | // balances can't exceed the max uint256 value. 100 | unchecked { 101 | balanceOf[to] += amount; 102 | } 103 | 104 | emit Transfer(address(0), to, amount); 105 | } 106 | 107 | function _burn(address from, uint256 amount) internal virtual { 108 | balanceOf[from] -= amount; 109 | 110 | // Cannot underflow because a user's balance 111 | // will never be larger than the total supply. 112 | unchecked { 113 | totalSupply -= amount; 114 | } 115 | 116 | emit Transfer(from, address(0), amount); 117 | } 118 | 119 | function _getImmutableVariablesOffset() internal pure returns (uint256 offset) { 120 | assembly { 121 | offset := sub(calldatasize(), add(shr(240, calldataload(sub(calldatasize(), 2))), 2)) 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/StakingPoolFactory.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0 2 | pragma solidity ^0.8.11; 3 | 4 | import {ClonesWithImmutableArgs} from "@clones/ClonesWithImmutableArgs.sol"; 5 | 6 | import {ERC20} from "solmate/tokens/ERC20.sol"; 7 | import {ERC721} from "solmate/tokens/ERC721.sol"; 8 | 9 | import {xERC20} from "./xERC20.sol"; 10 | import {ERC20StakingPool} from "./ERC20StakingPool.sol"; 11 | import {ERC721StakingPool} from "./ERC721StakingPool.sol"; 12 | 13 | /// @title StakingPoolFactory 14 | /// @author zefram.eth 15 | /// @notice Factory for deploying ERC20StakingPool and ERC721StakingPool contracts cheaply 16 | contract StakingPoolFactory { 17 | /// ----------------------------------------------------------------------- 18 | /// Library usage 19 | /// ----------------------------------------------------------------------- 20 | 21 | using ClonesWithImmutableArgs for address; 22 | 23 | /// ----------------------------------------------------------------------- 24 | /// Events 25 | /// ----------------------------------------------------------------------- 26 | 27 | event CreateXERC20(xERC20 stakingPool); 28 | event CreateERC20StakingPool(ERC20StakingPool stakingPool); 29 | event CreateERC721StakingPool(ERC721StakingPool stakingPool); 30 | 31 | /// ----------------------------------------------------------------------- 32 | /// Immutable parameters 33 | /// ----------------------------------------------------------------------- 34 | 35 | /// @notice The contract used as the template for all xERC20 contracts created 36 | xERC20 public immutable xERC20Implementation; 37 | 38 | /// @notice The contract used as the template for all ERC20StakingPool contracts created 39 | ERC20StakingPool public immutable erc20StakingPoolImplementation; 40 | 41 | /// @notice The contract used as the template for all ERC721StakingPool contracts created 42 | ERC721StakingPool public immutable erc721StakingPoolImplementation; 43 | 44 | constructor( 45 | xERC20 xERC20Implementation_, 46 | ERC20StakingPool erc20StakingPoolImplementation_, 47 | ERC721StakingPool erc721StakingPoolImplementation_ 48 | ) { 49 | xERC20Implementation = xERC20Implementation_; 50 | erc20StakingPoolImplementation = erc20StakingPoolImplementation_; 51 | erc721StakingPoolImplementation = erc721StakingPoolImplementation_; 52 | } 53 | 54 | /// @notice Creates an xERC20 contract 55 | /// @dev Uses a modified minimal proxy contract that stores immutable parameters in code and 56 | /// passes them in through calldata. See ClonesWithImmutableArgs. 57 | /// @param name The name of the xERC20 token 58 | /// @param symbol The symbol of the xERC20 token 59 | /// @param decimals The decimals of the xERC20 token 60 | /// @param stakeToken The token being staked in the pool 61 | /// @param DURATION The length of each reward period, in seconds 62 | /// @return stakingPool The created xERC20 contract 63 | function createXERC20(bytes32 name, bytes32 symbol, uint8 decimals, ERC20 stakeToken, uint64 DURATION) 64 | external 65 | returns (xERC20 stakingPool) 66 | { 67 | bytes memory data = abi.encodePacked(name, symbol, decimals, stakeToken, DURATION); 68 | 69 | stakingPool = xERC20(address(xERC20Implementation).clone(data)); 70 | stakingPool.initialize(msg.sender); 71 | 72 | emit CreateXERC20(stakingPool); 73 | } 74 | 75 | /// @notice Creates an ERC20StakingPool contract 76 | /// @dev Uses a modified minimal proxy contract that stores immutable parameters in code and 77 | /// passes them in through calldata. See ClonesWithImmutableArgs. 78 | /// @param rewardToken The token being rewarded to stakers 79 | /// @param stakeToken The token being staked in the pool 80 | /// @param DURATION The length of each reward period, in seconds 81 | /// @return stakingPool The created ERC20StakingPool contract 82 | function createERC20StakingPool(ERC20 rewardToken, ERC20 stakeToken, uint64 DURATION) 83 | external 84 | returns (ERC20StakingPool stakingPool) 85 | { 86 | bytes memory data = abi.encodePacked(rewardToken, stakeToken, DURATION); 87 | 88 | stakingPool = ERC20StakingPool(address(erc20StakingPoolImplementation).clone(data)); 89 | stakingPool.initialize(msg.sender); 90 | 91 | emit CreateERC20StakingPool(stakingPool); 92 | } 93 | 94 | /// @notice Creates an ERC721StakingPool contract 95 | /// @dev Uses a modified minimal proxy contract that stores immutable parameters in code and 96 | /// passes them in through calldata. See ClonesWithImmutableArgs. 97 | /// @param rewardToken The token being rewarded to stakers 98 | /// @param stakeToken The token being staked in the pool 99 | /// @param DURATION The length of each reward period, in seconds 100 | /// @return stakingPool The created ERC721StakingPool contract 101 | function createERC721StakingPool(ERC20 rewardToken, ERC721 stakeToken, uint64 DURATION) 102 | external 103 | returns (ERC721StakingPool stakingPool) 104 | { 105 | bytes memory data = abi.encodePacked(rewardToken, stakeToken, DURATION); 106 | 107 | stakingPool = ERC721StakingPool(address(erc721StakingPoolImplementation).clone(data)); 108 | stakingPool.initialize(msg.sender); 109 | 110 | emit CreateERC721StakingPool(stakingPool); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/lib/FullMath.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity >=0.8.0; 3 | 4 | /// @title Contains 512-bit math functions 5 | /// @notice Facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision 6 | /// @dev Handles "phantom overflow" i.e., allows multiplication and division where an intermediate value overflows 256 bits 7 | library FullMath { 8 | /// @notice Calculates floor(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0 9 | /// @param a The multiplicand 10 | /// @param b The multiplier 11 | /// @param denominator The divisor 12 | /// @return result The 256-bit result 13 | /// @dev Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv 14 | function mulDiv(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) { 15 | unchecked { 16 | // 512-bit multiply [prod1 prod0] = a * b 17 | // Compute the product mod 2**256 and mod 2**256 - 1 18 | // then use the Chinese Remainder Theorem to reconstruct 19 | // the 512 bit result. The result is stored in two 256 20 | // variables such that product = prod1 * 2**256 + prod0 21 | uint256 prod0; // Least significant 256 bits of the product 22 | uint256 prod1; // Most significant 256 bits of the product 23 | assembly { 24 | let mm := mulmod(a, b, not(0)) 25 | prod0 := mul(a, b) 26 | prod1 := sub(sub(mm, prod0), lt(mm, prod0)) 27 | } 28 | 29 | // Handle non-overflow cases, 256 by 256 division 30 | if (prod1 == 0) { 31 | require(denominator > 0); 32 | assembly { 33 | result := div(prod0, denominator) 34 | } 35 | return result; 36 | } 37 | 38 | // Make sure the result is less than 2**256. 39 | // Also prevents denominator == 0 40 | require(denominator > prod1); 41 | 42 | /////////////////////////////////////////////// 43 | // 512 by 256 division. 44 | /////////////////////////////////////////////// 45 | 46 | // Make division exact by subtracting the remainder from [prod1 prod0] 47 | // Compute remainder using mulmod 48 | uint256 remainder; 49 | assembly { 50 | remainder := mulmod(a, b, denominator) 51 | } 52 | // Subtract 256 bit number from 512 bit number 53 | assembly { 54 | prod1 := sub(prod1, gt(remainder, prod0)) 55 | prod0 := sub(prod0, remainder) 56 | } 57 | 58 | // Factor powers of two out of denominator 59 | // Compute largest power of two divisor of denominator. 60 | // Always >= 1. 61 | uint256 twos = (type(uint256).max - denominator + 1) & denominator; 62 | // Divide denominator by power of two 63 | assembly { 64 | denominator := div(denominator, twos) 65 | } 66 | 67 | // Divide [prod1 prod0] by the factors of two 68 | assembly { 69 | prod0 := div(prod0, twos) 70 | } 71 | // Shift in bits from prod1 into prod0. For this we need 72 | // to flip `twos` such that it is 2**256 / twos. 73 | // If twos is zero, then it becomes one 74 | assembly { 75 | twos := add(div(sub(0, twos), twos), 1) 76 | } 77 | prod0 |= prod1 * twos; 78 | 79 | // Invert denominator mod 2**256 80 | // Now that denominator is an odd number, it has an inverse 81 | // modulo 2**256 such that denominator * inv = 1 mod 2**256. 82 | // Compute the inverse by starting with a seed that is correct 83 | // correct for four bits. That is, denominator * inv = 1 mod 2**4 84 | uint256 inv = (3 * denominator) ^ 2; 85 | // Now use Newton-Raphson iteration to improve the precision. 86 | // Thanks to Hensel's lifting lemma, this also works in modular 87 | // arithmetic, doubling the correct bits in each step. 88 | inv *= 2 - denominator * inv; // inverse mod 2**8 89 | inv *= 2 - denominator * inv; // inverse mod 2**16 90 | inv *= 2 - denominator * inv; // inverse mod 2**32 91 | inv *= 2 - denominator * inv; // inverse mod 2**64 92 | inv *= 2 - denominator * inv; // inverse mod 2**128 93 | inv *= 2 - denominator * inv; // inverse mod 2**256 94 | 95 | // Because the division is now exact we can divide by multiplying 96 | // with the modular inverse of denominator. This will give us the 97 | // correct result modulo 2**256. Since the precoditions guarantee 98 | // that the outcome is less than 2**256, this is the final result. 99 | // We don't need to compute the high bits of the result and prod1 100 | // is no longer required. 101 | result = prod0 * inv; 102 | return result; 103 | } 104 | } 105 | 106 | /// @notice Calculates ceil(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0 107 | /// @param a The multiplicand 108 | /// @param b The multiplier 109 | /// @param denominator The divisor 110 | /// @return result The 256-bit result 111 | function mulDivRoundingUp(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) { 112 | result = mulDiv(a, b, denominator); 113 | unchecked { 114 | if (mulmod(a, b, denominator) > 0) { 115 | require(result < type(uint256).max); 116 | result++; 117 | } 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/test/xERC20.t.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Unlicense 2 | pragma solidity ^0.8.4; 3 | 4 | import {BaseTest, console} from "./base/BaseTest.sol"; 5 | 6 | import {xERC20} from "../xERC20.sol"; 7 | import {FullMath} from "../lib/FullMath.sol"; 8 | import {TestERC20} from "./mocks/TestERC20.sol"; 9 | import {ERC20StakingPool} from "../ERC20StakingPool.sol"; 10 | import {ERC721StakingPool} from "../ERC721StakingPool.sol"; 11 | import {StakingPoolFactory} from "../StakingPoolFactory.sol"; 12 | 13 | contract xERC20Test is BaseTest { 14 | uint64 constant DURATION = 7 days; 15 | address constant tester = address(0x69); 16 | uint256 constant PRECISION = 1e18; 17 | 18 | StakingPoolFactory factory; 19 | TestERC20 stakeToken; 20 | xERC20 stakingPool; 21 | 22 | function setUp() public { 23 | xERC20 xERC20Implementation = new xERC20(); 24 | ERC20StakingPool erc20StakingPoolImplementation = new ERC20StakingPool(); 25 | ERC721StakingPool erc721StakingPoolImplementation = new ERC721StakingPool(); 26 | factory = new StakingPoolFactory( 27 | xERC20Implementation, 28 | erc20StakingPoolImplementation, 29 | erc721StakingPoolImplementation 30 | ); 31 | 32 | stakeToken = new TestERC20(); 33 | 34 | stakingPool = factory.createXERC20(bytes32("Staked SHT"), bytes32("xSHT"), 18, stakeToken, DURATION); 35 | stakingPool.setRewardDistributor(address(this), true); 36 | 37 | stakeToken.mint(address(this), 1000 ether); 38 | stakeToken.approve(address(stakingPool), type(uint256).max); 39 | 40 | // do initial staking 41 | stakingPool.stake(1 ether); 42 | 43 | // do initial reward distribution 44 | stakingPool.distributeReward(10 ether); 45 | } 46 | 47 | /// ------------------------------------------------------------------- 48 | /// Gas benchmarking 49 | /// ------------------------------------------------------------------- 50 | 51 | function testGas_stake() public { 52 | vm.warp(3 days); 53 | stakingPool.stake(1 ether); 54 | } 55 | 56 | function testGas_withdraw() public { 57 | vm.warp(3 days); 58 | stakingPool.withdraw(0.5 ether); 59 | } 60 | 61 | /// ------------------------------------------------------------------- 62 | /// Correctness tests 63 | /// ------------------------------------------------------------------- 64 | 65 | function testCorrectness_stake(uint128 amount_, uint56 warpTime) public { 66 | vm.assume(amount_ > 0); 67 | vm.assume(warpTime > 0); 68 | uint256 amount = amount_; 69 | 70 | // deploy fresh pool 71 | xERC20 stakingPool_ = factory.createXERC20(bytes32("Staked SHT"), bytes32("xSHT"), 18, stakeToken, DURATION); 72 | 73 | vm.startPrank(tester); 74 | 75 | // warp to future 76 | vm.warp(warpTime); 77 | 78 | // mint stake tokens 79 | stakeToken.mint(tester, amount); 80 | 81 | // stake 82 | uint256 beforeStakingPoolStakeTokenBalance = stakeToken.balanceOf(address(stakingPool_)); 83 | stakeToken.approve(address(stakingPool_), amount); 84 | uint256 xERC20Amount = stakingPool_.stake(amount); 85 | 86 | // check balances 87 | // took stake tokens from tester to staking pool 88 | assertEqDecimal(stakeToken.balanceOf(tester), 0, 18); 89 | assertEqDecimal(stakeToken.balanceOf(address(stakingPool_)) - beforeStakingPoolStakeTokenBalance, amount, 18); 90 | // gave correct share amount 91 | assertEqDecimal(xERC20Amount, FullMath.mulDiv(amount, PRECISION, stakingPool_.getPricePerFullShare()), 18); 92 | assertEqDecimal(stakingPool_.balanceOf(tester), xERC20Amount, 18); 93 | } 94 | 95 | function testCorrectness_withdraw(uint128 amount_, uint56 warpTime, uint56 stakeTime) public { 96 | vm.assume(amount_ > 0); 97 | vm.assume(warpTime > 0); 98 | vm.assume(stakeTime > 0); 99 | uint256 amount = amount_; 100 | 101 | // deploy fresh pool 102 | xERC20 stakingPool_ = factory.createXERC20(bytes32("Staked SHT"), bytes32("xSHT"), 18, stakeToken, DURATION); 103 | 104 | vm.startPrank(tester); 105 | 106 | // warp to future 107 | vm.warp(warpTime); 108 | 109 | // mint stake tokens 110 | stakeToken.mint(tester, amount); 111 | 112 | // stake 113 | uint256 beforeStakingTesterStakeTokenBalance = stakeToken.balanceOf(tester); 114 | uint256 beforeStakingPoolStakeTokenBalance = stakeToken.balanceOf(address(stakingPool_)); 115 | stakeToken.approve(address(stakingPool_), amount); 116 | uint256 xERC20Amount = stakingPool_.stake(amount); 117 | 118 | // warp to simulate staking 119 | vm.warp(uint256(warpTime) + uint256(stakeTime)); 120 | 121 | // withdraw 122 | stakingPool_.withdraw(xERC20Amount); 123 | 124 | // check balance 125 | // staking and unstaking didn't change tester stake token balance in aggregate 126 | assertEqDecimalEpsilonBelow(stakeToken.balanceOf(tester), beforeStakingTesterStakeTokenBalance, 18, 1e36); 127 | // staking and unstaking didn't change the staking pool's stake token balance in aggregate 128 | assertLeDecimal( 129 | stakeToken.balanceOf(address(stakingPool_)) - beforeStakingPoolStakeTokenBalance, 130 | beforeStakingPoolStakeTokenBalance / 1e18, 131 | 18 132 | ); 133 | // burnt xERC20 tokens of tester 134 | assertEqDecimal(stakingPool_.balanceOf(tester), 0, 18); 135 | } 136 | 137 | function testCorrectness_distributeReward(uint128 amount_, uint56 warpTime, uint8 stakeTimeAsDurationPercentage) 138 | public 139 | { 140 | vm.assume(amount_ > 0); 141 | vm.assume(warpTime > 0); 142 | vm.assume(stakeTimeAsDurationPercentage > 0); 143 | 144 | // deploy fresh pool 145 | xERC20 stakingPool_ = factory.createXERC20(bytes32("Staked SHT"), bytes32("xSHT"), 18, stakeToken, DURATION); 146 | stakingPool_.setRewardDistributor(address(this), true); 147 | stakeToken.approve(address(stakingPool_), type(uint256).max); 148 | stakingPool_.stake(1 ether); 149 | 150 | uint256 amount = amount_; 151 | 152 | // warp to some time in the future 153 | vm.warp(warpTime); 154 | 155 | // mint stake token 156 | stakeToken.mint(address(this), amount); 157 | 158 | // notify new rewards 159 | uint256 beforeTotalPoolValue = 160 | FullMath.mulDiv(stakingPool_.getPricePerFullShare(), stakingPool_.totalSupply(), PRECISION); 161 | stakingPool_.distributeReward(uint128(amount)); 162 | 163 | // warp to simulate staking 164 | uint256 stakeTime = (DURATION * uint256(stakeTimeAsDurationPercentage)) / 100; 165 | vm.warp(warpTime + stakeTime); 166 | 167 | // check assertions 168 | uint256 expectedRewardAmount; 169 | if (stakeTime >= DURATION) { 170 | // past second reward period, all rewards have been distributed 171 | expectedRewardAmount = amount; 172 | } else { 173 | // during second reward period, rewards are partially distributed 174 | expectedRewardAmount = (amount * stakeTimeAsDurationPercentage) / 100; 175 | } 176 | uint256 rewardAmount = FullMath.mulDiv( 177 | stakingPool_.getPricePerFullShare(), stakingPool_.totalSupply(), PRECISION 178 | ) - beforeTotalPoolValue; 179 | assertEqDecimalEpsilonAround(rewardAmount, expectedRewardAmount, 18, 1); 180 | } 181 | 182 | function testFail_cannotReinitialize() public { 183 | stakingPool.initialize(address(this)); 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /src/xERC20.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0 2 | pragma solidity ^0.8.4; 3 | 4 | import {ERC20} from "solmate/tokens/ERC20.sol"; 5 | import {SafeTransferLib} from "solmate/utils/SafeTransferLib.sol"; 6 | 7 | import {Ownable} from "./lib/Ownable.sol"; 8 | import {FullMath} from "./lib/FullMath.sol"; 9 | import {ERC20 as CloneERC20} from "./lib/ERC20.sol"; 10 | import {Multicall} from "./lib/Multicall.sol"; 11 | import {SelfPermit} from "./lib/SelfPermit.sol"; 12 | 13 | /// @title xERC20 14 | /// @author zefram.eth 15 | /// @notice A special type of ERC20 staking pool where the reward token is the same as 16 | /// the stake token. This enables stakers to receive an xERC20 token representing their 17 | /// stake that can then be transferred or plugged into other things (e.g. Uniswap). 18 | /// @dev xERC20 is inspired by xSUSHI, but is superior because rewards are distributed over time rather 19 | /// than immediately, which prevents MEV bots from stealing the rewards or malicious users staking immediately 20 | /// before the reward distribution and unstaking immediately after. 21 | contract xERC20 is CloneERC20, Ownable, Multicall, SelfPermit { 22 | /// ----------------------------------------------------------------------- 23 | /// Library usage 24 | /// ----------------------------------------------------------------------- 25 | 26 | using SafeTransferLib for ERC20; 27 | 28 | /// ----------------------------------------------------------------------- 29 | /// Errors 30 | /// ----------------------------------------------------------------------- 31 | 32 | error Error_ZeroOwner(); 33 | error Error_AlreadyInitialized(); 34 | error Error_NotRewardDistributor(); 35 | error Error_ZeroSupply(); 36 | 37 | /// ----------------------------------------------------------------------- 38 | /// Events 39 | /// ----------------------------------------------------------------------- 40 | 41 | event RewardAdded(uint128 reward); 42 | event Staked(address indexed user, uint256 stakeTokenAmount, uint256 xERC20Amount); 43 | event Withdrawn(address indexed user, uint256 stakeTokenAmount, uint256 xERC20Amount); 44 | 45 | /// ----------------------------------------------------------------------- 46 | /// Constants 47 | /// ----------------------------------------------------------------------- 48 | 49 | uint256 internal constant PRECISION = 1e18; 50 | 51 | /// ----------------------------------------------------------------------- 52 | /// Storage variables 53 | /// ----------------------------------------------------------------------- 54 | 55 | uint64 public currentUnlockEndTimestamp; 56 | uint64 public lastRewardTimestamp; 57 | uint128 public lastRewardAmount; 58 | 59 | /// @notice Tracks if an address can call notifyReward() 60 | mapping(address => bool) public isRewardDistributor; 61 | 62 | /// ----------------------------------------------------------------------- 63 | /// Immutable parameters 64 | /// ----------------------------------------------------------------------- 65 | 66 | /// @notice The token being staked in the pool 67 | function stakeToken() public pure returns (ERC20) { 68 | return ERC20(_getArgAddress(0x41)); 69 | } 70 | 71 | /// @notice The length of each reward period, in seconds 72 | function DURATION() public pure returns (uint64) { 73 | return _getArgUint64(0x55); 74 | } 75 | 76 | /// ----------------------------------------------------------------------- 77 | /// Initialization 78 | /// ----------------------------------------------------------------------- 79 | 80 | /// @notice Initializes the owner, called by StakingPoolFactory 81 | /// @param initialOwner The initial owner of the contract 82 | function initialize(address initialOwner) external { 83 | if (owner() != address(0)) { 84 | revert Error_AlreadyInitialized(); 85 | } 86 | if (initialOwner == address(0)) { 87 | revert Error_ZeroOwner(); 88 | } 89 | 90 | _transferOwnership(initialOwner); 91 | } 92 | 93 | /// ----------------------------------------------------------------------- 94 | /// User actions 95 | /// ----------------------------------------------------------------------- 96 | 97 | /// @notice Stake tokens to receive xERC20 tokens 98 | /// @param stakeTokenAmount The amount of tokens to stake 99 | /// @return xERC20Amount The amount of xERC20 tokens minted 100 | function stake(uint256 stakeTokenAmount) external virtual returns (uint256 xERC20Amount) { 101 | /// ----------------------------------------------------------------------- 102 | /// Validation 103 | /// ----------------------------------------------------------------------- 104 | 105 | if (stakeTokenAmount == 0) { 106 | return 0; 107 | } 108 | 109 | /// ----------------------------------------------------------------------- 110 | /// State updates 111 | /// ----------------------------------------------------------------------- 112 | 113 | xERC20Amount = FullMath.mulDiv(stakeTokenAmount, PRECISION, getPricePerFullShare()); 114 | _mint(msg.sender, xERC20Amount); 115 | 116 | /// ----------------------------------------------------------------------- 117 | /// Effects 118 | /// ----------------------------------------------------------------------- 119 | 120 | stakeToken().safeTransferFrom(msg.sender, address(this), stakeTokenAmount); 121 | 122 | emit Staked(msg.sender, stakeTokenAmount, xERC20Amount); 123 | } 124 | 125 | /// @notice Withdraw tokens by burning xERC20 tokens 126 | /// @param xERC20Amount The amount of xERC20 to burn 127 | /// @return stakeTokenAmount The amount of staked tokens withdrawn 128 | function withdraw(uint256 xERC20Amount) external virtual returns (uint256 stakeTokenAmount) { 129 | /// ----------------------------------------------------------------------- 130 | /// Validation 131 | /// ----------------------------------------------------------------------- 132 | 133 | if (xERC20Amount == 0) { 134 | return 0; 135 | } 136 | 137 | /// ----------------------------------------------------------------------- 138 | /// State updates 139 | /// ----------------------------------------------------------------------- 140 | stakeTokenAmount = FullMath.mulDiv(xERC20Amount, getPricePerFullShare(), PRECISION); 141 | _burn(msg.sender, xERC20Amount); 142 | 143 | /// ----------------------------------------------------------------------- 144 | /// Effects 145 | /// ----------------------------------------------------------------------- 146 | 147 | stakeToken().safeTransfer(msg.sender, stakeTokenAmount); 148 | 149 | emit Withdrawn(msg.sender, stakeTokenAmount, xERC20Amount); 150 | } 151 | 152 | /// ----------------------------------------------------------------------- 153 | /// Getters 154 | /// ----------------------------------------------------------------------- 155 | 156 | /// @notice Compute the amount of staked tokens that can be withdrawn by burning 157 | /// 1 xERC20 token. Increases linearly during a reward distribution period. 158 | /// @dev Initialized to be PRECISION (representing 1:1) 159 | /// @return The amount of staked tokens that can be withdrawn by burning 160 | /// 1 xERC20 token 161 | function getPricePerFullShare() public view returns (uint256) { 162 | uint256 totalShares = totalSupply; 163 | uint256 stakeTokenBalance = stakeToken().balanceOf(address(this)); 164 | if (totalShares == 0 || stakeTokenBalance == 0) { 165 | return PRECISION; 166 | } 167 | uint256 lastRewardAmount_ = lastRewardAmount; 168 | uint256 currentUnlockEndTimestamp_ = currentUnlockEndTimestamp; 169 | if (lastRewardAmount_ == 0 || block.timestamp >= currentUnlockEndTimestamp_) { 170 | // no rewards or rewards fully unlocked 171 | // entire balance is withdrawable 172 | return FullMath.mulDiv(stakeTokenBalance, PRECISION, totalShares); 173 | } else { 174 | // rewards not fully unlocked 175 | // deduct locked rewards from balance 176 | uint256 lastRewardTimestamp_ = lastRewardTimestamp; 177 | // can't overflow since lockedRewardAmount < lastRewardAmount 178 | uint256 lockedRewardAmount = (lastRewardAmount_ * (currentUnlockEndTimestamp_ - block.timestamp)) 179 | / (currentUnlockEndTimestamp_ - lastRewardTimestamp_); 180 | return FullMath.mulDiv(stakeTokenBalance - lockedRewardAmount, PRECISION, totalShares); 181 | } 182 | } 183 | 184 | /// ----------------------------------------------------------------------- 185 | /// Owner actions 186 | /// ----------------------------------------------------------------------- 187 | 188 | /// @notice Distributes rewards to xERC20 holders 189 | /// @dev When not in a distribution period, start a new one with rewardUnlockPeriod seconds. 190 | /// When in a distribution period, add rewards to current period. 191 | function distributeReward(uint128 rewardAmount) external { 192 | /// ----------------------------------------------------------------------- 193 | /// Validation 194 | /// ----------------------------------------------------------------------- 195 | 196 | if (totalSupply == 0) { 197 | revert Error_ZeroSupply(); 198 | } 199 | if (!isRewardDistributor[msg.sender]) { 200 | revert Error_NotRewardDistributor(); 201 | } 202 | 203 | /// ----------------------------------------------------------------------- 204 | /// Storage loads 205 | /// ----------------------------------------------------------------------- 206 | 207 | uint256 currentUnlockEndTimestamp_ = currentUnlockEndTimestamp; 208 | 209 | /// ----------------------------------------------------------------------- 210 | /// State updates 211 | /// ----------------------------------------------------------------------- 212 | 213 | if (block.timestamp >= currentUnlockEndTimestamp_) { 214 | // start new reward period 215 | currentUnlockEndTimestamp = uint64(block.timestamp + DURATION()); 216 | lastRewardAmount = rewardAmount; 217 | } else { 218 | // add rewards to current reward period 219 | // can't overflow since lockedRewardAmount < lastRewardAmount 220 | uint256 lockedRewardAmount = (lastRewardAmount * (currentUnlockEndTimestamp_ - block.timestamp)) 221 | / (currentUnlockEndTimestamp_ - lastRewardTimestamp); 222 | // will revert if lastRewardAmount overflows 223 | lastRewardAmount = uint128(rewardAmount + lockedRewardAmount); 224 | } 225 | lastRewardTimestamp = uint64(block.timestamp); 226 | 227 | /// ----------------------------------------------------------------------- 228 | /// Effects 229 | /// ----------------------------------------------------------------------- 230 | 231 | stakeToken().safeTransferFrom(msg.sender, address(this), rewardAmount); 232 | 233 | emit RewardAdded(rewardAmount); 234 | } 235 | 236 | /// @notice Lets the owner add/remove accounts from the list of reward distributors. 237 | /// Reward distributors can call notifyRewardAmount() 238 | /// @param rewardDistributor The account to add/remove 239 | /// @param isRewardDistributor_ True to add the account, false to remove the account 240 | function setRewardDistributor(address rewardDistributor, bool isRewardDistributor_) external onlyOwner { 241 | isRewardDistributor[rewardDistributor] = isRewardDistributor_; 242 | } 243 | } 244 | -------------------------------------------------------------------------------- /src/test/ERC20StakingPool.t.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Unlicense 2 | pragma solidity ^0.8.4; 3 | 4 | import {BaseTest, console} from "./base/BaseTest.sol"; 5 | 6 | import {xERC20} from "../xERC20.sol"; 7 | import {TestERC20} from "./mocks/TestERC20.sol"; 8 | import {ERC20StakingPool} from "../ERC20StakingPool.sol"; 9 | import {ERC721StakingPool} from "../ERC721StakingPool.sol"; 10 | import {StakingPoolFactory} from "../StakingPoolFactory.sol"; 11 | 12 | contract ERC20StakingPoolTest is BaseTest { 13 | uint64 constant DURATION = 365 days; 14 | uint256 constant REWARD_AMOUNT = 10 ether; 15 | address constant tester = address(0x69); 16 | 17 | StakingPoolFactory factory; 18 | TestERC20 rewardToken; 19 | TestERC20 stakeToken; 20 | ERC20StakingPool stakingPool; 21 | 22 | function setUp() public { 23 | xERC20 xERC20Implementation = new xERC20(); 24 | ERC20StakingPool erc20StakingPoolImplementation = new ERC20StakingPool(); 25 | ERC721StakingPool erc721StakingPoolImplementation = new ERC721StakingPool(); 26 | factory = new StakingPoolFactory( 27 | xERC20Implementation, 28 | erc20StakingPoolImplementation, 29 | erc721StakingPoolImplementation 30 | ); 31 | 32 | rewardToken = new TestERC20(); 33 | stakeToken = new TestERC20(); 34 | 35 | stakingPool = factory.createERC20StakingPool(rewardToken, stakeToken, DURATION); 36 | 37 | rewardToken.mint(address(this), 1000 ether); 38 | stakeToken.mint(address(this), 1000 ether); 39 | stakeToken.approve(address(stakingPool), type(uint256).max); 40 | 41 | // do initial staking 42 | stakingPool.stake(1 ether); 43 | 44 | // distribute rewards 45 | rewardToken.transfer(address(stakingPool), REWARD_AMOUNT); 46 | stakingPool.setRewardDistributor(address(this), true); 47 | stakingPool.notifyRewardAmount(REWARD_AMOUNT); 48 | } 49 | 50 | /// ------------------------------------------------------------------- 51 | /// Gas benchmarking 52 | /// ------------------------------------------------------------------- 53 | 54 | function testGas_stake() public { 55 | vm.warp(7 days); 56 | stakingPool.stake(1 ether); 57 | } 58 | 59 | function testGas_withdraw() public { 60 | vm.warp(7 days); 61 | stakingPool.withdraw(0.5 ether); 62 | } 63 | 64 | function testGas_getReward() public { 65 | vm.warp(7 days); 66 | stakingPool.getReward(); 67 | } 68 | 69 | function testGas_exit() public { 70 | vm.warp(7 days); 71 | stakingPool.exit(); 72 | } 73 | 74 | /// ------------------------------------------------------------------- 75 | /// Correctness tests 76 | /// ------------------------------------------------------------------- 77 | 78 | function testCorrectness_stake(uint128 amount_, uint56 warpTime) public { 79 | vm.assume(amount_ > 0); 80 | vm.assume(warpTime > 0); 81 | uint256 amount = amount_; 82 | 83 | vm.startPrank(tester); 84 | 85 | // warp to future 86 | vm.warp(warpTime); 87 | 88 | // mint stake tokens 89 | stakeToken.mint(tester, amount); 90 | 91 | // stake 92 | uint256 beforeStakingPoolStakeTokenBalance = stakeToken.balanceOf(address(stakingPool)); 93 | stakeToken.approve(address(stakingPool), amount); 94 | stakingPool.stake(amount); 95 | 96 | // check balance 97 | assertEqDecimal(stakeToken.balanceOf(tester), 0, 18); 98 | assertEqDecimal(stakeToken.balanceOf(address(stakingPool)) - beforeStakingPoolStakeTokenBalance, amount, 18); 99 | assertEqDecimal(stakingPool.balanceOf(tester), amount, 18); 100 | } 101 | 102 | function testCorrectness_withdraw(uint128 amount_, uint56 warpTime, uint56 stakeTime) public { 103 | vm.assume(amount_ > 0); 104 | vm.assume(warpTime > 0); 105 | uint256 amount = amount_; 106 | 107 | vm.startPrank(tester); 108 | 109 | // warp to future 110 | vm.warp(warpTime); 111 | 112 | // mint stake tokens 113 | stakeToken.mint(tester, amount); 114 | 115 | // stake 116 | uint256 beforeStakingPoolStakeTokenBalance = stakeToken.balanceOf(address(stakingPool)); 117 | stakeToken.approve(address(stakingPool), amount); 118 | stakingPool.stake(amount); 119 | 120 | // warp to simulate staking 121 | vm.warp(uint256(warpTime) + uint256(stakeTime)); 122 | 123 | // withdraw 124 | stakingPool.withdraw(amount); 125 | 126 | // check balance 127 | assertEqDecimal(stakeToken.balanceOf(tester), amount, 18); 128 | assertEqDecimal(stakeToken.balanceOf(address(stakingPool)) - beforeStakingPoolStakeTokenBalance, 0, 18); 129 | assertEqDecimal(stakingPool.balanceOf(tester), 0, 18); 130 | } 131 | 132 | function testCorrectness_getReward(uint128 amount0_, uint128 amount1_, uint8 stakeTimeAsDurationPercentage) 133 | public 134 | { 135 | vm.assume(amount0_ > 0); 136 | vm.assume(amount1_ > 0); 137 | vm.assume(stakeTimeAsDurationPercentage > 0); 138 | uint256 amount0 = amount0_; 139 | uint256 amount1 = amount1_; 140 | 141 | /// ----------------------------------------------------------------------- 142 | /// Stake using address(this) 143 | /// ----------------------------------------------------------------------- 144 | 145 | // start from clean slate 146 | stakingPool.exit(); 147 | 148 | // mint stake tokens 149 | stakeToken.mint(address(this), amount0); 150 | 151 | // stake 152 | stakingPool.stake(amount0); 153 | 154 | /// ----------------------------------------------------------------------- 155 | /// Stake using tester 156 | /// ----------------------------------------------------------------------- 157 | 158 | vm.startPrank(tester); 159 | 160 | // mint stake tokens 161 | stakeToken.mint(tester, amount1); 162 | 163 | // stake 164 | stakeToken.approve(address(stakingPool), amount1); 165 | stakingPool.stake(amount1); 166 | 167 | // warp to simulate staking 168 | uint256 stakeTime = (DURATION * uint256(stakeTimeAsDurationPercentage)) / 100; 169 | vm.warp(stakeTime); 170 | 171 | // get reward 172 | uint256 beforeBalance = rewardToken.balanceOf(tester); 173 | stakingPool.getReward(); 174 | uint256 rewardAmount = rewardToken.balanceOf(tester) - beforeBalance; 175 | 176 | // check assertions 177 | uint256 expectedRewardAmount; 178 | if (stakeTime >= DURATION) { 179 | // past first reward period, all rewards have been distributed 180 | expectedRewardAmount = (REWARD_AMOUNT * amount1) / (amount0 + amount1); 181 | } else { 182 | // during first reward period, rewards are partially distributed 183 | expectedRewardAmount = 184 | (((REWARD_AMOUNT * stakeTimeAsDurationPercentage) / 100) * amount1) / (amount0 + amount1); 185 | } 186 | assertEqDecimalEpsilonBelow(rewardAmount, expectedRewardAmount, 18, 1e4); 187 | } 188 | 189 | function testCorrectness_exit(uint128 amount0_, uint128 amount1_, uint8 stakeTimeAsDurationPercentage) public { 190 | vm.assume(amount0_ > 0); 191 | vm.assume(amount1_ > 0); 192 | vm.assume(stakeTimeAsDurationPercentage > 0); 193 | uint256 amount0 = amount0_; 194 | uint256 amount1 = amount1_; 195 | 196 | /// ----------------------------------------------------------------------- 197 | /// Stake using address(this) 198 | /// ----------------------------------------------------------------------- 199 | 200 | // start from clean slate 201 | stakingPool.exit(); 202 | 203 | // mint stake tokens 204 | stakeToken.mint(address(this), amount0); 205 | 206 | // stake 207 | stakingPool.stake(amount0); 208 | 209 | /// ----------------------------------------------------------------------- 210 | /// Stake using tester 211 | /// ----------------------------------------------------------------------- 212 | 213 | vm.startPrank(tester); 214 | 215 | // mint stake tokens 216 | stakeToken.mint(tester, amount1); 217 | 218 | // stake 219 | stakeToken.approve(address(stakingPool), amount1); 220 | stakingPool.stake(amount1); 221 | 222 | // warp to simulate staking 223 | uint256 stakeTime = (DURATION * uint256(stakeTimeAsDurationPercentage)) / 100; 224 | vm.warp(stakeTime); 225 | 226 | // exit 227 | uint256 beforeStakeTokenBalance = stakeToken.balanceOf(tester); 228 | uint256 beforeRewardTokenBalance = rewardToken.balanceOf(tester); 229 | stakingPool.exit(); 230 | uint256 withdrawAmount = stakeToken.balanceOf(tester) - beforeStakeTokenBalance; 231 | uint256 rewardAmount = rewardToken.balanceOf(tester) - beforeRewardTokenBalance; 232 | 233 | // check assertions 234 | assertEqDecimal(withdrawAmount, amount1, 18); 235 | uint256 expectedRewardAmount; 236 | if (stakeTime >= DURATION) { 237 | expectedRewardAmount = (REWARD_AMOUNT * amount1) / (amount0 + amount1); 238 | } else { 239 | expectedRewardAmount = 240 | (((REWARD_AMOUNT * stakeTimeAsDurationPercentage) / 100) * amount1) / (amount0 + amount1); 241 | } 242 | assertEqDecimalEpsilonBelow(rewardAmount, expectedRewardAmount, 18, 1e4); 243 | } 244 | 245 | function testCorrectness_notifyRewardAmount(uint128 amount_, uint56 warpTime, uint8 stakeTimeAsDurationPercentage) 246 | public 247 | { 248 | vm.assume(amount_ > 0); 249 | vm.assume(warpTime > 0); 250 | vm.assume(stakeTimeAsDurationPercentage > 0); 251 | uint256 amount = amount_; 252 | 253 | // warp to some time in the future 254 | vm.warp(warpTime); 255 | 256 | // get earned reward amount from existing rewards 257 | uint256 beforeBalance = rewardToken.balanceOf(address(this)); 258 | stakingPool.getReward(); 259 | uint256 rewardAmount = rewardToken.balanceOf(address(this)) - beforeBalance; 260 | 261 | // compute expected earned rewards 262 | uint256 expectedRewardAmount; 263 | if (warpTime >= DURATION) { 264 | // past first reward period, all rewards have been distributed 265 | expectedRewardAmount = REWARD_AMOUNT; 266 | } else { 267 | // during first reward period, rewards are partially distributed 268 | expectedRewardAmount = (REWARD_AMOUNT * warpTime) / DURATION; 269 | } 270 | uint256 leftoverRewardAmount = REWARD_AMOUNT - expectedRewardAmount; 271 | 272 | // mint reward tokens 273 | rewardToken.mint(address(stakingPool), amount); 274 | 275 | // notify new rewards 276 | stakingPool.notifyRewardAmount(amount); 277 | 278 | // warp to simulate staking 279 | uint256 stakeTime = (DURATION * uint256(stakeTimeAsDurationPercentage)) / 100; 280 | vm.warp(warpTime + stakeTime); 281 | 282 | // get reward 283 | beforeBalance = rewardToken.balanceOf(address(this)); 284 | stakingPool.getReward(); 285 | rewardAmount += rewardToken.balanceOf(address(this)) - beforeBalance; 286 | 287 | // check assertions 288 | if (stakeTime >= DURATION) { 289 | // past second reward period, all rewards have been distributed 290 | expectedRewardAmount += leftoverRewardAmount + amount; 291 | } else { 292 | // during second reward period, rewards are partially distributed 293 | expectedRewardAmount += ((leftoverRewardAmount + amount) * stakeTimeAsDurationPercentage) / 100; 294 | } 295 | assertEqDecimalEpsilonBelow(rewardAmount, expectedRewardAmount, 18, 1e4); 296 | } 297 | 298 | function testFail_cannotReinitialize() public { 299 | stakingPool.initialize(address(this)); 300 | } 301 | } 302 | -------------------------------------------------------------------------------- /src/test/ERC721StakingPool.t.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Unlicense 2 | pragma solidity ^0.8.4; 3 | 4 | import {BaseTest, console} from "./base/BaseTest.sol"; 5 | 6 | import {ERC721TokenReceiver} from "solmate/tokens/ERC721.sol"; 7 | 8 | import {xERC20} from "../xERC20.sol"; 9 | import {TestERC20} from "./mocks/TestERC20.sol"; 10 | import {TestERC721} from "./mocks/TestERC721.sol"; 11 | import {ERC20StakingPool} from "../ERC20StakingPool.sol"; 12 | import {ERC721StakingPool} from "../ERC721StakingPool.sol"; 13 | import {StakingPoolFactory} from "../StakingPoolFactory.sol"; 14 | 15 | contract ERC721StakingPoolTest is BaseTest, ERC721TokenReceiver { 16 | uint64 constant DURATION = 365 days; 17 | uint256 constant REWARD_AMOUNT = 10 ether; 18 | uint256 constant INIT_NFT_BALANCE = 266; 19 | uint256 constant INIT_STAKE_BALANCE = 10; 20 | address constant tester = address(0x69); 21 | 22 | StakingPoolFactory factory; 23 | TestERC20 rewardToken; 24 | TestERC721 stakeToken; 25 | ERC721StakingPool stakingPool; 26 | 27 | function setUp() public { 28 | xERC20 xERC20Implementation = new xERC20(); 29 | ERC20StakingPool erc20StakingPoolImplementation = new ERC20StakingPool(); 30 | ERC721StakingPool erc721StakingPoolImplementation = new ERC721StakingPool(); 31 | factory = new StakingPoolFactory( 32 | xERC20Implementation, 33 | erc20StakingPoolImplementation, 34 | erc721StakingPoolImplementation 35 | ); 36 | 37 | rewardToken = new TestERC20(); 38 | stakeToken = new TestERC721(); 39 | 40 | stakingPool = factory.createERC721StakingPool(rewardToken, stakeToken, DURATION); 41 | 42 | rewardToken.mint(address(this), 1000 ether); 43 | for (uint256 i = 0; i < INIT_NFT_BALANCE; i++) { 44 | stakeToken.safeMint(address(this), i); 45 | } 46 | stakeToken.setApprovalForAll(address(stakingPool), true); 47 | 48 | // do initial staking 49 | uint256[] memory idList = _getIdList(0, INIT_STAKE_BALANCE); 50 | stakingPool.stake(idList); 51 | 52 | // distribute rewards 53 | rewardToken.transfer(address(stakingPool), REWARD_AMOUNT); 54 | stakingPool.setRewardDistributor(address(this), true); 55 | stakingPool.notifyRewardAmount(REWARD_AMOUNT); 56 | } 57 | 58 | /// ------------------------------------------------------------------- 59 | /// Gas benchmarking 60 | /// ------------------------------------------------------------------- 61 | 62 | function testGas_stake() public { 63 | vm.warp(7 days); 64 | 65 | uint256[] memory idList = _getIdList(INIT_STAKE_BALANCE, 1); 66 | stakingPool.stake(idList); 67 | } 68 | 69 | function testGas_withdraw() public { 70 | vm.warp(7 days); 71 | 72 | uint256[] memory idList = _getIdList(0, 1); 73 | stakingPool.withdraw(idList); 74 | } 75 | 76 | function testGas_getReward() public { 77 | vm.warp(7 days); 78 | stakingPool.getReward(); 79 | } 80 | 81 | function testGas_exit() public { 82 | vm.warp(7 days); 83 | 84 | uint256[] memory idList = _getIdList(0, 1); 85 | stakingPool.exit(idList); 86 | } 87 | 88 | /// ------------------------------------------------------------------- 89 | /// Correctness tests 90 | /// ------------------------------------------------------------------- 91 | 92 | function testCorrectness_stake(uint8 amount, uint56 warpTime) public { 93 | vm.assume(amount > 0); 94 | vm.assume(warpTime > 0); 95 | 96 | // warp to future 97 | vm.warp(warpTime); 98 | 99 | // stake 100 | uint256 beforeThisStakedBalance = stakingPool.balanceOf(address(this)); 101 | uint256 beforeThisStakeTokenBalance = stakeToken.balanceOf(address(this)); 102 | uint256 beforeStakingPoolStakeTokenBalance = stakeToken.balanceOf(address(stakingPool)); 103 | uint256[] memory idList = _getIdList(INIT_STAKE_BALANCE, amount); 104 | stakingPool.stake(idList); 105 | 106 | // check balance 107 | assertEq(beforeThisStakeTokenBalance - stakeToken.balanceOf(address(this)), amount); 108 | assertEq(stakeToken.balanceOf(address(stakingPool)) - beforeStakingPoolStakeTokenBalance, amount); 109 | assertEq(stakingPool.balanceOf(address(this)) - beforeThisStakedBalance, amount); 110 | } 111 | 112 | function testCorrectness_withdraw(uint8 amount, uint56 warpTime, uint56 stakeTime) public { 113 | vm.assume(amount > 0); 114 | vm.assume(warpTime > 0); 115 | vm.assume(stakeTime > 0); 116 | 117 | // warp to future 118 | vm.warp(warpTime); 119 | 120 | // stake 121 | uint256[] memory idList = _getIdList(INIT_STAKE_BALANCE, amount); 122 | stakingPool.stake(idList); 123 | 124 | // warp to simulate staking 125 | vm.warp(uint256(warpTime) + uint256(stakeTime)); 126 | 127 | // withdraw 128 | uint256 beforeThisStakedBalance = stakingPool.balanceOf(address(this)); 129 | uint256 beforeThisStakeTokenBalance = stakeToken.balanceOf(address(this)); 130 | uint256 beforeStakingPoolStakeTokenBalance = stakeToken.balanceOf(address(stakingPool)); 131 | stakingPool.withdraw(idList); 132 | 133 | // check balance 134 | assertEq(stakeToken.balanceOf(address(this)) - beforeThisStakeTokenBalance, amount); 135 | assertEq(beforeStakingPoolStakeTokenBalance - stakeToken.balanceOf(address(stakingPool)), amount); 136 | assertEq(beforeThisStakedBalance - stakingPool.balanceOf(address(this)), amount); 137 | } 138 | 139 | function testCorrectness_getReward(uint8 amount0, uint8 amount1, uint8 stakeTimeAsDurationPercentage) public { 140 | vm.assume(amount0 > 0); 141 | vm.assume(amount1 > 0); 142 | vm.assume(stakeTimeAsDurationPercentage > 0); 143 | 144 | /// ----------------------------------------------------------------------- 145 | /// Stake using address(this) 146 | /// ----------------------------------------------------------------------- 147 | 148 | // start from clean slate 149 | stakingPool.exit(_getIdList(0, INIT_STAKE_BALANCE)); 150 | 151 | // stake 152 | stakingPool.stake(_getIdList(0, amount0)); 153 | 154 | /// ----------------------------------------------------------------------- 155 | /// Stake using tester 156 | /// ----------------------------------------------------------------------- 157 | 158 | vm.startPrank(tester); 159 | 160 | // mint stake tokens 161 | uint256 startId = INIT_NFT_BALANCE; 162 | uint256[] memory idList = _getIdList(startId, amount1); 163 | for (uint256 i = 0; i < amount1; i++) { 164 | stakeToken.safeMint(tester, idList[i]); 165 | } 166 | 167 | // stake 168 | stakeToken.setApprovalForAll(address(stakingPool), true); 169 | stakingPool.stake(idList); 170 | 171 | // warp to simulate staking 172 | uint256 stakeTime = (DURATION * uint256(stakeTimeAsDurationPercentage)) / 100; 173 | vm.warp(stakeTime); 174 | 175 | // get reward 176 | uint256 beforeBalance = rewardToken.balanceOf(tester); 177 | stakingPool.getReward(); 178 | uint256 rewardAmount = rewardToken.balanceOf(tester) - beforeBalance; 179 | 180 | // check assertions 181 | uint256 expectedRewardAmount; 182 | if (stakeTime >= DURATION) { 183 | // past first reward period, all rewards have been distributed 184 | expectedRewardAmount = (REWARD_AMOUNT * uint256(amount1)) / (uint256(amount0) + uint256(amount1)); 185 | } else { 186 | // during first reward period, rewards are partially distributed 187 | expectedRewardAmount = (((REWARD_AMOUNT * stakeTimeAsDurationPercentage) / 100) * uint256(amount1)) 188 | / (uint256(amount0) + uint256(amount1)); 189 | } 190 | assertEqDecimalEpsilonBelow(rewardAmount, expectedRewardAmount, 18, 1e4); 191 | } 192 | 193 | function testCorrectness_exit(uint8 amount0, uint8 amount1, uint8 stakeTimeAsDurationPercentage) public { 194 | vm.assume(amount0 > 0); 195 | vm.assume(amount1 > 0); 196 | vm.assume(stakeTimeAsDurationPercentage > 0); 197 | 198 | /// ----------------------------------------------------------------------- 199 | /// Stake using address(this) 200 | /// ----------------------------------------------------------------------- 201 | 202 | // start from clean slate 203 | stakingPool.exit(_getIdList(0, INIT_STAKE_BALANCE)); 204 | 205 | // stake 206 | stakingPool.stake(_getIdList(0, amount0)); 207 | 208 | /// ----------------------------------------------------------------------- 209 | /// Stake using tester 210 | /// ----------------------------------------------------------------------- 211 | 212 | vm.startPrank(tester); 213 | 214 | // mint stake tokens 215 | uint256 startId = INIT_NFT_BALANCE; 216 | uint256[] memory idList = _getIdList(startId, amount1); 217 | for (uint256 i = 0; i < amount1; i++) { 218 | stakeToken.safeMint(tester, idList[i]); 219 | } 220 | 221 | // stake 222 | stakeToken.setApprovalForAll(address(stakingPool), true); 223 | stakingPool.stake(idList); 224 | 225 | // warp to simulate staking 226 | uint256 stakeTime = (DURATION * uint256(stakeTimeAsDurationPercentage)) / 100; 227 | vm.warp(stakeTime); 228 | 229 | // exit 230 | uint256 beforeStakeTokenBalance = stakeToken.balanceOf(tester); 231 | uint256 beforeRewardTokenBalance = rewardToken.balanceOf(tester); 232 | stakingPool.exit(idList); 233 | uint256 withdrawAmount = stakeToken.balanceOf(tester) - beforeStakeTokenBalance; 234 | uint256 rewardAmount = rewardToken.balanceOf(tester) - beforeRewardTokenBalance; 235 | 236 | // check assertions 237 | assertEq(withdrawAmount, amount1); 238 | uint256 expectedRewardAmount; 239 | if (stakeTime >= DURATION) { 240 | // past first reward period, all rewards have been distributed 241 | expectedRewardAmount = (REWARD_AMOUNT * uint256(amount1)) / (uint256(amount0) + uint256(amount1)); 242 | } else { 243 | // during first reward period, rewards are partially distributed 244 | expectedRewardAmount = (((REWARD_AMOUNT * stakeTimeAsDurationPercentage) / 100) * uint256(amount1)) 245 | / (uint256(amount0) + uint256(amount1)); 246 | } 247 | assertEqDecimalEpsilonBelow(rewardAmount, expectedRewardAmount, 18, 1e4); 248 | } 249 | 250 | function testCorrectness_notifyRewardAmount(uint128 amount_, uint56 warpTime, uint8 stakeTimeAsDurationPercentage) 251 | public 252 | { 253 | vm.assume(amount_ > 0); 254 | vm.assume(warpTime > 0); 255 | vm.assume(stakeTimeAsDurationPercentage > 0); 256 | uint256 amount = amount_; 257 | 258 | // warp to some time in the future 259 | vm.warp(warpTime); 260 | 261 | // get earned reward amount from existing rewards 262 | uint256 beforeBalance = rewardToken.balanceOf(address(this)); 263 | stakingPool.getReward(); 264 | uint256 rewardAmount = rewardToken.balanceOf(address(this)) - beforeBalance; 265 | 266 | // compute expected earned rewards 267 | uint256 expectedRewardAmount; 268 | if (warpTime >= DURATION) { 269 | // past first reward period, all rewards have been distributed 270 | expectedRewardAmount = REWARD_AMOUNT; 271 | } else { 272 | // during first reward period, rewards are partially distributed 273 | expectedRewardAmount = (REWARD_AMOUNT * warpTime) / DURATION; 274 | } 275 | uint256 leftoverRewardAmount = REWARD_AMOUNT - expectedRewardAmount; 276 | 277 | // mint reward tokens 278 | rewardToken.mint(address(stakingPool), amount); 279 | 280 | // notify new rewards 281 | stakingPool.notifyRewardAmount(amount); 282 | 283 | // warp to simulate staking 284 | uint256 stakeTime = (DURATION * uint256(stakeTimeAsDurationPercentage)) / 100; 285 | vm.warp(warpTime + stakeTime); 286 | 287 | // get reward 288 | beforeBalance = rewardToken.balanceOf(address(this)); 289 | stakingPool.getReward(); 290 | rewardAmount += rewardToken.balanceOf(address(this)) - beforeBalance; 291 | 292 | // check assertions 293 | if (stakeTime >= DURATION) { 294 | // past second reward period, all rewards have been distributed 295 | expectedRewardAmount += leftoverRewardAmount + amount; 296 | } else { 297 | // during second reward period, rewards are partially distributed 298 | expectedRewardAmount += ((leftoverRewardAmount + amount) * stakeTimeAsDurationPercentage) / 100; 299 | } 300 | assertEqDecimalEpsilonBelow(rewardAmount, expectedRewardAmount, 18, 1e4); 301 | } 302 | 303 | function testFail_cannotReinitialize() public { 304 | stakingPool.initialize(address(this)); 305 | } 306 | 307 | /// ----------------------------------------------------------------------- 308 | /// ERC721 compliance 309 | /// ----------------------------------------------------------------------- 310 | 311 | function onERC721Received(address, address, uint256, bytes calldata) external pure override returns (bytes4) { 312 | return this.onERC721Received.selector; 313 | } 314 | 315 | /// ----------------------------------------------------------------------- 316 | /// Utilities 317 | /// ----------------------------------------------------------------------- 318 | 319 | function _getIdList(uint256 startId, uint256 amount) internal pure returns (uint256[] memory idList) { 320 | idList = new uint256[](amount); 321 | for (uint256 i = startId; i < startId + amount; i++) { 322 | idList[i - startId] = i; 323 | } 324 | } 325 | } 326 | -------------------------------------------------------------------------------- /src/ERC20StakingPool.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0 2 | 3 | pragma solidity ^0.8.4; 4 | 5 | import {Clone} from "@clones/Clone.sol"; 6 | 7 | import {ERC20} from "solmate/tokens/ERC20.sol"; 8 | import {SafeTransferLib} from "solmate/utils/SafeTransferLib.sol"; 9 | 10 | import {Ownable} from "./lib/Ownable.sol"; 11 | import {FullMath} from "./lib/FullMath.sol"; 12 | import {Multicall} from "./lib/Multicall.sol"; 13 | import {SelfPermit} from "./lib/SelfPermit.sol"; 14 | 15 | /// @title ERC20StakingPool 16 | /// @author zefram.eth 17 | /// @notice A modern, gas optimized staking pool contract for rewarding ERC20 stakers 18 | /// with ERC20 tokens periodically and continuously 19 | contract ERC20StakingPool is Ownable, Clone, Multicall, SelfPermit { 20 | /// ----------------------------------------------------------------------- 21 | /// Library usage 22 | /// ----------------------------------------------------------------------- 23 | 24 | using SafeTransferLib for ERC20; 25 | 26 | /// ----------------------------------------------------------------------- 27 | /// Errors 28 | /// ----------------------------------------------------------------------- 29 | 30 | error Error_ZeroOwner(); 31 | error Error_AlreadyInitialized(); 32 | error Error_NotRewardDistributor(); 33 | error Error_AmountTooLarge(); 34 | 35 | /// ----------------------------------------------------------------------- 36 | /// Events 37 | /// ----------------------------------------------------------------------- 38 | 39 | event RewardAdded(uint256 reward); 40 | event Staked(address indexed user, uint256 amount); 41 | event Withdrawn(address indexed user, uint256 amount); 42 | event RewardPaid(address indexed user, uint256 reward); 43 | 44 | /// ----------------------------------------------------------------------- 45 | /// Constants 46 | /// ----------------------------------------------------------------------- 47 | 48 | uint256 internal constant PRECISION = 1e30; 49 | 50 | /// ----------------------------------------------------------------------- 51 | /// Storage variables 52 | /// ----------------------------------------------------------------------- 53 | 54 | /// @notice The last Unix timestamp (in seconds) when rewardPerTokenStored was updated 55 | uint64 public lastUpdateTime; 56 | /// @notice The Unix timestamp (in seconds) at which the current reward period ends 57 | uint64 public periodFinish; 58 | 59 | /// @notice The per-second rate at which rewardPerToken increases 60 | uint256 public rewardRate; 61 | /// @notice The last stored rewardPerToken value 62 | uint256 public rewardPerTokenStored; 63 | /// @notice The total tokens staked in the pool 64 | uint256 public totalSupply; 65 | 66 | /// @notice Tracks if an address can call notifyReward() 67 | mapping(address => bool) public isRewardDistributor; 68 | 69 | /// @notice The amount of tokens staked by an account 70 | mapping(address => uint256) public balanceOf; 71 | /// @notice The rewardPerToken value when an account last staked/withdrew/withdrew rewards 72 | mapping(address => uint256) public userRewardPerTokenPaid; 73 | /// @notice The earned() value when an account last staked/withdrew/withdrew rewards 74 | mapping(address => uint256) public rewards; 75 | 76 | /// ----------------------------------------------------------------------- 77 | /// Immutable parameters 78 | /// ----------------------------------------------------------------------- 79 | 80 | /// @notice The token being rewarded to stakers 81 | function rewardToken() public pure returns (ERC20 rewardToken_) { 82 | return ERC20(_getArgAddress(0)); 83 | } 84 | 85 | /// @notice The token being staked in the pool 86 | function stakeToken() public pure returns (ERC20 stakeToken_) { 87 | return ERC20(_getArgAddress(0x14)); 88 | } 89 | 90 | /// @notice The length of each reward period, in seconds 91 | function DURATION() public pure returns (uint64 DURATION_) { 92 | return _getArgUint64(0x28); 93 | } 94 | 95 | /// ----------------------------------------------------------------------- 96 | /// Initialization 97 | /// ----------------------------------------------------------------------- 98 | 99 | /// @notice Initializes the owner, called by StakingPoolFactory 100 | /// @param initialOwner The initial owner of the contract 101 | function initialize(address initialOwner) external { 102 | if (owner() != address(0)) { 103 | revert Error_AlreadyInitialized(); 104 | } 105 | if (initialOwner == address(0)) { 106 | revert Error_ZeroOwner(); 107 | } 108 | 109 | _transferOwnership(initialOwner); 110 | } 111 | 112 | /// ----------------------------------------------------------------------- 113 | /// User actions 114 | /// ----------------------------------------------------------------------- 115 | 116 | /// @notice Stakes tokens in the pool to earn rewards 117 | /// @param amount The amount of tokens to stake 118 | function stake(uint256 amount) external { 119 | /// ----------------------------------------------------------------------- 120 | /// Validation 121 | /// ----------------------------------------------------------------------- 122 | 123 | if (amount == 0) { 124 | return; 125 | } 126 | 127 | /// ----------------------------------------------------------------------- 128 | /// Storage loads 129 | /// ----------------------------------------------------------------------- 130 | 131 | uint256 accountBalance = balanceOf[msg.sender]; 132 | uint64 lastTimeRewardApplicable_ = lastTimeRewardApplicable(); 133 | uint256 totalSupply_ = totalSupply; 134 | uint256 rewardPerToken_ = _rewardPerToken(totalSupply_, lastTimeRewardApplicable_, rewardRate); 135 | 136 | /// ----------------------------------------------------------------------- 137 | /// State updates 138 | /// ----------------------------------------------------------------------- 139 | 140 | // accrue rewards 141 | rewardPerTokenStored = rewardPerToken_; 142 | lastUpdateTime = lastTimeRewardApplicable_; 143 | rewards[msg.sender] = _earned(msg.sender, accountBalance, rewardPerToken_, rewards[msg.sender]); 144 | userRewardPerTokenPaid[msg.sender] = rewardPerToken_; 145 | 146 | // stake 147 | totalSupply = totalSupply_ + amount; 148 | balanceOf[msg.sender] = accountBalance + amount; 149 | 150 | /// ----------------------------------------------------------------------- 151 | /// Effects 152 | /// ----------------------------------------------------------------------- 153 | 154 | stakeToken().safeTransferFrom(msg.sender, address(this), amount); 155 | 156 | emit Staked(msg.sender, amount); 157 | } 158 | 159 | /// @notice Withdraws staked tokens from the pool 160 | /// @param amount The amount of tokens to withdraw 161 | function withdraw(uint256 amount) external { 162 | /// ----------------------------------------------------------------------- 163 | /// Validation 164 | /// ----------------------------------------------------------------------- 165 | 166 | if (amount == 0) { 167 | return; 168 | } 169 | 170 | /// ----------------------------------------------------------------------- 171 | /// Storage loads 172 | /// ----------------------------------------------------------------------- 173 | 174 | uint256 accountBalance = balanceOf[msg.sender]; 175 | uint64 lastTimeRewardApplicable_ = lastTimeRewardApplicable(); 176 | uint256 totalSupply_ = totalSupply; 177 | uint256 rewardPerToken_ = _rewardPerToken(totalSupply_, lastTimeRewardApplicable_, rewardRate); 178 | 179 | /// ----------------------------------------------------------------------- 180 | /// State updates 181 | /// ----------------------------------------------------------------------- 182 | 183 | // accrue rewards 184 | rewardPerTokenStored = rewardPerToken_; 185 | lastUpdateTime = lastTimeRewardApplicable_; 186 | rewards[msg.sender] = _earned(msg.sender, accountBalance, rewardPerToken_, rewards[msg.sender]); 187 | userRewardPerTokenPaid[msg.sender] = rewardPerToken_; 188 | 189 | // withdraw stake 190 | balanceOf[msg.sender] = accountBalance - amount; 191 | // total supply has 1:1 relationship with staked amounts 192 | // so can't ever underflow 193 | unchecked { 194 | totalSupply = totalSupply_ - amount; 195 | } 196 | 197 | /// ----------------------------------------------------------------------- 198 | /// Effects 199 | /// ----------------------------------------------------------------------- 200 | 201 | stakeToken().safeTransfer(msg.sender, amount); 202 | 203 | emit Withdrawn(msg.sender, amount); 204 | } 205 | 206 | /// @notice Withdraws all staked tokens and earned rewards 207 | function exit() external { 208 | /// ----------------------------------------------------------------------- 209 | /// Validation 210 | /// ----------------------------------------------------------------------- 211 | 212 | uint256 accountBalance = balanceOf[msg.sender]; 213 | 214 | /// ----------------------------------------------------------------------- 215 | /// Storage loads 216 | /// ----------------------------------------------------------------------- 217 | 218 | uint64 lastTimeRewardApplicable_ = lastTimeRewardApplicable(); 219 | uint256 totalSupply_ = totalSupply; 220 | uint256 rewardPerToken_ = _rewardPerToken(totalSupply_, lastTimeRewardApplicable_, rewardRate); 221 | 222 | /// ----------------------------------------------------------------------- 223 | /// State updates 224 | /// ----------------------------------------------------------------------- 225 | 226 | // give rewards 227 | uint256 reward = _earned(msg.sender, accountBalance, rewardPerToken_, rewards[msg.sender]); 228 | if (reward > 0) { 229 | rewards[msg.sender] = 0; 230 | } 231 | 232 | // accrue rewards 233 | rewardPerTokenStored = rewardPerToken_; 234 | lastUpdateTime = lastTimeRewardApplicable_; 235 | userRewardPerTokenPaid[msg.sender] = rewardPerToken_; 236 | 237 | // withdraw stake 238 | balanceOf[msg.sender] = 0; 239 | // total supply has 1:1 relationship with staked amounts 240 | // so can't ever underflow 241 | unchecked { 242 | totalSupply = totalSupply_ - accountBalance; 243 | } 244 | 245 | /// ----------------------------------------------------------------------- 246 | /// Effects 247 | /// ----------------------------------------------------------------------- 248 | 249 | // transfer stake 250 | stakeToken().safeTransfer(msg.sender, accountBalance); 251 | emit Withdrawn(msg.sender, accountBalance); 252 | 253 | // transfer rewards 254 | if (reward > 0) { 255 | rewardToken().safeTransfer(msg.sender, reward); 256 | emit RewardPaid(msg.sender, reward); 257 | } 258 | } 259 | 260 | /// @notice Withdraws all earned rewards 261 | function getReward() external { 262 | /// ----------------------------------------------------------------------- 263 | /// Storage loads 264 | /// ----------------------------------------------------------------------- 265 | 266 | uint256 accountBalance = balanceOf[msg.sender]; 267 | uint64 lastTimeRewardApplicable_ = lastTimeRewardApplicable(); 268 | uint256 totalSupply_ = totalSupply; 269 | uint256 rewardPerToken_ = _rewardPerToken(totalSupply_, lastTimeRewardApplicable_, rewardRate); 270 | 271 | /// ----------------------------------------------------------------------- 272 | /// State updates 273 | /// ----------------------------------------------------------------------- 274 | 275 | uint256 reward = _earned(msg.sender, accountBalance, rewardPerToken_, rewards[msg.sender]); 276 | 277 | // accrue rewards 278 | rewardPerTokenStored = rewardPerToken_; 279 | lastUpdateTime = lastTimeRewardApplicable_; 280 | userRewardPerTokenPaid[msg.sender] = rewardPerToken_; 281 | 282 | // withdraw rewards 283 | if (reward > 0) { 284 | rewards[msg.sender] = 0; 285 | 286 | /// ----------------------------------------------------------------------- 287 | /// Effects 288 | /// ----------------------------------------------------------------------- 289 | 290 | rewardToken().safeTransfer(msg.sender, reward); 291 | emit RewardPaid(msg.sender, reward); 292 | } 293 | } 294 | 295 | /// ----------------------------------------------------------------------- 296 | /// Getters 297 | /// ----------------------------------------------------------------------- 298 | 299 | /// @notice The latest time at which stakers are earning rewards. 300 | function lastTimeRewardApplicable() public view returns (uint64) { 301 | return block.timestamp < periodFinish ? uint64(block.timestamp) : periodFinish; 302 | } 303 | 304 | /// @notice The amount of reward tokens each staked token has earned so far 305 | function rewardPerToken() external view returns (uint256) { 306 | return _rewardPerToken(totalSupply, lastTimeRewardApplicable(), rewardRate); 307 | } 308 | 309 | /// @notice The amount of reward tokens an account has accrued so far. Does not 310 | /// include already withdrawn rewards. 311 | function earned(address account) external view returns (uint256) { 312 | return _earned( 313 | account, 314 | balanceOf[account], 315 | _rewardPerToken(totalSupply, lastTimeRewardApplicable(), rewardRate), 316 | rewards[account] 317 | ); 318 | } 319 | 320 | /// ----------------------------------------------------------------------- 321 | /// Owner actions 322 | /// ----------------------------------------------------------------------- 323 | 324 | /// @notice Lets a reward distributor start a new reward period. The reward tokens must have already 325 | /// been transferred to this contract before calling this function. If it is called 326 | /// when a reward period is still active, a new reward period will begin from the time 327 | /// of calling this function, using the leftover rewards from the old reward period plus 328 | /// the newly sent rewards as the reward. 329 | /// @dev If the reward amount will cause an overflow when computing rewardPerToken, then 330 | /// this function will revert. 331 | /// @param reward The amount of reward tokens to use in the new reward period. 332 | function notifyRewardAmount(uint256 reward) external { 333 | /// ----------------------------------------------------------------------- 334 | /// Validation 335 | /// ----------------------------------------------------------------------- 336 | 337 | if (reward == 0) { 338 | return; 339 | } 340 | if (!isRewardDistributor[msg.sender]) { 341 | revert Error_NotRewardDistributor(); 342 | } 343 | 344 | /// ----------------------------------------------------------------------- 345 | /// Storage loads 346 | /// ----------------------------------------------------------------------- 347 | 348 | uint256 rewardRate_ = rewardRate; 349 | uint64 periodFinish_ = periodFinish; 350 | uint64 lastTimeRewardApplicable_ = block.timestamp < periodFinish_ ? uint64(block.timestamp) : periodFinish_; 351 | uint64 DURATION_ = DURATION(); 352 | uint256 totalSupply_ = totalSupply; 353 | 354 | /// ----------------------------------------------------------------------- 355 | /// State updates 356 | /// ----------------------------------------------------------------------- 357 | 358 | // accrue rewards 359 | rewardPerTokenStored = _rewardPerToken(totalSupply_, lastTimeRewardApplicable_, rewardRate_); 360 | lastUpdateTime = lastTimeRewardApplicable_; 361 | 362 | // record new reward 363 | uint256 newRewardRate; 364 | if (block.timestamp >= periodFinish_) { 365 | newRewardRate = reward / DURATION_; 366 | } else { 367 | uint256 remaining = periodFinish_ - block.timestamp; 368 | uint256 leftover = remaining * rewardRate_; 369 | newRewardRate = (reward + leftover) / DURATION_; 370 | } 371 | // prevent overflow when computing rewardPerToken 372 | if (newRewardRate >= ((type(uint256).max / PRECISION) / DURATION_)) { 373 | revert Error_AmountTooLarge(); 374 | } 375 | rewardRate = newRewardRate; 376 | lastUpdateTime = uint64(block.timestamp); 377 | periodFinish = uint64(block.timestamp + DURATION_); 378 | 379 | emit RewardAdded(reward); 380 | } 381 | 382 | /// @notice Lets the owner add/remove accounts from the list of reward distributors. 383 | /// Reward distributors can call notifyRewardAmount() 384 | /// @param rewardDistributor The account to add/remove 385 | /// @param isRewardDistributor_ True to add the account, false to remove the account 386 | function setRewardDistributor(address rewardDistributor, bool isRewardDistributor_) external onlyOwner { 387 | isRewardDistributor[rewardDistributor] = isRewardDistributor_; 388 | } 389 | 390 | /// ----------------------------------------------------------------------- 391 | /// Internal functions 392 | /// ----------------------------------------------------------------------- 393 | 394 | function _earned(address account, uint256 accountBalance, uint256 rewardPerToken_, uint256 accountRewards) 395 | internal 396 | view 397 | returns (uint256) 398 | { 399 | return FullMath.mulDiv(accountBalance, rewardPerToken_ - userRewardPerTokenPaid[account], PRECISION) 400 | + accountRewards; 401 | } 402 | 403 | function _rewardPerToken(uint256 totalSupply_, uint256 lastTimeRewardApplicable_, uint256 rewardRate_) 404 | internal 405 | view 406 | returns (uint256) 407 | { 408 | if (totalSupply_ == 0) { 409 | return rewardPerTokenStored; 410 | } 411 | return rewardPerTokenStored 412 | + FullMath.mulDiv((lastTimeRewardApplicable_ - lastUpdateTime) * PRECISION, rewardRate_, totalSupply_); 413 | } 414 | 415 | function _getImmutableVariablesOffset() internal pure returns (uint256 offset) { 416 | assembly { 417 | offset := sub(calldatasize(), add(shr(240, calldataload(sub(calldatasize(), 2))), 2)) 418 | } 419 | } 420 | } 421 | -------------------------------------------------------------------------------- /src/ERC721StakingPool.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0 2 | 3 | pragma solidity ^0.8.4; 4 | 5 | import {Clone} from "@clones/Clone.sol"; 6 | 7 | import {ERC20} from "solmate/tokens/ERC20.sol"; 8 | import {ERC721, ERC721TokenReceiver} from "solmate/tokens/ERC721.sol"; 9 | import {SafeTransferLib} from "solmate/utils/SafeTransferLib.sol"; 10 | 11 | import {Ownable} from "./lib/Ownable.sol"; 12 | import {FullMath} from "./lib/FullMath.sol"; 13 | 14 | /// @title ERC721StakingPool 15 | /// @author zefram.eth 16 | /// @notice A modern, gas optimized staking pool contract for rewarding ERC721 stakers 17 | /// with ERC20 tokens periodically and continuously 18 | contract ERC721StakingPool is Ownable, Clone, ERC721TokenReceiver { 19 | /// ----------------------------------------------------------------------- 20 | /// Library usage 21 | /// ----------------------------------------------------------------------- 22 | 23 | using SafeTransferLib for ERC20; 24 | 25 | /// ----------------------------------------------------------------------- 26 | /// Errors 27 | /// ----------------------------------------------------------------------- 28 | 29 | error Error_ZeroOwner(); 30 | error Error_AlreadyInitialized(); 31 | error Error_NotRewardDistributor(); 32 | error Error_AmountTooLarge(); 33 | error Error_NotTokenOwner(); 34 | error Error_NotStakeToken(); 35 | 36 | /// ----------------------------------------------------------------------- 37 | /// Events 38 | /// ----------------------------------------------------------------------- 39 | 40 | event RewardAdded(uint256 reward); 41 | event Staked(address indexed user, uint256[] idList); 42 | event Withdrawn(address indexed user, uint256[] idList); 43 | event RewardPaid(address indexed user, uint256 reward); 44 | 45 | /// ----------------------------------------------------------------------- 46 | /// Constants 47 | /// ----------------------------------------------------------------------- 48 | 49 | uint256 internal constant PRECISION = 1e30; 50 | address internal constant BURN_ADDRESS = address(0xdead); 51 | 52 | /// ----------------------------------------------------------------------- 53 | /// Storage variables 54 | /// ----------------------------------------------------------------------- 55 | 56 | /// @notice The last Unix timestamp (in seconds) when rewardPerTokenStored was updated 57 | uint64 public lastUpdateTime; 58 | /// @notice The Unix timestamp (in seconds) at which the current reward period ends 59 | uint64 public periodFinish; 60 | 61 | /// @notice The per-second rate at which rewardPerToken increases 62 | uint256 public rewardRate; 63 | /// @notice The last stored rewardPerToken value 64 | uint256 public rewardPerTokenStored; 65 | /// @notice The total tokens staked in the pool 66 | uint256 public totalSupply; 67 | 68 | /// @notice Tracks if an address can call notifyReward() 69 | mapping(address => bool) public isRewardDistributor; 70 | /// @notice The owner of a staked ERC721 token 71 | mapping(uint256 => address) public ownerOf; 72 | 73 | /// @notice The amount of tokens staked by an account 74 | mapping(address => uint256) public balanceOf; 75 | /// @notice The rewardPerToken value when an account last staked/withdrew/withdrew rewards 76 | mapping(address => uint256) public userRewardPerTokenPaid; 77 | /// @notice The earned() value when an account last staked/withdrew/withdrew rewards 78 | mapping(address => uint256) public rewards; 79 | 80 | /// ----------------------------------------------------------------------- 81 | /// Immutable parameters 82 | /// ----------------------------------------------------------------------- 83 | 84 | /// @notice The token being rewarded to stakers 85 | function rewardToken() public pure returns (ERC20 rewardToken_) { 86 | return ERC20(_getArgAddress(0)); 87 | } 88 | 89 | /// @notice The token being staked in the pool 90 | function stakeToken() public pure returns (ERC721 stakeToken_) { 91 | return ERC721(_getArgAddress(0x14)); 92 | } 93 | 94 | /// @notice The length of each reward period, in seconds 95 | function DURATION() public pure returns (uint64 DURATION_) { 96 | return _getArgUint64(0x28); 97 | } 98 | 99 | /// ----------------------------------------------------------------------- 100 | /// Initialization 101 | /// ----------------------------------------------------------------------- 102 | 103 | /// @notice Initializes the owner, called by StakingPoolFactory 104 | /// @param initialOwner The initial owner of the contract 105 | function initialize(address initialOwner) external { 106 | if (owner() != address(0)) { 107 | revert Error_AlreadyInitialized(); 108 | } 109 | if (initialOwner == address(0)) { 110 | revert Error_ZeroOwner(); 111 | } 112 | 113 | _transferOwnership(initialOwner); 114 | } 115 | 116 | /// ----------------------------------------------------------------------- 117 | /// User actions 118 | /// ----------------------------------------------------------------------- 119 | 120 | /// @notice Stakes a list of ERC721 tokens in the pool to earn rewards 121 | /// @param idList The list of ERC721 token IDs to stake 122 | function stake(uint256[] calldata idList) external { 123 | /// ----------------------------------------------------------------------- 124 | /// Validation 125 | /// ----------------------------------------------------------------------- 126 | 127 | if (idList.length == 0) { 128 | return; 129 | } 130 | 131 | /// ----------------------------------------------------------------------- 132 | /// Storage loads 133 | /// ----------------------------------------------------------------------- 134 | 135 | uint256 accountBalance = balanceOf[msg.sender]; 136 | uint64 lastTimeRewardApplicable_ = lastTimeRewardApplicable(); 137 | uint256 totalSupply_ = totalSupply; 138 | uint256 rewardPerToken_ = _rewardPerToken(totalSupply_, lastTimeRewardApplicable_, rewardRate); 139 | 140 | /// ----------------------------------------------------------------------- 141 | /// State updates 142 | /// ----------------------------------------------------------------------- 143 | 144 | // accrue rewards 145 | rewardPerTokenStored = rewardPerToken_; 146 | lastUpdateTime = lastTimeRewardApplicable_; 147 | rewards[msg.sender] = _earned(msg.sender, accountBalance, rewardPerToken_, rewards[msg.sender]); 148 | userRewardPerTokenPaid[msg.sender] = rewardPerToken_; 149 | 150 | // stake 151 | totalSupply = totalSupply_ + idList.length; 152 | balanceOf[msg.sender] = accountBalance + idList.length; 153 | unchecked { 154 | for (uint256 i = 0; i < idList.length; i++) { 155 | ownerOf[idList[i]] = msg.sender; 156 | } 157 | } 158 | 159 | /// ----------------------------------------------------------------------- 160 | /// Effects 161 | /// ----------------------------------------------------------------------- 162 | 163 | unchecked { 164 | for (uint256 i = 0; i < idList.length; i++) { 165 | stakeToken().safeTransferFrom(msg.sender, address(this), idList[i]); 166 | } 167 | } 168 | 169 | emit Staked(msg.sender, idList); 170 | } 171 | 172 | /// @notice Withdraws staked tokens from the pool 173 | /// @param idList The list of ERC721 token IDs to stake 174 | function withdraw(uint256[] calldata idList) external { 175 | /// ----------------------------------------------------------------------- 176 | /// Validation 177 | /// ----------------------------------------------------------------------- 178 | 179 | if (idList.length == 0) { 180 | return; 181 | } 182 | 183 | /// ----------------------------------------------------------------------- 184 | /// Storage loads 185 | /// ----------------------------------------------------------------------- 186 | 187 | uint256 accountBalance = balanceOf[msg.sender]; 188 | uint64 lastTimeRewardApplicable_ = lastTimeRewardApplicable(); 189 | uint256 totalSupply_ = totalSupply; 190 | uint256 rewardPerToken_ = _rewardPerToken(totalSupply_, lastTimeRewardApplicable_, rewardRate); 191 | 192 | /// ----------------------------------------------------------------------- 193 | /// State updates 194 | /// ----------------------------------------------------------------------- 195 | 196 | // accrue rewards 197 | rewardPerTokenStored = rewardPerToken_; 198 | lastUpdateTime = lastTimeRewardApplicable_; 199 | rewards[msg.sender] = _earned(msg.sender, accountBalance, rewardPerToken_, rewards[msg.sender]); 200 | userRewardPerTokenPaid[msg.sender] = rewardPerToken_; 201 | 202 | // withdraw stake 203 | balanceOf[msg.sender] = accountBalance - idList.length; 204 | // total supply has 1:1 relationship with staked amounts 205 | // so can't ever underflow 206 | unchecked { 207 | totalSupply = totalSupply_ - idList.length; 208 | for (uint256 i = 0; i < idList.length; i++) { 209 | // verify ownership 210 | address tokenOwner = ownerOf[idList[i]]; 211 | if (tokenOwner != msg.sender || tokenOwner == BURN_ADDRESS) { 212 | revert Error_NotTokenOwner(); 213 | } 214 | 215 | // keep the storage slot dirty to save gas 216 | // if someone else stakes the same token again 217 | ownerOf[idList[i]] = BURN_ADDRESS; 218 | } 219 | } 220 | 221 | /// ----------------------------------------------------------------------- 222 | /// Effects 223 | /// ----------------------------------------------------------------------- 224 | 225 | unchecked { 226 | for (uint256 i = 0; i < idList.length; i++) { 227 | stakeToken().safeTransferFrom(address(this), msg.sender, idList[i]); 228 | } 229 | } 230 | 231 | emit Withdrawn(msg.sender, idList); 232 | } 233 | 234 | /// @notice Withdraws specified staked tokens and earned rewards 235 | function exit(uint256[] calldata idList) external { 236 | /// ----------------------------------------------------------------------- 237 | /// Validation 238 | /// ----------------------------------------------------------------------- 239 | 240 | if (idList.length == 0) { 241 | return; 242 | } 243 | 244 | /// ----------------------------------------------------------------------- 245 | /// Storage loads 246 | /// ----------------------------------------------------------------------- 247 | 248 | uint256 accountBalance = balanceOf[msg.sender]; 249 | uint64 lastTimeRewardApplicable_ = lastTimeRewardApplicable(); 250 | uint256 totalSupply_ = totalSupply; 251 | uint256 rewardPerToken_ = _rewardPerToken(totalSupply_, lastTimeRewardApplicable_, rewardRate); 252 | 253 | /// ----------------------------------------------------------------------- 254 | /// State updates 255 | /// ----------------------------------------------------------------------- 256 | 257 | // give rewards 258 | uint256 reward = _earned(msg.sender, accountBalance, rewardPerToken_, rewards[msg.sender]); 259 | if (reward > 0) { 260 | rewards[msg.sender] = 0; 261 | } 262 | 263 | // accrue rewards 264 | rewardPerTokenStored = rewardPerToken_; 265 | lastUpdateTime = lastTimeRewardApplicable_; 266 | userRewardPerTokenPaid[msg.sender] = rewardPerToken_; 267 | 268 | // withdraw stake 269 | balanceOf[msg.sender] = accountBalance - idList.length; 270 | // total supply has 1:1 relationship with staked amounts 271 | // so can't ever underflow 272 | unchecked { 273 | totalSupply = totalSupply_ - idList.length; 274 | for (uint256 i = 0; i < idList.length; i++) { 275 | // verify ownership 276 | address tokenOwner = ownerOf[idList[i]]; 277 | if (tokenOwner != msg.sender || tokenOwner == BURN_ADDRESS) { 278 | revert Error_NotTokenOwner(); 279 | } 280 | 281 | // keep the storage slot dirty to save gas 282 | // if someone else stakes the same token again 283 | ownerOf[idList[i]] = BURN_ADDRESS; 284 | } 285 | } 286 | 287 | /// ----------------------------------------------------------------------- 288 | /// Effects 289 | /// ----------------------------------------------------------------------- 290 | 291 | // transfer stake 292 | unchecked { 293 | for (uint256 i = 0; i < idList.length; i++) { 294 | stakeToken().safeTransferFrom(address(this), msg.sender, idList[i]); 295 | } 296 | } 297 | emit Withdrawn(msg.sender, idList); 298 | 299 | // transfer rewards 300 | if (reward > 0) { 301 | rewardToken().safeTransfer(msg.sender, reward); 302 | emit RewardPaid(msg.sender, reward); 303 | } 304 | } 305 | 306 | /// @notice Withdraws all earned rewards 307 | function getReward() external { 308 | /// ----------------------------------------------------------------------- 309 | /// Storage loads 310 | /// ----------------------------------------------------------------------- 311 | 312 | uint256 accountBalance = balanceOf[msg.sender]; 313 | uint64 lastTimeRewardApplicable_ = lastTimeRewardApplicable(); 314 | uint256 totalSupply_ = totalSupply; 315 | uint256 rewardPerToken_ = _rewardPerToken(totalSupply_, lastTimeRewardApplicable_, rewardRate); 316 | 317 | /// ----------------------------------------------------------------------- 318 | /// State updates 319 | /// ----------------------------------------------------------------------- 320 | 321 | uint256 reward = _earned(msg.sender, accountBalance, rewardPerToken_, rewards[msg.sender]); 322 | 323 | // accrue rewards 324 | rewardPerTokenStored = rewardPerToken_; 325 | lastUpdateTime = lastTimeRewardApplicable_; 326 | userRewardPerTokenPaid[msg.sender] = rewardPerToken_; 327 | 328 | // withdraw rewards 329 | if (reward > 0) { 330 | rewards[msg.sender] = 0; 331 | 332 | /// ----------------------------------------------------------------------- 333 | /// Effects 334 | /// ----------------------------------------------------------------------- 335 | 336 | rewardToken().safeTransfer(msg.sender, reward); 337 | emit RewardPaid(msg.sender, reward); 338 | } 339 | } 340 | 341 | /// ----------------------------------------------------------------------- 342 | /// Getters 343 | /// ----------------------------------------------------------------------- 344 | 345 | /// @notice The latest time at which stakers are earning rewards. 346 | function lastTimeRewardApplicable() public view returns (uint64) { 347 | return block.timestamp < periodFinish ? uint64(block.timestamp) : periodFinish; 348 | } 349 | 350 | /// @notice The amount of reward tokens each staked token has earned so far 351 | function rewardPerToken() external view returns (uint256) { 352 | return _rewardPerToken(totalSupply, lastTimeRewardApplicable(), rewardRate); 353 | } 354 | 355 | /// @notice The amount of reward tokens an account has accrued so far. Does not 356 | /// include already withdrawn rewards. 357 | function earned(address account) external view returns (uint256) { 358 | return _earned( 359 | account, 360 | balanceOf[account], 361 | _rewardPerToken(totalSupply, lastTimeRewardApplicable(), rewardRate), 362 | rewards[account] 363 | ); 364 | } 365 | 366 | /// @dev ERC721 compliance 367 | function onERC721Received(address, address, uint256, bytes calldata) external view override returns (bytes4) { 368 | if (msg.sender != address(stakeToken())) { 369 | revert Error_NotStakeToken(); 370 | } 371 | return this.onERC721Received.selector; 372 | } 373 | 374 | /// ----------------------------------------------------------------------- 375 | /// Owner actions 376 | /// ----------------------------------------------------------------------- 377 | 378 | /// @notice Lets a reward distributor start a new reward period. The reward tokens must have already 379 | /// been transferred to this contract before calling this function. If it is called 380 | /// when a reward period is still active, a new reward period will begin from the time 381 | /// of calling this function, using the leftover rewards from the old reward period plus 382 | /// the newly sent rewards as the reward. 383 | /// @dev If the reward amount will cause an overflow when computing rewardPerToken, then 384 | /// this function will revert. 385 | /// @param reward The amount of reward tokens to use in the new reward period. 386 | function notifyRewardAmount(uint256 reward) external { 387 | /// ----------------------------------------------------------------------- 388 | /// Validation 389 | /// ----------------------------------------------------------------------- 390 | 391 | if (reward == 0) { 392 | return; 393 | } 394 | if (!isRewardDistributor[msg.sender]) { 395 | revert Error_NotRewardDistributor(); 396 | } 397 | 398 | /// ----------------------------------------------------------------------- 399 | /// Storage loads 400 | /// ----------------------------------------------------------------------- 401 | 402 | uint256 rewardRate_ = rewardRate; 403 | uint64 periodFinish_ = periodFinish; 404 | uint64 lastTimeRewardApplicable_ = block.timestamp < periodFinish_ ? uint64(block.timestamp) : periodFinish_; 405 | uint64 DURATION_ = DURATION(); 406 | uint256 totalSupply_ = totalSupply; 407 | 408 | /// ----------------------------------------------------------------------- 409 | /// State updates 410 | /// ----------------------------------------------------------------------- 411 | 412 | // accrue rewards 413 | rewardPerTokenStored = _rewardPerToken(totalSupply_, lastTimeRewardApplicable_, rewardRate_); 414 | lastUpdateTime = lastTimeRewardApplicable_; 415 | 416 | // record new reward 417 | uint256 newRewardRate; 418 | if (block.timestamp >= periodFinish_) { 419 | newRewardRate = reward / DURATION_; 420 | } else { 421 | uint256 remaining = periodFinish_ - block.timestamp; 422 | uint256 leftover = remaining * rewardRate_; 423 | newRewardRate = (reward + leftover) / DURATION_; 424 | } 425 | // prevent overflow when computing rewardPerToken 426 | if (newRewardRate >= ((type(uint256).max / PRECISION) / DURATION_)) { 427 | revert Error_AmountTooLarge(); 428 | } 429 | rewardRate = newRewardRate; 430 | lastUpdateTime = uint64(block.timestamp); 431 | periodFinish = uint64(block.timestamp + DURATION_); 432 | 433 | emit RewardAdded(reward); 434 | } 435 | 436 | /// @notice Lets the owner add/remove accounts from the list of reward distributors. 437 | /// Reward distributors can call notifyRewardAmount() 438 | /// @param rewardDistributor The account to add/remove 439 | /// @param isRewardDistributor_ True to add the account, false to remove the account 440 | function setRewardDistributor(address rewardDistributor, bool isRewardDistributor_) external onlyOwner { 441 | isRewardDistributor[rewardDistributor] = isRewardDistributor_; 442 | } 443 | 444 | /// ----------------------------------------------------------------------- 445 | /// Internal functions 446 | /// ----------------------------------------------------------------------- 447 | 448 | function _earned(address account, uint256 accountBalance, uint256 rewardPerToken_, uint256 accountRewards) 449 | internal 450 | view 451 | returns (uint256) 452 | { 453 | return FullMath.mulDiv(accountBalance, rewardPerToken_ - userRewardPerTokenPaid[account], PRECISION) 454 | + accountRewards; 455 | } 456 | 457 | function _rewardPerToken(uint256 totalSupply_, uint256 lastTimeRewardApplicable_, uint256 rewardRate_) 458 | internal 459 | view 460 | returns (uint256) 461 | { 462 | if (totalSupply_ == 0) { 463 | return rewardPerTokenStored; 464 | } 465 | return rewardPerTokenStored 466 | + FullMath.mulDiv((lastTimeRewardApplicable_ - lastUpdateTime) * PRECISION, rewardRate_, totalSupply_); 467 | } 468 | 469 | function _getImmutableVariablesOffset() internal pure returns (uint256 offset) { 470 | assembly { 471 | offset := sub(calldatasize(), add(shr(240, calldataload(sub(calldatasize(), 2))), 2)) 472 | } 473 | } 474 | } 475 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | --------------------------------------------------------------------------------