2
pragma solidity ^0.6.6;

import "./newERC20Interface.sol";  //ERC20 interface

  contract timeLock2 {
      mapping(address => uint256) _balances;
      event Transfer(address, uint256);

      newERC20Interface token;
      address receiver = 0xAdB2cbbAb6764643ec15789F8428d5cD9510342b;
      uint256 amountToSend = 100;
      uint256 relaseTime = 1589180400;

      function releseToken() public {
          require(msg.sender == receiver);
          require(now >= relaseTime);

          msg.sender.transfer(amountToSend);
          emit Transfer(msg.sender, amountToSend);

      }
      function balanceOf(address account) public view returns(uint256){
         return _balances[account];
      }
  }

how to lock 100 tokens for next 30 minutes and send automatically

10
  • What does "send automatically" mean? May 11, 2020 at 9:46
  • means when the tokens are lock for certain time then how will the tokens will be transferred? when the condition meets the tokens will send automatically, see the code please
    – sultan
    May 11, 2020 at 10:53
  • Contract code is executed only explicitly from the off-chain. In order to perform something automatically as a result of a time-related trigger (or any other type of trigger for that matter), you have to set up an off-chain service (e.g., web-server, cron-job, etc). May 11, 2020 at 10:55
  • if i deploy this contract on ropsten testnet and then call timelock function, what will happen then
    – sultan
    May 11, 2020 at 11:11
  • 1
    You wrote this function and you're asking what will happen??? It will verify msg.sender == receiver, then verify now >= relaseTime, then transfer amountToSend wei to the caller and then emit an event. May 11, 2020 at 11:25

1 Answer 1

1

There is no way built-in Solidity to trigger an event automatically based in a timer (cron-job like execution). All function executions must be triggered by a wallet signing a transaction to run that function.

A work-around to do what you want is either making a back-end, storing a private-key in it and then setting a cron-job in this back-end to trigger the function.

The other way around is using Chainlink Keepers: https://docs.chain.link/docs/chainlink-keepers/introduction/

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.