diff --git a/v4-client-py-v2/dydx_v4_client/node/builder.py b/v4-client-py-v2/dydx_v4_client/node/builder.py index 6822d8c5..1b8c8070 100644 --- a/v4-client-py-v2/dydx_v4_client/node/builder.py +++ b/v4-client-py-v2/dydx_v4_client/node/builder.py @@ -16,7 +16,7 @@ TxBody, ) -from dydx_v4_client.node.fee import calculate_fee +from dydx_v4_client.node.fee import calculate_fee, Denom from dydx_v4_client.wallet import Wallet @@ -60,7 +60,7 @@ class Builder: memo: str = "Client Example" def calculate_fee(self, gas_used) -> Fee: - gas_limit, amount = calculate_fee(gas_used) + gas_limit, amount = calculate_fee(gas_used, Denom(self.denomination)) return self.fee(gas_limit, self.coin(amount)) def coin(self, amount: int) -> Coin: diff --git a/v4-client-py-v2/dydx_v4_client/node/client.py b/v4-client-py-v2/dydx_v4_client/node/client.py index 937fd58d..d8b77b77 100644 --- a/v4-client-py-v2/dydx_v4_client/node/client.py +++ b/v4-client-py-v2/dydx_v4_client/node/client.py @@ -70,7 +70,7 @@ from dydx_v4_client.network import NodeConfig from dydx_v4_client.node.builder import Builder -from dydx_v4_client.node.fee import Coin, Fee, calculate_fee +from dydx_v4_client.node.fee import Coin, Fee, calculate_fee, Denom from dydx_v4_client.node.message import ( cancel_order, deposit, @@ -589,7 +589,7 @@ def calculate_fee(self, gas_used) -> Fee: Returns: Fee: The calculated fee. """ - gas_limit, amount = calculate_fee(gas_used) + gas_limit, amount = calculate_fee(gas_used, Denom(self.builder.denomination)) return Fee(gas_limit, [Coin(amount, self.builder.denomination)]) diff --git a/v4-client-py-v2/dydx_v4_client/node/fee.py b/v4-client-py-v2/dydx_v4_client/node/fee.py index 1783eb9d..cec7c1f3 100644 --- a/v4-client-py-v2/dydx_v4_client/node/fee.py +++ b/v4-client-py-v2/dydx_v4_client/node/fee.py @@ -1,6 +1,7 @@ from dataclasses import dataclass from math import ceil, floor from typing import List, Tuple +from enum import Enum from v4_proto.cosmos.base.v1beta1.coin_pb2 import Coin as ProtoCoin from v4_proto.cosmos.tx.v1beta1.tx_pb2 import Fee as ProtoFee @@ -11,6 +12,12 @@ DYDX_GAS_PRICE = 25000000000 +class Denom(Enum): + USDC = "ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5" + DYDX = "adydx" + DYDX_TNT = "adv4tnt" + + @dataclass class Coin: amount: int @@ -32,6 +39,16 @@ def as_proto(self) -> ProtoFee: ) -def calculate_fee(gas_used) -> Tuple[int, int]: +def calculate_fee(gas_used: int, denom: Denom = Denom.USDC) -> Tuple[int, int]: gas_limit = floor(gas_used * GAS_MULTIPLIER) - return gas_limit, ceil(gas_limit * GAS_PRICE) + + if denom in [Denom.DYDX, Denom.DYDX_TNT]: + gas_price = DYDX_GAS_PRICE + elif denom == Denom.USDC: + gas_price = GAS_PRICE + else: + raise ValueError(f"{denom} cannot be used to cover gas fees") + + fee_amount = ceil(gas_limit * gas_price) + + return gas_limit, fee_amount diff --git a/v4-client-py-v2/examples/calculate_fees_example.py b/v4-client-py-v2/examples/calculate_fees_example.py new file mode 100644 index 00000000..2aa3f181 --- /dev/null +++ b/v4-client-py-v2/examples/calculate_fees_example.py @@ -0,0 +1,18 @@ +import asyncio + +from dydx_v4_client.node.fee import Denom, calculate_fee + + +async def test(): + try: + for denom in Denom: + gas_limit, fee_amount = calculate_fee(100000, denom) + print(f"Denom: {denom.name}") + print(f"Gas Limit: {gas_limit}") + print(f"Amount: {fee_amount} {denom.name}") + print() + except ValueError as e: + print(f"Error: {e}") + + +asyncio.run(test()) diff --git a/v4-client-py-v2/pyproject.toml b/v4-client-py-v2/pyproject.toml index 48f5ef9b..6bb94bfc 100644 --- a/v4-client-py-v2/pyproject.toml +++ b/v4-client-py-v2/pyproject.toml @@ -1,8 +1,11 @@ [tool.poetry] name = "dydx-v4-client" -version = "1.0.1" +version = "1.1.0" description = "" -authors = ["Piotr Piwoński "] +authors = [ + "Saul Martin ", + "Piotr Piwoński ", +] readme = "README.md" [tool.poetry.dependencies]