diff --git a/pyproject.toml b/pyproject.toml index 3695d23..3c9c7e0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "bluefin_v2_client_sui" -version = "1.1.6" +version = "1.1.7" 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_rfq_client/quote.py b/src/bluefin_rfq_client/quote.py index be2cbe4..66522d5 100644 --- a/src/bluefin_rfq_client/quote.py +++ b/src/bluefin_rfq_client/quote.py @@ -1,6 +1,5 @@ from sui_utils import * -from typing import Self # Defines the Quote class diff --git a/src/bluefin_rfq_client/rfq.py b/src/bluefin_rfq_client/rfq.py index 85d03e3..9279ab8 100644 --- a/src/bluefin_rfq_client/rfq.py +++ b/src/bluefin_rfq_client/rfq.py @@ -196,10 +196,7 @@ def withdraw_from_vault(self, Returns: Tuple of bool (indicating status of execution) and sui chain response (dict). - """ - - - + """ move_function_params = [ vault, self.rfq_contracts.get_protocol_config(), @@ -228,6 +225,32 @@ def withdraw_from_vault(self, except Exception as e: return False , res + def get_vault_coin_balance(self, + vault: str, + token_type: str + ) -> str : + """ + get balance of specified token type locked in the vault + + Parameters: + vault (str): on chain vault object ID. + token_type (str): on chain token type of the coin (i.e for USDC , usdc_Address::usdc::USDC) + + Returns: + balance(str): balance of the coin scaled by coin decimals. + """ + + res = rpc_sui_getDynamicFieldObject( + self.url, + vault, + strip_hex_prefix(token_type), + SUI_STRING_OBJECT_TYPE) + try: + balance = res["result"]["data"]["content"]["fields"]["value"] + return balance + except Exception as e: + raise Exception("Could not fetch coin balance",e) + def create_vault(self, manager: str ) -> tuple[bool, dict] : diff --git a/src/sui_utils/rpc.py b/src/sui_utils/rpc.py index 40e7a50..0c42acd 100644 --- a/src/sui_utils/rpc.py +++ b/src/sui_utils/rpc.py @@ -104,6 +104,43 @@ def rpc_sui_executeTransactionBlock(url, txBytes, signature, maxRetries=5): time.sleep(1) return result +def rpc_sui_getDynamicFieldObject(url:str, parentObjectId: str, fieldName: str,fieldSuiObjectType:str, maxRetries=5): + """ + Fetches the on-chain dynamic field object corresponding to specified input params + Input: + url: url of the sui chain node + parentObjectId: id of the parent object for which dynamic field needs to be queried + fieldName: name of the dynamic field + fieldSuiObjectType: sui object type for the dynamic field name (eg. for string use , `0x1::string::String`) + + Output: + sui result object for the dynamic field + """ + base_dict = {} + base_dict["jsonrpc"] = "2.0" + base_dict["id"] = 5 + base_dict["method"] = "suix_getDynamicFieldObject" + base_dict["params"] = [] + base_dict["params"].append(parentObjectId) + base_dict["params"].append({ + "type": fieldSuiObjectType, + "value": fieldName + }) + payload = json.dumps(base_dict) + + headers = {"Content-Type": "application/json"} + + for i in range(0, maxRetries): + response = requests.request("POST", url, headers=headers, data=payload) + result = json.loads(response.text) + if "error" in result: + if result["error"]["message"].find(LOCKED_OBJECT_ERROR_CODE) == -1: + return result + else: + return result + + time.sleep(1) + return result def rpc_call_sui_function(url, params, method="suix_getCoins"): """ diff --git a/src/sui_utils/utilities.py b/src/sui_utils/utilities.py index 08656b9..c73fce5 100644 --- a/src/sui_utils/utilities.py +++ b/src/sui_utils/utilities.py @@ -16,6 +16,7 @@ BASE_1E18 = 1000000000000000000 BASE_1E6 = 1000000 # 1e6 for USDC token BASE_1E9 = 1000000000 +SUI_STRING_OBJECT_TYPE = "0x1::string::String" def getsha256Hash(callArgs: list) -> str: