Skip to content

Instantly share code, notes, and snippets.

@k06a
Created October 5, 2019 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k06a/f09e7c6c9319738a180e96d7bc8c18ce to your computer and use it in GitHub Desktop.
Save k06a/f09e7c6c9319738a180e96d7bc8c18ce to your computer and use it in GitHub Desktop.
Loopring Pool
pragma solidity ^0.5.0;
import "github.com/openzeppelin/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
/*
*
* staker1: |===========|
* : :
* staker2: : |===========|
* : : : :
* staker3: : : |===========|
* : : : : : :
* --|---|---|---|---|---|--> (time)
*
* Add new stake:
* share = stake_duration * stake_amount;
* totalShares = totalShares + share;
*
* Remove stake:
* totalShares = totalShares - share;
*
*/
contract PeriodsPool is ERC20 {
struct Stake {
uint64 at;
uint192 amount;
}
uint256 public totalReward;
mapping(address => Stake) public stakes;
function reward(address account) public view returns(uint256) {
return totalReward.mul(balanceOf(account)).div(totalSupply());
}
function _stake(address account, uint256 amount, uint256 duration) internal {
require(stakes[account].at == 0, "Stake exist");
_mint(account, amount.mul(duration));
stakes[account] = Stake({
at: uint64(now.add(duration)),
amount: uint192(amount)
});
}
function _claim(address account) internal {
Stake memory stake = stakes[account];
require(now >= stake.at, "Wait until staking finished");
_burn(account, balanceOf(account));
}
// Make stakes non-transferable
function _transfer(address /*sender*/, address /*recipient*/, uint256 /*amount*/) internal {
revert();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment