UNPKG

test-raydium-sdk-v2

Version:

An SDK for building applications on top of Raydium.

1 lines 313 kB
{"version":3,"sources":["../../../src/raydium/liquidity/utils.ts","../../../src/common/txTool/txUtils.ts","../../../src/common/logger.ts","../../../src/raydium/liquidity/serum.ts","../../../src/raydium/liquidity/constant.ts","../../../src/raydium/liquidity/stable.ts","../../../src/marshmallow/index.ts","../../../src/marshmallow/buffer-layout.ts","../../../src/raydium/liquidity/instruction.ts","../../../src/common/bignumber.ts","../../../node_modules/decimal.js/decimal.mjs","../../../src/module/token.ts","../../../src/common/pubKey.ts","../../../src/raydium/token/constant.ts","../../../src/module/fraction.ts","../../../src/module/formatter.ts","../../../src/module/price.ts","../../../src/module/currency.ts","../../../src/module/amount.ts","../../../src/module/percent.ts","../../../src/common/utility.ts","../../../src/raydium/liquidity/layout.ts"],"sourcesContent":["import { Connection, PublicKey } from \"@solana/web3.js\";\nimport { AmmV4Keys, AmmV5Keys } from \"@/api/type\";\nimport {\n findProgramAddress,\n simulateMultipleInstruction,\n parseSimulateLogToJson,\n parseSimulateValue,\n} from \"@/common/txTool/txUtils\";\nimport { getSerumAssociatedAuthority } from \"./serum\";\nimport { LiquidityPoolKeys } from \"./type\";\nimport { StableLayout } from \"./stable\";\nimport { makeSimulatePoolInfoInstruction } from \"./instruction\";\nimport BN from \"bn.js\";\n\ntype AssociatedName =\n | \"amm_associated_seed\"\n | \"lp_mint_associated_seed\"\n | \"coin_vault_associated_seed\"\n | \"pc_vault_associated_seed\"\n | \"lp_mint_associated_seed\"\n | \"temp_lp_token_associated_seed\"\n | \"open_order_associated_seed\"\n | \"target_associated_seed\"\n | \"withdraw_associated_seed\";\n\ninterface GetAssociatedParam {\n name: AssociatedName;\n programId: PublicKey;\n marketId: PublicKey;\n}\n\nexport function getAssociatedConfigId({ programId }: { programId: PublicKey }): PublicKey {\n const { publicKey } = findProgramAddress([Buffer.from(\"amm_config_account_seed\", \"utf-8\")], programId);\n return publicKey;\n}\n\nexport function getLiquidityAssociatedId({ name, programId, marketId }: GetAssociatedParam): PublicKey {\n const { publicKey } = findProgramAddress(\n [programId.toBuffer(), marketId.toBuffer(), Buffer.from(name, \"utf-8\")],\n programId,\n );\n return publicKey;\n}\n\nexport function getAssociatedOpenOrders({ programId, marketId }: { programId: PublicKey; marketId: PublicKey }) {\n const { publicKey } = findProgramAddress(\n [programId.toBuffer(), marketId.toBuffer(), Buffer.from(\"open_order_associated_seed\", \"utf-8\")],\n programId,\n );\n return publicKey;\n}\n\nexport function getLiquidityAssociatedAuthority({ programId }: { programId: PublicKey }): {\n publicKey: PublicKey;\n nonce: number;\n} {\n return findProgramAddress([Buffer.from([97, 109, 109, 32, 97, 117, 116, 104, 111, 114, 105, 116, 121])], programId);\n}\n\nexport function getAssociatedPoolKeys({\n version,\n marketVersion,\n marketId,\n baseMint,\n quoteMint,\n baseDecimals,\n quoteDecimals,\n programId,\n marketProgramId,\n}: {\n version: 4 | 5;\n marketVersion: 3;\n marketId: PublicKey;\n baseMint: PublicKey;\n quoteMint: PublicKey;\n baseDecimals: number;\n quoteDecimals: number;\n programId: PublicKey;\n marketProgramId: PublicKey;\n}): LiquidityPoolKeys {\n const id = getLiquidityAssociatedId({ name: \"amm_associated_seed\", programId, marketId });\n const lpMint = getLiquidityAssociatedId({ name: \"lp_mint_associated_seed\", programId, marketId });\n const { publicKey: authority, nonce } = getLiquidityAssociatedAuthority({ programId });\n const baseVault = getLiquidityAssociatedId({ name: \"coin_vault_associated_seed\", programId, marketId });\n const quoteVault = getLiquidityAssociatedId({ name: \"pc_vault_associated_seed\", programId, marketId });\n const lpVault = getLiquidityAssociatedId({ name: \"temp_lp_token_associated_seed\", programId, marketId });\n const openOrders = getAssociatedOpenOrders({ programId, marketId });\n const targetOrders = getLiquidityAssociatedId({ name: \"target_associated_seed\", programId, marketId });\n const withdrawQueue = getLiquidityAssociatedId({ name: \"withdraw_associated_seed\", programId, marketId });\n\n const { publicKey: marketAuthority } = getSerumAssociatedAuthority({\n programId: marketProgramId,\n marketId,\n });\n\n return {\n // base\n id,\n baseMint,\n quoteMint,\n lpMint,\n baseDecimals,\n quoteDecimals,\n lpDecimals: baseDecimals,\n // version\n version,\n programId,\n // keys\n authority,\n nonce,\n baseVault,\n quoteVault,\n lpVault,\n openOrders,\n targetOrders,\n withdrawQueue,\n // market version\n marketVersion,\n marketProgramId,\n // market keys\n marketId,\n marketAuthority,\n lookupTableAccount: PublicKey.default,\n configId: getAssociatedConfigId({ programId }),\n };\n}\n\nlet stableLayout: StableLayout | undefined;\n\nexport async function fetchMultipleInfo({\n connection,\n poolKeysList,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n config,\n}: {\n connection: Connection;\n poolKeysList: (AmmV4Keys | AmmV5Keys)[];\n config: any;\n}): Promise<\n {\n status: BN;\n baseDecimals: number;\n quoteDecimals: number;\n lpDecimals: number;\n baseReserve: BN;\n quoteReserve: BN;\n lpSupply: BN;\n startTime: BN;\n }[]\n> {\n if (!stableLayout) {\n stableLayout = new StableLayout({ connection });\n await stableLayout.initStableModelLayout();\n }\n\n const instructions = poolKeysList.map((pool) => makeSimulatePoolInfoInstruction({ poolKeys: pool }));\n const logs = await simulateMultipleInstruction(\n connection,\n instructions.map((i) => i.instruction),\n \"GetPoolData\",\n );\n\n const poolsInfo = logs.map((log) => {\n const json = parseSimulateLogToJson(log, \"GetPoolData\");\n\n const status = new BN(parseSimulateValue(json, \"status\"));\n const baseDecimals = Number(parseSimulateValue(json, \"coin_decimals\"));\n const quoteDecimals = Number(parseSimulateValue(json, \"pc_decimals\"));\n const lpDecimals = Number(parseSimulateValue(json, \"lp_decimals\"));\n const baseReserve = new BN(parseSimulateValue(json, \"pool_coin_amount\"));\n const quoteReserve = new BN(parseSimulateValue(json, \"pool_pc_amount\"));\n const lpSupply = new BN(parseSimulateValue(json, \"pool_lp_supply\"));\n // TODO fix it when split stable\n let startTime = \"0\";\n try {\n startTime = parseSimulateValue(json, \"pool_open_time\");\n } catch (error) {\n //\n }\n\n return {\n status,\n baseDecimals,\n quoteDecimals,\n lpDecimals,\n baseReserve,\n quoteReserve,\n lpSupply,\n startTime: new BN(startTime),\n };\n });\n\n return poolsInfo;\n}\n","import {\n Connection,\n PublicKey,\n ComputeBudgetProgram,\n SimulatedTransactionResponse,\n Transaction,\n TransactionInstruction,\n TransactionMessage,\n Keypair,\n EpochInfo,\n VersionedTransaction,\n} from \"@solana/web3.js\";\nimport { TOKEN_PROGRAM_ID } from \"@solana/spl-token\";\n\nimport { createLogger } from \"../logger\";\nimport { InstructionType } from \"./txType\";\nimport { CacheLTA } from \"./lookupTable\";\n\nimport { ComputeBudgetConfig } from \"@/raydium/type\";\n\nconst logger = createLogger(\"Raydium_txUtil\");\n\nexport const MAX_BASE64_SIZE = 1644;\n\nexport function addComputeBudget(config: ComputeBudgetConfig): {\n instructions: TransactionInstruction[];\n instructionTypes: string[];\n} {\n const ins: TransactionInstruction[] = [];\n const insTypes: string[] = [];\n if (config.microLamports) {\n ins.push(ComputeBudgetProgram.setComputeUnitPrice({ microLamports: config.microLamports }));\n insTypes.push(InstructionType.SetComputeUnitPrice);\n }\n if (config.units) {\n ins.push(ComputeBudgetProgram.setComputeUnitLimit({ units: config.units }));\n insTypes.push(InstructionType.SetComputeUnitLimit);\n }\n\n return {\n instructions: ins,\n instructionTypes: insTypes,\n };\n}\n\nexport async function getRecentBlockHash(connection: Connection): Promise<string> {\n try {\n return (await connection.getLatestBlockhash?.())?.blockhash || (await connection.getRecentBlockhash()).blockhash;\n } catch {\n return (await connection.getRecentBlockhash()).blockhash;\n }\n}\n\n/**\n * Forecast transaction size\n */\nexport function forecastTransactionSize(instructions: TransactionInstruction[], signers: PublicKey[]): boolean {\n if (instructions.length < 1) logger.logWithError(`no instructions provided: ${instructions.toString()}`);\n if (signers.length < 1) logger.logWithError(`no signers provided:, ${signers.toString()}`);\n\n const transaction = new Transaction();\n transaction.recentBlockhash = \"11111111111111111111111111111111\";\n transaction.feePayer = signers[0];\n transaction.add(...instructions);\n\n try {\n return Buffer.from(transaction.serialize({ verifySignatures: false })).toString(\"base64\").length < MAX_BASE64_SIZE;\n } catch (error) {\n return false;\n }\n}\n\n/**\n * Simulates multiple instruction\n */\n/**\n * Simulates multiple instruction\n */\nexport async function simulateMultipleInstruction(\n connection: Connection,\n instructions: TransactionInstruction[],\n keyword: string,\n batchRequest = true,\n): Promise<string[]> {\n const feePayer = new PublicKey(\"RaydiumSimuLateTransaction11111111111111111\");\n\n const transactions: Transaction[] = [];\n\n let transaction = new Transaction();\n transaction.feePayer = feePayer;\n\n for (const instruction of instructions) {\n if (!forecastTransactionSize([...transaction.instructions, instruction], [feePayer])) {\n transactions.push(transaction);\n transaction = new Transaction();\n transaction.feePayer = feePayer;\n }\n transaction.add(instruction);\n }\n if (transaction.instructions.length > 0) {\n transactions.push(transaction);\n }\n\n let results: SimulatedTransactionResponse[] = [];\n\n try {\n results = await simulateTransaction(connection, transactions, batchRequest);\n if (results.find((i) => i.err !== null)) throw Error(\"rpc simulateTransaction error\");\n } catch (error) {\n if (error instanceof Error) {\n logger.logWithError(\"failed to simulate for instructions\", \"RPC_ERROR\", {\n message: error.message,\n });\n }\n }\n\n const logs: string[] = [];\n for (const result of results) {\n logger.debug(\"simulate result:\", result);\n\n if (result.logs) {\n const filteredLog = result.logs.filter((log) => log && log.includes(keyword));\n logger.debug(\"filteredLog:\", logs);\n if (!filteredLog.length) logger.logWithError(\"simulate log not match keyword\", \"keyword\", keyword);\n logs.push(...filteredLog);\n }\n }\n\n return logs;\n}\n\nexport function parseSimulateLogToJson(log: string, keyword: string): any {\n const results = log.match(/{[\"\\w:,]+}/g);\n if (!results || results.length !== 1) {\n return logger.logWithError(`simulate log fail to match json, keyword: ${keyword}`);\n }\n\n return results[0];\n}\n\nexport function parseSimulateValue(log: string, key: string): any {\n const reg = new RegExp(`\"${key}\":(\\\\d+)`, \"g\");\n\n const results = reg.exec(log);\n if (!results || results.length !== 2) {\n return logger.logWithError(`simulate log fail to match key\", key: ${key}`);\n }\n\n return results[1];\n}\n\nexport interface ProgramAddress {\n publicKey: PublicKey;\n nonce: number;\n}\nexport function findProgramAddress(\n seeds: Array<Buffer | Uint8Array>,\n programId: PublicKey,\n): {\n publicKey: PublicKey;\n nonce: number;\n} {\n const [publicKey, nonce] = PublicKey.findProgramAddressSync(seeds, programId);\n return { publicKey, nonce };\n}\n\nexport async function simulateTransaction(\n connection: Connection,\n transactions: Transaction[],\n batchRequest?: boolean,\n): Promise<any[]> {\n let results: any[] = [];\n if (batchRequest) {\n const getLatestBlockhash = await connection.getLatestBlockhash();\n\n const encodedTransactions: string[] = [];\n for (const transaction of transactions) {\n transaction.recentBlockhash = getLatestBlockhash.blockhash;\n transaction.lastValidBlockHeight = getLatestBlockhash.lastValidBlockHeight;\n\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n const message = transaction._compile();\n const signData = message.serialize();\n\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n const wireTransaction = transaction._serialize(signData);\n const encodedTransaction = wireTransaction.toString(\"base64\");\n\n encodedTransactions.push(encodedTransaction);\n }\n\n const batch = encodedTransactions.map((keys) => {\n const args = connection._buildArgs([keys], undefined, \"base64\");\n return {\n methodName: \"simulateTransaction\",\n args,\n };\n });\n\n const reqData: { methodName: string; args: any[] }[][] = [];\n const itemReqIndex = 20;\n for (let i = 0; i < Math.ceil(batch.length / itemReqIndex); i++) {\n reqData.push(batch.slice(i * itemReqIndex, (i + 1) * itemReqIndex));\n }\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n results = await (\n await Promise.all(\n reqData.map(async (i) => (await (connection as any)._rpcBatchRequest(i)).map((ii) => ii.result.value)),\n )\n ).flat();\n } else {\n try {\n results = await Promise.all(\n transactions.map(async (transaction) => await (await connection.simulateTransaction(transaction)).value),\n );\n } catch (error) {\n if (error instanceof Error) {\n logger.logWithError(\"failed to get info for multiple accounts\", \"RPC_ERROR\", {\n message: error.message,\n });\n }\n }\n }\n\n return results;\n}\n\nexport function checkLegacyTxSize({\n instructions,\n payer,\n signers,\n}: {\n instructions: TransactionInstruction[];\n payer: PublicKey;\n signers: PublicKey[];\n}): boolean {\n return forecastTransactionSize(instructions, [payer, ...signers]);\n}\n\nexport function checkV0TxSize({\n instructions,\n payer,\n lookupTableAddressAccount,\n recentBlockhash = Keypair.generate().publicKey.toString(),\n}: {\n instructions: TransactionInstruction[];\n payer: PublicKey;\n lookupTableAddressAccount?: CacheLTA;\n recentBlockhash?: string;\n}): boolean {\n const transactionMessage = new TransactionMessage({\n payerKey: payer,\n recentBlockhash,\n instructions,\n });\n\n const messageV0 = transactionMessage.compileToV0Message(Object.values(lookupTableAddressAccount ?? {}));\n try {\n const buildLength = Buffer.from(new VersionedTransaction(messageV0).serialize()).toString(\"base64\").length;\n return buildLength < MAX_BASE64_SIZE;\n } catch (error) {\n return false;\n }\n}\n\nlet epochInfoCache: { time: number; data?: EpochInfo } = {\n time: 0,\n data: undefined,\n};\n\nexport async function getEpochInfo(connection: Connection): Promise<EpochInfo> {\n if (!epochInfoCache.data || (Date.now() - epochInfoCache.time) / 1000 > 30) {\n const data = await connection.getEpochInfo();\n epochInfoCache = {\n time: Date.now(),\n data,\n };\n return data;\n } else {\n return epochInfoCache.data;\n }\n}\n\nexport const toBuffer = (arr: Buffer | Uint8Array | Array<number>): Buffer => {\n if (Buffer.isBuffer(arr)) {\n return arr;\n } else if (arr instanceof Uint8Array) {\n return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength);\n } else {\n return Buffer.from(arr);\n }\n};\n\nexport function printSimulate(transactions: Transaction[] | VersionedTransaction[]): string[] {\n const allBase64: string[] = [];\n transactions.forEach((transaction) => {\n if (transaction instanceof Transaction) {\n if (!transaction.recentBlockhash) transaction.recentBlockhash = TOKEN_PROGRAM_ID.toBase58();\n if (!transaction.feePayer) transaction.feePayer = Keypair.generate().publicKey;\n }\n let serialized = transaction.serialize({ requireAllSignatures: false, verifySignatures: false });\n if (transaction instanceof VersionedTransaction) serialized = toBuffer(serialized);\n const base64 = serialized.toString(\"base64\");\n allBase64.push(base64);\n });\n console.log(\"simulate tx string:\", allBase64);\n\n return allBase64;\n}\n","import { get, set } from \"lodash\";\nimport dayjs from \"dayjs\";\nimport utc from \"dayjs/plugin/utc\";\ndayjs.extend(utc);\n\nexport type ModuleName = \"Common.Api\";\n\nexport enum LogLevel {\n Error,\n Warning,\n Info,\n Debug,\n}\nexport class Logger {\n private logLevel: LogLevel;\n private name: string;\n constructor(params: { name: string; logLevel?: LogLevel }) {\n this.logLevel = params.logLevel !== undefined ? params.logLevel : LogLevel.Error;\n this.name = params.name;\n }\n\n set level(logLevel: LogLevel) {\n this.logLevel = logLevel;\n }\n get time(): string {\n return dayjs().utc().format(\"YYYY/MM/DD HH:mm:ss UTC\");\n }\n get moduleName(): string {\n return this.name;\n }\n\n private isLogLevel(level: LogLevel): boolean {\n return level <= this.logLevel;\n }\n\n public error(...props): Logger {\n if (!this.isLogLevel(LogLevel.Error)) return this;\n console.error(this.time, this.name, \"sdk logger error\", ...props);\n return this;\n }\n\n public logWithError(...props): Logger {\n // this.error(...props)\n const msg = props.map((arg) => (typeof arg === \"object\" ? JSON.stringify(arg) : arg)).join(\", \");\n throw new Error(msg);\n }\n\n public warning(...props): Logger {\n if (!this.isLogLevel(LogLevel.Warning)) return this;\n console.warn(this.time, this.name, \"sdk logger warning\", ...props);\n return this;\n }\n\n public info(...props): Logger {\n if (!this.isLogLevel(LogLevel.Info)) return this;\n console.info(this.time, this.name, \"sdk logger info\", ...props);\n return this;\n }\n\n public debug(...props): Logger {\n if (!this.isLogLevel(LogLevel.Debug)) return this;\n console.debug(this.time, this.name, \"sdk logger debug\", ...props);\n return this;\n }\n}\n\nconst moduleLoggers: { [key in ModuleName]?: Logger } = {};\nconst moduleLevels: { [key in ModuleName]?: LogLevel } = {};\n\nexport function createLogger(moduleName: string): Logger {\n let logger = get(moduleLoggers, moduleName);\n if (!logger) {\n // default level is error\n const logLevel = get(moduleLevels, moduleName);\n\n logger = new Logger({ name: moduleName, logLevel });\n set(moduleLoggers, moduleName, logger);\n }\n\n return logger;\n}\n\nexport function setLoggerLevel(moduleName: string, level: LogLevel): void {\n set(moduleLevels, moduleName, level);\n\n const logger = get(moduleLoggers, moduleName);\n if (logger) logger.level = level;\n}\n","import { PublicKey } from \"@solana/web3.js\";\nimport { LIQUIDITY_VERSION_TO_SERUM_VERSION } from \"./constant\";\nimport { SerumVersion } from \"../serum\";\nimport { createLogger } from \"@/common/logger\";\n\nconst logger = createLogger(\"Raydium_liquidity_serum\");\n\nexport function getSerumVersion(version: number): SerumVersion {\n const serumVersion = LIQUIDITY_VERSION_TO_SERUM_VERSION[version];\n if (!serumVersion) logger.logWithError(\"invalid version\", \"version\", version);\n\n return serumVersion;\n}\n\nexport function getSerumAssociatedAuthority({ programId, marketId }: { programId: PublicKey; marketId: PublicKey }): {\n publicKey: PublicKey;\n nonce: number;\n} {\n const seeds = [marketId.toBuffer()];\n\n let nonce = 0;\n let publicKey: PublicKey;\n\n while (nonce < 100) {\n try {\n const seedsWithNonce = seeds.concat(Buffer.from([nonce]), Buffer.alloc(7));\n publicKey = PublicKey.createProgramAddressSync(seedsWithNonce, programId);\n } catch (err) {\n if (err instanceof TypeError) {\n throw err;\n }\n nonce++;\n continue;\n }\n return { publicKey, nonce };\n }\n\n logger.logWithError(\"unable to find a viable program address nonce\", \"params\", {\n programId,\n marketId,\n });\n throw new Error(\"unable to find a viable program address nonce\");\n}\n","import BN from \"bn.js\";\nimport { SerumVersion } from \"../serum\";\n\nexport const LIQUIDITY_FEES_NUMERATOR = new BN(25);\nexport const LIQUIDITY_FEES_DENOMINATOR = new BN(10000);\n\n// liquidity version => serum version\nexport const LIQUIDITY_VERSION_TO_SERUM_VERSION: {\n [key in 4 | 5]?: SerumVersion;\n} = {\n 4: 3,\n 5: 3,\n};\n","import { Connection, PublicKey } from \"@solana/web3.js\";\n\nimport { seq, struct, u64 } from \"@/marshmallow\";\n\nexport const MODEL_DATA_PUBKEY = new PublicKey(\"CDSr3ssLcRB6XYPJwAfFt18MZvEZp4LjHcvzBVZ45duo\");\nconst ELEMENT_SIZE = 50000;\n\nexport const DataElement = struct([u64(\"x\"), u64(\"y\"), u64(\"price\")]);\n\nexport const modelDataInfoLayout = struct([\n u64(\"accountType\"),\n u64(\"status\"),\n u64(\"multiplier\"),\n u64(\"validDataCount\"),\n seq(DataElement, ELEMENT_SIZE, \"DataElement\"),\n]);\n\nexport interface StableModelLayout {\n accountType: number;\n status: number;\n multiplier: number;\n validDataCount: number;\n DataElement: { x: number; y: number; price: number }[];\n}\n\nfunction estimateRangeByXyReal(_xReal: number, _yReal: number): number[] {\n return [0, ELEMENT_SIZE - 2];\n}\n\nfunction estimateRangeByX(_x: number): number[] {\n return [0, ELEMENT_SIZE - 2];\n}\n\nfunction estimateRangeByY(_y: number): number[] {\n return [0, ELEMENT_SIZE - 2];\n}\n\nfunction getMininumRangeByXyReal(\n layoutData: StableModelLayout,\n xReal: number,\n yReal: number,\n): [number, number, boolean] {\n const [min, max] = estimateRangeByXyReal(xReal, yReal);\n let minRangeIdx = min;\n let maxRangeIdx = max;\n let mid = 0;\n const target = (xReal * layoutData.multiplier) / yReal;\n while (minRangeIdx <= maxRangeIdx) {\n mid = Math.floor((maxRangeIdx + minRangeIdx) / 2);\n if (mid === 0 || mid >= ELEMENT_SIZE - 2) {\n return [mid, mid, false];\n }\n const cur = (layoutData.DataElement[mid].x * layoutData.multiplier) / layoutData.DataElement[mid].y;\n const left = (layoutData.DataElement[mid - 1].x * layoutData.multiplier) / layoutData.DataElement[mid - 1].y;\n const right = (layoutData.DataElement[mid + 1].x * layoutData.multiplier) / layoutData.DataElement[mid + 1].y;\n\n if (target === cur) {\n return [mid, mid, true];\n } else if (target === left) {\n return [mid - 1, mid - 1, true];\n } else if (target === right) {\n return [mid + 1, mid + 1, true];\n } else if (target < left) {\n maxRangeIdx = mid - 1;\n } else if (target > left && target < cur) {\n return [mid - 1, mid, true];\n } else if (target > cur && target < right) {\n return [mid, mid + 1, true];\n } else {\n minRangeIdx = mid + 1;\n }\n }\n return [mid, mid, false];\n}\nfunction getRatio(layoutData: StableModelLayout, xReal: number, yReal: number): number {\n const [minRangeIdx, maxRangeIdx, find] = getMininumRangeByXyReal(layoutData, xReal, yReal);\n\n if (!find) {\n return 0;\n }\n\n if (minRangeIdx === maxRangeIdx) {\n const x = layoutData.DataElement[minRangeIdx].x;\n const ratio = (xReal * layoutData.multiplier) / x;\n return ratio;\n } else {\n const x1 = layoutData.DataElement[minRangeIdx].x;\n const y1 = layoutData.DataElement[minRangeIdx].y;\n const x2 = layoutData.DataElement[maxRangeIdx].x;\n const y2 = layoutData.DataElement[maxRangeIdx].y;\n\n const xDenominator = yReal * (x2 * y1 - x1 * y2);\n const xNumerator1 = x1 * xDenominator;\n const xNumerator2 = (x2 - x1) * (xReal * y1 - x1 * yReal) * y2;\n\n const xNumerator = xNumerator1 + xNumerator2;\n const ratio = (xReal * layoutData.multiplier * xDenominator) / xNumerator;\n return ratio;\n }\n}\n\nfunction realToTable(layoutData: StableModelLayout, realValue: number, ratio: number): number {\n return (realValue * layoutData.multiplier) / ratio;\n}\n\nfunction tableToReal(layoutData: StableModelLayout, tableValue: number, ratio: number): number {\n return (tableValue * ratio) / layoutData.multiplier;\n}\n\nfunction getMinimumRangeByX(layoutData: StableModelLayout, x: number): [number, number, boolean] {\n const [min, max] = estimateRangeByX(x);\n let minRangeIdx = min;\n let maxRangeIdx = max;\n let mid = 0;\n const target = x;\n while (minRangeIdx < maxRangeIdx) {\n mid = Math.floor((maxRangeIdx + minRangeIdx) / 2);\n\n if (mid <= 0 || mid > ELEMENT_SIZE - 2) {\n return [mid, mid, false];\n }\n const cur = layoutData.DataElement[mid].x;\n const left = layoutData.DataElement[mid - 1].x;\n const right = layoutData.DataElement[mid + 1].x;\n\n if (target === cur) return [mid, mid, true];\n else if (target === left) return [mid - 1, mid - 1, true];\n else if (target === right) return [mid + 1, mid + 1, true];\n else if (target < left) maxRangeIdx = mid - 1;\n else if (target > left && target < cur) return [mid - 1, mid, true];\n else if (target > cur && target < right) return [mid, mid + 1, true];\n else minRangeIdx = mid + 1;\n }\n return [mid, mid, false];\n}\n\nfunction getMinimumRangeByY(layoutData: StableModelLayout, y: number): [number, number, boolean] {\n const [min, max] = estimateRangeByY(y);\n let minRangeIdx = min;\n let maxRangeIdx = max;\n let mid = 0;\n const target = y;\n while (minRangeIdx <= maxRangeIdx) {\n mid = Math.floor((maxRangeIdx + minRangeIdx) / 2);\n if (mid <= 0 || mid >= ELEMENT_SIZE - 2) {\n return [mid, mid, false];\n }\n\n const cur = layoutData.DataElement[mid].y;\n const left = layoutData.DataElement[mid - 1].y;\n const right = layoutData.DataElement[mid + 1].y;\n if (target === cur) return [mid, mid, true];\n else if (target === left) return [mid - 1, mid - 1, true];\n else if (target === right) return [mid + 1, mid + 1, true];\n else if (target < right) {\n minRangeIdx = mid + 1;\n } else if (target < left && target > cur) return [mid - 1, mid, true];\n else if (target < cur && target > right) return [mid, mid + 1, true];\n else maxRangeIdx = mid - 1;\n }\n return [mid, mid, false];\n}\n\nfunction getDataByX(\n layoutData: StableModelLayout,\n x: number,\n dx: number,\n priceUp: boolean,\n): [number, number, boolean, boolean] {\n const xWithDx = priceUp ? x + dx : x - dx;\n const [minIdx, maxIdx, find] = getMinimumRangeByX(layoutData, xWithDx);\n if (!find) return [0, 0, false, find];\n\n if (minIdx === maxIdx) return [layoutData.DataElement[maxIdx].price, layoutData.DataElement[maxIdx].y, false, find];\n else {\n const x1 = layoutData.DataElement[minIdx].x;\n const x2 = layoutData.DataElement[maxIdx].x;\n const p1 = layoutData.DataElement[minIdx].price;\n const p2 = layoutData.DataElement[maxIdx].price;\n const y1 = layoutData.DataElement[minIdx].y;\n const y2 = layoutData.DataElement[maxIdx].y;\n\n if (x >= x1 && x <= x2) {\n if (priceUp) return [p2, y2, true, find];\n else return [p1, y1, true, find];\n } else {\n let p, y;\n if (priceUp) {\n p = p1 + ((p2 - p1) * (x - x1)) / (x2 - x1);\n y = y1 - ((xWithDx - x1) * layoutData.multiplier) / p2;\n } else {\n p = p1 + ((p2 - p1) * (x - x1)) / (x2 - x1);\n y = y2 + ((x2 - xWithDx) * layoutData.multiplier) / p1;\n }\n return [p, y, false, find];\n }\n }\n}\n\nfunction getDataByY(\n layoutData: StableModelLayout,\n y: number,\n dy: number,\n priceUp: boolean,\n): [number, number, boolean, boolean] {\n const yWithDy = priceUp ? y - dy : y + dy;\n const [minIdx, maxIdx, find] = getMinimumRangeByY(layoutData, yWithDy);\n if (!find) return [0, 0, false, find];\n if (minIdx === maxIdx) return [layoutData.DataElement[maxIdx].price, layoutData.DataElement[maxIdx].x, false, find];\n else {\n const x1 = layoutData.DataElement[minIdx].x;\n const x2 = layoutData.DataElement[maxIdx].x;\n const p1 = layoutData.DataElement[minIdx].price;\n const p2 = layoutData.DataElement[maxIdx].price;\n const y1 = layoutData.DataElement[minIdx].y;\n const y2 = layoutData.DataElement[maxIdx].y;\n\n if (y >= y2 && y <= y1) {\n return priceUp ? [p2, x2, true, find] : [p1, x1, true, find];\n } else {\n let p, x;\n if (priceUp) {\n p = p1 + ((p2 - p1) * (y1 - y)) / (y1 - y2);\n x = x1 + (p2 * (y1 - yWithDy)) / layoutData.multiplier;\n } else {\n p = p1 + ((p2 - p1) * (y1 - y)) / (y1 - y2);\n x = x2 - (p1 * (yWithDy - y2)) / layoutData.multiplier;\n }\n return [p, x, false, find];\n }\n }\n}\n\nfunction getMidPrice(layoutData: StableModelLayout, x: number): number {\n const ret = getDataByX(layoutData, x, 0, false);\n if (ret[3]) return ret[0];\n else return 0;\n}\n\nexport function getDyByDxBaseIn(layoutData: StableModelLayout, xReal: number, yReal: number, dxReal: number): number {\n const ratio = getRatio(layoutData, xReal, yReal);\n const x = realToTable(layoutData, xReal, ratio);\n const y = realToTable(layoutData, yReal, ratio);\n const dx = realToTable(layoutData, dxReal, ratio);\n const priceUp = true;\n const [p, y2, lessTrade, find] = getDataByX(layoutData, x, dx, priceUp);\n if (!find) return 0;\n if (lessTrade) {\n const dyReal = (dxReal * layoutData.multiplier) / p;\n return dyReal;\n } else {\n const dy = y - y2;\n const dyReal = tableToReal(layoutData, dy, ratio);\n return dyReal;\n }\n}\n\nexport function getDxByDyBaseIn(layoutData: StableModelLayout, xReal: number, yReal: number, dyReal: number): number {\n const ratio = getRatio(layoutData, xReal, yReal);\n const x = realToTable(layoutData, xReal, ratio);\n const y = realToTable(layoutData, yReal, ratio);\n const dy = realToTable(layoutData, dyReal, ratio);\n const priceUp = false;\n const [p, x2, lessTrade, find] = getDataByY(layoutData, y, dy, priceUp);\n if (!find) return 0;\n if (lessTrade) {\n const dxReal = (dyReal * p) / layoutData.multiplier;\n return dxReal;\n } else {\n const dx = x - x2;\n const dxReal = tableToReal(layoutData, dx, ratio);\n return dxReal;\n }\n}\n\nexport function formatLayout(buffer: Buffer): StableModelLayout {\n const layoutInfo = modelDataInfoLayout.decode(buffer);\n return {\n accountType: layoutInfo.accountType.toNumber(),\n status: layoutInfo.status.toNumber(),\n multiplier: layoutInfo.multiplier.toNumber(),\n validDataCount: layoutInfo.validDataCount.toNumber(),\n DataElement: layoutInfo.DataElement.map((item: any) => ({\n x: item.x.toNumber(),\n y: item.y.toNumber(),\n price: item.price.toNumber(),\n })),\n };\n}\n\nexport function getStablePrice(\n layoutData: StableModelLayout,\n coinReal: number,\n pcReal: number,\n baseCoin: boolean,\n): number {\n const price =\n getMidPrice(layoutData, realToTable(layoutData, coinReal, getRatio(layoutData, coinReal, pcReal))) /\n layoutData.multiplier;\n return baseCoin ? price : 1 / price;\n}\n\nexport class StableLayout {\n private readonly connection: Connection;\n private _layoutData: StableModelLayout = {\n accountType: 0,\n status: 0,\n multiplier: 0,\n validDataCount: 0,\n DataElement: [],\n };\n\n constructor({ connection }: { connection: Connection }) {\n this.connection = connection;\n }\n\n get stableModelData(): StableModelLayout {\n return this._layoutData;\n }\n\n public async initStableModelLayout(): Promise<void> {\n if (this._layoutData.validDataCount === 0) {\n if (this.connection) {\n const acc = await this.connection.getAccountInfo(MODEL_DATA_PUBKEY);\n if (acc) this._layoutData = formatLayout(acc?.data);\n }\n }\n }\n}\n","import { PublicKey } from \"@solana/web3.js\";\nimport BN, { isBN } from \"bn.js\";\n\nimport {\n bits,\n blob,\n Blob,\n Layout,\n offset as _offset,\n seq as _seq,\n Structure as _Structure,\n u32 as _u32,\n u8 as _u8,\n UInt,\n union as _union,\n Union as _Union,\n} from \"./buffer-layout\";\n\nexport * from \"./buffer-layout\";\nexport { blob };\n\nexport class BNLayout<P extends string = \"\"> extends Layout<BN, P> {\n blob: Layout<Buffer>;\n signed: boolean;\n\n constructor(span: number, signed: boolean, property?: P) {\n //@ts-expect-error type wrong for super()'s type different from extends, but it desn't matter\n super(span, property);\n this.blob = blob(span);\n this.signed = signed;\n }\n\n /** @override */\n decode(b: Buffer, offset = 0): BN {\n const num = new BN(this.blob.decode(b, offset), 10, \"le\");\n if (this.signed) {\n return num.fromTwos(this.span * 8).clone();\n }\n return num;\n }\n\n /** @override */\n encode(src: BN, b: Buffer, offset = 0): number {\n if (typeof src === \"number\") src = new BN(src); // src will pass a number accidently in union\n if (this.signed) {\n src = src.toTwos(this.span * 8);\n }\n return this.blob.encode(src.toArrayLike(Buffer, \"le\", this.span), b, offset);\n }\n}\n\nexport class WideBits<P extends string = \"\"> extends Layout<Record<string, boolean>, P> {\n _lower: any;\n _upper: any;\n // TODO: unknown\n constructor(property?: P) {\n //@ts-expect-error type wrong for super()'s type different from extends , but it desn't matter\n super(8, property);\n this._lower = bits(_u32(), false);\n this._upper = bits(_u32(), false);\n }\n\n addBoolean(property: string): void {\n if (this._lower.fields.length < 32) {\n this._lower.addBoolean(property);\n } else {\n this._upper.addBoolean(property);\n }\n }\n\n decode(b: Buffer, offset = 0): Record<string, boolean> {\n const lowerDecoded = this._lower.decode(b, offset);\n const upperDecoded = this._upper.decode(b, offset + this._lower.span);\n return { ...lowerDecoded, ...upperDecoded };\n }\n\n encode(src: any /* TEMP */, b: Buffer, offset = 0): any {\n return this._lower.encode(src, b, offset) + this._upper.encode(src, b, offset + this._lower.span);\n }\n}\n\nexport function u8<P extends string = \"\">(property?: P): UInt<number, P> {\n return new UInt(1, property);\n}\n\nexport function u32<P extends string = \"\">(property?: P): UInt<number, P> {\n return new UInt(4, property);\n}\n\nexport function u64<P extends string = \"\">(property?: P): BNLayout<P> {\n return new BNLayout(8, false, property);\n}\n\nexport function u128<P extends string = \"\">(property?: P): BNLayout<P> {\n return new BNLayout(16, false, property);\n}\n\nexport function i8<P extends string = \"\">(property?: P): BNLayout<P> {\n return new BNLayout(1, true, property);\n}\n\nexport function i64<P extends string = \"\">(property?: P): BNLayout<P> {\n return new BNLayout(8, true, property);\n}\n\nexport function i128<P extends string = \"\">(property?: P): BNLayout<P> {\n return new BNLayout(16, true, property);\n}\n\nexport class WrappedLayout<T, U, P extends string = \"\"> extends Layout<U, P> {\n layout: Layout<T>;\n decoder: (data: T) => U;\n encoder: (src: U) => T;\n\n constructor(layout: Layout<T>, decoder: (data: T) => U, encoder: (src: U) => T, property?: P) {\n //@ts-expect-error type wrong for super()'s type different from extends , but it desn't matter\n super(layout.span, property);\n this.layout = layout;\n this.decoder = decoder;\n this.encoder = encoder;\n }\n\n decode(b: Buffer, offset?: number): U {\n return this.decoder(this.layout.decode(b, offset));\n }\n\n encode(src: U, b: Buffer, offset?: number): number {\n return this.layout.encode(this.encoder(src), b, offset);\n }\n\n getSpan(b: Buffer, offset?: number): number {\n return this.layout.getSpan(b, offset);\n }\n}\n\nexport function publicKey<P extends string = \"\">(property?: P): Layout<PublicKey, P> {\n return new WrappedLayout(\n blob(32),\n (b: Buffer) => new PublicKey(b),\n (key: PublicKey) => key.toBuffer(),\n property,\n );\n}\n\nexport class OptionLayout<T, P> extends Layout<T | null, P> {\n layout: Layout<T>;\n discriminator: Layout<number>;\n\n constructor(layout: Layout<T>, property?: P) {\n //@ts-expect-error type wrong for super()'s type different from extends , but it desn't matter\n super(-1, property);\n this.layout = layout;\n this.discriminator = _u8();\n }\n\n encode(src: T | null, b: Buffer, offset = 0): number {\n if (src === null || src === undefined) {\n return this.discriminator.encode(0, b, offset);\n }\n this.discriminator.encode(1, b, offset);\n return this.layout.encode(src, b, offset + 1) + 1;\n }\n\n decode(b: Buffer, offset = 0): T | null {\n const discriminator = this.discriminator.decode(b, offset);\n if (discriminator === 0) {\n return null;\n } else if (discriminator === 1) {\n return this.layout.decode(b, offset + 1);\n }\n throw new Error(\"Invalid option \" + this.property);\n }\n\n getSpan(b: Buffer, offset = 0): number {\n const discriminator = this.discriminator.decode(b, offset);\n if (discriminator === 0) {\n return 1;\n } else if (discriminator === 1) {\n return this.layout.getSpan(b, offset + 1) + 1;\n }\n throw new Error(\"Invalid option \" + this.property);\n }\n}\n\nexport function option<T, P extends string = \"\">(layout: Layout<T>, property?: P): Layout<T | null, P> {\n return new OptionLayout<T, P>(layout, property);\n}\n\nexport function bool<P extends string = \"\">(property?: P): Layout<boolean, P> {\n return new WrappedLayout(_u8(), decodeBool, encodeBool, property);\n}\n\nexport function decodeBool(value: number): boolean {\n if (value === 0) {\n return false;\n } else if (value === 1) {\n return true;\n }\n throw new Error(\"Invalid bool: \" + value);\n}\n\nexport function encodeBool(value: boolean): number {\n return value ? 1 : 0;\n}\n\nexport function vec<T, P extends string = \"\">(elementLayout: Layout<T>, property?: P): Layout<T[], P> {\n const length = _u32(\"length\");\n const layout: Layout<{ values: T[] }> = struct([\n length,\n seq(elementLayout, _offset(length, -length.span), \"values\"),\n ]) as any; // Something I don't know\n return new WrappedLayout(\n layout,\n ({ values }) => values,\n (values) => ({ values }),\n property,\n );\n}\n\nexport function tagged<T, P extends string = \"\">(tag: BN, layout: Layout<T>, property?: P): Layout<T, P> {\n const wrappedLayout: Layout<{ tag: BN; data: T }> = struct([u64(\"tag\"), layout.replicate(\"data\")]) as any; // Something I don't know\n\n function decodeTag({ tag: receivedTag, data }: { tag: BN; data: T }): T {\n if (!receivedTag.eq(tag)) {\n throw new Error(\"Invalid tag, expected: \" + tag.toString(\"hex\") + \", got: \" + receivedTag.toString(\"hex\"));\n }\n return data;\n }\n\n return new WrappedLayout(wrappedLayout, decodeTag, (data) => ({ tag, data }), property);\n}\n\nexport function vecU8<P extends string = \"\">(property?: P): Layout<Buffer, P> {\n const length = _u32(\"length\");\n const layout: Layout<{ data: Buffer }> = struct([length, blob(_offset(length, -length.span), \"data\")]) as any; // Something I don't know\n return new WrappedLayout(\n layout,\n ({ data }) => data,\n (data) => ({ data }),\n property,\n );\n}\n\nexport function str<P extends string = \"\">(property?: P): Layout<string, P> {\n return new WrappedLayout(\n vecU8(),\n (data) => data.toString(\"utf-8\"),\n (s) => Buffer.from(s, \"utf-8\"),\n property,\n );\n}\n\nexport interface EnumLayout<T, P extends string = \"\"> extends Layout<T, P> {\n registry: Record<string, Layout<any>>;\n}\n\nexport function rustEnum<T, P extends string = \"\">(variants: Layout<any>[], property?: P): EnumLayout<T, P> {\n const unionLayout = _union(_u8(), property);\n variants.forEach((variant, index) => unionLayout.addVariant(index, variant, variant.property));\n return unionLayout as any; // ?why use UnionLayout? This must be a fault\n}\n\nexport function array<T, P extends string = \"\">(\n elementLayout: Layout<T>,\n length: number,\n property?: P,\n): Layout<T[], P> {\n const layout = struct([seq(elementLayout, length, \"values\")]) as any as Layout<{ values: T[] }>; // Something I don't know\n return new WrappedLayout(\n layout,\n ({ values }) => values,\n (values) => ({ values }),\n property,\n );\n}\n\nexport class Structure<T, P, D> extends _Structure<T, P, D> {\n /** @override */\n decode(b: Buffer, offset?: number): D {\n return super.decode(b, offset);\n }\n}\n\nexport function struct<T, P extends string = \"\">(\n fields: T,\n property?: P,\n decodePrefixes?: boolean,\n): T extends Layout<infer Value, infer Property>[]\n ? Structure<\n Value,\n P,\n {\n [K in Exclude<Extract<Property, string>, \"\">]: Extract<T[number], Layout<any, K>> extends Layout<infer V, any>\n ? V\n : any;\n }\n >\n : any {\n //@ts-expect-error this type is not quite satisfied the define, but, never no need to worry about.\n return new Structure(fields, property, decodePrefixes);\n}\n\nexport type GetLayoutSchemaFromStructure<T extends Structure<any, any, any>> = T extends Structure<any, any, infer S>\n ? S\n : any;\nexport type GetStructureFromLayoutSchema<S> = Structure<any, any, S>;\n\nexport class Union<Schema> extends _Union<Schema> {\n encodeInstruction(instruction: any): Buffer {\n const instructionMaxSpan = Math.max(...Object.values(this.registry).map((r) => r.span));\n const b = Buffer.alloc(instructionMaxSpan);\n return b.slice(0, this.encode(instruction, b));\n }\n\n decodeInstruction(instruction: any): Partial<Schema> {\n return this.decode(instruction);\n }\n}\nexport function union<UnionSchema extends { [key: string]: any } = any>(\n discr: any,\n defaultLayout?: any,\n property?: string,\n): Union<UnionSchema> {\n return new Union(discr, defaultLayout, property);\n}\n\nclass Zeros extends Blob {\n decode(b: Buffer, offset: number): Buffer {\n const slice = super.decode(b, offset);\n if (!slice.every((v) => v === 0)) {\n throw new Error(\"nonzero padding bytes\");\n }\n return slice;\n }\n}\n\nexport function zeros(length: number): Zeros {\n return new Zeros(length);\n}\n\nexport function seq<T, P extends string = \"\", AnotherP extends string = \"\">(\n elementLayout: Layout<T, P>,\n count: number | BN | Layout<BN | number, P>,\n property?: AnotherP,\n): Layout<T[], AnotherP> {\n let parsedCount: number;\n const superCount =\n typeof count === \"number\"\n ? count\n : isBN(count)\n ? count.toNumber()\n : new Proxy(count as unknown as Layout<number> /* pretend to be Layout<number> */, {\n get(target, property): any {\n if (!parsedCount) {\n // get count in targetLayout. note that count may be BN\n const countProperty = Reflect.get(target, \"count\");\n\n // let targetLayout's property:count be a number\n parsedCount = isBN(countProperty) ? countProperty.toNumber() : countProperty;\n\n // record the count\n Reflect.set(target, \"count\", parsedCount);\n }\n return Reflect.get(target, property);\n },\n set(target, property, value): any {\n if (property === \"count\") {\n parsedCount = value;\n }\n return Reflect.set(target, property, value);\n },\n });\n\n // @ts-expect-error force type\n return _seq(elementLayout, superCount, property);\n}\n","import {\n bits as _bits,\n BitStructure as _BitStructure,\n blob as _blob,\n Blob as _Blob,\n cstr as _cstr,\n f32 as _f32,\n f32be as _f32be,\n f64 as _f64,\n f64be as _f64be,\n greedy as _greedy,\n Layout as _Layout,\n ns64 as _ns64,\n ns64be as _ns64be,\n nu64 as _nu64,\n nu64be as _nu64be,\n offset as _offset,\n s16 as _s16,\n s16be as _s16be,\n s24 as _s24,\n s24be as _s24be,\n s32 as _s32,\n s32be as _s32be,\n s40 as _s40,\n s40be as _s40be,\n s48 as _s48,\n s48be as _s48be,\n s8 as _s8,\n seq as _seq,\n struct as _struct,\n Structure as _Structure,\n u16 as _u16,\n u16be as _u16be,\n u24 as _u24,\n u24be as _u24be,\n u32 as _u32,\n u32be as _u32be,\n u40 as _u40,\n u40be as _u40be,\n u48 as _u48,\n u48be as _u48be,\n u8 as _u8,\n UInt as _UInt,\n union as _union,\n Union as _Union,\n unionLayoutDiscriminator as _unionLayoutDiscriminator,\n utf8 as _utf8,\n} from \"@solana/buffer-layout\";\n\n//#region ------------------- Layout -------------------\nexport interface Layout<T = any, P = \"\"> {\n span: number;\n property?: P;\n decode(b: Buffer, offset?: number): T;\n encode(src: T, b: Buffer, offset?: number): number;\n getSpan(b: Buffer, offset?: number): number;\n replicate<AP extends string>(name: AP): Layout<T, AP>;\n}\nexport interface LayoutConstructor {\n new <T, P>(): Layout<T, P>; // for class extends syntex\n new <T, P>(span?: T, property?: P): Layout<T, P>;\n readonly prototype: Layout;\n}\nexport const Layout = _Layout as unknown as LayoutConstructor;\n//#endregion\n\n//#region ------------------- Structure -------------------\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport interface Structure<T = any, P = \"\", DecodeSchema extends { [key: string]: any } = any>\n extends Layout<DecodeSchema, P> {\n span: number;\n decode(b: Buffer, offset?: number): DecodeSchema;\n layoutFor<AP extends string>(property: AP): Layout<DecodeSchema[AP]>;\n offsetOf<AP extends string>(property: AP): number;\n}\ninterface StructureConstructor {\n new <T = any, P = \"\", DecodeSchema extends { [key: string]: any } = any>(): Structure<T, P, DecodeSchema>;\n new <T = any, P = \"\", DecodeSchema extends { [key: string]: any } = any>(\n fields: T,\n property?: P,\n decodePrefixes?: boolean,\n ): Structure<T, P, DecodeSchema>;\n}\nexport const Structure = _Structure as unknown as StructureConstructor;\n//#endregion\n\n//#region ------------------- Union -------------------\nexport interface Union<UnionSchema extends { [key: string]: any } = any> extends Layout {\n registry: object;\n decode(b: Buffer, offset?: number): Partial<UnionSchema>;\n addVariant(\n variant: number,\n layout: Structure<any, any, Partial<UnionSchema>> | Layout<any, keyof UnionSchema>,\n property?: string,\n ): any /* TEMP: code in Layout.js 1809 */;\n}\ninterface UnionConstructor {\n new <UnionSchema extends { [key: string]: any } = any>(): Union<UnionSchema>;\n new <UnionSchema extends { [key: string]: any } = any>(\n discr: Layout<any, any>,\n defaultLayout: Layout<any, any>,\n property?: string,\n ): Union<UnionSchema>;\n}\nexport const Union = _Union as unknown as UnionConstructor;\n//#endregion\n\n//#region ------------------- BitStructure -------------------\nexport type BitStructure<T = unknown /* TEMP */, P = \"\"> = Layout<T, P>;\ninterface BitStructureConstructor {\n new (...params: any[]): BitStructure;\n}\nexport const BitStructure = _BitStructure as BitStructureConstructor;\n//#endregion\n\n//#region ------------------- UInt -------------------\nexport type UInt<T = any, P = \"\"> = Layout<T, P>;\ninterface UIntConstructor {\n new <T, P>(span?: T, property?: P): UInt<T, P>;\n}\nexport const UInt = _UInt as UIntConstructor;\n//#endregion\n\n//#region ------------------- Blob -------------------\nexport type Blob<P extends string = \"\"> = Layout<Buffer, P>;\ninterface BlobConstructor {\n new (...params: ConstructorParameters<LayoutConstructor>): Blob;\n}\nexport const Blob = _Blob as unknown as BlobConstructor;\n//#endregion\n\nexport const greedy = _greedy as <P extends string = \"\">(elementSpan?: number, property?: P) => Layout<number, P>;\nexport const u8 = _u8 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const u16 = _u16 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const u24 = _u24 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const u32 = _u32 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const u40 = _u40 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const u48 = _u48 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const nu64 = _nu64 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const u16be = _u16be as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const u24be = _u24be as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const u32be = _u32be as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const u40be = _u40be as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const u48be = _u48be as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const nu64be = _nu64be as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const s8 = _s8 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const s16 = _s16 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const s24 = _s24 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const s32 = _s32 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const s40 = _s40 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const s48 = _s48 as <P extends string = \"\">(property?: P) => Layout<number, P>;\nexport const ns64 = _ns64 a