UNPKG

@firefly-exchange/library-sui

Version:

Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui

113 lines (112 loc) 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const constants_1 = require("../../constants"); const enums_1 = require("../enums"); class Store { /** * Fetches the content of perpetual from the store. The store id could belong to EDS or IDS * @param suiClient Sui Client * @param storeID The id/address of the store. Could belong to EDS or IDS * @param perpName The symbol/name of the perpetual to be fetched * @param isExternalStore (optional) defaults to false * @returns */ static async getPerpetualFromStore(suiClient, storeID, perpName, isExternalStore) { const objDetails = await suiClient.getObject({ id: storeID, options: { showContent: true } }); // eslint-disable-next-line @typescript-eslint/no-explicit-any const perpTable = objDetails.data.content.fields.perpetuals.fields.id.id; const perpetual = await suiClient.getDynamicFieldObject({ parentId: perpTable, name: { type: "0x1::string::String", value: perpName } }); if (perpetual.error) return undefined; // eslint-disable-next-line @typescript-eslint/no-explicit-any const fields = perpetual.data.content.fields.value.fields; return isExternalStore ? fields.perpetual.fields : fields; } /** * Fetches the whitelisted operator address from the provided store * @param suiClient Sui Client * @param storeID The id/address of the internal store * @param operator The name of the operator to query * @returns address of funding operator if set else returns ZERO address */ static async getOperator(suiClient, storeID, operator) { const objDetails = await suiClient.getObject({ id: storeID, options: { showContent: true } }); // eslint-disable-next-line @typescript-eslint/no-explicit-any const operatorsTable = objDetails.data.content.fields.operators.fields.id .id; const entry = await suiClient.getDynamicFieldObject({ parentId: operatorsTable, name: { type: "0x1::string::String", value: operator } }); // eslint-disable-next-line @typescript-eslint/no-explicit-any return entry.error ? constants_1.ADDRESSES.ZERO : entry.data.content.fields.value; } /** * Returns the keys of the requested table * @param suiClient Sui Client * @param storeID The id/address of the internal store * @param table_index The table index for which to get keys * @param maxLimit The max number of hashes to be returned */ static async getTableKeys(suiClient, storeID, table_index, maxLimit = -1) { const objDetails = await suiClient.getObject({ id: storeID, options: { showContent: true } }); const fields = objDetails.data.content.fields; // eslint-disable-next-line @typescript-eslint/no-explicit-any const tableID = table_index == enums_1.PRUNE_TABLES_INDEX.HISTORY ? fields.hashes.fields.id.id : fields.filled_orders.fields.id.id; let pages = []; let nextCursor = undefined; let hasNextPage = true; while (hasNextPage) { const data = await suiClient.getDynamicFields({ parentId: tableID, cursor: nextCursor }); nextCursor = data.nextCursor; hasNextPage = data.hasNextPage; pages = [...pages, ...data.data]; // if we have reached the max required hashes, slice and return if (maxLimit > -1 && pages.length > maxLimit) { pages = pages.slice(0, maxLimit); break; } } return pages.map(d => d.name.value); } /** * Returns the whitelisted bankrupt liquidators * @param suiClient Sui Client * @param storeID The id/address of the internal store * @param type the table type for which to get keys * @param maxLimit The max number of hashes to be returned */ static async getLiquidators(suiClient, storeID) { const objDetails = await suiClient.getObject({ id: storeID, options: { showContent: true } }); // eslint-disable-next-line @typescript-eslint/no-explicit-any const liquidators = objDetails.data.content.fields.liquidators; return liquidators; } } exports.default = Store;