A smart contract which allows users to stake and un-stake a specified ERC20 token. Staked tokens are locked for a specific length of time (set by the contrat owner at the outset). Once the time period has elapsed, the user can remove their tokens again.
-
Compile the SimpleStaking.sol contract.
-
Deploy the contract by passing in the official ERC20 token's contract address as the one-and-only constructor parameter.
-
Call the
setTimestamp
function (as the contract owner) by passing in a value (in seconds) for which you want the timelock period to be. The value which you pass in, will be added to the block.timestamp as at the time you called the setTimestamp function. For example, if you passed in 3600, then the timelock period would be 1 hour from now.
This approval step is actually performed by the DApp's User Interface (UI). The DApp will first check the approval
relationship of the Simple Staking Smart Contract's deployment address and the user (msg.sender
) and then present the user with an offer to approve a MetaMask transaction (in the event that the approve
function needs to be actioned before the DApp can successfully proceed with the task of transferring the suggested amount of tokens from the ERC20 contract to the Staking contract). Once this back and forth has occured, the DApp will know that it is possible to proceed and will get on with the staking (as outlined in the step following this one)
Go to the ERC20 contract using wallet software of a user who holds ERC20 tokens.
Perform the approve
function by passing in the Simple Staking Smart Contract's deployment address. Also add an amount of tokens which you would like to approve the Simple Staking Smart Contract to spend on the user's behalf.
Confirm the approval worked by calling the allowance
function (passing in the user's address and the Simple Staking Smart Contract's deployment address)
Go to the Simple Staking Smart Contract and perform the stakeTokens
function, by passing in the ERC20 contract's address and the amount of tokens you approved in the above step.
The tokens will be staked now.
You can check that the Simple Staking Smart Contract actually holds these tokens by checking the ERC20's balanceOf
function (by passing in the Simple Staking Smart Contract's address)
You can also check that your user's balance of ERC20 tokens has reduced by 250 (was 500 before)
Go ahead and call the Simple Staking Smart Contract's unstakeTokens
function by passing in the ERC20 contract's address and also the amount you wish to unstake. The msg.sender
is the recipient by default, so we don't need to pass your user's address in. This is a feature which only allows the msg.sender
to unlock their own tokens. Other users can not unstake other random user tokens.
You will now see that your user's ERC20 balance in their wallet has risen back from 250 to 500.
You will also see that the ERC20 token contract no longer shows the Simple Staking Smart Contract's address as a holder of the ERC20 token.
Finally, you will also notice that the Simple Staking Smart Contract's balance
for that specific user is back to 0