背景:
两个合约间完成转账,并且在一个合约中调用另一个合约的函数
通过call()分别进行有函数名的含参调用或者callback匿名函数的回调
源代码:
pragma solidity ^0.4.0;
contract AddrTest{
event logdata(bytes data);
//callback func
function() payable{
logdata(msg.data);
}
function getBalance() returns (uint){
return this.balance;
}
uint score = 0;
function setScore(uint s) public{
score = s;
}
function getScore() returns (uint){
return score;
}
}
contract CallTest{
//Use this function to send wei
function deposit() payable{
}
//Send some wei to AddrTest
event logSendEvent(address to, uint value);
function transferEther(address towho) payable{
towho.transfer(10);
logSendEvent(towho, 10);
}
//Call AddrTest.callback
//Check the logs to see the data
function callNoFunc(address addr) returns (bool){
return addr.call("boss-li", 1234);
}
//Point to setScore in AddrTest
function callfunc(address addr) returns (bool){
bytes4 methodId = bytes4(keccak256("setScore(uint256)"));
return addr.call(methodId, 100);
}
function getBalance() returns (uint){
return this.balance;
}
}