解决“end" and "transfer" are only available for objects of type "address payable", not "address".”

最近写只能合约过程中,使用transfer()函数给某地址转账,结果一直提示这个错误。测试源代码如下:

address public addr1 = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
function contrTransfer() public payable{
        // transfer函数由被转账方调用,注意把函数设置为payable
        addr1.transfer(8*10**18);
    }

结果IDE直接提示:
"send" and "transfer" are only available for objects of type "address payable", not "address".
原来没这问题啊?去看了下solidity的changelog才发现,address又又又被动过了。官网链接如下:

https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html

其中一段:
The address type was split into address and address payable, where only address payable provides the transfer function. An address payable can be directly converted to an address, but the other way around is not allowed. Converting address to address payable is possible via conversion through uint160. If c is a contract, address© results in address payable only if c has a payable fallback function. If you use the withdraw pattern, you most likely do not have to change your code because transfer is only used on msg.sender instead of stored addresses and msg.sender is an address payable.

大意就是address类型被分成了address和address payable,在我的代码中,我直接声明的addr1显然只是address,需要转化一下。新代码如下:

    function contrTransfer() public payable{
        // transfer函数由被转账方调用,注意把函数设置为payable
        //最新版solidity把address分为了payable address和address
        address(uint160(addr1)).transfer(8*10**18);
    }

终于不报错了,运行成功!等等,这样好像更麻烦了,这是图啥,把地址转来转去的,这不是有病吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值