3

I am attempting to create a smart contract counter. Can I use the data returned by web3.eth.getTransaction() to determine if the transaction created a smart contract?

Julian Martinez
  • 409
  • 5
  • 14

1 Answers1

4

The information you're looking for is available by calling web3.eth.getTransactionReceipt. The returned dictionary includes a contractAddress key, if this value is not None the transaction was a contract deployment.

An example taken from the web3.py documentation:

>>> web3.eth.getTransactionReceipt('0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060')  # not yet mined
None
# wait for it to be mined....
>>> web3.eth.getTransactionReceipt('0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060')
AttributeDict({
    'blockHash': '0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd',
    'blockNumber': 46147,
    'contractAddress': None,
    'cumulativeGasUsed': 21000,
    'from': '0xA1E4380A3B1f749673E270229993eE55F35663b4',
    'gasUsed': 21000,
    'logs': [],
    'root': '96a8e009d2b88b1483e6941e6812e32263b05683fac202abc622a3e31aed1957',
    'to': '0x5DF9B87991262F6BA471F09758CDE1c0FC1De734',
    'transactionHash': '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060',
    'transactionIndex': 0,
})
iamdefinitelyahuman
  • 2,876
  • 1
  • 12
  • 32
  • 3
    Upvoted as this answers for contract creation transactions.. But OP should also be aware that a transaction can for example, call a function on a contract, which then creates a new contract: there is no API to detect these general cases. – eth Oct 23 '19 at 02:24