Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
Add eth_gasPrice module
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhargavasomu committed Jan 7, 2019
1 parent 9d932b2 commit 16c3872
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions trinity/rpc/modules/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,31 @@ async def estimateGas(self, txn_dict: Dict[str, Any], at_block: Union[str, int])
gas = self._chain.estimate_gas(transaction, header)
return hex(gas)

async def gasPrice(self) -> int:
raise NotImplementedError()
async def gasPrice(self) -> str:
canonical_header = self._chain.get_canonical_head()
canonical_block = self._chain.get_block_by_header(canonical_header)

# Get the latest block having non-zero transactions
latest_block_having_transactions = canonical_block
while len(latest_block_having_transactions.transactions) != 0:
# Make the transactions_having_latest_block as its parent, since it has no transactions
latest_block_header_having_transactions = self._chain.get_block_header_by_hash(
latest_block_having_transactions.header.parent_hash
)
latest_block_having_transactions = self._chain.get_block_by_header(
latest_block_header_having_transactions
)

# Sum of all the gas_price in transactions of the block
block_overall_gas_price = 0
for transaction in latest_block_having_transactions.transactions:
block_overall_gas_price += transaction.gas_price

# Get the average
num_transactions_latest_block = len(latest_block_having_transactions.transactions)
block_average_gas_price = block_overall_gas_price // num_transactions_latest_block

return hex(block_average_gas_price)

@format_params(decode_hex, to_int_if_hex)
async def getBalance(self, address: Address, at_block: Union[str, int]) -> str:
Expand Down

0 comments on commit 16c3872

Please # to comment.