Skip to content

Commit

Permalink
Fix DecodedTransactionMetadata model, fix types, update pre-commit (#…
Browse files Browse the repository at this point in the history
…136)

- added optional fields `from_address` and `to_address` to DecodedTransactionMetadata model
- updated black `pre-commit` version
- fixed wrong transaction metadata types
- updated tests
  • Loading branch information
kchojn authored Apr 6, 2022
1 parent cdd7193 commit 77a601c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
repos:
- repo: https://github.com/psf/black
rev: 19.3b0
rev: 22.3.0
hooks:
- id: black
language_version: python3.8
language_version: python3.9
name: Backend:black
alias: backend-black

Expand Down
5 changes: 4 additions & 1 deletion ethtx/decoders/abi/transfers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ def _transfers_calls(decoded_call):
for event in events:

# this is a signature of Transfer event valid for ERC20 and ERC721
if event.event_signature == "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef":
if (
event.event_signature
== "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
):

from_address = event.parameters[0].value
from_name = self._repository.get_address_label(
Expand Down
4 changes: 2 additions & 2 deletions ethtx/decoders/semantic/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
DecodedCall,
Proxy,
)
from ethtx.models.objects_model import BlockMetadata, TransactionMetadata
from ethtx.models.objects_model import BlockMetadata


class SemanticDecoder(ISemanticDecoder):
Expand Down Expand Up @@ -59,7 +59,7 @@ def decode_transaction(
def decode_metadata(
self,
block_metadata: BlockMetadata,
tx_metadata: TransactionMetadata,
tx_metadata: DecodedTransactionMetadata,
chain_id: str,
) -> DecodedTransactionMetadata:
return SemanticMetadataDecoder(repository=self.repository).decode(
Expand Down
4 changes: 2 additions & 2 deletions ethtx/decoders/semantic/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# limitations under the License.

from ethtx.models.decoded_model import DecodedTransactionMetadata, AddressInfo
from ethtx.models.objects_model import BlockMetadata, TransactionMetadata
from ethtx.models.objects_model import BlockMetadata
from .abc import SemanticSubmoduleAbc


Expand All @@ -21,7 +21,7 @@ class SemanticMetadataDecoder(SemanticSubmoduleAbc):
def decode(
self,
block_metadata: BlockMetadata,
tx_metadata: TransactionMetadata,
tx_metadata: DecodedTransactionMetadata,
chain_id: str,
) -> DecodedTransactionMetadata:
"""Semantically decode metadata."""
Expand Down
6 changes: 4 additions & 2 deletions ethtx/models/decoded_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import List, Any, Optional

from ethtx.models.base_model import BaseModel
from ethtx.models.objects_model import BlockMetadata, TransactionMetadata
from ethtx.models.objects_model import BlockMetadata
from ethtx.models.semantics_model import AddressSemantics, ERC20Semantics


Expand All @@ -33,6 +33,8 @@ class DecodedTransactionMetadata(BaseModel):
block_hash: Optional[str]
timestamp: Optional[datetime]
gas_price: Optional[int]
from_address: Optional[str]
to_address: Optional[str]
sender: Optional[AddressInfo]
receiver: Optional[AddressInfo]
tx_index: int
Expand Down Expand Up @@ -98,7 +100,7 @@ class DecodedBalance(BaseModel):

class DecodedTransaction(BaseModel):
block_metadata: BlockMetadata
metadata: TransactionMetadata
metadata: DecodedTransactionMetadata
events: List[DecodedEvent]
calls: Optional[DecodedCall]
transfers: List[DecodedTransfer]
Expand Down
4 changes: 3 additions & 1 deletion ethtx/providers/web3_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@ def get_erc20_token(
def guess_erc20_token(self, contract_address, chain_id: Optional[str] = None):
chain = self._get_node_connection(chain_id)

byte_code = chain.eth.get_code(Web3.toChecksumAddress(contract_address[-40:])).hex()
byte_code = chain.eth.get_code(
Web3.toChecksumAddress(contract_address[-40:])
).hex()

if all(
"63" + signature[2:] in byte_code
Expand Down
4 changes: 2 additions & 2 deletions tests/models/decoded_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ def test_decoded_transaction(self):

t = DecodedTransaction(
block_metadata=ObjectModelMock.BLOCK_METADATA,
metadata=ObjectModelMock.TRANSACTION_METADATA,
metadata=DecodedModelMock.DECODED_TRANSACTION_METADATA,
events=[de],
calls=dc,
transfers=[dt],
balances=[db],
)

assert t.block_metadata == ObjectModelMock.BLOCK_METADATA
assert t.metadata == ObjectModelMock.TRANSACTION_METADATA
assert t.metadata == DecodedModelMock.DECODED_TRANSACTION_METADATA
assert t.events == [de]
assert t.calls == dc
assert t.transfers == [dt]
Expand Down
16 changes: 15 additions & 1 deletion tests/models/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from ethtx.models.decoded_model import AddressInfo, Argument
from ethtx.models.decoded_model import AddressInfo, Argument, DecodedTransactionMetadata
from ethtx.models.objects_model import BlockMetadata, TransactionMetadata, Call, Event
from ethtx.models.semantics_model import ParameterSemantics, ContractSemantics

Expand All @@ -22,6 +22,20 @@ def now(cls):
class DecodedModelMock:
ADDRESS_INFO: AddressInfo = AddressInfo(address="address", name="name")
ARGUMENT: Argument = Argument(name="name", type="type", value=1)
DECODED_TRANSACTION_METADATA: DecodedTransactionMetadata = (
DecodedTransactionMetadata(
tx_hash="0x",
block_number=1,
gas_price=2,
from_address="0xa",
to_address="0xb",
tx_index=3,
tx_value=4,
gas_limit=5,
gas_used=1,
success=False,
)
)


class ObjectModelMock:
Expand Down

0 comments on commit 77a601c

Please sign in to comment.