address.transfer(uint256 value)
:给 address 转账 value(Wei),且调用异常会抛出。
address.call
,address.delegatecall
:智能合约相互调用时使用,两者区别是,A调用B函数,call方法结果展示到B中,delegatecall方法结果展示到A中。
1、合约代码
pragma solidity ^0.4.0;
contract CA{
uint public p;
event e(address add,uint p);
function fun(uint u1,uint u2)public{
p = u1 + u2;
emit e(msg.sender,p);
}
}
contract CB{
uint public q;
bool public b;
function CB() public payable {
}
function call1(address add)public returns(bool) {
b = add.call(bytes4(keccak256("fun(uint256,uint256)")),2,3);
return b;
}
function call2(address add)public returns(bool){
b = add.delegatecall(bytes4(keccak256("fun(uint256,uint256)")),1,2);
return b;
}
function sendDemo(address add) public{
uint u = 1 ether;
add.transfer(u);
}
}
2、部署CA和CB合约,CB合约部署时存入100以太币
3、调用CB合约的send Dome方法,向主账户发送1个以太币
4、发送成功,CB合约以太币变为99
5、调用CB合约call1方法,调用CA合约的fun方法
6、CA合约fun方法执行成功,P等于5
7、调用CB方法的call2方法,通过delegatecall调用CA的fun方法
8、CB合约的B等于YES,Q等于3,调用成功