diff --git a/pyproject.toml b/pyproject.toml index acf0214..92a8d49 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/src/bluefin_v2_client/client.py b/src/bluefin_v2_client/client.py index f37de47..9c9272f 100644 --- a/src/bluefin_v2_client/client.py +++ b/src/bluefin_v2_client/client.py @@ -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 = [] @@ -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 """ @@ -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))) @@ -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 @@ -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() diff --git a/src/bluefin_v2_client/utilities.py b/src/bluefin_v2_client/utilities.py index fcb59bd..42c0286 100644 --- a/src/bluefin_v2_client/utilities.py +++ b/src/bluefin_v2_client/utilities.py @@ -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)