通过接口创建的协定实例是否与原始部署的协定具有相同的地址? [英] Does a contract instance created via interfaces have the same address as the original deployed contract?

查看:15
本文介绍了通过接口创建的协定实例是否与原始部署的协定具有相同的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习坚固、区块链和智能合同课程--初级到专业的Python教程(https://www.youtube.com/watch?v=M576WGiDBdQ&t=28658s)。与其复制一大堆代码,我试着将我的问题概括如下:

考虑以下代码片段:

weth=interface.IWeth(SomeAddress)
tx=weth.deposit({"from":account, "value": 0.01*10**18})

我知道interface.IWeth(SomeAddress)通知以太虚拟机在SomeAddress(我将其称为SomeContract)使用接口的功能创建协定的实例。

我想确认以下几点:

wethSomeContract是否共享相同的地址?

下列状态更改是否会产生相同的结果?

weth.deposit({"from":account, "value": 0.01*10**18})

SomeContract.deposit({"from":account, "value": 0.01*10**18})

推荐答案

假设您有contractBcontractA。在contractB中,您想要与contractA交互,但您不知道协定的代码。你所知道的只是合同的接口和地址。在这种情况下,您通过接口在ContB内部与Contta交互,您对Contta所做的任何更改都将反映在区块链中的Contta内部。

在您的问题中,当您呼叫weth.deposit时,您实际上是在修改给定地址的合同状态。

您可以在Remix上轻松进行测试。

假设您有契约及其接口:

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    
    interface InterfaceA {
        function count() external view returns (uint256);
        function increment() external;
    }
    
    contract contractA {
        uint256 number = 0;
    
       function count() external view returns (uint256) {
          return number;
       }
    
       function increment() external {
          number++ ;
       }
    }
您编译它,然后部署它。(确保在部署它时,选择Contta Not InterfaceA)。获取部署地址。然后创建合同B:

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    
    import './contractA.sol' ;
    
    contract contractB {    
        address addressA;   
        constructor (address _addressA) {
              addressA = _addressA;
        }
    
        function getCount() external view returns (uint256) {
             InterfaceA b = InterfaceA(addressA);
             return b.count();
        }
    
        function addToIncrement() external {
             InterfaceA b = InterfaceA(addressA);
             b.increment();
        }
    }
现在当您部署它时,因为我编写了一个带参数的构造函数,所以您需要传递cotractA的地址。然后调用contractBaddToIncrement()。这将调用contractAincrement函数。现在转到contractA并调用count,您将获得更新值。

这篇关于通过接口创建的协定实例是否与原始部署的协定具有相同的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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