// File: openzeppelin-solidity/contracts/token/ERC20/ERC20Basic.sol
pragma solidity ^0.4.23;
/**
* @title ERC20Basic
* @dev Simpler version of ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/179
*/
contract ERC20Basic {
function totalSupply() public view returns (uint256);
function balanceOf(address who) public view returns (uint256);
function transfer(address to, uint256 value) public returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
}
// File: openzeppelin-solidity/contracts/token/ERC20/ERC20.sol
pragma solidity ^0.4.23;
/**
* @title ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/20
*/
contract ERC20 is ERC20Basic {
function allowance(address owner, address spender)
public view returns (uint256);
function transferFrom(address from, address to, uint256 value)
public returns (bool);
function approve(address spender, uint256 value) public returns (bool);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
// File: openzeppelin-solidity/contracts/math/SafeMath.sol
pragma solidity ^0.4.23;
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
c = a * b;
assert(c / a == b);
return c;
}
/**
* @dev Integer division of two numbers, truncating the quotient.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
// uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return a / b;
}
/**
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
assert(c >= a);
return c;
}
}
// File: contracts/ERC900/ERC900.sol
pragma solidity ^0.4.24;
/**
* @title ERC900 Simple Staking Interface
* @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-900.md
*/
contract ERC900 {
event Staked(address indexed user, uint256 amount, uint256 total, bytes data);
event Unstaked(address indexed user, uint256 amount, uint256 total, bytes data);
function stake(uint256 amount, bytes data) public;
function stakeFor(address user, uint256 amount, bytes data) public;
function unstake(uint256 amount, bytes data) public;
function totalStakedFor(address addr) public view returns (uint256);
function totalStaked() public view returns (uint256);
function token() public view returns (address);
function supportsHistory() public pure returns (bool);
// NOTE: Not implementing the optional functions
// function lastStakedFor(address addr) public view returns (uint256);
// function totalStakedForAt(address addr, uint256 blockNumber) public view returns (uint256);
// function totalStakedAt(uint256 blockNumber) public view returns (uint256);
}
// File: contracts/ERC900/ERC900BasicStakeContract.sol
/* solium-disable security/no-block-members */
pragma solidity ^0.4.24;
/**
* @title ERC900 Simple Staking Interface basic implementation
* @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-900.md
*/
contract ERC900BasicStakeContract is ERC900 {
// @TODO: deploy this separately so we don't have to deploy it multiple times for each contract
using SafeMath for uint256;
// Token used for staking
ERC20 stakingToken;
// The default duration of stake lock-in (in seconds)
uint256 public defaultLockInDuration;
// To save on gas, rather than create a separate mapping for totalStakedFor & personalStakes,
// both data structures are stored in a single mapping for a given addresses.
//
// It's possible to have a non-existing personalStakes, but have tokens in totalStakedFor
// if other users are staking on behalf of a given address.
mapping (address => StakeContract) public stakeHolders;
// Struct for personal stakes (i.e., stakes made by this address)
// unlockedTimestamp - when the stake unlocks (in seconds since Unix epoch)
// actualAmount - the amount of tokens in the stake
// stakedFor - the address the stake was staked for
struct Stake {
uint256 unlockedTimestamp;
uint256 actualAmount;
address stakedFor;
}
// Struct for all stake metadata at a particular address
// totalStakedFor - the number of tokens staked for this address
// personalStakeIndex - the index in the personalStakes array.
// personalStakes - append only array of stakes made by this address
// exists - whether or not there are stakes that involve this address
struct StakeContract {
uint256 totalStakedFor;
uint256 personalStakeIndex;
Stake[] personalStakes;
bool exists;
}
/**
* @dev Modifier that checks that this contract can transfer tokens from the
* balance in the stakingToken contract for the given address.
* @dev This modifier also transfers the tokens.
* @param _address address to transfer tokens from
* @param _amount uint256 the number of tokens
*/
modifier canStake(address _address, uint256 _amount) {
require(
stakingToken.transferFrom(_address, this, _amount),
"Stake required");
_;
}
/**
* @dev Constructor function
* @param _stakingToken ERC20 The address of the token contract used for staking
*/
constructor(ERC20 _stakingToken) public {
stakingToken = _stakingToken;
}
/**
* @dev Returns the timestamps for when active personal stakes for an address will unlock
* @dev These accessors functions are needed until https://github.com/ethereum/web3.js/issues/1241 is solved
* @param _address address that created the stakes
* @return uint256[] array of timestamps
*/
function getPersonalStakeUnlockedTimestamps(address _address) external view returns (uint256[]) {
uint256[] memory timestamps;
(timestamps,,) = getPersonalStakes(_address);
return timestamps;
}
/**
* @dev Returns the stake actualAmount for active personal stakes for an address
* @dev These accessors functions are needed until https://github.com/ethereum/web3.js/issues/1241 is solved
* @param _address address that created the stakes
* @return uint256[] array of actualAmounts
*/
function getPersonalStakeActualAmounts(address _address) external view returns (uint256[]) {
uint256[] memory actualAmounts;
(,actualAmounts,) = getPersonalStakes(_address);
return actualAmounts;
}
/**
* @dev Returns the addresses that each personal stake was created for by an address
* @dev These accessors functions are needed until https://github.com/ethereum/web3.js/issues/1241 is solved
* @param _address address that created the stakes
* @return address[] array of amounts
*/
function getPersonalStakeForAddresses(address _address) external view returns (address[]) {
address[] memory stakedFor;
(,,stakedFor) = getPersonalStakes(_address);
return stakedFor;
}
/**
* @notice Stakes a certain amount of tokens, this MUST transfer the given amount from the user
* @notice MUST trigger Staked event
* @param _amount uint256 the amount of tokens to stake
* @param _data bytes optional data to include in the Stake event
*/
function stake(uint256 _amount, bytes _data) public {
createStake(
msg.sender,
_amount,
defaultLockInDuration,
_data);
}
/**
* @notice Stakes a certain amount of tokens, this MUST transfer the given amount from the caller
* @notice MUST trigger Staked event
* @param _user address the address the tokens are staked for
* @param _amount uint256 the amount of tokens to stake
* @param _data bytes optional data to include in the Stake event
*/
function stakeFor(address _user, uint256 _amount, bytes _data) public {
createStake(
_user,
_amount,
defaultLockInDuration,
_data);
}
/**
* @notice Unstakes a certain amount of tokens, this SHOULD return the given amount of tokens to the user, if unstaking is currently not possible the function MUST revert
* @notice MUST trigger Unstaked event
* @dev Unstaking tokens is an atomic operation—either all of the tokens in a stake, or none of the tokens.
* @dev Users can only unstake a single stake at a time, it is must be their oldest active stake. Upon releasing that stake, the tokens will be
* transferred back to their account, and their personalStakeIndex will increment to the next active stake.
* @param _amount uint256 the amount of tokens to unstake
* @param _data bytes optional data to include in the Unstake event
*/
function unstake(uint256 _amount, bytes _data) public {
withdrawStake(
_amount,
_data);
}
/**
* @notice Returns the current total of tokens staked for an address
* @param _address address The address to query
* @return uint256 The number of tokens staked for the given address
*/
function totalStakedFor(address _address) public view returns (uint256) {
return stakeHolders[_address].totalStakedFor;
}
/**
* @notice Returns the current total of tokens staked
* @return uint256 The number of tokens staked in the contract
*/
function totalStaked() public view returns (uint256) {
return stakingToken.balanceOf(this);
}
/**
* @notice Address of the token being used by the staking interface
* @return address The address of the ERC20 token used for staking
*/
function token() public view returns (address) {
return stakingToken;
}
/**
* @notice MUST return true if the optional history functions are implemented, otherwise false
* @dev Since we don't implement the optional interface, this always returns false
* @return bool Whether or not the optional history functions are implemented
*/
function supportsHistory() public pure returns (bool) {
return false;
}
/**
* @dev Helper function to get specific properties of all of the personal stakes created by an address
* @param _address address The address to query
* @return (uint256[], uint256[], address[])
* timestamps array, actualAmounts array, stakedFor array
*/
function getPersonalStakes(
address _address
)
public
view
returns(uint256[], uint256[], address[])
{
StakeContract storage stakeContract = stakeHolders[_address];
uint256 arraySize = stakeContract.personalStakes.length - stakeContract.personalStakeIndex;
uint256[] memory unlockedTimestamps = new uint256[](arraySize);
uint256[] memory actualAmounts = new uint256[](arraySize);
address[] memory stakedFor = new address[](arraySize);
for (uint256 i = stakeContract.personalStakeIndex; i < stakeContract.personalStakes.length; i++) {
uint256 index = i - stakeContract.personalStakeIndex;
unlockedTimestamps[index] = stakeContract.personalStakes[i].unlockedTimestamp;
actualAmounts[index] = stakeContract.personalStakes[i].actualAmount;
stakedFor[index] = stakeContract.personalStakes[i].stakedFor;
}
return (
unlockedTimestamps,
actualAmounts,
stakedFor
);
}
/**
* @dev Helper function to create stakes for a given address
* @param _address address The address the stake is being created for
* @param _amount uint256 The number of tokens being staked
* @param _lockInDuration uint256 The duration to lock the tokens for
* @param _data bytes optional data to include in the Stake event
*/
function createStake(
address _address,
uint256 _amount,
uint256 _lockInDuration,
bytes _data
)
internal
canStake(msg.sender, _amount)
{
require(
_amount > 0,
"Stake amount has to be greater than 0!");
if (!stakeHolders[msg.sender].exists) {
stakeHolders[msg.sender].exists = true;
}
stakeHolders[_address].totalStakedFor = stakeHolders[_address].totalStakedFor.add(_amount);
stakeHolders[msg.sender].personalStakes.push(
Stake(
block.timestamp.add(_lockInDuration),
_amount,
_address)
);
emit Staked(
_address,
_amount,
totalStakedFor(_address),
_data);
}
/**
* @dev Helper function to withdraw stakes for the msg.sender
* @param _amount uint256 The amount to withdraw. MUST match the stake amount for the
* stake at personalStakeIndex.
* @param _data bytes optional data to include in the Unstake event
*/
function withdrawStake(
uint256 _amount,
bytes _data
)
internal
{
Stake storage personalStake = stakeHolders[msg.sender].personalStakes[stakeHolders[msg.sender].personalStakeIndex];
// Check that the current stake has unlocked & matches the unstake amount
require(
personalStake.unlockedTimestamp <= block.timestamp,
"The current stake hasn't unlocked yet");
require(
personalStake.actualAmount == _amount,
"The unstake amount does not match the current stake");
// Transfer the staked tokens from this contract back to the sender
// Notice that we are using transfer instead of transferFrom here, so
// no approval is needed beforehand.
require(
stakingToken.transfer(msg.sender, _amount),
"Unable to withdraw stake");
stakeHolders[personalStake.stakedFor].totalStakedFor = stakeHolders[personalStake.stakedFor]
.totalStakedFor.sub(personalStake.actualAmount);
personalStake.actualAmount = 0;
stakeHolders[msg.sender].personalStakeIndex++;
emit Unstaked(
personalStake.stakedFor,
_amount,
totalStakedFor(personalStake.stakedFor),
_data);
}
}
// File: contracts/ERC900/BasicStakingContract.sol
pragma solidity ^0.4.24;
/**
* @title BasicStakingContract
*/
contract BasicStakingContract is ERC900BasicStakeContract {
/**
* @dev Constructor function
* @param _stakingToken ERC20 The address of the token used for staking
* @param _lockInDuration uint256 The duration (in seconds) that stakes are required to be locked for
*/
constructor(
ERC20 _stakingToken,
uint256 _lockInDuration
)
public
ERC900BasicStakeContract(_stakingToken)
{
defaultLockInDuration = _lockInDuration;
}
}
{
"compilationTarget": {
"BasicStakingContract.sol": "BasicStakingContract"
},
"evmVersion": "byzantium",
"libraries": {},
"optimizer": {
"enabled": true,
"runs": 999999
},
"remappings": []
}
[{"constant":false,"inputs":[{"name":"_amount","type":"uint256"},{"name":"_data","type":"bytes"}],"name":"stake","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_user","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_data","type":"bytes"}],"name":"stakeFor","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_address","type":"address"}],"name":"getPersonalStakeUnlockedTimestamps","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_address","type":"address"}],"name":"getPersonalStakes","outputs":[{"name":"","type":"uint256[]"},{"name":"","type":"uint256[]"},{"name":"","type":"address[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_address","type":"address"}],"name":"totalStakedFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_address","type":"address"}],"name":"getPersonalStakeActualAmounts","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"supportsHistory","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"name":"_address","type":"address"}],"name":"getPersonalStakeForAddresses","outputs":[{"name":"","type":"address[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalStaked","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_amount","type":"uint256"},{"name":"_data","type":"bytes"}],"name":"unstake","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"stakeHolders","outputs":[{"name":"totalStakedFor","type":"uint256"},{"name":"personalStakeIndex","type":"uint256"},{"name":"exists","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"defaultLockInDuration","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"token","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_stakingToken","type":"address"},{"name":"_lockInDuration","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"total","type":"uint256"},{"indexed":false,"name":"data","type":"bytes"}],"name":"Staked","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"total","type":"uint256"},{"indexed":false,"name":"data","type":"bytes"}],"name":"Unstaked","type":"event"}]