Skip to content

Commit

Permalink
Merge pull request #518 from iamdefinitelyahuman/feat-gas-used
Browse files Browse the repository at this point in the history
Account.gas_used
  • Loading branch information
iamdefinitelyahuman authored May 14, 2020
2 parents 0ee2d52 + c37806b commit 62863a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions brownie/network/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
from brownie.exceptions import IncompatibleEVMVersion, UnknownAccount, VirtualMachineError

from .rpc import Rpc, _revert_register
from .state import TxHistory
from .transaction import TransactionReceipt
from .web3 import _resolve_address, web3

__tracebackhide__ = True

history = TxHistory()
rpc = Rpc()


Expand Down Expand Up @@ -197,6 +200,11 @@ def balance(self) -> Wei:
balance = web3.eth.getBalance(self.address)
return Wei(balance)

@property
def gas_used(self) -> int:
"""Returns the cumulative gas amount paid from this account."""
return sum(i.gas_used for i in history.from_sender(self.address))

@property
def nonce(self) -> int:
return web3.eth.getTransactionCount(self.address)
Expand Down
11 changes: 10 additions & 1 deletion docs/api-network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,23 @@ Account Attributes
>>> accounts[0].address
'0x7Ebaa12c5d1EE7fD498b51d4F9278DC45f8D627A'
.. py:attribute:: Account.gas_used
The cumulative gas amount paid for transactions from this account.
.. code-block:: python
>>> accounts[0].gas_used
21000
.. py:attribute:: Account.nonce
The current nonce of the address.
.. code-block:: python
>>> accounts[0].nonce
0
1
Account Methods
***************
Expand Down
7 changes: 7 additions & 0 deletions tests/network/account/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ def test_balance(accounts):
balance = accounts[0].balance()
assert type(balance) is Wei
assert balance == "100 ether"


def test_gas_paid(accounts):
accounts[0].transfer(accounts[1], 10000)
accounts[0].transfer(accounts[2], 10000)

assert accounts[0].gas_used == 42000

0 comments on commit 62863a2

Please sign in to comment.