// Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 import type { SuiClientTypes } from '@mysten/sui/client'; import { normalizeSuiAddress } from '@mysten/sui/utils'; import { BalanceManagerContract } from '../transactions/balanceManager.js'; import type { BalanceManager, MarginManager, Coin, Pool, MarginPool, PythConfig, } from '../types/index.js'; import type { CoinMap, PoolMap, MarginPoolMap, DeepbookPackageIds } from './constants.js'; import { ResourceNotFoundError, ConfigurationError, ErrorMessages } from './errors.js'; import { mainnetCoins, mainnetPackageIds, mainnetPools, testnetCoins, testnetPackageIds, testnetPools, mainnetMarginPools, testnetMarginPools, mainnetPythConfigs, testnetPythConfigs, } from './constants.js'; // Constants for numerical precision and scaling export const FLOAT_SCALAR = 1_000_000_000; // 10^9 - Used for floating point representation export const DEEP_SCALAR = 1_000_000; // 10^6 - DEEP token decimal places // Time-related constants export const MAX_TIMESTAMP = 1_844_674_407_370_955_161n; // Maximum Unix timestamp (approximately year 2554) export const PRICE_INFO_OBJECT_MAX_AGE_MS = 30_000; // 30 seconds in milliseconds // Transaction and fee constants export const GAS_BUDGET = 250_000_000; // 0.25 SUI (0.5 * 500M MIST) export const POOL_CREATION_FEE_DEEP = 500_000_000; // 500 DEEP tokens (500 * 10^6) export class DeepBookConfig { #coins: CoinMap; #pools: PoolMap; #marginPools: MarginPoolMap; network: SuiClientTypes.Network; balanceManagers: { [key: string]: BalanceManager }; marginManagers: { [key: string]: MarginManager }; address: string; pyth: PythConfig; DEEPBOOK_PACKAGE_ID: string; REGISTRY_ID: string; DEEP_TREASURY_ID: string; MARGIN_PACKAGE_ID: string; MARGIN_V1: string; MARGIN_REGISTRY_ID: string; LIQUIDATION_PACKAGE_ID: string; adminCap?: string; marginAdminCap?: string; marginMaintainerCap?: string; balanceManager: BalanceManagerContract; constructor({ network, address, adminCap, marginAdminCap, marginMaintainerCap, balanceManagers, marginManagers, coins, pools, marginPools, packageIds, pyth, pythAccessToken, }: { network: SuiClientTypes.Network; address: string; adminCap?: string; marginAdminCap?: string; marginMaintainerCap?: string; balanceManagers?: { [key: string]: BalanceManager }; marginManagers?: { [key: string]: MarginManager }; coins?: CoinMap; pools?: PoolMap; marginPools?: MarginPoolMap; packageIds?: DeepbookPackageIds; pyth?: PythConfig; /** * Bearer token for the Hermes serving Pyth's upgraded Core, which answers 401 * without one. Set this rather than `pyth` when the built-in state objects are * correct and only the credential is missing — `pyth` replaces the whole config, * so setting a token through it means restating the state object ids. * * Applied after `pyth`, so if both carry a token this one wins. */ pythAccessToken?: string; }) { this.network = network; this.address = normalizeSuiAddress(address); this.adminCap = adminCap; this.marginAdminCap = marginAdminCap; this.marginMaintainerCap = marginMaintainerCap; this.balanceManagers = balanceManagers || {}; this.marginManagers = marginManagers || {}; if (packageIds) { this.DEEPBOOK_PACKAGE_ID = packageIds.DEEPBOOK_PACKAGE_ID || ''; this.REGISTRY_ID = packageIds.REGISTRY_ID || ''; this.DEEP_TREASURY_ID = packageIds.DEEP_TREASURY_ID || ''; this.MARGIN_PACKAGE_ID = packageIds.MARGIN_PACKAGE_ID || ''; this.MARGIN_V1 = packageIds.MARGIN_V1 || ''; this.MARGIN_REGISTRY_ID = packageIds.MARGIN_REGISTRY_ID || ''; this.LIQUIDATION_PACKAGE_ID = packageIds.LIQUIDATION_PACKAGE_ID || ''; this.#coins = coins || {}; this.#pools = pools || {}; this.#marginPools = marginPools || {}; this.pyth = pyth || { pythStateId: '', wormholeStateId: '' }; } else if (network === 'mainnet') { this.#coins = coins || mainnetCoins; this.#pools = pools || mainnetPools; this.#marginPools = marginPools || mainnetMarginPools; this.DEEPBOOK_PACKAGE_ID = mainnetPackageIds.DEEPBOOK_PACKAGE_ID; this.REGISTRY_ID = mainnetPackageIds.REGISTRY_ID; this.DEEP_TREASURY_ID = mainnetPackageIds.DEEP_TREASURY_ID; this.MARGIN_PACKAGE_ID = mainnetPackageIds.MARGIN_PACKAGE_ID; this.MARGIN_V1 = mainnetPackageIds.MARGIN_V1; this.MARGIN_REGISTRY_ID = mainnetPackageIds.MARGIN_REGISTRY_ID; this.LIQUIDATION_PACKAGE_ID = mainnetPackageIds.LIQUIDATION_PACKAGE_ID; this.pyth = pyth || mainnetPythConfigs; } else if (network === 'testnet') { this.#coins = coins || testnetCoins; this.#pools = pools || testnetPools; this.#marginPools = marginPools || testnetMarginPools; this.DEEPBOOK_PACKAGE_ID = testnetPackageIds.DEEPBOOK_PACKAGE_ID; this.REGISTRY_ID = testnetPackageIds.REGISTRY_ID; this.DEEP_TREASURY_ID = testnetPackageIds.DEEP_TREASURY_ID; this.MARGIN_PACKAGE_ID = testnetPackageIds.MARGIN_PACKAGE_ID; this.MARGIN_V1 = testnetPackageIds.MARGIN_V1; this.MARGIN_REGISTRY_ID = testnetPackageIds.MARGIN_REGISTRY_ID; this.LIQUIDATION_PACKAGE_ID = testnetPackageIds.LIQUIDATION_PACKAGE_ID; this.pyth = pyth || testnetPythConfigs; } else { throw new Error( `Network '${network}' is not supported by default. Provide custom 'packageIds' for non-standard networks.`, ); } // Applied after the branches so a token composes with the built-in state objects // instead of forcing the caller to restate them. // Empty is treated as absent rather than as a credential: `PYTH_TOKEN=` exported // blank is the common way this arrives, and a blank bearer header 401s with a // worse message than the configuration error. if (pythAccessToken) { this.pyth = { ...this.pyth, accessToken: pythAccessToken }; } this.balanceManager = new BalanceManagerContract(this); } requirePyth() { const { pythStateId, wormholeStateId } = this.pyth; if (!pythStateId || !wormholeStateId) { throw new ConfigurationError( "Pyth configuration is required for price feed operations. Provide 'pyth' when using custom packageIds.", ); } } /** * The Pyth feed id for a coin under the active deployment. * * Pairs with {@link getPriceInfoObjectId}: Hermes is queried by feed id and the Move * call takes the object, so the two must come from the same deployment or the update * lands on an object the on-chain feed-id check then rejects. */ getFeedId(coinKey: string): string { const { feed } = this.getCoin(coinKey); if (!feed) { throw new ConfigurationError(`Coin '${coinKey}' has no Pyth feed id configured.`); } return feed; } /** * The `PriceInfoObject` id for a coin on Pyth's upgraded Core. * * Throws rather than passing `undefined` into a move call: a feed may simply have no * object on the upgraded deployment yet, and the resulting on-chain abort * (`EPriceFeedIdMismatch`) does not say which coin was at fault. */ getPriceInfoObjectId(coinKey: string): string { const { priceInfoObjectId } = this.getCoin(coinKey); if (!priceInfoObjectId) { throw new ConfigurationError( `Coin '${coinKey}' has no priceInfoObjectId. Pyth's upgraded Core has no price feed object for it on ${this.network}, or the id is missing from your coin config.`, ); } return priceInfoObjectId; } // Getters getCoin(key: string): Coin { const coin = this.#coins[key]; if (!coin) { throw new ResourceNotFoundError('Coin', key); } return coin; } getPool(key: string): Pool { const pool = this.#pools[key]; if (!pool) { throw new ResourceNotFoundError('Pool', key); } return pool; } getMarginPool(key: string): MarginPool { const pool = this.#marginPools[key]; if (!pool) { throw new ResourceNotFoundError('Margin pool', key); } return pool; } /** * @description Get the balance manager by key * @param managerKey Key of the balance manager * @returns The BalanceManager object */ getBalanceManager(managerKey: string): BalanceManager { if (!Object.hasOwn(this.balanceManagers, managerKey)) { throw new Error(ErrorMessages.BALANCE_MANAGER_NOT_FOUND(managerKey)); } return this.balanceManagers[managerKey]; } /** * @description Get the margin manager by key * @param managerKey Key of the margin manager * @returns The MarginManager object */ getMarginManager(managerKey: string): MarginManager { if (!Object.hasOwn(this.marginManagers, managerKey)) { throw new Error(ErrorMessages.MARGIN_MANAGER_NOT_FOUND(managerKey)); } return this.marginManagers[managerKey]; } }