1
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract Timelock {
  uint public constant duration = 365 days;
  uint public immutable end;
  address payable public immutable owner;

  constructor(address payable _owner) {
    end = block.timestamp + duration;
    owner = _owner; 
  }

  function deposit(address token, uint amount) external {
    IERC20(token).transferFrom(msg.sender, address(this), amount);
  }

  receive() external payable {}

  function withdraw(address token, uint amount) external {
    require(msg.sender == owner, 'only owner');
    require(block.timestamp >= end, 'too early');
    if(token == address(0)) { 
      owner.transfer(amount);
    } else {
      IERC20(token).transfer(owner, amount);
    }
  }
}

This is my Milk Contract, which I deployed before: https://testnet.bscscan.com/token/0x8384b0ad2044e3b4c43c89c425d3ba4830b16f36

If I want to call the deposit function of Contract TimeLock Metamask says: We were not able to estimate gas. There might be an error in the contract and this transaction may fail. I put the contract address of my deployed Milk Contract and the amount in the function deposit to write them via bscscan. I set also as address payable my Metamask address as owner from which I deployed the contracts, is this correct? or is it the address of milk contract? This is my migrations js file:

//const Migrations = artifacts.require("Migrations");
const Timelock = artifacts.require("Timelock");
module.exports = function (deployer) {
 // deployer.deploy(Migrations);
  deployer.deploy(Timelock,"0xec125D397cdA00f7390c1C6E76d4ca9902357fd3");
};

I think the problem is the IERC20 in the deposit function. Why do I need to import IERC20.sol and do I need them? Why I cannot write the function and why Metamask cannot estimade Gas? Any help would be appreciated. below the failed txhash: https://testnet.bscscan.com/tx/0x9ff191b4725781dc7174c58bb94fdfc61d4bbc484b04b21f61216ff822acda8f

1 Answer 1

1

The transaction reverted because you forgot to approve the TimeLock contract (address 0x91492C12C43a5dd018e32EA6b66178eCC71e22BF) to use your Milk token: Milk.allowance(0xec125D39..your_address, 0x91492C..TimeLock_address) = 0

To do it you need to call approve(timelock_address, amount) with the account that you will call deposit. E.g. to make your failed transaction working, you need to call this on Milk token contract:

approve(0x91492C12C43a5dd018e32EA6b66178eCC71e22BF, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)

You can do it using the WriteContract button on BsCscan of Milk Token

2
  • thank you very much, you are king. How can I withdraw the token again. Metamask says now again cannot esitmad gas. Do I need at withdraw again to call approve?
    – Hakikat41
    Commented Jan 2, 2023 at 22:09
  • According to the code of timelock, to withdraw you have to wait 365 days. end = block.timestamp + duration;, it's the time lock. And you must call withdraw with the same address that you used to deploy this contract: require(msg.sender == owner, 'only owner');.
    – minhhn2910
    Commented Jan 3, 2023 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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