调用另一个合同中的函数--可靠 [英] Call a function in another contract - Solidity

查看:15
本文介绍了调用另一个合同中的函数--可靠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用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);
};

我能做些什么?

推荐答案

您可以继承其他合同。假设您要从后期合同导入。

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(){}
}

这篇关于调用另一个合同中的函数--可靠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆