Skip to content

Commit

Permalink
feat(rpc): add sim_getTransactionsForAddress to be able to get all tr…
Browse files Browse the repository at this point in the history
…ansactions for a given address (#569)

* feat(rpc): add sim_getTransactionsForAddress to be able to get all transactions for a given address

Signed-off-by: Agustín Ramiro Díaz <[email protected]>

* order by creation date

Signed-off-by: Agustín Ramiro Díaz <[email protected]>

* add optional parameter

Signed-off-by: Agustín Ramiro Díaz <[email protected]>

---------

Signed-off-by: Agustín Ramiro Díaz <[email protected]>
  • Loading branch information
AgustinRamiroDiaz authored Oct 16, 2024
1 parent 272ab22 commit 5b647e1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
33 changes: 33 additions & 0 deletions backend/database_handler/transactions_processor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
# consensus/services/transactions_db_service.py
from enum import Enum
import rlp

from .models import Transactions, TransactionsAudit
from sqlalchemy.orm import Session
from sqlalchemy import or_, and_

from .models import TransactionStatus
from eth_utils import to_bytes, keccak, is_address
import json


class TransactionAddressFilter(Enum):
ALL = "all"
TO = "to"
FROM = "from"


class TransactionsProcessor:
def __init__(
self,
Expand Down Expand Up @@ -182,3 +190,28 @@ def get_transaction_count(self, address: str) -> int:
.count()
)
return count

def get_transactions_for_address(
self,
address: str,
filter: TransactionAddressFilter,
) -> list[dict]:
query = self.session.query(Transactions)

if filter == TransactionAddressFilter.TO:
query = query.filter(Transactions.to_address == address)
elif filter == TransactionAddressFilter.FROM:
query = query.filter(Transactions.from_address == address)
else: # TransactionFilter.ALL
query = query.filter(
or_(
Transactions.from_address == address,
Transactions.to_address == address,
)
)

transactions = query.order_by(Transactions.created_at.desc()).all()

return [
self._parse_transaction_data(transaction) for transaction in transactions
]
23 changes: 22 additions & 1 deletion backend/protocol_rpc/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
)
from backend.errors.errors import InvalidAddressError, InvalidTransactionError

from backend.database_handler.transactions_processor import TransactionsProcessor
from backend.database_handler.transactions_processor import (
TransactionAddressFilter,
TransactionsProcessor,
)
from backend.node.base import Node
from backend.node.genvm.types import ExecutionMode

Expand Down Expand Up @@ -498,6 +501,20 @@ def send_raw_transaction(
return transaction_hash


def get_transactions_for_address(
transactions_processor: TransactionsProcessor,
accounts_manager: AccountsManager,
address: str,
filter: str = TransactionAddressFilter.ALL.value,
) -> list[dict]:
if not accounts_manager.is_valid_address(address):
raise InvalidAddressError(address)

return transactions_processor.get_transactions_for_address(
address, TransactionAddressFilter(filter)
)


def register_all_rpc_endpoints(
jsonrpc: JSONRPC,
msg_handler: MessageHandler,
Expand Down Expand Up @@ -612,3 +629,7 @@ def register_all_rpc_endpoints(
partial(get_transaction_count, transactions_processor),
method_name="eth_getTransactionCount",
)
register_rpc_endpoint(
partial(get_transactions_for_address, transactions_processor, accounts_manager),
method_name="sim_getTransactionsForAddress",
)

0 comments on commit 5b647e1

Please sign in to comment.