0

Im trying to create a flow where contract A holds an address and all calls from contract B and contract C need to pass a simple msg.sender == systemAddress check.

All the while I need to be able to use any data in contract B within Contract C including the functions.

Here is a basic structure - not working, just trying to convey what I want to achieve. Any help would be greatly appreciated.

pragma solidity ^0.8.0;

contract A {

    address systemAddress = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;

    constructor(){}

    function fromSystem(address from) public view {
        require(
            from == systemAddress,
            "Invalid transaction source"
        );
    }
}

contract B {

    A a;

    uint[] data;

    constructor(address contractA){
        a = A(contractA);
        data.push(10);
    }

    modifier fromSystem() {
        a.fromSystem(msg.sender);
        _;
    }

    function funcB() public fromSystem returns (uint256){
        // do something
        return data[0];
    }
}

contract C {

    A a;
    B b;

    constructor(address contractA, address contractB){
        a = A(contractB);
        b = B(contractB);
    }

    modifier fromSystem() {
        a.fromSystem(msg.sender);
        _;
    }

    function funcC() public fromSystem {
        uint num = b.funcB();
        // expecting 10
    }
}

1 Answer 1

0

Solidity supports multiple inheritance. You can use Use is to derive from another contract. check out solidity docs for more information on inheritance. here how your example code will look like

pragma solidity ^0.8.0;

contract A {

address systemAddress = 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2;

constructor(){
    
}

function funcA(address from) public view {
    require(
        from == systemAddress,
        "Invalid transaction source"
    );
}

}

contract B is A {

uint[] data;

constructor(){
   
    data.push(10);
}

modifier fromSystem() {
 require(
        msg.sender == systemAddress,
        "Only systemAddress can call this."
    );
    _;
}

function funcB() public view fromSystem returns (uint256){
    // do something
    return data[0];
}

}

contract C is A, B{

constructor(){
   
}

function funcC() view public fromSystem returns (uint256) {
    uint num = B.funcB();
    return num;
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.