在另一个合约中调用函数 - Solidity

Call a function in another contract - Solidity

我需要使用 Truffle 在另一个合约中调用一个函数。这是我的样本合同:

Category.sol:

contract Category {
  /// ...
  /// @notice Check if category exists
  function isCategoryExists(uint256 index) external view returns (bool) {
    if (categories[index].isExist) {
      return true;
    }
    return false;
  }
}

Post.sol:

contract Post {
  /// ...
  /// @notice Create a post
  function createPost(PostInputStruct memory _input)
    external
    onlyValidInput(_input)
    returns (bool)
  {
    /// NEED TO CHECK IF CATEGORY EXISTS
    /// isCategoryExists() <<<from Category.sol>>>
  }
}

Deploy.js

const Category = artifacts.require("Category");
const Post = artifacts.require("Post");

module.exports = function (deployer) {
  deployer.deploy(Category);
  deployer.deploy(Post);
};

我能做什么?

您可以继承其他合约。假设您想从 Post 合同中导入。

contract Category is Post {
  /// ...
  /// @notice Check if category exists
  function isCategoryExists(uint256 index) external view returns (bool) {
    if (categories[index].isExist) {
      return true;
    }
    return false;
  }
  // you can call createPost
  createPost(){}
}