test-rrr-sdk
Version:
An SDK for building applications on top of Raydium.
1 lines • 1.55 MB
Source Map (JSON)
{"version":3,"sources":["../../src/raydium/raydium.ts","../../src/api/api.ts","../../src/common/accountInfo.ts","../../src/common/logger.ts","../../src/common/bignumber.ts","../../node_modules/decimal.js/decimal.mjs","../../src/module/amount.ts","../../src/module/formatter.ts","../../src/module/fraction.ts","../../src/common/constant.ts","../../src/raydium/token/constant.ts","../../src/module/token.ts","../../src/common/pubKey.ts","../../src/module/currency.ts","../../src/module/percent.ts","../../src/module/price.ts","../../src/common/utility.ts","../../src/common/lodash.ts","../../src/common/owner.ts","../../src/common/pda.ts","../../src/common/txTool/txUtils.ts","../../src/common/txTool/txType.ts","../../src/common/programId.ts","../../src/common/transfer.ts","../../src/common/txTool/lookupTable.ts","../../src/common/txTool/txTool.ts","../../src/common/fee.ts","../../src/api/url.ts","../../src/api/utils.ts","../../src/common/error.ts","../../src/raydium/account/account.ts","../../src/raydium/moduleBase.ts","../../src/raydium/account/instruction.ts","../../src/raydium/account/util.ts","../../node_modules/@noble/hashes/src/_assert.ts","../../node_modules/@noble/hashes/src/utils.ts","../../node_modules/@noble/hashes/src/_md.ts","../../node_modules/@noble/hashes/src/sha256.ts","../../src/marshmallow/index.ts","../../src/marshmallow/buffer-layout.ts","../../src/raydium/account/layout.ts","../../src/raydium/farm/farm.ts","../../src/raydium/farm/config.ts","../../src/raydium/farm/layout.ts","../../src/raydium/farm/instruction.ts","../../src/raydium/farm/util.ts","../../src/raydium/liquidity/liquidity.ts","../../src/raydium/clmm/instrument.ts","../../src/raydium/clmm/utils/tick.ts","../../src/raydium/clmm/utils/constants.ts","../../src/raydium/clmm/utils/math.ts","../../src/raydium/clmm/utils/util.ts","../../src/raydium/clmm/utils/pda.ts","../../src/raydium/clmm/utils/pool.ts","../../src/raydium/clmm/utils/position.ts","../../src/raydium/clmm/utils/tickQuery.ts","../../src/raydium/clmm/utils/tickarrayBitmap.ts","../../src/raydium/clmm/layout.ts","../../src/raydium/token/layout.ts","../../src/raydium/token/utils.ts","../../src/raydium/liquidity/constant.ts","../../src/raydium/liquidity/instruction.ts","../../src/raydium/liquidity/layout.ts","../../src/raydium/liquidity/stable.ts","../../src/raydium/liquidity/utils.ts","../../src/raydium/liquidity/serum.ts","../../src/raydium/marketV2/createMarket.ts","../../src/raydium/marketV2/instrument.ts","../../src/raydium/marketV2/layout.ts","../../src/raydium/clmm/clmm.ts","../../src/raydium/cpmm/cpmm.ts","../../src/raydium/cpmm/curve/calculator.ts","../../src/raydium/cpmm/curve/constantProduct.ts","../../src/raydium/cpmm/curve/fee.ts","../../src/raydium/cpmm/instruction.ts","../../src/raydium/cpmm/pda.ts","../../src/raydium/cpmm/layout.ts","../../src/raydium/tradeV2/trade.ts","../../src/raydium/serum/id.ts","../../src/raydium/serum/layout.ts","../../src/raydium/serum/serum.ts","../../src/raydium/tradeV2/instrument.ts","../../src/raydium/utils1216/utils1216.ts","../../src/raydium/ido/ido.ts","../../src/raydium/ido/instruction.ts","../../src/raydium/ido/layout.ts","../../src/raydium/launchpad/pad.ts","../../src/raydium/launchpad/instrument.ts","../../src/raydium/launchpad/launchpad.ts","../../src/raydium/launchpad/layout.ts","../../src/raydium/launchpad/curve/curve.ts","../../src/raydium/launchpad/curve/constantProductCurve.ts","../../src/raydium/launchpad/curve/curveBase.ts","../../src/raydium/launchpad/curve/fixedPriceCurve.ts","../../src/raydium/launchpad/curve/linearPriceCurve.ts","../../src/raydium/launchpad/curve/func.ts","../../src/raydium/token/token.ts"],"sourcesContent":["import { Connection, Keypair, PublicKey, EpochInfo, Commitment } from \"@solana/web3.js\";\nimport { merge } from \"lodash\";\n\nimport { Api, API_URL_CONFIG, ApiV3TokenRes, ApiV3Token, JupTokenType, AvailabilityCheckAPI3 } from \"../api\";\nimport { EMPTY_CONNECTION, EMPTY_OWNER } from \"../common/error\";\nimport { createLogger, Logger } from \"../common/logger\";\nimport { Owner } from \"../common/owner\";\nimport { Cluster } from \"../solana\";\n\nimport Account, { TokenAccountDataProp } from \"./account/account\";\nimport Farm from \"./farm/farm\";\nimport Liquidity from \"./liquidity/liquidity\";\nimport { Clmm } from \"./clmm\";\nimport Cpmm from \"./cpmm/cpmm\";\nimport TradeV2 from \"./tradeV2/trade\";\nimport Utils1216 from \"./utils1216\";\nimport MarketV2 from \"./marketV2\";\nimport Ido from \"./ido\";\nimport Launchpad from \"./launchpad/launchpad\";\n\nimport TokenModule from \"./token/token\";\nimport { SignAllTransactions } from \"./type\";\n\nexport interface RaydiumLoadParams extends TokenAccountDataProp, Omit<RaydiumApiBatchRequestParams, \"api\"> {\n /* ================= solana ================= */\n // solana web3 connection\n connection: Connection;\n // solana cluster/network/env\n cluster?: Cluster;\n // user public key\n owner?: PublicKey | Keypair;\n /* ================= api ================= */\n // api request interval in ms, -1 means never request again, 0 means always use fresh data, default is 5 mins (5 * 60 * 1000)\n apiRequestInterval?: number;\n // api request timeout in ms, default is 10 secs (10 * 1000)\n apiRequestTimeout?: number;\n apiCacheTime?: number;\n signAllTransactions?: SignAllTransactions;\n urlConfigs?: API_URL_CONFIG;\n logRequests?: boolean;\n logCount?: number;\n jupTokenType?: JupTokenType;\n disableFeatureCheck?: boolean;\n disableLoadToken?: boolean;\n blockhashCommitment?: Commitment;\n loopMultiTxStatus?: boolean;\n}\n\nexport interface RaydiumApiBatchRequestParams {\n api: Api;\n defaultChainTimeOffset?: number;\n defaultChainTime?: number;\n}\n\nexport type RaydiumConstructorParams = Required<RaydiumLoadParams> & RaydiumApiBatchRequestParams;\n\ninterface DataBase<T> {\n fetched: number;\n data: T;\n extInfo?: Record<string, any>;\n}\ninterface ApiData {\n tokens?: DataBase<ApiV3Token[]>;\n\n // v3 data\n tokenList?: DataBase<ApiV3TokenRes>;\n jupTokenList?: DataBase<ApiV3Token[]>;\n}\n\nexport class Raydium {\n public cluster: Cluster;\n public farm: Farm;\n public account: Account;\n public liquidity: Liquidity;\n public clmm: Clmm;\n public cpmm: Cpmm;\n public tradeV2: TradeV2;\n public utils1216: Utils1216;\n public marketV2: MarketV2;\n public ido: Ido;\n public token: TokenModule;\n public launchpad: Launchpad;\n public rawBalances: Map<string, string> = new Map();\n public apiData: ApiData;\n public availability: Partial<AvailabilityCheckAPI3>;\n public blockhashCommitment: Commitment;\n public loopMultiTxStatus?: boolean;\n\n private _connection: Connection;\n private _owner: Owner | undefined;\n public api: Api;\n private _apiCacheTime: number;\n private _signAllTransactions?: SignAllTransactions;\n private logger: Logger;\n private _chainTime?: {\n fetched: number;\n value: {\n chainTime: number;\n offset: number;\n };\n };\n private _epochInfo?: {\n fetched: number;\n value: EpochInfo;\n };\n\n constructor(config: RaydiumConstructorParams) {\n const {\n connection,\n cluster,\n owner,\n api,\n defaultChainTime,\n defaultChainTimeOffset,\n apiCacheTime,\n blockhashCommitment = \"confirmed\",\n loopMultiTxStatus,\n } = config;\n\n this._connection = connection;\n this.cluster = cluster || \"mainnet\";\n this._owner = owner ? new Owner(owner) : undefined;\n this._signAllTransactions = config.signAllTransactions;\n this.blockhashCommitment = blockhashCommitment;\n this.loopMultiTxStatus = loopMultiTxStatus;\n\n this.api = api;\n this._apiCacheTime = apiCacheTime || 5 * 60 * 1000;\n this.logger = createLogger(\"Raydium\");\n this.farm = new Farm({ scope: this, moduleName: \"Raydium_Farm\" });\n this.account = new Account({\n scope: this,\n moduleName: \"Raydium_Account\",\n tokenAccounts: config.tokenAccounts,\n tokenAccountRawInfos: config.tokenAccountRawInfos,\n });\n this.liquidity = new Liquidity({ scope: this, moduleName: \"Raydium_LiquidityV2\" });\n this.token = new TokenModule({ scope: this, moduleName: \"Raydium_tokenV2\" });\n this.tradeV2 = new TradeV2({ scope: this, moduleName: \"Raydium_tradeV2\" });\n this.clmm = new Clmm({ scope: this, moduleName: \"Raydium_clmm\" });\n this.cpmm = new Cpmm({ scope: this, moduleName: \"Raydium_cpmm\" });\n this.utils1216 = new Utils1216({ scope: this, moduleName: \"Raydium_utils1216\" });\n this.marketV2 = new MarketV2({ scope: this, moduleName: \"Raydium_marketV2\" });\n this.ido = new Ido({ scope: this, moduleName: \"Raydium_ido\" });\n this.launchpad = new Launchpad({ scope: this, moduleName: \"Raydium_lauchpad\" });\n\n this.availability = {};\n const now = new Date().getTime();\n this.apiData = {};\n\n if (defaultChainTimeOffset)\n this._chainTime = {\n fetched: now,\n value: {\n chainTime: defaultChainTime || Date.now() - defaultChainTimeOffset,\n offset: defaultChainTimeOffset,\n },\n };\n }\n\n static async load(config: RaydiumLoadParams): Promise<Raydium> {\n const custom: Required<RaydiumLoadParams> = merge(\n // default\n {\n cluster: \"mainnet\",\n owner: null,\n apiRequestInterval: 5 * 60 * 1000,\n apiRequestTimeout: 10 * 1000,\n },\n config,\n );\n const { cluster, apiRequestTimeout, logCount, logRequests, urlConfigs } = custom;\n\n const api = new Api({ cluster, timeout: apiRequestTimeout, urlConfigs, logCount, logRequests });\n const raydium = new Raydium({\n ...custom,\n api,\n });\n\n await raydium.fetchAvailabilityStatus(config.disableFeatureCheck ?? true);\n if (!config.disableLoadToken)\n await raydium.token.load({\n type: config.jupTokenType,\n });\n\n return raydium;\n }\n\n get owner(): Owner | undefined {\n return this._owner;\n }\n get ownerPubKey(): PublicKey {\n if (!this._owner) throw new Error(EMPTY_OWNER);\n return this._owner.publicKey;\n }\n public setOwner(owner?: PublicKey | Keypair): Raydium {\n this._owner = owner ? new Owner(owner) : undefined;\n this.account.resetTokenAccounts();\n return this;\n }\n get connection(): Connection {\n if (!this._connection) throw new Error(EMPTY_CONNECTION);\n return this._connection;\n }\n public setConnection(connection: Connection): Raydium {\n this._connection = connection;\n return this;\n }\n get signAllTransactions(): SignAllTransactions | undefined {\n return this._signAllTransactions;\n }\n public setSignAllTransactions(signAllTransactions?: SignAllTransactions): Raydium {\n this._signAllTransactions = signAllTransactions;\n return this;\n }\n\n public checkOwner(): void {\n if (!this.owner) {\n console.error(EMPTY_OWNER);\n throw new Error(EMPTY_OWNER);\n }\n }\n\n private isCacheInvalidate(time: number): boolean {\n return new Date().getTime() - time > this._apiCacheTime;\n }\n\n public async fetchChainTime(): Promise<void> {\n try {\n const data = await this.api.getChainTimeOffset();\n this._chainTime = {\n fetched: Date.now(),\n value: {\n chainTime: Date.now() + data.offset * 1000,\n offset: data.offset * 1000,\n },\n };\n } catch {\n this._chainTime = undefined;\n }\n }\n\n public async fetchV3TokenList(forceUpdate?: boolean): Promise<ApiV3TokenRes> {\n if (this.apiData.tokenList && !this.isCacheInvalidate(this.apiData.tokenList.fetched) && !forceUpdate)\n return this.apiData.tokenList.data;\n try {\n const raydiumList = await this.api.getTokenList();\n const dataObject = {\n fetched: Date.now(),\n data: raydiumList,\n };\n this.apiData.tokenList = dataObject;\n\n return dataObject.data;\n } catch (e) {\n console.error(e);\n return {\n mintList: [],\n blacklist: [],\n whiteList: [],\n };\n }\n }\n\n public async fetchJupTokenList(forceUpdate?: boolean): Promise<ApiV3Token[]> {\n const prevFetched = this.apiData.jupTokenList;\n if (prevFetched && !this.isCacheInvalidate(prevFetched.fetched) && !forceUpdate) return prevFetched.data;\n try {\n const jupList = await this.api.getJupTokenList();\n this.apiData.jupTokenList = {\n fetched: Date.now(),\n data: jupList.map((t) => ({\n ...t,\n mintAuthority: t.mint_authority || undefined,\n freezeAuthority: t.freeze_authority || undefined,\n })),\n };\n\n return this.apiData.jupTokenList.data;\n } catch (e) {\n console.error(e);\n return [];\n }\n }\n\n get chainTimeData(): { offset: number; chainTime: number } | undefined {\n return this._chainTime?.value;\n }\n\n public async chainTimeOffset(): Promise<number> {\n if (this._chainTime && Date.now() - this._chainTime.fetched <= 1000 * 60 * 5) return this._chainTime.value.offset;\n await this.fetchChainTime();\n return this._chainTime?.value.offset || 0;\n }\n\n public async currentBlockChainTime(): Promise<number> {\n if (this._chainTime && Date.now() - this._chainTime.fetched <= 1000 * 60 * 5)\n return this._chainTime.value.chainTime;\n await this.fetchChainTime();\n return this._chainTime?.value.chainTime || Date.now();\n }\n\n public async fetchEpochInfo(): Promise<EpochInfo> {\n if (this._epochInfo && Date.now() - this._epochInfo.fetched <= 1000 * 30) return this._epochInfo.value;\n this._epochInfo = {\n fetched: Date.now(),\n value: await this.connection.getEpochInfo(),\n };\n return this._epochInfo.value;\n }\n\n public async fetchAvailabilityStatus(skipCheck?: boolean): Promise<Partial<AvailabilityCheckAPI3>> {\n if (skipCheck) return {};\n try {\n const data = await this.api.fetchAvailabilityStatus();\n const isAllDisabled = data.all === false;\n this.availability = {\n all: data.all,\n swap: isAllDisabled ? false : data.swap,\n createConcentratedPosition: isAllDisabled ? false : data.createConcentratedPosition,\n addConcentratedPosition: isAllDisabled ? false : data.addConcentratedPosition,\n addStandardPosition: isAllDisabled ? false : data.addStandardPosition,\n removeConcentratedPosition: isAllDisabled ? false : data.removeConcentratedPosition,\n removeStandardPosition: isAllDisabled ? false : data.removeStandardPosition,\n addFarm: isAllDisabled ? false : data.addFarm,\n removeFarm: isAllDisabled ? false : data.removeFarm,\n };\n return data;\n } catch {\n return {};\n }\n }\n}\n","import axios, { AxiosInstance } from \"axios\";\n\nimport { createLogger, sleep } from \"../common\";\nimport { Cluster } from \"../solana\";\n\nimport {\n ApiClmmConfigInfo,\n ApiCpmmConfigInfo,\n ApiV3Token,\n FetchPoolParams,\n PoolsApiReturn,\n ApiV3PoolInfoItem,\n PoolKeys,\n FormatFarmInfoOut,\n FormatFarmKeyOut,\n AvailabilityCheckAPI3,\n PoolFetchType,\n} from \"./type\";\nimport { API_URLS, API_URL_CONFIG } from \"./url\";\nimport { updateReqHistory } from \"./utils\";\nimport { PublicKey } from \"@solana/web3.js\";\nimport { solToWSol } from \"../common\";\n\nconst logger = createLogger(\"Raydium_Api\");\nconst poolKeysCache: Map<string, PoolKeys> = new Map();\n\nexport async function endlessRetry<T>(name: string, call: () => Promise<T>, interval = 1000): Promise<T> {\n let result: T | undefined;\n\n while (result == undefined) {\n try {\n logger.debug(`Request ${name} through endlessRetry`);\n result = await call();\n } catch (err) {\n logger.error(`Request ${name} failed, retry after ${interval} ms`, err);\n await sleep(interval);\n }\n }\n\n return result;\n}\n\nexport interface ApiProps {\n cluster: Cluster;\n timeout: number;\n logRequests?: boolean;\n logCount?: number;\n urlConfigs?: API_URL_CONFIG;\n}\n\nexport class Api {\n public cluster: Cluster;\n\n public api: AxiosInstance;\n public logCount: number;\n\n public urlConfigs: API_URL_CONFIG;\n\n constructor({ cluster, timeout, logRequests, logCount, urlConfigs }: ApiProps) {\n this.cluster = cluster;\n this.urlConfigs = urlConfigs || {};\n this.logCount = logCount || 1000;\n\n this.api = axios.create({ baseURL: this.urlConfigs.BASE_HOST || API_URLS.BASE_HOST, timeout });\n\n this.api.interceptors.request.use(\n (config) => {\n // before request\n const { method, baseURL, url } = config;\n\n logger.debug(`${method?.toUpperCase()} ${baseURL}${url}`);\n\n return config;\n },\n (error) => {\n // request error\n logger.error(`Request failed`);\n\n return Promise.reject(error);\n },\n );\n this.api.interceptors.response.use(\n (response) => {\n // 2xx\n const { config, data, status } = response;\n const { method, baseURL, url } = config;\n\n if (logRequests) {\n updateReqHistory({\n status,\n url: `${baseURL}${url}`,\n params: config.params,\n data,\n logCount: this.logCount,\n });\n }\n\n logger.debug(`${method?.toUpperCase()} ${baseURL}${url} ${status}`);\n\n return data;\n },\n (error) => {\n // https://axios-http.com/docs/handling_errors\n // not 2xx\n const { config, response = {} } = error;\n const { status } = response;\n const { method, baseURL, url } = config;\n\n if (logRequests) {\n updateReqHistory({\n status,\n url: `${baseURL}${url}`,\n params: config.params,\n data: error.message,\n logCount: this.logCount,\n });\n }\n\n logger.error(`${method.toUpperCase()} ${baseURL}${url} ${status || error.message}`);\n\n return Promise.reject(error);\n },\n );\n }\n\n async getClmmConfigs(): Promise<ApiClmmConfigInfo[]> {\n const res = await this.api.get(this.urlConfigs.CLMM_CONFIG || API_URLS.CLMM_CONFIG);\n return res.data;\n }\n\n async getCpmmConfigs(): Promise<ApiCpmmConfigInfo[]> {\n const res = await this.api.get(this.urlConfigs.CPMM_CONFIG || API_URLS.CPMM_CONFIG);\n return res.data;\n }\n\n async getClmmPoolLines(poolId: string): Promise<{ price: string; liquidity: string }[]> {\n const res = await this.api.get(\n `${this.urlConfigs.POOL_LIQUIDITY_LINE || API_URLS.POOL_LIQUIDITY_LINE}?pool_id=${poolId}`,\n );\n return res.data;\n }\n\n async getBlockSlotCountForSecond(endpointUrl?: string): Promise<number> {\n if (!endpointUrl) return 2;\n const res: {\n id: string;\n jsonrpc: string;\n result: { numSlots: number; numTransactions: number; samplePeriodSecs: number; slot: number }[];\n } = await axios.post(endpointUrl, {\n id: \"getRecentPerformanceSamples\",\n jsonrpc: \"2.0\",\n method: \"getRecentPerformanceSamples\",\n params: [4],\n });\n const slotList = res.result.map((data) => data.numSlots);\n return slotList.reduce((a, b) => a + b, 0) / slotList.length / 60;\n }\n\n async getChainTimeOffset(): Promise<{ offset: number }> {\n const res = await this.api.get(this.urlConfigs.CHAIN_TIME || API_URLS.CHAIN_TIME);\n return res.data;\n }\n\n async getRpcs(): Promise<{\n rpcs: { batch: boolean; name: string; url: string; weight: number }[];\n strategy: string;\n }> {\n return this.api.get(this.urlConfigs.RPCS || API_URLS.RPCS);\n }\n\n async getTokenList(): Promise<{ mintList: ApiV3Token[]; blacklist: string[]; whiteList: string[] }> {\n const res = await this.api.get(this.urlConfigs.TOKEN_LIST || API_URLS.TOKEN_LIST);\n return res.data;\n }\n\n async getJupTokenList(): Promise<\n (ApiV3Token & {\n daily_volume: number;\n freeze_authority: string | null;\n mint_authority: string | null;\n })[]\n > {\n return this.api.get(\"\", {\n baseURL: this.urlConfigs.JUP_TOKEN_LIST || API_URLS.JUP_TOKEN_LIST,\n });\n }\n\n async getTokenInfo(mint: (string | PublicKey)[]): Promise<ApiV3Token[]> {\n const res = await this.api.get(\n (this.urlConfigs.MINT_INFO_ID || API_URLS.MINT_INFO_ID) + `?mints=${mint.map((m) => m.toString()).join(\",\")}`,\n );\n return res.data;\n }\n\n async getPoolList(props: FetchPoolParams = {}): Promise<PoolsApiReturn> {\n const { type = \"all\", sort = \"liquidity\", order = \"desc\", page = 0, pageSize = 100 } = props;\n const res = await this.api.get<PoolsApiReturn>(\n (this.urlConfigs.POOL_LIST || API_URLS.POOL_LIST) +\n `?poolType=${type}&poolSortField=${sort}&sortType=${order}&page=${page}&pageSize=${pageSize}`,\n );\n return res.data;\n }\n\n async fetchPoolById(props: { ids: string }): Promise<ApiV3PoolInfoItem[]> {\n const { ids } = props;\n const res = await this.api.get((this.urlConfigs.POOL_SEARCH_BY_ID || API_URLS.POOL_SEARCH_BY_ID) + `?ids=${ids}`);\n return res.data;\n }\n\n async fetchPoolKeysById(props: { idList: string[] }): Promise<PoolKeys[]> {\n const { idList } = props;\n\n const cacheList: PoolKeys[] = [];\n\n const readyList = idList.filter((poolId) => {\n if (poolKeysCache.has(poolId)) {\n cacheList.push(poolKeysCache.get(poolId)!);\n return false;\n }\n return true;\n });\n\n let data: PoolKeys[] = [];\n if (readyList.length) {\n const res = await this.api.get<PoolKeys[]>(\n (this.urlConfigs.POOL_KEY_BY_ID || API_URLS.POOL_KEY_BY_ID) + `?ids=${readyList.join(\",\")}`,\n );\n data = res.data.filter(Boolean);\n data.forEach((poolKey) => {\n poolKeysCache.set(poolKey.id, poolKey);\n });\n }\n\n return cacheList.concat(data);\n }\n\n async fetchPoolByMints(\n props: {\n mint1: string | PublicKey;\n mint2?: string | PublicKey;\n } & Omit<FetchPoolParams, \"pageSize\">,\n ): Promise<PoolsApiReturn> {\n const {\n mint1: propMint1,\n mint2: propMint2,\n type = PoolFetchType.All,\n sort = \"default\",\n order = \"desc\",\n page = 1,\n } = props;\n\n const [mint1, mint2] = [\n propMint1 ? solToWSol(propMint1).toBase58() : propMint1,\n propMint2 && propMint2 !== \"undefined\" ? solToWSol(propMint2).toBase58() : \"\",\n ];\n const [baseMint, quoteMint] = mint2 && mint1 > mint2 ? [mint2, mint1] : [mint1, mint2];\n\n const res = await this.api.get(\n (this.urlConfigs.POOL_SEARCH_MINT || API_URLS.POOL_SEARCH_MINT) +\n `?mint1=${baseMint}&mint2=${quoteMint}&poolType=${type}&poolSortField=${sort}&sortType=${order}&pageSize=100&page=${page}`,\n );\n return res.data;\n }\n\n async fetchFarmInfoById(props: { ids: string }): Promise<FormatFarmInfoOut[]> {\n const { ids } = props;\n\n const res = await this.api.get<FormatFarmInfoOut[]>(\n (this.urlConfigs.FARM_INFO || API_URLS.FARM_INFO) + `?ids=${ids}`,\n );\n return res.data;\n }\n\n async fetchFarmKeysById(props: { ids: string }): Promise<FormatFarmKeyOut[]> {\n const { ids } = props;\n\n const res = await this.api.get<FormatFarmKeyOut[]>(\n (this.urlConfigs.FARM_KEYS || API_URLS.FARM_KEYS) + `?ids=${ids}`,\n );\n return res.data;\n }\n\n async fetchAvailabilityStatus(): Promise<AvailabilityCheckAPI3> {\n const res = await this.api.get<AvailabilityCheckAPI3>(\n this.urlConfigs.CHECK_AVAILABILITY || API_URLS.CHECK_AVAILABILITY,\n );\n return res.data;\n }\n}\n","import { AccountInfo, Commitment, Connection, PublicKey } from \"@solana/web3.js\";\nimport { ReturnTypeFetchMultipleMintInfos } from \"../raydium/type\";\nimport { WSOLMint, chunkArray, solToWSol } from \"./\";\nimport { createLogger } from \"./logger\";\nimport { MINT_SIZE, TOKEN_PROGRAM_ID, getTransferFeeConfig, unpackMint } from \"@solana/spl-token\";\n\ninterface MultipleAccountsJsonRpcResponse {\n jsonrpc: string;\n id: string;\n error?: {\n code: number;\n message: string;\n };\n result: {\n context: { slot: number };\n value: { data: Array<string>; executable: boolean; lamports: number; owner: string; rentEpoch: number }[];\n };\n}\n\nexport interface GetMultipleAccountsInfoConfig {\n batchRequest?: boolean;\n commitment?: Commitment;\n chunkCount?: number;\n}\n\nconst logger = createLogger(\"Raydium_accountInfo_util\");\n\nexport async function getMultipleAccountsInfo(\n connection: Connection,\n publicKeys: PublicKey[],\n config?: GetMultipleAccountsInfoConfig,\n): Promise<(AccountInfo<Buffer> | null)[]> {\n const {\n batchRequest,\n commitment = \"confirmed\",\n chunkCount = 100,\n } = {\n batchRequest: false,\n ...config,\n };\n\n const chunkedKeys = chunkArray(publicKeys, chunkCount);\n let results: (AccountInfo<Buffer> | null)[][] = new Array(chunkedKeys.length).fill([]);\n\n if (batchRequest) {\n const batch = chunkedKeys.map((keys) => {\n const args = connection._buildArgs([keys.map((key) => key.toBase58())], commitment, \"base64\");\n return {\n methodName: \"getMultipleAccounts\",\n args,\n };\n });\n\n const _batch = chunkArray(batch, 10);\n\n const unsafeResponse: MultipleAccountsJsonRpcResponse[] = await (\n await Promise.all(_batch.map(async (i) => await (connection as any)._rpcBatchRequest(i)))\n ).flat();\n results = unsafeResponse.map((unsafeRes: MultipleAccountsJsonRpcResponse) => {\n if (unsafeRes.error)\n logger.logWithError(`failed to get info for multiple accounts, RPC_ERROR, ${unsafeRes.error.message}`);\n\n return unsafeRes.result.value.map((accountInfo) => {\n if (accountInfo) {\n const { data, executable, lamports, owner, rentEpoch } = accountInfo;\n\n if (data.length !== 2 && data[1] !== \"base64\") logger.logWithError(`info must be base64 encoded, RPC_ERROR`);\n\n return {\n data: Buffer.from(data[0], \"base64\"),\n executable,\n lamports,\n owner: new PublicKey(owner),\n rentEpoch,\n };\n }\n return null;\n });\n });\n } else {\n try {\n results = (await Promise.all(\n chunkedKeys.map((keys) => connection.getMultipleAccountsInfo(keys, commitment)),\n )) as (AccountInfo<Buffer> | null)[][];\n } catch (error) {\n if (error instanceof Error) {\n logger.logWithError(`failed to get info for multiple accounts, RPC_ERROR, ${error.message}`);\n }\n }\n }\n\n return results.flat();\n}\n\nexport async function getMultipleAccountsInfoWithCustomFlags<T extends { pubkey: PublicKey }>(\n connection: Connection,\n publicKeysWithCustomFlag: T[],\n config?: GetMultipleAccountsInfoConfig,\n): Promise<({ accountInfo: AccountInfo<Buffer> | null } & T)[]> {\n const multipleAccountsInfo = await getMultipleAccountsInfo(\n connection,\n publicKeysWithCustomFlag.map((o) => o.pubkey),\n config,\n );\n\n return publicKeysWithCustomFlag.map((o, idx) => ({ ...o, accountInfo: multipleAccountsInfo[idx] }));\n}\n\nexport enum AccountType {\n Uninitialized,\n Mint,\n Account,\n}\nexport const ACCOUNT_TYPE_SIZE = 1;\n\nexport async function fetchMultipleMintInfos({\n connection,\n mints,\n config,\n}: {\n connection: Connection;\n mints: PublicKey[];\n config?: { batchRequest?: boolean };\n}): Promise<ReturnTypeFetchMultipleMintInfos> {\n if (mints.length === 0) return {};\n const mintInfos = await getMultipleAccountsInfoWithCustomFlags(\n connection,\n mints.map((i) => ({ pubkey: solToWSol(i) })),\n config,\n );\n\n const mintK: ReturnTypeFetchMultipleMintInfos = {};\n for (const i of mintInfos) {\n if (!i.accountInfo || i.accountInfo.data.length < MINT_SIZE) {\n console.log(\"invalid mint account\", i.pubkey.toBase58());\n continue;\n }\n const t = unpackMint(i.pubkey, i.accountInfo, i.accountInfo?.owner);\n mintK[i.pubkey.toString()] = {\n ...t,\n programId: i.accountInfo?.owner || TOKEN_PROGRAM_ID,\n feeConfig: getTransferFeeConfig(t) ?? undefined,\n };\n }\n mintK[PublicKey.default.toBase58()] = mintK[WSOLMint.toBase58()];\n\n return mintK;\n}\n","import { get, set } from \"lodash\";\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 Date.now().toString();\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 BN from \"bn.js\";\nimport Decimal from \"decimal.js\";\nimport { CurrencyAmount, TokenAmount } from \"../module/amount\";\nimport { Currency } from \"../module/currency\";\nimport { Fraction } from \"../module/fraction\";\nimport { Percent } from \"../module/percent\";\nimport { Price } from \"../module/price\";\nimport { Token } from \"../module/token\";\nimport { SplToken, TokenJson } from \"../raydium/token/type\";\nimport { ReplaceType } from \"../raydium/type\";\nimport { parseBigNumberish } from \"./constant\";\nimport { mul } from \"./fractionUtil\";\nimport { notInnerObject } from \"./utility\";\n\nexport const BN_ZERO = new BN(0);\nexport const BN_ONE = new BN(1);\nexport const BN_TWO = new BN(2);\nexport const BN_THREE = new BN(3);\nexport const BN_FIVE = new BN(5);\nexport const BN_TEN = new BN(10);\nexport const BN_100 = new BN(100);\nexport const BN_1000 = new BN(1000);\nexport const BN_10000 = new BN(10000);\nexport type BigNumberish = BN | string | number | bigint;\nexport type Numberish = number | string | bigint | Fraction | BN;\n\nexport function tenExponential(shift: BigNumberish): BN {\n return BN_TEN.pow(parseBigNumberish(shift));\n}\n\n/**\n *\n * @example\n * getIntInfo(0.34) => { numerator: '34', denominator: '100'}\n * getIntInfo('0.34') //=> { numerator: '34', denominator: '100'}\n */\nexport function parseNumberInfo(n: Numberish | undefined): {\n denominator: string;\n numerator: string;\n sign?: string;\n int?: string;\n dec?: string;\n} {\n if (n === undefined) return { denominator: \"1\", numerator: \"0\" };\n if (n instanceof BN) {\n return { numerator: n.toString(), denominator: \"1\" };\n }\n\n if (n instanceof Fraction) {\n return { denominator: n.denominator.toString(), numerator: n.numerator.toString() };\n }\n\n const s = String(n);\n const [, sign = \"\", int = \"\", dec = \"\"] = s.replace(\",\", \"\").match(/(-?)(\\d*)\\.?(\\d*)/) ?? [];\n const denominator = \"1\" + \"0\".repeat(dec.length);\n const numerator = sign + (int === \"0\" ? \"\" : int) + dec || \"0\";\n return { denominator, numerator, sign, int, dec };\n}\n\n// round up\nexport function divCeil(a: BN, b: BN): BN {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n const dm = a.divmod(b);\n\n // Fast case - exact division\n if (dm.mod.isZero()) return dm.div;\n\n // Round up\n return dm.div.isNeg() ? dm.div.isubn(1) : dm.div.iaddn(1);\n}\n\nexport function shakeFractionDecimal(n: Fraction): string {\n const [, sign = \"\", int = \"\"] = n.toFixed(2).match(/(-?)(\\d*)\\.?(\\d*)/) ?? [];\n return `${sign}${int}`;\n}\n\nexport function toBN(n: Numberish, decimal: BigNumberish = 0): BN {\n if (n instanceof BN) return n;\n return new BN(shakeFractionDecimal(toFraction(n).mul(BN_TEN.pow(new BN(String(decimal))))));\n}\n\nexport function toFraction(value: Numberish): Fraction {\n // to complete math format(may have decimal), not int\n if (value instanceof Percent) return new Fraction(value.numerator, value.denominator);\n\n if (value instanceof Price) return value.adjusted;\n\n // to complete math format(may have decimal), not BN\n if (value instanceof TokenAmount)\n try {\n return toFraction(value.toExact());\n } catch {\n return new Fraction(BN_ZERO);\n }\n\n // do not ideal with other fraction value\n if (value instanceof Fraction) return value;\n\n // wrap to Fraction\n const n = String(value);\n const details = parseNumberInfo(n);\n return new Fraction(details.numerator, details.denominator);\n}\n\nexport function ceilDiv(tokenAmount: BN, feeNumerator: BN, feeDenominator: BN): BN {\n return tokenAmount.mul(feeNumerator).add(feeDenominator).sub(new BN(1)).div(feeDenominator);\n}\n\nexport function floorDiv(tokenAmount: BN, feeNumerator: BN, feeDenominator: BN): BN {\n return tokenAmount.mul(feeNumerator).div(feeDenominator);\n}\n\n/**\n * @example\n * toPercent(3.14) // => Percent { 314.00% }\n * toPercent(3.14, { alreadyDecimaled: true }) // => Percent {3.14%}\n */\nexport function toPercent(\n n: Numberish,\n options?: { /* usually used for backend data */ alreadyDecimaled?: boolean },\n): Percent {\n const { numerator, denominator } = parseNumberInfo(n);\n return new Percent(new BN(numerator), new BN(denominator).mul(options?.alreadyDecimaled ? new BN(100) : new BN(1)));\n}\n\nexport function toTokenPrice(params: {\n token: TokenJson | Token | SplToken;\n numberPrice: Numberish;\n decimalDone?: boolean;\n}): Price {\n const { token, numberPrice, decimalDone } = params;\n const usdCurrency = new Token({ mint: \"\", decimals: 6, symbol: \"usd\", name: \"usd\", skipMint: true });\n const { numerator, denominator } = parseNumberInfo(numberPrice);\n const parsedNumerator = decimalDone ? new BN(numerator).mul(BN_TEN.pow(new BN(token.decimals))) : numerator;\n const parsedDenominator = new BN(denominator).mul(BN_TEN.pow(new BN(usdCurrency.decimals)));\n\n return new Price({\n baseToken: usdCurrency,\n denominator: parsedDenominator.toString(),\n quoteToken: new Token({ ...token, skipMint: true, mint: \"\" }),\n numerator: parsedNumerator.toString(),\n });\n}\n\nexport function toUsdCurrency(amount: Numberish): CurrencyAmount {\n const usdCurrency = new Currency({ decimals: 6, symbol: \"usd\", name: \"usd\" });\n const amountBigNumber = toBN(mul(amount, 10 ** usdCurrency.decimals)!);\n return new CurrencyAmount(usdCurrency, amountBigNumber);\n}\n\nexport function toTotalPrice(amount: Numberish | undefined, price: Price | undefined): CurrencyAmount {\n if (!price || !amount) return toUsdCurrency(0);\n return toUsdCurrency(mul(amount, price)!);\n}\n\nexport function decimalToFraction(n: Decimal | undefined): Fraction | undefined {\n if (n == null) return undefined;\n const { numerator, denominator } = parseNumberInfo(n.toString());\n return new Fraction(numerator, denominator);\n}\n\nexport function isDecimal(val: unknown): boolean {\n return val instanceof Decimal;\n}\n\nexport function recursivelyDecimalToFraction<T>(info: T): ReplaceType<T, Decimal, Fraction> {\n // @ts-expect-error no need type for inner code\n return isDecimal(info)\n ? decimalToFraction(info as any)\n : Array.isArray(info)\n ? info.map((k) => recursivelyDecimalToFraction(k))\n : notInnerObject(info)\n ? Object.fromEntries(Object.entries(info as any).map(([k, v]) => [k, recursivelyDecimalToFraction(v)]))\n : info;\n}\n","/*\r\n * decimal.js v10.3.1\r\n * An arbitrary-precision Decimal type for JavaScript.\r\n * https://github.com/MikeMcl/decimal.js\r\n * Copyright (c) 2021 Michael Mclaughlin <M8ch88l@gmail.com>\r\n * MIT Licence\r\n */\r\n\r\n\r\n// ----------------------------------- EDITABLE DEFAULTS ------------------------------------ //\r\n\r\n\r\n // The maximum exponent magnitude.\r\n // The limit on the value of `toExpNeg`, `toExpPos`, `minE` and `maxE`.\r\nvar EXP_LIMIT = 9e15, // 0 to 9e15\r\n\r\n // The limit on the value of `precision`, and on the value of the first argument to\r\n // `toDecimalPlaces`, `toExponential`, `toFixed`, `toPrecision` and `toSignificantDigits`.\r\n MAX_DIGITS = 1e9, // 0 to 1e9\r\n\r\n // Base conversion alphabet.\r\n NUMERALS = '0123456789abcdef',\r\n\r\n // The natural logarithm of 10 (1025 digits).\r\n LN10 = '2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058',\r\n\r\n // Pi (1025 digits).\r\n PI = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789',\r\n\r\n\r\n // The initial configuration properties of the Decimal constructor.\r\n DEFAULTS = {\r\n\r\n // These values must be integers within the stated ranges (inclusive).\r\n // Most of these values can be changed at run-time using the `Decimal.config` method.\r\n\r\n // The maximum number of significant digits of the result of a calculation or base conversion.\r\n // E.g. `Decimal.config({ precision: 20 });`\r\n precision: 20, // 1 to MAX_DIGITS\r\n\r\n // The rounding mode used when rounding to `precision`.\r\n //\r\n // ROUND_UP 0 Away from zero.\r\n // ROUND_DOWN 1 Towards zero.\r\n // ROUND_CEIL 2 Towards +Infinity.\r\n // ROUND_FLOOR 3 Towards -Infinity.\r\n // ROUND_HALF_UP 4 Towards nearest neighbour. If equidistant, up.\r\n // ROUND_HALF_DOWN 5 Towards nearest neighbour. If equidistant, down.\r\n // ROUND_HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour.\r\n // ROUND_HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity.\r\n // ROUND_HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity.\r\n //\r\n // E.g.\r\n // `Decimal.rounding = 4;`\r\n // `Decimal.rounding = Decimal.ROUND_HALF_UP;`\r\n rounding: 4, // 0 to 8\r\n\r\n // The modulo mode used when calculating the modulus: a mod n.\r\n // The quotient (q = a / n) is calculated according to the corresponding rounding mode.\r\n // The remainder (r) is calculated as: r = a - n * q.\r\n //\r\n // UP 0 The remainder is positive if the dividend is negative, else is negative.\r\n // DOWN 1 The remainder has the same sign as the dividend (JavaScript %).\r\n // FLOOR 3 The remainder has the same sign as the divisor (Python %).\r\n // HALF_EVEN 6 The IEEE 754 remainder function.\r\n // EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)). Always positive.\r\n //\r\n // Truncated division (1), floored division (3), the IEEE 754 remainder (6), and Euclidian\r\n // division (9) are commonly used for the modulus operation. The other rounding modes can also\r\n // be used, but they may not give useful results.\r\n modulo: 1, // 0 to 9\r\n\r\n // The exponent value at and beneath which `toString` returns exponential notation.\r\n // JavaScript numbers: -7\r\n toExpNeg: -7, // 0 to -EXP_LIMIT\r\n\r\n // The exponent value at and above which `toString` returns exponential notation.\r\n // JavaScript numbers: 21\r\n toExpPos: 21, // 0 to EXP_LIMIT\r\n\r\n // The minimum exponent value, beneath which underflow to zero occurs.\r\n // JavaScript numbers: -324 (5e-324)\r\n minE: -EXP_LIMIT, // -1 to -EXP_LIMIT\r\n\r\n // The maximum exponent value, above which overflow to Infinity occurs.\r\n // JavaScript numbers: 308 (1.7976931348623157e+308)\r\n maxE: EXP_LIMIT, // 1 to EXP_LIMIT\r\n\r\n // Whether to use cryptographically-secure random number generation, if available.\r\n crypto: false // true/false\r\n },\r\n\r\n\r\n// ----------------------------------- END OF EDITABLE DEFAULTS ------------------------------- //\r\n\r\n\r\n inexact, quadrant,\r\n external = true,\r\n\r\n decimalError = '[DecimalError] ',\r\n invalidArgument = decimalError + 'Invalid argument: ',\r\n precisionLimitExceeded = decimalError + 'Precision limit exceeded',\r\n cryptoUnavailable = decimalError + 'crypto unavailable',\r\n tag = '[object Decimal]',\r\n\r\n mathfloor = Math.floor,\r\n mathpow = Math.pow,\r\n\r\n isBinary = /^0b([01]+(\\.[01]*)?|\\.[01]+)(p[+-]?\\d+)?$/i,\r\n isHex = /^0x([0-9a-f]+(\\.[0-9a-f]*)?|\\.[0-9a-f]+)(p[+-]?\\d+)?$/i,\r\n isOctal = /^0o([0-7]+(\\.[0-7]*)?|\\.[0-7]+)(p[+-]?\\d+)?$/i,\r\n isDecimal = /^(\\d+(\\.\\d*)?|\\.\\d+)(e[+-]?\\d+)?$/i,\r\n\r\n BASE = 1e7,\r\n LOG_BASE = 7,\r\n MAX_SAFE_INTEGER = 9007199254740991,\r\n\r\n LN10_PRECISION = LN10.length - 1,\r\n PI_PRECISION = PI.length - 1,\r\n\r\n // Decimal.prototype object\r\n P = { toStringTag: tag };\r\n\r\n\r\n// Decimal prototype methods\r\n\r\n\r\n/*\r\n * absoluteValue abs\r\n * ceil\r\n * clampedTo clamp\r\n * comparedTo cmp\r\n * cosine cos\r\n * cubeRoot cbrt\r\n * decimalPlaces dp\r\n * dividedBy div\r\n * dividedToIntegerBy divToInt\r\n * equals eq\r\n * floor\r\n * greaterThan gt\r\n * greaterThanOrEqualTo gte\r\n * hyperbolicCosine cosh\r\n * hyperbolicSine sinh\r\n * hyperbolicTangent tanh\r\n * inverseCosine acos\r\n * inverseHyperbolicCosine acosh\r\n * inverseHyperbolicSine asinh\r\n * inverseHyperbolicTangent atanh\r\n * inverseSine asin\r\n * inverseTangent atan\r\n * isFinite\r\n * isInteger isInt\r\n * isNaN\r\n * isNegative isNeg\r\n * isPositive isPos\r\n * isZero\r\n * lessThan lt\r\n * lessThanOrEqualTo lte\r\n * logarithm log\r\n * [maximum] [max]\r\n * [minimum] [min]\r\n * minus sub\r\n * modulo mod\r\n * naturalExponential exp\r\n * naturalLogarithm ln\r\n * negated neg\r\n * plus add\r\n * precision sd\r\n * round\r\n * sine sin\r\n * squareRoot sqrt\r\n * tangent tan\r\n * times mul\r\n * toBinary\r\n * toDecimalPlaces toDP\r\n * toExponential\r\n * toFixed\r\n * toFraction\r\n * toHexadecimal toHex\r\n * toNearest\r\n * toNumber\r\n * toOctal\r\n * toPower pow\r\n * toPrecision\r\n * toSignificantDigits toSD\r\n * toString\r\n * truncated trunc\r\n * valueOf toJSON\r\n */\r\n\r\n\r\n/*\r\n * Return a new Decimal whose value is the absolute value of this Decimal.\r\n *\r\n */\r\nP.absoluteValue = P.abs = function () {\r\n var x = new this.constructor(this);\r\n if (x.s < 0) x.s = 1;\r\n return finalise(x);\r\n};\r\n\r\n\r\n/*\r\n * Return a new Decimal whose value is the value of this Decimal rounded to a whole number in the\r\n * direction of positive Infinity.\r\n *\r\n */\r\nP.ceil = function () {\r\n return finalise(new this.constructor(this), this.e + 1, 2);\r\n};\r\n\r\n\r\n/*\r\n * Return a new Decimal whose value is the value of this Decimal clamped to the range\r\n * delineated by `min` and `max`.\r\n *\r\n * min {number|string|Decimal}\r\n * max {number|string|Decimal}\r\n *\r\n */\r\nP.clampedTo = P.clamp = function (min, max) {\r\n var k,\r\n x = this,\r\n Ctor = x.constructor;\r\n min = new Ctor(min);\r\n max = new Ctor(max);\r\n if (!min.s || !max.s) return new Ctor(NaN);\r\n if (min.gt(max)) throw Error(invalidArgument + max);\r\n k = x.cmp(min);\r\n return k < 0 ? min : x.cmp(max) > 0 ? max : new Ctor(x);\r\n};\r\n\r\n\r\n/*\r\n * Return\r\n * 1 if the value of this Decimal is greater than the value of `y`,\r\n * -1 if the value of this Decimal is less than the value of `y`,\r\n * 0 if they have the same value,\r\n * NaN if the value of either Decimal is NaN.\r\n *\r\n */\r\nP.comparedTo = P.cmp = function (y) {\r\n var i, j, xdL, ydL,\r\n x = this,\r\n xd = x.d,\r\n yd = (y = new x.constructor(y)).d,\r\n xs = x.s,\r\n ys = y.s;\r\n\r\n // Either NaN or ±Infinity?\r\n if (!xd || !yd) {\r\n return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1;\r\n }\r\n\r\n // Either zero?\r\n if (!xd[0] || !yd[0]) return xd[0] ? xs : yd[0] ? -ys : 0;\r\n\r\n // Signs differ?\r\n if (xs !== ys) return xs;\r\n\r\n // Compare exponents.\r\n if (x.e !== y.e) return x.e > y.e ^ xs < 0 ? 1 : -1;\r\n\r\n xdL = xd.length;\r\n ydL = yd.length;\r\n\r\n // Compare digit by digit.\r\n for (i = 0, j = xdL < ydL ? xdL : ydL; i < j; ++i) {\r\n if (xd[i] !== yd[i]) return xd[i] > yd[i] ^ xs < 0 ? 1 : -1;\r\n }\r\n\r\n // Compare lengths.\r\n return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1;\r\n};\r\n\r\n\r\n/*\r\n * Return a new Decimal whose value is the cosine of the value in radians of this Decimal.\r\n *\r\n * Domain: [-Infinity, Infinity]\r\n * Range: [-1, 1]\r\n *\r\n * cos(0) = 1\r\n * cos(-0) = 1\r\n * cos(Infinity) = NaN\r\n * cos(-Infinity) = NaN\r\n * cos(NaN) = NaN\r\n *\r\n */\r\nP.cosine = P.cos = function () {\r\n var pr, rm,\r\n x = this,\r\n Ctor = x.constructor;\r\n\r\n if (!x.d) return new Ctor(NaN);\r\n\r\n // cos(0) = cos(-0) = 1\r\n if (!x.d[0]) return new Ctor(1);\r\n\r\n pr = Ctor.precision;\r\n rm = Ctor.rounding;\r\n Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;\r\n Ctor.rounding = 1;\r\n\r\n x = cosine(Ctor, toLessThanHalfPi(Ctor, x));\r\n\r\n Ctor.precision = pr;\r\n Ctor.rounding = rm;\r\n\r\n return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true);\r\n};\r\n\r\n\r\n/*\r\n *\r\n * Return a new Decimal whose value is the cube root of the value of this Decimal, rounded to\r\n * `precision` significant digits using rounding mode `rounding`.\r\n *\r\n * cbrt(0) = 0\r\n * cbrt(-0) = -0\r\n * cbrt(1) = 1\r\n * cbrt(-1) = -1\r\n * cbrt(N) =