8

How can a contract method find the address which invoked it?

Suppose I have a contract A with method B. I used xyz address to call method B then how can method B see xyz address.

2 Answers 2

11

Use the msg.sender variable. It's automatically available in the contract:

function hello() {
  address from = msg.sender;
}

More information in the solidity docs

1
  • 1
    msg.sender would only return address of the direct caller, but if this caller a proxy contract it would not return an initial caller. The best suggestion so far is to to pass initial caller as a parameter ethereum.stackexchange.com/a/28977/23579
    – Gleichmut
    Commented Dec 22, 2022 at 9:40
3

Inside the contract you can use msg.sender to get the address of the caller. See Block and Transaction Properties for the list of global variables.

msg.sender (address): sender of the message (current call)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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