@cetusprotocol/dlmm-zap-sdk 1.0.0 → 1.0.2

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/package.json CHANGED
@@ -1,18 +1,25 @@
1
1
  {
2
2
  "name": "@cetusprotocol/dlmm-zap-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "SDK for cetus dlmm zap",
5
5
  "typings": "dist/index.d.ts",
6
6
  "main": "dist/index.js",
7
- "module": "dist/index.mjs",
7
+ "type": "module",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
8
15
  "scripts": {
9
- "test": "jest",
16
+ "test": "vitest",
10
17
  "lint": "eslint src/*.ts src/**/*.ts",
11
18
  "lint:fix": "eslint src/*.ts src/**/*.ts --fix",
12
19
  "build": "pnpm run build:tsup",
13
20
  "build:tsup": "npm run build:clean && npm run _build:node",
14
21
  "build:clean": "rm -rf dist",
15
- "_build:node": "tsup --format cjs,esm --dts",
22
+ "_build:node": "tsup --format esm --dts",
16
23
  "build:doc": "npx typedoc",
17
24
  "publish_batch:test": "npm publish --tag experimental",
18
25
  "publish:test": "node ../../scripts/version.js zap && npm publish --tag experimental"
@@ -24,6 +31,9 @@
24
31
  "keywords": [],
25
32
  "author": "test",
26
33
  "license": "Apache-2.0",
34
+ "dependencies": {
35
+ "@mysten/sui": "2.6.0"
36
+ },
27
37
  "devDependencies": {
28
38
  "@cetusprotocol/common-sdk": "workspace:*",
29
39
  "@cetusprotocol/dlmm-sdk": "workspace:*",
@@ -31,8 +41,7 @@
31
41
  },
32
42
  "peerDependencies": {
33
43
  "@cetusprotocol/common-sdk": "*",
34
- "@cetusprotocol/aggregator-sdk": ">=1.1.6",
35
- "@cetusprotocol/dlmm-sdk": "*",
36
- "@mysten/sui": "*"
44
+ "@cetusprotocol/aggregator-sdk": "*",
45
+ "@cetusprotocol/dlmm-sdk": "*"
37
46
  }
38
47
  }
package/dist/index.d.mts DELETED
@@ -1,331 +0,0 @@
1
- import { AggregatorClient } from '@cetusprotocol/aggregator-sdk';
2
- import { StrategyType, BinAmount, BinLiquidityInfo, CetusDlmmSDK } from '@cetusprotocol/dlmm-sdk';
3
- import { IModule, SdkWrapper, BaseSdkOptions, BaseError } from '@cetusprotocol/common-sdk';
4
- import { TransactionObjectArgument, Transaction } from '@mysten/sui/transactions';
5
- import Decimal from 'decimal.js';
6
-
7
- /**
8
- * Represents a pair of coins used in a financial context.
9
- */
10
- type CoinPairType = {
11
- /**
12
- * The address type of the coin a in the pair.
13
- */
14
- coin_type_a: SuiAddressType;
15
- /**
16
- * The address type of the coin b in the pair.
17
- */
18
- coin_type_b: SuiAddressType;
19
- };
20
-
21
- /**
22
- * Represents a SUI address, which is a string.
23
- */
24
- type SuiAddressType = string;
25
-
26
- declare const defaultSwapSlippage = 0.005;
27
- /**
28
- * Result of a swap operation containing amounts and price information
29
- */
30
- type SwapResult = {
31
- swap_in_amount: string;
32
- swap_out_amount: string;
33
- route_obj?: any;
34
- };
35
- type BaseDepositOptions = {
36
- pool_id: string;
37
- strategy_type: StrategyType;
38
- active_bin_of_pool?: BinAmount;
39
- lower_bin_id: number;
40
- upper_bin_id: number;
41
- active_id: number;
42
- bin_step: number;
43
- };
44
- type OnlyCoinDepositOptions = {
45
- fix_amount_a: boolean;
46
- coin_amount: string;
47
- };
48
- /**
49
- * Result of a deposit calculation
50
- */
51
- type CalculationDepositResult = {
52
- bin_infos: BinLiquidityInfo;
53
- swap_result?: SwapResult;
54
- fix_amount_a: boolean;
55
- coin_amount: string;
56
- };
57
- /**
58
- * Complete options for executing a deposit
59
- */
60
- type DepositOptions = {
61
- deposit_obj: CalculationDepositResult;
62
- slippage: number;
63
- pool_id: string;
64
- strategy_type: StrategyType;
65
- lower_bin_id: number;
66
- upper_bin_id: number;
67
- active_id: number;
68
- bin_step: number;
69
- swap_slippage?: number;
70
- pos_obj?: {
71
- pos_id: string | TransactionObjectArgument;
72
- collect_fee: boolean;
73
- collect_rewarder_types: string[];
74
- };
75
- };
76
- type WithdrawMode = 'OnlyCoinA' | 'OnlyCoinB' | 'Both';
77
- type CalculationWithdrawOptions = {
78
- remove_bin_range: BinAmount[];
79
- active_id: number;
80
- bin_step: number;
81
- expected_receive_amount: string;
82
- is_receive_coin_a: boolean;
83
- mode: WithdrawMode;
84
- coin_decimal_a: number;
85
- coin_decimal_b: number;
86
- prices?: {
87
- coin_a_price: string;
88
- coin_b_price: string;
89
- };
90
- } & CoinPairType;
91
- type CalculationWithdrawAvailableAmountOptions = {
92
- remove_bin_range: BinAmount[];
93
- active_id: number;
94
- bin_step: number;
95
- is_receive_coin_a: boolean;
96
- mode: WithdrawMode;
97
- coin_decimal_a: number;
98
- coin_decimal_b: number;
99
- prices?: {
100
- coin_a_price: string;
101
- coin_b_price: string;
102
- };
103
- };
104
- /**
105
- * Result of a withdrawal calculation
106
- */
107
- type CalculationWithdrawResult = {
108
- remove_liquidity_info: BinLiquidityInfo;
109
- mode: WithdrawMode;
110
- is_receive_coin_a?: boolean;
111
- swap_result?: SwapResult;
112
- expected_receive_amount: string;
113
- remove_percent: string;
114
- };
115
- /**
116
- * Complete options for executing a withdrawal
117
- */
118
- type WithdrawOptions = {
119
- withdraw_obj: CalculationWithdrawResult;
120
- swap_slippage?: number;
121
- pool_id: string;
122
- position_id: string;
123
- active_id: number;
124
- bin_step: number;
125
- slippage: number;
126
- reward_coins: string[];
127
- collect_fee: boolean;
128
- remove_percent?: number;
129
- is_close_position: boolean;
130
- } & CoinPairType;
131
-
132
- /**
133
- * ZapModule handles interactions with clmm pools within the system.
134
- */
135
- declare class ZapModule implements IModule<CetusDlmmZapSDK> {
136
- protected _sdk: CetusDlmmZapSDK;
137
- constructor(sdk: CetusDlmmZapSDK);
138
- /**
139
- * Returns the associated SDK instance
140
- */
141
- get sdk(): CetusDlmmZapSDK;
142
- private calculateBalanceSwapAmountWithoutActiveId;
143
- private calculateBalanceSwapAmount;
144
- /**
145
- * Pre-calculates the deposit amount based on the selected mode.
146
- * @param options
147
- * @param mode_options
148
- * @returns
149
- */
150
- preCalculateDepositAmount(options: BaseDepositOptions, mode_options: OnlyCoinDepositOptions): Promise<CalculationDepositResult>;
151
- buildDepositPayload(options: DepositOptions, tx?: Transaction): Promise<Transaction>;
152
- findRouters(from: string, target: string, amount: string): Promise<SwapResult>;
153
- calculateZapOutAvailableAmount(options: CalculationWithdrawAvailableAmountOptions): {
154
- available_amount: string;
155
- user_total_amount_a: string;
156
- user_total_amount_b: string;
157
- active_bin: BinAmount | undefined;
158
- is_receive_coin_a: boolean;
159
- };
160
- preCalculateWithdrawAmount(options: CalculationWithdrawOptions): Promise<CalculationWithdrawResult>;
161
- buildWithdrawPayload(options: WithdrawOptions): Promise<Transaction>;
162
- }
163
-
164
- /**
165
- * Represents options and configurations for an SDK.
166
- */
167
- interface SdkOptions extends BaseSdkOptions {
168
- /**
169
- * The URL of the aggregator service.
170
- */
171
- aggregator_url: string;
172
- /**
173
- * A list of aggregator providers.
174
- */
175
- providers: string[];
176
- /**
177
- * A list of Pyth price ID.
178
- */
179
- pyth_urls?: string[];
180
- }
181
- /**
182
- * The entry class of CetusDlmmZapSDK, which is almost responsible for all interactions with dlmm zap.
183
- */
184
- declare class CetusDlmmZapSDK extends SdkWrapper<SdkOptions> {
185
- /**
186
- * Module for managing vaults.
187
- */
188
- protected _zapModule: ZapModule;
189
- protected _dlmmSDK: CetusDlmmSDK;
190
- /**
191
- * Client for interacting with the Aggregator service.
192
- */
193
- protected _aggregatorClient: AggregatorClient;
194
- constructor(options: SdkOptions, dlmmSDK?: CetusDlmmSDK);
195
- setSenderAddress(value: string): void;
196
- getSenderAddress(validate?: boolean): string;
197
- /**
198
- * Updates the providers for the AggregatorClient.
199
- * @param providers - The new providers to set.
200
- */
201
- updateProviders(providers: string[]): void;
202
- updateFullRpcUrl(url: string): void;
203
- /**
204
- * Getter for the DlmmSDK property.
205
- * @returns {CetusDlmmSDK} The DlmmSDK property value.
206
- */
207
- get DlmmSDK(): CetusDlmmSDK;
208
- /**
209
- * Getter for the AggregatorClient property.
210
- * @returns {AggregatorClient} The AggregatorClient property value.
211
- */
212
- get AggregatorClient(): AggregatorClient;
213
- /**
214
- * Getter for the ZapModule property.
215
- * @returns {ZapModule} The ZapModule property value.
216
- */
217
- get Zap(): ZapModule;
218
- /**
219
- * Static factory method to initialize the SDK
220
- * @param options SDK initialization options
221
- * @param clmm_sdk Optional CLMM SDK instance
222
- * @returns An instance of CetusZapSDK
223
- */
224
- static createSDK(options: BaseSdkOptions, dlmm_sdk?: any): CetusDlmmZapSDK;
225
- /**
226
- * Create a custom SDK instance with the given options
227
- * @param options The options for the SDK
228
- * @returns An instance of CetusBurnSDK
229
- */
230
- static createCustomSDK<T extends BaseSdkOptions>(options: T & SdkOptions, dlmm_sdk?: CetusDlmmSDK): CetusDlmmZapSDK;
231
- }
232
-
233
- /**
234
- * Calculate if there is enough liquidity amount for adding liquidity to a pool
235
- * @param amount_a - Amount of token A
236
- * @param amount_b - Amount of token B
237
- * @param curr_sqrt_price - Current square root price of the pool
238
- * @param lower_tick - Lower tick boundary of the position
239
- * @param upper_tick - Upper tick boundary of the position
240
- * @param slippage - Slippage tolerance for the calculation
241
- * @param fix_amount_a - Whether to fix the amount of token A
242
- * @returns Object containing:
243
- * - is_enough_amount: Whether there is enough amount for the other token
244
- * - use_amount_a: Amount of token A to be used
245
- * - use_amount_b: Amount of token B to be used
246
- * - liquidity: Calculated liquidity amount
247
- * - amount_limit_a: Minimum amount limit for token A
248
- * - amount_limit_b: Minimum amount limit for token B
249
- * - remain_amount: Remaining amount of the non-fixed token
250
- */
251
- declare function calculateLiquidityAmountEnough(amount_a: string, amount_b: string, curr_sqrt_price: string, lower_tick: number, upper_tick: number, slippage: number, fix_amount_a: boolean): {
252
- is_enough_amount: boolean;
253
- use_amount_a: string;
254
- use_amount_b: string;
255
- liquidity: string;
256
- amount_limit_a: string;
257
- amount_limit_b: string;
258
- remain_amount: Decimal;
259
- };
260
- /**
261
- * Calculate the optimal side for adding liquidity based on current price and tick range
262
- * @param amount_a - Amount of token A
263
- * @param amount_b - Amount of token B
264
- * @param curr_sqrt_price - Current square root price of the pool
265
- * @param lower_tick - Lower tick boundary of the position
266
- * @param upper_tick - Upper tick boundary of the position
267
- * @param slippage - Slippage tolerance for the calculation
268
- * @param fix_amount_a - Whether to fix the amount of token A
269
- * @returns Object containing:
270
- * - fix_liquidity_amount_a: Whether to fix token A amount
271
- * - is_enough_amount: Whether there is enough amount for the other token
272
- * - use_amount_a: Amount of token A to be used
273
- * - use_amount_b: Amount of token B to be used
274
- * - liquidity: Calculated liquidity amount
275
- * - amount_limit_a: Minimum amount limit for token A
276
- * - amount_limit_b: Minimum amount limit for token B
277
- * - remain_amount: Remaining amount of the non-fixed token
278
- */
279
- declare function calculateLiquidityAmountSide(amount_a: string, amount_b: string, curr_sqrt_price: string, lower_tick: number, upper_tick: number, slippage: number, fix_amount_a: boolean): {
280
- is_enough_amount: boolean;
281
- use_amount_a: string;
282
- use_amount_b: string;
283
- liquidity: string;
284
- amount_limit_a: string;
285
- amount_limit_b: string;
286
- remain_amount: Decimal;
287
- fix_liquidity_amount_a: boolean;
288
- };
289
- /**
290
- * Calculate the swap amount needed to achieve target ratio
291
- * @param coin_amount - Total coin amount available
292
- * @param fix_amount_a - Whether to fix token A amount (true) or token B amount (false)
293
- * @param current_price - Current price:
294
- * - When fix_amount_a = true: current_price = B/A (price of B in terms of A)
295
- * - When fix_amount_a = false: current_price = A/B (price of A in terms of B)
296
- * @param target_ratio - Target ratio to achieve:
297
- * - When fix_amount_a = true: target_ratio = B/A
298
- * - When fix_amount_a = false: target_ratio = A/B
299
- * @param tolerance - Tolerance for the target ratio (default: 0.01)
300
- * @returns Object containing:
301
- * - swap_amount: Amount to swap
302
- * - final_amount_a: Final amount of token A (remaining A when fix_amount_a=true, obtained A when fix_amount_a=false)
303
- * - final_amount_b: Final amount of token B (obtained B when fix_amount_a=true, remaining B when fix_amount_a=false)
304
- */
305
- declare function calcExactSwapAmount(coin_amount: string, fix_amount_a: boolean, current_price: string, target_ratio: string): {
306
- swap_amount: string;
307
- final_amount_a: string;
308
- final_amount_b: string;
309
- };
310
-
311
- declare const zapMainnet: SdkOptions;
312
-
313
- declare const zapTestnet: SdkOptions;
314
-
315
- declare enum ZapErrorCode {
316
- UnsupportedDepositMode = "UnsupportedDepositMode",
317
- PositionIdUndefined = "PositionIdUndefined",
318
- ParameterError = "ParameterError",
319
- ReachMaxIterations = "ReachMaxIterations",
320
- BestLiquidityIsZero = "BestLiquidityIsZero",
321
- SwapAmountError = "SwapAmountError",
322
- AggregatorError = "AggregatorError"
323
- }
324
- declare class ZapError extends BaseError {
325
- constructor(message: string, errorCode?: ZapErrorCode, details?: Record<string, any>);
326
- static isZapErrorCode(e: any, code: ZapErrorCode): boolean;
327
- }
328
- declare const handleError: (code: ZapErrorCode, error: Error, details?: Record<string, any>) => never;
329
- declare const handleMessageError: (code: ZapErrorCode, message: string, details?: Record<string, any>) => never;
330
-
331
- export { type BaseDepositOptions, type CalculationDepositResult, type CalculationWithdrawAvailableAmountOptions, type CalculationWithdrawOptions, type CalculationWithdrawResult, CetusDlmmZapSDK, type DepositOptions, type OnlyCoinDepositOptions, type SdkOptions, type SwapResult, type WithdrawMode, type WithdrawOptions, ZapError, ZapErrorCode, ZapModule, calcExactSwapAmount, calculateLiquidityAmountEnough, calculateLiquidityAmountSide, CetusDlmmZapSDK as default, defaultSwapSlippage, handleError, handleMessageError, zapMainnet, zapTestnet };
package/dist/index.mjs DELETED
@@ -1,2 +0,0 @@
1
- var Ni=Object.create;var Ai=Object.defineProperty;var Wi=Object.getOwnPropertyDescriptor;var Ui=Object.getOwnPropertyNames;var zi=Object.getPrototypeOf,Hi=Object.prototype.hasOwnProperty;var Vi=(b=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(b,{get:(m,d)=>(typeof require<"u"?require:m)[d]}):b)(function(b){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+b+'" is not supported')});var Ji=(b,m)=>()=>(m||b((m={exports:{}}).exports,m),m.exports);var Qi=(b,m,d,g)=>{if(m&&typeof m=="object"||typeof m=="function")for(let f of Ui(m))!Hi.call(b,f)&&f!==d&&Ai(b,f,{get:()=>m[f],enumerable:!(g=Wi(m,f))||g.enumerable});return b};var Di=(b,m,d)=>(d=b!=null?Ni(zi(b)):{},Qi(m||!b||!b.__esModule?Ai(d,"default",{value:b,enumerable:!0}):d,b));var ni=Ji((Oi,ei)=>{"use strict";(function(b,m){"use strict";function d(h,t){if(!h)throw new Error(t||"Assertion failed")}function g(h,t){h.super_=t;var r=function(){};r.prototype=t.prototype,h.prototype=new r,h.prototype.constructor=h}function f(h,t,r){if(f.isBN(h))return h;this.negative=0,this.words=null,this.length=0,this.red=null,h!==null&&((t==="le"||t==="be")&&(r=t,t=10),this._init(h||0,t||10,r||"be"))}typeof b=="object"?b.exports=f:m.BN=f,f.BN=f,f.wordSize=26;var S;try{typeof window<"u"&&typeof window.Buffer<"u"?S=window.Buffer:S=Vi("buffer").Buffer}catch{}f.isBN=function(t){return t instanceof f?!0:t!==null&&typeof t=="object"&&t.constructor.wordSize===f.wordSize&&Array.isArray(t.words)},f.max=function(t,r){return t.cmp(r)>0?t:r},f.min=function(t,r){return t.cmp(r)<0?t:r},f.prototype._init=function(t,r,e){if(typeof t=="number")return this._initNumber(t,r,e);if(typeof t=="object")return this._initArray(t,r,e);r==="hex"&&(r=16),d(r===(r|0)&&r>=2&&r<=36),t=t.toString().replace(/\s+/g,"");var n=0;t[0]==="-"&&(n++,this.negative=1),n<t.length&&(r===16?this._parseHex(t,n,e):(this._parseBase(t,r,n),e==="le"&&this._initArray(this.toArray(),r,e)))},f.prototype._initNumber=function(t,r,e){t<0&&(this.negative=1,t=-t),t<67108864?(this.words=[t&67108863],this.length=1):t<4503599627370496?(this.words=[t&67108863,t/67108864&67108863],this.length=2):(d(t<9007199254740992),this.words=[t&67108863,t/67108864&67108863,1],this.length=3),e==="le"&&this._initArray(this.toArray(),r,e)},f.prototype._initArray=function(t,r,e){if(d(typeof t.length=="number"),t.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(t.length/3),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var s,l,u=0;if(e==="be")for(n=t.length-1,s=0;n>=0;n-=3)l=t[n]|t[n-1]<<8|t[n-2]<<16,this.words[s]|=l<<u&67108863,this.words[s+1]=l>>>26-u&67108863,u+=24,u>=26&&(u-=26,s++);else if(e==="le")for(n=0,s=0;n<t.length;n+=3)l=t[n]|t[n+1]<<8|t[n+2]<<16,this.words[s]|=l<<u&67108863,this.words[s+1]=l>>>26-u&67108863,u+=24,u>=26&&(u-=26,s++);return this._strip()};function M(h,t){var r=h.charCodeAt(t);if(r>=48&&r<=57)return r-48;if(r>=65&&r<=70)return r-55;if(r>=97&&r<=102)return r-87;d(!1,"Invalid character in "+h)}function _(h,t,r){var e=M(h,r);return r-1>=t&&(e|=M(h,r-1)<<4),e}f.prototype._parseHex=function(t,r,e){this.length=Math.ceil((t.length-r)/6),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var s=0,l=0,u;if(e==="be")for(n=t.length-1;n>=r;n-=2)u=_(t,r,n)<<s,this.words[l]|=u&67108863,s>=18?(s-=18,l+=1,this.words[l]|=u>>>26):s+=8;else{var o=t.length-r;for(n=o%2===0?r+1:r;n<t.length;n+=2)u=_(t,r,n)<<s,this.words[l]|=u&67108863,s>=18?(s-=18,l+=1,this.words[l]|=u>>>26):s+=8}this._strip()};function w(h,t,r,e){for(var n=0,s=0,l=Math.min(h.length,r),u=t;u<l;u++){var o=h.charCodeAt(u)-48;n*=e,o>=49?s=o-49+10:o>=17?s=o-17+10:s=o,d(o>=0&&s<e,"Invalid character"),n+=s}return n}f.prototype._parseBase=function(t,r,e){this.words=[0],this.length=1;for(var n=0,s=1;s<=67108863;s*=r)n++;n--,s=s/r|0;for(var l=t.length-e,u=l%n,o=Math.min(l,l-u)+e,i=0,a=e;a<o;a+=n)i=w(t,a,a+n,r),this.imuln(s),this.words[0]+i<67108864?this.words[0]+=i:this._iaddn(i);if(u!==0){var p=1;for(i=w(t,a,t.length,r),a=0;a<u;a++)p*=r;this.imuln(p),this.words[0]+i<67108864?this.words[0]+=i:this._iaddn(i)}this._strip()},f.prototype.copy=function(t){t.words=new Array(this.length);for(var r=0;r<this.length;r++)t.words[r]=this.words[r];t.length=this.length,t.negative=this.negative,t.red=this.red};function A(h,t){h.words=t.words,h.length=t.length,h.negative=t.negative,h.red=t.red}if(f.prototype._move=function(t){A(t,this)},f.prototype.clone=function(){var t=new f(null);return this.copy(t),t},f.prototype._expand=function(t){for(;this.length<t;)this.words[this.length++]=0;return this},f.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},f.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{f.prototype[Symbol.for("nodejs.util.inspect.custom")]=L}catch{f.prototype.inspect=L}else f.prototype.inspect=L;function L(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var P=["","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"],q=[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],D=[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];f.prototype.toString=function(t,r){t=t||10,r=r|0||1;var e;if(t===16||t==="hex"){e="";for(var n=0,s=0,l=0;l<this.length;l++){var u=this.words[l],o=((u<<n|s)&16777215).toString(16);s=u>>>24-n&16777215,n+=2,n>=26&&(n-=26,l--),s!==0||l!==this.length-1?e=P[6-o.length]+o+e:e=o+e}for(s!==0&&(e=s.toString(16)+e);e.length%r!==0;)e="0"+e;return this.negative!==0&&(e="-"+e),e}if(t===(t|0)&&t>=2&&t<=36){var i=q[t],a=D[t];e="";var p=this.clone();for(p.negative=0;!p.isZero();){var c=p.modrn(a).toString(t);p=p.idivn(a),p.isZero()?e=c+e:e=P[i-c.length]+c+e}for(this.isZero()&&(e="0"+e);e.length%r!==0;)e="0"+e;return this.negative!==0&&(e="-"+e),e}d(!1,"Base should be between 2 and 36")},f.prototype.toNumber=function(){var t=this.words[0];return this.length===2?t+=this.words[1]*67108864:this.length===3&&this.words[2]===1?t+=4503599627370496+this.words[1]*67108864:this.length>2&&d(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-t:t},f.prototype.toJSON=function(){return this.toString(16,2)},S&&(f.prototype.toBuffer=function(t,r){return this.toArrayLike(S,t,r)}),f.prototype.toArray=function(t,r){return this.toArrayLike(Array,t,r)};var C=function(t,r){return t.allocUnsafe?t.allocUnsafe(r):new t(r)};f.prototype.toArrayLike=function(t,r,e){this._strip();var n=this.byteLength(),s=e||Math.max(1,n);d(n<=s,"byte array longer than desired length"),d(s>0,"Requested array length <= 0");var l=C(t,s),u=r==="le"?"LE":"BE";return this["_toArrayLike"+u](l,n),l},f.prototype._toArrayLikeLE=function(t,r){for(var e=0,n=0,s=0,l=0;s<this.length;s++){var u=this.words[s]<<l|n;t[e++]=u&255,e<t.length&&(t[e++]=u>>8&255),e<t.length&&(t[e++]=u>>16&255),l===6?(e<t.length&&(t[e++]=u>>24&255),n=0,l=0):(n=u>>>24,l+=2)}if(e<t.length)for(t[e++]=n;e<t.length;)t[e++]=0},f.prototype._toArrayLikeBE=function(t,r){for(var e=t.length-1,n=0,s=0,l=0;s<this.length;s++){var u=this.words[s]<<l|n;t[e--]=u&255,e>=0&&(t[e--]=u>>8&255),e>=0&&(t[e--]=u>>16&255),l===6?(e>=0&&(t[e--]=u>>24&255),n=0,l=0):(n=u>>>24,l+=2)}if(e>=0)for(t[e--]=n;e>=0;)t[e--]=0},Math.clz32?f.prototype._countBits=function(t){return 32-Math.clz32(t)}:f.prototype._countBits=function(t){var r=t,e=0;return r>=4096&&(e+=13,r>>>=13),r>=64&&(e+=7,r>>>=7),r>=8&&(e+=4,r>>>=4),r>=2&&(e+=2,r>>>=2),e+r},f.prototype._zeroBits=function(t){if(t===0)return 26;var r=t,e=0;return(r&8191)===0&&(e+=13,r>>>=13),(r&127)===0&&(e+=7,r>>>=7),(r&15)===0&&(e+=4,r>>>=4),(r&3)===0&&(e+=2,r>>>=2),(r&1)===0&&e++,e},f.prototype.bitLength=function(){var t=this.words[this.length-1],r=this._countBits(t);return(this.length-1)*26+r};function W(h){for(var t=new Array(h.bitLength()),r=0;r<t.length;r++){var e=r/26|0,n=r%26;t[r]=h.words[e]>>>n&1}return t}f.prototype.zeroBits=function(){if(this.isZero())return 0;for(var t=0,r=0;r<this.length;r++){var e=this._zeroBits(this.words[r]);if(t+=e,e!==26)break}return t},f.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},f.prototype.toTwos=function(t){return this.negative!==0?this.abs().inotn(t).iaddn(1):this.clone()},f.prototype.fromTwos=function(t){return this.testn(t-1)?this.notn(t).iaddn(1).ineg():this.clone()},f.prototype.isNeg=function(){return this.negative!==0},f.prototype.neg=function(){return this.clone().ineg()},f.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},f.prototype.iuor=function(t){for(;this.length<t.length;)this.words[this.length++]=0;for(var r=0;r<t.length;r++)this.words[r]=this.words[r]|t.words[r];return this._strip()},f.prototype.ior=function(t){return d((this.negative|t.negative)===0),this.iuor(t)},f.prototype.or=function(t){return this.length>t.length?this.clone().ior(t):t.clone().ior(this)},f.prototype.uor=function(t){return this.length>t.length?this.clone().iuor(t):t.clone().iuor(this)},f.prototype.iuand=function(t){var r;this.length>t.length?r=t:r=this;for(var e=0;e<r.length;e++)this.words[e]=this.words[e]&t.words[e];return this.length=r.length,this._strip()},f.prototype.iand=function(t){return d((this.negative|t.negative)===0),this.iuand(t)},f.prototype.and=function(t){return this.length>t.length?this.clone().iand(t):t.clone().iand(this)},f.prototype.uand=function(t){return this.length>t.length?this.clone().iuand(t):t.clone().iuand(this)},f.prototype.iuxor=function(t){var r,e;this.length>t.length?(r=this,e=t):(r=t,e=this);for(var n=0;n<e.length;n++)this.words[n]=r.words[n]^e.words[n];if(this!==r)for(;n<r.length;n++)this.words[n]=r.words[n];return this.length=r.length,this._strip()},f.prototype.ixor=function(t){return d((this.negative|t.negative)===0),this.iuxor(t)},f.prototype.xor=function(t){return this.length>t.length?this.clone().ixor(t):t.clone().ixor(this)},f.prototype.uxor=function(t){return this.length>t.length?this.clone().iuxor(t):t.clone().iuxor(this)},f.prototype.inotn=function(t){d(typeof t=="number"&&t>=0);var r=Math.ceil(t/26)|0,e=t%26;this._expand(r),e>0&&r--;for(var n=0;n<r;n++)this.words[n]=~this.words[n]&67108863;return e>0&&(this.words[n]=~this.words[n]&67108863>>26-e),this._strip()},f.prototype.notn=function(t){return this.clone().inotn(t)},f.prototype.setn=function(t,r){d(typeof t=="number"&&t>=0);var e=t/26|0,n=t%26;return this._expand(e+1),r?this.words[e]=this.words[e]|1<<n:this.words[e]=this.words[e]&~(1<<n),this._strip()},f.prototype.iadd=function(t){var r;if(this.negative!==0&&t.negative===0)return this.negative=0,r=this.isub(t),this.negative^=1,this._normSign();if(this.negative===0&&t.negative!==0)return t.negative=0,r=this.isub(t),t.negative=1,r._normSign();var e,n;this.length>t.length?(e=this,n=t):(e=t,n=this);for(var s=0,l=0;l<n.length;l++)r=(e.words[l]|0)+(n.words[l]|0)+s,this.words[l]=r&67108863,s=r>>>26;for(;s!==0&&l<e.length;l++)r=(e.words[l]|0)+s,this.words[l]=r&67108863,s=r>>>26;if(this.length=e.length,s!==0)this.words[this.length]=s,this.length++;else if(e!==this)for(;l<e.length;l++)this.words[l]=e.words[l];return this},f.prototype.add=function(t){var r;return t.negative!==0&&this.negative===0?(t.negative=0,r=this.sub(t),t.negative^=1,r):t.negative===0&&this.negative!==0?(this.negative=0,r=t.sub(this),this.negative=1,r):this.length>t.length?this.clone().iadd(t):t.clone().iadd(this)},f.prototype.isub=function(t){if(t.negative!==0){t.negative=0;var r=this.iadd(t);return t.negative=1,r._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(t),this.negative=1,this._normSign();var e=this.cmp(t);if(e===0)return this.negative=0,this.length=1,this.words[0]=0,this;var n,s;e>0?(n=this,s=t):(n=t,s=this);for(var l=0,u=0;u<s.length;u++)r=(n.words[u]|0)-(s.words[u]|0)+l,l=r>>26,this.words[u]=r&67108863;for(;l!==0&&u<n.length;u++)r=(n.words[u]|0)+l,l=r>>26,this.words[u]=r&67108863;if(l===0&&u<n.length&&n!==this)for(;u<n.length;u++)this.words[u]=n.words[u];return this.length=Math.max(this.length,u),n!==this&&(this.negative=1),this._strip()},f.prototype.sub=function(t){return this.clone().isub(t)};function U(h,t,r){r.negative=t.negative^h.negative;var e=h.length+t.length|0;r.length=e,e=e-1|0;var n=h.words[0]|0,s=t.words[0]|0,l=n*s,u=l&67108863,o=l/67108864|0;r.words[0]=u;for(var i=1;i<e;i++){for(var a=o>>>26,p=o&67108863,c=Math.min(i,t.length-1),v=Math.max(0,i-h.length+1);v<=c;v++){var y=i-v|0;n=h.words[y]|0,s=t.words[v]|0,l=n*s+p,a+=l/67108864|0,p=l&67108863}r.words[i]=p|0,o=a|0}return o!==0?r.words[i]=o|0:r.length--,r._strip()}var Z=function(t,r,e){var n=t.words,s=r.words,l=e.words,u=0,o,i,a,p=n[0]|0,c=p&8191,v=p>>>13,y=n[1]|0,k=y&8191,F=y>>>13,kt=n[2]|0,K=kt&8191,N=kt>>>13,hi=n[3]|0,z=hi&8191,H=hi>>>13,fi=n[4]|0,V=fi&8191,J=fi>>>13,li=n[5]|0,Q=li&8191,X=li>>>13,ui=n[6]|0,Y=ui&8191,$=ui>>>13,di=n[7]|0,G=di&8191,j=di>>>13,mi=n[8]|0,tt=mi&8191,it=mi>>>13,pi=n[9]|0,rt=pi&8191,et=pi>>>13,ci=s[0]|0,nt=ci&8191,ot=ci>>>13,vi=s[1]|0,st=vi&8191,at=vi>>>13,gi=s[2]|0,ht=gi&8191,ft=gi>>>13,_i=s[3]|0,lt=_i&8191,ut=_i>>>13,Mi=s[4]|0,dt=Mi&8191,mt=Mi>>>13,wi=s[5]|0,pt=wi&8191,ct=wi>>>13,yi=s[6]|0,vt=yi&8191,gt=yi>>>13,bi=s[7]|0,_t=bi&8191,Mt=bi>>>13,xi=s[8]|0,wt=xi&8191,yt=xi>>>13,Si=s[9]|0,bt=Si&8191,xt=Si>>>13;e.negative=t.negative^r.negative,e.length=19,o=Math.imul(c,nt),i=Math.imul(c,ot),i=i+Math.imul(v,nt)|0,a=Math.imul(v,ot);var Ft=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Ft>>>26)|0,Ft&=67108863,o=Math.imul(k,nt),i=Math.imul(k,ot),i=i+Math.imul(F,nt)|0,a=Math.imul(F,ot),o=o+Math.imul(c,st)|0,i=i+Math.imul(c,at)|0,i=i+Math.imul(v,st)|0,a=a+Math.imul(v,at)|0;var Kt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Kt>>>26)|0,Kt&=67108863,o=Math.imul(K,nt),i=Math.imul(K,ot),i=i+Math.imul(N,nt)|0,a=Math.imul(N,ot),o=o+Math.imul(k,st)|0,i=i+Math.imul(k,at)|0,i=i+Math.imul(F,st)|0,a=a+Math.imul(F,at)|0,o=o+Math.imul(c,ht)|0,i=i+Math.imul(c,ft)|0,i=i+Math.imul(v,ht)|0,a=a+Math.imul(v,ft)|0;var Zt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Zt>>>26)|0,Zt&=67108863,o=Math.imul(z,nt),i=Math.imul(z,ot),i=i+Math.imul(H,nt)|0,a=Math.imul(H,ot),o=o+Math.imul(K,st)|0,i=i+Math.imul(K,at)|0,i=i+Math.imul(N,st)|0,a=a+Math.imul(N,at)|0,o=o+Math.imul(k,ht)|0,i=i+Math.imul(k,ft)|0,i=i+Math.imul(F,ht)|0,a=a+Math.imul(F,ft)|0,o=o+Math.imul(c,lt)|0,i=i+Math.imul(c,ut)|0,i=i+Math.imul(v,lt)|0,a=a+Math.imul(v,ut)|0;var Nt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Nt>>>26)|0,Nt&=67108863,o=Math.imul(V,nt),i=Math.imul(V,ot),i=i+Math.imul(J,nt)|0,a=Math.imul(J,ot),o=o+Math.imul(z,st)|0,i=i+Math.imul(z,at)|0,i=i+Math.imul(H,st)|0,a=a+Math.imul(H,at)|0,o=o+Math.imul(K,ht)|0,i=i+Math.imul(K,ft)|0,i=i+Math.imul(N,ht)|0,a=a+Math.imul(N,ft)|0,o=o+Math.imul(k,lt)|0,i=i+Math.imul(k,ut)|0,i=i+Math.imul(F,lt)|0,a=a+Math.imul(F,ut)|0,o=o+Math.imul(c,dt)|0,i=i+Math.imul(c,mt)|0,i=i+Math.imul(v,dt)|0,a=a+Math.imul(v,mt)|0;var Wt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Wt>>>26)|0,Wt&=67108863,o=Math.imul(Q,nt),i=Math.imul(Q,ot),i=i+Math.imul(X,nt)|0,a=Math.imul(X,ot),o=o+Math.imul(V,st)|0,i=i+Math.imul(V,at)|0,i=i+Math.imul(J,st)|0,a=a+Math.imul(J,at)|0,o=o+Math.imul(z,ht)|0,i=i+Math.imul(z,ft)|0,i=i+Math.imul(H,ht)|0,a=a+Math.imul(H,ft)|0,o=o+Math.imul(K,lt)|0,i=i+Math.imul(K,ut)|0,i=i+Math.imul(N,lt)|0,a=a+Math.imul(N,ut)|0,o=o+Math.imul(k,dt)|0,i=i+Math.imul(k,mt)|0,i=i+Math.imul(F,dt)|0,a=a+Math.imul(F,mt)|0,o=o+Math.imul(c,pt)|0,i=i+Math.imul(c,ct)|0,i=i+Math.imul(v,pt)|0,a=a+Math.imul(v,ct)|0;var Ut=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Ut>>>26)|0,Ut&=67108863,o=Math.imul(Y,nt),i=Math.imul(Y,ot),i=i+Math.imul($,nt)|0,a=Math.imul($,ot),o=o+Math.imul(Q,st)|0,i=i+Math.imul(Q,at)|0,i=i+Math.imul(X,st)|0,a=a+Math.imul(X,at)|0,o=o+Math.imul(V,ht)|0,i=i+Math.imul(V,ft)|0,i=i+Math.imul(J,ht)|0,a=a+Math.imul(J,ft)|0,o=o+Math.imul(z,lt)|0,i=i+Math.imul(z,ut)|0,i=i+Math.imul(H,lt)|0,a=a+Math.imul(H,ut)|0,o=o+Math.imul(K,dt)|0,i=i+Math.imul(K,mt)|0,i=i+Math.imul(N,dt)|0,a=a+Math.imul(N,mt)|0,o=o+Math.imul(k,pt)|0,i=i+Math.imul(k,ct)|0,i=i+Math.imul(F,pt)|0,a=a+Math.imul(F,ct)|0,o=o+Math.imul(c,vt)|0,i=i+Math.imul(c,gt)|0,i=i+Math.imul(v,vt)|0,a=a+Math.imul(v,gt)|0;var zt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(zt>>>26)|0,zt&=67108863,o=Math.imul(G,nt),i=Math.imul(G,ot),i=i+Math.imul(j,nt)|0,a=Math.imul(j,ot),o=o+Math.imul(Y,st)|0,i=i+Math.imul(Y,at)|0,i=i+Math.imul($,st)|0,a=a+Math.imul($,at)|0,o=o+Math.imul(Q,ht)|0,i=i+Math.imul(Q,ft)|0,i=i+Math.imul(X,ht)|0,a=a+Math.imul(X,ft)|0,o=o+Math.imul(V,lt)|0,i=i+Math.imul(V,ut)|0,i=i+Math.imul(J,lt)|0,a=a+Math.imul(J,ut)|0,o=o+Math.imul(z,dt)|0,i=i+Math.imul(z,mt)|0,i=i+Math.imul(H,dt)|0,a=a+Math.imul(H,mt)|0,o=o+Math.imul(K,pt)|0,i=i+Math.imul(K,ct)|0,i=i+Math.imul(N,pt)|0,a=a+Math.imul(N,ct)|0,o=o+Math.imul(k,vt)|0,i=i+Math.imul(k,gt)|0,i=i+Math.imul(F,vt)|0,a=a+Math.imul(F,gt)|0,o=o+Math.imul(c,_t)|0,i=i+Math.imul(c,Mt)|0,i=i+Math.imul(v,_t)|0,a=a+Math.imul(v,Mt)|0;var Ht=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Ht>>>26)|0,Ht&=67108863,o=Math.imul(tt,nt),i=Math.imul(tt,ot),i=i+Math.imul(it,nt)|0,a=Math.imul(it,ot),o=o+Math.imul(G,st)|0,i=i+Math.imul(G,at)|0,i=i+Math.imul(j,st)|0,a=a+Math.imul(j,at)|0,o=o+Math.imul(Y,ht)|0,i=i+Math.imul(Y,ft)|0,i=i+Math.imul($,ht)|0,a=a+Math.imul($,ft)|0,o=o+Math.imul(Q,lt)|0,i=i+Math.imul(Q,ut)|0,i=i+Math.imul(X,lt)|0,a=a+Math.imul(X,ut)|0,o=o+Math.imul(V,dt)|0,i=i+Math.imul(V,mt)|0,i=i+Math.imul(J,dt)|0,a=a+Math.imul(J,mt)|0,o=o+Math.imul(z,pt)|0,i=i+Math.imul(z,ct)|0,i=i+Math.imul(H,pt)|0,a=a+Math.imul(H,ct)|0,o=o+Math.imul(K,vt)|0,i=i+Math.imul(K,gt)|0,i=i+Math.imul(N,vt)|0,a=a+Math.imul(N,gt)|0,o=o+Math.imul(k,_t)|0,i=i+Math.imul(k,Mt)|0,i=i+Math.imul(F,_t)|0,a=a+Math.imul(F,Mt)|0,o=o+Math.imul(c,wt)|0,i=i+Math.imul(c,yt)|0,i=i+Math.imul(v,wt)|0,a=a+Math.imul(v,yt)|0;var Vt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Vt>>>26)|0,Vt&=67108863,o=Math.imul(rt,nt),i=Math.imul(rt,ot),i=i+Math.imul(et,nt)|0,a=Math.imul(et,ot),o=o+Math.imul(tt,st)|0,i=i+Math.imul(tt,at)|0,i=i+Math.imul(it,st)|0,a=a+Math.imul(it,at)|0,o=o+Math.imul(G,ht)|0,i=i+Math.imul(G,ft)|0,i=i+Math.imul(j,ht)|0,a=a+Math.imul(j,ft)|0,o=o+Math.imul(Y,lt)|0,i=i+Math.imul(Y,ut)|0,i=i+Math.imul($,lt)|0,a=a+Math.imul($,ut)|0,o=o+Math.imul(Q,dt)|0,i=i+Math.imul(Q,mt)|0,i=i+Math.imul(X,dt)|0,a=a+Math.imul(X,mt)|0,o=o+Math.imul(V,pt)|0,i=i+Math.imul(V,ct)|0,i=i+Math.imul(J,pt)|0,a=a+Math.imul(J,ct)|0,o=o+Math.imul(z,vt)|0,i=i+Math.imul(z,gt)|0,i=i+Math.imul(H,vt)|0,a=a+Math.imul(H,gt)|0,o=o+Math.imul(K,_t)|0,i=i+Math.imul(K,Mt)|0,i=i+Math.imul(N,_t)|0,a=a+Math.imul(N,Mt)|0,o=o+Math.imul(k,wt)|0,i=i+Math.imul(k,yt)|0,i=i+Math.imul(F,wt)|0,a=a+Math.imul(F,yt)|0,o=o+Math.imul(c,bt)|0,i=i+Math.imul(c,xt)|0,i=i+Math.imul(v,bt)|0,a=a+Math.imul(v,xt)|0;var Jt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Jt>>>26)|0,Jt&=67108863,o=Math.imul(rt,st),i=Math.imul(rt,at),i=i+Math.imul(et,st)|0,a=Math.imul(et,at),o=o+Math.imul(tt,ht)|0,i=i+Math.imul(tt,ft)|0,i=i+Math.imul(it,ht)|0,a=a+Math.imul(it,ft)|0,o=o+Math.imul(G,lt)|0,i=i+Math.imul(G,ut)|0,i=i+Math.imul(j,lt)|0,a=a+Math.imul(j,ut)|0,o=o+Math.imul(Y,dt)|0,i=i+Math.imul(Y,mt)|0,i=i+Math.imul($,dt)|0,a=a+Math.imul($,mt)|0,o=o+Math.imul(Q,pt)|0,i=i+Math.imul(Q,ct)|0,i=i+Math.imul(X,pt)|0,a=a+Math.imul(X,ct)|0,o=o+Math.imul(V,vt)|0,i=i+Math.imul(V,gt)|0,i=i+Math.imul(J,vt)|0,a=a+Math.imul(J,gt)|0,o=o+Math.imul(z,_t)|0,i=i+Math.imul(z,Mt)|0,i=i+Math.imul(H,_t)|0,a=a+Math.imul(H,Mt)|0,o=o+Math.imul(K,wt)|0,i=i+Math.imul(K,yt)|0,i=i+Math.imul(N,wt)|0,a=a+Math.imul(N,yt)|0,o=o+Math.imul(k,bt)|0,i=i+Math.imul(k,xt)|0,i=i+Math.imul(F,bt)|0,a=a+Math.imul(F,xt)|0;var Qt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Qt>>>26)|0,Qt&=67108863,o=Math.imul(rt,ht),i=Math.imul(rt,ft),i=i+Math.imul(et,ht)|0,a=Math.imul(et,ft),o=o+Math.imul(tt,lt)|0,i=i+Math.imul(tt,ut)|0,i=i+Math.imul(it,lt)|0,a=a+Math.imul(it,ut)|0,o=o+Math.imul(G,dt)|0,i=i+Math.imul(G,mt)|0,i=i+Math.imul(j,dt)|0,a=a+Math.imul(j,mt)|0,o=o+Math.imul(Y,pt)|0,i=i+Math.imul(Y,ct)|0,i=i+Math.imul($,pt)|0,a=a+Math.imul($,ct)|0,o=o+Math.imul(Q,vt)|0,i=i+Math.imul(Q,gt)|0,i=i+Math.imul(X,vt)|0,a=a+Math.imul(X,gt)|0,o=o+Math.imul(V,_t)|0,i=i+Math.imul(V,Mt)|0,i=i+Math.imul(J,_t)|0,a=a+Math.imul(J,Mt)|0,o=o+Math.imul(z,wt)|0,i=i+Math.imul(z,yt)|0,i=i+Math.imul(H,wt)|0,a=a+Math.imul(H,yt)|0,o=o+Math.imul(K,bt)|0,i=i+Math.imul(K,xt)|0,i=i+Math.imul(N,bt)|0,a=a+Math.imul(N,xt)|0;var Xt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Xt>>>26)|0,Xt&=67108863,o=Math.imul(rt,lt),i=Math.imul(rt,ut),i=i+Math.imul(et,lt)|0,a=Math.imul(et,ut),o=o+Math.imul(tt,dt)|0,i=i+Math.imul(tt,mt)|0,i=i+Math.imul(it,dt)|0,a=a+Math.imul(it,mt)|0,o=o+Math.imul(G,pt)|0,i=i+Math.imul(G,ct)|0,i=i+Math.imul(j,pt)|0,a=a+Math.imul(j,ct)|0,o=o+Math.imul(Y,vt)|0,i=i+Math.imul(Y,gt)|0,i=i+Math.imul($,vt)|0,a=a+Math.imul($,gt)|0,o=o+Math.imul(Q,_t)|0,i=i+Math.imul(Q,Mt)|0,i=i+Math.imul(X,_t)|0,a=a+Math.imul(X,Mt)|0,o=o+Math.imul(V,wt)|0,i=i+Math.imul(V,yt)|0,i=i+Math.imul(J,wt)|0,a=a+Math.imul(J,yt)|0,o=o+Math.imul(z,bt)|0,i=i+Math.imul(z,xt)|0,i=i+Math.imul(H,bt)|0,a=a+Math.imul(H,xt)|0;var Yt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Yt>>>26)|0,Yt&=67108863,o=Math.imul(rt,dt),i=Math.imul(rt,mt),i=i+Math.imul(et,dt)|0,a=Math.imul(et,mt),o=o+Math.imul(tt,pt)|0,i=i+Math.imul(tt,ct)|0,i=i+Math.imul(it,pt)|0,a=a+Math.imul(it,ct)|0,o=o+Math.imul(G,vt)|0,i=i+Math.imul(G,gt)|0,i=i+Math.imul(j,vt)|0,a=a+Math.imul(j,gt)|0,o=o+Math.imul(Y,_t)|0,i=i+Math.imul(Y,Mt)|0,i=i+Math.imul($,_t)|0,a=a+Math.imul($,Mt)|0,o=o+Math.imul(Q,wt)|0,i=i+Math.imul(Q,yt)|0,i=i+Math.imul(X,wt)|0,a=a+Math.imul(X,yt)|0,o=o+Math.imul(V,bt)|0,i=i+Math.imul(V,xt)|0,i=i+Math.imul(J,bt)|0,a=a+Math.imul(J,xt)|0;var $t=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+($t>>>26)|0,$t&=67108863,o=Math.imul(rt,pt),i=Math.imul(rt,ct),i=i+Math.imul(et,pt)|0,a=Math.imul(et,ct),o=o+Math.imul(tt,vt)|0,i=i+Math.imul(tt,gt)|0,i=i+Math.imul(it,vt)|0,a=a+Math.imul(it,gt)|0,o=o+Math.imul(G,_t)|0,i=i+Math.imul(G,Mt)|0,i=i+Math.imul(j,_t)|0,a=a+Math.imul(j,Mt)|0,o=o+Math.imul(Y,wt)|0,i=i+Math.imul(Y,yt)|0,i=i+Math.imul($,wt)|0,a=a+Math.imul($,yt)|0,o=o+Math.imul(Q,bt)|0,i=i+Math.imul(Q,xt)|0,i=i+Math.imul(X,bt)|0,a=a+Math.imul(X,xt)|0;var Gt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Gt>>>26)|0,Gt&=67108863,o=Math.imul(rt,vt),i=Math.imul(rt,gt),i=i+Math.imul(et,vt)|0,a=Math.imul(et,gt),o=o+Math.imul(tt,_t)|0,i=i+Math.imul(tt,Mt)|0,i=i+Math.imul(it,_t)|0,a=a+Math.imul(it,Mt)|0,o=o+Math.imul(G,wt)|0,i=i+Math.imul(G,yt)|0,i=i+Math.imul(j,wt)|0,a=a+Math.imul(j,yt)|0,o=o+Math.imul(Y,bt)|0,i=i+Math.imul(Y,xt)|0,i=i+Math.imul($,bt)|0,a=a+Math.imul($,xt)|0;var jt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(jt>>>26)|0,jt&=67108863,o=Math.imul(rt,_t),i=Math.imul(rt,Mt),i=i+Math.imul(et,_t)|0,a=Math.imul(et,Mt),o=o+Math.imul(tt,wt)|0,i=i+Math.imul(tt,yt)|0,i=i+Math.imul(it,wt)|0,a=a+Math.imul(it,yt)|0,o=o+Math.imul(G,bt)|0,i=i+Math.imul(G,xt)|0,i=i+Math.imul(j,bt)|0,a=a+Math.imul(j,xt)|0;var ti=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(ti>>>26)|0,ti&=67108863,o=Math.imul(rt,wt),i=Math.imul(rt,yt),i=i+Math.imul(et,wt)|0,a=Math.imul(et,yt),o=o+Math.imul(tt,bt)|0,i=i+Math.imul(tt,xt)|0,i=i+Math.imul(it,bt)|0,a=a+Math.imul(it,xt)|0;var ii=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(ii>>>26)|0,ii&=67108863,o=Math.imul(rt,bt),i=Math.imul(rt,xt),i=i+Math.imul(et,bt)|0,a=Math.imul(et,xt);var ri=(u+o|0)+((i&8191)<<13)|0;return u=(a+(i>>>13)|0)+(ri>>>26)|0,ri&=67108863,l[0]=Ft,l[1]=Kt,l[2]=Zt,l[3]=Nt,l[4]=Wt,l[5]=Ut,l[6]=zt,l[7]=Ht,l[8]=Vt,l[9]=Jt,l[10]=Qt,l[11]=Xt,l[12]=Yt,l[13]=$t,l[14]=Gt,l[15]=jt,l[16]=ti,l[17]=ii,l[18]=ri,u!==0&&(l[19]=u,e.length++),e};Math.imul||(Z=U);function E(h,t,r){r.negative=t.negative^h.negative,r.length=h.length+t.length;for(var e=0,n=0,s=0;s<r.length-1;s++){var l=n;n=0;for(var u=e&67108863,o=Math.min(s,t.length-1),i=Math.max(0,s-h.length+1);i<=o;i++){var a=s-i,p=h.words[a]|0,c=t.words[i]|0,v=p*c,y=v&67108863;l=l+(v/67108864|0)|0,y=y+u|0,u=y&67108863,l=l+(y>>>26)|0,n+=l>>>26,l&=67108863}r.words[s]=u,e=l,l=n}return e!==0?r.words[s]=e:r.length--,r._strip()}function R(h,t,r){return E(h,t,r)}f.prototype.mulTo=function(t,r){var e,n=this.length+t.length;return this.length===10&&t.length===10?e=Z(this,t,r):n<63?e=U(this,t,r):n<1024?e=E(this,t,r):e=R(this,t,r),e};function B(h,t){this.x=h,this.y=t}B.prototype.makeRBT=function(t){for(var r=new Array(t),e=f.prototype._countBits(t)-1,n=0;n<t;n++)r[n]=this.revBin(n,e,t);return r},B.prototype.revBin=function(t,r,e){if(t===0||t===e-1)return t;for(var n=0,s=0;s<r;s++)n|=(t&1)<<r-s-1,t>>=1;return n},B.prototype.permute=function(t,r,e,n,s,l){for(var u=0;u<l;u++)n[u]=r[t[u]],s[u]=e[t[u]]},B.prototype.transform=function(t,r,e,n,s,l){this.permute(l,t,r,e,n,s);for(var u=1;u<s;u<<=1)for(var o=u<<1,i=Math.cos(2*Math.PI/o),a=Math.sin(2*Math.PI/o),p=0;p<s;p+=o)for(var c=i,v=a,y=0;y<u;y++){var k=e[p+y],F=n[p+y],kt=e[p+y+u],K=n[p+y+u],N=c*kt-v*K;K=c*K+v*kt,kt=N,e[p+y]=k+kt,n[p+y]=F+K,e[p+y+u]=k-kt,n[p+y+u]=F-K,y!==o&&(N=i*c-a*v,v=i*v+a*c,c=N)}},B.prototype.guessLen13b=function(t,r){var e=Math.max(r,t)|1,n=e&1,s=0;for(e=e/2|0;e;e=e>>>1)s++;return 1<<s+1+n},B.prototype.conjugate=function(t,r,e){if(!(e<=1))for(var n=0;n<e/2;n++){var s=t[n];t[n]=t[e-n-1],t[e-n-1]=s,s=r[n],r[n]=-r[e-n-1],r[e-n-1]=-s}},B.prototype.normalize13b=function(t,r){for(var e=0,n=0;n<r/2;n++){var s=Math.round(t[2*n+1]/r)*8192+Math.round(t[2*n]/r)+e;t[n]=s&67108863,s<67108864?e=0:e=s/67108864|0}return t},B.prototype.convert13b=function(t,r,e,n){for(var s=0,l=0;l<r;l++)s=s+(t[l]|0),e[2*l]=s&8191,s=s>>>13,e[2*l+1]=s&8191,s=s>>>13;for(l=2*r;l<n;++l)e[l]=0;d(s===0),d((s&-8192)===0)},B.prototype.stub=function(t){for(var r=new Array(t),e=0;e<t;e++)r[e]=0;return r},B.prototype.mulp=function(t,r,e){var n=2*this.guessLen13b(t.length,r.length),s=this.makeRBT(n),l=this.stub(n),u=new Array(n),o=new Array(n),i=new Array(n),a=new Array(n),p=new Array(n),c=new Array(n),v=e.words;v.length=n,this.convert13b(t.words,t.length,u,n),this.convert13b(r.words,r.length,a,n),this.transform(u,l,o,i,n,s),this.transform(a,l,p,c,n,s);for(var y=0;y<n;y++){var k=o[y]*p[y]-i[y]*c[y];i[y]=o[y]*c[y]+i[y]*p[y],o[y]=k}return this.conjugate(o,i,n),this.transform(o,i,v,l,n,s),this.conjugate(v,l,n),this.normalize13b(v,n),e.negative=t.negative^r.negative,e.length=t.length+r.length,e._strip()},f.prototype.mul=function(t){var r=new f(null);return r.words=new Array(this.length+t.length),this.mulTo(t,r)},f.prototype.mulf=function(t){var r=new f(null);return r.words=new Array(this.length+t.length),R(this,t,r)},f.prototype.imul=function(t){return this.clone().mulTo(t,this)},f.prototype.imuln=function(t){var r=t<0;r&&(t=-t),d(typeof t=="number"),d(t<67108864);for(var e=0,n=0;n<this.length;n++){var s=(this.words[n]|0)*t,l=(s&67108863)+(e&67108863);e>>=26,e+=s/67108864|0,e+=l>>>26,this.words[n]=l&67108863}return e!==0&&(this.words[n]=e,this.length++),this.length=t===0?1:this.length,r?this.ineg():this},f.prototype.muln=function(t){return this.clone().imuln(t)},f.prototype.sqr=function(){return this.mul(this)},f.prototype.isqr=function(){return this.imul(this.clone())},f.prototype.pow=function(t){var r=W(t);if(r.length===0)return new f(1);for(var e=this,n=0;n<r.length&&r[n]===0;n++,e=e.sqr());if(++n<r.length)for(var s=e.sqr();n<r.length;n++,s=s.sqr())r[n]!==0&&(e=e.mul(s));return e},f.prototype.iushln=function(t){d(typeof t=="number"&&t>=0);var r=t%26,e=(t-r)/26,n=67108863>>>26-r<<26-r,s;if(r!==0){var l=0;for(s=0;s<this.length;s++){var u=this.words[s]&n,o=(this.words[s]|0)-u<<r;this.words[s]=o|l,l=u>>>26-r}l&&(this.words[s]=l,this.length++)}if(e!==0){for(s=this.length-1;s>=0;s--)this.words[s+e]=this.words[s];for(s=0;s<e;s++)this.words[s]=0;this.length+=e}return this._strip()},f.prototype.ishln=function(t){return d(this.negative===0),this.iushln(t)},f.prototype.iushrn=function(t,r,e){d(typeof t=="number"&&t>=0);var n;r?n=(r-r%26)/26:n=0;var s=t%26,l=Math.min((t-s)/26,this.length),u=67108863^67108863>>>s<<s,o=e;if(n-=l,n=Math.max(0,n),o){for(var i=0;i<l;i++)o.words[i]=this.words[i];o.length=l}if(l!==0)if(this.length>l)for(this.length-=l,i=0;i<this.length;i++)this.words[i]=this.words[i+l];else this.words[0]=0,this.length=1;var a=0;for(i=this.length-1;i>=0&&(a!==0||i>=n);i--){var p=this.words[i]|0;this.words[i]=a<<26-s|p>>>s,a=p&u}return o&&a!==0&&(o.words[o.length++]=a),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},f.prototype.ishrn=function(t,r,e){return d(this.negative===0),this.iushrn(t,r,e)},f.prototype.shln=function(t){return this.clone().ishln(t)},f.prototype.ushln=function(t){return this.clone().iushln(t)},f.prototype.shrn=function(t){return this.clone().ishrn(t)},f.prototype.ushrn=function(t){return this.clone().iushrn(t)},f.prototype.testn=function(t){d(typeof t=="number"&&t>=0);var r=t%26,e=(t-r)/26,n=1<<r;if(this.length<=e)return!1;var s=this.words[e];return!!(s&n)},f.prototype.imaskn=function(t){d(typeof t=="number"&&t>=0);var r=t%26,e=(t-r)/26;if(d(this.negative===0,"imaskn works only with positive numbers"),this.length<=e)return this;if(r!==0&&e++,this.length=Math.min(e,this.length),r!==0){var n=67108863^67108863>>>r<<r;this.words[this.length-1]&=n}return this._strip()},f.prototype.maskn=function(t){return this.clone().imaskn(t)},f.prototype.iaddn=function(t){return d(typeof t=="number"),d(t<67108864),t<0?this.isubn(-t):this.negative!==0?this.length===1&&(this.words[0]|0)<=t?(this.words[0]=t-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(t),this.negative=1,this):this._iaddn(t)},f.prototype._iaddn=function(t){this.words[0]+=t;for(var r=0;r<this.length&&this.words[r]>=67108864;r++)this.words[r]-=67108864,r===this.length-1?this.words[r+1]=1:this.words[r+1]++;return this.length=Math.max(this.length,r+1),this},f.prototype.isubn=function(t){if(d(typeof t=="number"),d(t<67108864),t<0)return this.iaddn(-t);if(this.negative!==0)return this.negative=0,this.iaddn(t),this.negative=1,this;if(this.words[0]-=t,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var r=0;r<this.length&&this.words[r]<0;r++)this.words[r]+=67108864,this.words[r+1]-=1;return this._strip()},f.prototype.addn=function(t){return this.clone().iaddn(t)},f.prototype.subn=function(t){return this.clone().isubn(t)},f.prototype.iabs=function(){return this.negative=0,this},f.prototype.abs=function(){return this.clone().iabs()},f.prototype._ishlnsubmul=function(t,r,e){var n=t.length+e,s;this._expand(n);var l,u=0;for(s=0;s<t.length;s++){l=(this.words[s+e]|0)+u;var o=(t.words[s]|0)*r;l-=o&67108863,u=(l>>26)-(o/67108864|0),this.words[s+e]=l&67108863}for(;s<this.length-e;s++)l=(this.words[s+e]|0)+u,u=l>>26,this.words[s+e]=l&67108863;if(u===0)return this._strip();for(d(u===-1),u=0,s=0;s<this.length;s++)l=-(this.words[s]|0)+u,u=l>>26,this.words[s]=l&67108863;return this.negative=1,this._strip()},f.prototype._wordDiv=function(t,r){var e=this.length-t.length,n=this.clone(),s=t,l=s.words[s.length-1]|0,u=this._countBits(l);e=26-u,e!==0&&(s=s.ushln(e),n.iushln(e),l=s.words[s.length-1]|0);var o=n.length-s.length,i;if(r!=="mod"){i=new f(null),i.length=o+1,i.words=new Array(i.length);for(var a=0;a<i.length;a++)i.words[a]=0}var p=n.clone()._ishlnsubmul(s,1,o);p.negative===0&&(n=p,i&&(i.words[o]=1));for(var c=o-1;c>=0;c--){var v=(n.words[s.length+c]|0)*67108864+(n.words[s.length+c-1]|0);for(v=Math.min(v/l|0,67108863),n._ishlnsubmul(s,v,c);n.negative!==0;)v--,n.negative=0,n._ishlnsubmul(s,1,c),n.isZero()||(n.negative^=1);i&&(i.words[c]=v)}return i&&i._strip(),n._strip(),r!=="div"&&e!==0&&n.iushrn(e),{div:i||null,mod:n}},f.prototype.divmod=function(t,r,e){if(d(!t.isZero()),this.isZero())return{div:new f(0),mod:new f(0)};var n,s,l;return this.negative!==0&&t.negative===0?(l=this.neg().divmod(t,r),r!=="mod"&&(n=l.div.neg()),r!=="div"&&(s=l.mod.neg(),e&&s.negative!==0&&s.iadd(t)),{div:n,mod:s}):this.negative===0&&t.negative!==0?(l=this.divmod(t.neg(),r),r!=="mod"&&(n=l.div.neg()),{div:n,mod:l.mod}):(this.negative&t.negative)!==0?(l=this.neg().divmod(t.neg(),r),r!=="div"&&(s=l.mod.neg(),e&&s.negative!==0&&s.isub(t)),{div:l.div,mod:s}):t.length>this.length||this.cmp(t)<0?{div:new f(0),mod:this}:t.length===1?r==="div"?{div:this.divn(t.words[0]),mod:null}:r==="mod"?{div:null,mod:new f(this.modrn(t.words[0]))}:{div:this.divn(t.words[0]),mod:new f(this.modrn(t.words[0]))}:this._wordDiv(t,r)},f.prototype.div=function(t){return this.divmod(t,"div",!1).div},f.prototype.mod=function(t){return this.divmod(t,"mod",!1).mod},f.prototype.umod=function(t){return this.divmod(t,"mod",!0).mod},f.prototype.divRound=function(t){var r=this.divmod(t);if(r.mod.isZero())return r.div;var e=r.div.negative!==0?r.mod.isub(t):r.mod,n=t.ushrn(1),s=t.andln(1),l=e.cmp(n);return l<0||s===1&&l===0?r.div:r.div.negative!==0?r.div.isubn(1):r.div.iaddn(1)},f.prototype.modrn=function(t){var r=t<0;r&&(t=-t),d(t<=67108863);for(var e=(1<<26)%t,n=0,s=this.length-1;s>=0;s--)n=(e*n+(this.words[s]|0))%t;return r?-n:n},f.prototype.modn=function(t){return this.modrn(t)},f.prototype.idivn=function(t){var r=t<0;r&&(t=-t),d(t<=67108863);for(var e=0,n=this.length-1;n>=0;n--){var s=(this.words[n]|0)+e*67108864;this.words[n]=s/t|0,e=s%t}return this._strip(),r?this.ineg():this},f.prototype.divn=function(t){return this.clone().idivn(t)},f.prototype.egcd=function(t){d(t.negative===0),d(!t.isZero());var r=this,e=t.clone();r.negative!==0?r=r.umod(t):r=r.clone();for(var n=new f(1),s=new f(0),l=new f(0),u=new f(1),o=0;r.isEven()&&e.isEven();)r.iushrn(1),e.iushrn(1),++o;for(var i=e.clone(),a=r.clone();!r.isZero();){for(var p=0,c=1;(r.words[0]&c)===0&&p<26;++p,c<<=1);if(p>0)for(r.iushrn(p);p-- >0;)(n.isOdd()||s.isOdd())&&(n.iadd(i),s.isub(a)),n.iushrn(1),s.iushrn(1);for(var v=0,y=1;(e.words[0]&y)===0&&v<26;++v,y<<=1);if(v>0)for(e.iushrn(v);v-- >0;)(l.isOdd()||u.isOdd())&&(l.iadd(i),u.isub(a)),l.iushrn(1),u.iushrn(1);r.cmp(e)>=0?(r.isub(e),n.isub(l),s.isub(u)):(e.isub(r),l.isub(n),u.isub(s))}return{a:l,b:u,gcd:e.iushln(o)}},f.prototype._invmp=function(t){d(t.negative===0),d(!t.isZero());var r=this,e=t.clone();r.negative!==0?r=r.umod(t):r=r.clone();for(var n=new f(1),s=new f(0),l=e.clone();r.cmpn(1)>0&&e.cmpn(1)>0;){for(var u=0,o=1;(r.words[0]&o)===0&&u<26;++u,o<<=1);if(u>0)for(r.iushrn(u);u-- >0;)n.isOdd()&&n.iadd(l),n.iushrn(1);for(var i=0,a=1;(e.words[0]&a)===0&&i<26;++i,a<<=1);if(i>0)for(e.iushrn(i);i-- >0;)s.isOdd()&&s.iadd(l),s.iushrn(1);r.cmp(e)>=0?(r.isub(e),n.isub(s)):(e.isub(r),s.isub(n))}var p;return r.cmpn(1)===0?p=n:p=s,p.cmpn(0)<0&&p.iadd(t),p},f.prototype.gcd=function(t){if(this.isZero())return t.abs();if(t.isZero())return this.abs();var r=this.clone(),e=t.clone();r.negative=0,e.negative=0;for(var n=0;r.isEven()&&e.isEven();n++)r.iushrn(1),e.iushrn(1);do{for(;r.isEven();)r.iushrn(1);for(;e.isEven();)e.iushrn(1);var s=r.cmp(e);if(s<0){var l=r;r=e,e=l}else if(s===0||e.cmpn(1)===0)break;r.isub(e)}while(!0);return e.iushln(n)},f.prototype.invm=function(t){return this.egcd(t).a.umod(t)},f.prototype.isEven=function(){return(this.words[0]&1)===0},f.prototype.isOdd=function(){return(this.words[0]&1)===1},f.prototype.andln=function(t){return this.words[0]&t},f.prototype.bincn=function(t){d(typeof t=="number");var r=t%26,e=(t-r)/26,n=1<<r;if(this.length<=e)return this._expand(e+1),this.words[e]|=n,this;for(var s=n,l=e;s!==0&&l<this.length;l++){var u=this.words[l]|0;u+=s,s=u>>>26,u&=67108863,this.words[l]=u}return s!==0&&(this.words[l]=s,this.length++),this},f.prototype.isZero=function(){return this.length===1&&this.words[0]===0},f.prototype.cmpn=function(t){var r=t<0;if(this.negative!==0&&!r)return-1;if(this.negative===0&&r)return 1;this._strip();var e;if(this.length>1)e=1;else{r&&(t=-t),d(t<=67108863,"Number is too big");var n=this.words[0]|0;e=n===t?0:n<t?-1:1}return this.negative!==0?-e|0:e},f.prototype.cmp=function(t){if(this.negative!==0&&t.negative===0)return-1;if(this.negative===0&&t.negative!==0)return 1;var r=this.ucmp(t);return this.negative!==0?-r|0:r},f.prototype.ucmp=function(t){if(this.length>t.length)return 1;if(this.length<t.length)return-1;for(var r=0,e=this.length-1;e>=0;e--){var n=this.words[e]|0,s=t.words[e]|0;if(n!==s){n<s?r=-1:n>s&&(r=1);break}}return r},f.prototype.gtn=function(t){return this.cmpn(t)===1},f.prototype.gt=function(t){return this.cmp(t)===1},f.prototype.gten=function(t){return this.cmpn(t)>=0},f.prototype.gte=function(t){return this.cmp(t)>=0},f.prototype.ltn=function(t){return this.cmpn(t)===-1},f.prototype.lt=function(t){return this.cmp(t)===-1},f.prototype.lten=function(t){return this.cmpn(t)<=0},f.prototype.lte=function(t){return this.cmp(t)<=0},f.prototype.eqn=function(t){return this.cmpn(t)===0},f.prototype.eq=function(t){return this.cmp(t)===0},f.red=function(t){return new T(t)},f.prototype.toRed=function(t){return d(!this.red,"Already a number in reduction context"),d(this.negative===0,"red works only with positives"),t.convertTo(this)._forceRed(t)},f.prototype.fromRed=function(){return d(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},f.prototype._forceRed=function(t){return this.red=t,this},f.prototype.forceRed=function(t){return d(!this.red,"Already a number in reduction context"),this._forceRed(t)},f.prototype.redAdd=function(t){return d(this.red,"redAdd works only with red numbers"),this.red.add(this,t)},f.prototype.redIAdd=function(t){return d(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,t)},f.prototype.redSub=function(t){return d(this.red,"redSub works only with red numbers"),this.red.sub(this,t)},f.prototype.redISub=function(t){return d(this.red,"redISub works only with red numbers"),this.red.isub(this,t)},f.prototype.redShl=function(t){return d(this.red,"redShl works only with red numbers"),this.red.shl(this,t)},f.prototype.redMul=function(t){return d(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.mul(this,t)},f.prototype.redIMul=function(t){return d(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.imul(this,t)},f.prototype.redSqr=function(){return d(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},f.prototype.redISqr=function(){return d(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},f.prototype.redSqrt=function(){return d(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},f.prototype.redInvm=function(){return d(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},f.prototype.redNeg=function(){return d(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},f.prototype.redPow=function(t){return d(this.red&&!t.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,t)};var I={k256:null,p224:null,p192:null,p25519:null};function O(h,t){this.name=h,this.p=new f(t,16),this.n=this.p.bitLength(),this.k=new f(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}O.prototype._tmp=function(){var t=new f(null);return t.words=new Array(Math.ceil(this.n/13)),t},O.prototype.ireduce=function(t){var r=t,e;do this.split(r,this.tmp),r=this.imulK(r),r=r.iadd(this.tmp),e=r.bitLength();while(e>this.n);var n=e<this.n?-1:r.ucmp(this.p);return n===0?(r.words[0]=0,r.length=1):n>0?r.isub(this.p):r.strip!==void 0?r.strip():r._strip(),r},O.prototype.split=function(t,r){t.iushrn(this.n,0,r)},O.prototype.imulK=function(t){return t.imul(this.k)};function At(){O.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}g(At,O),At.prototype.split=function(t,r){for(var e=4194303,n=Math.min(t.length,9),s=0;s<n;s++)r.words[s]=t.words[s];if(r.length=n,t.length<=9){t.words[0]=0,t.length=1;return}var l=t.words[9];for(r.words[r.length++]=l&e,s=10;s<t.length;s++){var u=t.words[s]|0;t.words[s-10]=(u&e)<<4|l>>>22,l=u}l>>>=22,t.words[s-10]=l,l===0&&t.length>10?t.length-=10:t.length-=9},At.prototype.imulK=function(t){t.words[t.length]=0,t.words[t.length+1]=0,t.length+=2;for(var r=0,e=0;e<t.length;e++){var n=t.words[e]|0;r+=n*977,t.words[e]=r&67108863,r=n*64+(r/67108864|0)}return t.words[t.length-1]===0&&(t.length--,t.words[t.length-1]===0&&t.length--),t};function St(){O.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}g(St,O);function Dt(){O.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}g(Dt,O);function qt(){O.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}g(qt,O),qt.prototype.imulK=function(t){for(var r=0,e=0;e<t.length;e++){var n=(t.words[e]|0)*19+r,s=n&67108863;n>>>=26,t.words[e]=s,r=n}return r!==0&&(t.words[t.length++]=r),t},f._prime=function(t){if(I[t])return I[t];var r;if(t==="k256")r=new At;else if(t==="p224")r=new St;else if(t==="p192")r=new Dt;else if(t==="p25519")r=new qt;else throw new Error("Unknown prime "+t);return I[t]=r,r};function T(h){if(typeof h=="string"){var t=f._prime(h);this.m=t.p,this.prime=t}else d(h.gtn(1),"modulus must be greater than 1"),this.m=h,this.prime=null}T.prototype._verify1=function(t){d(t.negative===0,"red works only with positives"),d(t.red,"red works only with red numbers")},T.prototype._verify2=function(t,r){d((t.negative|r.negative)===0,"red works only with positives"),d(t.red&&t.red===r.red,"red works only with red numbers")},T.prototype.imod=function(t){return this.prime?this.prime.ireduce(t)._forceRed(this):(A(t,t.umod(this.m)._forceRed(this)),t)},T.prototype.neg=function(t){return t.isZero()?t.clone():this.m.sub(t)._forceRed(this)},T.prototype.add=function(t,r){this._verify2(t,r);var e=t.add(r);return e.cmp(this.m)>=0&&e.isub(this.m),e._forceRed(this)},T.prototype.iadd=function(t,r){this._verify2(t,r);var e=t.iadd(r);return e.cmp(this.m)>=0&&e.isub(this.m),e},T.prototype.sub=function(t,r){this._verify2(t,r);var e=t.sub(r);return e.cmpn(0)<0&&e.iadd(this.m),e._forceRed(this)},T.prototype.isub=function(t,r){this._verify2(t,r);var e=t.isub(r);return e.cmpn(0)<0&&e.iadd(this.m),e},T.prototype.shl=function(t,r){return this._verify1(t),this.imod(t.ushln(r))},T.prototype.imul=function(t,r){return this._verify2(t,r),this.imod(t.imul(r))},T.prototype.mul=function(t,r){return this._verify2(t,r),this.imod(t.mul(r))},T.prototype.isqr=function(t){return this.imul(t,t.clone())},T.prototype.sqr=function(t){return this.mul(t,t)},T.prototype.sqrt=function(t){if(t.isZero())return t.clone();var r=this.m.andln(3);if(d(r%2===1),r===3){var e=this.m.add(new f(1)).iushrn(2);return this.pow(t,e)}for(var n=this.m.subn(1),s=0;!n.isZero()&&n.andln(1)===0;)s++,n.iushrn(1);d(!n.isZero());var l=new f(1).toRed(this),u=l.redNeg(),o=this.m.subn(1).iushrn(1),i=this.m.bitLength();for(i=new f(2*i*i).toRed(this);this.pow(i,o).cmp(u)!==0;)i.redIAdd(u);for(var a=this.pow(i,n),p=this.pow(t,n.addn(1).iushrn(1)),c=this.pow(t,n),v=s;c.cmp(l)!==0;){for(var y=c,k=0;y.cmp(l)!==0;k++)y=y.redSqr();d(k<v);var F=this.pow(a,new f(1).iushln(v-k-1));p=p.redMul(F),a=F.redSqr(),c=c.redMul(a),v=k}return p},T.prototype.invm=function(t){var r=t._invmp(this.m);return r.negative!==0?(r.negative=0,this.imod(r).redNeg()):this.imod(r)},T.prototype.pow=function(t,r){if(r.isZero())return new f(1).toRed(this);if(r.cmpn(1)===0)return t.clone();var e=4,n=new Array(1<<e);n[0]=new f(1).toRed(this),n[1]=t;for(var s=2;s<n.length;s++)n[s]=this.mul(n[s-1],t);var l=n[0],u=0,o=0,i=r.bitLength()%26;for(i===0&&(i=26),s=r.length-1;s>=0;s--){for(var a=r.words[s],p=i-1;p>=0;p--){var c=a>>p&1;if(l!==n[0]&&(l=this.sqr(l)),c===0&&u===0){o=0;continue}u<<=1,u|=c,o++,!(o!==e&&(s!==0||p!==0))&&(l=this.mul(l,n[u]),o=0,u=0)}i=26}return l},T.prototype.convertTo=function(t){var r=t.umod(this.m);return r===t?r.clone():r},T.prototype.convertFrom=function(t){var r=t.clone();return r.red=null,r},f.mont=function(t){return new Rt(t)};function Rt(h){T.call(this,h),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new f(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)}g(Rt,T),Rt.prototype.convertTo=function(t){return this.imod(t.ushln(this.shift))},Rt.prototype.convertFrom=function(t){var r=this.imod(t.mul(this.rinv));return r.red=null,r},Rt.prototype.imul=function(t,r){if(t.isZero()||r.isZero())return t.words[0]=0,t.length=1,t;var e=t.imul(r),n=e.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),s=e.isub(n).iushrn(this.shift),l=s;return s.cmp(this.m)>=0?l=s.isub(this.m):s.cmpn(0)<0&&(l=s.iadd(this.m)),l._forceRed(this)},Rt.prototype.mul=function(t,r){if(t.isZero()||r.isZero())return new f(0)._forceRed(this);var e=t.mul(r),n=e.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),s=e.isub(n).iushrn(this.shift),l=s;return s.cmp(this.m)>=0?l=s.isub(this.m):s.cmpn(0)<0&&(l=s.iadd(this.m)),l._forceRed(this)},Rt.prototype.invm=function(t){var r=this.imod(t._invmp(this.m).mul(this.r2));return r._forceRed(this)}})(typeof ei>"u"||ei,Oi)});import{AggregatorClient as Fi,Env as Pt}from"@cetusprotocol/aggregator-sdk";import{SuiClient as Ki}from"@mysten/sui/client";import{normalizeSuiAddress as Zi}from"@mysten/sui/utils";import{CetusDlmmSDK as rr}from"@cetusprotocol/dlmm-sdk";import{SdkWrapper as er}from"@cetusprotocol/common-sdk";import{FullRpcUrlMainnet as Xi,GraphRpcUrlMainnet as Yi}from"@cetusprotocol/common-sdk";var Ri={env:"mainnet",full_rpc_url:Xi,graph_rpc_url:Yi,aggregator_url:"https://api-sui.cetus.zone/router_v3",providers:[]};import{FullRpcUrlTestnet as $i,GraphRpcUrlTestnet as Gi}from"@cetusprotocol/common-sdk";var Bi={env:"testnet",full_rpc_url:$i,graph_rpc_url:Gi,aggregator_url:"https://api-sui.devcetus.com/router_v3",providers:["CETUS","DEEPBOOK","KRIYA","KRIYAV3","FLOWX","FLOWXV3","AFTERMATH","TURBOS","HAEDAL","VOLO","AFSUI"]};var Ii=Di(ni());import{Transaction as Li}from"@mysten/sui/transactions";import{CoinAssist as si,d as x,DETAILS_KEYS as Ot,fromDecimalsAmount as Ti,toDecimalsAmount as Pi}from"@cetusprotocol/common-sdk";import{BaseError as ji}from"@cetusprotocol/common-sdk";var ki=(_=>(_.UnsupportedDepositMode="UnsupportedDepositMode",_.PositionIdUndefined="PositionIdUndefined",_.ParameterError="ParameterError",_.ReachMaxIterations="ReachMaxIterations",_.BestLiquidityIsZero="BestLiquidityIsZero",_.SwapAmountError="SwapAmountError",_.AggregatorError="AggregatorError",_))(ki||{}),Lt=class extends ji{constructor(m,d,g){super(m,d||"UnknownError",g)}static isZapErrorCode(m,d){return this.isErrorCode(m,d)}},qi=(b,m,d)=>{throw new Lt(m.message,b,d)},Ct=(b,m,d)=>{throw new Lt(m,b,d)};var oi=.005;var Et=Di(ni());import{ClmmPoolUtil as tr,d as Bt,TickMath as ir}from"@cetusprotocol/common-sdk";function Ci(b,m,d,g,f,S,M){let _=M?new Et.default(b):new Et.default(m),w=tr.estLiquidityAndCoinAmountFromOneAmounts(g,f,_,M,!0,S,new Et.default(d)),A=M?_.toString():w.coin_amount_a.toString(),L=M?w.coin_amount_b.toString():_.toString(),P=Bt(b).sub(A),q=Bt(m).sub(L);return{is_enough_amount:M?q.gte(0):P.gte(0),use_amount_a:A,use_amount_b:L,liquidity:w.liquidity_amount,amount_limit_a:w.coin_amount_limit_a,amount_limit_b:w.coin_amount_limit_b,remain_amount:M?q:P}}function cr(b,m,d,g,f,S,M){let _=ir.sqrtPriceX64ToTickIndex(new Et.default(d)),w=M;_<g?w=!0:_>f&&(w=!1);let A=Ci(b,m,d,g,f,S,w);return A.is_enough_amount||(w=!w,A=Ci(b,m,d,g,f,S,w)),{fix_liquidity_amount_a:w,...A}}function Ei(b,m,d,g){let f=Bt(b),S=Bt(d),M=Bt(g),_=f,w=m?f:Bt(0),A=m?Bt(0):f,L=M,P=Bt(M).mul(.999),q=Bt(0),D=_,C=null;function W(E){if(m){let R=w.minus(E),B=A.plus(E.mul(S));return{A:R,B,R:B.div(R)}}else{let R=w.plus(E.mul(S)),B=A.minus(E);return{A:R,B,R:R.div(B)}}}for(let E=0;E<120;E++){let R=q.plus(D).div(2),{A:B,B:I,R:O}=W(R);if(B.lte(0)||I.lte(0)){D=R;continue}if(O.gt(L))D=R;else if(O.lt(P))q=R;else{if(C=R,Bt(P).eq(0))break;D=R}}if(!C)return{swap_amount:"0",final_amount_a:w.toFixed(0),final_amount_b:A.toFixed(0)};let{A:U,B:Z}=W(C);return{swap_amount:C.toFixed(0),final_amount_a:U.toFixed(0),final_amount_b:Z.toFixed(0)}}import{BinUtils as ai}from"@cetusprotocol/dlmm-sdk";var Tt=class{constructor(m){this._sdk=m}get sdk(){return this._sdk}async calculateBalanceSwapAmountWithoutActiveId(m,d){let{fix_amount_a:g,coin_amount:f}=d,{bin_step:S,pool_id:M,active_id:_,lower_bin_id:w,upper_bin_id:A,active_bin_of_pool:L,strategy_type:P}=m,q=await this._sdk.DlmmSDK.Pool.getPool(M,!1),D,C=g,W=f;return _>A?(C=!1,g?(D=await this.findRouters(q.coin_type_a,q.coin_type_b,f),W=x(D.swap_out_amount).mul(1-.001).toFixed(0)):W=f):_<w&&(C=!0,g?W=f:(D=await this.findRouters(q.coin_type_b,q.coin_type_a,f),W=x(D.swap_out_amount).mul(1-.001).toFixed(0))),{liquidity_info:await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:M,coin_amount:W,fix_amount_a:C,active_id:_,bin_step:S,lower_bin_id:w,upper_bin_id:A,active_bin_of_pool:L,strategy_type:P}),swap_result:D}}async calculateBalanceSwapAmount(m,d){let{fix_amount_a:g,coin_amount:f}=d,{bin_step:S,pool_id:M,active_id:_,lower_bin_id:w,upper_bin_id:A,active_bin_of_pool:L,strategy_type:P}=m;if(_>A||_<w)return this.calculateBalanceSwapAmountWithoutActiveId(m,d);let q=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:M,coin_amount:f,fix_amount_a:g,active_id:_,bin_step:S,lower_bin_id:w,upper_bin_id:A,active_bin_of_pool:L,strategy_type:P}),{amount_a:D,amount_b:C}=q,W=g?x(C).div(D):x(D).div(C),U=g?ai.getPricePerLamportFromBinId(_,S):x(1).div(ai.getPricePerLamportFromBinId(_,S)).toString(),Z=await this._sdk.DlmmSDK.Pool.getPool(M,!1);try{let t=await this.findRouters(g?Z.coin_type_a:Z.coin_type_b,g?Z.coin_type_b:Z.coin_type_a,f);t&&(U=x(t.swap_out_amount).div(t.swap_in_amount).toString())}catch{}let{swap_amount:E,final_amount_a:R,final_amount_b:B}=Ei(f,g,U,W.toString());console.log("\u{1F680} ~ ZapModule ~ swap_amount:",{swap_amount:E,final_amount_a:R,final_amount_b:B,target_rate:W,real_price:U,coin_amount:f,fix_amount_a:g});let I=E||"0",O,At,St,Dt=0,qt=.02,T,Rt,h;do{let t=x(f).sub(I).toFixed(0);St=await this.findRouters(g?Z.coin_type_a:Z.coin_type_b,g?Z.coin_type_b:Z.coin_type_a,I);let r=x(St.swap_out_amount).toFixed(0),e=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:M,coin_amount:t,fix_amount_a:g,active_id:_,bin_step:S,lower_bin_id:w,upper_bin_id:A,active_bin_of_pool:L,strategy_type:P}),{amount_a:n,amount_b:s}=e,l=g?s:n,u=x(r).sub(l).div(r);if(u.gt(0)&&(h===void 0||u.lt(h))&&(T=St,Rt=e,h=u),x(r).gt(l))if(x(u).gt(qt))console.log("calculateBalanceSwapAmount -: ",{swap_amount_in:I,real_remain_rate:u.toString(),cached_real_remain_rate:h?.toString(),temp_swap_result:St,deposit_amount_other:l,count:Dt}),I=x(I).mul(1-.01).toFixed(0),Dt++;else{O=St,At=e;break}else I=x(I).mul(1+.01).toFixed(0),I=Math.min(Number(x(f).mul(1-1e-4).toFixed(0)),Number(I)).toString(),console.log("calculateBalanceSwapAmount +: ",{swap_amount_in:I,real_remain_rate:u.toString(),cached_real_remain_rate:h?.toString(),temp_swap_result:St,deposit_amount_other:l,count:Dt}),Dt++;if(Dt>5)break}while(!O&&x(I).gt(0));if(O===void 0&&T&&(O=T,At=Rt),O===void 0&&St){let{swap_out_amount:t}=St,r=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:M,coin_amount:x(t).sub(1-.001).toFixed(0),fix_amount_a:!g,active_id:_,bin_step:S,lower_bin_id:w,upper_bin_id:A,active_bin_of_pool:L,strategy_type:P});O=St,At=r}return{swap_result:O,liquidity_info:At}}async preCalculateDepositAmount(m,d){let{fix_amount_a:g,coin_amount:f}=d;console.log("\u{1F680} ~ ZapModule ~ preCalculateDepositAmount ~ options:",{options:m,mode_options:d});let{liquidity_info:S,swap_result:M}=await this.calculateBalanceSwapAmount(m,d);return S===void 0?Ct("SwapAmountError","Best liquidity info is undefined",{[Ot.METHOD_NAME]:"preCalculateDepositAmount"}):{bin_infos:S,swap_result:M,fix_amount_a:g,coin_amount:f}}async buildDepositPayload(m,d){let{swap_slippage:g=oi,bin_step:f,pool_id:S,active_id:M,lower_bin_id:_,upper_bin_id:w,strategy_type:A,deposit_obj:L,slippage:P,pos_obj:q}=m,{fix_amount_a:D,coin_amount:C,swap_result:W,bin_infos:U}=L;d=d||new Li;let Z=await this._sdk.DlmmSDK.Pool.getPool(S,!1),{coin_type_a:E,coin_type_b:R}=Z,B,I;if(W){let O=D?E:R,{swap_in_amount:At,route_obj:St}=W,Dt=si.buildCoinWithBalance(BigInt(At),O,d),qt={router:St,slippage:g,txb:d,inputCoin:Dt},T=await this._sdk.AggregatorClient.fixableRouterSwapV3(qt);D?(B=si.buildCoinWithBalance(BigInt(U.amount_a),E,d),I=T):(B=T,I=si.buildCoinWithBalance(BigInt(U.amount_b),R,d))}if(q){let O={pool_id:S,bin_infos:U,coin_type_a:E,coin_type_b:R,active_id:M,strategy_type:A,max_price_slippage:P,bin_step:f,use_bin_infos:!1,position_id:q.pos_id,collect_fee:q.collect_fee,reward_coins:q.collect_rewarder_types,coin_object_id_a:B,coin_object_id_b:I};this._sdk.DlmmSDK.Position.addLiquidityPayload(O,d)}else{let O={pool_id:S,bin_infos:U,coin_type_a:E,coin_type_b:R,lower_bin_id:_,upper_bin_id:w,active_id:M,strategy_type:A,use_bin_infos:!1,max_price_slippage:P,bin_step:f,coin_object_id_a:B,coin_object_id_b:I};this._sdk.DlmmSDK.Position.addLiquidityPayload(O,d)}return d}async findRouters(m,d,g){let{providers:f}=this._sdk.sdkOptions,S=this._sdk.AggregatorClient;if(x(g).lt(1))return Ct("SwapAmountError","Swap amount is less than the minimum precision",{[Ot.METHOD_NAME]:"findRouters"});try{let M={from:m,target:d,amount:new Ii.default(g),byAmountIn:!0,depth:3,providers:f},_=await S.findRouters(M);if(_?.error)return Ct("AggregatorError",`Aggregator findRouters error: ${_?.error}`,{[Ot.METHOD_NAME]:"findRouters",[Ot.REQUEST_PARAMS]:M});if(!_?.paths||_?.paths?.length===0)return Ct("AggregatorError","Aggregator findRouters error: no router",{[Ot.METHOD_NAME]:"findRouters",[Ot.REQUEST_PARAMS]:M});let w=_.amountIn.toString(),A=_.amountOut.toString();return{swap_in_amount:w,swap_out_amount:A,route_obj:_}}catch(M){return console.log("\u{1F680} ~ ZapModule ~ error:",JSON.stringify(M,null,2)),qi("AggregatorError",M,{[Ot.METHOD_NAME]:"swapInPools"})}}calculateZapOutAvailableAmount(m){let{is_receive_coin_a:d,mode:g,active_id:f,bin_step:S,remove_bin_range:M,prices:_,coin_decimal_a:w,coin_decimal_b:A}=m,L;if(_){let{coin_a_price:R,coin_b_price:B}=_;L=x(R).div(B).toString()}else L=ai.getPriceFromBinId(f,S,w,A);let P,q="0",D="0",C="0";M.forEach(R=>{R.bin_id===f&&(P=R),q=x(q).add(R.amount_a).toFixed(0),D=x(D).add(R.amount_b).toFixed(0)});let W=x(q).sub(P?.amount_a||"0").toFixed(0),U=x(D).sub(P?.amount_b||"0").toFixed(0),Z=x(Pi(x(Ti(q,w)).mul(L).toString(),A)).toFixed(0),E=x(Pi(x(Ti(D,A)).div(L).toString(),w)).toFixed(0);return g==="OnlyCoinA"?d?C=W:C=Z:g==="OnlyCoinB"?(D=U,d?C=E:C=D):g==="Both"&&(d?C=x(E).add(q).toFixed(0):C=x(Z).add(D).toFixed(0)),{available_amount:C,user_total_amount_a:q,user_total_amount_b:D,active_bin:P,is_receive_coin_a:d}}async preCalculateWithdrawAmount(m){let{is_receive_coin_a:d,remove_bin_range:g,coin_type_a:f,coin_type_b:S,mode:M,expected_receive_amount:_,active_id:w}=m,{available_amount:A,user_total_amount_a:L,user_total_amount_b:P,active_bin:q}=this.calculateZapOutAvailableAmount(m);if(x(A).lt(x(_)))return Ct("SwapAmountError",`Available amount is less than the expected receive amount: ${A} < ${_}`,{[Ot.METHOD_NAME]:"preCalculateWithdrawAmount",[Ot.REQUEST_PARAMS]:m});let D=x(_).div(A),C;if(M==="Both"){let E=x(L).gt(0),R=E?x(L).mul(D).toFixed(0):x(P).mul(D).toFixed(0);C=this._sdk.DlmmSDK.Position.calculateRemoveLiquidityInfo({bins:g,active_id:w,fix_amount_a:E,coin_amount:R})}else{let E=M==="OnlyCoinA"?x(L).mul(D).toFixed(0):x(P).mul(D).toFixed(0);C=this._sdk.DlmmSDK.Position.calculateRemoveLiquidityInfo({bins:g,active_id:w,is_only_a:M==="OnlyCoinA",coin_amount:E})}let{amount_a:W,amount_b:U}=C,Z;return d&&x(U).gt(0)&&(Z=await this.findRouters(S,f,U)),!d&&x(W).gt(0)&&(Z=await this.findRouters(f,S,W)),{remove_liquidity_info:C,swap_result:Z,mode:M,is_receive_coin_a:d,expected_receive_amount:_,remove_percent:D.toString()}}async buildWithdrawPayload(m){let d=new Li,{withdraw_obj:g,swap_slippage:f=oi,bin_step:S,pool_id:M,active_id:_,collect_fee:w,reward_coins:A,coin_type_a:L,coin_type_b:P,position_id:q,slippage:D,remove_percent:C,is_close_position:W}=m,{remove_liquidity_info:U,swap_result:Z,is_receive_coin_a:E}=g,R={pool_id:M,bin_infos:U,coin_type_a:L,coin_type_b:P,position_id:q,slippage:D,active_id:_,reward_coins:A,collect_fee:w,bin_step:S,remove_percent:Number(C)},{coin_a_obj:B,coin_b_obj:I}=W?this._sdk.DlmmSDK.Position.closePositionNoTransferPayload(R,d):this._sdk.DlmmSDK.Position.removeLiquidityNoTransferPayload(R,d);if(Z){let{route_obj:O}=Z,At={router:O,slippage:f,txb:d,inputCoin:E?I:B},St=await this._sdk.AggregatorClient.fixableRouterSwapV3(At);E?d.transferObjects([B,St],this._sdk.getSenderAddress()):d.transferObjects([I,St],this._sdk.getSenderAddress())}else d.transferObjects([B,I],this._sdk.getSenderAddress());return d}};var It=class b extends er{constructor(m,d){super(m),this._zapModule=new Tt(this),this._dlmmSDK=d||rr.createSDK({env:m.env,full_rpc_url:m.full_rpc_url}),this._aggregatorClient=new Fi({signer:Zi("0x0"),client:m.sui_client||new Ki({url:m.full_rpc_url}),env:m.env==="testnet"?Pt.Testnet:Pt.Mainnet,pythUrls:m.pyth_urls})}setSenderAddress(m){this._dlmmSDK.setSenderAddress(m)}getSenderAddress(m=!0){return this._dlmmSDK.getSenderAddress(m)}updateProviders(m){if(m.length===0)throw new Error("providers is empty");this._sdkOptions.providers=m}updateFullRpcUrl(m){super.updateFullRpcUrl(m),this._dlmmSDK.updateFullRpcUrl(m),this._aggregatorClient=new Fi({signer:Zi("0x0"),client:new Ki({url:m}),env:this._sdkOptions.env==="testnet"?Pt.Testnet:Pt.Mainnet,pythUrls:this._sdkOptions.pyth_urls})}get DlmmSDK(){return this._dlmmSDK}get AggregatorClient(){return this._aggregatorClient}get Zap(){return this._zapModule}static createSDK(m,d){let{env:g="mainnet",full_rpc_url:f}=m;return g==="mainnet"?b.createCustomSDK({...Ri,...m},d):b.createCustomSDK({...Bi,...m},d)}static createCustomSDK(m,d){return new b(m,d)}};var Me=It;export{It as CetusDlmmZapSDK,Lt as ZapError,ki as ZapErrorCode,Tt as ZapModule,Ei as calcExactSwapAmount,Ci as calculateLiquidityAmountEnough,cr as calculateLiquidityAmountSide,Me as default,oi as defaultSwapSlippage,qi as handleError,Ct as handleMessageError,Ri as zapMainnet,Bi as zapTestnet};
2
- //# sourceMappingURL=index.mjs.map