【问题标题】:How to connect my smart contract with another deployed smart contract?如何将我的智能合约与另一个部署的智能合约连接起来?
【发布时间】:2021-10-15 18:30:59
【问题描述】:

Assalamualaikum,

我是区块链新手。所以我正在考虑部署一个智能合约作为rest api,并在我的另一个智能合约中使用它。 是否可以? 我知道 oracle 有助于获取数据,但它可以帮助交互两个已部署的合同吗? 提前致谢。

【问题讨论】:

    标签: blockchain ethereum solidity smartcontracts chainlink


    【解决方案1】:

    您可以在源合约中定义目标合约的接口。示例:

    TargetContract,部署在0x123 地址:

    pragma solidity ^0.8;
    
    contract TargetContract {
        function foo() external pure returns (bool) {
            return true;
        }
    }
    

    SourceContract,指向0x123TargetContract

    pragma solidity ^0.8;
    
    interface ITargetContract {
        function foo() external returns (bool);
    }
    
    contract SourceContract {
        function baz() external {
            ITargetContract targetContract = ITargetContract(address(0x123));
            bool returnedValue = targetContract.foo();
        }
    }
    

    【讨论】:

    • 非常感谢您的回答。它对我有用。但我很困惑,这是一个好习惯吗?我们是否应该使用来自多个 dapp 的已部署智能合约,就像我们在多个中心化应用中使用 api 一样?
    • 这是一种非常普遍的做法。例如:目标合约是已建立的 DEX,源合约允许通过 DEX 购买特定的代币……有点像 DEX 发布 API,源合约调用 API。
    猜你喜欢
    • 2022-07-25
    • 1970-01-01
    • 2022-12-06
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2022-12-18
    • 2021-11-15
    相关资源
    最近更新 更多