...by Daniel Szego
quote
"On a long enough timeline we will all become Satoshi Nakamoto.."
Daniel Szego

Thursday, December 21, 2017

Solidity and Truffle Tips and Tricks -msg.sender vs tx.origin


Ethereum transactions are initiated by a transaction and if there is multiply contracts involved, calling each contract us manifested by a message. So, having a transaction initiated by a user that calls the following contracts A -> B -> C -> D has one huge transaction and several internal messages: between A and B, between B and C, between C and D and between the initiator user and A.

tx.origin is always the user initiated the transaction itself,  msg.sender is the initiator of the message itself. In case of end user and contract A, the two elements are the same, but for example considering contract D, msg.sender is C and tx.origin is the user who initiated the transaction. 

Please note that using msg.sender is regarded as insecure and should be avoided.