7

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.

eth
  • 85,679
  • 53
  • 285
  • 406
Narayan Prusty
  • 1,229
  • 1
  • 11
  • 22

2 Answers2

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

dbryson
  • 6,403
  • 2
  • 27
  • 37
  • 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 https://ethereum.stackexchange.com/a/28977/23579 – Gleichmut Dec 22 '22 at 09: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)

niksmac
  • 9,673
  • 2
  • 40
  • 72