简体   繁体   中英

Figuring out a solidity Interface issue

I got an example of a solidity Interface.

1/ Any clue if this method is accurate as it's implementing the Interface within the inherited from Contract and not within the extension Contract.

2/ I tried implementing it, contractA functions run correctly, however contractB getCount function is not running correctly.

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

// SPDX-License-Identifier: MIT

import './contractA.sol' ;

pragma solidity ^0.8.0;

contract contractB {

address addressA;

function setPointer(address _addressA) external {
    addressA = _addressA;
}

function getCount() external view returns (uint256) {
    InterfaceA b = InterfaceA(addressA);
    b.count();
}

function addToIncrement() external {
    InterfaceA b = InterfaceA(addressA);
    b.increment();
}
}

在此处输入图像描述

I do not think that you can get the address of contractA, inside contractB like this:

      address addressA;

you should add a getter function inside contractA to return the address of contractA

contractA {
   function getAddress() public returns(address){
      return address(this)
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM