调用另一个合约中的函数Solidity



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

类别.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>>>
}
}

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

最新更新