solidity中transfer异常"send" and "transfer" are only available for objects of type address

简介: solidity中transfer异常"send" and "transfer" are only available for objects of type address

在solidity只能合约中,正常情况下我们可以使用transfer方法来进行转账操作,具体使用如下:


msg.sender.transfer(_value);


这样,就可以将指定的金额转到msg.sender的账户内。但是,还有一些情况时,ETH的接收者并不是msg.sender。那么该如何处理呢?使用下面这一种方式?


function transfer(address _to, uint256 _value) public {

       _to.transfer(_value);

       return;

}


如果真的使用此方法,你会发现在编译时会报如下异常:


browser/***: TypeError: "send" and "transfer" are only available for objects of type "address payable", not "address".

_to.transfer(_value);


针对此异常需要将地址转换为payable的。可通过两种形式,如果该地址是作为属性设置好的,比如通过set方法进行设置的,那么在定义该属性时需要定义为如下形式:


address payable receiver;


如果是像上面的示例方法中一样,是传递进来的参数,则可使用如下方式解决:


function transfer(address _to, uint256 _value) public {

       address(uint160(_to)).transfer(_value);

       return;

}


目录
相关文章
Rejected request from RFC1918 IP to public server address
Rejected request from RFC1918 IP to public server address
4944 0
Flutter之运行提示Could not update files on device: Connection closed before full header was received
Flutter之运行提示Could not update files on device: Connection closed before full header was received
779 0
|
8月前
|
消息中间件 Java
connection error;reply-code=503;unknown exchange type ‘x-delayed-message‘
connection error;reply-code=503;unknown exchange type ‘x-delayed-message‘
139 0
|
Python
Python POST data should be bytes, an iterable of bytes, or a file object. It ...
Python POST data should be bytes, an iterable of bytes, or a file object. It ...
125 0
|
应用服务中间件
Error:Remote staging type or host is not specified.
Error:Remote staging type or host is not specified.
183 0
|
程序员 Go API
|
开发工具 git
git 报错 RPC failed; curl 18 transfer closed with outstanding read data remaining
这个错误是因为项目太久,tag资源文件太大 找了很久网上都说这样: 配置如下: $ git  config --global http.
9756 0
|
缓存 开发工具 git
Git报错Error:RPC failed; curl 18 transfer closed with outstanding read data remaining
Git报错Error:RPC failed; curl 18 transfer closed with outstanding read data remaining
474 0