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

refactor: add use fallback decorator #85

Merged
merged 4 commits into from
Jan 15, 2024

Conversation

JuArce
Copy link
Collaborator

@JuArce JuArce commented Jan 13, 2024

Changes

  1. Add decorators to implement fallback mechanism for RPCs
    a. Add use_fallback(rpc_nodes, logger, error_message="Failed") decorator for sync functions.
    b. Add use_async_fallback(rpc_nodes, logger, error_message="Failed") for async functinos.
    Methods that use this decorator must receive as last parameter the rpc_node. Ie
@use_async_fallback(rpc_nodes, logger, "Failed to sign invoke transaction")
async def sign_invoke_transaction(call: Call, max_fee: int, rpc_node=main_rpc_node):
    return await rpc_node.account.sign_invoke_transaction(call, max_fee=max_fee)

This refactor keeps separate fallback mechanism from methods logic improving the code readability.

  1. Added two classes that contains client, account and contract to work with the RPC
class EthereumRpcNode:
    def __init__(self, rpc_url, private_key, contract_address, abi):
        self.w3 = Web3(Web3.HTTPProvider(rpc_url))
        self.account = self.w3.eth.account.from_key(private_key)
        self.contract = self.w3.eth.contract(address=contract_address, abi=abi)
class StarknetRpcNode:
    def __init__(self, rpc_url, private_key, wallet_address, contract_address, chain_id):
        self.full_node_client = MmFullNodeClient(node_url=rpc_url)
        key_pair = KeyPair.from_private_key(key=private_key)
        self.account = Account(
            client=self.full_node_client,
            address=wallet_address,
            key_pair=key_pair,
            chain=chain_id,
        )
        self.contract_address = contract_address

@JuArce JuArce linked an issue Jan 13, 2024 that may be closed by this pull request
Copy link
Collaborator

@rcatalan98 rcatalan98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a huge improvement in readability congrats! Just left a small comment.

mm-bot/src/services/ethereum.py Outdated Show resolved Hide resolved
@rcatalan98 rcatalan98 merged commit eb8595d into main Jan 15, 2024
@rcatalan98 rcatalan98 deleted the 54-refactor-add-use_fallback-decorator branch January 15, 2024 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor: add use_fallback decorator
2 participants