Skip to content

Commit 68dfe37

Browse files
authored
Merge branch 'main' into logan/upgrade-testnet-and-market-make-bot
2 parents 23d51b6 + 3d4d264 commit 68dfe37

3 files changed

Lines changed: 32 additions & 9 deletions

File tree

src/decibel/_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"round_to_valid_order_size",
3636
"amount_to_chain_units",
3737
"chain_units_to_amount",
38+
"bps_to_chain_units",
3839
"extract_vault_address_from_create_tx",
3940
"generate_random_replay_protection_nonce",
4041
]
@@ -385,6 +386,21 @@ def amount_to_chain_units(amount: float, decimals: int = 6) -> int:
385386
return round(amount * (10**decimals))
386387

387388

389+
def bps_to_chain_units(bps: int | float) -> int:
390+
"""Convert basis points (bps) to chain units for builder fees.
391+
392+
The contract uses FEE_PRECISION = 10000 to represent 1%.
393+
Since a basis point is 1/100th of a percent, multiply by 100.
394+
395+
Args:
396+
bps: Basis point value (e.g. 10 for 0.1%)
397+
398+
Returns:
399+
Chain units as integer (e.g. 10 bps -> 1000)
400+
"""
401+
return round(bps * 100)
402+
403+
388404
def chain_units_to_amount(chain_units: int, decimals: int = 6) -> float:
389405
"""Convert chain units to a decimal amount (e.g., 5670000 -> 5.67)."""
390406
return chain_units / (10**decimals)

src/decibel/write/__init__.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from decibel._subaccount_types import RenameSubaccount, RenameSubaccountArgs
1717
from decibel._transaction_builder import InputEntryFunctionData
1818
from decibel._utils import (
19+
bps_to_chain_units,
1920
get_market_addr,
2021
get_primary_subaccount_addr,
2122
post_request,
@@ -270,6 +271,7 @@ async def place_order(
270271
if sl_limit_price is not None and tick_size
271272
else sl_limit_price
272273
)
274+
final_builder_fee = bps_to_chain_units(builder_fee) if builder_fee is not None else None
273275

274276
pkg = self._config.deployment.package
275277

@@ -293,7 +295,7 @@ async def _send(addr: str) -> dict[str, Any]:
293295
final_sl_trigger,
294296
final_sl_limit,
295297
builder_addr,
296-
builder_fee,
298+
final_builder_fee,
297299
],
298300
),
299301
account_override,
@@ -359,6 +361,7 @@ async def place_twap_order(
359361
) -> PlaceOrderResult:
360362
market_addr = get_market_addr(market_name, self._config.deployment.perp_engine_global)
361363
pkg = self._config.deployment.package
364+
final_builder_fees = bps_to_chain_units(builder_fees) if builder_fees is not None else None
362365

363366
async def _send(addr: str) -> dict[str, Any]:
364367
return await self._send_tx(
@@ -375,7 +378,7 @@ async def _send(addr: str) -> dict[str, Any]:
375378
twap_frequency_seconds,
376379
twap_duration_seconds,
377380
builder_address,
378-
builder_fees,
381+
final_builder_fees,
379382
],
380383
),
381384
account_override,
@@ -1049,17 +1052,18 @@ async def approve_max_builder_fee(
10491052
self,
10501053
*,
10511054
builder_addr: str,
1052-
max_fee: int,
1055+
max_fee: int | float,
10531056
subaccount_addr: str | None = None,
10541057
) -> dict[str, Any]:
10551058
pkg = self._config.deployment.package
1059+
final_max_fee = bps_to_chain_units(max_fee)
10561060

10571061
async def _send(addr: str) -> dict[str, Any]:
10581062
return await self._send_tx(
10591063
InputEntryFunctionData(
10601064
function=f"{pkg}::dex_accounts_entry::approve_max_builder_fee_for_subaccount",
10611065
type_arguments=[],
1062-
function_arguments=[addr, builder_addr, max_fee],
1066+
function_arguments=[addr, builder_addr, final_max_fee],
10631067
)
10641068
)
10651069

@@ -1302,6 +1306,7 @@ def place_order(
13021306
if sl_limit_price is not None and tick_size
13031307
else sl_limit_price
13041308
)
1309+
final_builder_fee = bps_to_chain_units(builder_fee) if builder_fee is not None else None
13051310

13061311
pkg = self._config.deployment.package
13071312

@@ -1325,7 +1330,7 @@ def _send(addr: str) -> dict[str, Any]:
13251330
final_sl_trigger,
13261331
final_sl_limit,
13271332
builder_addr,
1328-
builder_fee,
1333+
final_builder_fee,
13291334
],
13301335
),
13311336
account_override,
@@ -1391,6 +1396,7 @@ def place_twap_order(
13911396
) -> PlaceOrderResult:
13921397
market_addr = get_market_addr(market_name, self._config.deployment.perp_engine_global)
13931398
pkg = self._config.deployment.package
1399+
final_builder_fees = bps_to_chain_units(builder_fees) if builder_fees is not None else None
13941400

13951401
def _send(addr: str) -> dict[str, Any]:
13961402
return self._send_tx(
@@ -1407,7 +1413,7 @@ def _send(addr: str) -> dict[str, Any]:
14071413
twap_frequency_seconds,
14081414
twap_duration_seconds,
14091415
builder_address,
1410-
builder_fees,
1416+
final_builder_fees,
14111417
],
14121418
),
14131419
account_override,
@@ -2077,17 +2083,18 @@ def approve_max_builder_fee(
20772083
self,
20782084
*,
20792085
builder_addr: str,
2080-
max_fee: int,
2086+
max_fee: int | float,
20812087
subaccount_addr: str | None = None,
20822088
) -> dict[str, Any]:
20832089
pkg = self._config.deployment.package
2090+
final_max_fee = bps_to_chain_units(max_fee)
20842091

20852092
def _send(addr: str) -> dict[str, Any]:
20862093
return self._send_tx(
20872094
InputEntryFunctionData(
20882095
function=f"{pkg}::dex_accounts_entry::approve_max_builder_fee_for_subaccount",
20892096
type_arguments=[],
2090-
function_arguments=[addr, builder_addr, max_fee],
2097+
function_arguments=[addr, builder_addr, final_max_fee],
20912098
)
20922099
)
20932100

src/decibel/write/_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class CancelTpSlOrderArgs(TypedDict):
151151

152152
class ApproveBuilderFeeArgs(TypedDict):
153153
builder_addr: str
154-
max_fee: int
154+
max_fee: int | float
155155
subaccount_addr: NotRequired[str | None]
156156

157157

0 commit comments

Comments
 (0)