最近写只能合约过程中,使用transfer()函数给某地址转账,结果一直提示这个错误。测试源代码如下:
address public addr1 = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
function contrTransfer() public payable{
// transfer函数由被转账方调用,注意把函数设置为payable
addr1.transfer(8*10**18);
}
结果IDE直接提示:
原来没这问题啊?去看了下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);
}
终于不报错了,运行成功!等等,这样好像更麻烦了,这是图啥,把地址转来转去的,这不是有病吗?