Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TransactionReceipt.timestamp #504

Merged
merged 3 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions brownie/network/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class TransactionReceipt:
gas_used: Gas used
input: Hexstring input data
nonce: Transaction nonce
block_number: Block number this transaction was included in
timestamp: Timestamp of the block this transaction was included in
txindex: Index of the transaction within the mined block
contract_address: Address of contract deployed by the transaction
logs: Raw transaction logs
Expand Down Expand Up @@ -270,6 +272,12 @@ def trace(self) -> Optional[List]:
self._expand_trace()
return self._trace

@property
def timestamp(self) -> Optional[int]:
if self.status == -1:
return None
return web3.eth.getBlock(self.block_number)["timestamp"]

def _await_confirmation(self, silent: bool) -> None:
# await tx showing in mempool
while True:
Expand Down
11 changes: 11 additions & 0 deletions docs/api-network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,17 @@ TransactionReceipt Attributes
>>> tx.status
1

.. py:attribute:: TransactionReceipt.timestamp

The timestamp of the block that this transaction was included in.

.. code-block:: python

>>> tx
<Transaction object '0xac54b49987a77805bf6bdd78fb4211b3dc3d283ff0144c231a905afa75a06db0'>
>>> tx.timestamp
1588957325

.. py:attribute:: TransactionReceipt.trace

An expanded `transaction trace <https://github.com/ethereum/go-ethereum/wiki/Tracing:-Introduction#user-content-basic-traces>`_ structLog, returned from the `debug_traceTransaction <https://github.com/ethereum/go-ethereum/wiki/Management-APIs#user-content-debug_tracetransaction>`_ RPC endpoint. If you are using Infura this attribute is not available.
Expand Down
11 changes: 11 additions & 0 deletions tests/network/transaction/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ def test_hash(tester):
assert a == a


def test_timestamp(accounts, web3):
tx = accounts[0].transfer(accounts[1], "1 ether")
assert tx.timestamp == web3.eth.getBlock(web3.eth.blockNumber)["timestamp"]


def test_timestamp_pending(accounts, web3):
tx = accounts[0].transfer(accounts[1], "1 ether")
tx.status = -1
assert tx.timestamp is None


def test_attribute_error(tester):
tx = tester.doNothing()
with pytest.raises(AttributeError):
Expand Down