0

I have the following code:

// Test.sol
pragma solidity ^0.5.2;

import "./TestBase.sol";

contract Test is TestBase{
    string[] name ;

    constructor() public payable {
      name.push("sheep");
      name.push("fox");
      name.push("whale");
    }

    function getNames() public returns(string[] memory){
        emit Log("Test",address(this),msg.sender,tx.origin);
        display();
        return name;
    }
}

// TestBase.sol
pragma solidity ^0.5.2;

contract TestBase2 {
    event Log(string context,address,address,address);

    function display2() public returns(address){
        emit Log("TestBase2",address(this),msg.sender,tx.origin);
        return msg.sender;
    }

}

contract TestBase is TestBase2{

    function display() public returns(address){
        emit Log("TestBase",address(this),msg.sender,tx.origin);
        display2();
        TestBase3 t = new TestBase3();
        t.display();
        return msg.sender;
    }
}

contract TestBase3 {
    event Log(string context,address,address,address);

    function display() public returns(address){
        emit Log("TestBase3",address(this),msg.sender,tx.origin);

        return msg.sender;
    }
}

/// Remix output

Remix account(as seen in right side column of web page):0xca35b7d915458ef540ade6068dfe2f44e8fa733c

from    0xca35b7d915458ef540ade6068dfe2f44e8fa733c
 to     Test.getNames() 0x0dcd2f752394c41875e259e00bb44fd505297caf
  decoded output    {
    "0": "string[]: sheep,fox,whale"
}
 logs   [
    {
        "from": "0x0dcd2f752394c41875e259e00bb44fd505297caf",
        "topic": "0x93667a083beb23e2ac43ce23abd64b5a09999b532c00513483ed11dc07061e32",
        "event": "Log",
        "args": {
            "0": "Test",
            "1": "0x0DCd2F752394c41875e259e00bb44fd505297caF",
            "2": "0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c",
            "3": "0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c",
            "context": "Test",
            "length": 4
        }
    },
    {
        "from": "0x0dcd2f752394c41875e259e00bb44fd505297caf",
        "topic": "0x93667a083beb23e2ac43ce23abd64b5a09999b532c00513483ed11dc07061e32",
        "event": "Log",
        "args": {
            "0": "TestBase",
            "1": "0x0DCd2F752394c41875e259e00bb44fd505297caF",
            "2": "0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c",
            "3": "0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c",
            "context": "TestBase",
            "length": 4
        }
    },
    {
        "from": "0x0dcd2f752394c41875e259e00bb44fd505297caf",
        "topic": "0x93667a083beb23e2ac43ce23abd64b5a09999b532c00513483ed11dc07061e32",
        "event": "Log",
        "args": {
            "0": "TestBase2",
            "1": "0x0DCd2F752394c41875e259e00bb44fd505297caF",
            "2": "0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c",
            "3": "0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c",
            "context": "TestBase2",
            "length": 4
        }
    },
    {
        "from": "0x28dd7d6f41331e5013ee6c802641cc63b06c238a",
        "topic": "0x93667a083beb23e2ac43ce23abd64b5a09999b532c00513483ed11dc07061e32",
        "event": "Log",
        "args": {
            "0": "TestBase3",
            "1": "0x28Dd7d6f41331e5013eE6c802641cC63B06C238a",
            "2": "0x0DCd2F752394c41875e259e00bb44fd505297caF",
            "3": "0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c",
            "context": "TestBase3",
            "length": 4
        }
    }
]

Can you please define the terms: from, to (is it user, account or wallet address), topic. To my knowledge address(this) refers current contract's address. As Test inherits TestBase which inherits TestBase2, address(this) points to same address. I would like to know whose address is this i.e., is it Test, TestBase or TestBase2.

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
pebble
  • 153
  • 1
  • 10

1 Answers1

1

I tried to sort out the example code but ...

Can you please define the terms: from, to (is it user, account or wallet address), topic.

  • from: In the context of the event log, the contract that emitted the event
  • to: Not present. To whom it may concern ...
  • topic: Have a look at this: What are event topics?

"From" is an overloaded term and it might cause some confusion. In the context of a contract, msg.sender is the wallet or contract that invoked the function. In the context of a transaction in a block, it's the externally owned account that signed a transaction. address(this) is indeed the address of the contract context in which the function is running.

Hopefully this example disambiguates things:

pragma solidity 0.5.2;

contract SuperClassParent {

    event LogSender(address sender, address origin, address me);

    function lookWhosTalking() public returns(address) {
        emit LogSender(msg.sender, tx.origin, address(this));
        return msg.sender;
    }

}

contract SubClassChild is SuperClassParent {

    // function lookWhosTalking() ... will override the inherited function 

    // function lookWhosTalking(arg,arg) is a different function

    function rightHere() public returns(address) {
        emit LogSender(msg.sender, tx.origin, address(this));
        return msg.sender;
    }

}

Note that inheritance is a compile-time source code roll-up. In flattened form, it would look something like this:

contract SubClassChild {

    event LogSender(address sender, address origin, address me);

    function lookWhosTalking() public returns(address) {
        emit LogSender(msg.sender, tx.origin, address(this));
        return msg.sender;
    }

    // function lookWhosTalking() ... not allowed

    // function lookWhosTalking(arg,arg) is a different function

    function rightHere() public returns(address) {
        emit LogSender(msg.sender, tx.origin, address(this));
        return msg.sender;
    }

}

I would warn against using tx.origin for security reasons.

Hope it helps.

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145