0

I have a time locked wallet code which withdraws the fund after some period:

// SPDX-License-Identifier: Unlicensed
pragma solidity ^0.8.0;

import '@openzeppelin/contracts/token/ERC20/ERC20.sol';

contract TimeLockedWallet {

    address public creator;
    address public owner;
    uint256 public unlockDate;
    uint256 public createdAt;

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    event Received(address from, uint256 amount);
    event Withdrew(address to, uint256 amount);
    event WithdrewTokens(address tokenContract, address to, uint256 amount);
    
    constructor(
        address _creator,
        address _owner,
        uint256 _unlockDate
    ) public {
        creator = _creator;
        owner = _owner;
        unlockDate = _unlockDate;
        createdAt = block.timestamp;
    }

    // keep all the ether sent to this address
    receive() external payable {
        Received(msg.sender, msg.value);
    }

    // callable by owner only, after specified time
    function withdraw() onlyOwner public {
       require(block.timestamp >= unlockDate, "reverted here");
       //now send all the balance
       payable(msg.sender).transfer(address(this).balance);
       emit Withdrew(msg.sender, address(this).balance);
    }

    // callable by owner only, after specified time, only for Tokens implementing ERC20
    function withdrawTokens(address _tokenContract) onlyOwner public {
       require(block.timestamp >= unlockDate);
       ERC20 token = ERC20(_tokenContract);
       //now send all the token balance
       uint256 tokenBalance = token.balanceOf(address(this));
       token.transfer(owner, tokenBalance);
       emit WithdrewTokens(_tokenContract, msg.sender, tokenBalance);
    }

    function info() public view returns(address, address, uint256, uint256, uint256) {
        return (creator, owner, unlockDate, createdAt, address(this).balance);
    }

}

When I try to test the code above in JavaScript, it reverts:

let timelockWallet = new TimeLockedWallet(ownerWallet[0]);
await timelockWallet.send(ethToSend, {from: owner});
await timelockWallet.withdraw();

Does anyone know what's wrong with my code? Thanks!

2
  • What version of Solidity are you using? May 27 at 8:16
  • Hi Paul, it's 0.8.0
    – user73476
    May 27 at 8:24
1

Not definitively, but onlyOwner and require are the usual suspects. You should add reasons to require(expression, "this is why it reverted"); statements to help with debugging.

I can't quite make out what Solidity version that is or what Withdrew is. It looks like an event emitter without emit. If it compiles, that sort of dates the compiler. It might be necessary to add a fallback or a receive function for the send() stage to work.

You should consider posting a complete example so people can replicate the error.

Hope it helps.

1
  • Hi Rob, thanks for your suggestion. I have edited my post with full script. it's 0.8.0 version and I tested witout onlyOwner and add the comment. It's still reverted.
    – user73476
    May 27 at 8:23

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.