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

feat: refactor fee calculation to support multiple denominations #253

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <piwonskp@gmail.com>"]
authors = [
"Saul Martin <saulmartin1@pm.me>",
"Piotr Piwoński <piwonskp@gmail.com>",
]
readme = "README.md"

[tool.poetry.dependencies]
Expand Down
Loading