Solidity Transaction Hash – How to Get Transaction Hash from a Function Call

soliditytransactions

Within a Solidity function, is the transaction hash available as a global variable from within the contract? I would have thought it would be something like tx.hash
in the same way you can do tx.origin etc.

My thinking is that the transaction hash will not be available until the contract has been mined. Is this correct?

Best Answer

Here you have all the global variables in Solidity:

Global Variables

  • block.coinbase (address): current block miner’s address
  • block.difficulty (uint): current block difficulty
  • block.gaslimit (uint): current block gaslimit
  • block.number (uint): current block number
  • block.blockhash (function(uint) returns (bytes32)): hash of the given block - only works for 256 most recent blocks
  • block.timestamp (uint): current block timestamp
  • msg.data (bytes): complete calldata
  • msg.gas (uint): remaining gas
  • msg.sender (address): sender of the message (current call)
  • msg.value (uint): number of wei sent with the message
  • now (uint): current block timestamp (alias for block.timestamp)
  • tx.gasprice (uint): gas price of the transaction
  • tx.origin (address): sender of the transaction (full call chain)
  • keccak256(...) returns (bytes32): compute the KECCAK256 hash of the (tightly packed) arguments
  • sha256(...) returns (bytes32): compute the SHA256 hash of the (tightly packed) arguments
  • ripemd160(...) returns (bytes20): compute RIPEMD of 256 the (tightly packed) arguments
  • ecrecover(bytes32, uint8, bytes32, bytes32) returns (address): recover public key from elliptic curve signature
  • addmod(uint x, uint y, uint k) returns (uint): compute (x + y) % k where the addition is performed with arbitrary precision and does not wrap around at 2**256.
  • mulmod(uint x, uint y, uint k) returns (uint): compute (x * y) % k where the multiplication is performed with arbitrary precision and does not wrap around at 2**256.
  • this (current contract’s type): the current contract, explicitly convertible to address
  • super: the contract one level higher in the inheritance hierarchy
  • selfdestruct(address): destroy the current contract, sending its funds to the given address
  • <address>.balance: balance of the address in Wei
  • <address>.send(uint256) returns (bool): send given amount of Wei to address, returns false on failure.

As you say, as the transaction is not mined you can't find here the tx.hash.