Search code examples
blockchainethereumsoliditysmartcontractsremix

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

enter image description here


Solution

  • I think the only issue is you are not returning anything from getCount. You added the return signature, you should had a warning when you compiled it.

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

    enter image description here