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
4920 0
|
3月前
|
API
【API Management】使用 APIM Inbound Policy 来修改Content‐Type Header的值
【API Management】使用 APIM Inbound Policy 来修改Content‐Type Header的值
【API Management】使用 APIM Inbound Policy 来修改Content‐Type Header的值
|
6月前
|
消息中间件 Java
connection error;reply-code=503;unknown exchange type ‘x-delayed-message‘
connection error;reply-code=503;unknown exchange type ‘x-delayed-message‘
107 0
Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC
Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC
|
应用服务中间件
Error:Remote staging type or host is not specified.
Error:Remote staging type or host is not specified.
155 0
|
程序员 Go API
|
Web App开发 .NET
解决.Net 4.0 A potentially dangerous Request.Form value was detected from the client 异常
解决ASP.NET 4.0   "A potentially dangerous Request.Form value was detected from the client". 错误在.net中,Request时出现有HTML或Javascript等字符串时,系统会认为是危险性值。
1148 0
|
安全 关系型数据库 网络安全