简体   繁体   English

松露固体-从不同合约调用功能

[英]Truffle Solidity - Calling function from different contract

I am trying to implement a function which calls a different function on another contract and returns that value. 我正在尝试实现一个函数,该函数在另一个合约上调用另一个函数并返回该值。 I have suspicion that this could be a problem with promises and functions being called before the value of the previous ones are received. 我怀疑这可能是一个问题,在收到前一个的值之前调用了promise和函数。 However I have no idea how you could actually implement this in the contract and if it is possible. 但是我不知道您如何在合同中实际执行此操作以及是否可行。 I am using Truffle, TestRPC and Solidity for writing the contract, and calling the functions from the truffle terminal. 我正在使用Truffle,TestRPC和Solidity编写合同,并从松露终端调用函数。 The contracts look like these (showing just the bare minimum functions): 合同如下所示(仅显示了最低限度的功能):

Main Contract: 主合同:

// 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];
    }

}

Company Contract 公司合同

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];
    }

}

Shareholder Contract: 股东合同:

contract Shareholder {

   uint public shareBalance; 

    // Function to return the share balance
    function getShareBalance() constant returns (uint balance){
      return shareBalance;
    }
}

EDIT: After Rob's response and more testing, I realize this does not have anything to do with promises but with the instantiation of the Company contract by the Creator contract. 编辑:在Rob的回应和更多测试之后,我意识到这与承诺无关,而是与创建者合同实例化了Company合同有关。 When deployed directly from truffle, the functions of Company.sol work, but when deployed from Creator only certain ones work, excluding the ones I mentioned. 直接从松露部署时,Company.sol的功能起作用,但是从Creator部署时,只有某些功能起作用,但我提到的功能除外。 I can't see anything wrong in my constructor createCompany() function, please let me know if you do. 我在构造函数createCompany()函数中看不到任何错误,如果您这样做,请告诉我。

Possibly you only posted part of the code? 可能您只发布了部分代码?

There is nothing to set shareholder[user], so no way the mapping can return anything except 0x0 which is the default value of a mapped address. 没有要设置owner [user]的内容,因此映射无法返回除0x0(映射地址的默认值)以外的任何内容。

As a pseudo solution, you need something along these lines in Company. 作为伪解决方案,您需要在Company中遵循这些方针。 In this version, Shareholder doesn't exist, so it could created and tracked in/by Company. 在此版本中,股东不存在,因此可以在公司中创建和跟踪。

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

I suspect the creation of a contract for each user is over-complicating matters, but this is a matter of the whole use-case you have in mind. 我怀疑为每个用户创建合同会使事情复杂化,但这是您要考虑的整个用例的问题。

Promises and callbacks are not in play between Smart Contracts. 智能合约之间没有约定和回调。 The inter-contract messages are (roughly) instantaneous. 合同间消息是(大致)即时的。

Hope it helps. 希望能帮助到你。

After a lot of trial and error I have finally fixed the issue, and it has to do with the ridiculously stupid way truffle works (if I'm correct). 经过大量的试验和错误,我终于解决了这个问题,这与松露的愚蠢方式(如果我是对的)有关。

Basically every time I made an update to a contract file and recompiled,the other contracts (eg. the Main one) wouldn't update with the new information so when instantiating the contract those functions would fail on every call. 基本上,每次我更新合同文件并重新编译时,其他合同(例如主合同)都不会使用新信息进行更新,因此在实例化合同时,这些功能将在每次调用时失败。 If I added a new empty line or something to the untouched file and re-saved it, then everything worked when compiling. 如果我在未修改的文件中添加了新的空行或其他内容并重新保存,则编译时一切正常。

Truffle developers, make sure to fix these bugs, it took me two weeks of my life to figure it out. 松露开发人员,确保修复这些错误,我花了两周的时间才弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动态调用外部合同 - Calling external contract in solidity dynamically 测试我的 Solidity 智能合约时遇到问题(带松露) - Having a Problem Testing my Solidity Smart Contract (w/ Truffle) 具有提升权限的 Solidity 调用合约 - Solidity calling contract with elevated permissions 在另一个合约中调用 function - Solidity - Call a function in another contract - Solidity Solidity:从 Marketplace 合约调用 NFT 合约的一个函数,只有 Marketplace 有权调用它 - Solidity: call a function of NFT contract from Marketplace contract, and only Marketplace has access to call it web3上调用solidity合约function时如何添加ETH作为参数 - How to add ETH as parameter when calling solidity contract function on web3 可靠性:调用另一个合约的函数时出错。 错误:发送值时应向构造函数付款 - Solidity: Error when calling a function of another contract. Error: The constructor should be payable when you send value 松露在过去没有调用该函数 - Truffle not calling the function in past time 如何在ganache / truffle / web3中解锁合同地址,以便我可以使用from作为调用函数? - How to unlock a contract address in ganache/truffle/web3 so that I can use it as from to call a function? Solidity 和 truffle 的编译器问题 - compiler problem with solidity and truffle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM