1

I have been following Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial (https://www.youtube.com/watch?v=M576WGiDBdQ&t=28658s). instead of copying a ton of code, I'm trying to generalize my question as follows:

consider the following code snippet:

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

I understand that interface.IWeth(SomeAddress) tells Ethereum virtual machine to create an instance of the contract at SomeAddress ( which I will call SomeContract) with the functionalities of the interface.

I would like to confirm the following:

Does weth and SomeContract share the same address?

will the following state changes have the same outcome?

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

and

SomeContract.deposit({"from":account, "value": 0.01*10**18})
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
maskara
  • 329
  • 1
  • 2
  • 12

3 Answers3

1

Let's say you have contractB and contractA. Inside contractB you want to interact with contractA but you do not know the code of contractA. All you know is interface and address of the contractA. In this case, you interact with contractA inside contractB via the interface, and any change you make to contractA will be reflected inside contractA in blockchain.

In your question when you call weth.deposit you are actually modifying the state of contract at given address.

You could test this easily on Remix.

Let's say you have contractA and interface of it:

    // 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++ ;
       }
    }

You compile it and then deploy it. (Make sure when you deploy it, select the contractA not interfaceA). Get the address of deployment. Then create contractB:

    // 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();
        }
    }

Now when you deploy this, since I wrote a constructor with an argument, you need to pass the address of cotractA. Then call addToIncrement() of contractB. This will call the increment function of contractA. Now go to contractA and call count and you will get updated value.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • @Yilmaz Internal what will be happening in this code segment **InterfaceA a = InterfaceA(address);**, maybe internal **contractA extend InterfaceA** is it working this way inside it? – Ng Sharma Apr 27 '22 at 06:29
0

I'm a newbie on Solidity and it's not clear to me if the address of contractA is the transaction hash parameter (first bytes) that is specified on Remix console when contractA is deployed or it is the account address from which I deployed contractA. This query is because I'm getting an error when I try to call addToIncrement function from contractB, so I guess it is not connecting with contractA.

Transaction hash contractA

Error while calling addToIncrement function on contractB

Nisase
  • 1
  • 1
  • I'm a newbie myself but I don't think the address of ContractA is the tx hash as you mentioned. the address of ContractA will show up on the left pane below "Deployed Contracts" and would read something line CONTRACTA AT 0X1D1.... Tha'ts where the ContractA got deployed to. you will need to copy that address and use it while deploying ContractB. Hope this helps – maskara Feb 14 '22 at 21:35
  • @maskara, you're right! --> "On the left you see CONTRACTA 0x1d1.... that is the address. at the end of that line u will see a a button to copy yhe address " – Nisase Feb 16 '22 at 20:35
0

An interface is basically used to supply the ABI to interact with the blockchain. To interact with contract A, contract B actually needs the ABI code and contract address of Contract A. I assume under-the-hood solidity compiler used the interface of Contract A (IA) to communicate deployed Contract A to generate the ABI for contract A.