Staking KEY
Overview
The StakingManager
contract provides the following functionality.
Addresses are able to "stake" KEY linked to a given Service Owner address and a serviceID 32-byte string. This allows to reserve different amount of tokens simultaneously in relation to different services .
Staked tokens can only be retrieved by the original sender.
Any Ethereum address is able to associate itself with multiple "serviceIDs" through setting up custom parameters such as minimum stake and lock-in period per serviceID. This is done through the methods
setServiceMinimumStake
andsetServiceStakePeriod
respectively.For the staking functionality to work, sender has to invoke
approve(stakingContractAddress, stakingAmount)
against on the token contract first, passing the staking contract address and the corresponding staking amount (including all decimal places). This is to allow the staking contract to spend funds (up to the limit set) on behalf of its owner. After token spending approval,StakingManager
methodstake
can be invoked by passing an amount, a serviceOnwer address and a serviceID. For "global" services not tied to any particular service owner, the "zero address" can be used as the serviceOwner (i.e.0x0000000000000000000000000000000000000000
).
If multiple stakes need to be done, sender can make one big approval that sums up to the desired total stake amount.
StakedAccess Contract Interface
All staking functionality is implemented by the StakingManager
contract, which includes the
following attributes:
Public State Variables
balances[address][serviceOwner][serviceID]
: A mapping that stores all stake balances for each sender address for each different service under a serviceOwner address.releaseDates[address][serviceOwner][serviceID]
: A mapping from addresses and bytes32 to a datetime in Unix format, representing the moment at which such stake can be released for a given service.stakePeriods[serviceOwner][serviceID]:
The minimum amount of days that each stake should be locked for before allowing token retrieval for a given service. Only the serviceOwner address can set this parameter.stakeMinimum[serviceOwner][serviceID]:
The minimum amount of tokens (including decimal places) that a sender can stake for a given service. Only the serviceOwner address can set this parameter.
Public Functions
stake(amount, serviceOwner, serviceID)
: On invoking thestake
function, anamount
of tokens is deducted from the sender address balance and added to the staking balance for a particularserviceID
under aserviceOwner
address. For the contract to be able to deduct tokens on behalf of the sender, sender must previously call the approve method of the token contract with at least the intendedamount
.withdraw(serviceOwner, serviceID)
: a stake sender can invoke thewithdraw
function by specifying aserviceOwner
andserviceID
, in which case the total stake for that service is sent back to the sender's wallet. If a minimum period has been set priorly to the stake action, such withdrawal can only be made if present moment is beyond what's set onreleaseDates
for that particular stake.hasStake(address, serviceOwner, serviceID)
: returns whether there's a stake made (greater than zero) by a sender on a particular service.hasStakeAboveMinimum(address, serviceID)
: returns whether there's a stake made (greater than the current minimum) by a sender on a particular service.setServiceStakePeriod(serviceID, amount)
(only service owner): Stake lock-in period can be changed for any arbitraryserviceID
under the caller's address. This does not affect stakes previously made.setServiceMinimumStake(serviceID, amount)
(only service owner): Minimum staking amount can be changed for any arbitraryserviceID
under the caller's address. This does not affect stakes previously made.
Events
KEYStaked(uint256 amount, address from, address serviceOwner, bytes32 serviceID)
: Emitted when an address has successfully staked an amount of KEY to a particular service.KEYStakeWithdrawn(uint256 amount, address from, address serviceOwner, bytes32 serviceID)
: Emitted when a KEY owner has withdrawn tokens previously staked for a certain serviceID.
Development
The smart contracts are being implemented in Solidity 0.4.23
.