From 1eb0ff66b4c7d1784c563fc788009a2e401de073 Mon Sep 17 00:00:00 2001 From: yasir_ejaz Date: Tue, 12 Sep 2023 22:08:14 +0400 Subject: [PATCH 1/6] Make coin id optional param in deposit to margin bank call --- src/bluefin_v2_client/client.py | 16 ++++++++++++++-- src/bluefin_v2_client/utilities.py | 6 ++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/bluefin_v2_client/client.py b/src/bluefin_v2_client/client.py index f37de47..75c0ad1 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 is "": + coin_id = self._get_coin_having_balance(amount) package_id = self.contracts.get_package_id() user_address = self.account.getUserAddress() callArgs = [] @@ -950,6 +952,16 @@ async def reset_cancel_on_disconnect_timer(self, params: PostTimerAttributes): ) return response + def _get_coin_having_balance(self, balance: int) -> str: + usdc_coins = self.get_usdc_coins()["data"] + balance = toSuiBase(balance) + for coin in usdc_coins: + if 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) From a12fc12aaa56e3ff71fba9fa674f6459f17f5dfd Mon Sep 17 00:00:00 2001 From: yasir_ejaz Date: Tue, 12 Sep 2023 22:10:13 +0400 Subject: [PATCH 2/6] Bump up version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" From f6b9a9ab98537e2e1af2898bfe406202c0317385 Mon Sep 17 00:00:00 2001 From: yasir_ejaz Date: Tue, 12 Sep 2023 22:21:40 +0400 Subject: [PATCH 3/6] Update condition --- src/bluefin_v2_client/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bluefin_v2_client/client.py b/src/bluefin_v2_client/client.py index 75c0ad1..3abc843 100644 --- a/src/bluefin_v2_client/client.py +++ b/src/bluefin_v2_client/client.py @@ -335,7 +335,7 @@ async def deposit_margin_to_bank(self, amount: int, coin_id: str = "") -> bool: Returns: Boolean: true if amount is successfully deposited, false otherwise """ - if coin_id is "": + if coin_id == "": coin_id = self._get_coin_having_balance(amount) package_id = self.contracts.get_package_id() user_address = self.account.getUserAddress() From c1827ae4e11c7413e05763153b294ad99cb15218 Mon Sep 17 00:00:00 2001 From: yasir_ejaz Date: Tue, 12 Sep 2023 22:28:06 +0400 Subject: [PATCH 4/6] Fix bug in get_margin_bank_balance method --- src/bluefin_v2_client/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bluefin_v2_client/client.py b/src/bluefin_v2_client/client.py index 3abc843..2bff303 100644 --- a/src/bluefin_v2_client/client.py +++ b/src/bluefin_v2_client/client.py @@ -630,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 From 27a53fcea763b123a2a7bfa8b870d6e5bc2524b9 Mon Sep 17 00:00:00 2001 From: yasir_ejaz Date: Tue, 12 Sep 2023 22:35:24 +0400 Subject: [PATCH 5/6] Fix usdc deposit function --- src/bluefin_v2_client/client.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/bluefin_v2_client/client.py b/src/bluefin_v2_client/client.py index 2bff303..f4e3b54 100644 --- a/src/bluefin_v2_client/client.py +++ b/src/bluefin_v2_client/client.py @@ -336,7 +336,7 @@ async def deposit_margin_to_bank(self, amount: int, coin_id: str = "") -> bool: Boolean: true if amount is successfully deposited, false otherwise """ if coin_id == "": - coin_id = self._get_coin_having_balance(amount) + coin_id = await self._get_coin_having_balance(amount) package_id = self.contracts.get_package_id() user_address = self.account.getUserAddress() callArgs = [] @@ -587,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 """ @@ -952,11 +952,12 @@ async def reset_cancel_on_disconnect_timer(self, params: PostTimerAttributes): ) return response - def _get_coin_having_balance(self, balance: int) -> str: - usdc_coins = self.get_usdc_coins()["data"] + 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 coin["balance"] <= balance: + if int(coin["balance"]) <= balance: return coin["coinObjectId"] raise Exception( "Insufficient balance, please add more SUI tokens or merge your existing tokens" From 9c23f97f038d9d468dab4c7440f2183390c0c2a6 Mon Sep 17 00:00:00 2001 From: yasir_ejaz Date: Tue, 12 Sep 2023 22:40:08 +0400 Subject: [PATCH 6/6] Fix base of get_usdc_balance method --- src/bluefin_v2_client/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bluefin_v2_client/client.py b/src/bluefin_v2_client/client.py index f4e3b54..9c9272f 100644 --- a/src/bluefin_v2_client/client.py +++ b/src/bluefin_v2_client/client.py @@ -611,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)))