haedal-vault-sdk 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +6 -5
- package/dist/index.js +16 -2
- package/dist/index.mjs +16 -2
- package/jest.config.js +10 -14
- package/package.json +10 -8
- package/tsup.config.js +9 -6
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CoinBalance } from '@mysten/sui/client';
|
|
2
2
|
import { AggregatorClient } from '@cetusprotocol/aggregator-sdk';
|
|
3
|
-
import { PaginationArgs, DataPage,
|
|
3
|
+
import { PaginationArgs, DataPage, SuiAddressType, FullClient, CoinAsset, SuiResource } from '@cetusprotocol/common-sdk';
|
|
4
|
+
import { Pool as Pool$1, CetusClmmSDK } from '@cetusprotocol/sui-clmm-sdk';
|
|
4
5
|
import { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions';
|
|
5
6
|
import * as _pythnetwork_price_service_sdk from '@pythnetwork/price-service-sdk';
|
|
6
7
|
import { SuiPriceServiceConnection, SuiPythClient } from '@pythnetwork/pyth-sui-js';
|
|
@@ -471,7 +472,7 @@ declare class PythPriceModule implements IModule {
|
|
|
471
472
|
* @returns A promise that resolves to a record mapping coin types to their respective price information.
|
|
472
473
|
*/
|
|
473
474
|
getLatestPrice(coinTypeList: string[], useCache?: boolean): Promise<Record<string, Price>>;
|
|
474
|
-
getPriceFeedsUpdateData(feedIds: string[]): Promise<Buffer[] | undefined>;
|
|
475
|
+
getPriceFeedsUpdateData(feedIds: string[]): Promise<Buffer<ArrayBufferLike>[] | undefined>;
|
|
475
476
|
getLatestPriceFeeds(feedIds: string[]): Promise<_pythnetwork_price_service_sdk.PriceFeed[] | undefined>;
|
|
476
477
|
/**
|
|
477
478
|
* Builds the payload to update Pyth price feeds with the provided coin types and transaction.
|
|
@@ -583,9 +584,9 @@ declare class VolatileVaultsSDK {
|
|
|
583
584
|
get PythPrice(): PythPriceModule;
|
|
584
585
|
/**
|
|
585
586
|
* Getter for the fullClient property.
|
|
586
|
-
* @returns {
|
|
587
|
+
* @returns {FullClient} The fullClient property value.
|
|
587
588
|
*/
|
|
588
|
-
get FullClient():
|
|
589
|
+
get FullClient(): FullClient;
|
|
589
590
|
/**
|
|
590
591
|
* Getter for the AggregatorClient instance.
|
|
591
592
|
* @returns {AggregatorClient} The AggregatorClient instance.
|
|
@@ -609,7 +610,7 @@ declare class VolatileVaultsSDK {
|
|
|
609
610
|
* @param coinType The type of the coin.
|
|
610
611
|
* @returns an array of coin assets.
|
|
611
612
|
*/
|
|
612
|
-
getOwnerCoinAssets(suiAddress: string, coinType?: string | null
|
|
613
|
+
getOwnerCoinAssets(suiAddress: string, coinType?: string | null): Promise<CoinAsset[]>;
|
|
613
614
|
/**
|
|
614
615
|
* Gets all coin balances for the given owner and coin type.
|
|
615
616
|
*
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
"use strict";var De=Object.create;var G=Object.defineProperty;var xe=Object.getOwnPropertyDescriptor;var Te=Object.getOwnPropertyNames;var Ie=Object.getPrototypeOf,Ee=Object.prototype.hasOwnProperty;var Le=(s,t)=>{for(var e in t)G(s,e,{get:t[e],enumerable:!0})},we=(s,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of Te(t))!Ee.call(s,i)&&i!==e&&G(s,i,{get:()=>t[i],enumerable:!(o=xe(t,i))||o.enumerable});return s};var U=(s,t,e)=>(e=s!=null?De(Ie(s)):{},we(t||!s||!s.__esModule?G(e,"default",{value:s,enumerable:!0}):e,s)),Ve=s=>we(G({},"__esModule",{value:!0}),s);var Ne={};Le(Ne,{PythPriceModule:()=>H,SdkEnv:()=>Se,VaultsModule:()=>W,VolatileVaultsSDK:()=>x,buildPoolData:()=>Y,buildVaultsBalance:()=>ce,calculateAum:()=>ae,calculateDepositRatioFixTokenA:()=>Pe,calculateTotalAumBasedOnQuote:()=>J,checkIsMinOrMaxIndex:()=>Q,convertU64BytesToDecimal:()=>Re,default:()=>$e,defaultProvider:()=>be,estCoinAmountsFromTotalAmount:()=>ne,feed_map_mainnet:()=>_e,feed_map_testnet:()=>ge,getCoinAmountsFromRatio:()=>M,getPositionPriceRange:()=>Ue,getPriceWithFormattedDecimals:()=>le,getPrimitivePricesFromBaseQuote:()=>Ce,getQuotePerBaseAndBasePerQuote:()=>re,getShareBufferAssets:()=>N,getUpdatePriceType:()=>de,getVaultValidBalances:()=>Z,get_lp_amount_by_liquidity:()=>je,get_lp_amount_by_tvl:()=>se,get_share_liquidity_by_amount:()=>j,initMainnetSDK:()=>fe,initTestnetSDK:()=>he,initVaultSDK:()=>Me,oraclePriceMultiplierDecimal:()=>q,printTransaction:()=>qe,toSuiObjectId:()=>ue,vaults_mainnet:()=>pe,vaults_testnet:()=>me});module.exports=Ve(Ne);var Fe=require("@mysten/sui/client"),ee=require("@mysten/sui/utils"),X=require("@cetusprotocol/aggregator-sdk"),E=require("@cetusprotocol/cetus-sui-clmm-sdk");var K=require("@mysten/sui/transactions"),D=U(require("bn.js")),Ae=require("@cetusprotocol/aggregator-sdk"),p=require("@cetusprotocol/cetus-sui-clmm-sdk"),ve=require("@mysten/sui/utils"),ke=U(require("decimal.js"));var S=U(require("bn.js")),z=U(require("decimal.js")),g=require("@cetusprotocol/cetus-sui-clmm-sdk");var k=require("@cetusprotocol/cetus-sui-clmm-sdk"),oe=U(require("bn.js")),ie=U(require("decimal.js")),Q=(s,t,e)=>{if(t==="min"){if(s===k.TickUtil.getMinIndex(e))return!0}else if(s===k.TickUtil.getMaxIndex(e))return!0;return!1};function Re(s){if(s.length!==8)throw new Error("Invalid u64 byte array length");let t=s.map(e=>e.toString(16).padStart(2,"0")).reverse().join("");return BigInt(`0x${t}`).toString()}function Pe(s,t,e,o,i){let n=new oe.default(10).pow(new oe.default(o)),{coinAmountB:r}=k.ClmmPoolUtil.estLiquidityAndcoinAmountFromOneAmounts(s,t,n,!0,!0,0,e),a=k.TickMath.sqrtPriceX64ToPrice(e,o,i),c=(0,k.d)(n.toString()).mul(a).div(new ie.default(10).pow(o)).mul(new ie.default(10).pow(i)),d=c.add(r.toString()),u=c.div(d),_=(0,k.d)(r.toString()).div(d);return{ratioA:u,ratioB:_}}function M(s,t,e,o,i,n,r){let a=(0,k.d)((0,k.toDecimalsAmount)((0,k.d)(e).mul(s).div(o).toString(),n)).toFixed(0),c=(0,k.d)((0,k.toDecimalsAmount)((0,k.d)(e).mul(t).div(i).toString(),r)).toFixed(0);return{amountA:a,amountB:c}}function ne(s,t,e,o,i,n,r,a){let{ratioA:c,ratioB:d}=Pe(s,t,e,r,a);return M(c,d,o,i,n,r,a)}var q=10n;function Y(s){let{fields:t,type:e}=s,{liquidity_range:o,pool_id:i,wrapped_position:n}=t.clmm_vault.fields,r=n.fields,a={clmm_pool_id:i,liquidity_range:{lower_offset:o.fields.lower_offset,rebalance_threshold:o.fields.rebalance_threshold,upper_offset:o.fields.upper_offset},wrapped_position:{coin_type_a:(0,g.fixCoinType)(r.coin_type_a.fields.name,!1),coin_type_b:(0,g.fixCoinType)(r.coin_type_b.fields.name,!1),description:r.description,id:r.id.id,index:r.index,liquidity:r.liquidity,name:r.name,pool:r.pool,tick_lower_index:(0,g.asIntN)(BigInt(r.tick_lower_index.fields.bits)),tick_upper_index:(0,g.asIntN)(BigInt(r.tick_upper_index.fields.bits)),url:r.url}},{balances:c}=t.buffer_assets.fields,d=c.fields.contents.map(_=>{let{key:l,value:P}=_.fields;return{coin_type:(0,g.fixCoinType)(l.fields.name,!1),value:P}});return{id:t.id.id,is_pause:t.is_pause,clmm_vault:a,lp_token_treasury:t.lp_token_treasury.fields.total_supply.fields.value,protocol_fees_handle:t.protocol_fees.fields.id.id,hard_cap:t.hard_cap,quote_type:(0,g.extractStructTagFromType)(t.quote_type.fields.name).full_address,lp_token_type:(0,g.extractStructTagFromType)(e).type_arguments[0],balances:d,protocol_fee_rate:t.protocol_fee_rate}}async function qe(s,t=!0){console.log("inputs",s.getData().inputs),s.getData().commands.forEach((e,o)=>{t&&console.log(`transaction ${o}: `,e)})}function re(s,t){let e=(0,g.d)(s).div(t).toString(),o=(0,g.d)(t).div(s).toString();return{quote_per_base:e,base_per_quote:o}}function Ce(s,t,e,o){let i=10n**(q+o-e),n=10n**(q+e-o),r=g.MathUtil.checkMulDivFloor(new S.default(s.toString()),new S.default(i.toString()),new S.default(t.toString()),64),a=g.MathUtil.checkMulDivFloor(new S.default(t.toString()),new S.default(n.toString()),new S.default(s.toString()),64);return{primitive_base_price:BigInt(r.toString()),primitive_quote_price:BigInt(a.toString())}}function J(s,t){let e=t?s[t].price:{coin_type:"",price:"1",coin_decimals:6,oracle_price:1000000000000000000000000n},o=new S.default("0");for(let[i,{price:n,value:r}]of Object.entries(s)){let{primitive_quote_price:a}=Ce(BigInt(e.oracle_price),BigInt(n.oracle_price),BigInt(e.coin_decimals),BigInt(n.coin_decimals)),c=g.MathUtil.checkMulDivFloor(new S.default(a.toString()),new S.default(r),new S.default((BigInt(10)**q).toString()),64);o=o.add(c)}return o.toString()}async function ae(s,t,e,o,i=!1){let{clmm_vault:n,quote_type:r}=t,{wrapped_position:a}=n,{tick_lower_index:c,tick_upper_index:d,liquidity:u}=a,{current_sqrt_price:_,coinTypeA:l,coinTypeB:P}=e,f=[...o],m=r&&(0,g.fixCoinType)(t.quote_type,!1),w=g.TickMath.tickIndexToSqrtPriceX64(c),h=g.TickMath.tickIndexToSqrtPriceX64(d),{coinA:y,coinB:b}=g.ClmmPoolUtil.getCoinAmountFromLiquidity(new S.default(u),new S.default(_),new S.default(w),new S.default(h),!1);f.push({coin_type:l,value:y.toString()}),f.push({coin_type:P,value:b.toString()});let C={};f.forEach(v=>{let A=(0,g.fixCoinType)(v.coin_type,!1),V=(0,g.d)(v.value),R=C[A]||0;C[A]=V.add(R).toString()});let F=Object.entries(C).filter(([v,A])=>(0,g.d)(A).gt(0)).map(([v])=>v);if(m&&!F.includes(m)&&F.push(m),F.length===0)return"0";let T=await s.sdk.PythPrice.getLatestPrice(F,i),I={};return F.forEach(v=>{let A=T[v];if(A)I[v]={price:A,value:C[v]||"0"};else throw Error(`find ${v} price fail `)}),J(I,m).toString()}function se(s,t,e){return(0,g.d)(s).eq(0)?t:(0,g.d)(s).mul(t).div(e).toFixed(0,z.default.ROUND_HALF_UP)}function je(s,t,e){return(0,g.d)(s).eq(0)?"0":(0,g.d)(s).mul(t).div(e).toFixed(0,z.default.ROUND_HALF_UP)}function j(s,t,e){return(0,g.d)(s).eq(0)?"0":(0,g.d)(t).mul(e).div(s).toFixed(0,z.default.ROUND_HALF_UP)}function N(s,t){let{balances:e,lp_token_treasury:o}=t,i={};return e.forEach(n=>{let r=j(o,s,n.value),a={coin_type:n.coin_type,value:r};i[a.coin_type]=a}),i}function ce(s,t,e,o){let{tick_lower_index:i,tick_upper_index:n,coin_type_a:r,coin_type_b:a,liquidity:c}=e.clmm_vault.wrapped_position,d=j(e.lp_token_treasury,t,c),u=g.TickMath.tickIndexToSqrtPriceX64(i),_=g.TickMath.tickIndexToSqrtPriceX64(n),l=g.ClmmPoolUtil.getCoinAmountFromLiquidity(new S.default(d),new S.default(o.current_sqrt_price),u,_,!0),P=l.coinA.toString(),f=l.coinB.toString(),m=N(t,e);console.log("\u{1F680} ~ buildVaultsBalance:",{share_buffer_assets:m,liquidity_amount_a:P,liquidity_amount_b:f,token_amount:t,pool:e});let w=(0,g.d)(P).add(m[r]?.value||"0").toFixed(0),h=(0,g.d)(f).add(m[a]?.value||"0").toFixed(0);return{vault_id:e.id,clmm_pool_id:o.poolAddress,owner:s,lp_token_type:e.lp_token_type,lp_token_balance:t,liquidity:d,tick_lower_index:i,tick_upper_index:n,amount_a:w,amount_b:h,coin_type_a:r,coin_type_b:a}}function Z(s,t,e){let o=[];return t.forEach(i=>{(0,g.d)(i.value).gt(0)&&o.push(i)}),e.forEach(i=>{(0,g.d)(i.value).gt(0)&&o.push(i)}),s.balances.forEach(i=>{(0,g.d)(i.value).gt(0)&&o.push(i)}),o}function de(s,t,e){let{coin_type_a:o,coin_type_b:i}=s.clmm_vault.wrapped_position,n=Z(s,t,e),r=new Set;return n.forEach(a=>r.add(a.coin_type)),r.add(o),r.add(i),Array.from(r)}function le(s,t){if(!q)throw new Error("oraclePriceMultiplierDecimal is required");if(s===0n)throw new Error("Invalid oracle price");if(t<0n){let e=10n**(q- -t);return s*e}return s/10n**(t+q)}function ue(s){return`0x${Buffer.from(s).toString("hex")}`}function Ue(s,t,e,o,i){let n="0",r="0";return Q(s,"min",Number(e))?n="0":n=g.TickMath.tickIndexToPrice(Number(s),o,i).toString(),Q(t,"max",Number(e))?r="\u221E":r=g.TickMath.tickIndexToPrice(Number(t),o,i).toString(),{minPrice:n,maxPrice:r}}var W=class{_sdk;constructor(t){this._sdk=t}get sdk(){return this._sdk}async getProtocolFeeRate(){let t="getProtocolFeeRate",e=this._sdk.getCache(t);if(e)return e;let{configs:o}=this._sdk.sdkOptions.vaults,n=(await this._sdk.FullClient.getObject({id:o.global_config_id,options:{showContent:!0}})).data.content.fields.protocol_fee_rate;return this._sdk.updateCache(t,n),n}async getOwnerVaultsBalance(t,e){let{pool:o,clmmPool:i}=await this.getVaultAndPool(e,!0),r=(await this._sdk.FullClient.getBalance({owner:t,coinType:o.lp_token_type})).totalBalance;return ce(t,r,o,i)}async getOwnerVaultsBalanceList(t){let{data:e}=await this.getPoolList(),o=[];for(let i=0;i<e.length;i++){let n=await this.getOwnerVaultsBalance(t,e[i].id);o.push(n)}return o}async getVaultsPositionRewarderAndFee(t){try{let{package_id:e}=this._sdk.sdkOptions.vaults,{pool:o,clmmPool:i}=await this.getVaultAndPool(t,!1),n=`getVaultsPositionRewarderAndFee_${t}`,r=this._sdk.getCache(n);if(r)return r;let{lp_token_type:a}=o,{rewarder_infos:c,poolAddress:d,coinTypeA:u,coinTypeB:_}=i,l=new K.Transaction;this.buildCollectRewardPayload({reward_coin_types:[...c.map(y=>y.coinAddress)],lp_token_type:a,pool_id:t,clmm_pool_id:d,coin_type_a:u,coin_type_b:_},l),this.buildCollectFeePayload({lp_token_type:a,pool_id:t,clmm_pool_id:d,coin_type_a:u,coin_type_b:_},l);let P=await this._sdk.FullClient.sendSimulationTransaction(l,(0,ve.normalizeSuiAddress)("0x0")),f=`${e}::pool::FeeClaimedEvent`,m=`${e}::pool::RewardClaimedEvent`,w=[],h=[];return P?.events.forEach(y=>{let{parsedJson:b,type:C}=y;C===f&&(w.push({coin_type:(0,p.fixCoinType)(u,!1),value:b.amount_a}),w.push({coin_type:(0,p.fixCoinType)(_,!1),value:b.amount_b})),C===m&&h.push({coin_type:(0,p.fixCoinType)(b.reward_type.name,!1),value:b.amount})}),this._sdk.updateCache(n,{feeList:w,rewarderList:h},10*1e3),{feeList:w,rewarderList:h}}catch(e){console.log("\u{1F680} ~ VaultsModule ~ getVaultsPositionRewarderAndFee ~ error:",e)}return{feeList:[],rewarderList:[]}}async getPoolList(t="all"){let e={data:[],hasNextPage:!1},{vaults:o}=this._sdk.sdkOptions,{FullClient:i}=this._sdk,n=await i.getDynamicFieldsByPage(o.configs.pools_handle,t),r=n.data.map(a=>a.name.value);return r.length>0&&((await i.batchGetObjects(r,{showContent:!0})).forEach(c=>{let d=Y(c.data.content);this._sdk.updateCache(`getPool_${d.id}`,d),e.data.push(d)}),e.nextCursor=n.nextCursor,e.hasNextPage=n.hasNextPage),e}async getPool(t,e=!0){let o=`getPool_${t}`,i=this._sdk.getCache(o,e);if(i)return i;let n=await this._sdk.FullClient.getObject({id:t,options:{showContent:!0}}),r=Y(n.data.content);return this._sdk.updateCache(o,r),r}buildCollectRewardPayload(t,e){let{coin_type_a:o,coin_type_b:i,pool_id:n,clmm_pool_id:r,lp_token_type:a,reward_coin_types:c}=t,{published_at:d,configs:u,clmm_configs:_}=this._sdk.sdkOptions.vaults;return e=e||new K.Transaction,c.forEach(l=>{e.moveCall({target:`${d}::pool::collect_reward`,typeArguments:[o,i,a,l],arguments:[e.object(n),e.object(u.global_config_id),e.object(_.global_config_id),e.object(r),e.object(_.global_vault_id),e.object(p.CLOCK_ADDRESS)]})}),e}buildCollectFeePayload(t,e){let{coin_type_a:o,coin_type_b:i,pool_id:n,clmm_pool_id:r,lp_token_type:a}=t,{published_at:c,configs:d,clmm_configs:u}=this._sdk.sdkOptions.vaults;return e=e||new K.Transaction,e.moveCall({target:`${c}::pool::collect_fee`,typeArguments:[o,i,a],arguments:[e.object(n),e.object(d.global_config_id),e.object(u.global_config_id),e.object(r)]}),e}async buildDepositPayload(t,e){let{pool_id:o,amount_a:i,amount_b:n,return_lp_coin:r}=t,{pool:a,clmmPool:c}=await this.getVaultAndPool(o,!0),{rewarderList:d,feeList:u}=await this.getVaultsPositionRewarderAndFee(o),_=de(a,d,u),{lp_token_type:l}=a,{rewarder_infos:P,poolAddress:f,coinTypeA:m,coinTypeB:w}=c,h=d.length>0?d.filter(A=>(0,p.d)(A.value.toString()).gt(0)).map(A=>(0,p.fixCoinType)(A.coin_type,!1)):P.map(A=>(0,p.fixCoinType)(A.coinAddress,!1)),{published_at:y,configs:b,clmm_configs:C}=this._sdk.sdkOptions.vaults;e=e||new K.Transaction,e=this.buildCollectRewardPayload({reward_coin_types:h,lp_token_type:l,pool_id:o,clmm_pool_id:f,coin_type_a:m,coin_type_b:w},e),e=this.buildCollectFeePayload({lp_token_type:l,pool_id:o,clmm_pool_id:f,coin_type_a:m,coin_type_b:w},e),e=await this._sdk.PythPrice.buildUpdatePythPricePayload(_,e),e.moveCall({target:`${y}::pool::calculate_aum`,typeArguments:[m,w,l],arguments:[e.object(o),e.object(b.global_config_id),e.object(b.pyth_oracle_id),e.object(C.global_config_id),e.object(f),e.object(p.CLOCK_ADDRESS)]});let F=await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress,m),T=await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress,w),I=p.TransactionUtil.buildCoinForAmount(e,F,BigInt(i),m,!1,!0),L=p.TransactionUtil.buildCoinForAmount(e,T,BigInt(n),w,!1,!0),v=e.moveCall({target:`${y}::pool::${r?"deposit_for":"deposit"}`,typeArguments:[m,w,l],arguments:[e.object(o),e.object(b.global_config_id),e.object(b.pyth_oracle_id),e.object(C.global_config_id),e.object(f),I.targetCoin,L.targetCoin,e.object(p.CLOCK_ADDRESS)]});return r?v:void 0}async buildWithdrawPayload(t,e){let{pool_id:o,burn_lp_amount:i,mode:n,slippage:r,partner:a}=t,{pool:c,clmmPool:d}=await this.getVaultAndPool(o,!0),{rewarderList:u}=await this.getVaultsPositionRewarderAndFee(o),{published_at:_,configs:l,clmm_configs:P}=this._sdk.sdkOptions.vaults;e=e||new K.Transaction;let{lp_token_type:f,clmm_vault:m,lp_token_treasury:w}=c,{rewarder_infos:h,poolAddress:y,coinTypeA:b,coinTypeB:C,liquidity:F}=d,T=u.length>0?u.filter(O=>(0,p.d)(O.value.toString()).gt(0)).map(O=>(0,p.fixCoinType)(O.coin_type,!1)):h.map(O=>(0,p.fixCoinType)(O.coinAddress,!1));e=this.buildCollectRewardPayload({reward_coin_types:T,lp_token_type:f,pool_id:o,clmm_pool_id:y,coin_type_a:b,coin_type_b:C},e),e=this.buildCollectFeePayload({lp_token_type:f,pool_id:o,clmm_pool_id:y,coin_type_a:b,coin_type_b:C},e);let I=await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress,f),L=p.TransactionUtil.buildCoinForAmount(e,I,BigInt(i),f,!1,!0),[v,A,V]=e.moveCall({target:`${_}::pool::withdraw`,typeArguments:[b,C,f],arguments:[e.object(o),e.object(l.global_config_id),e.object(P.global_config_id),e.object(y),L.targetCoin,e.object(p.CLOCK_ADDRESS)]}),R=this.buildWithdrawBufferReward({reward_coin_types:T.filter(O=>![(0,p.fixCoinType)(b,!1),(0,p.fixCoinType)(C,!1)].includes(O)),pool_id:o,withdraw_cert:V,lp_token_type:f},e);if(e.moveCall({target:`${_}::pool::destory_withdraw_cert`,typeArguments:[f],arguments:[e.object(o),e.object(l.global_config_id),V]}),n!=="FixedOneSide"){if(r===void 0)throw Error("the slippage is undefined");let O=n==="OnlyCoinA",te=O?A:v,Be=j(w,i,F.toString());await this.handleWithSwap(e,te,b,C,i,o,O,r,a,[{poolID:y,ticklower:m.wrapped_position.tick_lower_index,tickUpper:m.wrapped_position.tick_upper_index,deltaLiquidity:Number(Be)}]),e.transferObjects([O?v:A,...R],this._sdk.senderAddress)}else e.transferObjects([v,A,...R],this._sdk.senderAddress);return e}async handleWithSwap(t,e,o,i,n,r,a,c,d,u){let{receive_amount_a:_,receive_amount_b:l}=await this.preCalculateWithdrawAmount({pool_id:r,mode:"FixedOneSide",burn_lp_amount:n}),P=a?i:o,f=a?o:i,m=a?l:_,w={from:P,target:f,amount:new D.default(m),byAmountIn:!0,depth:3,splitAlgorithm:void 0,splitFactor:void 0,splitCount:void 0,providers:this._sdk.sdkOptions.providers,liquidityChanges:u},h=await this._sdk.AggregatorClient.findRouters(w);if(!h)throw Error("not find Router");if(h.error!==void 0)throw Error(`find Router error : ${h.error}`);let y={routers:h,inputCoin:e,slippage:c,txb:t,partner:d},b=await this._sdk.AggregatorClient.fixableRouterSwap(y);t.transferObjects([b],t.pure.address(this._sdk.getVerifySenderAddress()))}buildWithdrawBufferReward({reward_coin_types:t,pool_id:e,withdraw_cert:o,lp_token_type:i},n){let{published_at:r,configs:a}=this._sdk.sdkOptions.vaults;return t.map(c=>n.moveCall({target:`${r}::pool::withdraw_buffer_reward`,typeArguments:[i,c],arguments:[n.object(e),n.object(a.global_config_id),o]}))}async preCalculateDepositValue(t,e){let{amount_usd:o,pool_id:i,mode:n,uuid:r}=t,{pool:a,clmmPool:c}=await this.getVaultAndPool(i,!1),{current_sqrt_price:d}=c,{tick_lower_index:u,tick_upper_index:_,coin_type_a:l,coin_type_b:P}=a.clmm_vault.wrapped_position,f="0",m="0",w=await this._sdk.PythPrice.getLatestPrice([l,P]);console.log("\u{1F680} ~ VaultsModule ~ preCalculateDepositValue ~ priceMap:",w);let h=w[l],y=w[P];if(!h||!y)throw new Error("get coin price failed");switch(n){case"FixedOneSide":{let{amountA:b,amountB:C}=ne(u,_,new D.default(d),o,h.price,y.price,h.coin_decimals,y.coin_decimals);f=b.toString(),m=C.toString();break}case"FlexibleBoth":{let{amountA:b,amountB:C}=M((0,p.d)(.5),(0,p.d)(.5),o,h.price,y.price,h.coin_decimals,y.coin_decimals);f=b.toString(),m=C.toString();break}case"OnlyCoinA":{let{amountA:b,amountB:C}=M((0,p.d)(1),(0,p.d)(0),o,h.price,y.price,h.coin_decimals,y.coin_decimals);f=b.toString(),m=C.toString();break}case"OnlyCoinB":{let{amountA:b,amountB:C}=M((0,p.d)(0),(0,p.d)(1),o,h.price,y.price,h.coin_decimals,y.coin_decimals);f=b.toString(),m=C.toString();break}default:throw new Error(`Unsupported deposit mode: ${n}`)}return e&&this.calculateDepositLpAmountBoth(a,c,f,m).then(b=>{e.onSuccess(b,r)}).catch(b=>{e.onError(b,r)}),{deposit_amount_a:f,deposit_amount_b:m,mode:n}}async preCalculateDepositAmount(t,e){let{mode:o,pool_id:i,uuid:n}=t,r="0",a="0",{pool:c,clmmPool:d}=await this.getVaultAndPool(i,!1);switch(o){case"FixedOneSide":{let{fixed_amount:u,fixed_coin_a:_}=t,{other_amount:l}=this.calculateDepositOtherSide(c,d,u,_,e,n);_?(r=u,a=l):(a=u,r=l);break}case"FlexibleBoth":{let{coin_amount_a:u,coin_amount_b:_}=t;r=u,a=_,this.calculateDepositLpAmountBoth(c,d,u,_).then(l=>{e.onSuccess(l,n)}).catch(l=>{e.onError(l,n)});break}case"OnlyCoinA":{r=t.coin_amount_a,this.calculateDepositLpAmountBoth(c,d,r,"0").then(u=>{e.onSuccess(u,n)}).catch(u=>{e.onError(u,n)});break}case"OnlyCoinB":{a=t.coin_amount_b,this.calculateDepositLpAmountBoth(c,d,"0",a).then(u=>{e.onSuccess(u,n)}).catch(u=>{e.onError(u,n)});break}default:throw new Error(`Unsupported deposit mode: ${o}`)}return{deposit_amount_a:r,deposit_amount_b:a,mode:o}}async getVaultAndPool(t,e=!1){let o=await this.getPool(t,e);if(o===void 0)throw new Error("get vaults pool failed");let i=await this._sdk.CetusClmmSDK.Pool.getPool(o.clmm_vault.clmm_pool_id,e);if(i===void 0)throw new Error("get vaults clmmPool failed");return{clmmPool:i,pool:o}}calculateDepositOtherSide(t,e,o,i,n,r){let{clmm_vault:a}=t,{current_sqrt_price:c}=e,d=a.wrapped_position.tick_lower_index,u=a.wrapped_position.tick_upper_index,_=p.ClmmPoolUtil.estLiquidityAndcoinAmountFromOneAmounts(d,u,new D.default(o),i,!0,1,new D.default(c)),{coinAmountA:l,coinAmountB:P}=_;return this.calculateDepositLpAmountBoth(t,e,l.toString(),P.toString()).then(f=>{n.onSuccess(f,r)}).catch(f=>{n.onError(f,r)}),{other_amount:i?P.toString():l.toString()}}async calculateDepositLpAmountBoth(t,e,o,i){let{lp_token_treasury:n,hard_cap:r}=t,a=(0,p.fixCoinType)(e.coinTypeA,!1),c=(0,p.fixCoinType)(e.coinTypeB,!1),{rewarderList:d,feeList:u}=await this.getVaultsPositionRewarderAndFee(t.id),_=Z(t,u,d),l=await ae(this,t,e,_,!0),P=await this._sdk.PythPrice.getLatestPrice([a,c],!0),f=P[a],m=P[c];if(console.log("\u{1F680} ~ VaultsModule ~ calculateDepositLpAmountBoth ~ price:",{price_a:f,price_b:m}),!f)throw Error(`not find ${a} price`);if(!m)throw Error(`not find ${c} price`);let w={};w[a]={price:f,value:o.toString()},w[c]={price:m,value:i.toString()};let h=J(w,t.quote_type),y=se(n,h,l);return console.log("\u{1F680} ~ VaultsModule ~ calculateDepositLpAmountBoth :",{lp_token_treasury:n,userTvl:h,aum:l,lp_amount:y}),y}async preCalculateWithdrawAmount(t){let{mode:e,pool_id:o}=t,i="0",n="0",r="0",a="0",c,{pool:d,clmmPool:u}=await this.getVaultAndPool(o,!1);switch(e){case"FixedOneSide":let _=await this.calculateWithdrawOtherSide(d,u,t.burn_lp_amount);r=_.burn_lp_amount,i=_.receive_amount_a,n=_.receive_amount_b,a=_.remove_liquidity;break;case"OnlyCoinA":{let l=await this.calculateWithdrawOnlyCoin(d,u,t.burn_lp_amount,!0);r=l.burn_lp_amount,i=l.receive_amount_a,n=l.receive_amount_b,a=l.remove_liquidity,c=l.swap;break}case"OnlyCoinB":{let l=await this.calculateWithdrawOnlyCoin(d,u,t.burn_lp_amount,!1);r=l.burn_lp_amount,i=l.receive_amount_a,n=l.receive_amount_b,a=l.remove_liquidity,c=l.swap;break}default:throw new Error(`Unsupported withdraw mode: ${e}`)}return{remove_liquidity:a,burn_lp_amount:r,receive_amount_a:i,receive_amount_b:n,mode:e,swap:c}}async calculateWithdrawOtherSide(t,e,o){let{clmm_vault:i,lp_token_treasury:n}=t,{current_sqrt_price:r}=e,{coin_type_a:a,coin_type_b:c}=t.clmm_vault.wrapped_position,d=i.wrapped_position.tick_lower_index,u=i.wrapped_position.tick_upper_index,_=i.wrapped_position.liquidity,l=p.TickMath.tickIndexToSqrtPriceX64(d),P=p.TickMath.tickIndexToSqrtPriceX64(u),f=j(n,o,_),{coinA:m,coinB:w}=p.ClmmPoolUtil.getCoinAmountFromLiquidity(new D.default(f),new D.default(r),l,P,!1),h=N(o,t),y=(0,p.d)(m.toString()).add(h[a]?.value||"0").toFixed(0),b=(0,p.d)(w.toString()).add(h[c]?.value||"0").toFixed(0);return{receive_amount_a:y,receive_amount_b:b,burn_lp_amount:o,mode:"FixedOneSide",remove_liquidity:f}}async calculateWithdrawOnlyCoin(t,e,o,i){let{totalReceiveAmount:n,swapAmountIn:r,swapAmountOut:a,removeLiquidity:c}=await this.calculateWithdrawOnlyCoinByLpAmount(t,e,i,o);return{receive_amount_a:i?n:"0",receive_amount_b:i?"0":n,burn_lp_amount:o,remove_liquidity:c,swap:{swap_amount_in:r,swap_amount_out:a},mode:"FixedOneSide"}}async calculateWithdrawOnlyCoinByLpAmount(t,e,o,i){let{clmm_vault:n,lp_token_treasury:r}=t,{current_sqrt_price:a}=e,{coin_type_a:c,coin_type_b:d}=n.wrapped_position,u=n.wrapped_position.tick_lower_index,_=n.wrapped_position.tick_upper_index,l=n.wrapped_position.liquidity,P=p.TickMath.tickIndexToSqrtPriceX64(u),f=p.TickMath.tickIndexToSqrtPriceX64(_),m=j(r,i,l),{coinA:w,coinB:h}=p.ClmmPoolUtil.getCoinAmountFromLiquidity(new D.default(m),new D.default(a),P,f,!1),y=await this._sdk.PythPrice.getLatestPrice([c,d],!0),b=y[c],C=y[d];if(!b)throw Error(`not find ${c} price`);if(!C)throw Error(`not find ${d} price`);let F=o?C:b,T=o?b:C,{quote_per_base:I}=re(F.price,T.price),L=N(i,t),v=(0,p.d)(w.toString()).add(L[c]?.value||"0").toFixed(0),A=(0,p.d)(h.toString()).add(L[d]?.value||"0").toFixed(0),V=o?v:A,R=o?A:v,O=(0,p.toDecimalsAmount)((0,p.d)((0,p.fromDecimalsAmount)(R,o?C.coin_decimals:b.coin_decimals)).mul(I).toString(),o?b.coin_decimals:C.coin_decimals).toString(),te=(0,p.d)(V).add(O).toFixed(0);return{burn_lp_amount:i,removeLiquidity:m,is_coin_a:o,originAmount:V,swapAmountIn:R,swapAmountOut:(0,p.d)(O).toFixed(0,ke.default.ROUND_DOWN),totalReceiveAmount:te}}async getPackageConfigs(){let t={registry_id:"1",pools_handle:"",pyth_oracle_id:"1",oracle_infos_handle:"",prices_handle:"1",global_config_id:"1"},{package_id:e}=this._sdk.sdkOptions.vaults,o=await this._sdk.FullClient.getObject({id:e,options:{showPreviousTransaction:!0}}),i=(0,p.getObjectPreviousTransactionDigest)(o);(await this._sdk.FullClient.queryEventsByPage({Transaction:i})).data.forEach(u=>{let _=u.parsedJson;if(u.type)switch((0,Ae.extractStructTagFromType)(u.type).full_address){case`${e}::pool::InitEvent`:t.registry_id=_.registry_id;break;case`${e}::pyth_oracle::InitEvent`:t.pyth_oracle_id=_.pyth_oracle_id;break;case`${e}::config::InitConfigEvent`:t.global_config_id=_.global_config;break;default:break}});let r=await this._sdk.FullClient.getObject({id:t.registry_id,options:{showContent:!0}}),a=(0,p.getObjectFields)(r);t.pools_handle=a.pools.fields.id.id;let c=await this._sdk.FullClient.getObject({id:t.pyth_oracle_id,options:{showContent:!0}}),d=(0,p.getObjectFields)(c);return t.oracle_infos_handle=d.oracle_infos.fields.id.id,t.prices_handle=d.prices.fields.id.id,this._sdk.sdkOptions.vaults.configs=t,this._sdk.sdkOptions}};var $=require("@pythnetwork/pyth-sui-js"),ye=require("@mysten/sui/bcs"),B=require("@cetusprotocol/cetus-sui-clmm-sdk");var pe={env:"mainnet",fullRpcUrl:"https://fullnode.mainnet.sui.io",vaults:{package_id:"0xc4ebf35be1478318d78c324342854dd2735a036139373a9d41a1aa3a46a01d05",published_at:"0x8462eb7e1c52018f42f2dd2c33605f7212ad8702739a5eaacb0e1a0106f3dc6a",configs:{registry_id:"0x23014734280164270fa4c58f973c00079651e730d6f46491678ea8858f3bd13a",pools_handle:"0x31c810b6fcf3bb8699384e7c61fe9f7db53a04c4c22d514b3f79b6caa86183a8",pyth_oracle_id:"0xaed73fd854dfceedb2522903feec8c0e251764f5945bd38f969473024c4fb4d0",oracle_infos_handle:"0x6951ab4b36639e79ddbd9ba4478d7f72d1c5396b9198d0864c00e796c5815b01",prices_handle:"0x20a65a0f747f4f1ad465bdb61d784bbc5080ee5d200d5dd6594a354723b8c171",global_config_id:"0x5f8371674cd8e1e9eef5542ffe8adb339e3ff3a67894b14c954c7a92e992e0c1"},clmm_configs:{global_config_id:"0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f",global_vault_id:"0xce7bceef26d3ad1f6d9b6f13a953f053e6ed3ca77907516481ce99ae8e588f2b"},pyth_configs:{pyth_state_id:"0x1f9310238ee9298fb703c3419030b35b22bb1cc37113e3bb5007c99aec79e5b8",hermes_service_urls:{primary:"https://haedalp-pythnet-9ac9.mainnet.pythnet.rpcpool.com",secondary:"https://hermes.pyth.network"},wormhole_state_id:"0xaeab97f96cf9877fee2883315d459552b2b921edc16d7ceac6eab944dd88919c",pyth_package_id:"0x04e20ddf36af412a4096f9014f4a565af9e812db9a05cc40254846cf6ed0ad91",pyth_published_at:"0x04e20ddf36af412a4096f9014f4a565af9e812db9a05cc40254846cf6ed0ad91"}},aggregatorUrl:"https://api-sui.cetus.zone/router_v2",providers:["CETUS","TURBOS","STEAMM","SPRINGSUI","HAEDAL","FLOWX","AFTERMATH","ALPHAFI","BLUEFIN","METASTABLE","VOLO","SCALLOP","KRIYA","FLOWXV3","DEEPBOOKV3","AFSUI","BLUEMOVE","KRIYAV3","HAEDALPMM","OBRIC"]},_e={"0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC":{coin_type:"0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC",price_feed_id:"0xeaa020c61cc479712813461ce153894a96a6c00b21ed0cfc2798d1f9a9e9c94a",coin_decimals:6},"0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI":{coin_type:"0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI",price_feed_id:"0x23d7315113f5b1d3ba7a83604c44b94d79f4fd69af77f804fc7f920a6dc65744",coin_decimals:9},"0x06864a6f921804860930db6ddbe2e16acdf8504495ea7481637a1c8b9a8fe54b::cetus::CETUS":{coin_type:"0x06864a6f921804860930db6ddbe2e16acdf8504495ea7481637a1c8b9a8fe54b::cetus::CETUS",price_feed_id:"0xe5b274b2611143df055d6e7cd8d93fe1961716bcd4dca1cad87a83bc1e78c1ef",coin_decimals:9},"0xdeeb7a4662eec9f2f3def03fb937a663dddaa2e215b8078a284d026b7946c270::deep::DEEP":{coin_type:"0xdeeb7a4662eec9f2f3def03fb937a663dddaa2e215b8078a284d026b7946c270::deep::DEEP",price_feed_id:"0x29bdd5248234e33bd93d3b81100b5fa32eaa5997843847e2c2cb16d7c6d9f7ff",coin_decimals:6},"0xa99b8952d4f7d947ea77fe0ecdcc9e5fc0bcab2841d6e2a5aa00c3044e5544b5::navx::NAVX":{coin_type:"0xa99b8952d4f7d947ea77fe0ecdcc9e5fc0bcab2841d6e2a5aa00c3044e5544b5::navx::NAVX",price_feed_id:"0x88250f854c019ef4f88a5c073d52a18bb1c6ac437033f5932cd017d24917ab46",coin_decimals:9},"0x027792d9fed7f9844eb4839566001bb6f6cb4804f66aa2da6fe1ee242d896881::coin::COIN":{coin_type:"0x027792d9fed7f9844eb4839566001bb6f6cb4804f66aa2da6fe1ee242d896881::coin::COIN",price_feed_id:"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43",coin_decimals:9},"0xbde4ba4c2e274a60ce15c1cfff9e5c42e41654ac8b6d906a57efa4bd3c29f47d::hasui::HASUI":{coin_type:"0xbde4ba4c2e274a60ce15c1cfff9e5c42e41654ac8b6d906a57efa4bd3c29f47d::hasui::HASUI",price_feed_id:"0x6120ffcf96395c70aa77e72dcb900bf9d40dccab228efca59a17b90ce423d5e8",coin_decimals:9},"0xd0e89b2af5e4910726fbcd8b8dd37bb79b29e5f83f7491bca830e94f7f226d29::eth::ETH":{coin_type:"0xd0e89b2af5e4910726fbcd8b8dd37bb79b29e5f83f7491bca830e94f7f226d29::eth::ETH",price_feed_id:"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace",coin_decimals:8}};function fe(s){return s&&(pe.fullRpcUrl=s),new x(pe)}var me={env:"testnet",fullRpcUrl:"https://fullnode.testnet.sui.io",vaults:{package_id:"0x0a624f2dcafc9f7f381684b3d553a9837411925b6e8530b1f8f4e6ff3f1fde77",published_at:"0x36f9d8e6219e6ba4d8840165ec27676a1bcb43fc5712f87ca702ac7b5924507d",configs:{registry_id:"0xa2ebae3656c5fbdd2473caadfc077cd1780a881759a356f1a5bc0f9fd470adb9",pools_handle:"0x0df305d395f8e0359da0ac5717bc96e0e1da9792684ec8e8f6a20b7ab544f98e",pyth_oracle_id:"0x6c74aec8d4471d9b05f718c467204c9010c450b94a4e40b0b4031a7d6c386337",global_config_id:"0x8969b09cfdd72b1635263b569027ded4b91bbabd7d8cd91b21e355325e218b10",oracle_infos_handle:"0x8707c6665d202f277ffe8e7fd58684e232622a3e6826b94ba99f7569f253fa9d",prices_handle:"0x74230bb2c6a63235acbfe477ce16144afca7ccd16a458fa93f4605330b5d1c58"},clmm_configs:{global_config_id:"0x9774e359588ead122af1c7e7f64e14ade261cfeecdb5d0eb4a5b3b4c8ab8bd3e",global_vault_id:"0xf78d2ee3c312f298882cb680695e5e8c81b1d441a646caccc058006c2851ddea"},pyth_configs:{pyth_state_id:"0x243759059f4c3111179da5878c12f68d612c21a8d54d85edc86164bb18be1c7c",wormhole_state_id:"0x31358d198147da50db32eda2562951d53973a0c0ad5ed738e9b17d88b213d790",hermes_service_urls:{primary:"https://haedalp-pythnet-9ac9.mainnet.pythnet.rpcpool.com",secondary:"https://hermes-beta.pyth.network"},pyth_package_id:"0xabf837e98c26087cba0883c0a7a28326b1fa3c5e1e2c5abdb486f9e8f594c837",pyth_published_at:"0xabf837e98c26087cba0883c0a7a28326b1fa3c5e1e2c5abdb486f9e8f594c837"}},aggregatorUrl:"https://api-sui.devcetus.com/router_v2",providers:["CETUS","DEEPBOOK","KRIYA","KRIYAV3","FLOWX","FLOWXV3","AFTERMATH","TURBOS","HAEDAL","VOLO","AFSUI"]},ge={};function he(s){return s&&(me.fullRpcUrl=s),new x(me)}var Se=(e=>(e.mainnet="mainnet",e.testnet="testnet",e))(Se||{});function Me(s){let{env:t,fullNodeUrl:e}=s;return t==="mainnet"?fe(e):he(e)}var be=["CETUS","TURBOS","STEAMM","SPRINGSUI","HAEDAL","FLOWX","AFTERMATH","ALPHAFI","BLUEFIN","METASTABLE","VOLO","SCALLOP","KRIYA","FLOWXV3","DEEPBOOKV3","AFSUI","BLUEMOVE","KRIYAV3","HAEDALPMM","OBRIC"];function Oe(s){try{let{request_time:t,error:e,request_params:o,request_url:i,action:n}=s;if(window?.gtag){let r=e.message==="Request Timeout";window.gtag("event","API_Error",{request_action:n,request_url:i,request_time:t,request_params:JSON.stringify(o),error_msg:String(e)}),console.error(`[GA] Logged API ${r?"Timeout":"Error"}:`,i,e)}else console.error("[GA] gtag is not defined. Skipping analytics logging.")}catch(t){console.log("\u{1F680} ~ trackPythError ~ error:",t)}}var Ke=16*1024,H=class{_sdk;connection;suiPythClient;hasChangeConnection=!1;errorList=[];constructor(t){let{pyth_configs:e}=t.sdkOptions.vaults,{pyth_state_id:o,wormhole_state_id:i}=e;this._sdk=t,this.hasChangeConnection=!1;let{primary:n}=t.sdkOptions.vaults.pyth_configs.hermes_service_urls;if(n.length===0)throw new Error("primary url is empty");this.connection=new $.SuiPriceServiceConnection(n),this.suiPythClient=new $.SuiPythClient(this._sdk.FullClient,o,i)}get sdk(){return this._sdk}recordError(t,e,o){let{secondary:i,primary:n}=this._sdk.sdkOptions.vaults.pyth_configs.hermes_service_urls,r={error:t,request_params:e,request_url:this.hasChangeConnection&&i?i:n,action:o,request_time:Date.now()};Oe(r),this.errorList.push(r)}readErrorList(){let t=this.errorList;return this.errorList=[],t}async getFeedInfoList(t){let e=t.map(i=>this.getFeedInfo(i,!1));return await Promise.all(e)}priceCheck(t,e=60){let o=Math.floor(Date.now()/1e3);if(!(Math.abs(o-t.last_update_time)>e))return t}async getLatestPrice(t,e=!1){let o={},i=[];if(e?t.forEach(a=>{let c=this._sdk.getCache(`getLatestPrice_${a}`);c&&this.priceCheck(c,60)?o[a]=c:i.push(a)}):i=[...t],i.length===0)return o;let n=await this.getFeedInfoList(i);return(await this.getLatestPriceFeeds(n.map(a=>a.price_feed_id)))?.forEach((a,c)=>{let d=a.getPriceUnchecked();if(d){let{price:u,expo:_}=d,l=(0,B.d)(u).mul((0,B.d)(10).pow((0,B.d)(_))).toString(),P=n[c],f={coin_type:P.coin_type,price:l,coin_decimals:P.coin_decimals,oracle_price:0n,last_update_time:d.publishTime};f.oracle_price=le(BigInt(u),BigInt(_)),o[i[c]]=f,this._sdk.updateCache(`getLatestPrice_${f.coin_type}`,f)}}),o}async getPriceFeedsUpdateData(t){try{return await this.connection.getPriceFeedsUpdateData(t)}catch(e){this.recordError(e,t,"getPriceFeedsUpdateData");let{secondary:o}=this._sdk.sdkOptions.vaults.pyth_configs.hermes_service_urls;if(o){this.hasChangeConnection=!0,this.connection=new $.SuiPriceServiceConnection(o);try{return await this.connection.getPriceFeedsUpdateData(t)}catch(i){console.log("\u{1F680} ~ PythPriceModule ~ getPriceFeedsUpdateData ~ error:",i),this.recordError(i,t,"getPriceFeedsUpdateData")}}}}async getLatestPriceFeeds(t){try{return await this.connection.getLatestPriceFeeds(t)}catch(e){this.recordError(e,t,"getLatestPriceFeeds");let{secondary:o}=this._sdk.sdkOptions.vaults.pyth_configs.hermes_service_urls;if(o){this.hasChangeConnection=!0,this.connection=new $.SuiPriceServiceConnection(o);try{return await this.connection.getLatestPriceFeeds(t)}catch(i){this.recordError(i,t,"getLatestPriceFeeds")}}}}async buildUpdatePythPricePayload(t,e){let{pyth_configs:o,published_at:i,configs:n}=this._sdk.sdkOptions.vaults,{pyth_state_id:r,pyth_published_at:a,pyth_package_id:c}=o,{global_config_id:d,pyth_oracle_id:u}=n,_=(await this.getFeedInfoList(t)).map(h=>h.price_feed_id);if(t.length!==_.length)throw Error("find feed id fail");let l=await this.getPriceFeedsUpdateData(_);if(!l)throw new Error("get price update data fail");if(l.length>1)throw new Error("SDK does not support sending multiple accumulator messages in a single transaction");let P=this.suiPythClient.extractVaaBytesFromAccumulatorMessage(l[0]),f=await this.suiPythClient.verifyVaas([P],e),[m]=e.moveCall({target:`${a}::pyth::create_authenticated_price_infos_using_accumulator`,typeArguments:[],arguments:[e.object(r),e.pure(ye.bcs.vector(ye.bcs.U8).serialize(Array.from(l[0]),{maxSize:Ke}).toBytes()),f[0],e.object(B.CLOCK_ADDRESS)]}),w=0;for(let h of _){let y=await this.suiPythClient.getPriceFeedObjectId(h);if(!y)throw new Error(`Price feed ${h} not found, please create it first`);[m]=e.moveCall({target:`${i}::pyth_oracle::update_price`,typeArguments:[t[w]],arguments:[e.object(u),e.object(d),e.object(r),m,e.object(y),e.object(B.CLOCK_ADDRESS)]}),w+=1}return e.moveCall({target:`${a}::hot_potato_vector::destroy`,arguments:[m],typeArguments:[`${c}::price_info::PriceInfo`]}),e}getFeedInfoByJson(t){let{env:e}=this._sdk.sdkOptions,o=(0,B.fixCoinType)(t,!1);return e==="mainnet"?_e[o]:ge[o]}async getFeedInfo(t,e){let o=`getFeedInfo_${t}`,i=this._sdk.getCache(o,e);if(i)return i;let n=this.getFeedInfoByJson(t);if(n)return n;let{configs:r}=this._sdk.sdkOptions.vaults,a=await this._sdk.FullClient.getDynamicFieldObject({parentId:r.oracle_infos_handle,name:{type:"0x1::type_name::TypeName",value:(0,B.fixCoinType)(t,!0)}}),{fields:c}=a.data.content.fields.value,d={coin_type:t,price_feed_id:ue(c.price_feed_id),coin_decimals:c.coin_decimals};return console.log("\u{1F680} ~ PythPriceModule ~ getFeedInfo ~ info:",d),this._sdk.updateCache(o,d),d}};var x=class{_cache={};_sdkOptions;_senderAddress="";_vaults;_pythPrice;_aggregatorClient;_cetusClmmSDK;constructor(t){(!t.providers||t.providers.length===0)&&(t.providers=be),this._sdkOptions=t,this._cetusClmmSDK=t.env==="testnet"?(0,E.initTestnetSDK)(t.fullRpcUrl):(0,E.initMainnetSDK)(t.fullRpcUrl),this._vaults=new W(this),this._pythPrice=new H(this);let{primary:e,secondary:o}=t.vaults.pyth_configs.hermes_service_urls,i=[e,o].filter(Boolean);this._aggregatorClient=new X.AggregatorClient({endpoint:t.aggregatorUrl,signer:(0,ee.normalizeSuiAddress)("0x0"),client:new Fe.SuiClient({url:t.fullRpcUrl}),env:t.env==="testnet"?X.Env.Testnet:X.Env.Mainnet,pythUrls:i}),(0,E.patchFixSuiObjectId)(this._sdkOptions)}get senderAddress(){return this._senderAddress}set senderAddress(t){this._senderAddress=t,this.CetusClmmSDK.senderAddress=t}get sdkOptions(){return this._sdkOptions}get Vaults(){return this._vaults}get PythPrice(){return this._pythPrice}get FullClient(){return this._cetusClmmSDK.fullClient}get AggregatorClient(){return this._aggregatorClient}get CetusClmmSDK(){return this._cetusClmmSDK}getVerifySenderAddress(){if(this.senderAddress.length===0||!(0,ee.isValidSuiAddress)(this.senderAddress))throw Error("this sdk senderAddress is illegal");return this.senderAddress}async getOwnerCoinAssets(t,e,o=!0){return await this._cetusClmmSDK.getOwnerCoinAssets(t,e,o)}async getOwnerCoinBalances(t,e){return await this._cetusClmmSDK.getOwnerCoinBalances(t,e)}updateCache(t,e,o=E.cacheTime24h){this.CetusClmmSDK.updateCache(t,e,o)}getCache(t,e=!1){return this.CetusClmmSDK.getCache(t,e)}};var $e=x;0&&(module.exports={PythPriceModule,SdkEnv,VaultsModule,VolatileVaultsSDK,buildPoolData,buildVaultsBalance,calculateAum,calculateDepositRatioFixTokenA,calculateTotalAumBasedOnQuote,checkIsMinOrMaxIndex,convertU64BytesToDecimal,defaultProvider,estCoinAmountsFromTotalAmount,feed_map_mainnet,feed_map_testnet,getCoinAmountsFromRatio,getPositionPriceRange,getPriceWithFormattedDecimals,getPrimitivePricesFromBaseQuote,getQuotePerBaseAndBasePerQuote,getShareBufferAssets,getUpdatePriceType,getVaultValidBalances,get_lp_amount_by_liquidity,get_lp_amount_by_tvl,get_share_liquidity_by_amount,initMainnetSDK,initTestnetSDK,initVaultSDK,oraclePriceMultiplierDecimal,printTransaction,toSuiObjectId,vaults_mainnet,vaults_testnet});
|
|
2
|
-
|
|
1
|
+
"use strict";var Wn=Object.create;var Ne=Object.defineProperty;var zn=Object.getOwnPropertyDescriptor;var Gn=Object.getOwnPropertyNames;var Yn=Object.getPrototypeOf,Jn=Object.prototype.hasOwnProperty;var Qn=(i,t)=>{for(var e in t)Ne(i,e,{get:t[e],enumerable:!0})},fn=(i,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Gn(t))!Jn.call(i,o)&&o!==e&&Ne(i,o,{get:()=>t[o],enumerable:!(n=zn(t,o))||n.enumerable});return i};var _e=(i,t,e)=>(e=i!=null?Wn(Yn(i)):{},fn(t||!i||!i.__esModule?Ne(e,"default",{value:i,enumerable:!0}):e,i)),tr=i=>fn(Ne({},"__esModule",{value:!0}),i);var No={};Qn(No,{PythPriceModule:()=>Ie,SdkEnv:()=>Zn,VaultsModule:()=>Te,VolatileVaultsSDK:()=>oe,buildPoolData:()=>ze,buildVaultsBalance:()=>Di,calculateAum:()=>Ni,calculateDepositRatioFixTokenA:()=>jn,calculateTotalAumBasedOnQuote:()=>Ge,checkIsMinOrMaxIndex:()=>He,convertU64BytesToDecimal:()=>xo,default:()=>Bo,defaultProvider:()=>Xi,estCoinAmountsFromTotalAmount:()=>Ii,feed_map_mainnet:()=>Ui,feed_map_testnet:()=>Ki,getCoinAmountsFromRatio:()=>ye,getPositionPriceRange:()=>Oo,getPriceWithFormattedDecimals:()=>Ri,getPrimitivePricesFromBaseQuote:()=>Un,getQuotePerBaseAndBasePerQuote:()=>Bi,getShareBufferAssets:()=>Oe,getUpdatePriceType:()=>qi,getVaultValidBalances:()=>Ye,get_lp_amount_by_liquidity:()=>Eo,get_lp_amount_by_tvl:()=>Fi,get_share_liquidity_by_amount:()=>ge,initMainnetSDK:()=>Vi,initTestnetSDK:()=>Zi,initVaultSDK:()=>To,oraclePriceMultiplierDecimal:()=>pe,printTransaction:()=>Po,toSuiObjectId:()=>Li,vaults_mainnet:()=>ji,vaults_testnet:()=>$i});module.exports=tr(No);var Hn=require("@mysten/sui/client"),Je=require("@mysten/sui/utils"),Be=require("@cetusprotocol/aggregator-sdk");var lr=require("@mysten/sui/utils"),Ai=require("@mysten/sui/client"),hr=require("@mysten/sui/client"),fr=require("@mysten/sui/utils"),mr=require("@mysten/sui/keypairs/ed25519"),pr=require("@mysten/sui/keypairs/secp256k1"),fe=require("@mysten/sui/utils"),bi=require("@mysten/sui/transactions"),er=Object.create,Mn=Object.defineProperty,ir=Object.getOwnPropertyDescriptor,nr=Object.getOwnPropertyNames,rr=Object.getPrototypeOf,or=Object.prototype.hasOwnProperty,sr=(i=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(i,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):i)(function(i){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+i+'" is not supported')}),ar=(i,t)=>()=>(t||i((t={exports:{}}).exports,t),t.exports),ur=(i,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of nr(t))!or.call(i,o)&&o!==e&&Mn(i,o,{get:()=>t[o],enumerable:!(n=ir(t,o))||n.enumerable});return i},ke=(i,t,e)=>(e=i!=null?er(rr(i)):{},ur(t||!i||!i.__esModule?Mn(e,"default",{value:i,enumerable:!0}):e,i)),xe=ar((i,t)=>{"use strict";(function(e,n){"use strict";function o(r,l){if(!r)throw new Error(l||"Assertion failed")}function a(r,l){r.super_=l;var c=function(){};c.prototype=l.prototype,r.prototype=new c,r.prototype.constructor=r}function s(r,l,c){if(s.isBN(r))return r;this.negative=0,this.words=null,this.length=0,this.red=null,r!==null&&((l==="le"||l==="be")&&(c=l,l=10),this._init(r||0,l||10,c||"be"))}typeof e=="object"?e.exports=s:n.BN=s,s.BN=s,s.wordSize=26;var d;try{typeof window<"u"&&typeof window.Buffer<"u"?d=window.Buffer:d=sr("buffer").Buffer}catch{}s.isBN=function(r){return r instanceof s?!0:r!==null&&typeof r=="object"&&r.constructor.wordSize===s.wordSize&&Array.isArray(r.words)},s.max=function(r,l){return r.cmp(l)>0?r:l},s.min=function(r,l){return r.cmp(l)<0?r:l},s.prototype._init=function(r,l,c){if(typeof r=="number")return this._initNumber(r,l,c);if(typeof r=="object")return this._initArray(r,l,c);l==="hex"&&(l=16),o(l===(l|0)&&l>=2&&l<=36),r=r.toString().replace(/\s+/g,"");var f=0;r[0]==="-"&&(f++,this.negative=1),f<r.length&&(l===16?this._parseHex(r,f,c):(this._parseBase(r,l,f),c==="le"&&this._initArray(this.toArray(),l,c)))},s.prototype._initNumber=function(r,l,c){r<0&&(this.negative=1,r=-r),r<67108864?(this.words=[r&67108863],this.length=1):r<4503599627370496?(this.words=[r&67108863,r/67108864&67108863],this.length=2):(o(r<9007199254740992),this.words=[r&67108863,r/67108864&67108863,1],this.length=3),c==="le"&&this._initArray(this.toArray(),l,c)},s.prototype._initArray=function(r,l,c){if(o(typeof r.length=="number"),r.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(r.length/3),this.words=new Array(this.length);for(var f=0;f<this.length;f++)this.words[f]=0;var g,v,M=0;if(c==="be")for(f=r.length-1,g=0;f>=0;f-=3)v=r[f]|r[f-1]<<8|r[f-2]<<16,this.words[g]|=v<<M&67108863,this.words[g+1]=v>>>26-M&67108863,M+=24,M>=26&&(M-=26,g++);else if(c==="le")for(f=0,g=0;f<r.length;f+=3)v=r[f]|r[f+1]<<8|r[f+2]<<16,this.words[g]|=v<<M&67108863,this.words[g+1]=v>>>26-M&67108863,M+=24,M>=26&&(M-=26,g++);return this._strip()};function h(r,l){var c=r.charCodeAt(l);if(c>=48&&c<=57)return c-48;if(c>=65&&c<=70)return c-55;if(c>=97&&c<=102)return c-87;o(!1,"Invalid character in "+r)}function m(r,l,c){var f=h(r,c);return c-1>=l&&(f|=h(r,c-1)<<4),f}s.prototype._parseHex=function(r,l,c){this.length=Math.ceil((r.length-l)/6),this.words=new Array(this.length);for(var f=0;f<this.length;f++)this.words[f]=0;var g=0,v=0,M;if(c==="be")for(f=r.length-1;f>=l;f-=2)M=m(r,l,f)<<g,this.words[v]|=M&67108863,g>=18?(g-=18,v+=1,this.words[v]|=M>>>26):g+=8;else{var p=r.length-l;for(f=p%2===0?l+1:l;f<r.length;f+=2)M=m(r,l,f)<<g,this.words[v]|=M&67108863,g>=18?(g-=18,v+=1,this.words[v]|=M>>>26):g+=8}this._strip()};function w(r,l,c,f){for(var g=0,v=0,M=Math.min(r.length,c),p=l;p<M;p++){var u=r.charCodeAt(p)-48;g*=f,u>=49?v=u-49+10:u>=17?v=u-17+10:v=u,o(u>=0&&v<f,"Invalid character"),g+=v}return g}s.prototype._parseBase=function(r,l,c){this.words=[0],this.length=1;for(var f=0,g=1;g<=67108863;g*=l)f++;f--,g=g/l|0;for(var v=r.length-c,M=v%f,p=Math.min(v,v-M)+c,u=0,_=c;_<p;_+=f)u=w(r,_,_+f,l),this.imuln(g),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u);if(M!==0){var O=1;for(u=w(r,_,r.length,l),_=0;_<M;_++)O*=l;this.imuln(O),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u)}this._strip()},s.prototype.copy=function(r){r.words=new Array(this.length);for(var l=0;l<this.length;l++)r.words[l]=this.words[l];r.length=this.length,r.negative=this.negative,r.red=this.red};function b(r,l){r.words=l.words,r.length=l.length,r.negative=l.negative,r.red=l.red}if(s.prototype._move=function(r){b(r,this)},s.prototype.clone=function(){var r=new s(null);return this.copy(r),r},s.prototype._expand=function(r){for(;this.length<r;)this.words[this.length++]=0;return this},s.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},s.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{s.prototype[Symbol.for("nodejs.util.inspect.custom")]=y}catch{s.prototype.inspect=y}else s.prototype.inspect=y;function y(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var A=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],C=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],k=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];s.prototype.toString=function(r,l){r=r||10,l=l|0||1;var c;if(r===16||r==="hex"){c="";for(var f=0,g=0,v=0;v<this.length;v++){var M=this.words[v],p=((M<<f|g)&16777215).toString(16);g=M>>>24-f&16777215,f+=2,f>=26&&(f-=26,v--),g!==0||v!==this.length-1?c=A[6-p.length]+p+c:c=p+c}for(g!==0&&(c=g.toString(16)+c);c.length%l!==0;)c="0"+c;return this.negative!==0&&(c="-"+c),c}if(r===(r|0)&&r>=2&&r<=36){var u=C[r],_=k[r];c="";var O=this.clone();for(O.negative=0;!O.isZero();){var T=O.modrn(_).toString(r);O=O.idivn(_),O.isZero()?c=T+c:c=A[u-T.length]+T+c}for(this.isZero()&&(c="0"+c);c.length%l!==0;)c="0"+c;return this.negative!==0&&(c="-"+c),c}o(!1,"Base should be between 2 and 36")},s.prototype.toNumber=function(){var r=this.words[0];return this.length===2?r+=this.words[1]*67108864:this.length===3&&this.words[2]===1?r+=4503599627370496+this.words[1]*67108864:this.length>2&&o(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-r:r},s.prototype.toJSON=function(){return this.toString(16,2)},d&&(s.prototype.toBuffer=function(r,l){return this.toArrayLike(d,r,l)}),s.prototype.toArray=function(r,l){return this.toArrayLike(Array,r,l)};var B=function(r,l){return r.allocUnsafe?r.allocUnsafe(l):new r(l)};s.prototype.toArrayLike=function(r,l,c){this._strip();var f=this.byteLength(),g=c||Math.max(1,f);o(f<=g,"byte array longer than desired length"),o(g>0,"Requested array length <= 0");var v=B(r,g),M=l==="le"?"LE":"BE";return this["_toArrayLike"+M](v,f),v},s.prototype._toArrayLikeLE=function(r,l){for(var c=0,f=0,g=0,v=0;g<this.length;g++){var M=this.words[g]<<v|f;r[c++]=M&255,c<r.length&&(r[c++]=M>>8&255),c<r.length&&(r[c++]=M>>16&255),v===6?(c<r.length&&(r[c++]=M>>24&255),f=0,v=0):(f=M>>>24,v+=2)}if(c<r.length)for(r[c++]=f;c<r.length;)r[c++]=0},s.prototype._toArrayLikeBE=function(r,l){for(var c=r.length-1,f=0,g=0,v=0;g<this.length;g++){var M=this.words[g]<<v|f;r[c--]=M&255,c>=0&&(r[c--]=M>>8&255),c>=0&&(r[c--]=M>>16&255),v===6?(c>=0&&(r[c--]=M>>24&255),f=0,v=0):(f=M>>>24,v+=2)}if(c>=0)for(r[c--]=f;c>=0;)r[c--]=0},Math.clz32?s.prototype._countBits=function(r){return 32-Math.clz32(r)}:s.prototype._countBits=function(r){var l=r,c=0;return l>=4096&&(c+=13,l>>>=13),l>=64&&(c+=7,l>>>=7),l>=8&&(c+=4,l>>>=4),l>=2&&(c+=2,l>>>=2),c+l},s.prototype._zeroBits=function(r){if(r===0)return 26;var l=r,c=0;return!(l&8191)&&(c+=13,l>>>=13),!(l&127)&&(c+=7,l>>>=7),!(l&15)&&(c+=4,l>>>=4),!(l&3)&&(c+=2,l>>>=2),!(l&1)&&c++,c},s.prototype.bitLength=function(){var r=this.words[this.length-1],l=this._countBits(r);return(this.length-1)*26+l};function P(r){for(var l=new Array(r.bitLength()),c=0;c<l.length;c++){var f=c/26|0,g=c%26;l[c]=r.words[f]>>>g&1}return l}s.prototype.zeroBits=function(){if(this.isZero())return 0;for(var r=0,l=0;l<this.length;l++){var c=this._zeroBits(this.words[l]);if(r+=c,c!==26)break}return r},s.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},s.prototype.toTwos=function(r){return this.negative!==0?this.abs().inotn(r).iaddn(1):this.clone()},s.prototype.fromTwos=function(r){return this.testn(r-1)?this.notn(r).iaddn(1).ineg():this.clone()},s.prototype.isNeg=function(){return this.negative!==0},s.prototype.neg=function(){return this.clone().ineg()},s.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},s.prototype.iuor=function(r){for(;this.length<r.length;)this.words[this.length++]=0;for(var l=0;l<r.length;l++)this.words[l]=this.words[l]|r.words[l];return this._strip()},s.prototype.ior=function(r){return o((this.negative|r.negative)===0),this.iuor(r)},s.prototype.or=function(r){return this.length>r.length?this.clone().ior(r):r.clone().ior(this)},s.prototype.uor=function(r){return this.length>r.length?this.clone().iuor(r):r.clone().iuor(this)},s.prototype.iuand=function(r){var l;this.length>r.length?l=r:l=this;for(var c=0;c<l.length;c++)this.words[c]=this.words[c]&r.words[c];return this.length=l.length,this._strip()},s.prototype.iand=function(r){return o((this.negative|r.negative)===0),this.iuand(r)},s.prototype.and=function(r){return this.length>r.length?this.clone().iand(r):r.clone().iand(this)},s.prototype.uand=function(r){return this.length>r.length?this.clone().iuand(r):r.clone().iuand(this)},s.prototype.iuxor=function(r){var l,c;this.length>r.length?(l=this,c=r):(l=r,c=this);for(var f=0;f<c.length;f++)this.words[f]=l.words[f]^c.words[f];if(this!==l)for(;f<l.length;f++)this.words[f]=l.words[f];return this.length=l.length,this._strip()},s.prototype.ixor=function(r){return o((this.negative|r.negative)===0),this.iuxor(r)},s.prototype.xor=function(r){return this.length>r.length?this.clone().ixor(r):r.clone().ixor(this)},s.prototype.uxor=function(r){return this.length>r.length?this.clone().iuxor(r):r.clone().iuxor(this)},s.prototype.inotn=function(r){o(typeof r=="number"&&r>=0);var l=Math.ceil(r/26)|0,c=r%26;this._expand(l),c>0&&l--;for(var f=0;f<l;f++)this.words[f]=~this.words[f]&67108863;return c>0&&(this.words[f]=~this.words[f]&67108863>>26-c),this._strip()},s.prototype.notn=function(r){return this.clone().inotn(r)},s.prototype.setn=function(r,l){o(typeof r=="number"&&r>=0);var c=r/26|0,f=r%26;return this._expand(c+1),l?this.words[c]=this.words[c]|1<<f:this.words[c]=this.words[c]&~(1<<f),this._strip()},s.prototype.iadd=function(r){var l;if(this.negative!==0&&r.negative===0)return this.negative=0,l=this.isub(r),this.negative^=1,this._normSign();if(this.negative===0&&r.negative!==0)return r.negative=0,l=this.isub(r),r.negative=1,l._normSign();var c,f;this.length>r.length?(c=this,f=r):(c=r,f=this);for(var g=0,v=0;v<f.length;v++)l=(c.words[v]|0)+(f.words[v]|0)+g,this.words[v]=l&67108863,g=l>>>26;for(;g!==0&&v<c.length;v++)l=(c.words[v]|0)+g,this.words[v]=l&67108863,g=l>>>26;if(this.length=c.length,g!==0)this.words[this.length]=g,this.length++;else if(c!==this)for(;v<c.length;v++)this.words[v]=c.words[v];return this},s.prototype.add=function(r){var l;return r.negative!==0&&this.negative===0?(r.negative=0,l=this.sub(r),r.negative^=1,l):r.negative===0&&this.negative!==0?(this.negative=0,l=r.sub(this),this.negative=1,l):this.length>r.length?this.clone().iadd(r):r.clone().iadd(this)},s.prototype.isub=function(r){if(r.negative!==0){r.negative=0;var l=this.iadd(r);return r.negative=1,l._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(r),this.negative=1,this._normSign();var c=this.cmp(r);if(c===0)return this.negative=0,this.length=1,this.words[0]=0,this;var f,g;c>0?(f=this,g=r):(f=r,g=this);for(var v=0,M=0;M<g.length;M++)l=(f.words[M]|0)-(g.words[M]|0)+v,v=l>>26,this.words[M]=l&67108863;for(;v!==0&&M<f.length;M++)l=(f.words[M]|0)+v,v=l>>26,this.words[M]=l&67108863;if(v===0&&M<f.length&&f!==this)for(;M<f.length;M++)this.words[M]=f.words[M];return this.length=Math.max(this.length,M),f!==this&&(this.negative=1),this._strip()},s.prototype.sub=function(r){return this.clone().isub(r)};function q(r,l,c){c.negative=l.negative^r.negative;var f=r.length+l.length|0;c.length=f,f=f-1|0;var g=r.words[0]|0,v=l.words[0]|0,M=g*v,p=M&67108863,u=M/67108864|0;c.words[0]=p;for(var _=1;_<f;_++){for(var O=u>>>26,T=u&67108863,N=Math.min(_,l.length-1),j=Math.max(0,_-r.length+1);j<=N;j++){var $=_-j|0;g=r.words[$]|0,v=l.words[j]|0,M=g*v+T,O+=M/67108864|0,T=M&67108863}c.words[_]=T|0,u=O|0}return u!==0?c.words[_]=u|0:c.length--,c._strip()}var x=function(r,l,c){var f=r.words,g=l.words,v=c.words,M=0,p,u,_,O=f[0]|0,T=O&8191,N=O>>>13,j=f[1]|0,$=j&8191,W=j>>>13,se=f[2]|0,z=se&8191,Y=se>>>13,zi=f[3]|0,et=zi&8191,it=zi>>>13,Gi=f[4]|0,nt=Gi&8191,rt=Gi>>>13,Yi=f[5]|0,ot=Yi&8191,st=Yi>>>13,Ji=f[6]|0,at=Ji&8191,ut=Ji>>>13,Qi=f[7]|0,lt=Qi&8191,ct=Qi>>>13,tn=f[8]|0,dt=tn&8191,ht=tn>>>13,en=f[9]|0,ft=en&8191,mt=en>>>13,nn=g[0]|0,pt=nn&8191,gt=nn>>>13,rn=g[1]|0,_t=rn&8191,wt=rn>>>13,on=g[2]|0,bt=on&8191,vt=on>>>13,sn=g[3]|0,yt=sn&8191,Mt=sn>>>13,an=g[4]|0,St=an&8191,At=an>>>13,un=g[5]|0,Ct=un&8191,kt=un>>>13,ln=g[6]|0,xt=ln&8191,Pt=ln>>>13,cn=g[7]|0,Et=cn&8191,Ot=cn>>>13,dn=g[8]|0,Tt=dn&8191,It=dn>>>13,hn=g[9]|0,Bt=hn&8191,Nt=hn>>>13;c.negative=r.negative^l.negative,c.length=19,p=Math.imul(T,pt),u=Math.imul(T,gt),u=u+Math.imul(N,pt)|0,_=Math.imul(N,gt);var Qe=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(Qe>>>26)|0,Qe&=67108863,p=Math.imul($,pt),u=Math.imul($,gt),u=u+Math.imul(W,pt)|0,_=Math.imul(W,gt),p=p+Math.imul(T,_t)|0,u=u+Math.imul(T,wt)|0,u=u+Math.imul(N,_t)|0,_=_+Math.imul(N,wt)|0;var ti=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(ti>>>26)|0,ti&=67108863,p=Math.imul(z,pt),u=Math.imul(z,gt),u=u+Math.imul(Y,pt)|0,_=Math.imul(Y,gt),p=p+Math.imul($,_t)|0,u=u+Math.imul($,wt)|0,u=u+Math.imul(W,_t)|0,_=_+Math.imul(W,wt)|0,p=p+Math.imul(T,bt)|0,u=u+Math.imul(T,vt)|0,u=u+Math.imul(N,bt)|0,_=_+Math.imul(N,vt)|0;var ei=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(ei>>>26)|0,ei&=67108863,p=Math.imul(et,pt),u=Math.imul(et,gt),u=u+Math.imul(it,pt)|0,_=Math.imul(it,gt),p=p+Math.imul(z,_t)|0,u=u+Math.imul(z,wt)|0,u=u+Math.imul(Y,_t)|0,_=_+Math.imul(Y,wt)|0,p=p+Math.imul($,bt)|0,u=u+Math.imul($,vt)|0,u=u+Math.imul(W,bt)|0,_=_+Math.imul(W,vt)|0,p=p+Math.imul(T,yt)|0,u=u+Math.imul(T,Mt)|0,u=u+Math.imul(N,yt)|0,_=_+Math.imul(N,Mt)|0;var ii=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(ii>>>26)|0,ii&=67108863,p=Math.imul(nt,pt),u=Math.imul(nt,gt),u=u+Math.imul(rt,pt)|0,_=Math.imul(rt,gt),p=p+Math.imul(et,_t)|0,u=u+Math.imul(et,wt)|0,u=u+Math.imul(it,_t)|0,_=_+Math.imul(it,wt)|0,p=p+Math.imul(z,bt)|0,u=u+Math.imul(z,vt)|0,u=u+Math.imul(Y,bt)|0,_=_+Math.imul(Y,vt)|0,p=p+Math.imul($,yt)|0,u=u+Math.imul($,Mt)|0,u=u+Math.imul(W,yt)|0,_=_+Math.imul(W,Mt)|0,p=p+Math.imul(T,St)|0,u=u+Math.imul(T,At)|0,u=u+Math.imul(N,St)|0,_=_+Math.imul(N,At)|0;var ni=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(ni>>>26)|0,ni&=67108863,p=Math.imul(ot,pt),u=Math.imul(ot,gt),u=u+Math.imul(st,pt)|0,_=Math.imul(st,gt),p=p+Math.imul(nt,_t)|0,u=u+Math.imul(nt,wt)|0,u=u+Math.imul(rt,_t)|0,_=_+Math.imul(rt,wt)|0,p=p+Math.imul(et,bt)|0,u=u+Math.imul(et,vt)|0,u=u+Math.imul(it,bt)|0,_=_+Math.imul(it,vt)|0,p=p+Math.imul(z,yt)|0,u=u+Math.imul(z,Mt)|0,u=u+Math.imul(Y,yt)|0,_=_+Math.imul(Y,Mt)|0,p=p+Math.imul($,St)|0,u=u+Math.imul($,At)|0,u=u+Math.imul(W,St)|0,_=_+Math.imul(W,At)|0,p=p+Math.imul(T,Ct)|0,u=u+Math.imul(T,kt)|0,u=u+Math.imul(N,Ct)|0,_=_+Math.imul(N,kt)|0;var ri=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(ri>>>26)|0,ri&=67108863,p=Math.imul(at,pt),u=Math.imul(at,gt),u=u+Math.imul(ut,pt)|0,_=Math.imul(ut,gt),p=p+Math.imul(ot,_t)|0,u=u+Math.imul(ot,wt)|0,u=u+Math.imul(st,_t)|0,_=_+Math.imul(st,wt)|0,p=p+Math.imul(nt,bt)|0,u=u+Math.imul(nt,vt)|0,u=u+Math.imul(rt,bt)|0,_=_+Math.imul(rt,vt)|0,p=p+Math.imul(et,yt)|0,u=u+Math.imul(et,Mt)|0,u=u+Math.imul(it,yt)|0,_=_+Math.imul(it,Mt)|0,p=p+Math.imul(z,St)|0,u=u+Math.imul(z,At)|0,u=u+Math.imul(Y,St)|0,_=_+Math.imul(Y,At)|0,p=p+Math.imul($,Ct)|0,u=u+Math.imul($,kt)|0,u=u+Math.imul(W,Ct)|0,_=_+Math.imul(W,kt)|0,p=p+Math.imul(T,xt)|0,u=u+Math.imul(T,Pt)|0,u=u+Math.imul(N,xt)|0,_=_+Math.imul(N,Pt)|0;var oi=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(oi>>>26)|0,oi&=67108863,p=Math.imul(lt,pt),u=Math.imul(lt,gt),u=u+Math.imul(ct,pt)|0,_=Math.imul(ct,gt),p=p+Math.imul(at,_t)|0,u=u+Math.imul(at,wt)|0,u=u+Math.imul(ut,_t)|0,_=_+Math.imul(ut,wt)|0,p=p+Math.imul(ot,bt)|0,u=u+Math.imul(ot,vt)|0,u=u+Math.imul(st,bt)|0,_=_+Math.imul(st,vt)|0,p=p+Math.imul(nt,yt)|0,u=u+Math.imul(nt,Mt)|0,u=u+Math.imul(rt,yt)|0,_=_+Math.imul(rt,Mt)|0,p=p+Math.imul(et,St)|0,u=u+Math.imul(et,At)|0,u=u+Math.imul(it,St)|0,_=_+Math.imul(it,At)|0,p=p+Math.imul(z,Ct)|0,u=u+Math.imul(z,kt)|0,u=u+Math.imul(Y,Ct)|0,_=_+Math.imul(Y,kt)|0,p=p+Math.imul($,xt)|0,u=u+Math.imul($,Pt)|0,u=u+Math.imul(W,xt)|0,_=_+Math.imul(W,Pt)|0,p=p+Math.imul(T,Et)|0,u=u+Math.imul(T,Ot)|0,u=u+Math.imul(N,Et)|0,_=_+Math.imul(N,Ot)|0;var si=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(si>>>26)|0,si&=67108863,p=Math.imul(dt,pt),u=Math.imul(dt,gt),u=u+Math.imul(ht,pt)|0,_=Math.imul(ht,gt),p=p+Math.imul(lt,_t)|0,u=u+Math.imul(lt,wt)|0,u=u+Math.imul(ct,_t)|0,_=_+Math.imul(ct,wt)|0,p=p+Math.imul(at,bt)|0,u=u+Math.imul(at,vt)|0,u=u+Math.imul(ut,bt)|0,_=_+Math.imul(ut,vt)|0,p=p+Math.imul(ot,yt)|0,u=u+Math.imul(ot,Mt)|0,u=u+Math.imul(st,yt)|0,_=_+Math.imul(st,Mt)|0,p=p+Math.imul(nt,St)|0,u=u+Math.imul(nt,At)|0,u=u+Math.imul(rt,St)|0,_=_+Math.imul(rt,At)|0,p=p+Math.imul(et,Ct)|0,u=u+Math.imul(et,kt)|0,u=u+Math.imul(it,Ct)|0,_=_+Math.imul(it,kt)|0,p=p+Math.imul(z,xt)|0,u=u+Math.imul(z,Pt)|0,u=u+Math.imul(Y,xt)|0,_=_+Math.imul(Y,Pt)|0,p=p+Math.imul($,Et)|0,u=u+Math.imul($,Ot)|0,u=u+Math.imul(W,Et)|0,_=_+Math.imul(W,Ot)|0,p=p+Math.imul(T,Tt)|0,u=u+Math.imul(T,It)|0,u=u+Math.imul(N,Tt)|0,_=_+Math.imul(N,It)|0;var ai=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(ai>>>26)|0,ai&=67108863,p=Math.imul(ft,pt),u=Math.imul(ft,gt),u=u+Math.imul(mt,pt)|0,_=Math.imul(mt,gt),p=p+Math.imul(dt,_t)|0,u=u+Math.imul(dt,wt)|0,u=u+Math.imul(ht,_t)|0,_=_+Math.imul(ht,wt)|0,p=p+Math.imul(lt,bt)|0,u=u+Math.imul(lt,vt)|0,u=u+Math.imul(ct,bt)|0,_=_+Math.imul(ct,vt)|0,p=p+Math.imul(at,yt)|0,u=u+Math.imul(at,Mt)|0,u=u+Math.imul(ut,yt)|0,_=_+Math.imul(ut,Mt)|0,p=p+Math.imul(ot,St)|0,u=u+Math.imul(ot,At)|0,u=u+Math.imul(st,St)|0,_=_+Math.imul(st,At)|0,p=p+Math.imul(nt,Ct)|0,u=u+Math.imul(nt,kt)|0,u=u+Math.imul(rt,Ct)|0,_=_+Math.imul(rt,kt)|0,p=p+Math.imul(et,xt)|0,u=u+Math.imul(et,Pt)|0,u=u+Math.imul(it,xt)|0,_=_+Math.imul(it,Pt)|0,p=p+Math.imul(z,Et)|0,u=u+Math.imul(z,Ot)|0,u=u+Math.imul(Y,Et)|0,_=_+Math.imul(Y,Ot)|0,p=p+Math.imul($,Tt)|0,u=u+Math.imul($,It)|0,u=u+Math.imul(W,Tt)|0,_=_+Math.imul(W,It)|0,p=p+Math.imul(T,Bt)|0,u=u+Math.imul(T,Nt)|0,u=u+Math.imul(N,Bt)|0,_=_+Math.imul(N,Nt)|0;var ui=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(ui>>>26)|0,ui&=67108863,p=Math.imul(ft,_t),u=Math.imul(ft,wt),u=u+Math.imul(mt,_t)|0,_=Math.imul(mt,wt),p=p+Math.imul(dt,bt)|0,u=u+Math.imul(dt,vt)|0,u=u+Math.imul(ht,bt)|0,_=_+Math.imul(ht,vt)|0,p=p+Math.imul(lt,yt)|0,u=u+Math.imul(lt,Mt)|0,u=u+Math.imul(ct,yt)|0,_=_+Math.imul(ct,Mt)|0,p=p+Math.imul(at,St)|0,u=u+Math.imul(at,At)|0,u=u+Math.imul(ut,St)|0,_=_+Math.imul(ut,At)|0,p=p+Math.imul(ot,Ct)|0,u=u+Math.imul(ot,kt)|0,u=u+Math.imul(st,Ct)|0,_=_+Math.imul(st,kt)|0,p=p+Math.imul(nt,xt)|0,u=u+Math.imul(nt,Pt)|0,u=u+Math.imul(rt,xt)|0,_=_+Math.imul(rt,Pt)|0,p=p+Math.imul(et,Et)|0,u=u+Math.imul(et,Ot)|0,u=u+Math.imul(it,Et)|0,_=_+Math.imul(it,Ot)|0,p=p+Math.imul(z,Tt)|0,u=u+Math.imul(z,It)|0,u=u+Math.imul(Y,Tt)|0,_=_+Math.imul(Y,It)|0,p=p+Math.imul($,Bt)|0,u=u+Math.imul($,Nt)|0,u=u+Math.imul(W,Bt)|0,_=_+Math.imul(W,Nt)|0;var li=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(li>>>26)|0,li&=67108863,p=Math.imul(ft,bt),u=Math.imul(ft,vt),u=u+Math.imul(mt,bt)|0,_=Math.imul(mt,vt),p=p+Math.imul(dt,yt)|0,u=u+Math.imul(dt,Mt)|0,u=u+Math.imul(ht,yt)|0,_=_+Math.imul(ht,Mt)|0,p=p+Math.imul(lt,St)|0,u=u+Math.imul(lt,At)|0,u=u+Math.imul(ct,St)|0,_=_+Math.imul(ct,At)|0,p=p+Math.imul(at,Ct)|0,u=u+Math.imul(at,kt)|0,u=u+Math.imul(ut,Ct)|0,_=_+Math.imul(ut,kt)|0,p=p+Math.imul(ot,xt)|0,u=u+Math.imul(ot,Pt)|0,u=u+Math.imul(st,xt)|0,_=_+Math.imul(st,Pt)|0,p=p+Math.imul(nt,Et)|0,u=u+Math.imul(nt,Ot)|0,u=u+Math.imul(rt,Et)|0,_=_+Math.imul(rt,Ot)|0,p=p+Math.imul(et,Tt)|0,u=u+Math.imul(et,It)|0,u=u+Math.imul(it,Tt)|0,_=_+Math.imul(it,It)|0,p=p+Math.imul(z,Bt)|0,u=u+Math.imul(z,Nt)|0,u=u+Math.imul(Y,Bt)|0,_=_+Math.imul(Y,Nt)|0;var ci=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(ci>>>26)|0,ci&=67108863,p=Math.imul(ft,yt),u=Math.imul(ft,Mt),u=u+Math.imul(mt,yt)|0,_=Math.imul(mt,Mt),p=p+Math.imul(dt,St)|0,u=u+Math.imul(dt,At)|0,u=u+Math.imul(ht,St)|0,_=_+Math.imul(ht,At)|0,p=p+Math.imul(lt,Ct)|0,u=u+Math.imul(lt,kt)|0,u=u+Math.imul(ct,Ct)|0,_=_+Math.imul(ct,kt)|0,p=p+Math.imul(at,xt)|0,u=u+Math.imul(at,Pt)|0,u=u+Math.imul(ut,xt)|0,_=_+Math.imul(ut,Pt)|0,p=p+Math.imul(ot,Et)|0,u=u+Math.imul(ot,Ot)|0,u=u+Math.imul(st,Et)|0,_=_+Math.imul(st,Ot)|0,p=p+Math.imul(nt,Tt)|0,u=u+Math.imul(nt,It)|0,u=u+Math.imul(rt,Tt)|0,_=_+Math.imul(rt,It)|0,p=p+Math.imul(et,Bt)|0,u=u+Math.imul(et,Nt)|0,u=u+Math.imul(it,Bt)|0,_=_+Math.imul(it,Nt)|0;var di=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(di>>>26)|0,di&=67108863,p=Math.imul(ft,St),u=Math.imul(ft,At),u=u+Math.imul(mt,St)|0,_=Math.imul(mt,At),p=p+Math.imul(dt,Ct)|0,u=u+Math.imul(dt,kt)|0,u=u+Math.imul(ht,Ct)|0,_=_+Math.imul(ht,kt)|0,p=p+Math.imul(lt,xt)|0,u=u+Math.imul(lt,Pt)|0,u=u+Math.imul(ct,xt)|0,_=_+Math.imul(ct,Pt)|0,p=p+Math.imul(at,Et)|0,u=u+Math.imul(at,Ot)|0,u=u+Math.imul(ut,Et)|0,_=_+Math.imul(ut,Ot)|0,p=p+Math.imul(ot,Tt)|0,u=u+Math.imul(ot,It)|0,u=u+Math.imul(st,Tt)|0,_=_+Math.imul(st,It)|0,p=p+Math.imul(nt,Bt)|0,u=u+Math.imul(nt,Nt)|0,u=u+Math.imul(rt,Bt)|0,_=_+Math.imul(rt,Nt)|0;var hi=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(hi>>>26)|0,hi&=67108863,p=Math.imul(ft,Ct),u=Math.imul(ft,kt),u=u+Math.imul(mt,Ct)|0,_=Math.imul(mt,kt),p=p+Math.imul(dt,xt)|0,u=u+Math.imul(dt,Pt)|0,u=u+Math.imul(ht,xt)|0,_=_+Math.imul(ht,Pt)|0,p=p+Math.imul(lt,Et)|0,u=u+Math.imul(lt,Ot)|0,u=u+Math.imul(ct,Et)|0,_=_+Math.imul(ct,Ot)|0,p=p+Math.imul(at,Tt)|0,u=u+Math.imul(at,It)|0,u=u+Math.imul(ut,Tt)|0,_=_+Math.imul(ut,It)|0,p=p+Math.imul(ot,Bt)|0,u=u+Math.imul(ot,Nt)|0,u=u+Math.imul(st,Bt)|0,_=_+Math.imul(st,Nt)|0;var fi=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(fi>>>26)|0,fi&=67108863,p=Math.imul(ft,xt),u=Math.imul(ft,Pt),u=u+Math.imul(mt,xt)|0,_=Math.imul(mt,Pt),p=p+Math.imul(dt,Et)|0,u=u+Math.imul(dt,Ot)|0,u=u+Math.imul(ht,Et)|0,_=_+Math.imul(ht,Ot)|0,p=p+Math.imul(lt,Tt)|0,u=u+Math.imul(lt,It)|0,u=u+Math.imul(ct,Tt)|0,_=_+Math.imul(ct,It)|0,p=p+Math.imul(at,Bt)|0,u=u+Math.imul(at,Nt)|0,u=u+Math.imul(ut,Bt)|0,_=_+Math.imul(ut,Nt)|0;var mi=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(mi>>>26)|0,mi&=67108863,p=Math.imul(ft,Et),u=Math.imul(ft,Ot),u=u+Math.imul(mt,Et)|0,_=Math.imul(mt,Ot),p=p+Math.imul(dt,Tt)|0,u=u+Math.imul(dt,It)|0,u=u+Math.imul(ht,Tt)|0,_=_+Math.imul(ht,It)|0,p=p+Math.imul(lt,Bt)|0,u=u+Math.imul(lt,Nt)|0,u=u+Math.imul(ct,Bt)|0,_=_+Math.imul(ct,Nt)|0;var pi=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(pi>>>26)|0,pi&=67108863,p=Math.imul(ft,Tt),u=Math.imul(ft,It),u=u+Math.imul(mt,Tt)|0,_=Math.imul(mt,It),p=p+Math.imul(dt,Bt)|0,u=u+Math.imul(dt,Nt)|0,u=u+Math.imul(ht,Bt)|0,_=_+Math.imul(ht,Nt)|0;var gi=(M+p|0)+((u&8191)<<13)|0;M=(_+(u>>>13)|0)+(gi>>>26)|0,gi&=67108863,p=Math.imul(ft,Bt),u=Math.imul(ft,Nt),u=u+Math.imul(mt,Bt)|0,_=Math.imul(mt,Nt);var _i=(M+p|0)+((u&8191)<<13)|0;return M=(_+(u>>>13)|0)+(_i>>>26)|0,_i&=67108863,v[0]=Qe,v[1]=ti,v[2]=ei,v[3]=ii,v[4]=ni,v[5]=ri,v[6]=oi,v[7]=si,v[8]=ai,v[9]=ui,v[10]=li,v[11]=ci,v[12]=di,v[13]=hi,v[14]=fi,v[15]=mi,v[16]=pi,v[17]=gi,v[18]=_i,M!==0&&(v[19]=M,c.length++),c};Math.imul||(x=q);function R(r,l,c){c.negative=l.negative^r.negative,c.length=r.length+l.length;for(var f=0,g=0,v=0;v<c.length-1;v++){var M=g;g=0;for(var p=f&67108863,u=Math.min(v,l.length-1),_=Math.max(0,v-r.length+1);_<=u;_++){var O=v-_,T=r.words[O]|0,N=l.words[_]|0,j=T*N,$=j&67108863;M=M+(j/67108864|0)|0,$=$+p|0,p=$&67108863,M=M+($>>>26)|0,g+=M>>>26,M&=67108863}c.words[v]=p,f=M,M=g}return f!==0?c.words[v]=f:c.length--,c._strip()}function Dt(r,l,c){return R(r,l,c)}s.prototype.mulTo=function(r,l){var c,f=this.length+r.length;return this.length===10&&r.length===10?c=x(this,r,l):f<63?c=q(this,r,l):f<1024?c=R(this,r,l):c=Dt(this,r,l),c};function Z(r,l){this.x=r,this.y=l}Z.prototype.makeRBT=function(r){for(var l=new Array(r),c=s.prototype._countBits(r)-1,f=0;f<r;f++)l[f]=this.revBin(f,c,r);return l},Z.prototype.revBin=function(r,l,c){if(r===0||r===c-1)return r;for(var f=0,g=0;g<l;g++)f|=(r&1)<<l-g-1,r>>=1;return f},Z.prototype.permute=function(r,l,c,f,g,v){for(var M=0;M<v;M++)f[M]=l[r[M]],g[M]=c[r[M]]},Z.prototype.transform=function(r,l,c,f,g,v){this.permute(v,r,l,c,f,g);for(var M=1;M<g;M<<=1)for(var p=M<<1,u=Math.cos(2*Math.PI/p),_=Math.sin(2*Math.PI/p),O=0;O<g;O+=p)for(var T=u,N=_,j=0;j<M;j++){var $=c[O+j],W=f[O+j],se=c[O+j+M],z=f[O+j+M],Y=T*se-N*z;z=T*z+N*se,se=Y,c[O+j]=$+se,f[O+j]=W+z,c[O+j+M]=$-se,f[O+j+M]=W-z,j!==p&&(Y=u*T-_*N,N=u*N+_*T,T=Y)}},Z.prototype.guessLen13b=function(r,l){var c=Math.max(l,r)|1,f=c&1,g=0;for(c=c/2|0;c;c=c>>>1)g++;return 1<<g+1+f},Z.prototype.conjugate=function(r,l,c){if(!(c<=1))for(var f=0;f<c/2;f++){var g=r[f];r[f]=r[c-f-1],r[c-f-1]=g,g=l[f],l[f]=-l[c-f-1],l[c-f-1]=-g}},Z.prototype.normalize13b=function(r,l){for(var c=0,f=0;f<l/2;f++){var g=Math.round(r[2*f+1]/l)*8192+Math.round(r[2*f]/l)+c;r[f]=g&67108863,g<67108864?c=0:c=g/67108864|0}return r},Z.prototype.convert13b=function(r,l,c,f){for(var g=0,v=0;v<l;v++)g=g+(r[v]|0),c[2*v]=g&8191,g=g>>>13,c[2*v+1]=g&8191,g=g>>>13;for(v=2*l;v<f;++v)c[v]=0;o(g===0),o((g&-8192)===0)},Z.prototype.stub=function(r){for(var l=new Array(r),c=0;c<r;c++)l[c]=0;return l},Z.prototype.mulp=function(r,l,c){var f=2*this.guessLen13b(r.length,l.length),g=this.makeRBT(f),v=this.stub(f),M=new Array(f),p=new Array(f),u=new Array(f),_=new Array(f),O=new Array(f),T=new Array(f),N=c.words;N.length=f,this.convert13b(r.words,r.length,M,f),this.convert13b(l.words,l.length,_,f),this.transform(M,v,p,u,f,g),this.transform(_,v,O,T,f,g);for(var j=0;j<f;j++){var $=p[j]*O[j]-u[j]*T[j];u[j]=p[j]*T[j]+u[j]*O[j],p[j]=$}return this.conjugate(p,u,f),this.transform(p,u,N,v,f,g),this.conjugate(N,v,f),this.normalize13b(N,f),c.negative=r.negative^l.negative,c.length=r.length+l.length,c._strip()},s.prototype.mul=function(r){var l=new s(null);return l.words=new Array(this.length+r.length),this.mulTo(r,l)},s.prototype.mulf=function(r){var l=new s(null);return l.words=new Array(this.length+r.length),Dt(this,r,l)},s.prototype.imul=function(r){return this.clone().mulTo(r,this)},s.prototype.imuln=function(r){var l=r<0;l&&(r=-r),o(typeof r=="number"),o(r<67108864);for(var c=0,f=0;f<this.length;f++){var g=(this.words[f]|0)*r,v=(g&67108863)+(c&67108863);c>>=26,c+=g/67108864|0,c+=v>>>26,this.words[f]=v&67108863}return c!==0&&(this.words[f]=c,this.length++),this.length=r===0?1:this.length,l?this.ineg():this},s.prototype.muln=function(r){return this.clone().imuln(r)},s.prototype.sqr=function(){return this.mul(this)},s.prototype.isqr=function(){return this.imul(this.clone())},s.prototype.pow=function(r){var l=P(r);if(l.length===0)return new s(1);for(var c=this,f=0;f<l.length&&l[f]===0;f++,c=c.sqr());if(++f<l.length)for(var g=c.sqr();f<l.length;f++,g=g.sqr())l[f]!==0&&(c=c.mul(g));return c},s.prototype.iushln=function(r){o(typeof r=="number"&&r>=0);var l=r%26,c=(r-l)/26,f=67108863>>>26-l<<26-l,g;if(l!==0){var v=0;for(g=0;g<this.length;g++){var M=this.words[g]&f,p=(this.words[g]|0)-M<<l;this.words[g]=p|v,v=M>>>26-l}v&&(this.words[g]=v,this.length++)}if(c!==0){for(g=this.length-1;g>=0;g--)this.words[g+c]=this.words[g];for(g=0;g<c;g++)this.words[g]=0;this.length+=c}return this._strip()},s.prototype.ishln=function(r){return o(this.negative===0),this.iushln(r)},s.prototype.iushrn=function(r,l,c){o(typeof r=="number"&&r>=0);var f;l?f=(l-l%26)/26:f=0;var g=r%26,v=Math.min((r-g)/26,this.length),M=67108863^67108863>>>g<<g,p=c;if(f-=v,f=Math.max(0,f),p){for(var u=0;u<v;u++)p.words[u]=this.words[u];p.length=v}if(v!==0)if(this.length>v)for(this.length-=v,u=0;u<this.length;u++)this.words[u]=this.words[u+v];else this.words[0]=0,this.length=1;var _=0;for(u=this.length-1;u>=0&&(_!==0||u>=f);u--){var O=this.words[u]|0;this.words[u]=_<<26-g|O>>>g,_=O&M}return p&&_!==0&&(p.words[p.length++]=_),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},s.prototype.ishrn=function(r,l,c){return o(this.negative===0),this.iushrn(r,l,c)},s.prototype.shln=function(r){return this.clone().ishln(r)},s.prototype.ushln=function(r){return this.clone().iushln(r)},s.prototype.shrn=function(r){return this.clone().ishrn(r)},s.prototype.ushrn=function(r){return this.clone().iushrn(r)},s.prototype.testn=function(r){o(typeof r=="number"&&r>=0);var l=r%26,c=(r-l)/26,f=1<<l;if(this.length<=c)return!1;var g=this.words[c];return!!(g&f)},s.prototype.imaskn=function(r){o(typeof r=="number"&&r>=0);var l=r%26,c=(r-l)/26;if(o(this.negative===0,"imaskn works only with positive numbers"),this.length<=c)return this;if(l!==0&&c++,this.length=Math.min(c,this.length),l!==0){var f=67108863^67108863>>>l<<l;this.words[this.length-1]&=f}return this._strip()},s.prototype.maskn=function(r){return this.clone().imaskn(r)},s.prototype.iaddn=function(r){return o(typeof r=="number"),o(r<67108864),r<0?this.isubn(-r):this.negative!==0?this.length===1&&(this.words[0]|0)<=r?(this.words[0]=r-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(r),this.negative=1,this):this._iaddn(r)},s.prototype._iaddn=function(r){this.words[0]+=r;for(var l=0;l<this.length&&this.words[l]>=67108864;l++)this.words[l]-=67108864,l===this.length-1?this.words[l+1]=1:this.words[l+1]++;return this.length=Math.max(this.length,l+1),this},s.prototype.isubn=function(r){if(o(typeof r=="number"),o(r<67108864),r<0)return this.iaddn(-r);if(this.negative!==0)return this.negative=0,this.iaddn(r),this.negative=1,this;if(this.words[0]-=r,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var l=0;l<this.length&&this.words[l]<0;l++)this.words[l]+=67108864,this.words[l+1]-=1;return this._strip()},s.prototype.addn=function(r){return this.clone().iaddn(r)},s.prototype.subn=function(r){return this.clone().isubn(r)},s.prototype.iabs=function(){return this.negative=0,this},s.prototype.abs=function(){return this.clone().iabs()},s.prototype._ishlnsubmul=function(r,l,c){var f=r.length+c,g;this._expand(f);var v,M=0;for(g=0;g<r.length;g++){v=(this.words[g+c]|0)+M;var p=(r.words[g]|0)*l;v-=p&67108863,M=(v>>26)-(p/67108864|0),this.words[g+c]=v&67108863}for(;g<this.length-c;g++)v=(this.words[g+c]|0)+M,M=v>>26,this.words[g+c]=v&67108863;if(M===0)return this._strip();for(o(M===-1),M=0,g=0;g<this.length;g++)v=-(this.words[g]|0)+M,M=v>>26,this.words[g]=v&67108863;return this.negative=1,this._strip()},s.prototype._wordDiv=function(r,l){var c=this.length-r.length,f=this.clone(),g=r,v=g.words[g.length-1]|0,M=this._countBits(v);c=26-M,c!==0&&(g=g.ushln(c),f.iushln(c),v=g.words[g.length-1]|0);var p=f.length-g.length,u;if(l!=="mod"){u=new s(null),u.length=p+1,u.words=new Array(u.length);for(var _=0;_<u.length;_++)u.words[_]=0}var O=f.clone()._ishlnsubmul(g,1,p);O.negative===0&&(f=O,u&&(u.words[p]=1));for(var T=p-1;T>=0;T--){var N=(f.words[g.length+T]|0)*67108864+(f.words[g.length+T-1]|0);for(N=Math.min(N/v|0,67108863),f._ishlnsubmul(g,N,T);f.negative!==0;)N--,f.negative=0,f._ishlnsubmul(g,1,T),f.isZero()||(f.negative^=1);u&&(u.words[T]=N)}return u&&u._strip(),f._strip(),l!=="div"&&c!==0&&f.iushrn(c),{div:u||null,mod:f}},s.prototype.divmod=function(r,l,c){if(o(!r.isZero()),this.isZero())return{div:new s(0),mod:new s(0)};var f,g,v;return this.negative!==0&&r.negative===0?(v=this.neg().divmod(r,l),l!=="mod"&&(f=v.div.neg()),l!=="div"&&(g=v.mod.neg(),c&&g.negative!==0&&g.iadd(r)),{div:f,mod:g}):this.negative===0&&r.negative!==0?(v=this.divmod(r.neg(),l),l!=="mod"&&(f=v.div.neg()),{div:f,mod:v.mod}):this.negative&r.negative?(v=this.neg().divmod(r.neg(),l),l!=="div"&&(g=v.mod.neg(),c&&g.negative!==0&&g.isub(r)),{div:v.div,mod:g}):r.length>this.length||this.cmp(r)<0?{div:new s(0),mod:this}:r.length===1?l==="div"?{div:this.divn(r.words[0]),mod:null}:l==="mod"?{div:null,mod:new s(this.modrn(r.words[0]))}:{div:this.divn(r.words[0]),mod:new s(this.modrn(r.words[0]))}:this._wordDiv(r,l)},s.prototype.div=function(r){return this.divmod(r,"div",!1).div},s.prototype.mod=function(r){return this.divmod(r,"mod",!1).mod},s.prototype.umod=function(r){return this.divmod(r,"mod",!0).mod},s.prototype.divRound=function(r){var l=this.divmod(r);if(l.mod.isZero())return l.div;var c=l.div.negative!==0?l.mod.isub(r):l.mod,f=r.ushrn(1),g=r.andln(1),v=c.cmp(f);return v<0||g===1&&v===0?l.div:l.div.negative!==0?l.div.isubn(1):l.div.iaddn(1)},s.prototype.modrn=function(r){var l=r<0;l&&(r=-r),o(r<=67108863);for(var c=(1<<26)%r,f=0,g=this.length-1;g>=0;g--)f=(c*f+(this.words[g]|0))%r;return l?-f:f},s.prototype.modn=function(r){return this.modrn(r)},s.prototype.idivn=function(r){var l=r<0;l&&(r=-r),o(r<=67108863);for(var c=0,f=this.length-1;f>=0;f--){var g=(this.words[f]|0)+c*67108864;this.words[f]=g/r|0,c=g%r}return this._strip(),l?this.ineg():this},s.prototype.divn=function(r){return this.clone().idivn(r)},s.prototype.egcd=function(r){o(r.negative===0),o(!r.isZero());var l=this,c=r.clone();l.negative!==0?l=l.umod(r):l=l.clone();for(var f=new s(1),g=new s(0),v=new s(0),M=new s(1),p=0;l.isEven()&&c.isEven();)l.iushrn(1),c.iushrn(1),++p;for(var u=c.clone(),_=l.clone();!l.isZero();){for(var O=0,T=1;!(l.words[0]&T)&&O<26;++O,T<<=1);if(O>0)for(l.iushrn(O);O-- >0;)(f.isOdd()||g.isOdd())&&(f.iadd(u),g.isub(_)),f.iushrn(1),g.iushrn(1);for(var N=0,j=1;!(c.words[0]&j)&&N<26;++N,j<<=1);if(N>0)for(c.iushrn(N);N-- >0;)(v.isOdd()||M.isOdd())&&(v.iadd(u),M.isub(_)),v.iushrn(1),M.iushrn(1);l.cmp(c)>=0?(l.isub(c),f.isub(v),g.isub(M)):(c.isub(l),v.isub(f),M.isub(g))}return{a:v,b:M,gcd:c.iushln(p)}},s.prototype._invmp=function(r){o(r.negative===0),o(!r.isZero());var l=this,c=r.clone();l.negative!==0?l=l.umod(r):l=l.clone();for(var f=new s(1),g=new s(0),v=c.clone();l.cmpn(1)>0&&c.cmpn(1)>0;){for(var M=0,p=1;!(l.words[0]&p)&&M<26;++M,p<<=1);if(M>0)for(l.iushrn(M);M-- >0;)f.isOdd()&&f.iadd(v),f.iushrn(1);for(var u=0,_=1;!(c.words[0]&_)&&u<26;++u,_<<=1);if(u>0)for(c.iushrn(u);u-- >0;)g.isOdd()&&g.iadd(v),g.iushrn(1);l.cmp(c)>=0?(l.isub(c),f.isub(g)):(c.isub(l),g.isub(f))}var O;return l.cmpn(1)===0?O=f:O=g,O.cmpn(0)<0&&O.iadd(r),O},s.prototype.gcd=function(r){if(this.isZero())return r.abs();if(r.isZero())return this.abs();var l=this.clone(),c=r.clone();l.negative=0,c.negative=0;for(var f=0;l.isEven()&&c.isEven();f++)l.iushrn(1),c.iushrn(1);do{for(;l.isEven();)l.iushrn(1);for(;c.isEven();)c.iushrn(1);var g=l.cmp(c);if(g<0){var v=l;l=c,c=v}else if(g===0||c.cmpn(1)===0)break;l.isub(c)}while(!0);return c.iushln(f)},s.prototype.invm=function(r){return this.egcd(r).a.umod(r)},s.prototype.isEven=function(){return(this.words[0]&1)===0},s.prototype.isOdd=function(){return(this.words[0]&1)===1},s.prototype.andln=function(r){return this.words[0]&r},s.prototype.bincn=function(r){o(typeof r=="number");var l=r%26,c=(r-l)/26,f=1<<l;if(this.length<=c)return this._expand(c+1),this.words[c]|=f,this;for(var g=f,v=c;g!==0&&v<this.length;v++){var M=this.words[v]|0;M+=g,g=M>>>26,M&=67108863,this.words[v]=M}return g!==0&&(this.words[v]=g,this.length++),this},s.prototype.isZero=function(){return this.length===1&&this.words[0]===0},s.prototype.cmpn=function(r){var l=r<0;if(this.negative!==0&&!l)return-1;if(this.negative===0&&l)return 1;this._strip();var c;if(this.length>1)c=1;else{l&&(r=-r),o(r<=67108863,"Number is too big");var f=this.words[0]|0;c=f===r?0:f<r?-1:1}return this.negative!==0?-c|0:c},s.prototype.cmp=function(r){if(this.negative!==0&&r.negative===0)return-1;if(this.negative===0&&r.negative!==0)return 1;var l=this.ucmp(r);return this.negative!==0?-l|0:l},s.prototype.ucmp=function(r){if(this.length>r.length)return 1;if(this.length<r.length)return-1;for(var l=0,c=this.length-1;c>=0;c--){var f=this.words[c]|0,g=r.words[c]|0;if(f!==g){f<g?l=-1:f>g&&(l=1);break}}return l},s.prototype.gtn=function(r){return this.cmpn(r)===1},s.prototype.gt=function(r){return this.cmp(r)===1},s.prototype.gten=function(r){return this.cmpn(r)>=0},s.prototype.gte=function(r){return this.cmp(r)>=0},s.prototype.ltn=function(r){return this.cmpn(r)===-1},s.prototype.lt=function(r){return this.cmp(r)===-1},s.prototype.lten=function(r){return this.cmpn(r)<=0},s.prototype.lte=function(r){return this.cmp(r)<=0},s.prototype.eqn=function(r){return this.cmpn(r)===0},s.prototype.eq=function(r){return this.cmp(r)===0},s.red=function(r){return new U(r)},s.prototype.toRed=function(r){return o(!this.red,"Already a number in reduction context"),o(this.negative===0,"red works only with positives"),r.convertTo(this)._forceRed(r)},s.prototype.fromRed=function(){return o(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},s.prototype._forceRed=function(r){return this.red=r,this},s.prototype.forceRed=function(r){return o(!this.red,"Already a number in reduction context"),this._forceRed(r)},s.prototype.redAdd=function(r){return o(this.red,"redAdd works only with red numbers"),this.red.add(this,r)},s.prototype.redIAdd=function(r){return o(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,r)},s.prototype.redSub=function(r){return o(this.red,"redSub works only with red numbers"),this.red.sub(this,r)},s.prototype.redISub=function(r){return o(this.red,"redISub works only with red numbers"),this.red.isub(this,r)},s.prototype.redShl=function(r){return o(this.red,"redShl works only with red numbers"),this.red.shl(this,r)},s.prototype.redMul=function(r){return o(this.red,"redMul works only with red numbers"),this.red._verify2(this,r),this.red.mul(this,r)},s.prototype.redIMul=function(r){return o(this.red,"redMul works only with red numbers"),this.red._verify2(this,r),this.red.imul(this,r)},s.prototype.redSqr=function(){return o(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},s.prototype.redISqr=function(){return o(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},s.prototype.redSqrt=function(){return o(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},s.prototype.redInvm=function(){return o(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},s.prototype.redNeg=function(){return o(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},s.prototype.redPow=function(r){return o(this.red&&!r.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,r)};var Ut={k256:null,p224:null,p192:null,p25519:null};function G(r,l){this.name=r,this.p=new s(l,16),this.n=this.p.bitLength(),this.k=new s(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}G.prototype._tmp=function(){var r=new s(null);return r.words=new Array(Math.ceil(this.n/13)),r},G.prototype.ireduce=function(r){var l=r,c;do this.split(l,this.tmp),l=this.imulK(l),l=l.iadd(this.tmp),c=l.bitLength();while(c>this.n);var f=c<this.n?-1:l.ucmp(this.p);return f===0?(l.words[0]=0,l.length=1):f>0?l.isub(this.p):l.strip!==void 0?l.strip():l._strip(),l},G.prototype.split=function(r,l){r.iushrn(this.n,0,l)},G.prototype.imulK=function(r){return r.imul(this.k)};function K(){G.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}a(K,G),K.prototype.split=function(r,l){for(var c=4194303,f=Math.min(r.length,9),g=0;g<f;g++)l.words[g]=r.words[g];if(l.length=f,r.length<=9){r.words[0]=0,r.length=1;return}var v=r.words[9];for(l.words[l.length++]=v&c,g=10;g<r.length;g++){var M=r.words[g]|0;r.words[g-10]=(M&c)<<4|v>>>22,v=M}v>>>=22,r.words[g-10]=v,v===0&&r.length>10?r.length-=10:r.length-=9},K.prototype.imulK=function(r){r.words[r.length]=0,r.words[r.length+1]=0,r.length+=2;for(var l=0,c=0;c<r.length;c++){var f=r.words[c]|0;l+=f*977,r.words[c]=l&67108863,l=f*64+(l/67108864|0)}return r.words[r.length-1]===0&&(r.length--,r.words[r.length-1]===0&&r.length--),r};function X(){G.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}a(X,G);function Q(){G.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}a(Q,G);function Vt(){G.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}a(Vt,G),Vt.prototype.imulK=function(r){for(var l=0,c=0;c<r.length;c++){var f=(r.words[c]|0)*19+l,g=f&67108863;f>>>=26,r.words[c]=g,l=f}return l!==0&&(r.words[r.length++]=l),r},s._prime=function(r){if(Ut[r])return Ut[r];var l;if(r==="k256")l=new K;else if(r==="p224")l=new X;else if(r==="p192")l=new Q;else if(r==="p25519")l=new Vt;else throw new Error("Unknown prime "+r);return Ut[r]=l,l};function U(r){if(typeof r=="string"){var l=s._prime(r);this.m=l.p,this.prime=l}else o(r.gtn(1),"modulus must be greater than 1"),this.m=r,this.prime=null}U.prototype._verify1=function(r){o(r.negative===0,"red works only with positives"),o(r.red,"red works only with red numbers")},U.prototype._verify2=function(r,l){o((r.negative|l.negative)===0,"red works only with positives"),o(r.red&&r.red===l.red,"red works only with red numbers")},U.prototype.imod=function(r){return this.prime?this.prime.ireduce(r)._forceRed(this):(b(r,r.umod(this.m)._forceRed(this)),r)},U.prototype.neg=function(r){return r.isZero()?r.clone():this.m.sub(r)._forceRed(this)},U.prototype.add=function(r,l){this._verify2(r,l);var c=r.add(l);return c.cmp(this.m)>=0&&c.isub(this.m),c._forceRed(this)},U.prototype.iadd=function(r,l){this._verify2(r,l);var c=r.iadd(l);return c.cmp(this.m)>=0&&c.isub(this.m),c},U.prototype.sub=function(r,l){this._verify2(r,l);var c=r.sub(l);return c.cmpn(0)<0&&c.iadd(this.m),c._forceRed(this)},U.prototype.isub=function(r,l){this._verify2(r,l);var c=r.isub(l);return c.cmpn(0)<0&&c.iadd(this.m),c},U.prototype.shl=function(r,l){return this._verify1(r),this.imod(r.ushln(l))},U.prototype.imul=function(r,l){return this._verify2(r,l),this.imod(r.imul(l))},U.prototype.mul=function(r,l){return this._verify2(r,l),this.imod(r.mul(l))},U.prototype.isqr=function(r){return this.imul(r,r.clone())},U.prototype.sqr=function(r){return this.mul(r,r)},U.prototype.sqrt=function(r){if(r.isZero())return r.clone();var l=this.m.andln(3);if(o(l%2===1),l===3){var c=this.m.add(new s(1)).iushrn(2);return this.pow(r,c)}for(var f=this.m.subn(1),g=0;!f.isZero()&&f.andln(1)===0;)g++,f.iushrn(1);o(!f.isZero());var v=new s(1).toRed(this),M=v.redNeg(),p=this.m.subn(1).iushrn(1),u=this.m.bitLength();for(u=new s(2*u*u).toRed(this);this.pow(u,p).cmp(M)!==0;)u.redIAdd(M);for(var _=this.pow(u,f),O=this.pow(r,f.addn(1).iushrn(1)),T=this.pow(r,f),N=g;T.cmp(v)!==0;){for(var j=T,$=0;j.cmp(v)!==0;$++)j=j.redSqr();o($<N);var W=this.pow(_,new s(1).iushln(N-$-1));O=O.redMul(W),_=W.redSqr(),T=T.redMul(_),N=$}return O},U.prototype.invm=function(r){var l=r._invmp(this.m);return l.negative!==0?(l.negative=0,this.imod(l).redNeg()):this.imod(l)},U.prototype.pow=function(r,l){if(l.isZero())return new s(1).toRed(this);if(l.cmpn(1)===0)return r.clone();var c=4,f=new Array(1<<c);f[0]=new s(1).toRed(this),f[1]=r;for(var g=2;g<f.length;g++)f[g]=this.mul(f[g-1],r);var v=f[0],M=0,p=0,u=l.bitLength()%26;for(u===0&&(u=26),g=l.length-1;g>=0;g--){for(var _=l.words[g],O=u-1;O>=0;O--){var T=_>>O&1;if(v!==f[0]&&(v=this.sqr(v)),T===0&&M===0){p=0;continue}M<<=1,M|=T,p++,!(p!==c&&(g!==0||O!==0))&&(v=this.mul(v,f[M]),p=0,M=0)}u=26}return v},U.prototype.convertTo=function(r){var l=r.umod(this.m);return l===r?l.clone():l},U.prototype.convertFrom=function(r){var l=r.clone();return l.red=null,l},s.mont=function(r){return new Zt(r)};function Zt(r){U.call(this,r),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new s(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}a(Zt,U),Zt.prototype.convertTo=function(r){return this.imod(r.ushln(this.shift))},Zt.prototype.convertFrom=function(r){var l=this.imod(r.mul(this.rinv));return l.red=null,l},Zt.prototype.imul=function(r,l){if(r.isZero()||l.isZero())return r.words[0]=0,r.length=1,r;var c=r.imul(l),f=c.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),g=c.isub(f).iushrn(this.shift),v=g;return g.cmp(this.m)>=0?v=g.isub(this.m):g.cmpn(0)<0&&(v=g.iadd(this.m)),v._forceRed(this)},Zt.prototype.mul=function(r,l){if(r.isZero()||l.isZero())return new s(0)._forceRed(this);var c=r.mul(l),f=c.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),g=c.isub(f).iushrn(this.shift),v=g;return g.cmp(this.m)>=0?v=g.isub(this.m):g.cmpn(0)<0&&(v=g.iadd(this.m)),v._forceRed(this)},Zt.prototype.invm=function(r){var l=this.imod(r._invmp(this.m).mul(this.r2));return l._forceRed(this)}})(typeof t>"u"||t,i)}),wi={REQUEST_PARAMS:"requestParams",METHOD_NAME:"methodName"},cr=class Sn extends Error{constructor(t,e,n){super(t),this.code=e,this.details=n,this.name=this.constructor.name}static{this.DETAILS_KEYS=wi}toJSON(){return{name:this.name,message:this.message,code:this.code,details:this.details}}toString(){return JSON.stringify(this.toJSON())}static isErrorCode(t,e){return t instanceof Sn&&t.code===e}},dr=class extends cr{constructor(i,t,e){super(i,t||"UnknownError",e)}static isCommonErrorCode(i,t){return this.isErrorCode(i,t)}},Wt=(i,t,e)=>{throw new dr(t,i,e)},he="0x0000000000000000000000000000000000000000000000000000000000000006";var Do=(0,Ai.getFullnodeUrl)("mainnet"),qo=(0,Ai.getFullnodeUrl)("testnet");var An=864e5;var be=9e15,me=1e9,vi="0123456789abcdef",Le="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",je="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",yi={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-be,maxE:be,crypto:!1},Cn,ie,L=!0,$e="[DecimalError] ",de=$e+"Invalid argument: ",kn=$e+"Precision limit exceeded",xn=$e+"crypto unavailable",Pn="[object Decimal]",Kt=Math.floor,Rt=Math.pow,gr=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,_r=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,wr=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,En=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,Gt=1e7,D=7,br=9007199254740991,vr=Le.length-1,Mi=je.length-1,S={toStringTag:Pn};S.absoluteValue=S.abs=function(){var i=new this.constructor(this);return i.s<0&&(i.s=1),I(i)};S.ceil=function(){return I(new this.constructor(this),this.e+1,2)};S.clampedTo=S.clamp=function(i,t){var e,n=this,o=n.constructor;if(i=new o(i),t=new o(t),!i.s||!t.s)return new o(NaN);if(i.gt(t))throw Error(de+t);return e=n.cmp(i),e<0?i:n.cmp(t)>0?t:new o(n)};S.comparedTo=S.cmp=function(i){var t,e,n,o,a=this,s=a.d,d=(i=new a.constructor(i)).d,h=a.s,m=i.s;if(!s||!d)return!h||!m?NaN:h!==m?h:s===d?0:!s^h<0?1:-1;if(!s[0]||!d[0])return s[0]?h:d[0]?-m:0;if(h!==m)return h;if(a.e!==i.e)return a.e>i.e^h<0?1:-1;for(n=s.length,o=d.length,t=0,e=n<o?n:o;t<e;++t)if(s[t]!==d[t])return s[t]>d[t]^h<0?1:-1;return n===o?0:n>o^h<0?1:-1};S.cosine=S.cos=function(){var i,t,e=this,n=e.constructor;return e.d?e.d[0]?(i=n.precision,t=n.rounding,n.precision=i+Math.max(e.e,e.sd())+D,n.rounding=1,e=yr(n,Nn(n,e)),n.precision=i,n.rounding=t,I(ie==2||ie==3?e.neg():e,i,t,!0)):new n(1):new n(NaN)};S.cubeRoot=S.cbrt=function(){var i,t,e,n,o,a,s,d,h,m,w=this,b=w.constructor;if(!w.isFinite()||w.isZero())return new b(w);for(L=!1,a=w.s*Rt(w.s*w,1/3),!a||Math.abs(a)==1/0?(e=Lt(w.d),i=w.e,(a=(i-e.length+1)%3)&&(e+=a==1||a==-2?"0":"00"),a=Rt(e,1/3),i=Kt((i+1)/3)-(i%3==(i<0?-1:2)),a==1/0?e="5e"+i:(e=a.toExponential(),e=e.slice(0,e.indexOf("e")+1)+i),n=new b(e),n.s=w.s):n=new b(a.toString()),s=(i=b.precision)+3;;)if(d=n,h=d.times(d).times(d),m=h.plus(w),n=H(m.plus(w).times(d),m.plus(h),s+2,1),Lt(d.d).slice(0,s)===(e=Lt(n.d)).slice(0,s))if(e=e.slice(s-3,s+1),e=="9999"||!o&&e=="4999"){if(!o&&(I(d,i+1,0),d.times(d).times(d).eq(w))){n=d;break}s+=4,o=1}else{(!+e||!+e.slice(1)&&e.charAt(0)=="5")&&(I(n,i+1,1),t=!n.times(n).times(n).eq(w));break}return L=!0,I(n,i,b.rounding,t)};S.decimalPlaces=S.dp=function(){var i,t=this.d,e=NaN;if(t){if(i=t.length-1,e=(i-Kt(this.e/D))*D,i=t[i],i)for(;i%10==0;i/=10)e--;e<0&&(e=0)}return e};S.dividedBy=S.div=function(i){return H(this,new this.constructor(i))};S.dividedToIntegerBy=S.divToInt=function(i){var t=this,e=t.constructor;return I(H(t,new e(i),0,1,1),e.precision,e.rounding)};S.equals=S.eq=function(i){return this.cmp(i)===0};S.floor=function(){return I(new this.constructor(this),this.e+1,3)};S.greaterThan=S.gt=function(i){return this.cmp(i)>0};S.greaterThanOrEqualTo=S.gte=function(i){var t=this.cmp(i);return t==1||t===0};S.hyperbolicCosine=S.cosh=function(){var i,t,e,n,o,a=this,s=a.constructor,d=new s(1);if(!a.isFinite())return new s(a.s?1/0:NaN);if(a.isZero())return d;e=s.precision,n=s.rounding,s.precision=e+Math.max(a.e,a.sd())+4,s.rounding=1,o=a.d.length,o<32?(i=Math.ceil(o/3),t=(1/Ze(4,i)).toString()):(i=16,t="2.3283064365386962890625e-10"),a=ve(s,1,a.times(t),new s(1),!0);for(var h,m=i,w=new s(8);m--;)h=a.times(a),a=d.minus(h.times(w.minus(h.times(w))));return I(a,s.precision=e,s.rounding=n,!0)};S.hyperbolicSine=S.sinh=function(){var i,t,e,n,o=this,a=o.constructor;if(!o.isFinite()||o.isZero())return new a(o);if(t=a.precision,e=a.rounding,a.precision=t+Math.max(o.e,o.sd())+4,a.rounding=1,n=o.d.length,n<3)o=ve(a,2,o,o,!0);else{i=1.4*Math.sqrt(n),i=i>16?16:i|0,o=o.times(1/Ze(5,i)),o=ve(a,2,o,o,!0);for(var s,d=new a(5),h=new a(16),m=new a(20);i--;)s=o.times(o),o=o.times(d.plus(s.times(h.times(s).plus(m))))}return a.precision=t,a.rounding=e,I(o,t,e,!0)};S.hyperbolicTangent=S.tanh=function(){var i,t,e=this,n=e.constructor;return e.isFinite()?e.isZero()?new n(e):(i=n.precision,t=n.rounding,n.precision=i+7,n.rounding=1,H(e.sinh(),e.cosh(),n.precision=i,n.rounding=t)):new n(e.s)};S.inverseCosine=S.acos=function(){var i=this,t=i.constructor,e=i.abs().cmp(1),n=t.precision,o=t.rounding;return e!==-1?e===0?i.isNeg()?Qt(t,n,o):new t(0):new t(NaN):i.isZero()?Qt(t,n+4,o).times(.5):(t.precision=n+6,t.rounding=1,i=new t(1).minus(i).div(i.plus(1)).sqrt().atan(),t.precision=n,t.rounding=o,i.times(2))};S.inverseHyperbolicCosine=S.acosh=function(){var i,t,e=this,n=e.constructor;return e.lte(1)?new n(e.eq(1)?0:NaN):e.isFinite()?(i=n.precision,t=n.rounding,n.precision=i+Math.max(Math.abs(e.e),e.sd())+4,n.rounding=1,L=!1,e=e.times(e).minus(1).sqrt().plus(e),L=!0,n.precision=i,n.rounding=t,e.ln()):new n(e)};S.inverseHyperbolicSine=S.asinh=function(){var i,t,e=this,n=e.constructor;return!e.isFinite()||e.isZero()?new n(e):(i=n.precision,t=n.rounding,n.precision=i+2*Math.max(Math.abs(e.e),e.sd())+6,n.rounding=1,L=!1,e=e.times(e).plus(1).sqrt().plus(e),L=!0,n.precision=i,n.rounding=t,e.ln())};S.inverseHyperbolicTangent=S.atanh=function(){var i,t,e,n,o=this,a=o.constructor;return o.isFinite()?o.e>=0?new a(o.abs().eq(1)?o.s/0:o.isZero()?o:NaN):(i=a.precision,t=a.rounding,n=o.sd(),Math.max(n,i)<2*-o.e-1?I(new a(o),i,t,!0):(a.precision=e=n-o.e,o=H(o.plus(1),new a(1).minus(o),e+i,1),a.precision=i+4,a.rounding=1,o=o.ln(),a.precision=i,a.rounding=t,o.times(.5))):new a(NaN)};S.inverseSine=S.asin=function(){var i,t,e,n,o=this,a=o.constructor;return o.isZero()?new a(o):(t=o.abs().cmp(1),e=a.precision,n=a.rounding,t!==-1?t===0?(i=Qt(a,e+4,n).times(.5),i.s=o.s,i):new a(NaN):(a.precision=e+6,a.rounding=1,o=o.div(new a(1).minus(o.times(o)).sqrt().plus(1)).atan(),a.precision=e,a.rounding=n,o.times(2)))};S.inverseTangent=S.atan=function(){var i,t,e,n,o,a,s,d,h,m=this,w=m.constructor,b=w.precision,y=w.rounding;if(m.isFinite()){if(m.isZero())return new w(m);if(m.abs().eq(1)&&b+4<=Mi)return s=Qt(w,b+4,y).times(.25),s.s=m.s,s}else{if(!m.s)return new w(NaN);if(b+4<=Mi)return s=Qt(w,b+4,y).times(.5),s.s=m.s,s}for(w.precision=d=b+10,w.rounding=1,e=Math.min(28,d/D+2|0),i=e;i;--i)m=m.div(m.times(m).plus(1).sqrt().plus(1));for(L=!1,t=Math.ceil(d/D),n=1,h=m.times(m),s=new w(m),o=m;i!==-1;)if(o=o.times(h),a=s.minus(o.div(n+=2)),o=o.times(h),s=a.plus(o.div(n+=2)),s.d[t]!==void 0)for(i=t;s.d[i]===a.d[i]&&i--;);return e&&(s=s.times(2<<e-1)),L=!0,I(s,w.precision=b,w.rounding=y,!0)};S.isFinite=function(){return!!this.d};S.isInteger=S.isInt=function(){return!!this.d&&Kt(this.e/D)>this.d.length-2};S.isNaN=function(){return!this.s};S.isNegative=S.isNeg=function(){return this.s<0};S.isPositive=S.isPos=function(){return this.s>0};S.isZero=function(){return!!this.d&&this.d[0]===0};S.lessThan=S.lt=function(i){return this.cmp(i)<0};S.lessThanOrEqualTo=S.lte=function(i){return this.cmp(i)<1};S.logarithm=S.log=function(i){var t,e,n,o,a,s,d,h,m=this,w=m.constructor,b=w.precision,y=w.rounding,A=5;if(i==null)i=new w(10),t=!0;else{if(i=new w(i),e=i.d,i.s<0||!e||!e[0]||i.eq(1))return new w(NaN);t=i.eq(10)}if(e=m.d,m.s<0||!e||!e[0]||m.eq(1))return new w(e&&!e[0]?-1/0:m.s!=1?NaN:e?0:1/0);if(t)if(e.length>1)a=!0;else{for(o=e[0];o%10===0;)o/=10;a=o!==1}if(L=!1,d=b+A,s=ue(m,d),n=t?Ue(w,d+10):ue(i,d),h=H(s,n,d,1),Ce(h.d,o=b,y))do if(d+=10,s=ue(m,d),n=t?Ue(w,d+10):ue(i,d),h=H(s,n,d,1),!a){+Lt(h.d).slice(o+1,o+15)+1==1e14&&(h=I(h,b+1,0));break}while(Ce(h.d,o+=10,y));return L=!0,I(h,b,y)};S.minus=S.sub=function(i){var t,e,n,o,a,s,d,h,m,w,b,y,A=this,C=A.constructor;if(i=new C(i),!A.d||!i.d)return!A.s||!i.s?i=new C(NaN):A.d?i.s=-i.s:i=new C(i.d||A.s!==i.s?A:NaN),i;if(A.s!=i.s)return i.s=-i.s,A.plus(i);if(m=A.d,y=i.d,d=C.precision,h=C.rounding,!m[0]||!y[0]){if(y[0])i.s=-i.s;else if(m[0])i=new C(A);else return new C(h===3?-0:0);return L?I(i,d,h):i}if(e=Kt(i.e/D),w=Kt(A.e/D),m=m.slice(),a=w-e,a){for(b=a<0,b?(t=m,a=-a,s=y.length):(t=y,e=w,s=m.length),n=Math.max(Math.ceil(d/D),s)+2,a>n&&(a=n,t.length=1),t.reverse(),n=a;n--;)t.push(0);t.reverse()}else{for(n=m.length,s=y.length,b=n<s,b&&(s=n),n=0;n<s;n++)if(m[n]!=y[n]){b=m[n]<y[n];break}a=0}for(b&&(t=m,m=y,y=t,i.s=-i.s),s=m.length,n=y.length-s;n>0;--n)m[s++]=0;for(n=y.length;n>a;){if(m[--n]<y[n]){for(o=n;o&&m[--o]===0;)m[o]=Gt-1;--m[o],m[n]+=Gt}m[n]-=y[n]}for(;m[--s]===0;)m.pop();for(;m[0]===0;m.shift())--e;return m[0]?(i.d=m,i.e=Ke(m,e),L?I(i,d,h):i):new C(h===3?-0:0)};S.modulo=S.mod=function(i){var t,e=this,n=e.constructor;return i=new n(i),!e.d||!i.s||i.d&&!i.d[0]?new n(NaN):!i.d||e.d&&!e.d[0]?I(new n(e),n.precision,n.rounding):(L=!1,n.modulo==9?(t=H(e,i.abs(),0,3,1),t.s*=i.s):t=H(e,i,0,n.modulo,1),t=t.times(i),L=!0,e.minus(t))};S.naturalExponential=S.exp=function(){return Si(this)};S.naturalLogarithm=S.ln=function(){return ue(this)};S.negated=S.neg=function(){var i=new this.constructor(this);return i.s=-i.s,I(i)};S.plus=S.add=function(i){var t,e,n,o,a,s,d,h,m,w,b=this,y=b.constructor;if(i=new y(i),!b.d||!i.d)return!b.s||!i.s?i=new y(NaN):b.d||(i=new y(i.d||b.s===i.s?b:NaN)),i;if(b.s!=i.s)return i.s=-i.s,b.minus(i);if(m=b.d,w=i.d,d=y.precision,h=y.rounding,!m[0]||!w[0])return w[0]||(i=new y(b)),L?I(i,d,h):i;if(a=Kt(b.e/D),n=Kt(i.e/D),m=m.slice(),o=a-n,o){for(o<0?(e=m,o=-o,s=w.length):(e=w,n=a,s=m.length),a=Math.ceil(d/D),s=a>s?a+1:s+1,o>s&&(o=s,e.length=1),e.reverse();o--;)e.push(0);e.reverse()}for(s=m.length,o=w.length,s-o<0&&(o=s,e=w,w=m,m=e),t=0;o;)t=(m[--o]=m[o]+w[o]+t)/Gt|0,m[o]%=Gt;for(t&&(m.unshift(t),++n),s=m.length;m[--s]==0;)m.pop();return i.d=m,i.e=Ke(m,n),L?I(i,d,h):i};S.precision=S.sd=function(i){var t,e=this;if(i!==void 0&&i!==!!i&&i!==1&&i!==0)throw Error(de+i);return e.d?(t=On(e.d),i&&e.e+1>t&&(t=e.e+1)):t=NaN,t};S.round=function(){var i=this,t=i.constructor;return I(new t(i),i.e+1,t.rounding)};S.sine=S.sin=function(){var i,t,e=this,n=e.constructor;return e.isFinite()?e.isZero()?new n(e):(i=n.precision,t=n.rounding,n.precision=i+Math.max(e.e,e.sd())+D,n.rounding=1,e=Sr(n,Nn(n,e)),n.precision=i,n.rounding=t,I(ie>2?e.neg():e,i,t,!0)):new n(NaN)};S.squareRoot=S.sqrt=function(){var i,t,e,n,o,a,s=this,d=s.d,h=s.e,m=s.s,w=s.constructor;if(m!==1||!d||!d[0])return new w(!m||m<0&&(!d||d[0])?NaN:d?s:1/0);for(L=!1,m=Math.sqrt(+s),m==0||m==1/0?(t=Lt(d),(t.length+h)%2==0&&(t+="0"),m=Math.sqrt(t),h=Kt((h+1)/2)-(h<0||h%2),m==1/0?t="5e"+h:(t=m.toExponential(),t=t.slice(0,t.indexOf("e")+1)+h),n=new w(t)):n=new w(m.toString()),e=(h=w.precision)+3;;)if(a=n,n=a.plus(H(s,a,e+2,1)).times(.5),Lt(a.d).slice(0,e)===(t=Lt(n.d)).slice(0,e))if(t=t.slice(e-3,e+1),t=="9999"||!o&&t=="4999"){if(!o&&(I(a,h+1,0),a.times(a).eq(s))){n=a;break}e+=4,o=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(I(n,h+1,1),i=!n.times(n).eq(s));break}return L=!0,I(n,h,w.rounding,i)};S.tangent=S.tan=function(){var i,t,e=this,n=e.constructor;return e.isFinite()?e.isZero()?new n(e):(i=n.precision,t=n.rounding,n.precision=i+10,n.rounding=1,e=e.sin(),e.s=1,e=H(e,new n(1).minus(e.times(e)).sqrt(),i+10,0),n.precision=i,n.rounding=t,I(ie==2||ie==4?e.neg():e,i,t,!0)):new n(NaN)};S.times=S.mul=function(i){var t,e,n,o,a,s,d,h,m,w=this,b=w.constructor,y=w.d,A=(i=new b(i)).d;if(i.s*=w.s,!y||!y[0]||!A||!A[0])return new b(!i.s||y&&!y[0]&&!A||A&&!A[0]&&!y?NaN:!y||!A?i.s/0:i.s*0);for(e=Kt(w.e/D)+Kt(i.e/D),h=y.length,m=A.length,h<m&&(a=y,y=A,A=a,s=h,h=m,m=s),a=[],s=h+m,n=s;n--;)a.push(0);for(n=m;--n>=0;){for(t=0,o=h+n;o>n;)d=a[o]+A[n]*y[o-n-1]+t,a[o--]=d%Gt|0,t=d/Gt|0;a[o]=(a[o]+t)%Gt|0}for(;!a[--s];)a.pop();return t?++e:a.shift(),i.d=a,i.e=Ke(a,e),L?I(i,b.precision,b.rounding):i};S.toBinary=function(i,t){return Ci(this,2,i,t)};S.toDecimalPlaces=S.toDP=function(i,t){var e=this,n=e.constructor;return e=new n(e),i===void 0?e:(Xt(i,0,me),t===void 0?t=n.rounding:Xt(t,0,8),I(e,i+e.e+1,t))};S.toExponential=function(i,t){var e,n=this,o=n.constructor;return i===void 0?e=te(n,!0):(Xt(i,0,me),t===void 0?t=o.rounding:Xt(t,0,8),n=I(new o(n),i+1,t),e=te(n,!0,i+1)),n.isNeg()&&!n.isZero()?"-"+e:e};S.toFixed=function(i,t){var e,n,o=this,a=o.constructor;return i===void 0?e=te(o):(Xt(i,0,me),t===void 0?t=a.rounding:Xt(t,0,8),n=I(new a(o),i+o.e+1,t),e=te(n,!1,i+n.e+1)),o.isNeg()&&!o.isZero()?"-"+e:e};S.toFraction=function(i){var t,e,n,o,a,s,d,h,m,w,b,y,A=this,C=A.d,k=A.constructor;if(!C)return new k(A);if(m=e=new k(1),n=h=new k(0),t=new k(n),a=t.e=On(C)-A.e-1,s=a%D,t.d[0]=Rt(10,s<0?D+s:s),i==null)i=a>0?t:m;else{if(d=new k(i),!d.isInt()||d.lt(m))throw Error(de+d);i=d.gt(t)?a>0?t:m:d}for(L=!1,d=new k(Lt(C)),w=k.precision,k.precision=a=C.length*D*2;b=H(d,t,0,1,1),o=e.plus(b.times(n)),o.cmp(i)!=1;)e=n,n=o,o=m,m=h.plus(b.times(o)),h=o,o=t,t=d.minus(b.times(o)),d=o;return o=H(i.minus(e),n,0,1,1),h=h.plus(o.times(m)),e=e.plus(o.times(n)),h.s=m.s=A.s,y=H(m,n,a,1).minus(A).abs().cmp(H(h,e,a,1).minus(A).abs())<1?[m,n]:[h,e],k.precision=w,L=!0,y};S.toHexadecimal=S.toHex=function(i,t){return Ci(this,16,i,t)};S.toNearest=function(i,t){var e=this,n=e.constructor;if(e=new n(e),i==null){if(!e.d)return e;i=new n(1),t=n.rounding}else{if(i=new n(i),t===void 0?t=n.rounding:Xt(t,0,8),!e.d)return i.s?e:i;if(!i.d)return i.s&&(i.s=e.s),i}return i.d[0]?(L=!1,e=H(e,i,0,t,1).times(i),L=!0,I(e)):(i.s=e.s,e=i),e};S.toNumber=function(){return+this};S.toOctal=function(i,t){return Ci(this,8,i,t)};S.toPower=S.pow=function(i){var t,e,n,o,a,s,d=this,h=d.constructor,m=+(i=new h(i));if(!d.d||!i.d||!d.d[0]||!i.d[0])return new h(Rt(+d,m));if(d=new h(d),d.eq(1))return d;if(n=h.precision,a=h.rounding,i.eq(1))return I(d,n,a);if(t=Kt(i.e/D),t>=i.d.length-1&&(e=m<0?-m:m)<=br)return o=Tn(h,d,e,n),i.s<0?new h(1).div(o):I(o,n,a);if(s=d.s,s<0){if(t<i.d.length-1)return new h(NaN);if(!(i.d[t]&1)&&(s=1),d.e==0&&d.d[0]==1&&d.d.length==1)return d.s=s,d}return e=Rt(+d,m),t=e==0||!isFinite(e)?Kt(m*(Math.log("0."+Lt(d.d))/Math.LN10+d.e+1)):new h(e+"").e,t>h.maxE+1||t<h.minE-1?new h(t>0?s/0:0):(L=!1,h.rounding=d.s=1,e=Math.min(12,(t+"").length),o=Si(i.times(ue(d,n+e)),n),o.d&&(o=I(o,n+5,1),Ce(o.d,n,a)&&(t=n+10,o=I(Si(i.times(ue(d,t+e)),t),t+5,1),+Lt(o.d).slice(n+1,n+15)+1==1e14&&(o=I(o,n+1,0)))),o.s=s,L=!0,h.rounding=a,I(o,n,a))};S.toPrecision=function(i,t){var e,n=this,o=n.constructor;return i===void 0?e=te(n,n.e<=o.toExpNeg||n.e>=o.toExpPos):(Xt(i,1,me),t===void 0?t=o.rounding:Xt(t,0,8),n=I(new o(n),i,t),e=te(n,i<=n.e||n.e<=o.toExpNeg,i)),n.isNeg()&&!n.isZero()?"-"+e:e};S.toSignificantDigits=S.toSD=function(i,t){var e=this,n=e.constructor;return i===void 0?(i=n.precision,t=n.rounding):(Xt(i,1,me),t===void 0?t=n.rounding:Xt(t,0,8)),I(new n(e),i,t)};S.toString=function(){var i=this,t=i.constructor,e=te(i,i.e<=t.toExpNeg||i.e>=t.toExpPos);return i.isNeg()&&!i.isZero()?"-"+e:e};S.truncated=S.trunc=function(){return I(new this.constructor(this),this.e+1,1)};S.valueOf=S.toJSON=function(){var i=this,t=i.constructor,e=te(i,i.e<=t.toExpNeg||i.e>=t.toExpPos);return i.isNeg()?"-"+e:e};function Lt(i){var t,e,n,o=i.length-1,a="",s=i[0];if(o>0){for(a+=s,t=1;t<o;t++)n=i[t]+"",e=D-n.length,e&&(a+=ae(e)),a+=n;s=i[t],n=s+"",e=D-n.length,e&&(a+=ae(e))}else if(s===0)return"0";for(;s%10===0;)s/=10;return a+s}function Xt(i,t,e){if(i!==~~i||i<t||i>e)throw Error(de+i)}function Ce(i,t,e,n){var o,a,s,d;for(a=i[0];a>=10;a/=10)--t;return--t<0?(t+=D,o=0):(o=Math.ceil((t+1)/D),t%=D),a=Rt(10,D-t),d=i[o]%a|0,n==null?t<3?(t==0?d=d/100|0:t==1&&(d=d/10|0),s=e<4&&d==99999||e>3&&d==49999||d==5e4||d==0):s=(e<4&&d+1==a||e>3&&d+1==a/2)&&(i[o+1]/a/100|0)==Rt(10,t-2)-1||(d==a/2||d==0)&&(i[o+1]/a/100|0)==0:t<4?(t==0?d=d/1e3|0:t==1?d=d/100|0:t==2&&(d=d/10|0),s=(n||e<4)&&d==9999||!n&&e>3&&d==4999):s=((n||e<4)&&d+1==a||!n&&e>3&&d+1==a/2)&&(i[o+1]/a/1e3|0)==Rt(10,t-3)-1,s}function qe(i,t,e){for(var n,o=[0],a,s=0,d=i.length;s<d;){for(a=o.length;a--;)o[a]*=t;for(o[0]+=vi.indexOf(i.charAt(s++)),n=0;n<o.length;n++)o[n]>e-1&&(o[n+1]===void 0&&(o[n+1]=0),o[n+1]+=o[n]/e|0,o[n]%=e)}return o.reverse()}function yr(i,t){var e,n,o;if(t.isZero())return t;n=t.d.length,n<32?(e=Math.ceil(n/3),o=(1/Ze(4,e)).toString()):(e=16,o="2.3283064365386962890625e-10"),i.precision+=e,t=ve(i,1,t.times(o),new i(1));for(var a=e;a--;){var s=t.times(t);t=s.times(s).minus(s).times(8).plus(1)}return i.precision-=e,t}var H=function(){function i(n,o,a){var s,d=0,h=n.length;for(n=n.slice();h--;)s=n[h]*o+d,n[h]=s%a|0,d=s/a|0;return d&&n.unshift(d),n}function t(n,o,a,s){var d,h;if(a!=s)h=a>s?1:-1;else for(d=h=0;d<a;d++)if(n[d]!=o[d]){h=n[d]>o[d]?1:-1;break}return h}function e(n,o,a,s){for(var d=0;a--;)n[a]-=d,d=n[a]<o[a]?1:0,n[a]=d*s+n[a]-o[a];for(;!n[0]&&n.length>1;)n.shift()}return function(n,o,a,s,d,h){var m,w,b,y,A,C,k,B,P,q,x,R,Dt,Z,Ut,G,K,X,Q,Vt,U=n.constructor,Zt=n.s==o.s?1:-1,r=n.d,l=o.d;if(!r||!r[0]||!l||!l[0])return new U(!n.s||!o.s||(r?l&&r[0]==l[0]:!l)?NaN:r&&r[0]==0||!l?Zt*0:Zt/0);for(h?(A=1,w=n.e-o.e):(h=Gt,A=D,w=Kt(n.e/A)-Kt(o.e/A)),Q=l.length,K=r.length,P=new U(Zt),q=P.d=[],b=0;l[b]==(r[b]||0);b++);if(l[b]>(r[b]||0)&&w--,a==null?(Z=a=U.precision,s=U.rounding):d?Z=a+(n.e-o.e)+1:Z=a,Z<0)q.push(1),C=!0;else{if(Z=Z/A+2|0,b=0,Q==1){for(y=0,l=l[0],Z++;(b<K||y)&&Z--;b++)Ut=y*h+(r[b]||0),q[b]=Ut/l|0,y=Ut%l|0;C=y||b<K}else{for(y=h/(l[0]+1)|0,y>1&&(l=i(l,y,h),r=i(r,y,h),Q=l.length,K=r.length),G=Q,x=r.slice(0,Q),R=x.length;R<Q;)x[R++]=0;Vt=l.slice(),Vt.unshift(0),X=l[0],l[1]>=h/2&&++X;do y=0,m=t(l,x,Q,R),m<0?(Dt=x[0],Q!=R&&(Dt=Dt*h+(x[1]||0)),y=Dt/X|0,y>1?(y>=h&&(y=h-1),k=i(l,y,h),B=k.length,R=x.length,m=t(k,x,B,R),m==1&&(y--,e(k,Q<B?Vt:l,B,h))):(y==0&&(m=y=1),k=l.slice()),B=k.length,B<R&&k.unshift(0),e(x,k,R,h),m==-1&&(R=x.length,m=t(l,x,Q,R),m<1&&(y++,e(x,Q<R?Vt:l,R,h))),R=x.length):m===0&&(y++,x=[0]),q[b++]=y,m&&x[0]?x[R++]=r[G]||0:(x=[r[G]],R=1);while((G++<K||x[0]!==void 0)&&Z--);C=x[0]!==void 0}q[0]||q.shift()}if(A==1)P.e=w,Cn=C;else{for(b=1,y=q[0];y>=10;y/=10)b++;P.e=b+w*A-1,I(P,d?a+P.e+1:a,s,C)}return P}}();function I(i,t,e,n){var o,a,s,d,h,m,w,b,y,A=i.constructor;t:if(t!=null){if(b=i.d,!b)return i;for(o=1,d=b[0];d>=10;d/=10)o++;if(a=t-o,a<0)a+=D,s=t,w=b[y=0],h=w/Rt(10,o-s-1)%10|0;else if(y=Math.ceil((a+1)/D),d=b.length,y>=d)if(n){for(;d++<=y;)b.push(0);w=h=0,o=1,a%=D,s=a-D+1}else break t;else{for(w=d=b[y],o=1;d>=10;d/=10)o++;a%=D,s=a-D+o,h=s<0?0:w/Rt(10,o-s-1)%10|0}if(n=n||t<0||b[y+1]!==void 0||(s<0?w:w%Rt(10,o-s-1)),m=e<4?(h||n)&&(e==0||e==(i.s<0?3:2)):h>5||h==5&&(e==4||n||e==6&&(a>0?s>0?w/Rt(10,o-s):0:b[y-1])%10&1||e==(i.s<0?8:7)),t<1||!b[0])return b.length=0,m?(t-=i.e+1,b[0]=Rt(10,(D-t%D)%D),i.e=-t||0):b[0]=i.e=0,i;if(a==0?(b.length=y,d=1,y--):(b.length=y+1,d=Rt(10,D-a),b[y]=s>0?(w/Rt(10,o-s)%Rt(10,s)|0)*d:0),m)for(;;)if(y==0){for(a=1,s=b[0];s>=10;s/=10)a++;for(s=b[0]+=d,d=1;s>=10;s/=10)d++;a!=d&&(i.e++,b[0]==Gt&&(b[0]=1));break}else{if(b[y]+=d,b[y]!=Gt)break;b[y--]=0,d=1}for(a=b.length;b[--a]===0;)b.pop()}return L&&(i.e>A.maxE?(i.d=null,i.e=NaN):i.e<A.minE&&(i.e=0,i.d=[0])),i}function te(i,t,e){if(!i.isFinite())return Bn(i);var n,o=i.e,a=Lt(i.d),s=a.length;return t?(e&&(n=e-s)>0?a=a.charAt(0)+"."+a.slice(1)+ae(n):s>1&&(a=a.charAt(0)+"."+a.slice(1)),a=a+(i.e<0?"e":"e+")+i.e):o<0?(a="0."+ae(-o-1)+a,e&&(n=e-s)>0&&(a+=ae(n))):o>=s?(a+=ae(o+1-s),e&&(n=e-o-1)>0&&(a=a+"."+ae(n))):((n=o+1)<s&&(a=a.slice(0,n)+"."+a.slice(n)),e&&(n=e-s)>0&&(o+1===s&&(a+="."),a+=ae(n))),a}function Ke(i,t){var e=i[0];for(t*=D;e>=10;e/=10)t++;return t}function Ue(i,t,e){if(t>vr)throw L=!0,e&&(i.precision=e),Error(kn);return I(new i(Le),t,1,!0)}function Qt(i,t,e){if(t>Mi)throw Error(kn);return I(new i(je),t,e,!0)}function On(i){var t=i.length-1,e=t*D+1;if(t=i[t],t){for(;t%10==0;t/=10)e--;for(t=i[0];t>=10;t/=10)e++}return e}function ae(i){for(var t="";i--;)t+="0";return t}function Tn(i,t,e,n){var o,a=new i(1),s=Math.ceil(n/D+4);for(L=!1;;){if(e%2&&(a=a.times(t),pn(a.d,s)&&(o=!0)),e=Kt(e/2),e===0){e=a.d.length-1,o&&a.d[e]===0&&++a.d[e];break}t=t.times(t),pn(t.d,s)}return L=!0,a}function mn(i){return i.d[i.d.length-1]&1}function In(i,t,e){for(var n,o,a=new i(t[0]),s=0;++s<t.length;){if(o=new i(t[s]),!o.s){a=o;break}n=a.cmp(o),(n===e||n===0&&a.s===e)&&(a=o)}return a}function Si(i,t){var e,n,o,a,s,d,h,m=0,w=0,b=0,y=i.constructor,A=y.rounding,C=y.precision;if(!i.d||!i.d[0]||i.e>17)return new y(i.d?i.d[0]?i.s<0?0:1/0:1:i.s?i.s<0?0:i:NaN);for(t==null?(L=!1,h=C):h=t,d=new y(.03125);i.e>-2;)i=i.times(d),b+=5;for(n=Math.log(Rt(2,b))/Math.LN10*2+5|0,h+=n,e=a=s=new y(1),y.precision=h;;){if(a=I(a.times(i),h,1),e=e.times(++w),d=s.plus(H(a,e,h,1)),Lt(d.d).slice(0,h)===Lt(s.d).slice(0,h)){for(o=b;o--;)s=I(s.times(s),h,1);if(t==null)if(m<3&&Ce(s.d,h-n,A,m))y.precision=h+=10,e=a=d=new y(1),w=0,m++;else return I(s,y.precision=C,A,L=!0);else return y.precision=C,s}s=d}}function ue(i,t){var e,n,o,a,s,d,h,m,w,b,y,A=1,C=10,k=i,B=k.d,P=k.constructor,q=P.rounding,x=P.precision;if(k.s<0||!B||!B[0]||!k.e&&B[0]==1&&B.length==1)return new P(B&&!B[0]?-1/0:k.s!=1?NaN:B?0:k);if(t==null?(L=!1,w=x):w=t,P.precision=w+=C,e=Lt(B),n=e.charAt(0),Math.abs(a=k.e)<15e14){for(;n<7&&n!=1||n==1&&e.charAt(1)>3;)k=k.times(i),e=Lt(k.d),n=e.charAt(0),A++;a=k.e,n>1?(k=new P("0."+e),a++):k=new P(n+"."+e.slice(1))}else return m=Ue(P,w+2,x).times(a+""),k=ue(new P(n+"."+e.slice(1)),w-C).plus(m),P.precision=x,t==null?I(k,x,q,L=!0):k;for(b=k,h=s=k=H(k.minus(1),k.plus(1),w,1),y=I(k.times(k),w,1),o=3;;){if(s=I(s.times(y),w,1),m=h.plus(H(s,new P(o),w,1)),Lt(m.d).slice(0,w)===Lt(h.d).slice(0,w))if(h=h.times(2),a!==0&&(h=h.plus(Ue(P,w+2,x).times(a+""))),h=H(h,new P(A),w,1),t==null)if(Ce(h.d,w-C,q,d))P.precision=w+=C,m=s=k=H(b.minus(1),b.plus(1),w,1),y=I(k.times(k),w,1),o=d=1;else return I(h,P.precision=x,q,L=!0);else return P.precision=x,h;h=m,o+=2}}function Bn(i){return String(i.s*i.s/0)}function Re(i,t){var e,n,o;for((e=t.indexOf("."))>-1&&(t=t.replace(".","")),(n=t.search(/e/i))>0?(e<0&&(e=n),e+=+t.slice(n+1),t=t.substring(0,n)):e<0&&(e=t.length),n=0;t.charCodeAt(n)===48;n++);for(o=t.length;t.charCodeAt(o-1)===48;--o);if(t=t.slice(n,o),t){if(o-=n,i.e=e=e-n-1,i.d=[],n=(e+1)%D,e<0&&(n+=D),n<o){for(n&&i.d.push(+t.slice(0,n)),o-=D;n<o;)i.d.push(+t.slice(n,n+=D));t=t.slice(n),n=D-t.length}else n-=o;for(;n--;)t+="0";i.d.push(+t),L&&(i.e>i.constructor.maxE?(i.d=null,i.e=NaN):i.e<i.constructor.minE&&(i.e=0,i.d=[0]))}else i.e=0,i.d=[0];return i}function Mr(i,t){var e,n,o,a,s,d,h,m,w;if(t.indexOf("_")>-1){if(t=t.replace(/(\d)_(?=\d)/g,"$1"),En.test(t))return Re(i,t)}else if(t==="Infinity"||t==="NaN")return+t||(i.s=NaN),i.e=NaN,i.d=null,i;if(_r.test(t))e=16,t=t.toLowerCase();else if(gr.test(t))e=2;else if(wr.test(t))e=8;else throw Error(de+t);for(a=t.search(/p/i),a>0?(h=+t.slice(a+1),t=t.substring(2,a)):t=t.slice(2),a=t.indexOf("."),s=a>=0,n=i.constructor,s&&(t=t.replace(".",""),d=t.length,a=d-a,o=Tn(n,new n(e),a,a*2)),m=qe(t,e,Gt),w=m.length-1,a=w;m[a]===0;--a)m.pop();return a<0?new n(i.s*0):(i.e=Ke(m,w),i.d=m,L=!1,s&&(i=H(i,o,d*4)),h&&(i=i.times(Math.abs(h)<54?Rt(2,h):Pe.pow(2,h))),L=!0,i)}function Sr(i,t){var e,n=t.d.length;if(n<3)return t.isZero()?t:ve(i,2,t,t);e=1.4*Math.sqrt(n),e=e>16?16:e|0,t=t.times(1/Ze(5,e)),t=ve(i,2,t,t);for(var o,a=new i(5),s=new i(16),d=new i(20);e--;)o=t.times(t),t=t.times(a.plus(o.times(s.times(o).minus(d))));return t}function ve(i,t,e,n,o){var a,s,d,h,m=1,w=i.precision,b=Math.ceil(w/D);for(L=!1,h=e.times(e),d=new i(n);;){if(s=H(d.times(h),new i(t++*t++),w,1),d=o?n.plus(s):n.minus(s),n=H(s.times(h),new i(t++*t++),w,1),s=d.plus(n),s.d[b]!==void 0){for(a=b;s.d[a]===d.d[a]&&a--;);if(a==-1)break}a=d,d=n,n=s,s=a,m++}return L=!0,s.d.length=b+1,s}function Ze(i,t){for(var e=i;--t;)e*=i;return e}function Nn(i,t){var e,n=t.s<0,o=Qt(i,i.precision,1),a=o.times(.5);if(t=t.abs(),t.lte(a))return ie=n?4:1,t;if(e=t.divToInt(o),e.isZero())ie=n?3:2;else{if(t=t.minus(e.times(o)),t.lte(a))return ie=mn(e)?n?2:3:n?4:1,t;ie=mn(e)?n?1:4:n?3:2}return t.minus(o).abs()}function Ci(i,t,e,n){var o,a,s,d,h,m,w,b,y,A=i.constructor,C=e!==void 0;if(C?(Xt(e,1,me),n===void 0?n=A.rounding:Xt(n,0,8)):(e=A.precision,n=A.rounding),!i.isFinite())w=Bn(i);else{for(w=te(i),s=w.indexOf("."),C?(o=2,t==16?e=e*4-3:t==8&&(e=e*3-2)):o=t,s>=0&&(w=w.replace(".",""),y=new A(1),y.e=w.length-s,y.d=qe(te(y),10,o),y.e=y.d.length),b=qe(w,10,o),a=h=b.length;b[--h]==0;)b.pop();if(!b[0])w=C?"0p+0":"0";else{if(s<0?a--:(i=new A(i),i.d=b,i.e=a,i=H(i,y,e,n,0,o),b=i.d,a=i.e,m=Cn),s=b[e],d=o/2,m=m||b[e+1]!==void 0,m=n<4?(s!==void 0||m)&&(n===0||n===(i.s<0?3:2)):s>d||s===d&&(n===4||m||n===6&&b[e-1]&1||n===(i.s<0?8:7)),b.length=e,m)for(;++b[--e]>o-1;)b[e]=0,e||(++a,b.unshift(1));for(h=b.length;!b[h-1];--h);for(s=0,w="";s<h;s++)w+=vi.charAt(b[s]);if(C){if(h>1)if(t==16||t==8){for(s=t==16?4:3,--h;h%s;h++)w+="0";for(b=qe(w,o,t),h=b.length;!b[h-1];--h);for(s=1,w="1.";s<h;s++)w+=vi.charAt(b[s])}else w=w.charAt(0)+"."+w.slice(1);w=w+(a<0?"p":"p+")+a}else if(a<0){for(;++a;)w="0"+w;w="0."+w}else if(++a>h)for(a-=h;a--;)w+="0";else a<h&&(w=w.slice(0,a)+"."+w.slice(a))}w=(t==16?"0x":t==2?"0b":t==8?"0o":"")+w}return i.s<0?"-"+w:w}function pn(i,t){if(i.length>t)return i.length=t,!0}function Ar(i){return new this(i).abs()}function Cr(i){return new this(i).acos()}function kr(i){return new this(i).acosh()}function xr(i,t){return new this(i).plus(t)}function Pr(i){return new this(i).asin()}function Er(i){return new this(i).asinh()}function Or(i){return new this(i).atan()}function Tr(i){return new this(i).atanh()}function Ir(i,t){i=new this(i),t=new this(t);var e,n=this.precision,o=this.rounding,a=n+4;return!i.s||!t.s?e=new this(NaN):!i.d&&!t.d?(e=Qt(this,a,1).times(t.s>0?.25:.75),e.s=i.s):!t.d||i.isZero()?(e=t.s<0?Qt(this,n,o):new this(0),e.s=i.s):!i.d||t.isZero()?(e=Qt(this,a,1).times(.5),e.s=i.s):t.s<0?(this.precision=a,this.rounding=1,e=this.atan(H(i,t,a,1)),t=Qt(this,a,1),this.precision=n,this.rounding=o,e=i.s<0?e.minus(t):e.plus(t)):e=this.atan(H(i,t,a,1)),e}function Br(i){return new this(i).cbrt()}function Nr(i){return I(i=new this(i),i.e+1,2)}function Fr(i,t,e){return new this(i).clamp(t,e)}function Dr(i){if(!i||typeof i!="object")throw Error($e+"Object expected");var t,e,n,o=i.defaults===!0,a=["precision",1,me,"rounding",0,8,"toExpNeg",-be,0,"toExpPos",0,be,"maxE",0,be,"minE",-be,0,"modulo",0,9];for(t=0;t<a.length;t+=3)if(e=a[t],o&&(this[e]=yi[e]),(n=i[e])!==void 0)if(Kt(n)===n&&n>=a[t+1]&&n<=a[t+2])this[e]=n;else throw Error(de+e+": "+n);if(e="crypto",o&&(this[e]=yi[e]),(n=i[e])!==void 0)if(n===!0||n===!1||n===0||n===1)if(n)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[e]=!0;else throw Error(xn);else this[e]=!1;else throw Error(de+e+": "+n);return this}function qr(i){return new this(i).cos()}function Rr(i){return new this(i).cosh()}function Fn(i){var t,e,n;function o(a){var s,d,h,m=this;if(!(m instanceof o))return new o(a);if(m.constructor=o,gn(a)){m.s=a.s,L?!a.d||a.e>o.maxE?(m.e=NaN,m.d=null):a.e<o.minE?(m.e=0,m.d=[0]):(m.e=a.e,m.d=a.d.slice()):(m.e=a.e,m.d=a.d?a.d.slice():a.d);return}if(h=typeof a,h==="number"){if(a===0){m.s=1/a<0?-1:1,m.e=0,m.d=[0];return}if(a<0?(a=-a,m.s=-1):m.s=1,a===~~a&&a<1e7){for(s=0,d=a;d>=10;d/=10)s++;L?s>o.maxE?(m.e=NaN,m.d=null):s<o.minE?(m.e=0,m.d=[0]):(m.e=s,m.d=[a]):(m.e=s,m.d=[a]);return}if(a*0!==0){a||(m.s=NaN),m.e=NaN,m.d=null;return}return Re(m,a.toString())}if(h==="string")return(d=a.charCodeAt(0))===45?(a=a.slice(1),m.s=-1):(d===43&&(a=a.slice(1)),m.s=1),En.test(a)?Re(m,a):Mr(m,a);if(h==="bigint")return a<0?(a=-a,m.s=-1):m.s=1,Re(m,a.toString());throw Error(de+a)}if(o.prototype=S,o.ROUND_UP=0,o.ROUND_DOWN=1,o.ROUND_CEIL=2,o.ROUND_FLOOR=3,o.ROUND_HALF_UP=4,o.ROUND_HALF_DOWN=5,o.ROUND_HALF_EVEN=6,o.ROUND_HALF_CEIL=7,o.ROUND_HALF_FLOOR=8,o.EUCLID=9,o.config=o.set=Dr,o.clone=Fn,o.isDecimal=gn,o.abs=Ar,o.acos=Cr,o.acosh=kr,o.add=xr,o.asin=Pr,o.asinh=Er,o.atan=Or,o.atanh=Tr,o.atan2=Ir,o.cbrt=Br,o.ceil=Nr,o.clamp=Fr,o.cos=qr,o.cosh=Rr,o.div=Lr,o.exp=jr,o.floor=Ur,o.hypot=Vr,o.ln=$r,o.log=Kr,o.log10=Xr,o.log2=Zr,o.max=Hr,o.min=Wr,o.mod=zr,o.mul=Gr,o.pow=Yr,o.random=Jr,o.round=Qr,o.sign=to,o.sin=eo,o.sinh=io,o.sqrt=no,o.sub=ro,o.sum=oo,o.tan=so,o.tanh=ao,o.trunc=uo,i===void 0&&(i={}),i&&i.defaults!==!0)for(n=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],t=0;t<n.length;)i.hasOwnProperty(e=n[t++])||(i[e]=this[e]);return o.config(i),o}function Lr(i,t){return new this(i).div(t)}function jr(i){return new this(i).exp()}function Ur(i){return I(i=new this(i),i.e+1,3)}function Vr(){var i,t,e=new this(0);for(L=!1,i=0;i<arguments.length;)if(t=new this(arguments[i++]),t.d)e.d&&(e=e.plus(t.times(t)));else{if(t.s)return L=!0,new this(1/0);e=t}return L=!0,e.sqrt()}function gn(i){return i instanceof Pe||i&&i.toStringTag===Pn||!1}function $r(i){return new this(i).ln()}function Kr(i,t){return new this(i).log(t)}function Zr(i){return new this(i).log(2)}function Xr(i){return new this(i).log(10)}function Hr(){return In(this,arguments,-1)}function Wr(){return In(this,arguments,1)}function zr(i,t){return new this(i).mod(t)}function Gr(i,t){return new this(i).mul(t)}function Yr(i,t){return new this(i).pow(t)}function Jr(i){var t,e,n,o,a=0,s=new this(1),d=[];if(i===void 0?i=this.precision:Xt(i,1,me),n=Math.ceil(i/D),this.crypto)if(crypto.getRandomValues)for(t=crypto.getRandomValues(new Uint32Array(n));a<n;)o=t[a],o>=429e7?t[a]=crypto.getRandomValues(new Uint32Array(1))[0]:d[a++]=o%1e7;else if(crypto.randomBytes){for(t=crypto.randomBytes(n*=4);a<n;)o=t[a]+(t[a+1]<<8)+(t[a+2]<<16)+((t[a+3]&127)<<24),o>=214e7?crypto.randomBytes(4).copy(t,a):(d.push(o%1e7),a+=4);a=n/4}else throw Error(xn);else for(;a<n;)d[a++]=Math.random()*1e7|0;for(n=d[--a],i%=D,n&&i&&(o=Rt(10,D-i),d[a]=(n/o|0)*o);d[a]===0;a--)d.pop();if(a<0)e=0,d=[0];else{for(e=-1;d[0]===0;e-=D)d.shift();for(n=1,o=d[0];o>=10;o/=10)n++;n<D&&(e-=D-n)}return s.e=e,s.d=d,s}function Qr(i){return I(i=new this(i),i.e+1,this.rounding)}function to(i){return i=new this(i),i.d?i.d[0]?i.s:0*i.s:i.s||NaN}function eo(i){return new this(i).sin()}function io(i){return new this(i).sinh()}function no(i){return new this(i).sqrt()}function ro(i,t){return new this(i).sub(t)}function oo(){var i=0,t=arguments,e=new this(t[i]);for(L=!1;e.s&&++i<t.length;)e=e.plus(t[i]);return L=!0,I(e,this.precision,this.rounding)}function so(i){return new this(i).tan()}function ao(i){return new this(i).tanh()}function uo(i){return I(i=new this(i),i.e+1,1)}S[Symbol.for("nodejs.util.inspect.custom")]=S.toString;S[Symbol.toStringTag]="Decimal";var Pe=S.constructor=Fn(yi);Le=new Pe(Le);je=new Pe(je);var Jt=Pe;function E(i){return Jt.isDecimal(i)?i:new Jt(i===void 0?0:i)}function Dn(i){return E(10).pow(E(i).abs())}function le(i,t){let e=Dn(E(t));return Number(E(i).mul(e))}function ki(i,t=32){return Number(BigInt.asIntN(t,BigInt(i)))}function Ve(i,t){let e=Dn(E(t));return Number(E(i).div(e))}var qn="0x2::sui::SUI",lo="0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI";var Ee=class Ft{static totalBalance(t,e){let n=BigInt(0);return t.forEach(o=>{e===o.coin_type&&(n+=BigInt(o.balance))}),n}static getCoinAssets(t,e){let n=[];return e.forEach(o=>{wn(o.coin_type)===wn(t)&&n.push(o)}),n}static isSuiCoin(t){return ce(t).full_address===qn}static selectCoinObjectIdGreaterThanOrEqual(t,e,n=[]){let o=Ft.selectCoinAssetGreaterThanOrEqual(t,e,n),a=o.selected_coins.map(h=>h.coin_object_id),s=o.remain_coins,d=o.selected_coins.map(h=>h.balance.toString());return{object_array:a,remain_coins:s,amount_array:d}}static selectCoinAssetGreaterThanOrEqual(t,e,n=[]){let o=Ft.sortByBalance(t.filter(m=>!n.includes(m.coin_object_id))),a=Ft.calculateTotalBalance(o);if(a<e)return{selected_coins:[],remain_coins:o};if(a===e)return{selected_coins:o,remain_coins:[]};let s=BigInt(0),d=[],h=[...o];for(;s<a;){let m=e-s,w=h.findIndex(y=>y.balance>=m);if(w!==-1){d.push(h[w]),h.splice(w,1);break}let b=h.pop();b.balance>0&&(d.push(b),s+=b.balance)}return{selected_coins:Ft.sortByBalance(d),remain_coins:Ft.sortByBalance(h)}}static sortByBalance(t){return t.sort((e,n)=>e.balance<n.balance?-1:e.balance>n.balance?1:0)}static sortByBalanceDes(t){return t.sort((e,n)=>e.balance>n.balance?-1:e.balance<n.balance?0:1)}static calculateTotalBalance(t){return t.reduce((e,n)=>e+n.balance,BigInt(0))}static buildCoinForAmount(t,e,n,o,a=!0,s=!0){let d=Ft.getCoinAssets(o,e);if(n===BigInt(0))return this.buildZeroValueCoin(e,t,o,a);let h=Ft.calculateTotalBalance(d);if(h<n)throw new Error(`The amount(${h}) is Insufficient balance for ${o} , expect ${n} `);return this.buildCoin(t,e,d,n,o,a,s)}static buildCoinWithBalance(t,e,n){return t===BigInt(0)&&Ft.isSuiCoin(e)?n.add((0,bi.coinWithBalance)({balance:t,useGasCoin:!1})):n.add((0,bi.coinWithBalance)({balance:t,type:e}))}static buildVectorCoin(t,e,n,o,a,s=!0){if(Ft.isSuiCoin(a)){let y=t.splitCoins(t.gas,[t.pure.u64(o)]);return{selected_coins:[],target_coin:y,remain_coins:e,target_coin_amount:o.toString(),is_mint_zero_coin:!1,original_spited_coin:t.gas}}let{original_spited_coin:d,target_coin:h,target_coin_amount:m,remain_coins:w,selected_coins:b}=this.buildSpitTargeCoin(t,o,n,s);return s?{target_coin:t.makeMoveVec({elements:[h]}),selected_coins:b,remain_coins:w,target_coin_amount:m,is_mint_zero_coin:!1,original_spited_coin:d}:{selected_coins:b,target_coin:t.makeMoveVec({elements:b.map(y=>t.object(y))}),remain_coins:w,target_coin_amount:m,is_mint_zero_coin:!1}}static buildOneCoin(t,e,n,o,a=!0){if(Ft.isSuiCoin(o)){if(n===0n&&e.length>1){let h=Ft.selectCoinObjectIdGreaterThanOrEqual(e,n);return{selected_coins:h.object_array,target_coin:t.object(h.object_array[0]),remain_coins:h.remain_coins,target_coin_amount:h.amount_array[0],is_mint_zero_coin:!1}}let s=Ft.selectCoinObjectIdGreaterThanOrEqual(e,n),d=t.splitCoins(t.gas,[t.pure.u64(n)]);return{selected_coins:[],target_coin:d,remain_coins:s.remain_coins,target_coin_amount:n.toString(),is_mint_zero_coin:!1,original_spited_coin:t.gas}}return this.buildSpitTargeCoin(t,n,e,a)}static buildSpitTargeCoin(t,e,n,o){let a=Ft.selectCoinObjectIdGreaterThanOrEqual(n,e),s=a.amount_array.reduce((C,k)=>Number(C)+Number(k),0).toString(),d=a.object_array,[h,...m]=d,w=t.object(h),b=w,y=a.amount_array.reduce((C,k)=>Number(C)+Number(k),0).toString(),A;return m.length>0&&t.mergeCoins(w,m.map(C=>t.object(C))),o&&Number(s)>Number(e)&&(b=t.splitCoins(w,[t.pure.u64(e)]),A=w),{original_spited_coin:A,target_coin:b,target_coin_amount:y,remain_coins:a.remain_coins,selected_coins:a.object_array,is_mint_zero_coin:!1}}static buildCoin(t,e,n,o,a,s=!0,d=!0){return s?this.buildVectorCoin(t,e,n,o,a,d):this.buildOneCoin(t,n,o,a,d)}static buildZeroValueCoin(t,e,n,o=!0){let a=this.callMintZeroValueCoin(e,n),s;return o?s=e.makeMoveVec({elements:[a]}):s=a,{target_coin:s,remain_coins:t,selected_coins:[],is_mint_zero_coin:!0,target_coin_amount:"0"}}static buildCoinForAmountInterval(t,e,n,o,a=!0,s=!0){let d=Ft.getCoinAssets(o,e);if(n.amount_first===BigInt(0))return d.length>0?this.buildCoin(t,[...e],[...d],n.amount_first,o,a,s):this.buildZeroValueCoin(e,t,o,a);let h=Ft.calculateTotalBalance(d);if(h>=n.amount_first)return this.buildCoin(t,[...e],[...d],n.amount_first,o,a,s);if(h<n.amount_second)throw new Error(`The amount(${h}) is Insufficient balance for ${o} , expect ${n.amount_second} `);return this.buildCoin(t,[...e],[...d],n.amount_second,o,a,s)}static{this.callMintZeroValueCoin=(t,e)=>t.moveCall({target:"0x2::coin::zero",typeArguments:[e]})}static fromBalance(t,e,n){return n.moveCall({target:"0x2::coin::from_balance",typeArguments:[e],arguments:[t]})}static getCoinAmountObjId(t,e){let n=t.amount_coin_array.find(o=>!o.used&&E(o.amount).eq(e)?(o.used=!0,!0):!1);return n?n.coin_object_id:Wt("CoinNotFound",`Coin not found for ${e} ${t.coin_type}`)}static buildMultiCoinInput(t,e,n,o){let a=Ft.getCoinAssets(n,e);if(Ft.isSuiCoin(n)){let b=t.splitCoins(t.gas,o.map(y=>t.pure.u64(y)));return{amount_coin_array:o.map((y,A)=>({coin_object_id:b[A],amount:y.toString(),used:!1})),coin_type:n,remain_coins:a}}let s=o.reduce((b,y)=>b+y,BigInt(0)),d=Ft.selectCoinObjectIdGreaterThanOrEqual(a,s);if(d.object_array.length===0)return Wt("InsufficientBalance",`No enough coins for ${n} expect ${s} actual ${Ft.calculateTotalBalance(a)}`);let[h,...m]=d.object_array;m.length>0&&t.mergeCoins(h,[...m]);let w=t.splitCoins(h,o.map(b=>t.pure.u64(b)));return{amount_coin_array:o.map((b,y)=>({coin_object_id:w[y],amount:b.toString(),used:!1})),remain_coins:d.remain_coins,coin_type:n}}};function co(i){return i.startsWith("0x")?`${i.slice(2)}`:i}function _n(i,...t){let e=Array.isArray(t[t.length-1])?t.pop():[],n=[i,...t].filter(Boolean).join("::");return e&&e.length&&(n+=`<${e.join(", ")}>`),n}function ce(i){try{let t=i.replace(/\s/g,""),e=t.match(/(<.+>)$/)?.[0]?.match(/(\w+::\w+::\w+)(?:<.*?>(?!>))?/g);if(e){t=t.slice(0,t.indexOf("<"));let a={...ce(t),type_arguments:e.map(s=>ce(s).source_address)};return a.type_arguments=a.type_arguments.map(s=>Ee.isSuiCoin(s)?s:ce(s).source_address),a.source_address=_n(a.full_address,a.type_arguments),a}let n=t.split("::"),o={full_address:t,address:t===qn||t===lo?"0x2":(0,fe.normalizeSuiObjectId)(n[0]),module:n[1],name:n[2],type_arguments:[],source_address:""};return o.full_address=`${o.address}::${o.module}::${o.name}`,o.source_address=_n(o.full_address,o.type_arguments),o}catch{return{full_address:i,address:"",module:"",name:"",type_arguments:[],source_address:i}}}function wn(i){return ce(i).source_address}function ho(i){return i.toLowerCase().startsWith("0x")?(0,fe.normalizeSuiObjectId)(i):i}var tt=(i,t=!0)=>{let e=i.split("::"),n=e.shift(),o=(0,fe.normalizeSuiObjectId)(n);return t&&(o=co(o)),`${o}::${e.join("::")}`};function xi(i){for(let t in i){let e=typeof i[t];if(e==="object")xi(i[t]);else if(e==="string"){let n=i[t];n&&!n.includes("::")&&(i[t]=ho(n))}}}var fo=ke(xe()),bn=443636,vn=-443636,mo="79226673515401279992447579055";var po="4295048016",Ro=new fo.default(1e6),go=(i=>(i[i.BelowRange=0]="BelowRange",i[i.InRange=1]="InRange",i[i.AboveRange=2]="AboveRange",i))(go||{}),qt=ke(xe()),ee=new qt.default(0),Ae=new qt.default(1),Xe=new qt.default(2),Lo=Xe.pow(new qt.default(128)),yn=Xe.pow(new qt.default(64)).sub(Ae),_o=Xe.pow(new qt.default(128)).sub(Ae),zt=class{static toX64BN(i){return i.mul(new qt.default(2).pow(new qt.default(64)))}static toX64Decimal(i){return i.mul(Jt.pow(2,64))}static toX64(i){return new qt.default(i.mul(Jt.pow(2,64)).floor().toFixed())}static fromX64(i){return new Jt(i.toString()).mul(Jt.pow(2,-64))}static fromX64Decimal(i){return i.mul(Jt.pow(2,-64))}static fromX64BN(i){return i.div(new qt.default(2).pow(new qt.default(64)))}static shiftRightRoundUp(i){let t=i.shrn(64);return i.mod(yn).gt(ee)&&(t=t.add(Ae)),t}static divRoundUp(i,t){return i.mod(t).eq(ee)?i.div(t):i.div(t).add(new qt.default(1))}static subUnderflowU128(i,t){return i.lt(t)?i.sub(t).add(_o):i.sub(t)}static checkUnsignedSub(i,t){let e=i.sub(t);if(e.isNeg())throw new Error("Unsigned integer sub overflow");return e}static checkMul(i,t,e){let n=i.mul(t);return this.isOverflow(n,e)&&Wt("MultiplicationOverflow","Multiplication overflow"),n}static checkMulDivFloor(i,t,e,n){e.eq(ee)&&Wt("DivisionByZero","Divide by zero");let o=i.mul(t).div(e);return this.isOverflow(o,n)&&Wt("MultiplicationOverflow","Multiplication div overflow"),o}static checkMulDivCeil(i,t,e,n){if(e.eq(ee))throw new Error("Divide by zero");let o=i.mul(t).add(e.sub(Ae)).div(e);if(this.isOverflow(o,n))throw new Error("Multiplication div overflow");return o}static checkMulDivRound(i,t,e,n){e.eq(ee)&&Wt("DivisionByZero","Divide by zero");let o=i.mul(t.add(e.shrn(1))).div(e);return this.isOverflow(o,n)&&Wt("MultiplicationOverflow","Multiplication div overflow"),o}static checkMulShiftRight(i,t,e,n){let o=i.mul(t).div(new qt.default(2).pow(new qt.default(e)));return this.isOverflow(o,n)&&Wt("MultiplicationOverflow","Multiplication shift right overflow"),o}static checkMulShiftRight64RoundUpIf(i,t,e,n){let o=i.mul(t),a=n&&o.and(yn).gt(ee)?o.shrn(64).add(Ae):o.shrn(64);return this.isOverflow(a,e)&&Wt("MultiplicationOverflow","Multiplication shift right overflow"),a}static checkMulShiftLeft(i,t,e,n){let o=i.mul(t).shln(e);return this.isOverflow(o,n)&&Wt("MultiplicationOverflow","Multiplication shift left overflow"),o}static checkDivRoundUpIf(i,t,e){return t.eq(ee)&&Wt("DivisionByZero","Divide by zero"),e?this.divRoundUp(i,t):i.div(t)}static isOverflow(i,t){return i.gte(Xe.pow(new qt.default(t)))}static sign(i){return i.testn(127)?1:0}static is_neg(i){return this.sign(i)===1}static abs_u128(i){return i.gt(ee)?i:this.u128Neg(i.subn(1))}static u128Neg(i){return i.uxor(new qt.default("ffffffffffffffffffffffffffffffff",16))}static neg(i){return this.is_neg(i)?i.abs():this.negFrom(i)}static abs(i){return this.sign(i)===0?i:this.u128Neg(i.sub(new qt.default(1)))}static negFrom(i){return i.eq(ee)?i:this.u128Neg(i).add(new qt.default(1)).or(new qt.default(1).shln(127))}},Pi=class{static getMinIndex(i){return vn+Math.abs(vn)%i}static getMaxIndex(i){return bn-bn%i}};function Rn(i){return i.data}function wo(i){return i.content!==void 0}function bo(i){let t="data"in i?Rn(i):i;if(!(!t||!wo(t)||t.content.dataType!=="moveObject"))return t.content}function Ln(i){return Rn(i)?.previousTransaction}function Ei(i){let t=bo(i)?.fields;if(t)return"fields"in t?t.fields:t}Jt.config({precision:64,rounding:Jt.ROUND_DOWN,toExpNeg:-64,toExpPos:64});var $t=Jt,Ht=ke(xe()),F=ke(xe()),vo=14,yo="59543866431248",Mo="184467440737095516",So="15793534762490258745";function Ao(i,t,e){let n=i.toTwos(e).shln(t);return n.imaskn(e+1),n.fromTwos(e)}function V(i,t,e){let n=i.toTwos(e).shrn(t);return n.imaskn(e-t+1),n.fromTwos(e-t)}function Co(i){let t;return i&1?t=new F.default("79232123823359799118286999567"):t=new F.default("79228162514264337593543950336"),i&2&&(t=V(t.mul(new F.default("79236085330515764027303304731")),96,256)),i&4&&(t=V(t.mul(new F.default("79244008939048815603706035061")),96,256)),i&8&&(t=V(t.mul(new F.default("79259858533276714757314932305")),96,256)),i&16&&(t=V(t.mul(new F.default("79291567232598584799939703904")),96,256)),i&32&&(t=V(t.mul(new F.default("79355022692464371645785046466")),96,256)),i&64&&(t=V(t.mul(new F.default("79482085999252804386437311141")),96,256)),i&128&&(t=V(t.mul(new F.default("79736823300114093921829183326")),96,256)),i&256&&(t=V(t.mul(new F.default("80248749790819932309965073892")),96,256)),i&512&&(t=V(t.mul(new F.default("81282483887344747381513967011")),96,256)),i&1024&&(t=V(t.mul(new F.default("83390072131320151908154831281")),96,256)),i&2048&&(t=V(t.mul(new F.default("87770609709833776024991924138")),96,256)),i&4096&&(t=V(t.mul(new F.default("97234110755111693312479820773")),96,256)),i&8192&&(t=V(t.mul(new F.default("119332217159966728226237229890")),96,256)),i&16384&&(t=V(t.mul(new F.default("179736315981702064433883588727")),96,256)),i&32768&&(t=V(t.mul(new F.default("407748233172238350107850275304")),96,256)),i&65536&&(t=V(t.mul(new F.default("2098478828474011932436660412517")),96,256)),i&131072&&(t=V(t.mul(new F.default("55581415166113811149459800483533")),96,256)),i&262144&&(t=V(t.mul(new F.default("38992368544603139932233054999993551")),96,256)),V(t,32,256)}function ko(i){let t=Math.abs(i),e;return t&1?e=new F.default("18445821805675392311"):e=new F.default("18446744073709551616"),t&2&&(e=V(e.mul(new F.default("18444899583751176498")),64,256)),t&4&&(e=V(e.mul(new F.default("18443055278223354162")),64,256)),t&8&&(e=V(e.mul(new F.default("18439367220385604838")),64,256)),t&16&&(e=V(e.mul(new F.default("18431993317065449817")),64,256)),t&32&&(e=V(e.mul(new F.default("18417254355718160513")),64,256)),t&64&&(e=V(e.mul(new F.default("18387811781193591352")),64,256)),t&128&&(e=V(e.mul(new F.default("18329067761203520168")),64,256)),t&256&&(e=V(e.mul(new F.default("18212142134806087854")),64,256)),t&512&&(e=V(e.mul(new F.default("17980523815641551639")),64,256)),t&1024&&(e=V(e.mul(new F.default("17526086738831147013")),64,256)),t&2048&&(e=V(e.mul(new F.default("16651378430235024244")),64,256)),t&4096&&(e=V(e.mul(new F.default("15030750278693429944")),64,256)),t&8192&&(e=V(e.mul(new F.default("12247334978882834399")),64,256)),t&16384&&(e=V(e.mul(new F.default("8131365268884726200")),64,256)),t&32768&&(e=V(e.mul(new F.default("3584323654723342297")),64,256)),t&65536&&(e=V(e.mul(new F.default("696457651847595233")),64,256)),t&131072&&(e=V(e.mul(new F.default("26294789957452057")),64,256)),t&262144&&(e=V(e.mul(new F.default("37481735321082")),64,256)),e}var J=class Yt{static priceToSqrtPriceX64(t,e,n){return zt.toX64(t.mul($t.pow(10,n-e)).sqrt())}static sqrtPriceX64ToPrice(t,e,n){return zt.fromX64(t).pow(2).mul($t.pow(10,e-n))}static tickIndexToSqrtPriceX64(t){return t>0?new F.default(Co(t)):new F.default(ko(t))}static sqrtPriceX64ToTickIndex(t){if(t.gt(new F.default(mo))||t.lt(new F.default(po)))throw new Error("Provided sqrtPrice is not within the supported sqrtPrice range.");let e=t.bitLength()-1,n=new F.default(e-64),o=Ao(n,32,128),a=new F.default("8000000000000000","hex"),s=0,d=new F.default(0),h=e>=64?t.shrn(e-63):t.shln(63-e);for(;a.gt(new F.default(0))&&s<vo;){h=h.mul(h);let A=h.shrn(127);h=h.shrn(63+A.toNumber()),d=d.add(a.mul(A)),a=a.shrn(1),s+=1}let m=d.shrn(32),w=o.add(m).mul(new F.default(yo)),b=V(w.sub(new F.default(Mo)),64,128).toNumber(),y=V(w.add(new F.default(So)),64,128).toNumber();return b===y?b:Yt.tickIndexToSqrtPriceX64(y).lte(t)?y:b}static tickIndexToPrice(t,e,n){return Yt.sqrtPriceX64ToPrice(Yt.tickIndexToSqrtPriceX64(t),e,n)}static priceToTickIndex(t,e,n){return Yt.sqrtPriceX64ToTickIndex(Yt.priceToSqrtPriceX64(t,e,n))}static priceToInitializeTickIndex(t,e,n,o){return Yt.getInitializeTickIndex(Yt.priceToTickIndex(t,e,n),o)}static getInitializeTickIndex(t,e){return t-t%e}static getNextInitializeTickIndex(t,e){return Yt.getInitializeTickIndex(t,e)+e}static getPrevInitializeTickIndex(t,e){return Yt.getInitializeTickIndex(t,e)-e}};function Fe(i,t,e){let n=Ht.default.min(i,t),o=Ht.default.max(i,t),a=zt.fromX64BN(e.mul(o).mul(n)),s=o.sub(n);return!a.isZero()&&!s.isZero()?a.div(s):new Ht.default(0)}function De(i,t,e){let n=Ht.default.min(i,t),o=Ht.default.max(i,t).sub(n);return o.isZero()?new Ht.default(0):e.shln(64).div(o)}var ne=class we{static getCoinAmountFromLiquidity(t,e,n,o,a){let s=new $t(t.toString()),d=new $t(e.toString()),h=new $t(n.toString()),m=new $t(o.toString()),w,b;return e.lt(n)?(w=zt.toX64Decimal(s).mul(m.sub(h)).div(h.mul(m)),b=new $t(0)):e.lt(o)?(w=zt.toX64Decimal(s).mul(m.sub(d)).div(d.mul(m)),b=zt.fromX64Decimal(s.mul(d.sub(h)))):(w=new $t(0),b=zt.fromX64Decimal(s.mul(m.sub(h)))),a?{coin_amount_a:w.ceil().toString(),coin_amount_b:b.ceil().toString()}:{coin_amount_a:w.floor().toString(),coin_amount_b:b.floor().toString()}}static estLiquidityAndCoinAmountFromOneAmounts(t,e,n,o,a,s,d){let h=J.sqrtPriceX64ToTickIndex(d),m=J.tickIndexToSqrtPriceX64(t),w=J.tickIndexToSqrtPriceX64(e),b;if(h<t){if(!o)throw new Error("lower tick cannot calculate liquidity by coinB");b=Fe(m,w,n)}else if(h>e){if(o)throw new Error("upper tick cannot calculate liquidity by coinA");b=De(w,m,n)}else o?b=Fe(d,w,n):b=De(d,m,n);let y=we.getCoinAmountFromLiquidity(b,d,m,w,a),A=a?E(y.coin_amount_a.toString()).mul(1+s).toString():E(y.coin_amount_a.toString()).mul(1-s).toString(),C=a?E(y.coin_amount_b.toString()).mul(1+s).toString():E(y.coin_amount_b.toString()).mul(1-s).toString();return{coin_amount_a:y.coin_amount_a,coin_amount_b:y.coin_amount_b,coin_amount_limit_a:a?$t.ceil(A).toString():$t.floor(A).toString(),coin_amount_limit_b:a?$t.ceil(C).toString():$t.floor(C).toString(),liquidity_amount:b.toString(),fix_amount_a:o}}static estimateLiquidityFromCoinAmounts(t,e,n,o){if(e>n)return Wt("InvalidTick","lower tick cannot be greater than lower tick",{[wi.METHOD_NAME]:"estimateLiquidityFromCoinAmounts",[wi.REQUEST_PARAMS]:{lower_tick:e,upper_tick:n,token_amount:o}});let a=J.sqrtPriceX64ToTickIndex(t),s=J.tickIndexToSqrtPriceX64(e),d=J.tickIndexToSqrtPriceX64(n);if(a<e)return Fe(s,d,new Ht.default(o.coin_amount_a)).toString();if(a>=n)return De(d,s,new Ht.default(o.coin_amount_b)).toString();let h=Fe(t,d,new Ht.default(o.coin_amount_a)),m=De(t,s,new Ht.default(o.coin_amount_b));return Ht.default.min(h,m).toString()}static calculateDepositRatio(t,e,n){let o=new Ht.default(1e8),{coin_amount_b:a}=we.estLiquidityAndCoinAmountFromOneAmounts(t,e,o,!0,!0,0,n),s=J.sqrtPriceX64ToPrice(n,0,0),d=E(o.toString()).mul(s),h=d.add(a.toString()),m=d.div(h),w=E(a.toString()).div(h);return{ratio_a:m,ratio_b:w}}static calculateAmountDepositRatio(t,e,n,o,a){let s=J.sqrtPriceX64ToPrice(n,o,a),d=J.sqrtPriceX64ToTickIndex(n);if(d<t)return{ratio_a:new $t(1),ratio_b:new $t(0),curr_price:s};if(d>e)return{ratio_a:new $t(0),ratio_b:new $t(1),curr_price:s};let h=new Ht.default(le(1,o)),{coin_amount_b:m}=we.estLiquidityAndCoinAmountFromOneAmounts(t,e,h,!0,!0,0,n),w=Ve(h.toString(),o),b=Ve(m.toString(),a),y=E(w).add(b),A=E(w).div(y),C=E(b).div(y);return{ratio_a:A,ratio_b:C,curr_price:s}}static getCoinAmountsFromRatio(t,e,n,o,a,s,d){let h=E(le(E(n).mul(t).div(o).toString(),s)).toFixed(0),m=E(le(E(n).mul(e).div(a).toString(),d)).toFixed(0);return{amount_a:h,amount_b:m}}static estCoinAmountsFromTotalAmount(t,e,n,o,a,s,d,h){let{ratio_a:m,ratio_b:w}=we.calculateDepositRatio(t,e,n);return we.getCoinAmountsFromRatio(m,w,o,a,s,d,h)}static getPositionStatus(t,e,n){return t<e?0:t<n?1:2}};var jo=ke(xe());var Wi=require("@cetusprotocol/sui-clmm-sdk");var Me=require("@mysten/sui/transactions"),re=_e(require("bn.js")),Vn=require("@cetusprotocol/aggregator-sdk");var $n=require("@mysten/sui/utils"),Kn=_e(require("decimal.js"));var jt=_e(require("bn.js")),We=_e(require("decimal.js"));var Oi=_e(require("bn.js")),Ti=_e(require("decimal.js")),He=(i,t,e)=>{if(t==="min"){if(i===Pi.getMinIndex(e))return!0}else if(i===Pi.getMaxIndex(e))return!0;return!1};function xo(i){if(i.length!==8)throw new Error("Invalid u64 byte array length");let t=i.map(e=>e.toString(16).padStart(2,"0")).reverse().join("");return BigInt(`0x${t}`).toString()}function jn(i,t,e,n,o){let a=new Oi.default(10).pow(new Oi.default(n)),{coin_amount_b:s}=ne.estLiquidityAndCoinAmountFromOneAmounts(i,t,a,!0,!0,0,e),d=J.sqrtPriceX64ToPrice(e,n,o),h=E(a.toString()).mul(d).div(new Ti.default(10).pow(n)).mul(new Ti.default(10).pow(o)),m=h.add(s.toString()),w=h.div(m),b=E(s.toString()).div(m);return{ratioA:w,ratioB:b}}function ye(i,t,e,n,o,a,s){let d=E(le(E(e).mul(i).div(n).toString(),a)).toFixed(0),h=E(le(E(e).mul(t).div(o).toString(),s)).toFixed(0);return{amountA:d,amountB:h}}function Ii(i,t,e,n,o,a,s,d){let{ratioA:h,ratioB:m}=jn(i,t,e,s,d);return ye(h,m,n,o,a,s,d)}var pe=10n;function ze(i){let{fields:t,type:e}=i,{liquidity_range:n,pool_id:o,wrapped_position:a}=t.clmm_vault.fields,s=a.fields,d={clmm_pool_id:o,liquidity_range:{lower_offset:n.fields.lower_offset,rebalance_threshold:n.fields.rebalance_threshold,upper_offset:n.fields.upper_offset},wrapped_position:{coin_type_a:tt(s.coin_type_a.fields.name,!1),coin_type_b:tt(s.coin_type_b.fields.name,!1),description:s.description,id:s.id.id,index:s.index,liquidity:s.liquidity,name:s.name,pool:s.pool,tick_lower_index:ki(BigInt(s.tick_lower_index.fields.bits)),tick_upper_index:ki(BigInt(s.tick_upper_index.fields.bits)),url:s.url}},{balances:h}=t.buffer_assets.fields,m=h.fields.contents.map(b=>{let{key:y,value:A}=b.fields;return{coin_type:tt(y.fields.name,!1),value:A}});return{id:t.id.id,is_pause:t.is_pause,clmm_vault:d,lp_token_treasury:t.lp_token_treasury.fields.total_supply.fields.value,protocol_fees_handle:t.protocol_fees.fields.id.id,hard_cap:t.hard_cap,quote_type:ce(t.quote_type.fields.name).full_address,lp_token_type:ce(e).type_arguments[0],balances:m,protocol_fee_rate:t.protocol_fee_rate}}async function Po(i,t=!0){i.getData().commands.forEach((e,n)=>{})}function Bi(i,t){let e=E(i).div(t).toString(),n=E(t).div(i).toString();return{quote_per_base:e,base_per_quote:n}}function Un(i,t,e,n){let o=10n**(pe+n-e),a=10n**(pe+e-n),s=zt.checkMulDivFloor(new jt.default(i.toString()),new jt.default(o.toString()),new jt.default(t.toString()),64),d=zt.checkMulDivFloor(new jt.default(t.toString()),new jt.default(a.toString()),new jt.default(i.toString()),64);return{primitive_base_price:BigInt(s.toString()),primitive_quote_price:BigInt(d.toString())}}function Ge(i,t){let e=t?i[t].price:{coin_type:"",price:"1",coin_decimals:6,oracle_price:1000000000000000000000000n},n=new jt.default("0");for(let[o,{price:a,value:s}]of Object.entries(i)){let{primitive_quote_price:d}=Un(BigInt(e.oracle_price),BigInt(a.oracle_price),BigInt(e.coin_decimals),BigInt(a.coin_decimals)),h=zt.checkMulDivFloor(new jt.default(d.toString()),new jt.default(s),new jt.default((BigInt(10)**pe).toString()),64);n=n.add(h)}return n.toString()}async function Ni(i,t,e,n,o=!1){let{clmm_vault:a,quote_type:s}=t,{wrapped_position:d}=a,{tick_lower_index:h,tick_upper_index:m,liquidity:w}=d,{current_sqrt_price:b,coin_type_a:y,coin_type_b:A}=e,C=[...n],k=s&&tt(t.quote_type,!1),B=J.tickIndexToSqrtPriceX64(h),P=J.tickIndexToSqrtPriceX64(m),{coin_amount_a:q,coin_amount_b:x}=ne.getCoinAmountFromLiquidity(new jt.default(w),new jt.default(b),new jt.default(B),new jt.default(P),!1);C.push({coin_type:y,value:q.toString()}),C.push({coin_type:A,value:x.toString()});let R={};C.forEach(K=>{let X=tt(K.coin_type,!1),Q=E(K.value),Vt=R[X]||0;R[X]=Q.add(Vt).toString()});let Dt=Object.entries(R).filter(([K,X])=>E(X).gt(0)).map(([K])=>K);if(k&&!Dt.includes(k)&&Dt.push(k),Dt.length===0)return"0";let Z=await i.sdk.PythPrice.getLatestPrice(Dt,o),Ut={};return Dt.forEach(K=>{let X=Z[K];if(X)Ut[K]={price:X,value:R[K]||"0"};else throw Error(`find ${K} price fail `)}),Ge(Ut,k).toString()}function Fi(i,t,e){return E(i).eq(0)?t:E(i).mul(t).div(e).toFixed(0,We.default.ROUND_HALF_UP)}function Eo(i,t,e){return E(i).eq(0)?"0":E(i).mul(t).div(e).toFixed(0,We.default.ROUND_HALF_UP)}function ge(i,t,e){return E(i).eq(0)?"0":E(t).mul(e).div(i).toFixed(0,We.default.ROUND_HALF_UP)}function Oe(i,t){let{balances:e,lp_token_treasury:n}=t,o={};return e.forEach(a=>{let s=ge(n,i,a.value),d={coin_type:a.coin_type,value:s};o[d.coin_type]=d}),o}function Di(i,t,e,n){let{tick_lower_index:o,tick_upper_index:a,coin_type_a:s,coin_type_b:d,liquidity:h}=e.clmm_vault.wrapped_position,m=ge(e.lp_token_treasury,t,h),w=J.tickIndexToSqrtPriceX64(o),b=J.tickIndexToSqrtPriceX64(a),y=ne.getCoinAmountFromLiquidity(new jt.default(m),new jt.default(n.current_sqrt_price),w,b,!0),A=y.coin_amount_a.toString(),C=y.coin_amount_b.toString(),k=Oe(t,e),B=E(A).add(k[s]?.value||"0").toFixed(0),P=E(C).add(k[d]?.value||"0").toFixed(0);return{vault_id:e.id,clmm_pool_id:n.id,owner:i,lp_token_type:e.lp_token_type,lp_token_balance:t,liquidity:m,tick_lower_index:o,tick_upper_index:a,amount_a:B,amount_b:P,coin_type_a:s,coin_type_b:d}}function Ye(i,t,e){let n=[];return t.forEach(o=>{E(o.value).gt(0)&&n.push(o)}),e.forEach(o=>{E(o.value).gt(0)&&n.push(o)}),i.balances.forEach(o=>{E(o.value).gt(0)&&n.push(o)}),n}function qi(i,t,e){let{coin_type_a:n,coin_type_b:o}=i.clmm_vault.wrapped_position,a=Ye(i,t,e),s=new Set;return a.forEach(d=>s.add(d.coin_type)),s.add(n),s.add(o),Array.from(s)}function Ri(i,t){if(!pe)throw new Error("oraclePriceMultiplierDecimal is required");if(i===0n)throw new Error("Invalid oracle price");if(t<0n){let e=10n**(pe- -t);return i*e}return i/10n**(t+pe)}function Li(i){return`0x${Buffer.from(i).toString("hex")}`}function Oo(i,t,e,n,o){let a="0",s="0";return He(i,"min",Number(e))?a="0":a=J.tickIndexToPrice(Number(i),n,o).toString(),He(t,"max",Number(e))?s="\u221E":s=J.tickIndexToPrice(Number(t),n,o).toString(),{minPrice:a,maxPrice:s}}var Te=class{_sdk;constructor(t){this._sdk=t}get sdk(){return this._sdk}async getProtocolFeeRate(){let t="getProtocolFeeRate",e=this._sdk.getCache(t);if(e)return e;let{configs:n}=this._sdk.sdkOptions.vaults,a=(await this._sdk.FullClient.getObject({id:n.global_config_id,options:{showContent:!0}})).data.content.fields.protocol_fee_rate;return this._sdk.updateCache(t,a),a}async getOwnerVaultsBalance(t,e){let{pool:n,clmmPool:o}=await this.getVaultAndPool(e,!0),s=(await this._sdk.FullClient.getBalance({owner:t,coinType:n.lp_token_type})).totalBalance;return Di(t,s,n,o)}async getOwnerVaultsBalanceList(t){let{data:e}=await this.getPoolList(),n=[];for(let o=0;o<e.length;o++){let a=await this.getOwnerVaultsBalance(t,e[o].id);n.push(a)}return n}async getVaultsPositionRewarderAndFee(t){try{let{package_id:e}=this._sdk.sdkOptions.vaults,{pool:n,clmmPool:o}=await this.getVaultAndPool(t,!1),a=`getVaultsPositionRewarderAndFee_${t}`,s=this._sdk.getCache(a);if(s)return s;let{lp_token_type:d}=n,{rewarder_infos:h,id:m,coin_type_a:w,coin_type_b:b}=o,y=new Me.Transaction;this.buildCollectRewardPayload({reward_coin_types:[...h.map(q=>q.coin_type)],lp_token_type:d,pool_id:t,clmm_pool_id:m,coin_type_a:w,coin_type_b:b},y),this.buildCollectFeePayload({lp_token_type:d,pool_id:t,clmm_pool_id:m,coin_type_a:w,coin_type_b:b},y);let A=await this._sdk.FullClient.sendSimulationTransaction(y,(0,$n.normalizeSuiAddress)("0x0")),C=`${e}::pool::FeeClaimedEvent`,k=`${e}::pool::RewardClaimedEvent`,B=[],P=[];return A?.events.forEach(q=>{let{parsedJson:x,type:R}=q;R===C&&(B.push({coin_type:tt(w,!1),value:x.amount_a}),B.push({coin_type:tt(b,!1),value:x.amount_b})),R===k&&P.push({coin_type:tt(x.reward_type.name,!1),value:x.amount})}),this._sdk.updateCache(a,{feeList:B,rewarderList:P},10*1e3),{feeList:B,rewarderList:P}}catch{}return{feeList:[],rewarderList:[]}}async getPoolList(t="all"){let e={data:[],has_next_page:!1},{vaults:n}=this._sdk.sdkOptions,{FullClient:o}=this._sdk,a=await o.getDynamicFieldsByPage(n.configs.pools_handle,t),s=a.data.map(d=>d.name.value);return s.length>0&&((await o.batchGetObjects(s,{showContent:!0})).forEach(h=>{let m=ze(h.data.content);this._sdk.updateCache(`getPool_${m.id}`,m),e.data.push(m)}),e.next_cursor=a.next_cursor,e.has_next_page=a.has_next_page),e}async getPool(t,e=!0){let n=`getPool_${t}`,o=this._sdk.getCache(n,e);if(o)return o;let a=await this._sdk.FullClient.getObject({id:t,options:{showContent:!0}}),s=ze(a.data.content);return this._sdk.updateCache(n,s),s}buildCollectRewardPayload(t,e){let{coin_type_a:n,coin_type_b:o,pool_id:a,clmm_pool_id:s,lp_token_type:d,reward_coin_types:h}=t,{published_at:m,configs:w,clmm_configs:b}=this._sdk.sdkOptions.vaults;return e=e||new Me.Transaction,h.forEach(y=>{e.moveCall({target:`${m}::pool::collect_reward`,typeArguments:[n,o,d,y],arguments:[e.object(a),e.object(w.global_config_id),e.object(b.global_config_id),e.object(s),e.object(b.global_vault_id),e.object(he)]})}),e}buildCollectFeePayload(t,e){let{coin_type_a:n,coin_type_b:o,pool_id:a,clmm_pool_id:s,lp_token_type:d}=t,{published_at:h,configs:m,clmm_configs:w}=this._sdk.sdkOptions.vaults;return e=e||new Me.Transaction,e.moveCall({target:`${h}::pool::collect_fee`,typeArguments:[n,o,d],arguments:[e.object(a),e.object(m.global_config_id),e.object(w.global_config_id),e.object(s)]}),e}async buildDepositPayload(t,e){let{pool_id:n,amount_a:o,amount_b:a,return_lp_coin:s}=t,{pool:d,clmmPool:h}=await this.getVaultAndPool(n,!0),{rewarderList:m,feeList:w}=await this.getVaultsPositionRewarderAndFee(n),b=qi(d,m,w),{lp_token_type:y}=d,{rewarder_infos:A,id:C,coin_type_a:k,coin_type_b:B}=h,P=m.length>0?m.filter(X=>E(X.value.toString()).gt(0)).map(X=>tt(X.coin_type,!1)):A.map(X=>tt(X.coin_type,!1)),{published_at:q,configs:x,clmm_configs:R}=this._sdk.sdkOptions.vaults;e=e||new Me.Transaction,e=this.buildCollectRewardPayload({reward_coin_types:P,lp_token_type:y,pool_id:n,clmm_pool_id:C,coin_type_a:k,coin_type_b:B},e),e=this.buildCollectFeePayload({lp_token_type:y,pool_id:n,clmm_pool_id:C,coin_type_a:k,coin_type_b:B},e),e=await this._sdk.PythPrice.buildUpdatePythPricePayload(b,e),e.moveCall({target:`${q}::pool::calculate_aum`,typeArguments:[k,B,y],arguments:[e.object(n),e.object(x.global_config_id),e.object(x.pyth_oracle_id),e.object(R.global_config_id),e.object(C),e.object(he)]});let Dt=await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress,k),Z=await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress,B),Ut=Ee.buildCoinForAmount(e,Dt,BigInt(o),k,!1,!0),G=Ee.buildCoinForAmount(e,Z,BigInt(a),B,!1,!0),K=e.moveCall({target:`${q}::pool::${s?"deposit_for":"deposit"}`,typeArguments:[k,B,y],arguments:[e.object(n),e.object(x.global_config_id),e.object(x.pyth_oracle_id),e.object(R.global_config_id),e.object(C),Ut.target_coin,G.target_coin,e.object(he)]});return s?K:void 0}async buildWithdrawPayload(t,e){let{pool_id:n,burn_lp_amount:o,mode:a,slippage:s,partner:d}=t,{pool:h,clmmPool:m}=await this.getVaultAndPool(n,!0),{rewarderList:w}=await this.getVaultsPositionRewarderAndFee(n),{published_at:b,configs:y,clmm_configs:A}=this._sdk.sdkOptions.vaults;e=e||new Me.Transaction;let{lp_token_type:C,clmm_vault:k,lp_token_treasury:B}=h,{rewarder_infos:P,id:q,coin_type_a:x,coin_type_b:R,liquidity:Dt}=m,Z=w.length>0?w.filter(U=>E(U.value.toString()).gt(0)).map(U=>tt(U.coin_type,!1)):P.map(U=>tt(U.coin_type,!1));e=this.buildCollectRewardPayload({reward_coin_types:Z,lp_token_type:C,pool_id:n,clmm_pool_id:q,coin_type_a:x,coin_type_b:R},e),e=this.buildCollectFeePayload({lp_token_type:C,pool_id:n,clmm_pool_id:q,coin_type_a:x,coin_type_b:R},e);let Ut=await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress,C),G=Ee.buildCoinForAmount(e,Ut,BigInt(o),C,!1,!0),[K,X,Q]=e.moveCall({target:`${b}::pool::withdraw`,typeArguments:[x,R,C],arguments:[e.object(n),e.object(y.global_config_id),e.object(A.global_config_id),e.object(q),G.target_coin,e.object(he)]}),Vt=this.buildWithdrawBufferReward({reward_coin_types:Z.filter(U=>![tt(x,!1),tt(R,!1)].includes(U)),pool_id:n,withdraw_cert:Q,lp_token_type:C},e);if(e.moveCall({target:`${b}::pool::destory_withdraw_cert`,typeArguments:[C],arguments:[e.object(n),e.object(y.global_config_id),Q]}),a!=="FixedOneSide"){if(s===void 0)throw Error("the slippage is undefined");let U=a==="OnlyCoinA",Zt=U?X:K,r=ge(B,o,Dt.toString());await this.handleWithSwap(e,Zt,x,R,o,n,U,s,d,[{poolID:q,ticklower:k.wrapped_position.tick_lower_index,tickUpper:k.wrapped_position.tick_upper_index,deltaLiquidity:Number(r)}]),e.transferObjects([U?K:X,...Vt],this._sdk.senderAddress)}else e.transferObjects([K,X,...Vt],this._sdk.senderAddress);return e}async handleWithSwap(t,e,n,o,a,s,d,h,m,w){let{receive_amount_a:b,receive_amount_b:y}=await this.preCalculateWithdrawAmount({pool_id:s,mode:"FixedOneSide",burn_lp_amount:a}),A=d?o:n,C=d?n:o,k=d?y:b,B={from:A,target:C,amount:new re.default(k),byAmountIn:!0,depth:3,splitAlgorithm:void 0,splitFactor:void 0,splitCount:void 0,providers:this._sdk.sdkOptions.providers,liquidityChanges:w},P=await this._sdk.AggregatorClient.findRouters(B);if(!P)throw Error("not find Router");if(P.error!==void 0)throw Error(`find Router error : ${P.error}`);let q={routers:P,inputCoin:e,slippage:h,txb:t,partner:m},x=await this._sdk.AggregatorClient.fixableRouterSwap(q);t.transferObjects([x],t.pure.address(this._sdk.getVerifySenderAddress()))}buildWithdrawBufferReward({reward_coin_types:t,pool_id:e,withdraw_cert:n,lp_token_type:o},a){let{published_at:s,configs:d}=this._sdk.sdkOptions.vaults;return t.map(h=>a.moveCall({target:`${s}::pool::withdraw_buffer_reward`,typeArguments:[o,h],arguments:[a.object(e),a.object(d.global_config_id),n]}))}async preCalculateDepositValue(t,e){let{amount_usd:n,pool_id:o,mode:a,uuid:s}=t,{pool:d,clmmPool:h}=await this.getVaultAndPool(o,!1),{current_sqrt_price:m}=h,{tick_lower_index:w,tick_upper_index:b,coin_type_a:y,coin_type_b:A}=d.clmm_vault.wrapped_position,C="0",k="0",B=await this._sdk.PythPrice.getLatestPrice([y,A],!0),P=B[y],q=B[A];if(!P||!q)throw new Error("get coin price failed");switch(a){case"FixedOneSide":{let{amountA:x,amountB:R}=Ii(w,b,new re.default(m),n,P.price,q.price,P.coin_decimals,q.coin_decimals);C=x.toString(),k=R.toString();break}case"FlexibleBoth":{let{amountA:x,amountB:R}=ye(E(.5),E(.5),n,P.price,q.price,P.coin_decimals,q.coin_decimals);C=x.toString(),k=R.toString();break}case"OnlyCoinA":{let{amountA:x,amountB:R}=ye(E(1),E(0),n,P.price,q.price,P.coin_decimals,q.coin_decimals);C=x.toString(),k=R.toString();break}case"OnlyCoinB":{let{amountA:x,amountB:R}=ye(E(0),E(1),n,P.price,q.price,P.coin_decimals,q.coin_decimals);C=x.toString(),k=R.toString();break}default:throw new Error(`Unsupported deposit mode: ${a}`)}return e&&this.calculateDepositLpAmountBoth(d,h,C,k).then(x=>{e.onSuccess(x,s)}).catch(x=>{e.onError(x,s)}),{deposit_amount_a:C,deposit_amount_b:k,mode:a}}async preCalculateDepositAmount(t,e){let{mode:n,pool_id:o,uuid:a}=t,s="0",d="0",{pool:h,clmmPool:m}=await this.getVaultAndPool(o,!1);switch(n){case"FixedOneSide":{let{fixed_amount:w,fixed_coin_a:b}=t,{other_amount:y}=this.calculateDepositOtherSide(h,m,w,b,e,a);b?(s=w,d=y):(d=w,s=y);break}case"FlexibleBoth":{let{coin_amount_a:w,coin_amount_b:b}=t;s=w,d=b,this.calculateDepositLpAmountBoth(h,m,w,b).then(y=>{e.onSuccess(y,a)}).catch(y=>{e.onError(y,a)});break}case"OnlyCoinA":{s=t.coin_amount_a,this.calculateDepositLpAmountBoth(h,m,s,"0").then(w=>{e.onSuccess(w,a)}).catch(w=>{e.onError(w,a)});break}case"OnlyCoinB":{d=t.coin_amount_b,this.calculateDepositLpAmountBoth(h,m,"0",d).then(w=>{e.onSuccess(w,a)}).catch(w=>{e.onError(w,a)});break}default:throw new Error(`Unsupported deposit mode: ${n}`)}return{deposit_amount_a:s,deposit_amount_b:d,mode:n}}async getVaultAndPool(t,e=!1){let n=await this.getPool(t,e);if(n===void 0)throw new Error("get vaults pool failed");let o=await this._sdk.CetusClmmSDK.Pool.getPool(n.clmm_vault.clmm_pool_id,e);if(o===void 0)throw new Error("get vaults clmmPool failed");return{clmmPool:o,pool:n}}calculateDepositOtherSide(t,e,n,o,a,s){let{clmm_vault:d}=t,{current_sqrt_price:h}=e,m=d.wrapped_position.tick_lower_index,w=d.wrapped_position.tick_upper_index,b=ne.estLiquidityAndCoinAmountFromOneAmounts(m,w,new re.default(n),o,!0,1,new re.default(h)),{coin_amount_a:y,coin_amount_b:A}=b;return this.calculateDepositLpAmountBoth(t,e,y.toString(),A.toString()).then(C=>{a.onSuccess(C,s)}).catch(C=>{a.onError(C,s)}),{other_amount:o?A.toString():y.toString()}}async calculateDepositLpAmountBoth(t,e,n,o){let{lp_token_treasury:a,hard_cap:s}=t,d=tt(e.coin_type_a,!1),h=tt(e.coin_type_b,!1),{rewarderList:m,feeList:w}=await this.getVaultsPositionRewarderAndFee(t.id),b=Ye(t,w,m),y=await Ni(this,t,e,b,!0),A=await this._sdk.PythPrice.getLatestPrice([d,h],!0),C=A[d],k=A[h];if(!C)throw Error(`not find ${d} price`);if(!k)throw Error(`not find ${h} price`);let B={};B[d]={price:C,value:n.toString()},B[h]={price:k,value:o.toString()};let P=Ge(B,t.quote_type);return Fi(a,P,y)}async preCalculateWithdrawAmount(t){let{mode:e,pool_id:n}=t,o="0",a="0",s="0",d="0",h,{pool:m,clmmPool:w}=await this.getVaultAndPool(n,!1);switch(e){case"FixedOneSide":let b=await this.calculateWithdrawOtherSide(m,w,t.burn_lp_amount);s=b.burn_lp_amount,o=b.receive_amount_a,a=b.receive_amount_b,d=b.remove_liquidity;break;case"OnlyCoinA":{let y=await this.calculateWithdrawOnlyCoin(m,w,t.burn_lp_amount,!0);s=y.burn_lp_amount,o=y.receive_amount_a,a=y.receive_amount_b,d=y.remove_liquidity,h=y.swap;break}case"OnlyCoinB":{let y=await this.calculateWithdrawOnlyCoin(m,w,t.burn_lp_amount,!1);s=y.burn_lp_amount,o=y.receive_amount_a,a=y.receive_amount_b,d=y.remove_liquidity,h=y.swap;break}default:throw new Error(`Unsupported withdraw mode: ${e}`)}return{remove_liquidity:d,burn_lp_amount:s,receive_amount_a:o,receive_amount_b:a,mode:e,swap:h}}async calculateWithdrawOtherSide(t,e,n){let{clmm_vault:o,lp_token_treasury:a}=t,{current_sqrt_price:s}=e,{coin_type_a:d,coin_type_b:h}=t.clmm_vault.wrapped_position,m=o.wrapped_position.tick_lower_index,w=o.wrapped_position.tick_upper_index,b=o.wrapped_position.liquidity,y=J.tickIndexToSqrtPriceX64(m),A=J.tickIndexToSqrtPriceX64(w),C=ge(a,n,b),{coin_amount_a:k,coin_amount_b:B}=ne.getCoinAmountFromLiquidity(new re.default(C),new re.default(s),y,A,!1),P=Oe(n,t),q=E(k.toString()).add(P[d]?.value||"0").toFixed(0),x=E(B.toString()).add(P[h]?.value||"0").toFixed(0);return{receive_amount_a:q,receive_amount_b:x,burn_lp_amount:n,mode:"FixedOneSide",remove_liquidity:C}}async calculateWithdrawOnlyCoin(t,e,n,o){let{totalReceiveAmount:a,swapAmountIn:s,swapAmountOut:d,removeLiquidity:h}=await this.calculateWithdrawOnlyCoinByLpAmount(t,e,o,n);return{receive_amount_a:o?a:"0",receive_amount_b:o?"0":a,burn_lp_amount:n,remove_liquidity:h,swap:{swap_amount_in:s,swap_amount_out:d},mode:"FixedOneSide"}}async calculateWithdrawOnlyCoinByLpAmount(t,e,n,o){let{clmm_vault:a,lp_token_treasury:s}=t,{current_sqrt_price:d}=e,{coin_type_a:h,coin_type_b:m}=a.wrapped_position,w=a.wrapped_position.tick_lower_index,b=a.wrapped_position.tick_upper_index,y=a.wrapped_position.liquidity,A=J.tickIndexToSqrtPriceX64(w),C=J.tickIndexToSqrtPriceX64(b),k=ge(s,o,y),{coin_amount_a:B,coin_amount_b:P}=ne.getCoinAmountFromLiquidity(new re.default(k),new re.default(d),A,C,!1),q=await this._sdk.PythPrice.getLatestPrice([h,m],!0),x=q[h],R=q[m];if(!x)throw Error(`not find ${h} price`);if(!R)throw Error(`not find ${m} price`);let Dt=n?R:x,Z=n?x:R,{quote_per_base:Ut}=Bi(Dt.price,Z.price),G=Oe(o,t),K=E(B.toString()).add(G[h]?.value||"0").toFixed(0),X=E(P.toString()).add(G[m]?.value||"0").toFixed(0),Q=n?K:X,Vt=n?X:K,U=le(E(Ve(Vt,n?R.coin_decimals:x.coin_decimals)).mul(Ut).toString(),n?x.coin_decimals:R.coin_decimals).toString(),Zt=E(Q).add(U).toFixed(0);return{burn_lp_amount:o,removeLiquidity:k,is_coin_a:n,originAmount:Q,swapAmountIn:Vt,swapAmountOut:E(U).toFixed(0,Kn.default.ROUND_DOWN),totalReceiveAmount:Zt}}async getPackageConfigs(){let t={registry_id:"1",pools_handle:"",pyth_oracle_id:"1",oracle_infos_handle:"",prices_handle:"1",global_config_id:"1"},{package_id:e}=this._sdk.sdkOptions.vaults,n=await this._sdk.FullClient.getObject({id:e,options:{showPreviousTransaction:!0}}),o=Ln(n);(await this._sdk.FullClient.queryEventsByPage({Transaction:o})).data.forEach(w=>{let b=w.parsedJson;if(w.type)switch((0,Vn.extractStructTagFromType)(w.type).full_address){case`${e}::pool::InitEvent`:t.registry_id=b.registry_id;break;case`${e}::pyth_oracle::InitEvent`:t.pyth_oracle_id=b.pyth_oracle_id;break;case`${e}::config::InitConfigEvent`:t.global_config_id=b.global_config;break;default:break}});let s=await this._sdk.FullClient.getObject({id:t.registry_id,options:{showContent:!0}}),d=Ei(s);t.pools_handle=d.pools.fields.id.id;let h=await this._sdk.FullClient.getObject({id:t.pyth_oracle_id,options:{showContent:!0}}),m=Ei(h);return t.oracle_infos_handle=m.oracle_infos.fields.id.id,t.prices_handle=m.prices.fields.id.id,this._sdk.sdkOptions.vaults.configs=t,this._sdk.sdkOptions}};var Se=require("@pythnetwork/pyth-sui-js"),Hi=require("@mysten/sui/bcs");var ji={env:"mainnet",fullRpcUrl:"https://fullnode.mainnet.sui.io",vaults:{package_id:"0xc4ebf35be1478318d78c324342854dd2735a036139373a9d41a1aa3a46a01d05",published_at:"0x8462eb7e1c52018f42f2dd2c33605f7212ad8702739a5eaacb0e1a0106f3dc6a",configs:{registry_id:"0x23014734280164270fa4c58f973c00079651e730d6f46491678ea8858f3bd13a",pools_handle:"0x31c810b6fcf3bb8699384e7c61fe9f7db53a04c4c22d514b3f79b6caa86183a8",pyth_oracle_id:"0xaed73fd854dfceedb2522903feec8c0e251764f5945bd38f969473024c4fb4d0",oracle_infos_handle:"0x6951ab4b36639e79ddbd9ba4478d7f72d1c5396b9198d0864c00e796c5815b01",prices_handle:"0x20a65a0f747f4f1ad465bdb61d784bbc5080ee5d200d5dd6594a354723b8c171",global_config_id:"0x5f8371674cd8e1e9eef5542ffe8adb339e3ff3a67894b14c954c7a92e992e0c1"},clmm_configs:{global_config_id:"0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f",global_vault_id:"0xce7bceef26d3ad1f6d9b6f13a953f053e6ed3ca77907516481ce99ae8e588f2b"},pyth_configs:{pyth_state_id:"0x1f9310238ee9298fb703c3419030b35b22bb1cc37113e3bb5007c99aec79e5b8",hermes_service_urls:{primary:"https://hermes.pyth.network"},wormhole_state_id:"0xaeab97f96cf9877fee2883315d459552b2b921edc16d7ceac6eab944dd88919c",pyth_package_id:"0x04e20ddf36af412a4096f9014f4a565af9e812db9a05cc40254846cf6ed0ad91",pyth_published_at:"0x04e20ddf36af412a4096f9014f4a565af9e812db9a05cc40254846cf6ed0ad91"}},aggregatorUrl:"https://api-sui.cetus.zone/router_v2",providers:["CETUS","TURBOS","STEAMM","SPRINGSUI","HAEDAL","FLOWX","AFTERMATH","ALPHAFI","BLUEFIN","METASTABLE","VOLO","SCALLOP","KRIYA","FLOWXV3","DEEPBOOKV3","AFSUI","BLUEMOVE","KRIYAV3","HAEDALPMM","OBRIC"]},Ui={"0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC":{coin_type:"0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC",price_feed_id:"0xeaa020c61cc479712813461ce153894a96a6c00b21ed0cfc2798d1f9a9e9c94a",coin_decimals:6},"0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI":{coin_type:"0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI",price_feed_id:"0x23d7315113f5b1d3ba7a83604c44b94d79f4fd69af77f804fc7f920a6dc65744",coin_decimals:9},"0x06864a6f921804860930db6ddbe2e16acdf8504495ea7481637a1c8b9a8fe54b::cetus::CETUS":{coin_type:"0x06864a6f921804860930db6ddbe2e16acdf8504495ea7481637a1c8b9a8fe54b::cetus::CETUS",price_feed_id:"0xe5b274b2611143df055d6e7cd8d93fe1961716bcd4dca1cad87a83bc1e78c1ef",coin_decimals:9},"0xdeeb7a4662eec9f2f3def03fb937a663dddaa2e215b8078a284d026b7946c270::deep::DEEP":{coin_type:"0xdeeb7a4662eec9f2f3def03fb937a663dddaa2e215b8078a284d026b7946c270::deep::DEEP",price_feed_id:"0x29bdd5248234e33bd93d3b81100b5fa32eaa5997843847e2c2cb16d7c6d9f7ff",coin_decimals:6},"0xa99b8952d4f7d947ea77fe0ecdcc9e5fc0bcab2841d6e2a5aa00c3044e5544b5::navx::NAVX":{coin_type:"0xa99b8952d4f7d947ea77fe0ecdcc9e5fc0bcab2841d6e2a5aa00c3044e5544b5::navx::NAVX",price_feed_id:"0x88250f854c019ef4f88a5c073d52a18bb1c6ac437033f5932cd017d24917ab46",coin_decimals:9},"0x027792d9fed7f9844eb4839566001bb6f6cb4804f66aa2da6fe1ee242d896881::coin::COIN":{coin_type:"0x027792d9fed7f9844eb4839566001bb6f6cb4804f66aa2da6fe1ee242d896881::coin::COIN",price_feed_id:"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43",coin_decimals:9},"0xbde4ba4c2e274a60ce15c1cfff9e5c42e41654ac8b6d906a57efa4bd3c29f47d::hasui::HASUI":{coin_type:"0xbde4ba4c2e274a60ce15c1cfff9e5c42e41654ac8b6d906a57efa4bd3c29f47d::hasui::HASUI",price_feed_id:"0x6120ffcf96395c70aa77e72dcb900bf9d40dccab228efca59a17b90ce423d5e8",coin_decimals:9},"0xd0e89b2af5e4910726fbcd8b8dd37bb79b29e5f83f7491bca830e94f7f226d29::eth::ETH":{coin_type:"0xd0e89b2af5e4910726fbcd8b8dd37bb79b29e5f83f7491bca830e94f7f226d29::eth::ETH",price_feed_id:"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace",coin_decimals:8}};function Vi(i){return i&&(ji.fullRpcUrl=i),new oe(ji)}var $i={env:"testnet",fullRpcUrl:"https://fullnode.testnet.sui.io",vaults:{package_id:"0x0a624f2dcafc9f7f381684b3d553a9837411925b6e8530b1f8f4e6ff3f1fde77",published_at:"0x36f9d8e6219e6ba4d8840165ec27676a1bcb43fc5712f87ca702ac7b5924507d",configs:{registry_id:"0xa2ebae3656c5fbdd2473caadfc077cd1780a881759a356f1a5bc0f9fd470adb9",pools_handle:"0x0df305d395f8e0359da0ac5717bc96e0e1da9792684ec8e8f6a20b7ab544f98e",pyth_oracle_id:"0x6c74aec8d4471d9b05f718c467204c9010c450b94a4e40b0b4031a7d6c386337",global_config_id:"0x8969b09cfdd72b1635263b569027ded4b91bbabd7d8cd91b21e355325e218b10",oracle_infos_handle:"0x8707c6665d202f277ffe8e7fd58684e232622a3e6826b94ba99f7569f253fa9d",prices_handle:"0x74230bb2c6a63235acbfe477ce16144afca7ccd16a458fa93f4605330b5d1c58"},clmm_configs:{global_config_id:"0x9774e359588ead122af1c7e7f64e14ade261cfeecdb5d0eb4a5b3b4c8ab8bd3e",global_vault_id:"0xf78d2ee3c312f298882cb680695e5e8c81b1d441a646caccc058006c2851ddea"},pyth_configs:{pyth_state_id:"0x243759059f4c3111179da5878c12f68d612c21a8d54d85edc86164bb18be1c7c",wormhole_state_id:"0x31358d198147da50db32eda2562951d53973a0c0ad5ed738e9b17d88b213d790",hermes_service_urls:{primary:"https://hermes-beta.pyth.network"},pyth_package_id:"0xabf837e98c26087cba0883c0a7a28326b1fa3c5e1e2c5abdb486f9e8f594c837",pyth_published_at:"0xabf837e98c26087cba0883c0a7a28326b1fa3c5e1e2c5abdb486f9e8f594c837"}},aggregatorUrl:"https://api-sui.devcetus.com/router_v2",providers:["CETUS","DEEPBOOK","KRIYA","KRIYAV3","FLOWX","FLOWXV3","AFTERMATH","TURBOS","HAEDAL","VOLO","AFSUI"]},Ki={};function Zi(i){return i&&($i.fullRpcUrl=i),new oe($i)}var Zn=(e=>(e.mainnet="mainnet",e.testnet="testnet",e))(Zn||{});function To(i){let{env:t,fullNodeUrl:e}=i;return t==="mainnet"?Vi(e):Zi(e)}var Xi=["CETUS","TURBOS","STEAMM","SPRINGSUI","HAEDAL","FLOWX","AFTERMATH","ALPHAFI","BLUEFIN","METASTABLE","VOLO","SCALLOP","KRIYA","FLOWXV3","DEEPBOOKV3","AFSUI","BLUEMOVE","KRIYAV3","HAEDALPMM","OBRIC"];function Xn(i){try{let{request_time:t,error:e,request_params:n,request_url:o,action:a}=i;if(window?.gtag){let s=e.message==="Request Timeout";window.gtag("event","API_Error",{request_action:a,request_url:o,request_time:t,request_params:JSON.stringify(n),error_msg:String(e)})}}catch{}}var Io=16*1024,Ie=class{_sdk;connection;suiPythClient;hasChangeConnection=!1;errorList=[];constructor(t){let{pyth_configs:e}=t.sdkOptions.vaults,{pyth_state_id:n,wormhole_state_id:o}=e;this._sdk=t,this.hasChangeConnection=!1;let{primary:a}=t.sdkOptions.vaults.pyth_configs.hermes_service_urls;if(a.length===0)throw new Error("primary url is empty");this.connection=new Se.SuiPriceServiceConnection(a),this.suiPythClient=new Se.SuiPythClient(this._sdk.FullClient,n,o)}get sdk(){return this._sdk}recordError(t,e,n){let{secondary:o,primary:a}=this._sdk.sdkOptions.vaults.pyth_configs.hermes_service_urls,s={error:t,request_params:e,request_url:this.hasChangeConnection&&o?o:a,action:n,request_time:Date.now()};Xn(s),this.errorList.push(s)}readErrorList(){let t=this.errorList;return this.errorList=[],t}async getFeedInfoList(t){let e=t.map(o=>this.getFeedInfo(o,!1));return await Promise.all(e)}priceCheck(t,e=60){let n=Math.floor(Date.now()/1e3);if(!(Math.abs(n-t.last_update_time)>e))return t}async getLatestPrice(t,e=!1){let n={},o=[];if(e?t.forEach(d=>{let h=this._sdk.getCache(`getLatestPrice_${d}`);h&&this.priceCheck(h,60)?n[d]=h:o.push(d)}):o=[...t],o.length===0)return n;let a=await this.getFeedInfoList(o);return(await this.getLatestPriceFeeds(a.map(d=>d.price_feed_id)))?.forEach((d,h)=>{let m=d.getPriceUnchecked();if(m){let{price:w,expo:b}=m,y=E(w).mul(E(10).pow(E(b))).toString(),A=a[h],C={coin_type:A.coin_type,price:y,coin_decimals:A.coin_decimals,oracle_price:0n,last_update_time:m.publishTime};C.oracle_price=Ri(BigInt(w),BigInt(b)),n[o[h]]=C,this._sdk.updateCache(`getLatestPrice_${C.coin_type}`,C)}}),n}async getPriceFeedsUpdateData(t){try{return await this.connection.getPriceFeedsUpdateData(t)}catch(e){this.recordError(e,t,"getPriceFeedsUpdateData");let{secondary:n}=this._sdk.sdkOptions.vaults.pyth_configs.hermes_service_urls;if(n){this.hasChangeConnection=!0,this.connection=new Se.SuiPriceServiceConnection(n);try{return await this.connection.getPriceFeedsUpdateData(t)}catch(o){this.recordError(o,t,"getPriceFeedsUpdateData")}}}}async getLatestPriceFeeds(t){try{return await this.connection.getLatestPriceFeeds(t)}catch(e){this.recordError(e,t,"getLatestPriceFeeds");let{secondary:n}=this._sdk.sdkOptions.vaults.pyth_configs.hermes_service_urls;if(n){this.hasChangeConnection=!0,this.connection=new Se.SuiPriceServiceConnection(n);try{return await this.connection.getLatestPriceFeeds(t)}catch(o){this.recordError(o,t,"getLatestPriceFeeds")}}}}async buildUpdatePythPricePayload(t,e){let{pyth_configs:n,published_at:o,configs:a}=this._sdk.sdkOptions.vaults,{pyth_state_id:s,pyth_published_at:d,pyth_package_id:h}=n,{global_config_id:m,pyth_oracle_id:w}=a,b=(await this.getFeedInfoList(t)).map(P=>P.price_feed_id);if(t.length!==b.length)throw Error("find feed id fail");let y=await this.getPriceFeedsUpdateData(b);if(!y)throw new Error("get price update data fail");if(y.length>1)throw new Error("SDK does not support sending multiple accumulator messages in a single transaction");let A=this.suiPythClient.extractVaaBytesFromAccumulatorMessage(y[0]),C=await this.suiPythClient.verifyVaas([A],e),[k]=e.moveCall({target:`${d}::pyth::create_authenticated_price_infos_using_accumulator`,typeArguments:[],arguments:[e.object(s),e.pure(Hi.bcs.vector(Hi.bcs.U8).serialize(Array.from(y[0]),{maxSize:Io}).toBytes()),C[0],e.object(he)]}),B=0;for(let P of b){let q=await this.suiPythClient.getPriceFeedObjectId(P);if(!q)throw new Error(`Price feed ${P} not found, please create it first`);[k]=e.moveCall({target:`${o}::pyth_oracle::update_price`,typeArguments:[t[B]],arguments:[e.object(w),e.object(m),e.object(s),k,e.object(q),e.object(he)]}),B+=1}return e.moveCall({target:`${d}::hot_potato_vector::destroy`,arguments:[k],typeArguments:[`${h}::price_info::PriceInfo`]}),e}getFeedInfoByJson(t){let{env:e}=this._sdk.sdkOptions,n=tt(t,!1);return e==="mainnet"?Ui[n]:Ki[n]}async getFeedInfo(t,e){let n=`getFeedInfo_${t}`,o=this._sdk.getCache(n,e);if(o)return o;let a=this.getFeedInfoByJson(t);if(a)return a;let{configs:s}=this._sdk.sdkOptions.vaults,d=await this._sdk.FullClient.getDynamicFieldObject({parentId:s.oracle_infos_handle,name:{type:"0x1::type_name::TypeName",value:tt(t,!0)}}),{fields:h}=d.data.content.fields.value,m={coin_type:t,price_feed_id:Li(h.price_feed_id),coin_decimals:h.coin_decimals};return this._sdk.updateCache(n,m),m}};var oe=class{_cache={};_sdkOptions;_senderAddress="";_vaults;_pythPrice;_aggregatorClient;_cetusClmmSDK;constructor(t){(!t.providers||t.providers.length===0)&&(t.providers=Xi),this._sdkOptions=t,this._cetusClmmSDK=t.env==="testnet"?Wi.CetusClmmSDK.createSDK({env:"testnet",full_rpc_url:t.fullRpcUrl}):Wi.CetusClmmSDK.createSDK({env:"mainnet",full_rpc_url:t.fullRpcUrl}),this._vaults=new Te(this),this._pythPrice=new Ie(this);let{primary:e,secondary:n}=t.vaults.pyth_configs.hermes_service_urls,o=[e,n].filter(Boolean);this._aggregatorClient=new Be.AggregatorClient({endpoint:t.aggregatorUrl,signer:(0,Je.normalizeSuiAddress)("0x0"),client:new Hn.SuiClient({url:t.fullRpcUrl}),env:t.env==="testnet"?Be.Env.Testnet:Be.Env.Mainnet,pythUrls:o}),xi(this._sdkOptions)}get senderAddress(){return this._senderAddress}set senderAddress(t){this._senderAddress=t,this.CetusClmmSDK.setSenderAddress(t)}get sdkOptions(){return this._sdkOptions}get Vaults(){return this._vaults}get PythPrice(){return this._pythPrice}get FullClient(){return this._cetusClmmSDK.FullClient}get AggregatorClient(){return this._aggregatorClient}get CetusClmmSDK(){return this._cetusClmmSDK}getVerifySenderAddress(){if(this.senderAddress.length===0||!(0,Je.isValidSuiAddress)(this.senderAddress))throw Error("this sdk senderAddress is illegal");return this.senderAddress}async getOwnerCoinAssets(t,e){return await this._cetusClmmSDK.FullClient.getOwnerCoinAssets(t,e)}async getOwnerCoinBalances(t,e){return await this._cetusClmmSDK.FullClient.getOwnerCoinBalances(t,e)}updateCache(t,e,n=An){this.CetusClmmSDK.updateCache(t,e,n)}getCache(t,e=!1){return this.CetusClmmSDK.getCache(t,e)}};var Bo=oe;0&&(module.exports={PythPriceModule,SdkEnv,VaultsModule,VolatileVaultsSDK,buildPoolData,buildVaultsBalance,calculateAum,calculateDepositRatioFixTokenA,calculateTotalAumBasedOnQuote,checkIsMinOrMaxIndex,convertU64BytesToDecimal,defaultProvider,estCoinAmountsFromTotalAmount,feed_map_mainnet,feed_map_testnet,getCoinAmountsFromRatio,getPositionPriceRange,getPriceWithFormattedDecimals,getPrimitivePricesFromBaseQuote,getQuotePerBaseAndBasePerQuote,getShareBufferAssets,getUpdatePriceType,getVaultValidBalances,get_lp_amount_by_liquidity,get_lp_amount_by_tvl,get_share_liquidity_by_amount,initMainnetSDK,initTestnetSDK,initVaultSDK,oraclePriceMultiplierDecimal,printTransaction,toSuiObjectId,vaults_mainnet,vaults_testnet});
|
|
2
|
+
/*! Bundled license information:
|
|
3
|
+
|
|
4
|
+
@cetusprotocol/common-sdk/dist/index.mjs:
|
|
5
|
+
(*! Bundled license information:
|
|
6
|
+
|
|
7
|
+
decimal.js/decimal.mjs:
|
|
8
|
+
(*!
|
|
9
|
+
* decimal.js v10.5.0
|
|
10
|
+
* An arbitrary-precision Decimal type for JavaScript.
|
|
11
|
+
* https://github.com/MikeMcl/decimal.js
|
|
12
|
+
* Copyright (c) 2025 Michael Mclaughlin <M8ch88l@gmail.com>
|
|
13
|
+
* MIT Licence
|
|
14
|
+
*)
|
|
15
|
+
*)
|
|
16
|
+
*/
|