0

I have a question on staking of coins.

Suppose there are 10000 tokens in a user wallet, and he wants to put 5000 tokens on staking for a certain time period (i.e That 5000 tokens should go to an admin wallet and after the time period being completed, it will revert back in user account wallet).

Is this possible?

1

Sure, it is possible. User just needs to send 5000 tokens to admin, and after certain time period admin needs to send these tokens back.

I guess, you want this to be implemented in a trustless way. If so, admin should be a smart contract. Smart contract cannot do anything on its own, but may only react to incoming transaction, so user will probably need to initiate the refund himself. Here how this could work:

contract admin {
  IERC20 public token;

  struct StakeInfo {
    address user;
    uint amount;
    uint unlockTime;
  }

  StakeInfo [] public stakes;

  constructor (IERC20 _token) public {
    token = _token;
  }

  function putAtStake (uint _amount, uint _unlockTime)
  public returns (uint _stakeID){
    require (_unlockTime > block.timestamp);

    _stakeID = stakes.length;

    stakes.push (StakeInfo ({
      user: msg.sender,
      amount: _amount,
      unlockTime: _unlockTime
    })); 

    require (token.transferFrom (msg.sender, address (this), _amount));

    emit Stake (_stakeID, msg.sender, _amount, _unlockTime);
  }

  function refund (uint _stakeID) public {
    StakeInfo memory stake = stakes [_stakeID];

    require (stake.user != address (0));
    require (stake.unlockTime <= block.timestamp);

    delete stakes [_stakeID];

    require (token.transfer (stake.user, stake.amount));
  }

  event Stake (uint indexed id, address indexed user, uint amound, uint unlockTime);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.