import { Network } from "@aptos-labs/ts-sdk"; export const getUrlFromNetwork = (network: Network) => { switch (network) { case Network.MAINNET: return "https://api.testnet.tapp.exchange/v1"; case Network.TESTNET: return "https://api.testnet.tapp.exchange/v1"; default: throw new Error(`Network is not supported: ${network}`); } }; export const getSpacingFromFee = (fee: number): number => { const feeSpacingMap: Record = { 100: 1, 500: 10, 3000: 60, 10000: 200, }; const spacing = feeSpacingMap[fee]; if (spacing === undefined) { throw new Error(`Unsupported fee tier: ${fee}`); } return spacing; }; export const toU64 = (value: number): bigint => { if (value < 0) { // Convert signed value to its unsigned (two's complement) representation const U64_MAX = 2n ** 64n; // Maximum value for u64 return U64_MAX + BigInt(value); } return BigInt(value); }; export const priceToTickIndex = (price: number, tickSpacing = 60) => { if (price <= 0) { throw new Error("Price must be greater than 0"); } const ln1_0001 = Math.log(1.0001); // Natural log of 1.0001 const tickIndex = Math.log(price) / ln1_0001; // Compute raw tick index // Align tick index to tick spacing return Math.floor(tickIndex / tickSpacing) * tickSpacing; }; export const roundPriceToTickSpacing = (price: number, fee: number) => { const tickSpacing = getSpacingFromFee(fee); const tick = Math.floor(Math.log(price) / Math.log(1.0001)); const roundedTick = Math.round(tick / tickSpacing) * tickSpacing; return Math.pow(1.0001, roundedTick); };