Reputation: 31
So, i am writing a smart contract on Solidity, and i thought my compiler was wrong or something, but i tried using Remix, Truffle and Hardhat, and them all give the same error, i don't know that i am doing wrong because i explicit declared the "Beneficiary" variable as payable, even in the constructor, can anyone help me?
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
//This contract uses a "Timelock" function, please note that if you actually use it, you CANNOT withdraw funds until the set date!!!!
contract Banking {
address public Beneficiary;
uint256 public givenTime;
constructor(address payable _Beneficiary, uint256 _givenTime) {
require(_givenTime > block.timestamp); //Make sure the time is in the future.
Beneficiary = _Beneficiary;
givenTime = _givenTime;
}
function Withdraw() public {
require(block.timestamp >= givenTime);
address(Beneficiary).transfer(address(this).balance);
}
}
Upvotes: 0
Views: 418
Reputation: 608
2 Things are wrong:
When you init Beneficiary you didn't init him as payable like this:
address public payable Beneficiary;
And in the Withdraw()
you should cast the address into payable like this:
payable(Beneficiary).transfer(address(this).balance);
Also it is not recommended to use transfer anymore because of gas restrictions, I recommend that you use call instead like this:
(bool success,) = payable(Beneficiary).call{value: address(this).balance}("");
require(success, "transaction failed");
Upvotes: 1