Skip to content

Commit

Permalink
feat: refactor fee calculation to support multiple denominations (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtin0x authored Sep 30, 2024
1 parent e62c959 commit 61ebc6e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
4 changes: 2 additions & 2 deletions v4-client-py-v2/dydx_v4_client/node/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions v4-client-py-v2/dydx_v4_client/node/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)])


Expand Down
21 changes: 19 additions & 2 deletions v4-client-py-v2/dydx_v4_client/node/fee.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +12,12 @@
DYDX_GAS_PRICE = 25000000000


class Denom(Enum):
USDC = "ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5"
DYDX = "adydx"
DYDX_TNT = "adv4tnt"


@dataclass
class Coin:
amount: int
Expand All @@ -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
18 changes: 18 additions & 0 deletions v4-client-py-v2/examples/calculate_fees_example.py
Original file line number Diff line number Diff line change
@@ -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())
7 changes: 5 additions & 2 deletions v4-client-py-v2/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
[tool.poetry]
name = "dydx-v4-client"
version = "1.0.1"
version = "1.1.0"
description = ""
authors = ["Piotr Piwoński <[email protected]>"]
authors = [
"Saul Martin <[email protected]>",
"Piotr Piwoński <[email protected]>",
]
readme = "README.md"

[tool.poetry.dependencies]
Expand Down

0 comments on commit 61ebc6e

Please sign in to comment.