From f7b1810d16b38ed6806b54a7054d4fd3edccf411 Mon Sep 17 00:00:00 2001 From: Felix <62290842+ctrl-Felix@users.noreply.github.com> Date: Sun, 11 Jun 2023 15:32:20 +0200 Subject: [PATCH] feat: add gas estimation to grpcclient --- pyproject.toml | 2 +- src/mospy/clients/GRPCClient.py | 59 +++++++++++++++++++++++++++----- src/mospy/clients/HTTPClient.py | 4 +-- tests/clients/test_grpcclient.py | 10 ++++++ tests/clients/test_httpclient.py | 5 +-- 5 files changed, 66 insertions(+), 14 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0cb2364..96dff49 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "mospy-wallet" -version = "0.3.7" +version = "0.4" description = "This package is a fork of cosmospy and is a light framework for the cosmos ecosystem" authors = [ { name = "ctrl-felix", email = "dev@ctrl-felix.de" }, diff --git a/src/mospy/clients/GRPCClient.py b/src/mospy/clients/GRPCClient.py index 765274f..5cc3647 100644 --- a/src/mospy/clients/GRPCClient.py +++ b/src/mospy/clients/GRPCClient.py @@ -37,15 +37,19 @@ def __init__( if protobuf.lower() in _protobuf_packages.keys() else protobuf) try: - self.BroadcastTxRequest = importlib.import_module( + self._cosmos_tx_service_pb2 = importlib.import_module( _protobuf_package + - ".cosmos.tx.v1beta1.service_pb2").BroadcastTxRequest - self.query_pb2 = importlib.import_module( + ".cosmos.tx.v1beta1.service_pb2") + self._cosmos_auth_query_pb2 = importlib.import_module( _protobuf_package + ".cosmos.auth.v1beta1.query_pb2") - self.query_pb2_grpc = importlib.import_module( + self._cosmos_auth_query_pb2_grpc = importlib.import_module( _protobuf_package + ".cosmos.auth.v1beta1.query_pb2_grpc") - self.service_pb2_grpc = importlib.import_module( + self._cosmos_tx_service_pb2_grpc = importlib.import_module( _protobuf_package + ".cosmos.tx.v1beta1.service_pb2_grpc") + + self._BroadcastTxRequest = self._cosmos_tx_service_pb2.BroadcastTxRequest + self._SimulateTxRequest = self._cosmos_tx_service_pb2.SimulateRequest + except AttributeError: raise ImportError( "It seems that you are importing conflicting protobuf files. Have sou set the protobuf attribute to specify your coin? Check out the documentation for more information." @@ -78,8 +82,8 @@ def load_account_data(self, account: Account): con = self._connect() address = account.address - query_stub = self.query_pb2_grpc.QueryStub(con) - account_request = self.query_pb2.QueryAccountRequest(address=address) + query_stub = self._cosmos_auth_query_pb2_grpc.QueryStub(con) + account_request = self._cosmos_auth_query_pb2.QueryAccountRequest(address=address) req = query_stub.Account(account_request) data = dict(MessageToDict(req.account)) @@ -112,12 +116,12 @@ def broadcast_transaction(self, con = self._connect() tx_bytes = transaction.get_tx_bytes() - tx_request = self.BroadcastTxRequest( + tx_request = self._BroadcastTxRequest( tx_bytes=tx_bytes, mode=2 # BROADCAST_MODE_SYNC ) - tx_stub = self.service_pb2_grpc.ServiceStub(con) + tx_stub = self._cosmos_tx_service_pb2_grpc.ServiceStub(con) tx_data = tx_stub.BroadcastTx(tx_request) hash = tx_data.tx_response.txhash @@ -125,3 +129,40 @@ def broadcast_transaction(self, log = None if code == 0 else tx_data.tx_response.raw_log return {"hash": hash, "code": code, "log": log} + + def estimate_gas(self, + *, + transaction: Transaction, + update: bool = True, + multiplier: float = 1.2 + ) ->int: + """ + Simulate a transaction to get the estimated gas usage. + + Note: + Takes only positional arguments + + Args: + transaction (Transaction): The transaction object + update (bool): Update the transaction with the estimated gas amount + multiplier (float): Multiplier for the estimated gas when updating the transaction. Defaults to 1.2 + + Returns: + expedted_gas: Expected gas + """ + con = self._connect() + tx_bytes = transaction.get_tx_bytes() + + simulate_request = self._SimulateTxRequest( + tx_bytes=tx_bytes, + ) + + tx_stub = self._cosmos_tx_service_pb2_grpc.ServiceStub(con) + tx_data = tx_stub.Simulate(simulate_request) + + gas_used = tx_data.gas_info.gas_used + + if update: + transaction.set_gas(int(gas_used * multiplier)) + + return gas_used diff --git a/src/mospy/clients/HTTPClient.py b/src/mospy/clients/HTTPClient.py index 963a8cb..84e3606 100644 --- a/src/mospy/clients/HTTPClient.py +++ b/src/mospy/clients/HTTPClient.py @@ -99,7 +99,7 @@ def estimate_gas(self, Args: transaction (Transaction): The transaction object update (bool): Update the transaction with the estimated gas amount - update (float): Multiplier for the estimated gas when updating the transaction. Defaults to 1.2 + multiplier (float): Multiplier for the estimated gas when updating the transaction. Defaults to 1.2 timeout (int): Timeout Returns: @@ -114,6 +114,6 @@ def estimate_gas(self, gas_used = int(data["gas_info"]["gas_used"]) if update: - transaction.set_gas(int(gas_used * multiplier)) + return gas_used \ No newline at end of file diff --git a/tests/clients/test_grpcclient.py b/tests/clients/test_grpcclient.py index a77ad38..6c2a02e 100644 --- a/tests/clients/test_grpcclient.py +++ b/tests/clients/test_grpcclient.py @@ -52,3 +52,13 @@ def test_transaction_submitting(self): assert ( tx_data["hash"] == "54B845AEB1523803D4EAF2330AE5759A83458CB5F0211159D04CC257428503C4") + + + client.load_account_data(account=account) + + gas_used = client.estimate_gas( + transaction=tx, + update=False, + ) + + assert gas_used > 0 diff --git a/tests/clients/test_httpclient.py b/tests/clients/test_httpclient.py index e7e558e..5214d3a 100644 --- a/tests/clients/test_httpclient.py +++ b/tests/clients/test_httpclient.py @@ -53,10 +53,11 @@ def test_transaction_submitting(self): client.load_account_data(account=account) - gas_wanted = client.estimate_gas( + gas_used = client.estimate_gas( transaction=tx, update=False, ) - assert gas_wanted > 0 + assert gas_used > 0 +