簡體   English   中英

松露固體-從不同合約調用功能

[英]Truffle Solidity - Calling function from different contract

我正在嘗試實現一個函數,該函數在另一個合約上調用另一個函數並返回該值。 我懷疑這可能是一個問題,在收到前一個的值之前調用了promise和函數。 但是我不知道您如何在合同中實際執行此操作以及是否可行。 我正在使用Truffle,TestRPC和Solidity編寫合同,並從松露終端調用函數。 合同如下所示(僅顯示了最低限度的功能):

主合同:

// 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的回應和更多測試之后,我意識到這與承諾無關,而是與創建者合同實例化了Company合同有關。 直接從松露部署時,Company.sol的功能起作用,但是從Creator部署時,只有某些功能起作用,但我提到的功能除外。 我在構造函數createCompany()函數中看不到任何錯誤,如果您這樣做,請告訴我。

可能您只發布了部分代碼?

沒有要設置owner [user]的內容,因此映射無法返回除0x0(映射地址的默認值)以外的任何內容。

作為偽解決方案,您需要在Company中遵循這些方針。 在此版本中,股東不存在,因此可以在公司中創建和跟蹤。

function newShareholder() returns (address shareholderContract) {
    Shareholder shareholder  new Shareholder(); // new contract
    shareholder[msg.sender] = shareholder; // store addr with user
    return shareholder;
}

我懷疑為每個用戶創建合同會使事情復雜化,但這是您要考慮的整個用例的問題。

智能合約之間沒有約定和回調。 合同間消息是(大致)即時的。

希望能幫助到你。

經過大量的試驗和錯誤,我終於解決了這個問題,這與松露的愚蠢方式(如果我是對的)有關。

基本上,每次我更新合同文件並重新編譯時,其他合同(例如主合同)都不會使用新信息進行更新,因此在實例化合同時,這些功能將在每次調用時失敗。 如果我在未修改的文件中添加了新的空行或其他內容並重新保存,則編譯時一切正常。

松露開發人員,確保修復這些錯誤,我花了兩周的時間才弄清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM