'Solidity Type "send" and "transfer" are only available for objects of type "address payable" using a payable address
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);
}
}
Solution 1:[1]
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");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kuly14 |
