Skip to content

Commit

Permalink
feat: add gas estimation to grpcclient
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrl-Felix committed Jun 11, 2023
1 parent a1b8fcb commit f7b1810
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]" },
Expand Down
59 changes: 50 additions & 9 deletions src/mospy/clients/GRPCClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -112,16 +116,53 @@ 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
code = tx_data.tx_response.code
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
4 changes: 2 additions & 2 deletions src/mospy/clients/HTTPClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
10 changes: 10 additions & 0 deletions tests/clients/test_grpcclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions tests/clients/test_httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f7b1810

Please sign in to comment.