合约之间的简单调用之interface

2022-10-09  本文已影响0人  哦名字好难

废话不多说 直接上代码

注意点 必须是public的可以让其他合约进行访问 并且做好权限控制


// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract heroCardData{

    uint256 public  limit = 100;

    string public users = "qwertyuiop"; 

    mapping (address=>address) public userList;

    function getUsers() public view returns (string memory name){

        return users;

    }

    function setLimit(uint256 _limit) public {

        limit = _limit;

    }

    function changeValue(uint256 orderId) public pure returns (uint256) {

        return ++orderId;

    }

}

在使用合约中写入 注意点方法名要一致


interface IA {

    function limit() external view returns (uint256);

    function getUsers() external view returns (string memory);

    function changeValue(uint256 orderId) external pure returns (uint256);

}

在正式使用合约中 传入部署的heroCardData合约地址


contract myTest{

    IA datesss;

    constructor(address dataAddress){

        datesss = IA(dataAddress);

    }

    function getLimit() public view returns (uint256) {

        return datesss.limit();

    }

    function getUserList() public view returns (string memory) {

        return datesss.getUsers();

    }

    function changes(uint256 orders) public view returns (uint256) {

        return datesss.changeValue(orders);

    }

}

上一篇下一篇

猜你喜欢

热点阅读