Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "bluefin_v2_client"
version = "2.3.1"
version = "2.4.0"
description = "Library to interact with Bluefin exchange protocol including its off-chain api-gateway and on-chain contracts"
readme = "README.md"
requires-python = ">=3.8"
Expand Down
23 changes: 18 additions & 5 deletions src/bluefin_v2_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,18 @@ async def post_signed_order(self, params: PlaceOrderRequest):
)

## Contract calls
async def deposit_margin_to_bank(self, amount: int, coin_id: str) -> bool:
async def deposit_margin_to_bank(self, amount: int, coin_id: str = "") -> bool:
"""
Deposits given amount of USDC from user's account to margin bank

Inputs:
amount (number): quantity of usdc to be deposited to bank in base decimals (1,2 etc)

coin_id (string) (optional): the id of the coin you want the amount to be deducted from
Returns:
Boolean: true if amount is successfully deposited, false otherwise
"""
if coin_id == "":
coin_id = await self._get_coin_having_balance(amount)
package_id = self.contracts.get_package_id()
user_address = self.account.getUserAddress()
callArgs = []
Expand Down Expand Up @@ -585,7 +587,7 @@ async def get_native_chain_token_balance(self) -> float:
except Exception as e:
raise (Exception(f"Failed to get balance, error: {e}"))

def get_usdc_coins(self):
async def get_usdc_coins(self):
"""
Returns the list of the usdc coins owned by user
"""
Expand All @@ -609,7 +611,7 @@ async def get_usdc_balance(self) -> float:
result = rpc_call_sui_function(
self.url, callArgs, method="suix_getBalance"
)["totalBalance"]
return fromSuiBase(result)
return fromUsdcBase(result)

except Exception as e:
raise (Exception("Failed to get balance, Exception: {}".format(e)))
Expand All @@ -628,7 +630,7 @@ async def get_margin_bank_balance(self) -> float:
self.url, call_args, method="suix_getDynamicFieldObject"
)

balance = from1e18(
balance = fromSuiBase(
result["data"]["content"]["fields"]["value"]["fields"]["balance"]
)
return balance
Expand Down Expand Up @@ -950,6 +952,17 @@ async def reset_cancel_on_disconnect_timer(self, params: PostTimerAttributes):
)
return response

async def _get_coin_having_balance(self, balance: int) -> str:
usdc_coins_resp = await self.get_usdc_coins()
usdc_coins = usdc_coins_resp["data"]
balance = toSuiBase(balance)
for coin in usdc_coins:
if int(coin["balance"]) <= balance:
return coin["coinObjectId"]
raise Exception(
"Insufficient balance, please add more SUI tokens or merge your existing tokens"
)

async def close_connections(self):
# close aio http connection
await self.apis.close_session()
Expand Down
6 changes: 6 additions & 0 deletions src/bluefin_v2_client/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def fromSuiBase(number: Union[str, int]) -> float:
return number / float(BASE_1E9)


def toSuiBase(number: Union[str, int]) -> int:
"""Takes in a number and multiplies it by 1e9"""
number = int(number)
return number * BASE_1E9


def toUsdcBase(number: Union[int, float]) -> int:
"""Converts a number to usdc contract onchain representation i.e. multiply it by 1e6"""
return int(number * BASE_1E6)
Expand Down