我正在尝试实现一个函数,该函数在另一个合同上调用不同的函数并返回该值。我怀疑这可能是在接收到先前的值之前调用承诺和函数的问题。但是,我不知道您如何在合同中实际实施这一点以及是否可能。我正在使用 Truffle、TestRPC 和 Solidity 编写合约,并从 truffle 终端调用函数。合约看起来像这样(仅显示最低限度的功能):
主合同:
// One (main) contract to control them all
import 'Company.sol';
contract Creator {
string[] public names;
address[] public companies;
// Creates a company
function createCompany(string _companyName, uint _noOfShares, uint _pricePerShare, address _creator) returns(address) {
address newCompany = new Company(_companyName, _noOfShares, _pricePerShare, _creator);
names.push(_companyName);
companies.push(newCompany);
return newCompany;
}
// Returns the name of a company given an index
function getName(uint i) constant returns(string companyName) {
return names[i];
}
// Returns the address of a company given an index
function getAddress(uint i) constant returns(address companyAddress) {
return companies[i];
}
// Returns the address of the last company created
function getLastAddress() constant returns(address companyAddress) {
return companies[companies.length - 1];
}
}
公司合同
import 'Shareholder.sol';
contract Company {
mapping(address => address) public shareholders; //Mapping from user
account address to shareholder contract address
function getShareNo(address _user) constant returns (uint _amount){
// Get the shareholder contract address for the user
address sellerContractAddr = getShareholder(_user);
Shareholder sellerContract = Shareholder(sellerContractAddr);
uint shares = sellerContract.getShareBalance();
return shares;
}
function getShareholder(address _user) constant returns (address shareholder){
return shareholders[_user];
}
}
股东合同:
contract Shareholder {
uint public shareBalance;
// Function to return the share balance
function getShareBalance() constant returns (uint balance){
return shareBalance;
}
}
编辑:在 Rob 的回复和更多测试之后,我意识到这与承诺无关,而是与 Creator 合同对 Company 合同的实例化有关。当直接从 truffle 部署时,Company.sol 的功能可以工作,但是当从 Creator 部署时,只有某些功能可以工作,不包括我提到的那些。我在我的构造函数 createCompany() 函数中看不到任何错误,如果有,请告诉我。