@cetusprotocol/dlmm-zap-sdk 0.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.
@@ -0,0 +1,319 @@
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
+ } & CoinPairType;
85
+ type CalculationWithdrawAvailableAmountOptions = {
86
+ remove_bin_range: BinAmount[];
87
+ active_id: number;
88
+ bin_step: number;
89
+ is_receive_coin_a: boolean;
90
+ mode: WithdrawMode;
91
+ };
92
+ /**
93
+ * Result of a withdrawal calculation
94
+ */
95
+ type CalculationWithdrawResult = {
96
+ remove_liquidity_info: BinLiquidityInfo;
97
+ mode: WithdrawMode;
98
+ is_receive_coin_a?: boolean;
99
+ swap_result?: SwapResult;
100
+ expected_receive_amount: string;
101
+ remove_percent: string;
102
+ };
103
+ /**
104
+ * Complete options for executing a withdrawal
105
+ */
106
+ type WithdrawOptions = {
107
+ withdraw_obj: CalculationWithdrawResult;
108
+ swap_slippage?: number;
109
+ pool_id: string;
110
+ position_id: string;
111
+ active_id: number;
112
+ bin_step: number;
113
+ slippage: number;
114
+ reward_coins: string[];
115
+ collect_fee: boolean;
116
+ remove_percent?: number;
117
+ is_close_position: boolean;
118
+ } & CoinPairType;
119
+
120
+ /**
121
+ * ZapModule handles interactions with clmm pools within the system.
122
+ */
123
+ declare class ZapModule implements IModule<CetusDlmmZapSDK> {
124
+ protected _sdk: CetusDlmmZapSDK;
125
+ constructor(sdk: CetusDlmmZapSDK);
126
+ /**
127
+ * Returns the associated SDK instance
128
+ */
129
+ get sdk(): CetusDlmmZapSDK;
130
+ private calculateBalanceSwapAmountWithoutActiveId;
131
+ private calculateBalanceSwapAmount;
132
+ /**
133
+ * Pre-calculates the deposit amount based on the selected mode.
134
+ * @param options
135
+ * @param mode_options
136
+ * @returns
137
+ */
138
+ preCalculateDepositAmount(options: BaseDepositOptions, mode_options: OnlyCoinDepositOptions): Promise<CalculationDepositResult>;
139
+ buildDepositPayload(options: DepositOptions, tx?: Transaction): Promise<Transaction>;
140
+ findRouters(from: string, target: string, amount: string): Promise<SwapResult>;
141
+ calculateZapOutAvailableAmount(options: CalculationWithdrawAvailableAmountOptions): {
142
+ available_amount: string;
143
+ user_total_amount_a: string;
144
+ user_total_amount_b: string;
145
+ active_bin: BinAmount | undefined;
146
+ is_receive_coin_a: boolean;
147
+ };
148
+ preCalculateWithdrawAmount(options: CalculationWithdrawOptions): Promise<CalculationWithdrawResult>;
149
+ buildWithdrawPayload(options: WithdrawOptions): Promise<Transaction>;
150
+ }
151
+
152
+ /**
153
+ * Represents options and configurations for an SDK.
154
+ */
155
+ interface SdkOptions extends BaseSdkOptions {
156
+ /**
157
+ * The URL of the aggregator service.
158
+ */
159
+ aggregator_url: string;
160
+ /**
161
+ * A list of aggregator providers.
162
+ */
163
+ providers: string[];
164
+ /**
165
+ * A list of Pyth price ID.
166
+ */
167
+ pyth_urls?: string[];
168
+ }
169
+ /**
170
+ * The entry class of CetusDlmmZapSDK, which is almost responsible for all interactions with dlmm zap.
171
+ */
172
+ declare class CetusDlmmZapSDK extends SdkWrapper<SdkOptions> {
173
+ /**
174
+ * Module for managing vaults.
175
+ */
176
+ protected _zapModule: ZapModule;
177
+ protected _dlmmSDK: CetusDlmmSDK;
178
+ /**
179
+ * Client for interacting with the Aggregator service.
180
+ */
181
+ protected _aggregatorClient: AggregatorClient;
182
+ constructor(options: SdkOptions, dlmmSDK?: CetusDlmmSDK);
183
+ setSenderAddress(value: string): void;
184
+ getSenderAddress(validate?: boolean): string;
185
+ /**
186
+ * Updates the providers for the AggregatorClient.
187
+ * @param providers - The new providers to set.
188
+ */
189
+ updateProviders(providers: string[]): void;
190
+ updateFullRpcUrl(url: string): void;
191
+ /**
192
+ * Getter for the DlmmSDK property.
193
+ * @returns {CetusDlmmSDK} The DlmmSDK property value.
194
+ */
195
+ get DlmmSDK(): CetusDlmmSDK;
196
+ /**
197
+ * Getter for the AggregatorClient property.
198
+ * @returns {AggregatorClient} The AggregatorClient property value.
199
+ */
200
+ get AggregatorClient(): AggregatorClient;
201
+ /**
202
+ * Getter for the ZapModule property.
203
+ * @returns {ZapModule} The ZapModule property value.
204
+ */
205
+ get Zap(): ZapModule;
206
+ /**
207
+ * Static factory method to initialize the SDK
208
+ * @param options SDK initialization options
209
+ * @param clmm_sdk Optional CLMM SDK instance
210
+ * @returns An instance of CetusZapSDK
211
+ */
212
+ static createSDK(options: BaseSdkOptions, dlmm_sdk?: any): CetusDlmmZapSDK;
213
+ /**
214
+ * Create a custom SDK instance with the given options
215
+ * @param options The options for the SDK
216
+ * @returns An instance of CetusBurnSDK
217
+ */
218
+ static createCustomSDK<T extends BaseSdkOptions>(options: T & SdkOptions, dlmm_sdk?: CetusDlmmSDK): CetusDlmmZapSDK;
219
+ }
220
+
221
+ /**
222
+ * Calculate if there is enough liquidity amount for adding liquidity to a pool
223
+ * @param amount_a - Amount of token A
224
+ * @param amount_b - Amount of token B
225
+ * @param curr_sqrt_price - Current square root price of the pool
226
+ * @param lower_tick - Lower tick boundary of the position
227
+ * @param upper_tick - Upper tick boundary of the position
228
+ * @param slippage - Slippage tolerance for the calculation
229
+ * @param fix_amount_a - Whether to fix the amount of token A
230
+ * @returns Object containing:
231
+ * - is_enough_amount: Whether there is enough amount for the other token
232
+ * - use_amount_a: Amount of token A to be used
233
+ * - use_amount_b: Amount of token B to be used
234
+ * - liquidity: Calculated liquidity amount
235
+ * - amount_limit_a: Minimum amount limit for token A
236
+ * - amount_limit_b: Minimum amount limit for token B
237
+ * - remain_amount: Remaining amount of the non-fixed token
238
+ */
239
+ 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): {
240
+ is_enough_amount: boolean;
241
+ use_amount_a: string;
242
+ use_amount_b: string;
243
+ liquidity: string;
244
+ amount_limit_a: string;
245
+ amount_limit_b: string;
246
+ remain_amount: Decimal;
247
+ };
248
+ /**
249
+ * Calculate the optimal side for adding liquidity based on current price and tick range
250
+ * @param amount_a - Amount of token A
251
+ * @param amount_b - Amount of token B
252
+ * @param curr_sqrt_price - Current square root price of the pool
253
+ * @param lower_tick - Lower tick boundary of the position
254
+ * @param upper_tick - Upper tick boundary of the position
255
+ * @param slippage - Slippage tolerance for the calculation
256
+ * @param fix_amount_a - Whether to fix the amount of token A
257
+ * @returns Object containing:
258
+ * - fix_liquidity_amount_a: Whether to fix token A amount
259
+ * - is_enough_amount: Whether there is enough amount for the other token
260
+ * - use_amount_a: Amount of token A to be used
261
+ * - use_amount_b: Amount of token B to be used
262
+ * - liquidity: Calculated liquidity amount
263
+ * - amount_limit_a: Minimum amount limit for token A
264
+ * - amount_limit_b: Minimum amount limit for token B
265
+ * - remain_amount: Remaining amount of the non-fixed token
266
+ */
267
+ 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): {
268
+ is_enough_amount: boolean;
269
+ use_amount_a: string;
270
+ use_amount_b: string;
271
+ liquidity: string;
272
+ amount_limit_a: string;
273
+ amount_limit_b: string;
274
+ remain_amount: Decimal;
275
+ fix_liquidity_amount_a: boolean;
276
+ };
277
+ /**
278
+ * Calculate the swap amount needed to achieve target ratio
279
+ * @param coin_amount - Total coin amount available
280
+ * @param fix_amount_a - Whether to fix token A amount (true) or token B amount (false)
281
+ * @param current_price - Current price:
282
+ * - When fix_amount_a = true: current_price = B/A (price of B in terms of A)
283
+ * - When fix_amount_a = false: current_price = A/B (price of A in terms of B)
284
+ * @param target_ratio - Target ratio to achieve:
285
+ * - When fix_amount_a = true: target_ratio = B/A
286
+ * - When fix_amount_a = false: target_ratio = A/B
287
+ * @param tolerance - Tolerance for the target ratio (default: 0.01)
288
+ * @returns Object containing:
289
+ * - swap_amount: Amount to swap
290
+ * - final_amount_a: Final amount of token A (remaining A when fix_amount_a=true, obtained A when fix_amount_a=false)
291
+ * - final_amount_b: Final amount of token B (obtained B when fix_amount_a=true, remaining B when fix_amount_a=false)
292
+ */
293
+ declare function calcExactSwapAmount(coin_amount: string, fix_amount_a: boolean, current_price: string, target_ratio: string): {
294
+ swap_amount: string;
295
+ final_amount_a: string;
296
+ final_amount_b: string;
297
+ };
298
+
299
+ declare const zapMainnet: SdkOptions;
300
+
301
+ declare const zapTestnet: SdkOptions;
302
+
303
+ declare enum ZapErrorCode {
304
+ UnsupportedDepositMode = "UnsupportedDepositMode",
305
+ PositionIdUndefined = "PositionIdUndefined",
306
+ ParameterError = "ParameterError",
307
+ ReachMaxIterations = "ReachMaxIterations",
308
+ BestLiquidityIsZero = "BestLiquidityIsZero",
309
+ SwapAmountError = "SwapAmountError",
310
+ AggregatorError = "AggregatorError"
311
+ }
312
+ declare class ZapError extends BaseError {
313
+ constructor(message: string, errorCode?: ZapErrorCode, details?: Record<string, any>);
314
+ static isZapErrorCode(e: any, code: ZapErrorCode): boolean;
315
+ }
316
+ declare const handleError: (code: ZapErrorCode, error: Error, details?: Record<string, any>) => never;
317
+ declare const handleMessageError: (code: ZapErrorCode, message: string, details?: Record<string, any>) => never;
318
+
319
+ 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.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";var Wi=Object.create;var It=Object.defineProperty;var Ui=Object.getOwnPropertyDescriptor;var zi=Object.getOwnPropertyNames;var Hi=Object.getPrototypeOf,Vi=Object.prototype.hasOwnProperty;var Ji=(S,m)=>()=>(m||S((m={exports:{}}).exports,m),m.exports),Qi=(S,m)=>{for(var d in m)It(S,d,{get:m[d],enumerable:!0})},Pi=(S,m,d,g)=>{if(m&&typeof m=="object"||typeof m=="function")for(let f of zi(m))!Vi.call(S,f)&&f!==d&&It(S,f,{get:()=>m[f],enumerable:!(g=Ui(m,f))||g.enumerable});return S};var Ti=(S,m,d)=>(d=S!=null?Wi(Hi(S)):{},Pi(m||!S||!S.__esModule?It(d,"default",{value:S,enumerable:!0}):d,S)),Xi=S=>Pi(It({},"__esModule",{value:!0}),S);var li=Ji((Ii,fi)=>{"use strict";(function(S,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 S=="object"?S.exports=f:m.BN=f,f.BN=f,f.wordSize=26;var A;try{typeof window<"u"&&typeof window.Buffer<"u"?A=window.Buffer:A=require("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 w(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=w(h,r);return r-1>=t&&(e|=w(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 y(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=y(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=y(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 b(h,t){h.words=t.words,h.length=t.length,h.negative=t.negative,h.red=t.red}if(f.prototype._move=function(t){b(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")]=D}catch{f.prototype.inspect=D}else f.prototype.inspect=D;function D(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var B=["","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"],K=[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];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=B[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=K[t],a=k[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=B[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)},A&&(f.prototype.toBuffer=function(t,r){return this.toArrayLike(A,t,r)}),f.prototype.toArray=function(t,r){return this.toArrayLike(Array,t,r)};var E=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=E(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 bt(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 x=i-v|0;n=h.words[x]|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 N=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,x=n[1]|0,O=x&8191,I=x>>>13,Ot=n[2]|0,F=Ot&8191,Z=Ot>>>13,_i=n[3]|0,U=_i&8191,z=_i>>>13,Mi=n[4]|0,H=Mi&8191,V=Mi>>>13,wi=n[5]|0,J=wi&8191,Q=wi>>>13,yi=n[6]|0,X=yi&8191,Y=yi>>>13,xi=n[7]|0,$=xi&8191,G=xi>>>13,bi=n[8]|0,j=bi&8191,tt=bi>>>13,Si=n[9]|0,it=Si&8191,rt=Si>>>13,Ai=s[0]|0,et=Ai&8191,nt=Ai>>>13,Di=s[1]|0,ot=Di&8191,st=Di>>>13,Ri=s[2]|0,at=Ri&8191,ht=Ri>>>13,Bi=s[3]|0,ft=Bi&8191,lt=Bi>>>13,Oi=s[4]|0,ut=Oi&8191,dt=Oi>>>13,ki=s[5]|0,mt=ki&8191,pt=ki>>>13,qi=s[6]|0,ct=qi&8191,vt=qi>>>13,Ci=s[7]|0,gt=Ci&8191,_t=Ci>>>13,Li=s[8]|0,Mt=Li&8191,wt=Li>>>13,Ei=s[9]|0,yt=Ei&8191,xt=Ei>>>13;e.negative=t.negative^r.negative,e.length=19,o=Math.imul(c,et),i=Math.imul(c,nt),i=i+Math.imul(v,et)|0,a=Math.imul(v,nt);var Wt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Wt>>>26)|0,Wt&=67108863,o=Math.imul(O,et),i=Math.imul(O,nt),i=i+Math.imul(I,et)|0,a=Math.imul(I,nt),o=o+Math.imul(c,ot)|0,i=i+Math.imul(c,st)|0,i=i+Math.imul(v,ot)|0,a=a+Math.imul(v,st)|0;var Ut=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Ut>>>26)|0,Ut&=67108863,o=Math.imul(F,et),i=Math.imul(F,nt),i=i+Math.imul(Z,et)|0,a=Math.imul(Z,nt),o=o+Math.imul(O,ot)|0,i=i+Math.imul(O,st)|0,i=i+Math.imul(I,ot)|0,a=a+Math.imul(I,st)|0,o=o+Math.imul(c,at)|0,i=i+Math.imul(c,ht)|0,i=i+Math.imul(v,at)|0,a=a+Math.imul(v,ht)|0;var zt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(zt>>>26)|0,zt&=67108863,o=Math.imul(U,et),i=Math.imul(U,nt),i=i+Math.imul(z,et)|0,a=Math.imul(z,nt),o=o+Math.imul(F,ot)|0,i=i+Math.imul(F,st)|0,i=i+Math.imul(Z,ot)|0,a=a+Math.imul(Z,st)|0,o=o+Math.imul(O,at)|0,i=i+Math.imul(O,ht)|0,i=i+Math.imul(I,at)|0,a=a+Math.imul(I,ht)|0,o=o+Math.imul(c,ft)|0,i=i+Math.imul(c,lt)|0,i=i+Math.imul(v,ft)|0,a=a+Math.imul(v,lt)|0;var Ht=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Ht>>>26)|0,Ht&=67108863,o=Math.imul(H,et),i=Math.imul(H,nt),i=i+Math.imul(V,et)|0,a=Math.imul(V,nt),o=o+Math.imul(U,ot)|0,i=i+Math.imul(U,st)|0,i=i+Math.imul(z,ot)|0,a=a+Math.imul(z,st)|0,o=o+Math.imul(F,at)|0,i=i+Math.imul(F,ht)|0,i=i+Math.imul(Z,at)|0,a=a+Math.imul(Z,ht)|0,o=o+Math.imul(O,ft)|0,i=i+Math.imul(O,lt)|0,i=i+Math.imul(I,ft)|0,a=a+Math.imul(I,lt)|0,o=o+Math.imul(c,ut)|0,i=i+Math.imul(c,dt)|0,i=i+Math.imul(v,ut)|0,a=a+Math.imul(v,dt)|0;var Vt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Vt>>>26)|0,Vt&=67108863,o=Math.imul(J,et),i=Math.imul(J,nt),i=i+Math.imul(Q,et)|0,a=Math.imul(Q,nt),o=o+Math.imul(H,ot)|0,i=i+Math.imul(H,st)|0,i=i+Math.imul(V,ot)|0,a=a+Math.imul(V,st)|0,o=o+Math.imul(U,at)|0,i=i+Math.imul(U,ht)|0,i=i+Math.imul(z,at)|0,a=a+Math.imul(z,ht)|0,o=o+Math.imul(F,ft)|0,i=i+Math.imul(F,lt)|0,i=i+Math.imul(Z,ft)|0,a=a+Math.imul(Z,lt)|0,o=o+Math.imul(O,ut)|0,i=i+Math.imul(O,dt)|0,i=i+Math.imul(I,ut)|0,a=a+Math.imul(I,dt)|0,o=o+Math.imul(c,mt)|0,i=i+Math.imul(c,pt)|0,i=i+Math.imul(v,mt)|0,a=a+Math.imul(v,pt)|0;var Jt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Jt>>>26)|0,Jt&=67108863,o=Math.imul(X,et),i=Math.imul(X,nt),i=i+Math.imul(Y,et)|0,a=Math.imul(Y,nt),o=o+Math.imul(J,ot)|0,i=i+Math.imul(J,st)|0,i=i+Math.imul(Q,ot)|0,a=a+Math.imul(Q,st)|0,o=o+Math.imul(H,at)|0,i=i+Math.imul(H,ht)|0,i=i+Math.imul(V,at)|0,a=a+Math.imul(V,ht)|0,o=o+Math.imul(U,ft)|0,i=i+Math.imul(U,lt)|0,i=i+Math.imul(z,ft)|0,a=a+Math.imul(z,lt)|0,o=o+Math.imul(F,ut)|0,i=i+Math.imul(F,dt)|0,i=i+Math.imul(Z,ut)|0,a=a+Math.imul(Z,dt)|0,o=o+Math.imul(O,mt)|0,i=i+Math.imul(O,pt)|0,i=i+Math.imul(I,mt)|0,a=a+Math.imul(I,pt)|0,o=o+Math.imul(c,ct)|0,i=i+Math.imul(c,vt)|0,i=i+Math.imul(v,ct)|0,a=a+Math.imul(v,vt)|0;var Qt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Qt>>>26)|0,Qt&=67108863,o=Math.imul($,et),i=Math.imul($,nt),i=i+Math.imul(G,et)|0,a=Math.imul(G,nt),o=o+Math.imul(X,ot)|0,i=i+Math.imul(X,st)|0,i=i+Math.imul(Y,ot)|0,a=a+Math.imul(Y,st)|0,o=o+Math.imul(J,at)|0,i=i+Math.imul(J,ht)|0,i=i+Math.imul(Q,at)|0,a=a+Math.imul(Q,ht)|0,o=o+Math.imul(H,ft)|0,i=i+Math.imul(H,lt)|0,i=i+Math.imul(V,ft)|0,a=a+Math.imul(V,lt)|0,o=o+Math.imul(U,ut)|0,i=i+Math.imul(U,dt)|0,i=i+Math.imul(z,ut)|0,a=a+Math.imul(z,dt)|0,o=o+Math.imul(F,mt)|0,i=i+Math.imul(F,pt)|0,i=i+Math.imul(Z,mt)|0,a=a+Math.imul(Z,pt)|0,o=o+Math.imul(O,ct)|0,i=i+Math.imul(O,vt)|0,i=i+Math.imul(I,ct)|0,a=a+Math.imul(I,vt)|0,o=o+Math.imul(c,gt)|0,i=i+Math.imul(c,_t)|0,i=i+Math.imul(v,gt)|0,a=a+Math.imul(v,_t)|0;var Xt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Xt>>>26)|0,Xt&=67108863,o=Math.imul(j,et),i=Math.imul(j,nt),i=i+Math.imul(tt,et)|0,a=Math.imul(tt,nt),o=o+Math.imul($,ot)|0,i=i+Math.imul($,st)|0,i=i+Math.imul(G,ot)|0,a=a+Math.imul(G,st)|0,o=o+Math.imul(X,at)|0,i=i+Math.imul(X,ht)|0,i=i+Math.imul(Y,at)|0,a=a+Math.imul(Y,ht)|0,o=o+Math.imul(J,ft)|0,i=i+Math.imul(J,lt)|0,i=i+Math.imul(Q,ft)|0,a=a+Math.imul(Q,lt)|0,o=o+Math.imul(H,ut)|0,i=i+Math.imul(H,dt)|0,i=i+Math.imul(V,ut)|0,a=a+Math.imul(V,dt)|0,o=o+Math.imul(U,mt)|0,i=i+Math.imul(U,pt)|0,i=i+Math.imul(z,mt)|0,a=a+Math.imul(z,pt)|0,o=o+Math.imul(F,ct)|0,i=i+Math.imul(F,vt)|0,i=i+Math.imul(Z,ct)|0,a=a+Math.imul(Z,vt)|0,o=o+Math.imul(O,gt)|0,i=i+Math.imul(O,_t)|0,i=i+Math.imul(I,gt)|0,a=a+Math.imul(I,_t)|0,o=o+Math.imul(c,Mt)|0,i=i+Math.imul(c,wt)|0,i=i+Math.imul(v,Mt)|0,a=a+Math.imul(v,wt)|0;var Yt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Yt>>>26)|0,Yt&=67108863,o=Math.imul(it,et),i=Math.imul(it,nt),i=i+Math.imul(rt,et)|0,a=Math.imul(rt,nt),o=o+Math.imul(j,ot)|0,i=i+Math.imul(j,st)|0,i=i+Math.imul(tt,ot)|0,a=a+Math.imul(tt,st)|0,o=o+Math.imul($,at)|0,i=i+Math.imul($,ht)|0,i=i+Math.imul(G,at)|0,a=a+Math.imul(G,ht)|0,o=o+Math.imul(X,ft)|0,i=i+Math.imul(X,lt)|0,i=i+Math.imul(Y,ft)|0,a=a+Math.imul(Y,lt)|0,o=o+Math.imul(J,ut)|0,i=i+Math.imul(J,dt)|0,i=i+Math.imul(Q,ut)|0,a=a+Math.imul(Q,dt)|0,o=o+Math.imul(H,mt)|0,i=i+Math.imul(H,pt)|0,i=i+Math.imul(V,mt)|0,a=a+Math.imul(V,pt)|0,o=o+Math.imul(U,ct)|0,i=i+Math.imul(U,vt)|0,i=i+Math.imul(z,ct)|0,a=a+Math.imul(z,vt)|0,o=o+Math.imul(F,gt)|0,i=i+Math.imul(F,_t)|0,i=i+Math.imul(Z,gt)|0,a=a+Math.imul(Z,_t)|0,o=o+Math.imul(O,Mt)|0,i=i+Math.imul(O,wt)|0,i=i+Math.imul(I,Mt)|0,a=a+Math.imul(I,wt)|0,o=o+Math.imul(c,yt)|0,i=i+Math.imul(c,xt)|0,i=i+Math.imul(v,yt)|0,a=a+Math.imul(v,xt)|0;var $t=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+($t>>>26)|0,$t&=67108863,o=Math.imul(it,ot),i=Math.imul(it,st),i=i+Math.imul(rt,ot)|0,a=Math.imul(rt,st),o=o+Math.imul(j,at)|0,i=i+Math.imul(j,ht)|0,i=i+Math.imul(tt,at)|0,a=a+Math.imul(tt,ht)|0,o=o+Math.imul($,ft)|0,i=i+Math.imul($,lt)|0,i=i+Math.imul(G,ft)|0,a=a+Math.imul(G,lt)|0,o=o+Math.imul(X,ut)|0,i=i+Math.imul(X,dt)|0,i=i+Math.imul(Y,ut)|0,a=a+Math.imul(Y,dt)|0,o=o+Math.imul(J,mt)|0,i=i+Math.imul(J,pt)|0,i=i+Math.imul(Q,mt)|0,a=a+Math.imul(Q,pt)|0,o=o+Math.imul(H,ct)|0,i=i+Math.imul(H,vt)|0,i=i+Math.imul(V,ct)|0,a=a+Math.imul(V,vt)|0,o=o+Math.imul(U,gt)|0,i=i+Math.imul(U,_t)|0,i=i+Math.imul(z,gt)|0,a=a+Math.imul(z,_t)|0,o=o+Math.imul(F,Mt)|0,i=i+Math.imul(F,wt)|0,i=i+Math.imul(Z,Mt)|0,a=a+Math.imul(Z,wt)|0,o=o+Math.imul(O,yt)|0,i=i+Math.imul(O,xt)|0,i=i+Math.imul(I,yt)|0,a=a+Math.imul(I,xt)|0;var Gt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Gt>>>26)|0,Gt&=67108863,o=Math.imul(it,at),i=Math.imul(it,ht),i=i+Math.imul(rt,at)|0,a=Math.imul(rt,ht),o=o+Math.imul(j,ft)|0,i=i+Math.imul(j,lt)|0,i=i+Math.imul(tt,ft)|0,a=a+Math.imul(tt,lt)|0,o=o+Math.imul($,ut)|0,i=i+Math.imul($,dt)|0,i=i+Math.imul(G,ut)|0,a=a+Math.imul(G,dt)|0,o=o+Math.imul(X,mt)|0,i=i+Math.imul(X,pt)|0,i=i+Math.imul(Y,mt)|0,a=a+Math.imul(Y,pt)|0,o=o+Math.imul(J,ct)|0,i=i+Math.imul(J,vt)|0,i=i+Math.imul(Q,ct)|0,a=a+Math.imul(Q,vt)|0,o=o+Math.imul(H,gt)|0,i=i+Math.imul(H,_t)|0,i=i+Math.imul(V,gt)|0,a=a+Math.imul(V,_t)|0,o=o+Math.imul(U,Mt)|0,i=i+Math.imul(U,wt)|0,i=i+Math.imul(z,Mt)|0,a=a+Math.imul(z,wt)|0,o=o+Math.imul(F,yt)|0,i=i+Math.imul(F,xt)|0,i=i+Math.imul(Z,yt)|0,a=a+Math.imul(Z,xt)|0;var jt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(jt>>>26)|0,jt&=67108863,o=Math.imul(it,ft),i=Math.imul(it,lt),i=i+Math.imul(rt,ft)|0,a=Math.imul(rt,lt),o=o+Math.imul(j,ut)|0,i=i+Math.imul(j,dt)|0,i=i+Math.imul(tt,ut)|0,a=a+Math.imul(tt,dt)|0,o=o+Math.imul($,mt)|0,i=i+Math.imul($,pt)|0,i=i+Math.imul(G,mt)|0,a=a+Math.imul(G,pt)|0,o=o+Math.imul(X,ct)|0,i=i+Math.imul(X,vt)|0,i=i+Math.imul(Y,ct)|0,a=a+Math.imul(Y,vt)|0,o=o+Math.imul(J,gt)|0,i=i+Math.imul(J,_t)|0,i=i+Math.imul(Q,gt)|0,a=a+Math.imul(Q,_t)|0,o=o+Math.imul(H,Mt)|0,i=i+Math.imul(H,wt)|0,i=i+Math.imul(V,Mt)|0,a=a+Math.imul(V,wt)|0,o=o+Math.imul(U,yt)|0,i=i+Math.imul(U,xt)|0,i=i+Math.imul(z,yt)|0,a=a+Math.imul(z,xt)|0;var ti=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(ti>>>26)|0,ti&=67108863,o=Math.imul(it,ut),i=Math.imul(it,dt),i=i+Math.imul(rt,ut)|0,a=Math.imul(rt,dt),o=o+Math.imul(j,mt)|0,i=i+Math.imul(j,pt)|0,i=i+Math.imul(tt,mt)|0,a=a+Math.imul(tt,pt)|0,o=o+Math.imul($,ct)|0,i=i+Math.imul($,vt)|0,i=i+Math.imul(G,ct)|0,a=a+Math.imul(G,vt)|0,o=o+Math.imul(X,gt)|0,i=i+Math.imul(X,_t)|0,i=i+Math.imul(Y,gt)|0,a=a+Math.imul(Y,_t)|0,o=o+Math.imul(J,Mt)|0,i=i+Math.imul(J,wt)|0,i=i+Math.imul(Q,Mt)|0,a=a+Math.imul(Q,wt)|0,o=o+Math.imul(H,yt)|0,i=i+Math.imul(H,xt)|0,i=i+Math.imul(V,yt)|0,a=a+Math.imul(V,xt)|0;var ii=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(ii>>>26)|0,ii&=67108863,o=Math.imul(it,mt),i=Math.imul(it,pt),i=i+Math.imul(rt,mt)|0,a=Math.imul(rt,pt),o=o+Math.imul(j,ct)|0,i=i+Math.imul(j,vt)|0,i=i+Math.imul(tt,ct)|0,a=a+Math.imul(tt,vt)|0,o=o+Math.imul($,gt)|0,i=i+Math.imul($,_t)|0,i=i+Math.imul(G,gt)|0,a=a+Math.imul(G,_t)|0,o=o+Math.imul(X,Mt)|0,i=i+Math.imul(X,wt)|0,i=i+Math.imul(Y,Mt)|0,a=a+Math.imul(Y,wt)|0,o=o+Math.imul(J,yt)|0,i=i+Math.imul(J,xt)|0,i=i+Math.imul(Q,yt)|0,a=a+Math.imul(Q,xt)|0;var ri=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(ri>>>26)|0,ri&=67108863,o=Math.imul(it,ct),i=Math.imul(it,vt),i=i+Math.imul(rt,ct)|0,a=Math.imul(rt,vt),o=o+Math.imul(j,gt)|0,i=i+Math.imul(j,_t)|0,i=i+Math.imul(tt,gt)|0,a=a+Math.imul(tt,_t)|0,o=o+Math.imul($,Mt)|0,i=i+Math.imul($,wt)|0,i=i+Math.imul(G,Mt)|0,a=a+Math.imul(G,wt)|0,o=o+Math.imul(X,yt)|0,i=i+Math.imul(X,xt)|0,i=i+Math.imul(Y,yt)|0,a=a+Math.imul(Y,xt)|0;var ei=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(ei>>>26)|0,ei&=67108863,o=Math.imul(it,gt),i=Math.imul(it,_t),i=i+Math.imul(rt,gt)|0,a=Math.imul(rt,_t),o=o+Math.imul(j,Mt)|0,i=i+Math.imul(j,wt)|0,i=i+Math.imul(tt,Mt)|0,a=a+Math.imul(tt,wt)|0,o=o+Math.imul($,yt)|0,i=i+Math.imul($,xt)|0,i=i+Math.imul(G,yt)|0,a=a+Math.imul(G,xt)|0;var ni=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(ni>>>26)|0,ni&=67108863,o=Math.imul(it,Mt),i=Math.imul(it,wt),i=i+Math.imul(rt,Mt)|0,a=Math.imul(rt,wt),o=o+Math.imul(j,yt)|0,i=i+Math.imul(j,xt)|0,i=i+Math.imul(tt,yt)|0,a=a+Math.imul(tt,xt)|0;var oi=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(oi>>>26)|0,oi&=67108863,o=Math.imul(it,yt),i=Math.imul(it,xt),i=i+Math.imul(rt,yt)|0,a=Math.imul(rt,xt);var si=(u+o|0)+((i&8191)<<13)|0;return u=(a+(i>>>13)|0)+(si>>>26)|0,si&=67108863,l[0]=Wt,l[1]=Ut,l[2]=zt,l[3]=Ht,l[4]=Vt,l[5]=Jt,l[6]=Qt,l[7]=Xt,l[8]=Yt,l[9]=$t,l[10]=Gt,l[11]=jt,l[12]=ti,l[13]=ii,l[14]=ri,l[15]=ei,l[16]=ni,l[17]=oi,l[18]=si,u!==0&&(l[19]=u,e.length++),e};Math.imul||(N=bt);function P(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,x=v&67108863;l=l+(v/67108864|0)|0,x=x+u|0,u=x&67108863,l=l+(x>>>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 L(h,t,r){return P(h,t,r)}f.prototype.mulTo=function(t,r){var e,n=this.length+t.length;return this.length===10&&t.length===10?e=N(this,t,r):n<63?e=bt(this,t,r):n<1024?e=P(this,t,r):e=L(this,t,r),e};function q(h,t){this.x=h,this.y=t}q.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},q.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},q.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]]},q.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,x=0;x<u;x++){var O=e[p+x],I=n[p+x],Ot=e[p+x+u],F=n[p+x+u],Z=c*Ot-v*F;F=c*F+v*Ot,Ot=Z,e[p+x]=O+Ot,n[p+x]=I+F,e[p+x+u]=O-Ot,n[p+x+u]=I-F,x!==o&&(Z=i*c-a*v,v=i*v+a*c,c=Z)}},q.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},q.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}},q.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},q.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)},q.prototype.stub=function(t){for(var r=new Array(t),e=0;e<t;e++)r[e]=0;return r},q.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 x=0;x<n;x++){var O=o[x]*p[x]-i[x]*c[x];i[x]=o[x]*c[x]+i[x]*p[x],o[x]=O}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),L(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,x=1;(e.words[0]&x)===0&&v<26;++v,x<<=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 C(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 T={k256:null,p224:null,p192:null,p25519:null};function R(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()}R.prototype._tmp=function(){var t=new f(null);return t.words=new Array(Math.ceil(this.n/13)),t},R.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},R.prototype.split=function(t,r){t.iushrn(this.n,0,r)},R.prototype.imulK=function(t){return t.imul(this.k)};function Dt(){R.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}g(Dt,R),Dt.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},Dt.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(){R.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}g(St,R);function Rt(){R.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}g(Rt,R);function qt(){R.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}g(qt,R),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(T[t])return T[t];var r;if(t==="k256")r=new Dt;else if(t==="p224")r=new St;else if(t==="p192")r=new Rt;else if(t==="p25519")r=new qt;else throw new Error("Unknown prime "+t);return T[t]=r,r};function C(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}C.prototype._verify1=function(t){d(t.negative===0,"red works only with positives"),d(t.red,"red works only with red numbers")},C.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")},C.prototype.imod=function(t){return this.prime?this.prime.ireduce(t)._forceRed(this):(b(t,t.umod(this.m)._forceRed(this)),t)},C.prototype.neg=function(t){return t.isZero()?t.clone():this.m.sub(t)._forceRed(this)},C.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)},C.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},C.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)},C.prototype.isub=function(t,r){this._verify2(t,r);var e=t.isub(r);return e.cmpn(0)<0&&e.iadd(this.m),e},C.prototype.shl=function(t,r){return this._verify1(t),this.imod(t.ushln(r))},C.prototype.imul=function(t,r){return this._verify2(t,r),this.imod(t.imul(r))},C.prototype.mul=function(t,r){return this._verify2(t,r),this.imod(t.mul(r))},C.prototype.isqr=function(t){return this.imul(t,t.clone())},C.prototype.sqr=function(t){return this.mul(t,t)},C.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 x=c,O=0;x.cmp(l)!==0;O++)x=x.redSqr();d(O<v);var I=this.pow(a,new f(1).iushln(v-O-1));p=p.redMul(I),a=I.redSqr(),c=c.redMul(a),v=O}return p},C.prototype.invm=function(t){var r=t._invmp(this.m);return r.negative!==0?(r.negative=0,this.imod(r).redNeg()):this.imod(r)},C.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},C.prototype.convertTo=function(t){var r=t.umod(this.m);return r===t?r.clone():r},C.prototype.convertFrom=function(t){var r=t.clone();return r.red=null,r},f.mont=function(t){return new Bt(t)};function Bt(h){C.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(Bt,C),Bt.prototype.convertTo=function(t){return this.imod(t.ushln(this.shift))},Bt.prototype.convertFrom=function(t){var r=this.imod(t.mul(this.rinv));return r.red=null,r},Bt.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)},Bt.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)},Bt.prototype.invm=function(t){var r=this.imod(t._invmp(this.m).mul(this.r2));return r._forceRed(this)}})(typeof fi>"u"||fi,Ii)});var Gi={};Qi(Gi,{CetusDlmmZapSDK:()=>Tt,ZapError:()=>Lt,ZapErrorCode:()=>ui,ZapModule:()=>Pt,calcExactSwapAmount:()=>pi,calculateLiquidityAmountEnough:()=>mi,calculateLiquidityAmountSide:()=>Yi,default:()=>$i,defaultSwapSlippage:()=>Zt,handleError:()=>di,handleMessageError:()=>Ct,zapMainnet:()=>ai,zapTestnet:()=>hi});module.exports=Xi(Gi);var kt=require("@cetusprotocol/aggregator-sdk"),vi=require("@mysten/sui/client"),gi=require("@mysten/sui/utils"),Zi=require("@cetusprotocol/dlmm-sdk"),Ni=require("@cetusprotocol/common-sdk");var Ft=require("@cetusprotocol/common-sdk"),ai={env:"mainnet",full_rpc_url:Ft.FullRpcUrlMainnet,graph_rpc_url:Ft.GraphRpcUrlMainnet,aggregator_url:"https://api-sui.cetus.zone/router_v3",providers:[]};var Kt=require("@cetusprotocol/common-sdk"),hi={env:"testnet",full_rpc_url:Kt.FullRpcUrlTestnet,graph_rpc_url:Kt.GraphRpcUrlTestnet,aggregator_url:"https://api-sui.devcetus.com/router_v3",providers:["CETUS","DEEPBOOK","KRIYA","KRIYAV3","FLOWX","FLOWXV3","AFTERMATH","TURBOS","HAEDAL","VOLO","AFSUI"]};var ci=require("@mysten/sui/transactions"),Ki=Ti(li()),M=require("@cetusprotocol/common-sdk");var Fi=require("@cetusprotocol/common-sdk"),ui=(_=>(_.UnsupportedDepositMode="UnsupportedDepositMode",_.PositionIdUndefined="PositionIdUndefined",_.ParameterError="ParameterError",_.ReachMaxIterations="ReachMaxIterations",_.BestLiquidityIsZero="BestLiquidityIsZero",_.SwapAmountError="SwapAmountError",_.AggregatorError="AggregatorError",_))(ui||{}),Lt=class extends Fi.BaseError{constructor(m,d,g){super(m,d||"UnknownError",g)}static isZapErrorCode(m,d){return this.isErrorCode(m,d)}},di=(S,m,d)=>{throw new Lt(m.message,S,d)},Ct=(S,m,d)=>{throw new Lt(m,S,d)};var Zt=.005;var Et=Ti(li()),At=require("@cetusprotocol/common-sdk");function mi(S,m,d,g,f,A,w){let _=w?new Et.default(S):new Et.default(m),y=At.ClmmPoolUtil.estLiquidityAndCoinAmountFromOneAmounts(g,f,_,w,!0,A,new Et.default(d)),b=w?_.toString():y.coin_amount_a.toString(),D=w?y.coin_amount_b.toString():_.toString(),B=(0,At.d)(S).sub(b),K=(0,At.d)(m).sub(D);return{is_enough_amount:w?K.gte(0):B.gte(0),use_amount_a:b,use_amount_b:D,liquidity:y.liquidity_amount,amount_limit_a:y.coin_amount_limit_a,amount_limit_b:y.coin_amount_limit_b,remain_amount:w?K:B}}function Yi(S,m,d,g,f,A,w){let _=At.TickMath.sqrtPriceX64ToTickIndex(new Et.default(d)),y=w;_<g?y=!0:_>f&&(y=!1);let b=mi(S,m,d,g,f,A,y);return b.is_enough_amount||(y=!y,b=mi(S,m,d,g,f,A,y)),{fix_liquidity_amount_a:y,...b}}function pi(S,m,d,g){let f=(0,At.d)(S),A=(0,At.d)(d),w=(0,At.d)(g),_=f,y=m?f:(0,At.d)(0),b=m?(0,At.d)(0):f,D=w,B=(0,At.d)(w).mul(.999),K=(0,At.d)(0),k=_,E=null;function W(P){if(m){let L=y.minus(P),q=b.plus(P.mul(A));return{A:L,B:q,R:q.div(L)}}else{let L=y.plus(P.mul(A)),q=b.minus(P);return{A:L,B:q,R:L.div(q)}}}for(let P=0;P<120;P++){let L=K.plus(k).div(2),{A:q,B:T,R}=W(L);if(q.lte(0)||T.lte(0)){k=L;continue}if(R.gt(D))k=L;else if(R.lt(B))K=L;else{if(E=L,(0,At.d)(B).eq(0))break;k=L}}if(!E)return{swap_amount:"0",final_amount_a:y.toFixed(0),final_amount_b:b.toFixed(0)};let{A:bt,B:N}=W(E);return{swap_amount:E.toFixed(0),final_amount_a:bt.toFixed(0),final_amount_b:N.toFixed(0)}}var Nt=require("@cetusprotocol/dlmm-sdk"),Pt=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:A,pool_id:w,active_id:_,lower_bin_id:y,upper_bin_id:b,active_bin_of_pool:D,strategy_type:B}=m,K=await this._sdk.DlmmSDK.Pool.getPool(w,!1),k,E=g,W=f;return _>b?(E=!1,g?(k=await this.findRouters(K.coin_type_a,K.coin_type_b,f),W=(0,M.d)(k.swap_out_amount).mul(1-.001).toFixed(0)):W=f):_<y&&(E=!0,g?W=f:(k=await this.findRouters(K.coin_type_b,K.coin_type_a,f),W=(0,M.d)(k.swap_out_amount).mul(1-.001).toFixed(0))),{liquidity_info:await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:w,coin_amount:W,fix_amount_a:E,active_id:_,bin_step:A,lower_bin_id:y,upper_bin_id:b,active_bin_of_pool:D,strategy_type:B}),swap_result:k}}async calculateBalanceSwapAmount(m,d){let{fix_amount_a:g,coin_amount:f}=d,{bin_step:A,pool_id:w,active_id:_,lower_bin_id:y,upper_bin_id:b,active_bin_of_pool:D,strategy_type:B}=m;if(_>b||_<y)return this.calculateBalanceSwapAmountWithoutActiveId(m,d);let K=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:w,coin_amount:f,fix_amount_a:g,active_id:_,bin_step:A,lower_bin_id:y,upper_bin_id:b,active_bin_of_pool:D,strategy_type:B}),{amount_a:k,amount_b:E}=K,W=g?(0,M.d)(E).div(k):(0,M.d)(k).div(E),bt=g?Nt.BinUtils.getPricePerLamportFromBinId(_,A):(0,M.d)(1).div(Nt.BinUtils.getPricePerLamportFromBinId(_,A)).toString(),N=await this._sdk.DlmmSDK.Pool.getPool(w,!1);try{let t=await this.findRouters(g?N.coin_type_a:N.coin_type_b,g?N.coin_type_b:N.coin_type_a,f);t&&(bt=(0,M.d)(t.swap_out_amount).div(t.swap_in_amount).toString())}catch{}let{swap_amount:P,final_amount_a:L,final_amount_b:q}=pi(f,g,bt,W.toString());console.log("\u{1F680} ~ ZapModule ~ swap_amount:",{swap_amount:P,final_amount_a:L,final_amount_b:q,target_rate:W,real_price:bt,coin_amount:f,fix_amount_a:g});let T=P||"0",R,Dt,St,Rt=0,qt=.02,C,Bt,h;do{let t=(0,M.d)(f).sub(T).toFixed(0);St=await this.findRouters(g?N.coin_type_a:N.coin_type_b,g?N.coin_type_b:N.coin_type_a,T);let r=(0,M.d)(St.swap_out_amount).toFixed(0),e=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:w,coin_amount:t,fix_amount_a:g,active_id:_,bin_step:A,lower_bin_id:y,upper_bin_id:b,active_bin_of_pool:D,strategy_type:B}),{amount_a:n,amount_b:s}=e,l=g?s:n,u=(0,M.d)(r).sub(l).div(r);if(u.gt(0)&&(h===void 0||u.lt(h))&&(C=St,Bt=e,h=u),(0,M.d)(r).gt(l))if((0,M.d)(u).gt(qt))console.log("calculateBalanceSwapAmount -: ",{swap_amount_in:T,real_remain_rate:u.toString(),cached_real_remain_rate:h?.toString(),temp_swap_result:St,deposit_amount_other:l,count:Rt}),T=(0,M.d)(T).mul(1-.01).toFixed(0),Rt++;else{R=St,Dt=e;break}else T=(0,M.d)(T).mul(1+.01).toFixed(0),T=Math.min(Number((0,M.d)(f).mul(1-1e-4).toFixed(0)),Number(T)).toString(),console.log("calculateBalanceSwapAmount +: ",{swap_amount_in:T,real_remain_rate:u.toString(),cached_real_remain_rate:h?.toString(),temp_swap_result:St,deposit_amount_other:l,count:Rt}),Rt++;if(Rt>5)break}while(!R&&(0,M.d)(T).gt(0));if(R===void 0&&C&&(R=C,Dt=Bt),R===void 0&&St){let{swap_out_amount:t}=St,r=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:w,coin_amount:(0,M.d)(t).sub(1-.001).toFixed(0),fix_amount_a:!g,active_id:_,bin_step:A,lower_bin_id:y,upper_bin_id:b,active_bin_of_pool:D,strategy_type:B});R=St,Dt=r}return{swap_result:R,liquidity_info:Dt}}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:A,swap_result:w}=await this.calculateBalanceSwapAmount(m,d);return A===void 0?Ct("SwapAmountError","Best liquidity info is undefined",{[M.DETAILS_KEYS.METHOD_NAME]:"preCalculateDepositAmount"}):{bin_infos:A,swap_result:w,fix_amount_a:g,coin_amount:f}}async buildDepositPayload(m,d){let{swap_slippage:g=Zt,bin_step:f,pool_id:A,active_id:w,lower_bin_id:_,upper_bin_id:y,strategy_type:b,deposit_obj:D,slippage:B,pos_obj:K}=m,{fix_amount_a:k,coin_amount:E,swap_result:W,bin_infos:bt}=D;d=d||new ci.Transaction;let N=await this._sdk.DlmmSDK.Pool.getPool(A,!1),{coin_type_a:P,coin_type_b:L}=N,q,T;if(W){let R=k?P:L,{swap_in_amount:Dt,route_obj:St}=W,Rt=M.CoinAssist.buildCoinWithBalance(BigInt(Dt),R,d),qt={router:St,slippage:g,txb:d,inputCoin:Rt},C=await this._sdk.AggregatorClient.fixableRouterSwapV3(qt);k?(q=M.CoinAssist.buildCoinWithBalance(BigInt(bt.amount_a),P,d),T=C):(q=C,T=M.CoinAssist.buildCoinWithBalance(BigInt(bt.amount_b),L,d))}if(K){let R={pool_id:A,bin_infos:bt,coin_type_a:P,coin_type_b:L,active_id:w,strategy_type:b,max_price_slippage:B,bin_step:f,use_bin_infos:!1,position_id:K.pos_id,collect_fee:K.collect_fee,reward_coins:K.collect_rewarder_types,coin_object_id_a:q,coin_object_id_b:T};this._sdk.DlmmSDK.Position.addLiquidityPayload(R,d)}else{let R={pool_id:A,bin_infos:bt,coin_type_a:P,coin_type_b:L,lower_bin_id:_,upper_bin_id:y,active_id:w,strategy_type:b,use_bin_infos:!1,max_price_slippage:B,bin_step:f,coin_object_id_a:q,coin_object_id_b:T};this._sdk.DlmmSDK.Position.addLiquidityPayload(R,d)}return d}async findRouters(m,d,g){let{providers:f}=this._sdk.sdkOptions,A=this._sdk.AggregatorClient;if((0,M.d)(g).lt(1))return Ct("SwapAmountError","Swap amount is less than the minimum precision",{[M.DETAILS_KEYS.METHOD_NAME]:"findRouters"});try{let w={from:m,target:d,amount:new Ki.default(g),byAmountIn:!0,depth:3,providers:f},_=await A.findRouters(w);if(_?.error)return Ct("AggregatorError",`Aggregator findRouters error: ${_?.error}`,{[M.DETAILS_KEYS.METHOD_NAME]:"findRouters",[M.DETAILS_KEYS.REQUEST_PARAMS]:w});if(!_?.paths||_?.paths?.length===0)return Ct("AggregatorError","Aggregator findRouters error: no router",{[M.DETAILS_KEYS.METHOD_NAME]:"findRouters",[M.DETAILS_KEYS.REQUEST_PARAMS]:w});let y=_.amountIn.toString(),b=_.amountOut.toString();return{swap_in_amount:y,swap_out_amount:b,route_obj:_}}catch(w){return console.log("\u{1F680} ~ ZapModule ~ error:",JSON.stringify(w,null,2)),di("AggregatorError",w,{[M.DETAILS_KEYS.METHOD_NAME]:"swapInPools"})}}calculateZapOutAvailableAmount(m){let{is_receive_coin_a:d,mode:g,active_id:f,bin_step:A,remove_bin_range:w}=m,_=Nt.BinUtils.getPricePerLamportFromBinId(f,A),y,b="0",D="0",B="0";w.forEach(E=>{E.bin_id===f&&(y=E),b=(0,M.d)(b).add(E.amount_a).toFixed(0),D=(0,M.d)(D).add(E.amount_b).toFixed(0)});let K=(0,M.d)(b).sub(y?.amount_a||"0").toFixed(0),k=(0,M.d)(D).sub(y?.amount_b||"0").toFixed(0);return g==="OnlyCoinA"?d?B=K:B=(0,M.d)(b).mul(_).toFixed(0):g==="OnlyCoinB"?(D=k,d?B=(0,M.d)(D).div(_).toFixed(0):B=D):g==="Both"&&(d?B=(0,M.d)(D).div(_).add(b).toFixed(0):B=(0,M.d)(b).mul(_).add(D).toFixed(0)),{available_amount:B,user_total_amount_a:b,user_total_amount_b:D,active_bin:y,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:A,mode:w,expected_receive_amount:_,active_id:y}=m,{available_amount:b,user_total_amount_a:D,user_total_amount_b:B,active_bin:K}=this.calculateZapOutAvailableAmount(m);if((0,M.d)(b).lt((0,M.d)(_)))return Ct("SwapAmountError",`Available amount is less than the expected receive amount: ${b} < ${_}`,{[M.DETAILS_KEYS.METHOD_NAME]:"preCalculateWithdrawAmount",[M.DETAILS_KEYS.REQUEST_PARAMS]:m});let k=(0,M.d)(_).div(b),E;if(w==="Both"){let P=(0,M.d)(D).gt(0),L=P?(0,M.d)(D).mul(k).toFixed(0):(0,M.d)(B).mul(k).toFixed(0);E=this._sdk.DlmmSDK.Position.calculateRemoveLiquidityInfo({bins:g,active_id:y,fix_amount_a:P,coin_amount:L})}else{let P=w==="OnlyCoinA"?(0,M.d)(D).mul(k).toFixed(0):(0,M.d)(B).mul(k).toFixed(0);E=this._sdk.DlmmSDK.Position.calculateRemoveLiquidityInfo({bins:g,active_id:y,is_only_a:w==="OnlyCoinA",coin_amount:P})}let{amount_a:W,amount_b:bt}=E,N;return d&&(0,M.d)(bt).gt(0)&&(N=await this.findRouters(A,f,bt)),!d&&(0,M.d)(W).gt(0)&&(N=await this.findRouters(f,A,W)),{remove_liquidity_info:E,swap_result:N,mode:w,is_receive_coin_a:d,expected_receive_amount:_,remove_percent:k.toString()}}async buildWithdrawPayload(m){let d=new ci.Transaction,{withdraw_obj:g,swap_slippage:f=Zt,bin_step:A,pool_id:w,active_id:_,collect_fee:y,reward_coins:b,coin_type_a:D,coin_type_b:B,position_id:K,slippage:k,remove_percent:E,is_close_position:W}=m,{remove_liquidity_info:bt,swap_result:N,is_receive_coin_a:P}=g,L={pool_id:w,bin_infos:bt,coin_type_a:D,coin_type_b:B,position_id:K,slippage:k,active_id:_,reward_coins:b,collect_fee:y,bin_step:A,remove_percent:Number(E)},{coin_a_obj:q,coin_b_obj:T}=W?this._sdk.DlmmSDK.Position.closePositionNoTransferPayload(L,d):this._sdk.DlmmSDK.Position.removeLiquidityNoTransferPayload(L,d);if(N){let{route_obj:R}=N,Dt={router:R,slippage:f,txb:d,inputCoin:P?T:q},St=await this._sdk.AggregatorClient.fixableRouterSwapV3(Dt);P?d.transferObjects([q,St],this._sdk.getSenderAddress()):d.transferObjects([T,St],this._sdk.getSenderAddress())}else d.transferObjects([q,T],this._sdk.getSenderAddress());return d}};var Tt=class S extends Ni.SdkWrapper{constructor(m,d){super(m),this._zapModule=new Pt(this),this._dlmmSDK=d||Zi.CetusDlmmSDK.createSDK({env:m.env,full_rpc_url:m.full_rpc_url}),this._aggregatorClient=new kt.AggregatorClient({signer:(0,gi.normalizeSuiAddress)("0x0"),client:m.sui_client||new vi.SuiClient({url:m.full_rpc_url}),env:m.env==="testnet"?kt.Env.Testnet:kt.Env.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 kt.AggregatorClient({signer:(0,gi.normalizeSuiAddress)("0x0"),client:new vi.SuiClient({url:m}),env:this._sdkOptions.env==="testnet"?kt.Env.Testnet:kt.Env.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"?S.createCustomSDK({...ai,...m},d):S.createCustomSDK({...hi,...m},d)}static createCustomSDK(m,d){return new S(m,d)}};var $i=Tt;0&&(module.exports={CetusDlmmZapSDK,ZapError,ZapErrorCode,ZapModule,calcExactSwapAmount,calculateLiquidityAmountEnough,calculateLiquidityAmountSide,defaultSwapSlippage,handleError,handleMessageError,zapMainnet,zapTestnet});
2
+ //# sourceMappingURL=index.js.map