0

As we all know that an external account can send a transaction to smart contract. But I'm confused when it comes to how contracts talk with each other.

Contracts that exist within the global scope of Ethereum’s state can talk to other contracts within that same scope. The way they do this is via “messages” or “internal transactions” to other contracts. We can think of messages or internal transactions as being similar to transactions, with the major difference that they are NOT generated by externally owned accounts. Instead, they are generated by contracts. They are virtual objects that, unlike transactions, are not serialized and only exist in the Ethereum execution environment.1

I fully understand what it mean in the quote. But my question is HOW ? Are there some examples of smart contracts in Solidity show how contracts communicate with each other via "message"?

  • Whenever a smart contract calls a function in another smart contract, it does so via a 'message'. – Jesbus Nov 30 '18 at 22:16
  • To add to Jess answer, I would say that the fact that contracts communicate via "message" is just a low-level detail from the point of view of a smart contract developer. – Julien Klepatch Dec 2 '18 at 13:03
1

Here's a simple Solidity example:

contract One {
    function callme() external {
        // ...
    }
}

contract Two {
    // This function accepts as a parameter the address for a deployed
    // instance of the One contract.
    function callOne(One one) external {
        // call the function callme() on our instance of One
        one.callme();
    }
}
| improve this answer | |
1

A smart contract is identified with an address on ethereum blockchain. So, you can call it from another by referencing its address.

As far as I know, there is a way to do this:

contract CalledContract {
    function doSomething() public {
    }
}

contract CallerContract {
    function callAnotherContract(address addr) external {
        CalledContract called = CalledContract(addr);
        called.doSomething();
    }
}

Remember that your called method/function should be public, otherwise you can not call it. If it has external visibility, it can be called only from outside the EVM.

| improve this answer | |

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.