@hyr0-xyz/program 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.
- package/README.md +152 -0
- package/dist/index.d.mts +709 -0
- package/dist/index.d.ts +709 -0
- package/dist/index.js +3985 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +3896 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +75 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,3985 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var kit = require('@solana/kit');
|
|
4
|
+
|
|
5
|
+
// src/sdk/vault.ts
|
|
6
|
+
|
|
7
|
+
// node_modules/decimal.js/decimal.mjs
|
|
8
|
+
var EXP_LIMIT = 9e15;
|
|
9
|
+
var MAX_DIGITS = 1e9;
|
|
10
|
+
var NUMERALS = "0123456789abcdef";
|
|
11
|
+
var LN10 = "2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058";
|
|
12
|
+
var PI = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789";
|
|
13
|
+
var DEFAULTS = {
|
|
14
|
+
// These values must be integers within the stated ranges (inclusive).
|
|
15
|
+
// Most of these values can be changed at run-time using the `Decimal.config` method.
|
|
16
|
+
// The maximum number of significant digits of the result of a calculation or base conversion.
|
|
17
|
+
// E.g. `Decimal.config({ precision: 20 });`
|
|
18
|
+
precision: 20,
|
|
19
|
+
// 1 to MAX_DIGITS
|
|
20
|
+
// The rounding mode used when rounding to `precision`.
|
|
21
|
+
//
|
|
22
|
+
// ROUND_UP 0 Away from zero.
|
|
23
|
+
// ROUND_DOWN 1 Towards zero.
|
|
24
|
+
// ROUND_CEIL 2 Towards +Infinity.
|
|
25
|
+
// ROUND_FLOOR 3 Towards -Infinity.
|
|
26
|
+
// ROUND_HALF_UP 4 Towards nearest neighbour. If equidistant, up.
|
|
27
|
+
// ROUND_HALF_DOWN 5 Towards nearest neighbour. If equidistant, down.
|
|
28
|
+
// ROUND_HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour.
|
|
29
|
+
// ROUND_HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity.
|
|
30
|
+
// ROUND_HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity.
|
|
31
|
+
//
|
|
32
|
+
// E.g.
|
|
33
|
+
// `Decimal.rounding = 4;`
|
|
34
|
+
// `Decimal.rounding = Decimal.ROUND_HALF_UP;`
|
|
35
|
+
rounding: 4,
|
|
36
|
+
// 0 to 8
|
|
37
|
+
// The modulo mode used when calculating the modulus: a mod n.
|
|
38
|
+
// The quotient (q = a / n) is calculated according to the corresponding rounding mode.
|
|
39
|
+
// The remainder (r) is calculated as: r = a - n * q.
|
|
40
|
+
//
|
|
41
|
+
// UP 0 The remainder is positive if the dividend is negative, else is negative.
|
|
42
|
+
// DOWN 1 The remainder has the same sign as the dividend (JavaScript %).
|
|
43
|
+
// FLOOR 3 The remainder has the same sign as the divisor (Python %).
|
|
44
|
+
// HALF_EVEN 6 The IEEE 754 remainder function.
|
|
45
|
+
// EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)). Always positive.
|
|
46
|
+
//
|
|
47
|
+
// Truncated division (1), floored division (3), the IEEE 754 remainder (6), and Euclidian
|
|
48
|
+
// division (9) are commonly used for the modulus operation. The other rounding modes can also
|
|
49
|
+
// be used, but they may not give useful results.
|
|
50
|
+
modulo: 1,
|
|
51
|
+
// 0 to 9
|
|
52
|
+
// The exponent value at and beneath which `toString` returns exponential notation.
|
|
53
|
+
// JavaScript numbers: -7
|
|
54
|
+
toExpNeg: -7,
|
|
55
|
+
// 0 to -EXP_LIMIT
|
|
56
|
+
// The exponent value at and above which `toString` returns exponential notation.
|
|
57
|
+
// JavaScript numbers: 21
|
|
58
|
+
toExpPos: 21,
|
|
59
|
+
// 0 to EXP_LIMIT
|
|
60
|
+
// The minimum exponent value, beneath which underflow to zero occurs.
|
|
61
|
+
// JavaScript numbers: -324 (5e-324)
|
|
62
|
+
minE: -EXP_LIMIT,
|
|
63
|
+
// -1 to -EXP_LIMIT
|
|
64
|
+
// The maximum exponent value, above which overflow to Infinity occurs.
|
|
65
|
+
// JavaScript numbers: 308 (1.7976931348623157e+308)
|
|
66
|
+
maxE: EXP_LIMIT,
|
|
67
|
+
// 1 to EXP_LIMIT
|
|
68
|
+
// Whether to use cryptographically-secure random number generation, if available.
|
|
69
|
+
crypto: false
|
|
70
|
+
// true/false
|
|
71
|
+
};
|
|
72
|
+
var inexact;
|
|
73
|
+
var quadrant;
|
|
74
|
+
var external = true;
|
|
75
|
+
var decimalError = "[DecimalError] ";
|
|
76
|
+
var invalidArgument = decimalError + "Invalid argument: ";
|
|
77
|
+
var precisionLimitExceeded = decimalError + "Precision limit exceeded";
|
|
78
|
+
var cryptoUnavailable = decimalError + "crypto unavailable";
|
|
79
|
+
var tag = "[object Decimal]";
|
|
80
|
+
var mathfloor = Math.floor;
|
|
81
|
+
var mathpow = Math.pow;
|
|
82
|
+
var isBinary = /^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i;
|
|
83
|
+
var isHex = /^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i;
|
|
84
|
+
var isOctal = /^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i;
|
|
85
|
+
var isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
|
|
86
|
+
var BASE = 1e7;
|
|
87
|
+
var LOG_BASE = 7;
|
|
88
|
+
var MAX_SAFE_INTEGER = 9007199254740991;
|
|
89
|
+
var LN10_PRECISION = LN10.length - 1;
|
|
90
|
+
var PI_PRECISION = PI.length - 1;
|
|
91
|
+
var P = { toStringTag: tag };
|
|
92
|
+
P.absoluteValue = P.abs = function() {
|
|
93
|
+
var x = new this.constructor(this);
|
|
94
|
+
if (x.s < 0) x.s = 1;
|
|
95
|
+
return finalise(x);
|
|
96
|
+
};
|
|
97
|
+
P.ceil = function() {
|
|
98
|
+
return finalise(new this.constructor(this), this.e + 1, 2);
|
|
99
|
+
};
|
|
100
|
+
P.clampedTo = P.clamp = function(min2, max2) {
|
|
101
|
+
var k, x = this, Ctor = x.constructor;
|
|
102
|
+
min2 = new Ctor(min2);
|
|
103
|
+
max2 = new Ctor(max2);
|
|
104
|
+
if (!min2.s || !max2.s) return new Ctor(NaN);
|
|
105
|
+
if (min2.gt(max2)) throw Error(invalidArgument + max2);
|
|
106
|
+
k = x.cmp(min2);
|
|
107
|
+
return k < 0 ? min2 : x.cmp(max2) > 0 ? max2 : new Ctor(x);
|
|
108
|
+
};
|
|
109
|
+
P.comparedTo = P.cmp = function(y) {
|
|
110
|
+
var i, j, xdL, ydL, x = this, xd = x.d, yd = (y = new x.constructor(y)).d, xs = x.s, ys = y.s;
|
|
111
|
+
if (!xd || !yd) {
|
|
112
|
+
return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1;
|
|
113
|
+
}
|
|
114
|
+
if (!xd[0] || !yd[0]) return xd[0] ? xs : yd[0] ? -ys : 0;
|
|
115
|
+
if (xs !== ys) return xs;
|
|
116
|
+
if (x.e !== y.e) return x.e > y.e ^ xs < 0 ? 1 : -1;
|
|
117
|
+
xdL = xd.length;
|
|
118
|
+
ydL = yd.length;
|
|
119
|
+
for (i = 0, j = xdL < ydL ? xdL : ydL; i < j; ++i) {
|
|
120
|
+
if (xd[i] !== yd[i]) return xd[i] > yd[i] ^ xs < 0 ? 1 : -1;
|
|
121
|
+
}
|
|
122
|
+
return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1;
|
|
123
|
+
};
|
|
124
|
+
P.cosine = P.cos = function() {
|
|
125
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
126
|
+
if (!x.d) return new Ctor(NaN);
|
|
127
|
+
if (!x.d[0]) return new Ctor(1);
|
|
128
|
+
pr = Ctor.precision;
|
|
129
|
+
rm = Ctor.rounding;
|
|
130
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
|
|
131
|
+
Ctor.rounding = 1;
|
|
132
|
+
x = cosine(Ctor, toLessThanHalfPi(Ctor, x));
|
|
133
|
+
Ctor.precision = pr;
|
|
134
|
+
Ctor.rounding = rm;
|
|
135
|
+
return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true);
|
|
136
|
+
};
|
|
137
|
+
P.cubeRoot = P.cbrt = function() {
|
|
138
|
+
var e, m, n, r, rep, s, sd, t, t3, t3plusx, x = this, Ctor = x.constructor;
|
|
139
|
+
if (!x.isFinite() || x.isZero()) return new Ctor(x);
|
|
140
|
+
external = false;
|
|
141
|
+
s = x.s * mathpow(x.s * x, 1 / 3);
|
|
142
|
+
if (!s || Math.abs(s) == 1 / 0) {
|
|
143
|
+
n = digitsToString(x.d);
|
|
144
|
+
e = x.e;
|
|
145
|
+
if (s = (e - n.length + 1) % 3) n += s == 1 || s == -2 ? "0" : "00";
|
|
146
|
+
s = mathpow(n, 1 / 3);
|
|
147
|
+
e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2));
|
|
148
|
+
if (s == 1 / 0) {
|
|
149
|
+
n = "5e" + e;
|
|
150
|
+
} else {
|
|
151
|
+
n = s.toExponential();
|
|
152
|
+
n = n.slice(0, n.indexOf("e") + 1) + e;
|
|
153
|
+
}
|
|
154
|
+
r = new Ctor(n);
|
|
155
|
+
r.s = x.s;
|
|
156
|
+
} else {
|
|
157
|
+
r = new Ctor(s.toString());
|
|
158
|
+
}
|
|
159
|
+
sd = (e = Ctor.precision) + 3;
|
|
160
|
+
for (; ; ) {
|
|
161
|
+
t = r;
|
|
162
|
+
t3 = t.times(t).times(t);
|
|
163
|
+
t3plusx = t3.plus(x);
|
|
164
|
+
r = divide(t3plusx.plus(x).times(t), t3plusx.plus(t3), sd + 2, 1);
|
|
165
|
+
if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
|
|
166
|
+
n = n.slice(sd - 3, sd + 1);
|
|
167
|
+
if (n == "9999" || !rep && n == "4999") {
|
|
168
|
+
if (!rep) {
|
|
169
|
+
finalise(t, e + 1, 0);
|
|
170
|
+
if (t.times(t).times(t).eq(x)) {
|
|
171
|
+
r = t;
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
sd += 4;
|
|
176
|
+
rep = 1;
|
|
177
|
+
} else {
|
|
178
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
|
|
179
|
+
finalise(r, e + 1, 1);
|
|
180
|
+
m = !r.times(r).times(r).eq(x);
|
|
181
|
+
}
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
external = true;
|
|
187
|
+
return finalise(r, e, Ctor.rounding, m);
|
|
188
|
+
};
|
|
189
|
+
P.decimalPlaces = P.dp = function() {
|
|
190
|
+
var w, d = this.d, n = NaN;
|
|
191
|
+
if (d) {
|
|
192
|
+
w = d.length - 1;
|
|
193
|
+
n = (w - mathfloor(this.e / LOG_BASE)) * LOG_BASE;
|
|
194
|
+
w = d[w];
|
|
195
|
+
if (w) for (; w % 10 == 0; w /= 10) n--;
|
|
196
|
+
if (n < 0) n = 0;
|
|
197
|
+
}
|
|
198
|
+
return n;
|
|
199
|
+
};
|
|
200
|
+
P.dividedBy = P.div = function(y) {
|
|
201
|
+
return divide(this, new this.constructor(y));
|
|
202
|
+
};
|
|
203
|
+
P.dividedToIntegerBy = P.divToInt = function(y) {
|
|
204
|
+
var x = this, Ctor = x.constructor;
|
|
205
|
+
return finalise(divide(x, new Ctor(y), 0, 1, 1), Ctor.precision, Ctor.rounding);
|
|
206
|
+
};
|
|
207
|
+
P.equals = P.eq = function(y) {
|
|
208
|
+
return this.cmp(y) === 0;
|
|
209
|
+
};
|
|
210
|
+
P.floor = function() {
|
|
211
|
+
return finalise(new this.constructor(this), this.e + 1, 3);
|
|
212
|
+
};
|
|
213
|
+
P.greaterThan = P.gt = function(y) {
|
|
214
|
+
return this.cmp(y) > 0;
|
|
215
|
+
};
|
|
216
|
+
P.greaterThanOrEqualTo = P.gte = function(y) {
|
|
217
|
+
var k = this.cmp(y);
|
|
218
|
+
return k == 1 || k === 0;
|
|
219
|
+
};
|
|
220
|
+
P.hyperbolicCosine = P.cosh = function() {
|
|
221
|
+
var k, n, pr, rm, len, x = this, Ctor = x.constructor, one = new Ctor(1);
|
|
222
|
+
if (!x.isFinite()) return new Ctor(x.s ? 1 / 0 : NaN);
|
|
223
|
+
if (x.isZero()) return one;
|
|
224
|
+
pr = Ctor.precision;
|
|
225
|
+
rm = Ctor.rounding;
|
|
226
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
|
|
227
|
+
Ctor.rounding = 1;
|
|
228
|
+
len = x.d.length;
|
|
229
|
+
if (len < 32) {
|
|
230
|
+
k = Math.ceil(len / 3);
|
|
231
|
+
n = (1 / tinyPow(4, k)).toString();
|
|
232
|
+
} else {
|
|
233
|
+
k = 16;
|
|
234
|
+
n = "2.3283064365386962890625e-10";
|
|
235
|
+
}
|
|
236
|
+
x = taylorSeries(Ctor, 1, x.times(n), new Ctor(1), true);
|
|
237
|
+
var cosh2_x, i = k, d8 = new Ctor(8);
|
|
238
|
+
for (; i--; ) {
|
|
239
|
+
cosh2_x = x.times(x);
|
|
240
|
+
x = one.minus(cosh2_x.times(d8.minus(cosh2_x.times(d8))));
|
|
241
|
+
}
|
|
242
|
+
return finalise(x, Ctor.precision = pr, Ctor.rounding = rm, true);
|
|
243
|
+
};
|
|
244
|
+
P.hyperbolicSine = P.sinh = function() {
|
|
245
|
+
var k, pr, rm, len, x = this, Ctor = x.constructor;
|
|
246
|
+
if (!x.isFinite() || x.isZero()) return new Ctor(x);
|
|
247
|
+
pr = Ctor.precision;
|
|
248
|
+
rm = Ctor.rounding;
|
|
249
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
|
|
250
|
+
Ctor.rounding = 1;
|
|
251
|
+
len = x.d.length;
|
|
252
|
+
if (len < 3) {
|
|
253
|
+
x = taylorSeries(Ctor, 2, x, x, true);
|
|
254
|
+
} else {
|
|
255
|
+
k = 1.4 * Math.sqrt(len);
|
|
256
|
+
k = k > 16 ? 16 : k | 0;
|
|
257
|
+
x = x.times(1 / tinyPow(5, k));
|
|
258
|
+
x = taylorSeries(Ctor, 2, x, x, true);
|
|
259
|
+
var sinh2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
|
|
260
|
+
for (; k--; ) {
|
|
261
|
+
sinh2_x = x.times(x);
|
|
262
|
+
x = x.times(d5.plus(sinh2_x.times(d16.times(sinh2_x).plus(d20))));
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
Ctor.precision = pr;
|
|
266
|
+
Ctor.rounding = rm;
|
|
267
|
+
return finalise(x, pr, rm, true);
|
|
268
|
+
};
|
|
269
|
+
P.hyperbolicTangent = P.tanh = function() {
|
|
270
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
271
|
+
if (!x.isFinite()) return new Ctor(x.s);
|
|
272
|
+
if (x.isZero()) return new Ctor(x);
|
|
273
|
+
pr = Ctor.precision;
|
|
274
|
+
rm = Ctor.rounding;
|
|
275
|
+
Ctor.precision = pr + 7;
|
|
276
|
+
Ctor.rounding = 1;
|
|
277
|
+
return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm);
|
|
278
|
+
};
|
|
279
|
+
P.inverseCosine = P.acos = function() {
|
|
280
|
+
var x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, rm = Ctor.rounding;
|
|
281
|
+
if (k !== -1) {
|
|
282
|
+
return k === 0 ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0) : new Ctor(NaN);
|
|
283
|
+
}
|
|
284
|
+
if (x.isZero()) return getPi(Ctor, pr + 4, rm).times(0.5);
|
|
285
|
+
Ctor.precision = pr + 6;
|
|
286
|
+
Ctor.rounding = 1;
|
|
287
|
+
x = new Ctor(1).minus(x).div(x.plus(1)).sqrt().atan();
|
|
288
|
+
Ctor.precision = pr;
|
|
289
|
+
Ctor.rounding = rm;
|
|
290
|
+
return x.times(2);
|
|
291
|
+
};
|
|
292
|
+
P.inverseHyperbolicCosine = P.acosh = function() {
|
|
293
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
294
|
+
if (x.lte(1)) return new Ctor(x.eq(1) ? 0 : NaN);
|
|
295
|
+
if (!x.isFinite()) return new Ctor(x);
|
|
296
|
+
pr = Ctor.precision;
|
|
297
|
+
rm = Ctor.rounding;
|
|
298
|
+
Ctor.precision = pr + Math.max(Math.abs(x.e), x.sd()) + 4;
|
|
299
|
+
Ctor.rounding = 1;
|
|
300
|
+
external = false;
|
|
301
|
+
x = x.times(x).minus(1).sqrt().plus(x);
|
|
302
|
+
external = true;
|
|
303
|
+
Ctor.precision = pr;
|
|
304
|
+
Ctor.rounding = rm;
|
|
305
|
+
return x.ln();
|
|
306
|
+
};
|
|
307
|
+
P.inverseHyperbolicSine = P.asinh = function() {
|
|
308
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
309
|
+
if (!x.isFinite() || x.isZero()) return new Ctor(x);
|
|
310
|
+
pr = Ctor.precision;
|
|
311
|
+
rm = Ctor.rounding;
|
|
312
|
+
Ctor.precision = pr + 2 * Math.max(Math.abs(x.e), x.sd()) + 6;
|
|
313
|
+
Ctor.rounding = 1;
|
|
314
|
+
external = false;
|
|
315
|
+
x = x.times(x).plus(1).sqrt().plus(x);
|
|
316
|
+
external = true;
|
|
317
|
+
Ctor.precision = pr;
|
|
318
|
+
Ctor.rounding = rm;
|
|
319
|
+
return x.ln();
|
|
320
|
+
};
|
|
321
|
+
P.inverseHyperbolicTangent = P.atanh = function() {
|
|
322
|
+
var pr, rm, wpr, xsd, x = this, Ctor = x.constructor;
|
|
323
|
+
if (!x.isFinite()) return new Ctor(NaN);
|
|
324
|
+
if (x.e >= 0) return new Ctor(x.abs().eq(1) ? x.s / 0 : x.isZero() ? x : NaN);
|
|
325
|
+
pr = Ctor.precision;
|
|
326
|
+
rm = Ctor.rounding;
|
|
327
|
+
xsd = x.sd();
|
|
328
|
+
if (Math.max(xsd, pr) < 2 * -x.e - 1) return finalise(new Ctor(x), pr, rm, true);
|
|
329
|
+
Ctor.precision = wpr = xsd - x.e;
|
|
330
|
+
x = divide(x.plus(1), new Ctor(1).minus(x), wpr + pr, 1);
|
|
331
|
+
Ctor.precision = pr + 4;
|
|
332
|
+
Ctor.rounding = 1;
|
|
333
|
+
x = x.ln();
|
|
334
|
+
Ctor.precision = pr;
|
|
335
|
+
Ctor.rounding = rm;
|
|
336
|
+
return x.times(0.5);
|
|
337
|
+
};
|
|
338
|
+
P.inverseSine = P.asin = function() {
|
|
339
|
+
var halfPi, k, pr, rm, x = this, Ctor = x.constructor;
|
|
340
|
+
if (x.isZero()) return new Ctor(x);
|
|
341
|
+
k = x.abs().cmp(1);
|
|
342
|
+
pr = Ctor.precision;
|
|
343
|
+
rm = Ctor.rounding;
|
|
344
|
+
if (k !== -1) {
|
|
345
|
+
if (k === 0) {
|
|
346
|
+
halfPi = getPi(Ctor, pr + 4, rm).times(0.5);
|
|
347
|
+
halfPi.s = x.s;
|
|
348
|
+
return halfPi;
|
|
349
|
+
}
|
|
350
|
+
return new Ctor(NaN);
|
|
351
|
+
}
|
|
352
|
+
Ctor.precision = pr + 6;
|
|
353
|
+
Ctor.rounding = 1;
|
|
354
|
+
x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan();
|
|
355
|
+
Ctor.precision = pr;
|
|
356
|
+
Ctor.rounding = rm;
|
|
357
|
+
return x.times(2);
|
|
358
|
+
};
|
|
359
|
+
P.inverseTangent = P.atan = function() {
|
|
360
|
+
var i, j, k, n, px, t, r, wpr, x2, x = this, Ctor = x.constructor, pr = Ctor.precision, rm = Ctor.rounding;
|
|
361
|
+
if (!x.isFinite()) {
|
|
362
|
+
if (!x.s) return new Ctor(NaN);
|
|
363
|
+
if (pr + 4 <= PI_PRECISION) {
|
|
364
|
+
r = getPi(Ctor, pr + 4, rm).times(0.5);
|
|
365
|
+
r.s = x.s;
|
|
366
|
+
return r;
|
|
367
|
+
}
|
|
368
|
+
} else if (x.isZero()) {
|
|
369
|
+
return new Ctor(x);
|
|
370
|
+
} else if (x.abs().eq(1) && pr + 4 <= PI_PRECISION) {
|
|
371
|
+
r = getPi(Ctor, pr + 4, rm).times(0.25);
|
|
372
|
+
r.s = x.s;
|
|
373
|
+
return r;
|
|
374
|
+
}
|
|
375
|
+
Ctor.precision = wpr = pr + 10;
|
|
376
|
+
Ctor.rounding = 1;
|
|
377
|
+
k = Math.min(28, wpr / LOG_BASE + 2 | 0);
|
|
378
|
+
for (i = k; i; --i) x = x.div(x.times(x).plus(1).sqrt().plus(1));
|
|
379
|
+
external = false;
|
|
380
|
+
j = Math.ceil(wpr / LOG_BASE);
|
|
381
|
+
n = 1;
|
|
382
|
+
x2 = x.times(x);
|
|
383
|
+
r = new Ctor(x);
|
|
384
|
+
px = x;
|
|
385
|
+
for (; i !== -1; ) {
|
|
386
|
+
px = px.times(x2);
|
|
387
|
+
t = r.minus(px.div(n += 2));
|
|
388
|
+
px = px.times(x2);
|
|
389
|
+
r = t.plus(px.div(n += 2));
|
|
390
|
+
if (r.d[j] !== void 0) for (i = j; r.d[i] === t.d[i] && i--; ) ;
|
|
391
|
+
}
|
|
392
|
+
if (k) r = r.times(2 << k - 1);
|
|
393
|
+
external = true;
|
|
394
|
+
return finalise(r, Ctor.precision = pr, Ctor.rounding = rm, true);
|
|
395
|
+
};
|
|
396
|
+
P.isFinite = function() {
|
|
397
|
+
return !!this.d;
|
|
398
|
+
};
|
|
399
|
+
P.isInteger = P.isInt = function() {
|
|
400
|
+
return !!this.d && mathfloor(this.e / LOG_BASE) > this.d.length - 2;
|
|
401
|
+
};
|
|
402
|
+
P.isNaN = function() {
|
|
403
|
+
return !this.s;
|
|
404
|
+
};
|
|
405
|
+
P.isNegative = P.isNeg = function() {
|
|
406
|
+
return this.s < 0;
|
|
407
|
+
};
|
|
408
|
+
P.isPositive = P.isPos = function() {
|
|
409
|
+
return this.s > 0;
|
|
410
|
+
};
|
|
411
|
+
P.isZero = function() {
|
|
412
|
+
return !!this.d && this.d[0] === 0;
|
|
413
|
+
};
|
|
414
|
+
P.lessThan = P.lt = function(y) {
|
|
415
|
+
return this.cmp(y) < 0;
|
|
416
|
+
};
|
|
417
|
+
P.lessThanOrEqualTo = P.lte = function(y) {
|
|
418
|
+
return this.cmp(y) < 1;
|
|
419
|
+
};
|
|
420
|
+
P.logarithm = P.log = function(base) {
|
|
421
|
+
var isBase10, d, denominator, k, inf, num, sd, r, arg = this, Ctor = arg.constructor, pr = Ctor.precision, rm = Ctor.rounding, guard = 5;
|
|
422
|
+
if (base == null) {
|
|
423
|
+
base = new Ctor(10);
|
|
424
|
+
isBase10 = true;
|
|
425
|
+
} else {
|
|
426
|
+
base = new Ctor(base);
|
|
427
|
+
d = base.d;
|
|
428
|
+
if (base.s < 0 || !d || !d[0] || base.eq(1)) return new Ctor(NaN);
|
|
429
|
+
isBase10 = base.eq(10);
|
|
430
|
+
}
|
|
431
|
+
d = arg.d;
|
|
432
|
+
if (arg.s < 0 || !d || !d[0] || arg.eq(1)) {
|
|
433
|
+
return new Ctor(d && !d[0] ? -1 / 0 : arg.s != 1 ? NaN : d ? 0 : 1 / 0);
|
|
434
|
+
}
|
|
435
|
+
if (isBase10) {
|
|
436
|
+
if (d.length > 1) {
|
|
437
|
+
inf = true;
|
|
438
|
+
} else {
|
|
439
|
+
for (k = d[0]; k % 10 === 0; ) k /= 10;
|
|
440
|
+
inf = k !== 1;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
external = false;
|
|
444
|
+
sd = pr + guard;
|
|
445
|
+
num = naturalLogarithm(arg, sd);
|
|
446
|
+
denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
|
|
447
|
+
r = divide(num, denominator, sd, 1);
|
|
448
|
+
if (checkRoundingDigits(r.d, k = pr, rm)) {
|
|
449
|
+
do {
|
|
450
|
+
sd += 10;
|
|
451
|
+
num = naturalLogarithm(arg, sd);
|
|
452
|
+
denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
|
|
453
|
+
r = divide(num, denominator, sd, 1);
|
|
454
|
+
if (!inf) {
|
|
455
|
+
if (+digitsToString(r.d).slice(k + 1, k + 15) + 1 == 1e14) {
|
|
456
|
+
r = finalise(r, pr + 1, 0);
|
|
457
|
+
}
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
} while (checkRoundingDigits(r.d, k += 10, rm));
|
|
461
|
+
}
|
|
462
|
+
external = true;
|
|
463
|
+
return finalise(r, pr, rm);
|
|
464
|
+
};
|
|
465
|
+
P.minus = P.sub = function(y) {
|
|
466
|
+
var d, e, i, j, k, len, pr, rm, xd, xe, xLTy, yd, x = this, Ctor = x.constructor;
|
|
467
|
+
y = new Ctor(y);
|
|
468
|
+
if (!x.d || !y.d) {
|
|
469
|
+
if (!x.s || !y.s) y = new Ctor(NaN);
|
|
470
|
+
else if (x.d) y.s = -y.s;
|
|
471
|
+
else y = new Ctor(y.d || x.s !== y.s ? x : NaN);
|
|
472
|
+
return y;
|
|
473
|
+
}
|
|
474
|
+
if (x.s != y.s) {
|
|
475
|
+
y.s = -y.s;
|
|
476
|
+
return x.plus(y);
|
|
477
|
+
}
|
|
478
|
+
xd = x.d;
|
|
479
|
+
yd = y.d;
|
|
480
|
+
pr = Ctor.precision;
|
|
481
|
+
rm = Ctor.rounding;
|
|
482
|
+
if (!xd[0] || !yd[0]) {
|
|
483
|
+
if (yd[0]) y.s = -y.s;
|
|
484
|
+
else if (xd[0]) y = new Ctor(x);
|
|
485
|
+
else return new Ctor(rm === 3 ? -0 : 0);
|
|
486
|
+
return external ? finalise(y, pr, rm) : y;
|
|
487
|
+
}
|
|
488
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
489
|
+
xe = mathfloor(x.e / LOG_BASE);
|
|
490
|
+
xd = xd.slice();
|
|
491
|
+
k = xe - e;
|
|
492
|
+
if (k) {
|
|
493
|
+
xLTy = k < 0;
|
|
494
|
+
if (xLTy) {
|
|
495
|
+
d = xd;
|
|
496
|
+
k = -k;
|
|
497
|
+
len = yd.length;
|
|
498
|
+
} else {
|
|
499
|
+
d = yd;
|
|
500
|
+
e = xe;
|
|
501
|
+
len = xd.length;
|
|
502
|
+
}
|
|
503
|
+
i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2;
|
|
504
|
+
if (k > i) {
|
|
505
|
+
k = i;
|
|
506
|
+
d.length = 1;
|
|
507
|
+
}
|
|
508
|
+
d.reverse();
|
|
509
|
+
for (i = k; i--; ) d.push(0);
|
|
510
|
+
d.reverse();
|
|
511
|
+
} else {
|
|
512
|
+
i = xd.length;
|
|
513
|
+
len = yd.length;
|
|
514
|
+
xLTy = i < len;
|
|
515
|
+
if (xLTy) len = i;
|
|
516
|
+
for (i = 0; i < len; i++) {
|
|
517
|
+
if (xd[i] != yd[i]) {
|
|
518
|
+
xLTy = xd[i] < yd[i];
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
k = 0;
|
|
523
|
+
}
|
|
524
|
+
if (xLTy) {
|
|
525
|
+
d = xd;
|
|
526
|
+
xd = yd;
|
|
527
|
+
yd = d;
|
|
528
|
+
y.s = -y.s;
|
|
529
|
+
}
|
|
530
|
+
len = xd.length;
|
|
531
|
+
for (i = yd.length - len; i > 0; --i) xd[len++] = 0;
|
|
532
|
+
for (i = yd.length; i > k; ) {
|
|
533
|
+
if (xd[--i] < yd[i]) {
|
|
534
|
+
for (j = i; j && xd[--j] === 0; ) xd[j] = BASE - 1;
|
|
535
|
+
--xd[j];
|
|
536
|
+
xd[i] += BASE;
|
|
537
|
+
}
|
|
538
|
+
xd[i] -= yd[i];
|
|
539
|
+
}
|
|
540
|
+
for (; xd[--len] === 0; ) xd.pop();
|
|
541
|
+
for (; xd[0] === 0; xd.shift()) --e;
|
|
542
|
+
if (!xd[0]) return new Ctor(rm === 3 ? -0 : 0);
|
|
543
|
+
y.d = xd;
|
|
544
|
+
y.e = getBase10Exponent(xd, e);
|
|
545
|
+
return external ? finalise(y, pr, rm) : y;
|
|
546
|
+
};
|
|
547
|
+
P.modulo = P.mod = function(y) {
|
|
548
|
+
var q, x = this, Ctor = x.constructor;
|
|
549
|
+
y = new Ctor(y);
|
|
550
|
+
if (!x.d || !y.s || y.d && !y.d[0]) return new Ctor(NaN);
|
|
551
|
+
if (!y.d || x.d && !x.d[0]) {
|
|
552
|
+
return finalise(new Ctor(x), Ctor.precision, Ctor.rounding);
|
|
553
|
+
}
|
|
554
|
+
external = false;
|
|
555
|
+
if (Ctor.modulo == 9) {
|
|
556
|
+
q = divide(x, y.abs(), 0, 3, 1);
|
|
557
|
+
q.s *= y.s;
|
|
558
|
+
} else {
|
|
559
|
+
q = divide(x, y, 0, Ctor.modulo, 1);
|
|
560
|
+
}
|
|
561
|
+
q = q.times(y);
|
|
562
|
+
external = true;
|
|
563
|
+
return x.minus(q);
|
|
564
|
+
};
|
|
565
|
+
P.naturalExponential = P.exp = function() {
|
|
566
|
+
return naturalExponential(this);
|
|
567
|
+
};
|
|
568
|
+
P.naturalLogarithm = P.ln = function() {
|
|
569
|
+
return naturalLogarithm(this);
|
|
570
|
+
};
|
|
571
|
+
P.negated = P.neg = function() {
|
|
572
|
+
var x = new this.constructor(this);
|
|
573
|
+
x.s = -x.s;
|
|
574
|
+
return finalise(x);
|
|
575
|
+
};
|
|
576
|
+
P.plus = P.add = function(y) {
|
|
577
|
+
var carry, d, e, i, k, len, pr, rm, xd, yd, x = this, Ctor = x.constructor;
|
|
578
|
+
y = new Ctor(y);
|
|
579
|
+
if (!x.d || !y.d) {
|
|
580
|
+
if (!x.s || !y.s) y = new Ctor(NaN);
|
|
581
|
+
else if (!x.d) y = new Ctor(y.d || x.s === y.s ? x : NaN);
|
|
582
|
+
return y;
|
|
583
|
+
}
|
|
584
|
+
if (x.s != y.s) {
|
|
585
|
+
y.s = -y.s;
|
|
586
|
+
return x.minus(y);
|
|
587
|
+
}
|
|
588
|
+
xd = x.d;
|
|
589
|
+
yd = y.d;
|
|
590
|
+
pr = Ctor.precision;
|
|
591
|
+
rm = Ctor.rounding;
|
|
592
|
+
if (!xd[0] || !yd[0]) {
|
|
593
|
+
if (!yd[0]) y = new Ctor(x);
|
|
594
|
+
return external ? finalise(y, pr, rm) : y;
|
|
595
|
+
}
|
|
596
|
+
k = mathfloor(x.e / LOG_BASE);
|
|
597
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
598
|
+
xd = xd.slice();
|
|
599
|
+
i = k - e;
|
|
600
|
+
if (i) {
|
|
601
|
+
if (i < 0) {
|
|
602
|
+
d = xd;
|
|
603
|
+
i = -i;
|
|
604
|
+
len = yd.length;
|
|
605
|
+
} else {
|
|
606
|
+
d = yd;
|
|
607
|
+
e = k;
|
|
608
|
+
len = xd.length;
|
|
609
|
+
}
|
|
610
|
+
k = Math.ceil(pr / LOG_BASE);
|
|
611
|
+
len = k > len ? k + 1 : len + 1;
|
|
612
|
+
if (i > len) {
|
|
613
|
+
i = len;
|
|
614
|
+
d.length = 1;
|
|
615
|
+
}
|
|
616
|
+
d.reverse();
|
|
617
|
+
for (; i--; ) d.push(0);
|
|
618
|
+
d.reverse();
|
|
619
|
+
}
|
|
620
|
+
len = xd.length;
|
|
621
|
+
i = yd.length;
|
|
622
|
+
if (len - i < 0) {
|
|
623
|
+
i = len;
|
|
624
|
+
d = yd;
|
|
625
|
+
yd = xd;
|
|
626
|
+
xd = d;
|
|
627
|
+
}
|
|
628
|
+
for (carry = 0; i; ) {
|
|
629
|
+
carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0;
|
|
630
|
+
xd[i] %= BASE;
|
|
631
|
+
}
|
|
632
|
+
if (carry) {
|
|
633
|
+
xd.unshift(carry);
|
|
634
|
+
++e;
|
|
635
|
+
}
|
|
636
|
+
for (len = xd.length; xd[--len] == 0; ) xd.pop();
|
|
637
|
+
y.d = xd;
|
|
638
|
+
y.e = getBase10Exponent(xd, e);
|
|
639
|
+
return external ? finalise(y, pr, rm) : y;
|
|
640
|
+
};
|
|
641
|
+
P.precision = P.sd = function(z) {
|
|
642
|
+
var k, x = this;
|
|
643
|
+
if (z !== void 0 && z !== !!z && z !== 1 && z !== 0) throw Error(invalidArgument + z);
|
|
644
|
+
if (x.d) {
|
|
645
|
+
k = getPrecision(x.d);
|
|
646
|
+
if (z && x.e + 1 > k) k = x.e + 1;
|
|
647
|
+
} else {
|
|
648
|
+
k = NaN;
|
|
649
|
+
}
|
|
650
|
+
return k;
|
|
651
|
+
};
|
|
652
|
+
P.round = function() {
|
|
653
|
+
var x = this, Ctor = x.constructor;
|
|
654
|
+
return finalise(new Ctor(x), x.e + 1, Ctor.rounding);
|
|
655
|
+
};
|
|
656
|
+
P.sine = P.sin = function() {
|
|
657
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
658
|
+
if (!x.isFinite()) return new Ctor(NaN);
|
|
659
|
+
if (x.isZero()) return new Ctor(x);
|
|
660
|
+
pr = Ctor.precision;
|
|
661
|
+
rm = Ctor.rounding;
|
|
662
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
|
|
663
|
+
Ctor.rounding = 1;
|
|
664
|
+
x = sine(Ctor, toLessThanHalfPi(Ctor, x));
|
|
665
|
+
Ctor.precision = pr;
|
|
666
|
+
Ctor.rounding = rm;
|
|
667
|
+
return finalise(quadrant > 2 ? x.neg() : x, pr, rm, true);
|
|
668
|
+
};
|
|
669
|
+
P.squareRoot = P.sqrt = function() {
|
|
670
|
+
var m, n, sd, r, rep, t, x = this, d = x.d, e = x.e, s = x.s, Ctor = x.constructor;
|
|
671
|
+
if (s !== 1 || !d || !d[0]) {
|
|
672
|
+
return new Ctor(!s || s < 0 && (!d || d[0]) ? NaN : d ? x : 1 / 0);
|
|
673
|
+
}
|
|
674
|
+
external = false;
|
|
675
|
+
s = Math.sqrt(+x);
|
|
676
|
+
if (s == 0 || s == 1 / 0) {
|
|
677
|
+
n = digitsToString(d);
|
|
678
|
+
if ((n.length + e) % 2 == 0) n += "0";
|
|
679
|
+
s = Math.sqrt(n);
|
|
680
|
+
e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
|
|
681
|
+
if (s == 1 / 0) {
|
|
682
|
+
n = "5e" + e;
|
|
683
|
+
} else {
|
|
684
|
+
n = s.toExponential();
|
|
685
|
+
n = n.slice(0, n.indexOf("e") + 1) + e;
|
|
686
|
+
}
|
|
687
|
+
r = new Ctor(n);
|
|
688
|
+
} else {
|
|
689
|
+
r = new Ctor(s.toString());
|
|
690
|
+
}
|
|
691
|
+
sd = (e = Ctor.precision) + 3;
|
|
692
|
+
for (; ; ) {
|
|
693
|
+
t = r;
|
|
694
|
+
r = t.plus(divide(x, t, sd + 2, 1)).times(0.5);
|
|
695
|
+
if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
|
|
696
|
+
n = n.slice(sd - 3, sd + 1);
|
|
697
|
+
if (n == "9999" || !rep && n == "4999") {
|
|
698
|
+
if (!rep) {
|
|
699
|
+
finalise(t, e + 1, 0);
|
|
700
|
+
if (t.times(t).eq(x)) {
|
|
701
|
+
r = t;
|
|
702
|
+
break;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
sd += 4;
|
|
706
|
+
rep = 1;
|
|
707
|
+
} else {
|
|
708
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
|
|
709
|
+
finalise(r, e + 1, 1);
|
|
710
|
+
m = !r.times(r).eq(x);
|
|
711
|
+
}
|
|
712
|
+
break;
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
external = true;
|
|
717
|
+
return finalise(r, e, Ctor.rounding, m);
|
|
718
|
+
};
|
|
719
|
+
P.tangent = P.tan = function() {
|
|
720
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
721
|
+
if (!x.isFinite()) return new Ctor(NaN);
|
|
722
|
+
if (x.isZero()) return new Ctor(x);
|
|
723
|
+
pr = Ctor.precision;
|
|
724
|
+
rm = Ctor.rounding;
|
|
725
|
+
Ctor.precision = pr + 10;
|
|
726
|
+
Ctor.rounding = 1;
|
|
727
|
+
x = x.sin();
|
|
728
|
+
x.s = 1;
|
|
729
|
+
x = divide(x, new Ctor(1).minus(x.times(x)).sqrt(), pr + 10, 0);
|
|
730
|
+
Ctor.precision = pr;
|
|
731
|
+
Ctor.rounding = rm;
|
|
732
|
+
return finalise(quadrant == 2 || quadrant == 4 ? x.neg() : x, pr, rm, true);
|
|
733
|
+
};
|
|
734
|
+
P.times = P.mul = function(y) {
|
|
735
|
+
var carry, e, i, k, r, rL, t, xdL, ydL, x = this, Ctor = x.constructor, xd = x.d, yd = (y = new Ctor(y)).d;
|
|
736
|
+
y.s *= x.s;
|
|
737
|
+
if (!xd || !xd[0] || !yd || !yd[0]) {
|
|
738
|
+
return new Ctor(!y.s || xd && !xd[0] && !yd || yd && !yd[0] && !xd ? NaN : !xd || !yd ? y.s / 0 : y.s * 0);
|
|
739
|
+
}
|
|
740
|
+
e = mathfloor(x.e / LOG_BASE) + mathfloor(y.e / LOG_BASE);
|
|
741
|
+
xdL = xd.length;
|
|
742
|
+
ydL = yd.length;
|
|
743
|
+
if (xdL < ydL) {
|
|
744
|
+
r = xd;
|
|
745
|
+
xd = yd;
|
|
746
|
+
yd = r;
|
|
747
|
+
rL = xdL;
|
|
748
|
+
xdL = ydL;
|
|
749
|
+
ydL = rL;
|
|
750
|
+
}
|
|
751
|
+
r = [];
|
|
752
|
+
rL = xdL + ydL;
|
|
753
|
+
for (i = rL; i--; ) r.push(0);
|
|
754
|
+
for (i = ydL; --i >= 0; ) {
|
|
755
|
+
carry = 0;
|
|
756
|
+
for (k = xdL + i; k > i; ) {
|
|
757
|
+
t = r[k] + yd[i] * xd[k - i - 1] + carry;
|
|
758
|
+
r[k--] = t % BASE | 0;
|
|
759
|
+
carry = t / BASE | 0;
|
|
760
|
+
}
|
|
761
|
+
r[k] = (r[k] + carry) % BASE | 0;
|
|
762
|
+
}
|
|
763
|
+
for (; !r[--rL]; ) r.pop();
|
|
764
|
+
if (carry) ++e;
|
|
765
|
+
else r.shift();
|
|
766
|
+
y.d = r;
|
|
767
|
+
y.e = getBase10Exponent(r, e);
|
|
768
|
+
return external ? finalise(y, Ctor.precision, Ctor.rounding) : y;
|
|
769
|
+
};
|
|
770
|
+
P.toBinary = function(sd, rm) {
|
|
771
|
+
return toStringBinary(this, 2, sd, rm);
|
|
772
|
+
};
|
|
773
|
+
P.toDecimalPlaces = P.toDP = function(dp, rm) {
|
|
774
|
+
var x = this, Ctor = x.constructor;
|
|
775
|
+
x = new Ctor(x);
|
|
776
|
+
if (dp === void 0) return x;
|
|
777
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
778
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
779
|
+
else checkInt32(rm, 0, 8);
|
|
780
|
+
return finalise(x, dp + x.e + 1, rm);
|
|
781
|
+
};
|
|
782
|
+
P.toExponential = function(dp, rm) {
|
|
783
|
+
var str, x = this, Ctor = x.constructor;
|
|
784
|
+
if (dp === void 0) {
|
|
785
|
+
str = finiteToString(x, true);
|
|
786
|
+
} else {
|
|
787
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
788
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
789
|
+
else checkInt32(rm, 0, 8);
|
|
790
|
+
x = finalise(new Ctor(x), dp + 1, rm);
|
|
791
|
+
str = finiteToString(x, true, dp + 1);
|
|
792
|
+
}
|
|
793
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
794
|
+
};
|
|
795
|
+
P.toFixed = function(dp, rm) {
|
|
796
|
+
var str, y, x = this, Ctor = x.constructor;
|
|
797
|
+
if (dp === void 0) {
|
|
798
|
+
str = finiteToString(x);
|
|
799
|
+
} else {
|
|
800
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
801
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
802
|
+
else checkInt32(rm, 0, 8);
|
|
803
|
+
y = finalise(new Ctor(x), dp + x.e + 1, rm);
|
|
804
|
+
str = finiteToString(y, false, dp + y.e + 1);
|
|
805
|
+
}
|
|
806
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
807
|
+
};
|
|
808
|
+
P.toFraction = function(maxD) {
|
|
809
|
+
var d, d0, d1, d2, e, k, n, n0, n1, pr, q, r, x = this, xd = x.d, Ctor = x.constructor;
|
|
810
|
+
if (!xd) return new Ctor(x);
|
|
811
|
+
n1 = d0 = new Ctor(1);
|
|
812
|
+
d1 = n0 = new Ctor(0);
|
|
813
|
+
d = new Ctor(d1);
|
|
814
|
+
e = d.e = getPrecision(xd) - x.e - 1;
|
|
815
|
+
k = e % LOG_BASE;
|
|
816
|
+
d.d[0] = mathpow(10, k < 0 ? LOG_BASE + k : k);
|
|
817
|
+
if (maxD == null) {
|
|
818
|
+
maxD = e > 0 ? d : n1;
|
|
819
|
+
} else {
|
|
820
|
+
n = new Ctor(maxD);
|
|
821
|
+
if (!n.isInt() || n.lt(n1)) throw Error(invalidArgument + n);
|
|
822
|
+
maxD = n.gt(d) ? e > 0 ? d : n1 : n;
|
|
823
|
+
}
|
|
824
|
+
external = false;
|
|
825
|
+
n = new Ctor(digitsToString(xd));
|
|
826
|
+
pr = Ctor.precision;
|
|
827
|
+
Ctor.precision = e = xd.length * LOG_BASE * 2;
|
|
828
|
+
for (; ; ) {
|
|
829
|
+
q = divide(n, d, 0, 1, 1);
|
|
830
|
+
d2 = d0.plus(q.times(d1));
|
|
831
|
+
if (d2.cmp(maxD) == 1) break;
|
|
832
|
+
d0 = d1;
|
|
833
|
+
d1 = d2;
|
|
834
|
+
d2 = n1;
|
|
835
|
+
n1 = n0.plus(q.times(d2));
|
|
836
|
+
n0 = d2;
|
|
837
|
+
d2 = d;
|
|
838
|
+
d = n.minus(q.times(d2));
|
|
839
|
+
n = d2;
|
|
840
|
+
}
|
|
841
|
+
d2 = divide(maxD.minus(d0), d1, 0, 1, 1);
|
|
842
|
+
n0 = n0.plus(d2.times(n1));
|
|
843
|
+
d0 = d0.plus(d2.times(d1));
|
|
844
|
+
n0.s = n1.s = x.s;
|
|
845
|
+
r = divide(n1, d1, e, 1).minus(x).abs().cmp(divide(n0, d0, e, 1).minus(x).abs()) < 1 ? [n1, d1] : [n0, d0];
|
|
846
|
+
Ctor.precision = pr;
|
|
847
|
+
external = true;
|
|
848
|
+
return r;
|
|
849
|
+
};
|
|
850
|
+
P.toHexadecimal = P.toHex = function(sd, rm) {
|
|
851
|
+
return toStringBinary(this, 16, sd, rm);
|
|
852
|
+
};
|
|
853
|
+
P.toNearest = function(y, rm) {
|
|
854
|
+
var x = this, Ctor = x.constructor;
|
|
855
|
+
x = new Ctor(x);
|
|
856
|
+
if (y == null) {
|
|
857
|
+
if (!x.d) return x;
|
|
858
|
+
y = new Ctor(1);
|
|
859
|
+
rm = Ctor.rounding;
|
|
860
|
+
} else {
|
|
861
|
+
y = new Ctor(y);
|
|
862
|
+
if (rm === void 0) {
|
|
863
|
+
rm = Ctor.rounding;
|
|
864
|
+
} else {
|
|
865
|
+
checkInt32(rm, 0, 8);
|
|
866
|
+
}
|
|
867
|
+
if (!x.d) return y.s ? x : y;
|
|
868
|
+
if (!y.d) {
|
|
869
|
+
if (y.s) y.s = x.s;
|
|
870
|
+
return y;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
if (y.d[0]) {
|
|
874
|
+
external = false;
|
|
875
|
+
x = divide(x, y, 0, rm, 1).times(y);
|
|
876
|
+
external = true;
|
|
877
|
+
finalise(x);
|
|
878
|
+
} else {
|
|
879
|
+
y.s = x.s;
|
|
880
|
+
x = y;
|
|
881
|
+
}
|
|
882
|
+
return x;
|
|
883
|
+
};
|
|
884
|
+
P.toNumber = function() {
|
|
885
|
+
return +this;
|
|
886
|
+
};
|
|
887
|
+
P.toOctal = function(sd, rm) {
|
|
888
|
+
return toStringBinary(this, 8, sd, rm);
|
|
889
|
+
};
|
|
890
|
+
P.toPower = P.pow = function(y) {
|
|
891
|
+
var e, k, pr, r, rm, s, x = this, Ctor = x.constructor, yn = +(y = new Ctor(y));
|
|
892
|
+
if (!x.d || !y.d || !x.d[0] || !y.d[0]) return new Ctor(mathpow(+x, yn));
|
|
893
|
+
x = new Ctor(x);
|
|
894
|
+
if (x.eq(1)) return x;
|
|
895
|
+
pr = Ctor.precision;
|
|
896
|
+
rm = Ctor.rounding;
|
|
897
|
+
if (y.eq(1)) return finalise(x, pr, rm);
|
|
898
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
899
|
+
if (e >= y.d.length - 1 && (k = yn < 0 ? -yn : yn) <= MAX_SAFE_INTEGER) {
|
|
900
|
+
r = intPow(Ctor, x, k, pr);
|
|
901
|
+
return y.s < 0 ? new Ctor(1).div(r) : finalise(r, pr, rm);
|
|
902
|
+
}
|
|
903
|
+
s = x.s;
|
|
904
|
+
if (s < 0) {
|
|
905
|
+
if (e < y.d.length - 1) return new Ctor(NaN);
|
|
906
|
+
if ((y.d[e] & 1) == 0) s = 1;
|
|
907
|
+
if (x.e == 0 && x.d[0] == 1 && x.d.length == 1) {
|
|
908
|
+
x.s = s;
|
|
909
|
+
return x;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
k = mathpow(+x, yn);
|
|
913
|
+
e = k == 0 || !isFinite(k) ? mathfloor(yn * (Math.log("0." + digitsToString(x.d)) / Math.LN10 + x.e + 1)) : new Ctor(k + "").e;
|
|
914
|
+
if (e > Ctor.maxE + 1 || e < Ctor.minE - 1) return new Ctor(e > 0 ? s / 0 : 0);
|
|
915
|
+
external = false;
|
|
916
|
+
Ctor.rounding = x.s = 1;
|
|
917
|
+
k = Math.min(12, (e + "").length);
|
|
918
|
+
r = naturalExponential(y.times(naturalLogarithm(x, pr + k)), pr);
|
|
919
|
+
if (r.d) {
|
|
920
|
+
r = finalise(r, pr + 5, 1);
|
|
921
|
+
if (checkRoundingDigits(r.d, pr, rm)) {
|
|
922
|
+
e = pr + 10;
|
|
923
|
+
r = finalise(naturalExponential(y.times(naturalLogarithm(x, e + k)), e), e + 5, 1);
|
|
924
|
+
if (+digitsToString(r.d).slice(pr + 1, pr + 15) + 1 == 1e14) {
|
|
925
|
+
r = finalise(r, pr + 1, 0);
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
r.s = s;
|
|
930
|
+
external = true;
|
|
931
|
+
Ctor.rounding = rm;
|
|
932
|
+
return finalise(r, pr, rm);
|
|
933
|
+
};
|
|
934
|
+
P.toPrecision = function(sd, rm) {
|
|
935
|
+
var str, x = this, Ctor = x.constructor;
|
|
936
|
+
if (sd === void 0) {
|
|
937
|
+
str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
938
|
+
} else {
|
|
939
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
940
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
941
|
+
else checkInt32(rm, 0, 8);
|
|
942
|
+
x = finalise(new Ctor(x), sd, rm);
|
|
943
|
+
str = finiteToString(x, sd <= x.e || x.e <= Ctor.toExpNeg, sd);
|
|
944
|
+
}
|
|
945
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
946
|
+
};
|
|
947
|
+
P.toSignificantDigits = P.toSD = function(sd, rm) {
|
|
948
|
+
var x = this, Ctor = x.constructor;
|
|
949
|
+
if (sd === void 0) {
|
|
950
|
+
sd = Ctor.precision;
|
|
951
|
+
rm = Ctor.rounding;
|
|
952
|
+
} else {
|
|
953
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
954
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
955
|
+
else checkInt32(rm, 0, 8);
|
|
956
|
+
}
|
|
957
|
+
return finalise(new Ctor(x), sd, rm);
|
|
958
|
+
};
|
|
959
|
+
P.toString = function() {
|
|
960
|
+
var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
961
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
962
|
+
};
|
|
963
|
+
P.truncated = P.trunc = function() {
|
|
964
|
+
return finalise(new this.constructor(this), this.e + 1, 1);
|
|
965
|
+
};
|
|
966
|
+
P.valueOf = P.toJSON = function() {
|
|
967
|
+
var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
968
|
+
return x.isNeg() ? "-" + str : str;
|
|
969
|
+
};
|
|
970
|
+
function digitsToString(d) {
|
|
971
|
+
var i, k, ws, indexOfLastWord = d.length - 1, str = "", w = d[0];
|
|
972
|
+
if (indexOfLastWord > 0) {
|
|
973
|
+
str += w;
|
|
974
|
+
for (i = 1; i < indexOfLastWord; i++) {
|
|
975
|
+
ws = d[i] + "";
|
|
976
|
+
k = LOG_BASE - ws.length;
|
|
977
|
+
if (k) str += getZeroString(k);
|
|
978
|
+
str += ws;
|
|
979
|
+
}
|
|
980
|
+
w = d[i];
|
|
981
|
+
ws = w + "";
|
|
982
|
+
k = LOG_BASE - ws.length;
|
|
983
|
+
if (k) str += getZeroString(k);
|
|
984
|
+
} else if (w === 0) {
|
|
985
|
+
return "0";
|
|
986
|
+
}
|
|
987
|
+
for (; w % 10 === 0; ) w /= 10;
|
|
988
|
+
return str + w;
|
|
989
|
+
}
|
|
990
|
+
function checkInt32(i, min2, max2) {
|
|
991
|
+
if (i !== ~~i || i < min2 || i > max2) {
|
|
992
|
+
throw Error(invalidArgument + i);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
function checkRoundingDigits(d, i, rm, repeating) {
|
|
996
|
+
var di, k, r, rd;
|
|
997
|
+
for (k = d[0]; k >= 10; k /= 10) --i;
|
|
998
|
+
if (--i < 0) {
|
|
999
|
+
i += LOG_BASE;
|
|
1000
|
+
di = 0;
|
|
1001
|
+
} else {
|
|
1002
|
+
di = Math.ceil((i + 1) / LOG_BASE);
|
|
1003
|
+
i %= LOG_BASE;
|
|
1004
|
+
}
|
|
1005
|
+
k = mathpow(10, LOG_BASE - i);
|
|
1006
|
+
rd = d[di] % k | 0;
|
|
1007
|
+
if (repeating == null) {
|
|
1008
|
+
if (i < 3) {
|
|
1009
|
+
if (i == 0) rd = rd / 100 | 0;
|
|
1010
|
+
else if (i == 1) rd = rd / 10 | 0;
|
|
1011
|
+
r = rm < 4 && rd == 99999 || rm > 3 && rd == 49999 || rd == 5e4 || rd == 0;
|
|
1012
|
+
} else {
|
|
1013
|
+
r = (rm < 4 && rd + 1 == k || rm > 3 && rd + 1 == k / 2) && (d[di + 1] / k / 100 | 0) == mathpow(10, i - 2) - 1 || (rd == k / 2 || rd == 0) && (d[di + 1] / k / 100 | 0) == 0;
|
|
1014
|
+
}
|
|
1015
|
+
} else {
|
|
1016
|
+
if (i < 4) {
|
|
1017
|
+
if (i == 0) rd = rd / 1e3 | 0;
|
|
1018
|
+
else if (i == 1) rd = rd / 100 | 0;
|
|
1019
|
+
else if (i == 2) rd = rd / 10 | 0;
|
|
1020
|
+
r = (repeating || rm < 4) && rd == 9999 || !repeating && rm > 3 && rd == 4999;
|
|
1021
|
+
} else {
|
|
1022
|
+
r = ((repeating || rm < 4) && rd + 1 == k || !repeating && rm > 3 && rd + 1 == k / 2) && (d[di + 1] / k / 1e3 | 0) == mathpow(10, i - 3) - 1;
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
return r;
|
|
1026
|
+
}
|
|
1027
|
+
function convertBase(str, baseIn, baseOut) {
|
|
1028
|
+
var j, arr = [0], arrL, i = 0, strL = str.length;
|
|
1029
|
+
for (; i < strL; ) {
|
|
1030
|
+
for (arrL = arr.length; arrL--; ) arr[arrL] *= baseIn;
|
|
1031
|
+
arr[0] += NUMERALS.indexOf(str.charAt(i++));
|
|
1032
|
+
for (j = 0; j < arr.length; j++) {
|
|
1033
|
+
if (arr[j] > baseOut - 1) {
|
|
1034
|
+
if (arr[j + 1] === void 0) arr[j + 1] = 0;
|
|
1035
|
+
arr[j + 1] += arr[j] / baseOut | 0;
|
|
1036
|
+
arr[j] %= baseOut;
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
return arr.reverse();
|
|
1041
|
+
}
|
|
1042
|
+
function cosine(Ctor, x) {
|
|
1043
|
+
var k, len, y;
|
|
1044
|
+
if (x.isZero()) return x;
|
|
1045
|
+
len = x.d.length;
|
|
1046
|
+
if (len < 32) {
|
|
1047
|
+
k = Math.ceil(len / 3);
|
|
1048
|
+
y = (1 / tinyPow(4, k)).toString();
|
|
1049
|
+
} else {
|
|
1050
|
+
k = 16;
|
|
1051
|
+
y = "2.3283064365386962890625e-10";
|
|
1052
|
+
}
|
|
1053
|
+
Ctor.precision += k;
|
|
1054
|
+
x = taylorSeries(Ctor, 1, x.times(y), new Ctor(1));
|
|
1055
|
+
for (var i = k; i--; ) {
|
|
1056
|
+
var cos2x = x.times(x);
|
|
1057
|
+
x = cos2x.times(cos2x).minus(cos2x).times(8).plus(1);
|
|
1058
|
+
}
|
|
1059
|
+
Ctor.precision -= k;
|
|
1060
|
+
return x;
|
|
1061
|
+
}
|
|
1062
|
+
var divide = /* @__PURE__ */ (function() {
|
|
1063
|
+
function multiplyInteger(x, k, base) {
|
|
1064
|
+
var temp, carry = 0, i = x.length;
|
|
1065
|
+
for (x = x.slice(); i--; ) {
|
|
1066
|
+
temp = x[i] * k + carry;
|
|
1067
|
+
x[i] = temp % base | 0;
|
|
1068
|
+
carry = temp / base | 0;
|
|
1069
|
+
}
|
|
1070
|
+
if (carry) x.unshift(carry);
|
|
1071
|
+
return x;
|
|
1072
|
+
}
|
|
1073
|
+
function compare(a, b, aL, bL) {
|
|
1074
|
+
var i, r;
|
|
1075
|
+
if (aL != bL) {
|
|
1076
|
+
r = aL > bL ? 1 : -1;
|
|
1077
|
+
} else {
|
|
1078
|
+
for (i = r = 0; i < aL; i++) {
|
|
1079
|
+
if (a[i] != b[i]) {
|
|
1080
|
+
r = a[i] > b[i] ? 1 : -1;
|
|
1081
|
+
break;
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
return r;
|
|
1086
|
+
}
|
|
1087
|
+
function subtract(a, b, aL, base) {
|
|
1088
|
+
var i = 0;
|
|
1089
|
+
for (; aL--; ) {
|
|
1090
|
+
a[aL] -= i;
|
|
1091
|
+
i = a[aL] < b[aL] ? 1 : 0;
|
|
1092
|
+
a[aL] = i * base + a[aL] - b[aL];
|
|
1093
|
+
}
|
|
1094
|
+
for (; !a[0] && a.length > 1; ) a.shift();
|
|
1095
|
+
}
|
|
1096
|
+
return function(x, y, pr, rm, dp, base) {
|
|
1097
|
+
var cmp, e, i, k, logBase, more, prod, prodL, q, qd, rem, remL, rem0, sd, t, xi, xL, yd0, yL, yz, Ctor = x.constructor, sign2 = x.s == y.s ? 1 : -1, xd = x.d, yd = y.d;
|
|
1098
|
+
if (!xd || !xd[0] || !yd || !yd[0]) {
|
|
1099
|
+
return new Ctor(
|
|
1100
|
+
// Return NaN if either NaN, or both Infinity or 0.
|
|
1101
|
+
!x.s || !y.s || (xd ? yd && xd[0] == yd[0] : !yd) ? NaN : (
|
|
1102
|
+
// Return ±0 if x is 0 or y is ±Infinity, or return ±Infinity as y is 0.
|
|
1103
|
+
xd && xd[0] == 0 || !yd ? sign2 * 0 : sign2 / 0
|
|
1104
|
+
)
|
|
1105
|
+
);
|
|
1106
|
+
}
|
|
1107
|
+
if (base) {
|
|
1108
|
+
logBase = 1;
|
|
1109
|
+
e = x.e - y.e;
|
|
1110
|
+
} else {
|
|
1111
|
+
base = BASE;
|
|
1112
|
+
logBase = LOG_BASE;
|
|
1113
|
+
e = mathfloor(x.e / logBase) - mathfloor(y.e / logBase);
|
|
1114
|
+
}
|
|
1115
|
+
yL = yd.length;
|
|
1116
|
+
xL = xd.length;
|
|
1117
|
+
q = new Ctor(sign2);
|
|
1118
|
+
qd = q.d = [];
|
|
1119
|
+
for (i = 0; yd[i] == (xd[i] || 0); i++) ;
|
|
1120
|
+
if (yd[i] > (xd[i] || 0)) e--;
|
|
1121
|
+
if (pr == null) {
|
|
1122
|
+
sd = pr = Ctor.precision;
|
|
1123
|
+
rm = Ctor.rounding;
|
|
1124
|
+
} else if (dp) {
|
|
1125
|
+
sd = pr + (x.e - y.e) + 1;
|
|
1126
|
+
} else {
|
|
1127
|
+
sd = pr;
|
|
1128
|
+
}
|
|
1129
|
+
if (sd < 0) {
|
|
1130
|
+
qd.push(1);
|
|
1131
|
+
more = true;
|
|
1132
|
+
} else {
|
|
1133
|
+
sd = sd / logBase + 2 | 0;
|
|
1134
|
+
i = 0;
|
|
1135
|
+
if (yL == 1) {
|
|
1136
|
+
k = 0;
|
|
1137
|
+
yd = yd[0];
|
|
1138
|
+
sd++;
|
|
1139
|
+
for (; (i < xL || k) && sd--; i++) {
|
|
1140
|
+
t = k * base + (xd[i] || 0);
|
|
1141
|
+
qd[i] = t / yd | 0;
|
|
1142
|
+
k = t % yd | 0;
|
|
1143
|
+
}
|
|
1144
|
+
more = k || i < xL;
|
|
1145
|
+
} else {
|
|
1146
|
+
k = base / (yd[0] + 1) | 0;
|
|
1147
|
+
if (k > 1) {
|
|
1148
|
+
yd = multiplyInteger(yd, k, base);
|
|
1149
|
+
xd = multiplyInteger(xd, k, base);
|
|
1150
|
+
yL = yd.length;
|
|
1151
|
+
xL = xd.length;
|
|
1152
|
+
}
|
|
1153
|
+
xi = yL;
|
|
1154
|
+
rem = xd.slice(0, yL);
|
|
1155
|
+
remL = rem.length;
|
|
1156
|
+
for (; remL < yL; ) rem[remL++] = 0;
|
|
1157
|
+
yz = yd.slice();
|
|
1158
|
+
yz.unshift(0);
|
|
1159
|
+
yd0 = yd[0];
|
|
1160
|
+
if (yd[1] >= base / 2) ++yd0;
|
|
1161
|
+
do {
|
|
1162
|
+
k = 0;
|
|
1163
|
+
cmp = compare(yd, rem, yL, remL);
|
|
1164
|
+
if (cmp < 0) {
|
|
1165
|
+
rem0 = rem[0];
|
|
1166
|
+
if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
|
|
1167
|
+
k = rem0 / yd0 | 0;
|
|
1168
|
+
if (k > 1) {
|
|
1169
|
+
if (k >= base) k = base - 1;
|
|
1170
|
+
prod = multiplyInteger(yd, k, base);
|
|
1171
|
+
prodL = prod.length;
|
|
1172
|
+
remL = rem.length;
|
|
1173
|
+
cmp = compare(prod, rem, prodL, remL);
|
|
1174
|
+
if (cmp == 1) {
|
|
1175
|
+
k--;
|
|
1176
|
+
subtract(prod, yL < prodL ? yz : yd, prodL, base);
|
|
1177
|
+
}
|
|
1178
|
+
} else {
|
|
1179
|
+
if (k == 0) cmp = k = 1;
|
|
1180
|
+
prod = yd.slice();
|
|
1181
|
+
}
|
|
1182
|
+
prodL = prod.length;
|
|
1183
|
+
if (prodL < remL) prod.unshift(0);
|
|
1184
|
+
subtract(rem, prod, remL, base);
|
|
1185
|
+
if (cmp == -1) {
|
|
1186
|
+
remL = rem.length;
|
|
1187
|
+
cmp = compare(yd, rem, yL, remL);
|
|
1188
|
+
if (cmp < 1) {
|
|
1189
|
+
k++;
|
|
1190
|
+
subtract(rem, yL < remL ? yz : yd, remL, base);
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
remL = rem.length;
|
|
1194
|
+
} else if (cmp === 0) {
|
|
1195
|
+
k++;
|
|
1196
|
+
rem = [0];
|
|
1197
|
+
}
|
|
1198
|
+
qd[i++] = k;
|
|
1199
|
+
if (cmp && rem[0]) {
|
|
1200
|
+
rem[remL++] = xd[xi] || 0;
|
|
1201
|
+
} else {
|
|
1202
|
+
rem = [xd[xi]];
|
|
1203
|
+
remL = 1;
|
|
1204
|
+
}
|
|
1205
|
+
} while ((xi++ < xL || rem[0] !== void 0) && sd--);
|
|
1206
|
+
more = rem[0] !== void 0;
|
|
1207
|
+
}
|
|
1208
|
+
if (!qd[0]) qd.shift();
|
|
1209
|
+
}
|
|
1210
|
+
if (logBase == 1) {
|
|
1211
|
+
q.e = e;
|
|
1212
|
+
inexact = more;
|
|
1213
|
+
} else {
|
|
1214
|
+
for (i = 1, k = qd[0]; k >= 10; k /= 10) i++;
|
|
1215
|
+
q.e = i + e * logBase - 1;
|
|
1216
|
+
finalise(q, dp ? pr + q.e + 1 : pr, rm, more);
|
|
1217
|
+
}
|
|
1218
|
+
return q;
|
|
1219
|
+
};
|
|
1220
|
+
})();
|
|
1221
|
+
function finalise(x, sd, rm, isTruncated) {
|
|
1222
|
+
var digits, i, j, k, rd, roundUp, w, xd, xdi, Ctor = x.constructor;
|
|
1223
|
+
out: if (sd != null) {
|
|
1224
|
+
xd = x.d;
|
|
1225
|
+
if (!xd) return x;
|
|
1226
|
+
for (digits = 1, k = xd[0]; k >= 10; k /= 10) digits++;
|
|
1227
|
+
i = sd - digits;
|
|
1228
|
+
if (i < 0) {
|
|
1229
|
+
i += LOG_BASE;
|
|
1230
|
+
j = sd;
|
|
1231
|
+
w = xd[xdi = 0];
|
|
1232
|
+
rd = w / mathpow(10, digits - j - 1) % 10 | 0;
|
|
1233
|
+
} else {
|
|
1234
|
+
xdi = Math.ceil((i + 1) / LOG_BASE);
|
|
1235
|
+
k = xd.length;
|
|
1236
|
+
if (xdi >= k) {
|
|
1237
|
+
if (isTruncated) {
|
|
1238
|
+
for (; k++ <= xdi; ) xd.push(0);
|
|
1239
|
+
w = rd = 0;
|
|
1240
|
+
digits = 1;
|
|
1241
|
+
i %= LOG_BASE;
|
|
1242
|
+
j = i - LOG_BASE + 1;
|
|
1243
|
+
} else {
|
|
1244
|
+
break out;
|
|
1245
|
+
}
|
|
1246
|
+
} else {
|
|
1247
|
+
w = k = xd[xdi];
|
|
1248
|
+
for (digits = 1; k >= 10; k /= 10) digits++;
|
|
1249
|
+
i %= LOG_BASE;
|
|
1250
|
+
j = i - LOG_BASE + digits;
|
|
1251
|
+
rd = j < 0 ? 0 : w / mathpow(10, digits - j - 1) % 10 | 0;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
isTruncated = isTruncated || sd < 0 || xd[xdi + 1] !== void 0 || (j < 0 ? w : w % mathpow(10, digits - j - 1));
|
|
1255
|
+
roundUp = rm < 4 ? (rd || isTruncated) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) : rd > 5 || rd == 5 && (rm == 4 || isTruncated || rm == 6 && // Check whether the digit to the left of the rounding digit is odd.
|
|
1256
|
+
(i > 0 ? j > 0 ? w / mathpow(10, digits - j) : 0 : xd[xdi - 1]) % 10 & 1 || rm == (x.s < 0 ? 8 : 7));
|
|
1257
|
+
if (sd < 1 || !xd[0]) {
|
|
1258
|
+
xd.length = 0;
|
|
1259
|
+
if (roundUp) {
|
|
1260
|
+
sd -= x.e + 1;
|
|
1261
|
+
xd[0] = mathpow(10, (LOG_BASE - sd % LOG_BASE) % LOG_BASE);
|
|
1262
|
+
x.e = -sd || 0;
|
|
1263
|
+
} else {
|
|
1264
|
+
xd[0] = x.e = 0;
|
|
1265
|
+
}
|
|
1266
|
+
return x;
|
|
1267
|
+
}
|
|
1268
|
+
if (i == 0) {
|
|
1269
|
+
xd.length = xdi;
|
|
1270
|
+
k = 1;
|
|
1271
|
+
xdi--;
|
|
1272
|
+
} else {
|
|
1273
|
+
xd.length = xdi + 1;
|
|
1274
|
+
k = mathpow(10, LOG_BASE - i);
|
|
1275
|
+
xd[xdi] = j > 0 ? (w / mathpow(10, digits - j) % mathpow(10, j) | 0) * k : 0;
|
|
1276
|
+
}
|
|
1277
|
+
if (roundUp) {
|
|
1278
|
+
for (; ; ) {
|
|
1279
|
+
if (xdi == 0) {
|
|
1280
|
+
for (i = 1, j = xd[0]; j >= 10; j /= 10) i++;
|
|
1281
|
+
j = xd[0] += k;
|
|
1282
|
+
for (k = 1; j >= 10; j /= 10) k++;
|
|
1283
|
+
if (i != k) {
|
|
1284
|
+
x.e++;
|
|
1285
|
+
if (xd[0] == BASE) xd[0] = 1;
|
|
1286
|
+
}
|
|
1287
|
+
break;
|
|
1288
|
+
} else {
|
|
1289
|
+
xd[xdi] += k;
|
|
1290
|
+
if (xd[xdi] != BASE) break;
|
|
1291
|
+
xd[xdi--] = 0;
|
|
1292
|
+
k = 1;
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
for (i = xd.length; xd[--i] === 0; ) xd.pop();
|
|
1297
|
+
}
|
|
1298
|
+
if (external) {
|
|
1299
|
+
if (x.e > Ctor.maxE) {
|
|
1300
|
+
x.d = null;
|
|
1301
|
+
x.e = NaN;
|
|
1302
|
+
} else if (x.e < Ctor.minE) {
|
|
1303
|
+
x.e = 0;
|
|
1304
|
+
x.d = [0];
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
return x;
|
|
1308
|
+
}
|
|
1309
|
+
function finiteToString(x, isExp, sd) {
|
|
1310
|
+
if (!x.isFinite()) return nonFiniteToString(x);
|
|
1311
|
+
var k, e = x.e, str = digitsToString(x.d), len = str.length;
|
|
1312
|
+
if (isExp) {
|
|
1313
|
+
if (sd && (k = sd - len) > 0) {
|
|
1314
|
+
str = str.charAt(0) + "." + str.slice(1) + getZeroString(k);
|
|
1315
|
+
} else if (len > 1) {
|
|
1316
|
+
str = str.charAt(0) + "." + str.slice(1);
|
|
1317
|
+
}
|
|
1318
|
+
str = str + (x.e < 0 ? "e" : "e+") + x.e;
|
|
1319
|
+
} else if (e < 0) {
|
|
1320
|
+
str = "0." + getZeroString(-e - 1) + str;
|
|
1321
|
+
if (sd && (k = sd - len) > 0) str += getZeroString(k);
|
|
1322
|
+
} else if (e >= len) {
|
|
1323
|
+
str += getZeroString(e + 1 - len);
|
|
1324
|
+
if (sd && (k = sd - e - 1) > 0) str = str + "." + getZeroString(k);
|
|
1325
|
+
} else {
|
|
1326
|
+
if ((k = e + 1) < len) str = str.slice(0, k) + "." + str.slice(k);
|
|
1327
|
+
if (sd && (k = sd - len) > 0) {
|
|
1328
|
+
if (e + 1 === len) str += ".";
|
|
1329
|
+
str += getZeroString(k);
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
return str;
|
|
1333
|
+
}
|
|
1334
|
+
function getBase10Exponent(digits, e) {
|
|
1335
|
+
var w = digits[0];
|
|
1336
|
+
for (e *= LOG_BASE; w >= 10; w /= 10) e++;
|
|
1337
|
+
return e;
|
|
1338
|
+
}
|
|
1339
|
+
function getLn10(Ctor, sd, pr) {
|
|
1340
|
+
if (sd > LN10_PRECISION) {
|
|
1341
|
+
external = true;
|
|
1342
|
+
if (pr) Ctor.precision = pr;
|
|
1343
|
+
throw Error(precisionLimitExceeded);
|
|
1344
|
+
}
|
|
1345
|
+
return finalise(new Ctor(LN10), sd, 1, true);
|
|
1346
|
+
}
|
|
1347
|
+
function getPi(Ctor, sd, rm) {
|
|
1348
|
+
if (sd > PI_PRECISION) throw Error(precisionLimitExceeded);
|
|
1349
|
+
return finalise(new Ctor(PI), sd, rm, true);
|
|
1350
|
+
}
|
|
1351
|
+
function getPrecision(digits) {
|
|
1352
|
+
var w = digits.length - 1, len = w * LOG_BASE + 1;
|
|
1353
|
+
w = digits[w];
|
|
1354
|
+
if (w) {
|
|
1355
|
+
for (; w % 10 == 0; w /= 10) len--;
|
|
1356
|
+
for (w = digits[0]; w >= 10; w /= 10) len++;
|
|
1357
|
+
}
|
|
1358
|
+
return len;
|
|
1359
|
+
}
|
|
1360
|
+
function getZeroString(k) {
|
|
1361
|
+
var zs = "";
|
|
1362
|
+
for (; k--; ) zs += "0";
|
|
1363
|
+
return zs;
|
|
1364
|
+
}
|
|
1365
|
+
function intPow(Ctor, x, n, pr) {
|
|
1366
|
+
var isTruncated, r = new Ctor(1), k = Math.ceil(pr / LOG_BASE + 4);
|
|
1367
|
+
external = false;
|
|
1368
|
+
for (; ; ) {
|
|
1369
|
+
if (n % 2) {
|
|
1370
|
+
r = r.times(x);
|
|
1371
|
+
if (truncate(r.d, k)) isTruncated = true;
|
|
1372
|
+
}
|
|
1373
|
+
n = mathfloor(n / 2);
|
|
1374
|
+
if (n === 0) {
|
|
1375
|
+
n = r.d.length - 1;
|
|
1376
|
+
if (isTruncated && r.d[n] === 0) ++r.d[n];
|
|
1377
|
+
break;
|
|
1378
|
+
}
|
|
1379
|
+
x = x.times(x);
|
|
1380
|
+
truncate(x.d, k);
|
|
1381
|
+
}
|
|
1382
|
+
external = true;
|
|
1383
|
+
return r;
|
|
1384
|
+
}
|
|
1385
|
+
function isOdd(n) {
|
|
1386
|
+
return n.d[n.d.length - 1] & 1;
|
|
1387
|
+
}
|
|
1388
|
+
function maxOrMin(Ctor, args, n) {
|
|
1389
|
+
var k, y, x = new Ctor(args[0]), i = 0;
|
|
1390
|
+
for (; ++i < args.length; ) {
|
|
1391
|
+
y = new Ctor(args[i]);
|
|
1392
|
+
if (!y.s) {
|
|
1393
|
+
x = y;
|
|
1394
|
+
break;
|
|
1395
|
+
}
|
|
1396
|
+
k = x.cmp(y);
|
|
1397
|
+
if (k === n || k === 0 && x.s === n) {
|
|
1398
|
+
x = y;
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
return x;
|
|
1402
|
+
}
|
|
1403
|
+
function naturalExponential(x, sd) {
|
|
1404
|
+
var denominator, guard, j, pow2, sum2, t, wpr, rep = 0, i = 0, k = 0, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
|
|
1405
|
+
if (!x.d || !x.d[0] || x.e > 17) {
|
|
1406
|
+
return new Ctor(x.d ? !x.d[0] ? 1 : x.s < 0 ? 0 : 1 / 0 : x.s ? x.s < 0 ? 0 : x : 0 / 0);
|
|
1407
|
+
}
|
|
1408
|
+
if (sd == null) {
|
|
1409
|
+
external = false;
|
|
1410
|
+
wpr = pr;
|
|
1411
|
+
} else {
|
|
1412
|
+
wpr = sd;
|
|
1413
|
+
}
|
|
1414
|
+
t = new Ctor(0.03125);
|
|
1415
|
+
while (x.e > -2) {
|
|
1416
|
+
x = x.times(t);
|
|
1417
|
+
k += 5;
|
|
1418
|
+
}
|
|
1419
|
+
guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0;
|
|
1420
|
+
wpr += guard;
|
|
1421
|
+
denominator = pow2 = sum2 = new Ctor(1);
|
|
1422
|
+
Ctor.precision = wpr;
|
|
1423
|
+
for (; ; ) {
|
|
1424
|
+
pow2 = finalise(pow2.times(x), wpr, 1);
|
|
1425
|
+
denominator = denominator.times(++i);
|
|
1426
|
+
t = sum2.plus(divide(pow2, denominator, wpr, 1));
|
|
1427
|
+
if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum2.d).slice(0, wpr)) {
|
|
1428
|
+
j = k;
|
|
1429
|
+
while (j--) sum2 = finalise(sum2.times(sum2), wpr, 1);
|
|
1430
|
+
if (sd == null) {
|
|
1431
|
+
if (rep < 3 && checkRoundingDigits(sum2.d, wpr - guard, rm, rep)) {
|
|
1432
|
+
Ctor.precision = wpr += 10;
|
|
1433
|
+
denominator = pow2 = t = new Ctor(1);
|
|
1434
|
+
i = 0;
|
|
1435
|
+
rep++;
|
|
1436
|
+
} else {
|
|
1437
|
+
return finalise(sum2, Ctor.precision = pr, rm, external = true);
|
|
1438
|
+
}
|
|
1439
|
+
} else {
|
|
1440
|
+
Ctor.precision = pr;
|
|
1441
|
+
return sum2;
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
sum2 = t;
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
function naturalLogarithm(y, sd) {
|
|
1448
|
+
var c, c0, denominator, e, numerator, rep, sum2, t, wpr, x1, x2, n = 1, guard = 10, x = y, xd = x.d, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
|
|
1449
|
+
if (x.s < 0 || !xd || !xd[0] || !x.e && xd[0] == 1 && xd.length == 1) {
|
|
1450
|
+
return new Ctor(xd && !xd[0] ? -1 / 0 : x.s != 1 ? NaN : xd ? 0 : x);
|
|
1451
|
+
}
|
|
1452
|
+
if (sd == null) {
|
|
1453
|
+
external = false;
|
|
1454
|
+
wpr = pr;
|
|
1455
|
+
} else {
|
|
1456
|
+
wpr = sd;
|
|
1457
|
+
}
|
|
1458
|
+
Ctor.precision = wpr += guard;
|
|
1459
|
+
c = digitsToString(xd);
|
|
1460
|
+
c0 = c.charAt(0);
|
|
1461
|
+
if (Math.abs(e = x.e) < 15e14) {
|
|
1462
|
+
while (c0 < 7 && c0 != 1 || c0 == 1 && c.charAt(1) > 3) {
|
|
1463
|
+
x = x.times(y);
|
|
1464
|
+
c = digitsToString(x.d);
|
|
1465
|
+
c0 = c.charAt(0);
|
|
1466
|
+
n++;
|
|
1467
|
+
}
|
|
1468
|
+
e = x.e;
|
|
1469
|
+
if (c0 > 1) {
|
|
1470
|
+
x = new Ctor("0." + c);
|
|
1471
|
+
e++;
|
|
1472
|
+
} else {
|
|
1473
|
+
x = new Ctor(c0 + "." + c.slice(1));
|
|
1474
|
+
}
|
|
1475
|
+
} else {
|
|
1476
|
+
t = getLn10(Ctor, wpr + 2, pr).times(e + "");
|
|
1477
|
+
x = naturalLogarithm(new Ctor(c0 + "." + c.slice(1)), wpr - guard).plus(t);
|
|
1478
|
+
Ctor.precision = pr;
|
|
1479
|
+
return sd == null ? finalise(x, pr, rm, external = true) : x;
|
|
1480
|
+
}
|
|
1481
|
+
x1 = x;
|
|
1482
|
+
sum2 = numerator = x = divide(x.minus(1), x.plus(1), wpr, 1);
|
|
1483
|
+
x2 = finalise(x.times(x), wpr, 1);
|
|
1484
|
+
denominator = 3;
|
|
1485
|
+
for (; ; ) {
|
|
1486
|
+
numerator = finalise(numerator.times(x2), wpr, 1);
|
|
1487
|
+
t = sum2.plus(divide(numerator, new Ctor(denominator), wpr, 1));
|
|
1488
|
+
if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum2.d).slice(0, wpr)) {
|
|
1489
|
+
sum2 = sum2.times(2);
|
|
1490
|
+
if (e !== 0) sum2 = sum2.plus(getLn10(Ctor, wpr + 2, pr).times(e + ""));
|
|
1491
|
+
sum2 = divide(sum2, new Ctor(n), wpr, 1);
|
|
1492
|
+
if (sd == null) {
|
|
1493
|
+
if (checkRoundingDigits(sum2.d, wpr - guard, rm, rep)) {
|
|
1494
|
+
Ctor.precision = wpr += guard;
|
|
1495
|
+
t = numerator = x = divide(x1.minus(1), x1.plus(1), wpr, 1);
|
|
1496
|
+
x2 = finalise(x.times(x), wpr, 1);
|
|
1497
|
+
denominator = rep = 1;
|
|
1498
|
+
} else {
|
|
1499
|
+
return finalise(sum2, Ctor.precision = pr, rm, external = true);
|
|
1500
|
+
}
|
|
1501
|
+
} else {
|
|
1502
|
+
Ctor.precision = pr;
|
|
1503
|
+
return sum2;
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
sum2 = t;
|
|
1507
|
+
denominator += 2;
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
function nonFiniteToString(x) {
|
|
1511
|
+
return String(x.s * x.s / 0);
|
|
1512
|
+
}
|
|
1513
|
+
function parseDecimal(x, str) {
|
|
1514
|
+
var e, i, len;
|
|
1515
|
+
if ((e = str.indexOf(".")) > -1) str = str.replace(".", "");
|
|
1516
|
+
if ((i = str.search(/e/i)) > 0) {
|
|
1517
|
+
if (e < 0) e = i;
|
|
1518
|
+
e += +str.slice(i + 1);
|
|
1519
|
+
str = str.substring(0, i);
|
|
1520
|
+
} else if (e < 0) {
|
|
1521
|
+
e = str.length;
|
|
1522
|
+
}
|
|
1523
|
+
for (i = 0; str.charCodeAt(i) === 48; i++) ;
|
|
1524
|
+
for (len = str.length; str.charCodeAt(len - 1) === 48; --len) ;
|
|
1525
|
+
str = str.slice(i, len);
|
|
1526
|
+
if (str) {
|
|
1527
|
+
len -= i;
|
|
1528
|
+
x.e = e = e - i - 1;
|
|
1529
|
+
x.d = [];
|
|
1530
|
+
i = (e + 1) % LOG_BASE;
|
|
1531
|
+
if (e < 0) i += LOG_BASE;
|
|
1532
|
+
if (i < len) {
|
|
1533
|
+
if (i) x.d.push(+str.slice(0, i));
|
|
1534
|
+
for (len -= LOG_BASE; i < len; ) x.d.push(+str.slice(i, i += LOG_BASE));
|
|
1535
|
+
str = str.slice(i);
|
|
1536
|
+
i = LOG_BASE - str.length;
|
|
1537
|
+
} else {
|
|
1538
|
+
i -= len;
|
|
1539
|
+
}
|
|
1540
|
+
for (; i--; ) str += "0";
|
|
1541
|
+
x.d.push(+str);
|
|
1542
|
+
if (external) {
|
|
1543
|
+
if (x.e > x.constructor.maxE) {
|
|
1544
|
+
x.d = null;
|
|
1545
|
+
x.e = NaN;
|
|
1546
|
+
} else if (x.e < x.constructor.minE) {
|
|
1547
|
+
x.e = 0;
|
|
1548
|
+
x.d = [0];
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
} else {
|
|
1552
|
+
x.e = 0;
|
|
1553
|
+
x.d = [0];
|
|
1554
|
+
}
|
|
1555
|
+
return x;
|
|
1556
|
+
}
|
|
1557
|
+
function parseOther(x, str) {
|
|
1558
|
+
var base, Ctor, divisor, i, isFloat, len, p, xd, xe;
|
|
1559
|
+
if (str.indexOf("_") > -1) {
|
|
1560
|
+
str = str.replace(/(\d)_(?=\d)/g, "$1");
|
|
1561
|
+
if (isDecimal.test(str)) return parseDecimal(x, str);
|
|
1562
|
+
} else if (str === "Infinity" || str === "NaN") {
|
|
1563
|
+
if (!+str) x.s = NaN;
|
|
1564
|
+
x.e = NaN;
|
|
1565
|
+
x.d = null;
|
|
1566
|
+
return x;
|
|
1567
|
+
}
|
|
1568
|
+
if (isHex.test(str)) {
|
|
1569
|
+
base = 16;
|
|
1570
|
+
str = str.toLowerCase();
|
|
1571
|
+
} else if (isBinary.test(str)) {
|
|
1572
|
+
base = 2;
|
|
1573
|
+
} else if (isOctal.test(str)) {
|
|
1574
|
+
base = 8;
|
|
1575
|
+
} else {
|
|
1576
|
+
throw Error(invalidArgument + str);
|
|
1577
|
+
}
|
|
1578
|
+
i = str.search(/p/i);
|
|
1579
|
+
if (i > 0) {
|
|
1580
|
+
p = +str.slice(i + 1);
|
|
1581
|
+
str = str.substring(2, i);
|
|
1582
|
+
} else {
|
|
1583
|
+
str = str.slice(2);
|
|
1584
|
+
}
|
|
1585
|
+
i = str.indexOf(".");
|
|
1586
|
+
isFloat = i >= 0;
|
|
1587
|
+
Ctor = x.constructor;
|
|
1588
|
+
if (isFloat) {
|
|
1589
|
+
str = str.replace(".", "");
|
|
1590
|
+
len = str.length;
|
|
1591
|
+
i = len - i;
|
|
1592
|
+
divisor = intPow(Ctor, new Ctor(base), i, i * 2);
|
|
1593
|
+
}
|
|
1594
|
+
xd = convertBase(str, base, BASE);
|
|
1595
|
+
xe = xd.length - 1;
|
|
1596
|
+
for (i = xe; xd[i] === 0; --i) xd.pop();
|
|
1597
|
+
if (i < 0) return new Ctor(x.s * 0);
|
|
1598
|
+
x.e = getBase10Exponent(xd, xe);
|
|
1599
|
+
x.d = xd;
|
|
1600
|
+
external = false;
|
|
1601
|
+
if (isFloat) x = divide(x, divisor, len * 4);
|
|
1602
|
+
if (p) x = x.times(Math.abs(p) < 54 ? mathpow(2, p) : Decimal.pow(2, p));
|
|
1603
|
+
external = true;
|
|
1604
|
+
return x;
|
|
1605
|
+
}
|
|
1606
|
+
function sine(Ctor, x) {
|
|
1607
|
+
var k, len = x.d.length;
|
|
1608
|
+
if (len < 3) {
|
|
1609
|
+
return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
|
|
1610
|
+
}
|
|
1611
|
+
k = 1.4 * Math.sqrt(len);
|
|
1612
|
+
k = k > 16 ? 16 : k | 0;
|
|
1613
|
+
x = x.times(1 / tinyPow(5, k));
|
|
1614
|
+
x = taylorSeries(Ctor, 2, x, x);
|
|
1615
|
+
var sin2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
|
|
1616
|
+
for (; k--; ) {
|
|
1617
|
+
sin2_x = x.times(x);
|
|
1618
|
+
x = x.times(d5.plus(sin2_x.times(d16.times(sin2_x).minus(d20))));
|
|
1619
|
+
}
|
|
1620
|
+
return x;
|
|
1621
|
+
}
|
|
1622
|
+
function taylorSeries(Ctor, n, x, y, isHyperbolic) {
|
|
1623
|
+
var j, t, u, x2, pr = Ctor.precision, k = Math.ceil(pr / LOG_BASE);
|
|
1624
|
+
external = false;
|
|
1625
|
+
x2 = x.times(x);
|
|
1626
|
+
u = new Ctor(y);
|
|
1627
|
+
for (; ; ) {
|
|
1628
|
+
t = divide(u.times(x2), new Ctor(n++ * n++), pr, 1);
|
|
1629
|
+
u = isHyperbolic ? y.plus(t) : y.minus(t);
|
|
1630
|
+
y = divide(t.times(x2), new Ctor(n++ * n++), pr, 1);
|
|
1631
|
+
t = u.plus(y);
|
|
1632
|
+
if (t.d[k] !== void 0) {
|
|
1633
|
+
for (j = k; t.d[j] === u.d[j] && j--; ) ;
|
|
1634
|
+
if (j == -1) break;
|
|
1635
|
+
}
|
|
1636
|
+
j = u;
|
|
1637
|
+
u = y;
|
|
1638
|
+
y = t;
|
|
1639
|
+
t = j;
|
|
1640
|
+
}
|
|
1641
|
+
external = true;
|
|
1642
|
+
t.d.length = k + 1;
|
|
1643
|
+
return t;
|
|
1644
|
+
}
|
|
1645
|
+
function tinyPow(b, e) {
|
|
1646
|
+
var n = b;
|
|
1647
|
+
while (--e) n *= b;
|
|
1648
|
+
return n;
|
|
1649
|
+
}
|
|
1650
|
+
function toLessThanHalfPi(Ctor, x) {
|
|
1651
|
+
var t, isNeg = x.s < 0, pi = getPi(Ctor, Ctor.precision, 1), halfPi = pi.times(0.5);
|
|
1652
|
+
x = x.abs();
|
|
1653
|
+
if (x.lte(halfPi)) {
|
|
1654
|
+
quadrant = isNeg ? 4 : 1;
|
|
1655
|
+
return x;
|
|
1656
|
+
}
|
|
1657
|
+
t = x.divToInt(pi);
|
|
1658
|
+
if (t.isZero()) {
|
|
1659
|
+
quadrant = isNeg ? 3 : 2;
|
|
1660
|
+
} else {
|
|
1661
|
+
x = x.minus(t.times(pi));
|
|
1662
|
+
if (x.lte(halfPi)) {
|
|
1663
|
+
quadrant = isOdd(t) ? isNeg ? 2 : 3 : isNeg ? 4 : 1;
|
|
1664
|
+
return x;
|
|
1665
|
+
}
|
|
1666
|
+
quadrant = isOdd(t) ? isNeg ? 1 : 4 : isNeg ? 3 : 2;
|
|
1667
|
+
}
|
|
1668
|
+
return x.minus(pi).abs();
|
|
1669
|
+
}
|
|
1670
|
+
function toStringBinary(x, baseOut, sd, rm) {
|
|
1671
|
+
var base, e, i, k, len, roundUp, str, xd, y, Ctor = x.constructor, isExp = sd !== void 0;
|
|
1672
|
+
if (isExp) {
|
|
1673
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
1674
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1675
|
+
else checkInt32(rm, 0, 8);
|
|
1676
|
+
} else {
|
|
1677
|
+
sd = Ctor.precision;
|
|
1678
|
+
rm = Ctor.rounding;
|
|
1679
|
+
}
|
|
1680
|
+
if (!x.isFinite()) {
|
|
1681
|
+
str = nonFiniteToString(x);
|
|
1682
|
+
} else {
|
|
1683
|
+
str = finiteToString(x);
|
|
1684
|
+
i = str.indexOf(".");
|
|
1685
|
+
if (isExp) {
|
|
1686
|
+
base = 2;
|
|
1687
|
+
if (baseOut == 16) {
|
|
1688
|
+
sd = sd * 4 - 3;
|
|
1689
|
+
} else if (baseOut == 8) {
|
|
1690
|
+
sd = sd * 3 - 2;
|
|
1691
|
+
}
|
|
1692
|
+
} else {
|
|
1693
|
+
base = baseOut;
|
|
1694
|
+
}
|
|
1695
|
+
if (i >= 0) {
|
|
1696
|
+
str = str.replace(".", "");
|
|
1697
|
+
y = new Ctor(1);
|
|
1698
|
+
y.e = str.length - i;
|
|
1699
|
+
y.d = convertBase(finiteToString(y), 10, base);
|
|
1700
|
+
y.e = y.d.length;
|
|
1701
|
+
}
|
|
1702
|
+
xd = convertBase(str, 10, base);
|
|
1703
|
+
e = len = xd.length;
|
|
1704
|
+
for (; xd[--len] == 0; ) xd.pop();
|
|
1705
|
+
if (!xd[0]) {
|
|
1706
|
+
str = isExp ? "0p+0" : "0";
|
|
1707
|
+
} else {
|
|
1708
|
+
if (i < 0) {
|
|
1709
|
+
e--;
|
|
1710
|
+
} else {
|
|
1711
|
+
x = new Ctor(x);
|
|
1712
|
+
x.d = xd;
|
|
1713
|
+
x.e = e;
|
|
1714
|
+
x = divide(x, y, sd, rm, 0, base);
|
|
1715
|
+
xd = x.d;
|
|
1716
|
+
e = x.e;
|
|
1717
|
+
roundUp = inexact;
|
|
1718
|
+
}
|
|
1719
|
+
i = xd[sd];
|
|
1720
|
+
k = base / 2;
|
|
1721
|
+
roundUp = roundUp || xd[sd + 1] !== void 0;
|
|
1722
|
+
roundUp = rm < 4 ? (i !== void 0 || roundUp) && (rm === 0 || rm === (x.s < 0 ? 3 : 2)) : i > k || i === k && (rm === 4 || roundUp || rm === 6 && xd[sd - 1] & 1 || rm === (x.s < 0 ? 8 : 7));
|
|
1723
|
+
xd.length = sd;
|
|
1724
|
+
if (roundUp) {
|
|
1725
|
+
for (; ++xd[--sd] > base - 1; ) {
|
|
1726
|
+
xd[sd] = 0;
|
|
1727
|
+
if (!sd) {
|
|
1728
|
+
++e;
|
|
1729
|
+
xd.unshift(1);
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
for (len = xd.length; !xd[len - 1]; --len) ;
|
|
1734
|
+
for (i = 0, str = ""; i < len; i++) str += NUMERALS.charAt(xd[i]);
|
|
1735
|
+
if (isExp) {
|
|
1736
|
+
if (len > 1) {
|
|
1737
|
+
if (baseOut == 16 || baseOut == 8) {
|
|
1738
|
+
i = baseOut == 16 ? 4 : 3;
|
|
1739
|
+
for (--len; len % i; len++) str += "0";
|
|
1740
|
+
xd = convertBase(str, base, baseOut);
|
|
1741
|
+
for (len = xd.length; !xd[len - 1]; --len) ;
|
|
1742
|
+
for (i = 1, str = "1."; i < len; i++) str += NUMERALS.charAt(xd[i]);
|
|
1743
|
+
} else {
|
|
1744
|
+
str = str.charAt(0) + "." + str.slice(1);
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
str = str + (e < 0 ? "p" : "p+") + e;
|
|
1748
|
+
} else if (e < 0) {
|
|
1749
|
+
for (; ++e; ) str = "0" + str;
|
|
1750
|
+
str = "0." + str;
|
|
1751
|
+
} else {
|
|
1752
|
+
if (++e > len) for (e -= len; e--; ) str += "0";
|
|
1753
|
+
else if (e < len) str = str.slice(0, e) + "." + str.slice(e);
|
|
1754
|
+
}
|
|
1755
|
+
}
|
|
1756
|
+
str = (baseOut == 16 ? "0x" : baseOut == 2 ? "0b" : baseOut == 8 ? "0o" : "") + str;
|
|
1757
|
+
}
|
|
1758
|
+
return x.s < 0 ? "-" + str : str;
|
|
1759
|
+
}
|
|
1760
|
+
function truncate(arr, len) {
|
|
1761
|
+
if (arr.length > len) {
|
|
1762
|
+
arr.length = len;
|
|
1763
|
+
return true;
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
function abs(x) {
|
|
1767
|
+
return new this(x).abs();
|
|
1768
|
+
}
|
|
1769
|
+
function acos(x) {
|
|
1770
|
+
return new this(x).acos();
|
|
1771
|
+
}
|
|
1772
|
+
function acosh(x) {
|
|
1773
|
+
return new this(x).acosh();
|
|
1774
|
+
}
|
|
1775
|
+
function add(x, y) {
|
|
1776
|
+
return new this(x).plus(y);
|
|
1777
|
+
}
|
|
1778
|
+
function asin(x) {
|
|
1779
|
+
return new this(x).asin();
|
|
1780
|
+
}
|
|
1781
|
+
function asinh(x) {
|
|
1782
|
+
return new this(x).asinh();
|
|
1783
|
+
}
|
|
1784
|
+
function atan(x) {
|
|
1785
|
+
return new this(x).atan();
|
|
1786
|
+
}
|
|
1787
|
+
function atanh(x) {
|
|
1788
|
+
return new this(x).atanh();
|
|
1789
|
+
}
|
|
1790
|
+
function atan2(y, x) {
|
|
1791
|
+
y = new this(y);
|
|
1792
|
+
x = new this(x);
|
|
1793
|
+
var r, pr = this.precision, rm = this.rounding, wpr = pr + 4;
|
|
1794
|
+
if (!y.s || !x.s) {
|
|
1795
|
+
r = new this(NaN);
|
|
1796
|
+
} else if (!y.d && !x.d) {
|
|
1797
|
+
r = getPi(this, wpr, 1).times(x.s > 0 ? 0.25 : 0.75);
|
|
1798
|
+
r.s = y.s;
|
|
1799
|
+
} else if (!x.d || y.isZero()) {
|
|
1800
|
+
r = x.s < 0 ? getPi(this, pr, rm) : new this(0);
|
|
1801
|
+
r.s = y.s;
|
|
1802
|
+
} else if (!y.d || x.isZero()) {
|
|
1803
|
+
r = getPi(this, wpr, 1).times(0.5);
|
|
1804
|
+
r.s = y.s;
|
|
1805
|
+
} else if (x.s < 0) {
|
|
1806
|
+
this.precision = wpr;
|
|
1807
|
+
this.rounding = 1;
|
|
1808
|
+
r = this.atan(divide(y, x, wpr, 1));
|
|
1809
|
+
x = getPi(this, wpr, 1);
|
|
1810
|
+
this.precision = pr;
|
|
1811
|
+
this.rounding = rm;
|
|
1812
|
+
r = y.s < 0 ? r.minus(x) : r.plus(x);
|
|
1813
|
+
} else {
|
|
1814
|
+
r = this.atan(divide(y, x, wpr, 1));
|
|
1815
|
+
}
|
|
1816
|
+
return r;
|
|
1817
|
+
}
|
|
1818
|
+
function cbrt(x) {
|
|
1819
|
+
return new this(x).cbrt();
|
|
1820
|
+
}
|
|
1821
|
+
function ceil(x) {
|
|
1822
|
+
return finalise(x = new this(x), x.e + 1, 2);
|
|
1823
|
+
}
|
|
1824
|
+
function clamp(x, min2, max2) {
|
|
1825
|
+
return new this(x).clamp(min2, max2);
|
|
1826
|
+
}
|
|
1827
|
+
function config(obj) {
|
|
1828
|
+
if (!obj || typeof obj !== "object") throw Error(decimalError + "Object expected");
|
|
1829
|
+
var i, p, v, useDefaults = obj.defaults === true, ps = [
|
|
1830
|
+
"precision",
|
|
1831
|
+
1,
|
|
1832
|
+
MAX_DIGITS,
|
|
1833
|
+
"rounding",
|
|
1834
|
+
0,
|
|
1835
|
+
8,
|
|
1836
|
+
"toExpNeg",
|
|
1837
|
+
-EXP_LIMIT,
|
|
1838
|
+
0,
|
|
1839
|
+
"toExpPos",
|
|
1840
|
+
0,
|
|
1841
|
+
EXP_LIMIT,
|
|
1842
|
+
"maxE",
|
|
1843
|
+
0,
|
|
1844
|
+
EXP_LIMIT,
|
|
1845
|
+
"minE",
|
|
1846
|
+
-EXP_LIMIT,
|
|
1847
|
+
0,
|
|
1848
|
+
"modulo",
|
|
1849
|
+
0,
|
|
1850
|
+
9
|
|
1851
|
+
];
|
|
1852
|
+
for (i = 0; i < ps.length; i += 3) {
|
|
1853
|
+
if (p = ps[i], useDefaults) this[p] = DEFAULTS[p];
|
|
1854
|
+
if ((v = obj[p]) !== void 0) {
|
|
1855
|
+
if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
|
|
1856
|
+
else throw Error(invalidArgument + p + ": " + v);
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
if (p = "crypto", useDefaults) this[p] = DEFAULTS[p];
|
|
1860
|
+
if ((v = obj[p]) !== void 0) {
|
|
1861
|
+
if (v === true || v === false || v === 0 || v === 1) {
|
|
1862
|
+
if (v) {
|
|
1863
|
+
if (typeof crypto != "undefined" && crypto && (crypto.getRandomValues || crypto.randomBytes)) {
|
|
1864
|
+
this[p] = true;
|
|
1865
|
+
} else {
|
|
1866
|
+
throw Error(cryptoUnavailable);
|
|
1867
|
+
}
|
|
1868
|
+
} else {
|
|
1869
|
+
this[p] = false;
|
|
1870
|
+
}
|
|
1871
|
+
} else {
|
|
1872
|
+
throw Error(invalidArgument + p + ": " + v);
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
return this;
|
|
1876
|
+
}
|
|
1877
|
+
function cos(x) {
|
|
1878
|
+
return new this(x).cos();
|
|
1879
|
+
}
|
|
1880
|
+
function cosh(x) {
|
|
1881
|
+
return new this(x).cosh();
|
|
1882
|
+
}
|
|
1883
|
+
function clone(obj) {
|
|
1884
|
+
var i, p, ps;
|
|
1885
|
+
function Decimal2(v) {
|
|
1886
|
+
var e, i2, t, x = this;
|
|
1887
|
+
if (!(x instanceof Decimal2)) return new Decimal2(v);
|
|
1888
|
+
x.constructor = Decimal2;
|
|
1889
|
+
if (isDecimalInstance(v)) {
|
|
1890
|
+
x.s = v.s;
|
|
1891
|
+
if (external) {
|
|
1892
|
+
if (!v.d || v.e > Decimal2.maxE) {
|
|
1893
|
+
x.e = NaN;
|
|
1894
|
+
x.d = null;
|
|
1895
|
+
} else if (v.e < Decimal2.minE) {
|
|
1896
|
+
x.e = 0;
|
|
1897
|
+
x.d = [0];
|
|
1898
|
+
} else {
|
|
1899
|
+
x.e = v.e;
|
|
1900
|
+
x.d = v.d.slice();
|
|
1901
|
+
}
|
|
1902
|
+
} else {
|
|
1903
|
+
x.e = v.e;
|
|
1904
|
+
x.d = v.d ? v.d.slice() : v.d;
|
|
1905
|
+
}
|
|
1906
|
+
return;
|
|
1907
|
+
}
|
|
1908
|
+
t = typeof v;
|
|
1909
|
+
if (t === "number") {
|
|
1910
|
+
if (v === 0) {
|
|
1911
|
+
x.s = 1 / v < 0 ? -1 : 1;
|
|
1912
|
+
x.e = 0;
|
|
1913
|
+
x.d = [0];
|
|
1914
|
+
return;
|
|
1915
|
+
}
|
|
1916
|
+
if (v < 0) {
|
|
1917
|
+
v = -v;
|
|
1918
|
+
x.s = -1;
|
|
1919
|
+
} else {
|
|
1920
|
+
x.s = 1;
|
|
1921
|
+
}
|
|
1922
|
+
if (v === ~~v && v < 1e7) {
|
|
1923
|
+
for (e = 0, i2 = v; i2 >= 10; i2 /= 10) e++;
|
|
1924
|
+
if (external) {
|
|
1925
|
+
if (e > Decimal2.maxE) {
|
|
1926
|
+
x.e = NaN;
|
|
1927
|
+
x.d = null;
|
|
1928
|
+
} else if (e < Decimal2.minE) {
|
|
1929
|
+
x.e = 0;
|
|
1930
|
+
x.d = [0];
|
|
1931
|
+
} else {
|
|
1932
|
+
x.e = e;
|
|
1933
|
+
x.d = [v];
|
|
1934
|
+
}
|
|
1935
|
+
} else {
|
|
1936
|
+
x.e = e;
|
|
1937
|
+
x.d = [v];
|
|
1938
|
+
}
|
|
1939
|
+
return;
|
|
1940
|
+
}
|
|
1941
|
+
if (v * 0 !== 0) {
|
|
1942
|
+
if (!v) x.s = NaN;
|
|
1943
|
+
x.e = NaN;
|
|
1944
|
+
x.d = null;
|
|
1945
|
+
return;
|
|
1946
|
+
}
|
|
1947
|
+
return parseDecimal(x, v.toString());
|
|
1948
|
+
}
|
|
1949
|
+
if (t === "string") {
|
|
1950
|
+
if ((i2 = v.charCodeAt(0)) === 45) {
|
|
1951
|
+
v = v.slice(1);
|
|
1952
|
+
x.s = -1;
|
|
1953
|
+
} else {
|
|
1954
|
+
if (i2 === 43) v = v.slice(1);
|
|
1955
|
+
x.s = 1;
|
|
1956
|
+
}
|
|
1957
|
+
return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v);
|
|
1958
|
+
}
|
|
1959
|
+
if (t === "bigint") {
|
|
1960
|
+
if (v < 0) {
|
|
1961
|
+
v = -v;
|
|
1962
|
+
x.s = -1;
|
|
1963
|
+
} else {
|
|
1964
|
+
x.s = 1;
|
|
1965
|
+
}
|
|
1966
|
+
return parseDecimal(x, v.toString());
|
|
1967
|
+
}
|
|
1968
|
+
throw Error(invalidArgument + v);
|
|
1969
|
+
}
|
|
1970
|
+
Decimal2.prototype = P;
|
|
1971
|
+
Decimal2.ROUND_UP = 0;
|
|
1972
|
+
Decimal2.ROUND_DOWN = 1;
|
|
1973
|
+
Decimal2.ROUND_CEIL = 2;
|
|
1974
|
+
Decimal2.ROUND_FLOOR = 3;
|
|
1975
|
+
Decimal2.ROUND_HALF_UP = 4;
|
|
1976
|
+
Decimal2.ROUND_HALF_DOWN = 5;
|
|
1977
|
+
Decimal2.ROUND_HALF_EVEN = 6;
|
|
1978
|
+
Decimal2.ROUND_HALF_CEIL = 7;
|
|
1979
|
+
Decimal2.ROUND_HALF_FLOOR = 8;
|
|
1980
|
+
Decimal2.EUCLID = 9;
|
|
1981
|
+
Decimal2.config = Decimal2.set = config;
|
|
1982
|
+
Decimal2.clone = clone;
|
|
1983
|
+
Decimal2.isDecimal = isDecimalInstance;
|
|
1984
|
+
Decimal2.abs = abs;
|
|
1985
|
+
Decimal2.acos = acos;
|
|
1986
|
+
Decimal2.acosh = acosh;
|
|
1987
|
+
Decimal2.add = add;
|
|
1988
|
+
Decimal2.asin = asin;
|
|
1989
|
+
Decimal2.asinh = asinh;
|
|
1990
|
+
Decimal2.atan = atan;
|
|
1991
|
+
Decimal2.atanh = atanh;
|
|
1992
|
+
Decimal2.atan2 = atan2;
|
|
1993
|
+
Decimal2.cbrt = cbrt;
|
|
1994
|
+
Decimal2.ceil = ceil;
|
|
1995
|
+
Decimal2.clamp = clamp;
|
|
1996
|
+
Decimal2.cos = cos;
|
|
1997
|
+
Decimal2.cosh = cosh;
|
|
1998
|
+
Decimal2.div = div;
|
|
1999
|
+
Decimal2.exp = exp;
|
|
2000
|
+
Decimal2.floor = floor;
|
|
2001
|
+
Decimal2.hypot = hypot;
|
|
2002
|
+
Decimal2.ln = ln;
|
|
2003
|
+
Decimal2.log = log;
|
|
2004
|
+
Decimal2.log10 = log10;
|
|
2005
|
+
Decimal2.log2 = log2;
|
|
2006
|
+
Decimal2.max = max;
|
|
2007
|
+
Decimal2.min = min;
|
|
2008
|
+
Decimal2.mod = mod;
|
|
2009
|
+
Decimal2.mul = mul;
|
|
2010
|
+
Decimal2.pow = pow;
|
|
2011
|
+
Decimal2.random = random;
|
|
2012
|
+
Decimal2.round = round;
|
|
2013
|
+
Decimal2.sign = sign;
|
|
2014
|
+
Decimal2.sin = sin;
|
|
2015
|
+
Decimal2.sinh = sinh;
|
|
2016
|
+
Decimal2.sqrt = sqrt;
|
|
2017
|
+
Decimal2.sub = sub;
|
|
2018
|
+
Decimal2.sum = sum;
|
|
2019
|
+
Decimal2.tan = tan;
|
|
2020
|
+
Decimal2.tanh = tanh;
|
|
2021
|
+
Decimal2.trunc = trunc;
|
|
2022
|
+
if (obj === void 0) obj = {};
|
|
2023
|
+
if (obj) {
|
|
2024
|
+
if (obj.defaults !== true) {
|
|
2025
|
+
ps = ["precision", "rounding", "toExpNeg", "toExpPos", "maxE", "minE", "modulo", "crypto"];
|
|
2026
|
+
for (i = 0; i < ps.length; ) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
Decimal2.config(obj);
|
|
2030
|
+
return Decimal2;
|
|
2031
|
+
}
|
|
2032
|
+
function div(x, y) {
|
|
2033
|
+
return new this(x).div(y);
|
|
2034
|
+
}
|
|
2035
|
+
function exp(x) {
|
|
2036
|
+
return new this(x).exp();
|
|
2037
|
+
}
|
|
2038
|
+
function floor(x) {
|
|
2039
|
+
return finalise(x = new this(x), x.e + 1, 3);
|
|
2040
|
+
}
|
|
2041
|
+
function hypot() {
|
|
2042
|
+
var i, n, t = new this(0);
|
|
2043
|
+
external = false;
|
|
2044
|
+
for (i = 0; i < arguments.length; ) {
|
|
2045
|
+
n = new this(arguments[i++]);
|
|
2046
|
+
if (!n.d) {
|
|
2047
|
+
if (n.s) {
|
|
2048
|
+
external = true;
|
|
2049
|
+
return new this(1 / 0);
|
|
2050
|
+
}
|
|
2051
|
+
t = n;
|
|
2052
|
+
} else if (t.d) {
|
|
2053
|
+
t = t.plus(n.times(n));
|
|
2054
|
+
}
|
|
2055
|
+
}
|
|
2056
|
+
external = true;
|
|
2057
|
+
return t.sqrt();
|
|
2058
|
+
}
|
|
2059
|
+
function isDecimalInstance(obj) {
|
|
2060
|
+
return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
|
|
2061
|
+
}
|
|
2062
|
+
function ln(x) {
|
|
2063
|
+
return new this(x).ln();
|
|
2064
|
+
}
|
|
2065
|
+
function log(x, y) {
|
|
2066
|
+
return new this(x).log(y);
|
|
2067
|
+
}
|
|
2068
|
+
function log2(x) {
|
|
2069
|
+
return new this(x).log(2);
|
|
2070
|
+
}
|
|
2071
|
+
function log10(x) {
|
|
2072
|
+
return new this(x).log(10);
|
|
2073
|
+
}
|
|
2074
|
+
function max() {
|
|
2075
|
+
return maxOrMin(this, arguments, -1);
|
|
2076
|
+
}
|
|
2077
|
+
function min() {
|
|
2078
|
+
return maxOrMin(this, arguments, 1);
|
|
2079
|
+
}
|
|
2080
|
+
function mod(x, y) {
|
|
2081
|
+
return new this(x).mod(y);
|
|
2082
|
+
}
|
|
2083
|
+
function mul(x, y) {
|
|
2084
|
+
return new this(x).mul(y);
|
|
2085
|
+
}
|
|
2086
|
+
function pow(x, y) {
|
|
2087
|
+
return new this(x).pow(y);
|
|
2088
|
+
}
|
|
2089
|
+
function random(sd) {
|
|
2090
|
+
var d, e, k, n, i = 0, r = new this(1), rd = [];
|
|
2091
|
+
if (sd === void 0) sd = this.precision;
|
|
2092
|
+
else checkInt32(sd, 1, MAX_DIGITS);
|
|
2093
|
+
k = Math.ceil(sd / LOG_BASE);
|
|
2094
|
+
if (!this.crypto) {
|
|
2095
|
+
for (; i < k; ) rd[i++] = Math.random() * 1e7 | 0;
|
|
2096
|
+
} else if (crypto.getRandomValues) {
|
|
2097
|
+
d = crypto.getRandomValues(new Uint32Array(k));
|
|
2098
|
+
for (; i < k; ) {
|
|
2099
|
+
n = d[i];
|
|
2100
|
+
if (n >= 429e7) {
|
|
2101
|
+
d[i] = crypto.getRandomValues(new Uint32Array(1))[0];
|
|
2102
|
+
} else {
|
|
2103
|
+
rd[i++] = n % 1e7;
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
} else if (crypto.randomBytes) {
|
|
2107
|
+
d = crypto.randomBytes(k *= 4);
|
|
2108
|
+
for (; i < k; ) {
|
|
2109
|
+
n = d[i] + (d[i + 1] << 8) + (d[i + 2] << 16) + ((d[i + 3] & 127) << 24);
|
|
2110
|
+
if (n >= 214e7) {
|
|
2111
|
+
crypto.randomBytes(4).copy(d, i);
|
|
2112
|
+
} else {
|
|
2113
|
+
rd.push(n % 1e7);
|
|
2114
|
+
i += 4;
|
|
2115
|
+
}
|
|
2116
|
+
}
|
|
2117
|
+
i = k / 4;
|
|
2118
|
+
} else {
|
|
2119
|
+
throw Error(cryptoUnavailable);
|
|
2120
|
+
}
|
|
2121
|
+
k = rd[--i];
|
|
2122
|
+
sd %= LOG_BASE;
|
|
2123
|
+
if (k && sd) {
|
|
2124
|
+
n = mathpow(10, LOG_BASE - sd);
|
|
2125
|
+
rd[i] = (k / n | 0) * n;
|
|
2126
|
+
}
|
|
2127
|
+
for (; rd[i] === 0; i--) rd.pop();
|
|
2128
|
+
if (i < 0) {
|
|
2129
|
+
e = 0;
|
|
2130
|
+
rd = [0];
|
|
2131
|
+
} else {
|
|
2132
|
+
e = -1;
|
|
2133
|
+
for (; rd[0] === 0; e -= LOG_BASE) rd.shift();
|
|
2134
|
+
for (k = 1, n = rd[0]; n >= 10; n /= 10) k++;
|
|
2135
|
+
if (k < LOG_BASE) e -= LOG_BASE - k;
|
|
2136
|
+
}
|
|
2137
|
+
r.e = e;
|
|
2138
|
+
r.d = rd;
|
|
2139
|
+
return r;
|
|
2140
|
+
}
|
|
2141
|
+
function round(x) {
|
|
2142
|
+
return finalise(x = new this(x), x.e + 1, this.rounding);
|
|
2143
|
+
}
|
|
2144
|
+
function sign(x) {
|
|
2145
|
+
x = new this(x);
|
|
2146
|
+
return x.d ? x.d[0] ? x.s : 0 * x.s : x.s || NaN;
|
|
2147
|
+
}
|
|
2148
|
+
function sin(x) {
|
|
2149
|
+
return new this(x).sin();
|
|
2150
|
+
}
|
|
2151
|
+
function sinh(x) {
|
|
2152
|
+
return new this(x).sinh();
|
|
2153
|
+
}
|
|
2154
|
+
function sqrt(x) {
|
|
2155
|
+
return new this(x).sqrt();
|
|
2156
|
+
}
|
|
2157
|
+
function sub(x, y) {
|
|
2158
|
+
return new this(x).sub(y);
|
|
2159
|
+
}
|
|
2160
|
+
function sum() {
|
|
2161
|
+
var i = 0, args = arguments, x = new this(args[i]);
|
|
2162
|
+
external = false;
|
|
2163
|
+
for (; x.s && ++i < args.length; ) x = x.plus(args[i]);
|
|
2164
|
+
external = true;
|
|
2165
|
+
return finalise(x, this.precision, this.rounding);
|
|
2166
|
+
}
|
|
2167
|
+
function tan(x) {
|
|
2168
|
+
return new this(x).tan();
|
|
2169
|
+
}
|
|
2170
|
+
function tanh(x) {
|
|
2171
|
+
return new this(x).tanh();
|
|
2172
|
+
}
|
|
2173
|
+
function trunc(x) {
|
|
2174
|
+
return finalise(x = new this(x), x.e + 1, 1);
|
|
2175
|
+
}
|
|
2176
|
+
P[Symbol.for("nodejs.util.inspect.custom")] = P.toString;
|
|
2177
|
+
P[Symbol.toStringTag] = "Decimal";
|
|
2178
|
+
var Decimal = P.constructor = clone(DEFAULTS);
|
|
2179
|
+
LN10 = new Decimal(LN10);
|
|
2180
|
+
PI = new Decimal(PI);
|
|
2181
|
+
var decimal_default = Decimal;
|
|
2182
|
+
function getTransactionAccountEncoder() {
|
|
2183
|
+
return kit.getStructEncoder([
|
|
2184
|
+
["pubkey", kit.getAddressEncoder()],
|
|
2185
|
+
["isSigner", kit.getBooleanEncoder()],
|
|
2186
|
+
["isWritable", kit.getBooleanEncoder()]
|
|
2187
|
+
]);
|
|
2188
|
+
}
|
|
2189
|
+
function getTransactionAccountDecoder() {
|
|
2190
|
+
return kit.getStructDecoder([
|
|
2191
|
+
["pubkey", kit.getAddressDecoder()],
|
|
2192
|
+
["isSigner", kit.getBooleanDecoder()],
|
|
2193
|
+
["isWritable", kit.getBooleanDecoder()]
|
|
2194
|
+
]);
|
|
2195
|
+
}
|
|
2196
|
+
function getTransactionAccountCodec() {
|
|
2197
|
+
return kit.combineCodec(
|
|
2198
|
+
getTransactionAccountEncoder(),
|
|
2199
|
+
getTransactionAccountDecoder()
|
|
2200
|
+
);
|
|
2201
|
+
}
|
|
2202
|
+
var FeeCollectionFrequency = /* @__PURE__ */ ((FeeCollectionFrequency2) => {
|
|
2203
|
+
FeeCollectionFrequency2[FeeCollectionFrequency2["Monthly"] = 0] = "Monthly";
|
|
2204
|
+
FeeCollectionFrequency2[FeeCollectionFrequency2["Quarterly"] = 1] = "Quarterly";
|
|
2205
|
+
FeeCollectionFrequency2[FeeCollectionFrequency2["Annually"] = 2] = "Annually";
|
|
2206
|
+
FeeCollectionFrequency2[FeeCollectionFrequency2["OnWithdrawal"] = 3] = "OnWithdrawal";
|
|
2207
|
+
return FeeCollectionFrequency2;
|
|
2208
|
+
})(FeeCollectionFrequency || {});
|
|
2209
|
+
function getFeeCollectionFrequencyEncoder() {
|
|
2210
|
+
return kit.getEnumEncoder(FeeCollectionFrequency);
|
|
2211
|
+
}
|
|
2212
|
+
function getFeeCollectionFrequencyDecoder() {
|
|
2213
|
+
return kit.getEnumDecoder(FeeCollectionFrequency);
|
|
2214
|
+
}
|
|
2215
|
+
function getFeeCollectionFrequencyCodec() {
|
|
2216
|
+
return kit.combineCodec(
|
|
2217
|
+
getFeeCollectionFrequencyEncoder(),
|
|
2218
|
+
getFeeCollectionFrequencyDecoder()
|
|
2219
|
+
);
|
|
2220
|
+
}
|
|
2221
|
+
|
|
2222
|
+
// src/types/managerFeeStructure.ts
|
|
2223
|
+
function getManagerFeeStructureEncoder() {
|
|
2224
|
+
return kit.getStructEncoder([
|
|
2225
|
+
["performanceFeeRate", kit.getU16Encoder()],
|
|
2226
|
+
["managementFeeRate", kit.getU16Encoder()],
|
|
2227
|
+
["collectionFrequency", getFeeCollectionFrequencyEncoder()],
|
|
2228
|
+
["highWaterMark", kit.getU64Encoder()],
|
|
2229
|
+
["feeRecipient", kit.getAddressEncoder()]
|
|
2230
|
+
]);
|
|
2231
|
+
}
|
|
2232
|
+
function getManagerFeeStructureDecoder() {
|
|
2233
|
+
return kit.getStructDecoder([
|
|
2234
|
+
["performanceFeeRate", kit.getU16Decoder()],
|
|
2235
|
+
["managementFeeRate", kit.getU16Decoder()],
|
|
2236
|
+
["collectionFrequency", getFeeCollectionFrequencyDecoder()],
|
|
2237
|
+
["highWaterMark", kit.getU64Decoder()],
|
|
2238
|
+
["feeRecipient", kit.getAddressDecoder()]
|
|
2239
|
+
]);
|
|
2240
|
+
}
|
|
2241
|
+
function getManagerFeeStructureCodec() {
|
|
2242
|
+
return kit.combineCodec(
|
|
2243
|
+
getManagerFeeStructureEncoder(),
|
|
2244
|
+
getManagerFeeStructureDecoder()
|
|
2245
|
+
);
|
|
2246
|
+
}
|
|
2247
|
+
|
|
2248
|
+
// src/types/policy.ts
|
|
2249
|
+
var Policy = /* @__PURE__ */ ((Policy2) => {
|
|
2250
|
+
Policy2["AllowAny"] = "AllowAny";
|
|
2251
|
+
Policy2["DenyAll"] = "DenyAll";
|
|
2252
|
+
Policy2["LimitTransfer"] = "LimitTransfer";
|
|
2253
|
+
Policy2["Owners"] = "Owners";
|
|
2254
|
+
Policy2["Multisig"] = "Multisig";
|
|
2255
|
+
return Policy2;
|
|
2256
|
+
})(Policy || {});
|
|
2257
|
+
function getVaultDecoder() {
|
|
2258
|
+
return kit.getStructDecoder([
|
|
2259
|
+
["discriminator", kit.fixDecoderSize(kit.getBytesDecoder(), 8)],
|
|
2260
|
+
["policyProgram", kit.getAddressDecoder()],
|
|
2261
|
+
["seed", kit.addDecoderSizePrefix(kit.getUtf8Decoder(), kit.getU32Decoder())],
|
|
2262
|
+
["authority", kit.getAddressDecoder()],
|
|
2263
|
+
["shareMint", kit.getAddressDecoder()],
|
|
2264
|
+
["underlyingMint", kit.getAddressDecoder()],
|
|
2265
|
+
["manager", kit.getOptionDecoder(kit.getAddressDecoder())],
|
|
2266
|
+
["parentVault", kit.getOptionDecoder(kit.getAddressDecoder())],
|
|
2267
|
+
["allocation", kit.getU64Decoder()],
|
|
2268
|
+
["onchainBalance", kit.getU64Decoder()],
|
|
2269
|
+
["offchainBalance", kit.getU64Decoder()],
|
|
2270
|
+
["totalBalance", kit.getU64Decoder()],
|
|
2271
|
+
["lastBalanceUpdate", kit.getU64Decoder()],
|
|
2272
|
+
["highWaterMark", kit.getU64Decoder()],
|
|
2273
|
+
["totalFeesPaid", kit.getU64Decoder()],
|
|
2274
|
+
["managerFees", kit.getOptionDecoder(getManagerFeeStructureDecoder())],
|
|
2275
|
+
["createdAt", kit.getU64Decoder()],
|
|
2276
|
+
["lastFeeCollection", kit.getU64Decoder()]
|
|
2277
|
+
]);
|
|
2278
|
+
}
|
|
2279
|
+
function decodeVault(encodedAccount) {
|
|
2280
|
+
return kit.decodeAccount(
|
|
2281
|
+
encodedAccount,
|
|
2282
|
+
getVaultDecoder()
|
|
2283
|
+
);
|
|
2284
|
+
}
|
|
2285
|
+
async function fetchVault(rpc, address, config2) {
|
|
2286
|
+
const maybeAccount = await fetchMaybeVault(rpc, address, config2);
|
|
2287
|
+
kit.assertAccountExists(maybeAccount);
|
|
2288
|
+
return maybeAccount;
|
|
2289
|
+
}
|
|
2290
|
+
async function fetchMaybeVault(rpc, address, config2) {
|
|
2291
|
+
const maybeAccount = await kit.fetchEncodedAccount(rpc, address, config2);
|
|
2292
|
+
return decodeVault(maybeAccount);
|
|
2293
|
+
}
|
|
2294
|
+
function getTransactionDecoder() {
|
|
2295
|
+
return kit.getStructDecoder([
|
|
2296
|
+
["discriminator", kit.fixDecoderSize(kit.getBytesDecoder(), 8)],
|
|
2297
|
+
["nonce", kit.getU64Decoder()],
|
|
2298
|
+
["didExecute", kit.getBooleanDecoder()],
|
|
2299
|
+
["vault", kit.getAddressDecoder()],
|
|
2300
|
+
["programId", kit.getAddressDecoder()],
|
|
2301
|
+
["data", kit.addDecoderSizePrefix(kit.getBytesDecoder(), kit.getU32Decoder())],
|
|
2302
|
+
["accounts", kit.getArrayDecoder(getTransactionAccountDecoder())]
|
|
2303
|
+
]);
|
|
2304
|
+
}
|
|
2305
|
+
function decodeTransaction(encodedAccount) {
|
|
2306
|
+
return kit.decodeAccount(
|
|
2307
|
+
encodedAccount,
|
|
2308
|
+
getTransactionDecoder()
|
|
2309
|
+
);
|
|
2310
|
+
}
|
|
2311
|
+
async function fetchTransaction(rpc, address, config2) {
|
|
2312
|
+
const maybeAccount = await fetchMaybeTransaction(rpc, address, config2);
|
|
2313
|
+
kit.assertAccountExists(maybeAccount);
|
|
2314
|
+
return maybeAccount;
|
|
2315
|
+
}
|
|
2316
|
+
async function fetchMaybeTransaction(rpc, address, config2) {
|
|
2317
|
+
const maybeAccount = await kit.fetchEncodedAccount(rpc, address, config2);
|
|
2318
|
+
return decodeTransaction(maybeAccount);
|
|
2319
|
+
}
|
|
2320
|
+
|
|
2321
|
+
// src/programs/hyroProtocol.ts
|
|
2322
|
+
var HYRO_PROTOCOL_PROGRAM_ADDRESS = "EGfondqUWdztf79joC5CBDi9HQEhgUtSsVmbPK838At4";
|
|
2323
|
+
|
|
2324
|
+
// src/programs/dropper.ts
|
|
2325
|
+
var DROPPER_PROGRAM_ADDRESS = "5Lk3HERSPsH82FQH9xGnp6YXjoTczQs6jpJoyG7M5kiC";
|
|
2326
|
+
|
|
2327
|
+
// src/programs/policyPrograms.ts
|
|
2328
|
+
var POLICY_ALLOW_ANY_PROGRAM_ADDRESS = "8PahHajSVz2QZn3cCTVH5Ldq2CJzD1GiPrqAiVmuB91Q";
|
|
2329
|
+
var POLICY_DENY_ALL_PROGRAM_ADDRESS = "FK8qiE3SoLfvtoQUXSdTwKEwmcBWjGBteh5m1VrD91zu";
|
|
2330
|
+
var POLICY_LIMIT_TRANSFER_PROGRAM_ADDRESS = "BhX5NffdrXhv7sPPBkobYNDQbBLsTQhrcTsTbTYGj389";
|
|
2331
|
+
var POLICY_OWNERS_PROGRAM_ADDRESS = "AgcQY5x8iB3vf2oQwR1V78ZczTkCy5EUP6pZAdrmHbLX";
|
|
2332
|
+
var POLICY_MULTISIG_PROGRAM_ADDRESS = "CaiBHtLhiBJJCdzUksjjXWEQfcXpFnCvLDLU6QhstAwE";
|
|
2333
|
+
var POLICY_CHALLENGES_PROGRAM_ADDRESS = "8Vewok54U1fwpbytBX7JDfv2nNTKCEqu93DcFPCRdgWM";
|
|
2334
|
+
function expectSome(value) {
|
|
2335
|
+
if (value === null || value === void 0) {
|
|
2336
|
+
throw new Error("Expected a value but received null or undefined.");
|
|
2337
|
+
}
|
|
2338
|
+
return value;
|
|
2339
|
+
}
|
|
2340
|
+
function expectAddress(value) {
|
|
2341
|
+
if (!value) {
|
|
2342
|
+
throw new Error("Expected a Address.");
|
|
2343
|
+
}
|
|
2344
|
+
if (typeof value === "object" && "address" in value) {
|
|
2345
|
+
return value.address;
|
|
2346
|
+
}
|
|
2347
|
+
if (Array.isArray(value)) {
|
|
2348
|
+
return value[0];
|
|
2349
|
+
}
|
|
2350
|
+
return value;
|
|
2351
|
+
}
|
|
2352
|
+
function getAccountMetaFactory(programAddress, optionalAccountStrategy) {
|
|
2353
|
+
return (account) => {
|
|
2354
|
+
if (!account.value) {
|
|
2355
|
+
return Object.freeze({
|
|
2356
|
+
address: programAddress,
|
|
2357
|
+
role: kit.AccountRole.READONLY
|
|
2358
|
+
});
|
|
2359
|
+
}
|
|
2360
|
+
const writableRole = account.isWritable ? kit.AccountRole.WRITABLE : kit.AccountRole.READONLY;
|
|
2361
|
+
return Object.freeze({
|
|
2362
|
+
address: expectAddress(account.value),
|
|
2363
|
+
role: isTransactionSigner(account.value) ? kit.upgradeRoleToSigner(writableRole) : writableRole,
|
|
2364
|
+
...isTransactionSigner(account.value) ? { signer: account.value } : {}
|
|
2365
|
+
});
|
|
2366
|
+
};
|
|
2367
|
+
}
|
|
2368
|
+
function isTransactionSigner(value) {
|
|
2369
|
+
return !!value && typeof value === "object" && "address" in value && kit.isTransactionSigner(value);
|
|
2370
|
+
}
|
|
2371
|
+
|
|
2372
|
+
// src/instructions/dropper/transferLamports.ts
|
|
2373
|
+
var TRANSFER_LAMPORTS_DISCRIMINATOR = new Uint8Array([
|
|
2374
|
+
62,
|
|
2375
|
+
53,
|
|
2376
|
+
201,
|
|
2377
|
+
68,
|
|
2378
|
+
102,
|
|
2379
|
+
134,
|
|
2380
|
+
83,
|
|
2381
|
+
103
|
|
2382
|
+
]);
|
|
2383
|
+
function getTransferLamportsDiscriminatorBytes() {
|
|
2384
|
+
return kit.fixEncoderSize(kit.getBytesEncoder(), 8).encode(
|
|
2385
|
+
TRANSFER_LAMPORTS_DISCRIMINATOR
|
|
2386
|
+
);
|
|
2387
|
+
}
|
|
2388
|
+
function getTransferLamportsInstructionDataEncoder() {
|
|
2389
|
+
return kit.transformEncoder(
|
|
2390
|
+
kit.getStructEncoder([
|
|
2391
|
+
["discriminator", kit.fixEncoderSize(kit.getBytesEncoder(), 8)],
|
|
2392
|
+
["amount", kit.getU64Encoder()]
|
|
2393
|
+
]),
|
|
2394
|
+
(value) => ({ ...value, discriminator: TRANSFER_LAMPORTS_DISCRIMINATOR })
|
|
2395
|
+
);
|
|
2396
|
+
}
|
|
2397
|
+
function getTransferLamportsInstructionDataDecoder() {
|
|
2398
|
+
return kit.getStructDecoder([
|
|
2399
|
+
["discriminator", kit.fixDecoderSize(kit.getBytesDecoder(), 8)],
|
|
2400
|
+
["amount", kit.getU64Decoder()]
|
|
2401
|
+
]);
|
|
2402
|
+
}
|
|
2403
|
+
function getTransferLamportsInstructionDataCodec() {
|
|
2404
|
+
return kit.combineCodec(
|
|
2405
|
+
getTransferLamportsInstructionDataEncoder(),
|
|
2406
|
+
getTransferLamportsInstructionDataDecoder()
|
|
2407
|
+
);
|
|
2408
|
+
}
|
|
2409
|
+
function getTransferLamportsInstruction(input, config2) {
|
|
2410
|
+
const programAddress = config2?.programAddress ?? DROPPER_PROGRAM_ADDRESS;
|
|
2411
|
+
const originalAccounts = {
|
|
2412
|
+
from: { value: input.from ?? null, isWritable: true },
|
|
2413
|
+
to: { value: input.to ?? null, isWritable: true },
|
|
2414
|
+
systemProgram: { value: input.systemProgram ?? null, isWritable: false }
|
|
2415
|
+
};
|
|
2416
|
+
const accounts = originalAccounts;
|
|
2417
|
+
const args = { ...input };
|
|
2418
|
+
if (!accounts.systemProgram.value) {
|
|
2419
|
+
accounts.systemProgram.value = "11111111111111111111111111111111";
|
|
2420
|
+
}
|
|
2421
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
2422
|
+
const instruction = {
|
|
2423
|
+
accounts: [
|
|
2424
|
+
getAccountMeta(accounts.from),
|
|
2425
|
+
getAccountMeta(accounts.to),
|
|
2426
|
+
getAccountMeta(accounts.systemProgram)
|
|
2427
|
+
],
|
|
2428
|
+
programAddress,
|
|
2429
|
+
data: getTransferLamportsInstructionDataEncoder().encode(
|
|
2430
|
+
args
|
|
2431
|
+
)
|
|
2432
|
+
};
|
|
2433
|
+
return instruction;
|
|
2434
|
+
}
|
|
2435
|
+
function parseTransferLamportsInstruction(instruction) {
|
|
2436
|
+
if (instruction.accounts.length < 3) {
|
|
2437
|
+
throw new Error("Not enough accounts");
|
|
2438
|
+
}
|
|
2439
|
+
let accountIndex = 0;
|
|
2440
|
+
const getNextAccount = () => {
|
|
2441
|
+
const accountMeta = instruction.accounts[accountIndex];
|
|
2442
|
+
accountIndex += 1;
|
|
2443
|
+
return accountMeta;
|
|
2444
|
+
};
|
|
2445
|
+
return {
|
|
2446
|
+
programAddress: instruction.programAddress,
|
|
2447
|
+
accounts: {
|
|
2448
|
+
from: getNextAccount(),
|
|
2449
|
+
to: getNextAccount(),
|
|
2450
|
+
systemProgram: getNextAccount()
|
|
2451
|
+
},
|
|
2452
|
+
data: getTransferLamportsInstructionDataDecoder().decode(instruction.data)
|
|
2453
|
+
};
|
|
2454
|
+
}
|
|
2455
|
+
var CREATE_TX_DISCRIMINATOR = new Uint8Array([
|
|
2456
|
+
97,
|
|
2457
|
+
223,
|
|
2458
|
+
80,
|
|
2459
|
+
153,
|
|
2460
|
+
55,
|
|
2461
|
+
13,
|
|
2462
|
+
155,
|
|
2463
|
+
12
|
|
2464
|
+
]);
|
|
2465
|
+
function getCreateTxDiscriminatorBytes() {
|
|
2466
|
+
return kit.fixEncoderSize(kit.getBytesEncoder(), 8).encode(CREATE_TX_DISCRIMINATOR);
|
|
2467
|
+
}
|
|
2468
|
+
function getCreateTxInstructionDataEncoder() {
|
|
2469
|
+
return kit.transformEncoder(
|
|
2470
|
+
kit.getStructEncoder([
|
|
2471
|
+
["discriminator", kit.fixEncoderSize(kit.getBytesEncoder(), 8)],
|
|
2472
|
+
["nonce", kit.getU64Encoder()],
|
|
2473
|
+
["pid", kit.getAddressEncoder()],
|
|
2474
|
+
["accs", kit.getArrayEncoder(getTransactionAccountEncoder())],
|
|
2475
|
+
["data", kit.addEncoderSizePrefix(kit.getBytesEncoder(), kit.getU32Encoder())]
|
|
2476
|
+
]),
|
|
2477
|
+
(value) => ({ ...value, discriminator: CREATE_TX_DISCRIMINATOR })
|
|
2478
|
+
);
|
|
2479
|
+
}
|
|
2480
|
+
function getCreateTxInstructionDataDecoder() {
|
|
2481
|
+
return kit.getStructDecoder([
|
|
2482
|
+
["discriminator", kit.fixDecoderSize(kit.getBytesDecoder(), 8)],
|
|
2483
|
+
["nonce", kit.getU64Decoder()],
|
|
2484
|
+
["pid", kit.getAddressDecoder()],
|
|
2485
|
+
["accs", kit.getArrayDecoder(getTransactionAccountDecoder())],
|
|
2486
|
+
["data", kit.addDecoderSizePrefix(kit.getBytesDecoder(), kit.getU32Decoder())]
|
|
2487
|
+
]);
|
|
2488
|
+
}
|
|
2489
|
+
function getCreateTxInstructionDataCodec() {
|
|
2490
|
+
return kit.combineCodec(
|
|
2491
|
+
getCreateTxInstructionDataEncoder(),
|
|
2492
|
+
getCreateTxInstructionDataDecoder()
|
|
2493
|
+
);
|
|
2494
|
+
}
|
|
2495
|
+
async function getCreateTxInstructionAsync(input, config2) {
|
|
2496
|
+
const programAddress = config2?.programAddress ?? HYRO_PROTOCOL_PROGRAM_ADDRESS;
|
|
2497
|
+
const originalAccounts = {
|
|
2498
|
+
vault: { value: input.vault ?? null, isWritable: false },
|
|
2499
|
+
transaction: { value: input.transaction ?? null, isWritable: true },
|
|
2500
|
+
policyAccount: { value: input.policyAccount ?? null, isWritable: true },
|
|
2501
|
+
policyProgram: { value: input.policyProgram ?? null, isWritable: false },
|
|
2502
|
+
vaultSigner: { value: input.vaultSigner ?? null, isWritable: false },
|
|
2503
|
+
signer: { value: input.signer ?? null, isWritable: true },
|
|
2504
|
+
systemProgram: { value: input.systemProgram ?? null, isWritable: false }
|
|
2505
|
+
};
|
|
2506
|
+
const accounts = originalAccounts;
|
|
2507
|
+
const args = { ...input };
|
|
2508
|
+
if (!accounts.transaction.value) {
|
|
2509
|
+
accounts.transaction.value = await kit.getProgramDerivedAddress({
|
|
2510
|
+
programAddress,
|
|
2511
|
+
seeds: [
|
|
2512
|
+
kit.getAddressEncoder().encode(expectAddress(accounts.vault.value)),
|
|
2513
|
+
kit.getU64Encoder().encode(expectSome(args.nonce))
|
|
2514
|
+
]
|
|
2515
|
+
});
|
|
2516
|
+
}
|
|
2517
|
+
if (!accounts.vaultSigner.value) {
|
|
2518
|
+
accounts.vaultSigner.value = await kit.getProgramDerivedAddress({
|
|
2519
|
+
programAddress,
|
|
2520
|
+
seeds: [kit.getAddressEncoder().encode(expectAddress(accounts.vault.value))]
|
|
2521
|
+
});
|
|
2522
|
+
}
|
|
2523
|
+
if (!accounts.systemProgram.value) {
|
|
2524
|
+
accounts.systemProgram.value = "11111111111111111111111111111111";
|
|
2525
|
+
}
|
|
2526
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
2527
|
+
const instruction = {
|
|
2528
|
+
accounts: [
|
|
2529
|
+
getAccountMeta(accounts.vault),
|
|
2530
|
+
getAccountMeta(accounts.transaction),
|
|
2531
|
+
getAccountMeta(accounts.policyAccount),
|
|
2532
|
+
getAccountMeta(accounts.policyProgram),
|
|
2533
|
+
getAccountMeta(accounts.vaultSigner),
|
|
2534
|
+
getAccountMeta(accounts.signer),
|
|
2535
|
+
getAccountMeta(accounts.systemProgram)
|
|
2536
|
+
],
|
|
2537
|
+
programAddress,
|
|
2538
|
+
data: getCreateTxInstructionDataEncoder().encode(
|
|
2539
|
+
args
|
|
2540
|
+
)
|
|
2541
|
+
};
|
|
2542
|
+
return instruction;
|
|
2543
|
+
}
|
|
2544
|
+
function getCreateTxInstruction(input, config2) {
|
|
2545
|
+
const programAddress = config2?.programAddress ?? HYRO_PROTOCOL_PROGRAM_ADDRESS;
|
|
2546
|
+
const originalAccounts = {
|
|
2547
|
+
vault: { value: input.vault ?? null, isWritable: false },
|
|
2548
|
+
transaction: { value: input.transaction ?? null, isWritable: true },
|
|
2549
|
+
policyAccount: { value: input.policyAccount ?? null, isWritable: true },
|
|
2550
|
+
policyProgram: { value: input.policyProgram ?? null, isWritable: false },
|
|
2551
|
+
vaultSigner: { value: input.vaultSigner ?? null, isWritable: false },
|
|
2552
|
+
signer: { value: input.signer ?? null, isWritable: true },
|
|
2553
|
+
systemProgram: { value: input.systemProgram ?? null, isWritable: false }
|
|
2554
|
+
};
|
|
2555
|
+
const accounts = originalAccounts;
|
|
2556
|
+
const args = { ...input };
|
|
2557
|
+
if (!accounts.systemProgram.value) {
|
|
2558
|
+
accounts.systemProgram.value = "11111111111111111111111111111111";
|
|
2559
|
+
}
|
|
2560
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
2561
|
+
const instruction = {
|
|
2562
|
+
accounts: [
|
|
2563
|
+
getAccountMeta(accounts.vault),
|
|
2564
|
+
getAccountMeta(accounts.transaction),
|
|
2565
|
+
getAccountMeta(accounts.policyAccount),
|
|
2566
|
+
getAccountMeta(accounts.policyProgram),
|
|
2567
|
+
getAccountMeta(accounts.vaultSigner),
|
|
2568
|
+
getAccountMeta(accounts.signer),
|
|
2569
|
+
getAccountMeta(accounts.systemProgram)
|
|
2570
|
+
],
|
|
2571
|
+
programAddress,
|
|
2572
|
+
data: getCreateTxInstructionDataEncoder().encode(
|
|
2573
|
+
args
|
|
2574
|
+
)
|
|
2575
|
+
};
|
|
2576
|
+
return instruction;
|
|
2577
|
+
}
|
|
2578
|
+
function parseCreateTxInstruction(instruction) {
|
|
2579
|
+
if (instruction.accounts.length < 7) {
|
|
2580
|
+
throw new Error("Not enough accounts");
|
|
2581
|
+
}
|
|
2582
|
+
let accountIndex = 0;
|
|
2583
|
+
const getNextAccount = () => {
|
|
2584
|
+
const accountMeta = instruction.accounts[accountIndex];
|
|
2585
|
+
accountIndex += 1;
|
|
2586
|
+
return accountMeta;
|
|
2587
|
+
};
|
|
2588
|
+
return {
|
|
2589
|
+
programAddress: instruction.programAddress,
|
|
2590
|
+
accounts: {
|
|
2591
|
+
vault: getNextAccount(),
|
|
2592
|
+
transaction: getNextAccount(),
|
|
2593
|
+
policyAccount: getNextAccount(),
|
|
2594
|
+
policyProgram: getNextAccount(),
|
|
2595
|
+
vaultSigner: getNextAccount(),
|
|
2596
|
+
signer: getNextAccount(),
|
|
2597
|
+
systemProgram: getNextAccount()
|
|
2598
|
+
},
|
|
2599
|
+
data: getCreateTxInstructionDataDecoder().decode(instruction.data)
|
|
2600
|
+
};
|
|
2601
|
+
}
|
|
2602
|
+
var EXECUTE_TX_DISCRIMINATOR = new Uint8Array([
|
|
2603
|
+
249,
|
|
2604
|
+
17,
|
|
2605
|
+
145,
|
|
2606
|
+
23,
|
|
2607
|
+
12,
|
|
2608
|
+
252,
|
|
2609
|
+
17,
|
|
2610
|
+
41
|
|
2611
|
+
]);
|
|
2612
|
+
function getExecuteTxDiscriminatorBytes() {
|
|
2613
|
+
return kit.fixEncoderSize(kit.getBytesEncoder(), 8).encode(EXECUTE_TX_DISCRIMINATOR);
|
|
2614
|
+
}
|
|
2615
|
+
function getExecuteTxInstructionDataEncoder() {
|
|
2616
|
+
return kit.transformEncoder(
|
|
2617
|
+
kit.getStructEncoder([["discriminator", kit.fixEncoderSize(kit.getBytesEncoder(), 8)]]),
|
|
2618
|
+
(value) => ({ ...value, discriminator: EXECUTE_TX_DISCRIMINATOR })
|
|
2619
|
+
);
|
|
2620
|
+
}
|
|
2621
|
+
function getExecuteTxInstructionDataDecoder() {
|
|
2622
|
+
return kit.getStructDecoder([
|
|
2623
|
+
["discriminator", kit.fixDecoderSize(kit.getBytesDecoder(), 8)]
|
|
2624
|
+
]);
|
|
2625
|
+
}
|
|
2626
|
+
function getExecuteTxInstructionDataCodec() {
|
|
2627
|
+
return kit.combineCodec(
|
|
2628
|
+
getExecuteTxInstructionDataEncoder(),
|
|
2629
|
+
getExecuteTxInstructionDataDecoder()
|
|
2630
|
+
);
|
|
2631
|
+
}
|
|
2632
|
+
async function getExecuteTxInstructionAsync(input, config2) {
|
|
2633
|
+
const programAddress = config2?.programAddress ?? HYRO_PROTOCOL_PROGRAM_ADDRESS;
|
|
2634
|
+
const originalAccounts = {
|
|
2635
|
+
vault: { value: input.vault ?? null, isWritable: false },
|
|
2636
|
+
transaction: { value: input.transaction ?? null, isWritable: false },
|
|
2637
|
+
vaultSigner: { value: input.vaultSigner ?? null, isWritable: false },
|
|
2638
|
+
policyAccount: { value: input.policyAccount ?? null, isWritable: true },
|
|
2639
|
+
policyProgram: { value: input.policyProgram ?? null, isWritable: false },
|
|
2640
|
+
signer: { value: input.signer ?? null, isWritable: true }
|
|
2641
|
+
};
|
|
2642
|
+
const accounts = originalAccounts;
|
|
2643
|
+
if (!accounts.vaultSigner.value) {
|
|
2644
|
+
accounts.vaultSigner.value = await kit.getProgramDerivedAddress({
|
|
2645
|
+
programAddress,
|
|
2646
|
+
seeds: [kit.getAddressEncoder().encode(expectAddress(accounts.vault.value))]
|
|
2647
|
+
});
|
|
2648
|
+
}
|
|
2649
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
2650
|
+
const instruction = {
|
|
2651
|
+
accounts: [
|
|
2652
|
+
getAccountMeta(accounts.vault),
|
|
2653
|
+
getAccountMeta(accounts.transaction),
|
|
2654
|
+
getAccountMeta(accounts.vaultSigner),
|
|
2655
|
+
getAccountMeta(accounts.policyAccount),
|
|
2656
|
+
getAccountMeta(accounts.policyProgram),
|
|
2657
|
+
getAccountMeta(accounts.signer)
|
|
2658
|
+
],
|
|
2659
|
+
programAddress,
|
|
2660
|
+
data: getExecuteTxInstructionDataEncoder().encode({})
|
|
2661
|
+
};
|
|
2662
|
+
return instruction;
|
|
2663
|
+
}
|
|
2664
|
+
function getExecuteTxInstruction(input, config2) {
|
|
2665
|
+
const programAddress = config2?.programAddress ?? HYRO_PROTOCOL_PROGRAM_ADDRESS;
|
|
2666
|
+
const originalAccounts = {
|
|
2667
|
+
vault: { value: input.vault ?? null, isWritable: false },
|
|
2668
|
+
transaction: { value: input.transaction ?? null, isWritable: false },
|
|
2669
|
+
vaultSigner: { value: input.vaultSigner ?? null, isWritable: false },
|
|
2670
|
+
policyAccount: { value: input.policyAccount ?? null, isWritable: true },
|
|
2671
|
+
policyProgram: { value: input.policyProgram ?? null, isWritable: false },
|
|
2672
|
+
signer: { value: input.signer ?? null, isWritable: true }
|
|
2673
|
+
};
|
|
2674
|
+
const accounts = originalAccounts;
|
|
2675
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
2676
|
+
const instruction = {
|
|
2677
|
+
accounts: [
|
|
2678
|
+
getAccountMeta(accounts.vault),
|
|
2679
|
+
getAccountMeta(accounts.transaction),
|
|
2680
|
+
getAccountMeta(accounts.vaultSigner),
|
|
2681
|
+
getAccountMeta(accounts.policyAccount),
|
|
2682
|
+
getAccountMeta(accounts.policyProgram),
|
|
2683
|
+
getAccountMeta(accounts.signer)
|
|
2684
|
+
],
|
|
2685
|
+
programAddress,
|
|
2686
|
+
data: getExecuteTxInstructionDataEncoder().encode({})
|
|
2687
|
+
};
|
|
2688
|
+
return instruction;
|
|
2689
|
+
}
|
|
2690
|
+
function parseExecuteTxInstruction(instruction) {
|
|
2691
|
+
if (instruction.accounts.length < 6) {
|
|
2692
|
+
throw new Error("Not enough accounts");
|
|
2693
|
+
}
|
|
2694
|
+
let accountIndex = 0;
|
|
2695
|
+
const getNextAccount = () => {
|
|
2696
|
+
const accountMeta = instruction.accounts[accountIndex];
|
|
2697
|
+
accountIndex += 1;
|
|
2698
|
+
return accountMeta;
|
|
2699
|
+
};
|
|
2700
|
+
return {
|
|
2701
|
+
programAddress: instruction.programAddress,
|
|
2702
|
+
accounts: {
|
|
2703
|
+
vault: getNextAccount(),
|
|
2704
|
+
transaction: getNextAccount(),
|
|
2705
|
+
vaultSigner: getNextAccount(),
|
|
2706
|
+
policyAccount: getNextAccount(),
|
|
2707
|
+
policyProgram: getNextAccount(),
|
|
2708
|
+
signer: getNextAccount()
|
|
2709
|
+
},
|
|
2710
|
+
data: getExecuteTxInstructionDataDecoder().decode(instruction.data)
|
|
2711
|
+
};
|
|
2712
|
+
}
|
|
2713
|
+
var INITIALIZE_VAULT_DISCRIMINATOR = new Uint8Array([
|
|
2714
|
+
48,
|
|
2715
|
+
191,
|
|
2716
|
+
163,
|
|
2717
|
+
44,
|
|
2718
|
+
71,
|
|
2719
|
+
129,
|
|
2720
|
+
63,
|
|
2721
|
+
164
|
|
2722
|
+
]);
|
|
2723
|
+
function getInitializeVaultDiscriminatorBytes() {
|
|
2724
|
+
return kit.fixEncoderSize(kit.getBytesEncoder(), 8).encode(
|
|
2725
|
+
INITIALIZE_VAULT_DISCRIMINATOR
|
|
2726
|
+
);
|
|
2727
|
+
}
|
|
2728
|
+
function getInitializeVaultInstructionDataEncoder() {
|
|
2729
|
+
return kit.transformEncoder(
|
|
2730
|
+
kit.getStructEncoder([
|
|
2731
|
+
["discriminator", kit.fixEncoderSize(kit.getBytesEncoder(), 8)],
|
|
2732
|
+
["seed", kit.addEncoderSizePrefix(kit.getUtf8Encoder(), kit.getU32Encoder())],
|
|
2733
|
+
["policyProgram", kit.getAddressEncoder()]
|
|
2734
|
+
]),
|
|
2735
|
+
(value) => ({ ...value, discriminator: INITIALIZE_VAULT_DISCRIMINATOR })
|
|
2736
|
+
);
|
|
2737
|
+
}
|
|
2738
|
+
function getInitializeVaultInstructionDataDecoder() {
|
|
2739
|
+
return kit.getStructDecoder([
|
|
2740
|
+
["discriminator", kit.fixDecoderSize(kit.getBytesDecoder(), 8)],
|
|
2741
|
+
["seed", kit.addDecoderSizePrefix(kit.getUtf8Decoder(), kit.getU32Decoder())],
|
|
2742
|
+
["policyProgram", kit.getAddressDecoder()]
|
|
2743
|
+
]);
|
|
2744
|
+
}
|
|
2745
|
+
function getInitializeVaultInstructionDataCodec() {
|
|
2746
|
+
return kit.combineCodec(
|
|
2747
|
+
getInitializeVaultInstructionDataEncoder(),
|
|
2748
|
+
getInitializeVaultInstructionDataDecoder()
|
|
2749
|
+
);
|
|
2750
|
+
}
|
|
2751
|
+
async function getInitializeVaultInstructionAsync(input, config2) {
|
|
2752
|
+
const programAddress = config2?.programAddress ?? HYRO_PROTOCOL_PROGRAM_ADDRESS;
|
|
2753
|
+
const originalAccounts = {
|
|
2754
|
+
vault: { value: input.vault ?? null, isWritable: true },
|
|
2755
|
+
authority: { value: input.authority ?? null, isWritable: false },
|
|
2756
|
+
signer: { value: input.signer ?? null, isWritable: true },
|
|
2757
|
+
systemProgram: { value: input.systemProgram ?? null, isWritable: false },
|
|
2758
|
+
shareSignerPda: { value: input.shareSignerPda ?? null, isWritable: false },
|
|
2759
|
+
shareMint: { value: input.shareMint ?? null, isWritable: true },
|
|
2760
|
+
vaultTokenAccount: {
|
|
2761
|
+
value: input.vaultTokenAccount ?? null,
|
|
2762
|
+
isWritable: true
|
|
2763
|
+
},
|
|
2764
|
+
authorityTokenAccount: {
|
|
2765
|
+
value: input.authorityTokenAccount ?? null,
|
|
2766
|
+
isWritable: true
|
|
2767
|
+
},
|
|
2768
|
+
underlyingMint: { value: input.underlyingMint ?? null, isWritable: false },
|
|
2769
|
+
tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
|
|
2770
|
+
rent: { value: input.rent ?? null, isWritable: false }
|
|
2771
|
+
};
|
|
2772
|
+
const accounts = originalAccounts;
|
|
2773
|
+
const args = { ...input };
|
|
2774
|
+
if (!accounts.vault.value) {
|
|
2775
|
+
accounts.vault.value = await kit.getProgramDerivedAddress({
|
|
2776
|
+
programAddress,
|
|
2777
|
+
seeds: [
|
|
2778
|
+
kit.addEncoderSizePrefix(kit.getUtf8Encoder(), kit.getU32Encoder()).encode(
|
|
2779
|
+
expectSome(args.seed)
|
|
2780
|
+
)
|
|
2781
|
+
]
|
|
2782
|
+
});
|
|
2783
|
+
}
|
|
2784
|
+
if (!accounts.authority.value) {
|
|
2785
|
+
accounts.authority.value = await kit.getProgramDerivedAddress({
|
|
2786
|
+
programAddress,
|
|
2787
|
+
seeds: [kit.getAddressEncoder().encode(expectAddress(accounts.vault.value))]
|
|
2788
|
+
});
|
|
2789
|
+
}
|
|
2790
|
+
if (!accounts.systemProgram.value) {
|
|
2791
|
+
accounts.systemProgram.value = "11111111111111111111111111111111";
|
|
2792
|
+
}
|
|
2793
|
+
if (!accounts.shareSignerPda.value) {
|
|
2794
|
+
accounts.shareSignerPda.value = await kit.getProgramDerivedAddress({
|
|
2795
|
+
programAddress,
|
|
2796
|
+
seeds: [
|
|
2797
|
+
kit.getBytesEncoder().encode(
|
|
2798
|
+
new Uint8Array([
|
|
2799
|
+
118,
|
|
2800
|
+
97,
|
|
2801
|
+
117,
|
|
2802
|
+
108,
|
|
2803
|
+
116,
|
|
2804
|
+
95,
|
|
2805
|
+
115,
|
|
2806
|
+
104,
|
|
2807
|
+
97,
|
|
2808
|
+
114,
|
|
2809
|
+
101,
|
|
2810
|
+
95,
|
|
2811
|
+
115,
|
|
2812
|
+
105,
|
|
2813
|
+
103,
|
|
2814
|
+
110,
|
|
2815
|
+
101,
|
|
2816
|
+
114
|
|
2817
|
+
])
|
|
2818
|
+
),
|
|
2819
|
+
kit.getAddressEncoder().encode(expectAddress(accounts.vault.value))
|
|
2820
|
+
]
|
|
2821
|
+
});
|
|
2822
|
+
}
|
|
2823
|
+
if (!accounts.shareMint.value) {
|
|
2824
|
+
accounts.shareMint.value = await kit.getProgramDerivedAddress({
|
|
2825
|
+
programAddress,
|
|
2826
|
+
seeds: [
|
|
2827
|
+
kit.getBytesEncoder().encode(
|
|
2828
|
+
new Uint8Array([
|
|
2829
|
+
118,
|
|
2830
|
+
97,
|
|
2831
|
+
117,
|
|
2832
|
+
108,
|
|
2833
|
+
116,
|
|
2834
|
+
95,
|
|
2835
|
+
115,
|
|
2836
|
+
104,
|
|
2837
|
+
97,
|
|
2838
|
+
114,
|
|
2839
|
+
101,
|
|
2840
|
+
95,
|
|
2841
|
+
109,
|
|
2842
|
+
105,
|
|
2843
|
+
110,
|
|
2844
|
+
116
|
|
2845
|
+
])
|
|
2846
|
+
),
|
|
2847
|
+
kit.getAddressEncoder().encode(expectAddress(accounts.vault.value))
|
|
2848
|
+
]
|
|
2849
|
+
});
|
|
2850
|
+
}
|
|
2851
|
+
if (!accounts.vaultTokenAccount.value) {
|
|
2852
|
+
accounts.vaultTokenAccount.value = await kit.getProgramDerivedAddress({
|
|
2853
|
+
programAddress,
|
|
2854
|
+
seeds: [
|
|
2855
|
+
kit.getBytesEncoder().encode(
|
|
2856
|
+
new Uint8Array([
|
|
2857
|
+
118,
|
|
2858
|
+
97,
|
|
2859
|
+
117,
|
|
2860
|
+
108,
|
|
2861
|
+
116,
|
|
2862
|
+
95,
|
|
2863
|
+
116,
|
|
2864
|
+
111,
|
|
2865
|
+
107,
|
|
2866
|
+
101,
|
|
2867
|
+
110,
|
|
2868
|
+
95,
|
|
2869
|
+
97,
|
|
2870
|
+
99,
|
|
2871
|
+
99,
|
|
2872
|
+
111,
|
|
2873
|
+
117,
|
|
2874
|
+
110,
|
|
2875
|
+
116
|
|
2876
|
+
])
|
|
2877
|
+
),
|
|
2878
|
+
kit.getAddressEncoder().encode(expectAddress(accounts.vault.value)),
|
|
2879
|
+
kit.getAddressEncoder().encode(
|
|
2880
|
+
expectAddress(accounts.underlyingMint.value)
|
|
2881
|
+
)
|
|
2882
|
+
]
|
|
2883
|
+
});
|
|
2884
|
+
}
|
|
2885
|
+
if (!accounts.authorityTokenAccount.value) {
|
|
2886
|
+
accounts.authorityTokenAccount.value = await kit.getProgramDerivedAddress({
|
|
2887
|
+
programAddress,
|
|
2888
|
+
seeds: [
|
|
2889
|
+
kit.getBytesEncoder().encode(
|
|
2890
|
+
new Uint8Array([
|
|
2891
|
+
97,
|
|
2892
|
+
117,
|
|
2893
|
+
116,
|
|
2894
|
+
104,
|
|
2895
|
+
111,
|
|
2896
|
+
114,
|
|
2897
|
+
105,
|
|
2898
|
+
116,
|
|
2899
|
+
121,
|
|
2900
|
+
95,
|
|
2901
|
+
116,
|
|
2902
|
+
111,
|
|
2903
|
+
107,
|
|
2904
|
+
101,
|
|
2905
|
+
110,
|
|
2906
|
+
95,
|
|
2907
|
+
97,
|
|
2908
|
+
99,
|
|
2909
|
+
99,
|
|
2910
|
+
111,
|
|
2911
|
+
117,
|
|
2912
|
+
110,
|
|
2913
|
+
116
|
|
2914
|
+
])
|
|
2915
|
+
),
|
|
2916
|
+
kit.getAddressEncoder().encode(expectAddress(accounts.authority.value)),
|
|
2917
|
+
kit.getAddressEncoder().encode(
|
|
2918
|
+
expectAddress(accounts.underlyingMint.value)
|
|
2919
|
+
)
|
|
2920
|
+
]
|
|
2921
|
+
});
|
|
2922
|
+
}
|
|
2923
|
+
if (!accounts.tokenProgram.value) {
|
|
2924
|
+
accounts.tokenProgram.value = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
|
|
2925
|
+
}
|
|
2926
|
+
if (!accounts.rent.value) {
|
|
2927
|
+
accounts.rent.value = "SysvarRent111111111111111111111111111111111";
|
|
2928
|
+
}
|
|
2929
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
2930
|
+
const instruction = {
|
|
2931
|
+
accounts: [
|
|
2932
|
+
getAccountMeta(accounts.vault),
|
|
2933
|
+
getAccountMeta(accounts.authority),
|
|
2934
|
+
getAccountMeta(accounts.signer),
|
|
2935
|
+
getAccountMeta(accounts.systemProgram),
|
|
2936
|
+
getAccountMeta(accounts.shareSignerPda),
|
|
2937
|
+
getAccountMeta(accounts.shareMint),
|
|
2938
|
+
getAccountMeta(accounts.vaultTokenAccount),
|
|
2939
|
+
getAccountMeta(accounts.authorityTokenAccount),
|
|
2940
|
+
getAccountMeta(accounts.underlyingMint),
|
|
2941
|
+
getAccountMeta(accounts.tokenProgram),
|
|
2942
|
+
getAccountMeta(accounts.rent)
|
|
2943
|
+
],
|
|
2944
|
+
programAddress,
|
|
2945
|
+
data: getInitializeVaultInstructionDataEncoder().encode(
|
|
2946
|
+
args
|
|
2947
|
+
)
|
|
2948
|
+
};
|
|
2949
|
+
return instruction;
|
|
2950
|
+
}
|
|
2951
|
+
function getInitializeVaultInstruction(input, config2) {
|
|
2952
|
+
const programAddress = config2?.programAddress ?? HYRO_PROTOCOL_PROGRAM_ADDRESS;
|
|
2953
|
+
const originalAccounts = {
|
|
2954
|
+
vault: { value: input.vault ?? null, isWritable: true },
|
|
2955
|
+
authority: { value: input.authority ?? null, isWritable: false },
|
|
2956
|
+
signer: { value: input.signer ?? null, isWritable: true },
|
|
2957
|
+
systemProgram: { value: input.systemProgram ?? null, isWritable: false },
|
|
2958
|
+
shareSignerPda: { value: input.shareSignerPda ?? null, isWritable: false },
|
|
2959
|
+
shareMint: { value: input.shareMint ?? null, isWritable: true },
|
|
2960
|
+
vaultTokenAccount: {
|
|
2961
|
+
value: input.vaultTokenAccount ?? null,
|
|
2962
|
+
isWritable: true
|
|
2963
|
+
},
|
|
2964
|
+
authorityTokenAccount: {
|
|
2965
|
+
value: input.authorityTokenAccount ?? null,
|
|
2966
|
+
isWritable: true
|
|
2967
|
+
},
|
|
2968
|
+
underlyingMint: { value: input.underlyingMint ?? null, isWritable: false },
|
|
2969
|
+
tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
|
|
2970
|
+
rent: { value: input.rent ?? null, isWritable: false }
|
|
2971
|
+
};
|
|
2972
|
+
const accounts = originalAccounts;
|
|
2973
|
+
const args = { ...input };
|
|
2974
|
+
if (!accounts.systemProgram.value) {
|
|
2975
|
+
accounts.systemProgram.value = "11111111111111111111111111111111";
|
|
2976
|
+
}
|
|
2977
|
+
if (!accounts.tokenProgram.value) {
|
|
2978
|
+
accounts.tokenProgram.value = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
|
|
2979
|
+
}
|
|
2980
|
+
if (!accounts.rent.value) {
|
|
2981
|
+
accounts.rent.value = "SysvarRent111111111111111111111111111111111";
|
|
2982
|
+
}
|
|
2983
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
2984
|
+
const instruction = {
|
|
2985
|
+
accounts: [
|
|
2986
|
+
getAccountMeta(accounts.vault),
|
|
2987
|
+
getAccountMeta(accounts.authority),
|
|
2988
|
+
getAccountMeta(accounts.signer),
|
|
2989
|
+
getAccountMeta(accounts.systemProgram),
|
|
2990
|
+
getAccountMeta(accounts.shareSignerPda),
|
|
2991
|
+
getAccountMeta(accounts.shareMint),
|
|
2992
|
+
getAccountMeta(accounts.vaultTokenAccount),
|
|
2993
|
+
getAccountMeta(accounts.authorityTokenAccount),
|
|
2994
|
+
getAccountMeta(accounts.underlyingMint),
|
|
2995
|
+
getAccountMeta(accounts.tokenProgram),
|
|
2996
|
+
getAccountMeta(accounts.rent)
|
|
2997
|
+
],
|
|
2998
|
+
programAddress,
|
|
2999
|
+
data: getInitializeVaultInstructionDataEncoder().encode(
|
|
3000
|
+
args
|
|
3001
|
+
)
|
|
3002
|
+
};
|
|
3003
|
+
return instruction;
|
|
3004
|
+
}
|
|
3005
|
+
function parseInitializeVaultInstruction(instruction) {
|
|
3006
|
+
if (instruction.accounts.length < 11) {
|
|
3007
|
+
throw new Error("Not enough accounts");
|
|
3008
|
+
}
|
|
3009
|
+
let accountIndex = 0;
|
|
3010
|
+
const getNextAccount = () => {
|
|
3011
|
+
const accountMeta = instruction.accounts[accountIndex];
|
|
3012
|
+
accountIndex += 1;
|
|
3013
|
+
return accountMeta;
|
|
3014
|
+
};
|
|
3015
|
+
return {
|
|
3016
|
+
programAddress: instruction.programAddress,
|
|
3017
|
+
accounts: {
|
|
3018
|
+
vault: getNextAccount(),
|
|
3019
|
+
authority: getNextAccount(),
|
|
3020
|
+
signer: getNextAccount(),
|
|
3021
|
+
systemProgram: getNextAccount(),
|
|
3022
|
+
shareSignerPda: getNextAccount(),
|
|
3023
|
+
shareMint: getNextAccount(),
|
|
3024
|
+
vaultTokenAccount: getNextAccount(),
|
|
3025
|
+
authorityTokenAccount: getNextAccount(),
|
|
3026
|
+
underlyingMint: getNextAccount(),
|
|
3027
|
+
tokenProgram: getNextAccount(),
|
|
3028
|
+
rent: getNextAccount()
|
|
3029
|
+
},
|
|
3030
|
+
data: getInitializeVaultInstructionDataDecoder().decode(instruction.data)
|
|
3031
|
+
};
|
|
3032
|
+
}
|
|
3033
|
+
var INITIALIZE_LIMIT_TRANSFER_DISCRIMINATOR = new Uint8Array([
|
|
3034
|
+
231,
|
|
3035
|
+
140,
|
|
3036
|
+
57,
|
|
3037
|
+
76,
|
|
3038
|
+
17,
|
|
3039
|
+
243,
|
|
3040
|
+
161,
|
|
3041
|
+
149
|
|
3042
|
+
]);
|
|
3043
|
+
function getInitializeLimitTransferDiscriminatorBytes() {
|
|
3044
|
+
return kit.fixEncoderSize(kit.getBytesEncoder(), 8).encode(
|
|
3045
|
+
INITIALIZE_LIMIT_TRANSFER_DISCRIMINATOR
|
|
3046
|
+
);
|
|
3047
|
+
}
|
|
3048
|
+
function getInitializeLimitTransferInstructionDataEncoder() {
|
|
3049
|
+
return kit.transformEncoder(
|
|
3050
|
+
kit.getStructEncoder([
|
|
3051
|
+
["discriminator", kit.fixEncoderSize(kit.getBytesEncoder(), 8)],
|
|
3052
|
+
["min", kit.getU64Encoder()],
|
|
3053
|
+
["max", kit.getU64Encoder()]
|
|
3054
|
+
]),
|
|
3055
|
+
(value) => ({
|
|
3056
|
+
...value,
|
|
3057
|
+
discriminator: INITIALIZE_LIMIT_TRANSFER_DISCRIMINATOR
|
|
3058
|
+
})
|
|
3059
|
+
);
|
|
3060
|
+
}
|
|
3061
|
+
function getInitializeLimitTransferInstructionDataDecoder() {
|
|
3062
|
+
return kit.getStructDecoder([
|
|
3063
|
+
["discriminator", kit.fixDecoderSize(kit.getBytesDecoder(), 8)],
|
|
3064
|
+
["min", kit.getU64Decoder()],
|
|
3065
|
+
["max", kit.getU64Decoder()]
|
|
3066
|
+
]);
|
|
3067
|
+
}
|
|
3068
|
+
function getInitializeLimitTransferInstructionDataCodec() {
|
|
3069
|
+
return kit.combineCodec(
|
|
3070
|
+
getInitializeLimitTransferInstructionDataEncoder(),
|
|
3071
|
+
getInitializeLimitTransferInstructionDataDecoder()
|
|
3072
|
+
);
|
|
3073
|
+
}
|
|
3074
|
+
async function getInitializeLimitTransferInstructionAsync(input, config2) {
|
|
3075
|
+
const programAddress = config2?.programAddress ?? POLICY_LIMIT_TRANSFER_PROGRAM_ADDRESS;
|
|
3076
|
+
const originalAccounts = {
|
|
3077
|
+
vault: { value: input.vault ?? null, isWritable: false },
|
|
3078
|
+
policyAccount: { value: input.policyAccount ?? null, isWritable: true },
|
|
3079
|
+
signer: { value: input.signer ?? null, isWritable: true },
|
|
3080
|
+
systemProgram: { value: input.systemProgram ?? null, isWritable: false }
|
|
3081
|
+
};
|
|
3082
|
+
const accounts = originalAccounts;
|
|
3083
|
+
const args = { ...input };
|
|
3084
|
+
if (!accounts.policyAccount.value) {
|
|
3085
|
+
accounts.policyAccount.value = await kit.getProgramDerivedAddress({
|
|
3086
|
+
programAddress,
|
|
3087
|
+
seeds: [kit.getAddressEncoder().encode(expectAddress(accounts.vault.value))]
|
|
3088
|
+
});
|
|
3089
|
+
}
|
|
3090
|
+
if (!accounts.systemProgram.value) {
|
|
3091
|
+
accounts.systemProgram.value = "11111111111111111111111111111111";
|
|
3092
|
+
}
|
|
3093
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
3094
|
+
const instruction = {
|
|
3095
|
+
accounts: [
|
|
3096
|
+
getAccountMeta(accounts.vault),
|
|
3097
|
+
getAccountMeta(accounts.policyAccount),
|
|
3098
|
+
getAccountMeta(accounts.signer),
|
|
3099
|
+
getAccountMeta(accounts.systemProgram)
|
|
3100
|
+
],
|
|
3101
|
+
programAddress,
|
|
3102
|
+
data: getInitializeLimitTransferInstructionDataEncoder().encode(
|
|
3103
|
+
args
|
|
3104
|
+
)
|
|
3105
|
+
};
|
|
3106
|
+
return instruction;
|
|
3107
|
+
}
|
|
3108
|
+
function getInitializeLimitTransferInstruction(input, config2) {
|
|
3109
|
+
const programAddress = config2?.programAddress ?? POLICY_LIMIT_TRANSFER_PROGRAM_ADDRESS;
|
|
3110
|
+
const originalAccounts = {
|
|
3111
|
+
vault: { value: input.vault ?? null, isWritable: false },
|
|
3112
|
+
policyAccount: { value: input.policyAccount ?? null, isWritable: true },
|
|
3113
|
+
signer: { value: input.signer ?? null, isWritable: true },
|
|
3114
|
+
systemProgram: { value: input.systemProgram ?? null, isWritable: false }
|
|
3115
|
+
};
|
|
3116
|
+
const accounts = originalAccounts;
|
|
3117
|
+
const args = { ...input };
|
|
3118
|
+
if (!accounts.systemProgram.value) {
|
|
3119
|
+
accounts.systemProgram.value = "11111111111111111111111111111111";
|
|
3120
|
+
}
|
|
3121
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
3122
|
+
const instruction = {
|
|
3123
|
+
accounts: [
|
|
3124
|
+
getAccountMeta(accounts.vault),
|
|
3125
|
+
getAccountMeta(accounts.policyAccount),
|
|
3126
|
+
getAccountMeta(accounts.signer),
|
|
3127
|
+
getAccountMeta(accounts.systemProgram)
|
|
3128
|
+
],
|
|
3129
|
+
programAddress,
|
|
3130
|
+
data: getInitializeLimitTransferInstructionDataEncoder().encode(
|
|
3131
|
+
args
|
|
3132
|
+
)
|
|
3133
|
+
};
|
|
3134
|
+
return instruction;
|
|
3135
|
+
}
|
|
3136
|
+
function parseInitializeLimitTransferInstruction(instruction) {
|
|
3137
|
+
if (instruction.accounts.length < 4) {
|
|
3138
|
+
throw new Error("Not enough accounts");
|
|
3139
|
+
}
|
|
3140
|
+
let accountIndex = 0;
|
|
3141
|
+
const getNextAccount = () => {
|
|
3142
|
+
const accountMeta = instruction.accounts[accountIndex];
|
|
3143
|
+
accountIndex += 1;
|
|
3144
|
+
return accountMeta;
|
|
3145
|
+
};
|
|
3146
|
+
return {
|
|
3147
|
+
programAddress: instruction.programAddress,
|
|
3148
|
+
accounts: {
|
|
3149
|
+
vault: getNextAccount(),
|
|
3150
|
+
policyAccount: getNextAccount(),
|
|
3151
|
+
signer: getNextAccount(),
|
|
3152
|
+
systemProgram: getNextAccount()
|
|
3153
|
+
},
|
|
3154
|
+
data: getInitializeLimitTransferInstructionDataDecoder().decode(
|
|
3155
|
+
instruction.data
|
|
3156
|
+
)
|
|
3157
|
+
};
|
|
3158
|
+
}
|
|
3159
|
+
var APPROVE_DISCRIMINATOR = new Uint8Array([
|
|
3160
|
+
69,
|
|
3161
|
+
74,
|
|
3162
|
+
217,
|
|
3163
|
+
36,
|
|
3164
|
+
115,
|
|
3165
|
+
117,
|
|
3166
|
+
97,
|
|
3167
|
+
76
|
|
3168
|
+
]);
|
|
3169
|
+
function getApproveDiscriminatorBytes() {
|
|
3170
|
+
return kit.fixEncoderSize(kit.getBytesEncoder(), 8).encode(APPROVE_DISCRIMINATOR);
|
|
3171
|
+
}
|
|
3172
|
+
function getApproveInstructionDataEncoder() {
|
|
3173
|
+
return kit.transformEncoder(
|
|
3174
|
+
kit.getStructEncoder([["discriminator", kit.fixEncoderSize(kit.getBytesEncoder(), 8)]]),
|
|
3175
|
+
(value) => ({ ...value, discriminator: APPROVE_DISCRIMINATOR })
|
|
3176
|
+
);
|
|
3177
|
+
}
|
|
3178
|
+
function getApproveInstructionDataDecoder() {
|
|
3179
|
+
return kit.getStructDecoder([
|
|
3180
|
+
["discriminator", kit.fixDecoderSize(kit.getBytesDecoder(), 8)]
|
|
3181
|
+
]);
|
|
3182
|
+
}
|
|
3183
|
+
function getApproveInstructionDataCodec() {
|
|
3184
|
+
return kit.combineCodec(
|
|
3185
|
+
getApproveInstructionDataEncoder(),
|
|
3186
|
+
getApproveInstructionDataDecoder()
|
|
3187
|
+
);
|
|
3188
|
+
}
|
|
3189
|
+
function getApproveInstruction(input, config2) {
|
|
3190
|
+
const programAddress = config2?.programAddress ?? POLICY_MULTISIG_PROGRAM_ADDRESS;
|
|
3191
|
+
const originalAccounts = {
|
|
3192
|
+
policyAccount: { value: input.policyAccount ?? null, isWritable: true },
|
|
3193
|
+
owner: { value: input.owner ?? null, isWritable: false }
|
|
3194
|
+
};
|
|
3195
|
+
const accounts = originalAccounts;
|
|
3196
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
3197
|
+
const instruction = {
|
|
3198
|
+
accounts: [
|
|
3199
|
+
getAccountMeta(accounts.policyAccount),
|
|
3200
|
+
getAccountMeta(accounts.owner)
|
|
3201
|
+
],
|
|
3202
|
+
programAddress,
|
|
3203
|
+
data: getApproveInstructionDataEncoder().encode({})
|
|
3204
|
+
};
|
|
3205
|
+
return instruction;
|
|
3206
|
+
}
|
|
3207
|
+
function parseApproveInstruction(instruction) {
|
|
3208
|
+
if (instruction.accounts.length < 2) {
|
|
3209
|
+
throw new Error("Not enough accounts");
|
|
3210
|
+
}
|
|
3211
|
+
let accountIndex = 0;
|
|
3212
|
+
const getNextAccount = () => {
|
|
3213
|
+
const accountMeta = instruction.accounts[accountIndex];
|
|
3214
|
+
accountIndex += 1;
|
|
3215
|
+
return accountMeta;
|
|
3216
|
+
};
|
|
3217
|
+
return {
|
|
3218
|
+
programAddress: instruction.programAddress,
|
|
3219
|
+
accounts: {
|
|
3220
|
+
policyAccount: getNextAccount(),
|
|
3221
|
+
owner: getNextAccount()
|
|
3222
|
+
},
|
|
3223
|
+
data: getApproveInstructionDataDecoder().decode(instruction.data)
|
|
3224
|
+
};
|
|
3225
|
+
}
|
|
3226
|
+
var INITIALIZE_MULTISIG_DISCRIMINATOR = new Uint8Array([
|
|
3227
|
+
220,
|
|
3228
|
+
130,
|
|
3229
|
+
117,
|
|
3230
|
+
21,
|
|
3231
|
+
27,
|
|
3232
|
+
227,
|
|
3233
|
+
78,
|
|
3234
|
+
213
|
|
3235
|
+
]);
|
|
3236
|
+
function getInitializeMultisigDiscriminatorBytes() {
|
|
3237
|
+
return kit.fixEncoderSize(kit.getBytesEncoder(), 8).encode(
|
|
3238
|
+
INITIALIZE_MULTISIG_DISCRIMINATOR
|
|
3239
|
+
);
|
|
3240
|
+
}
|
|
3241
|
+
function getInitializeMultisigInstructionDataEncoder() {
|
|
3242
|
+
return kit.transformEncoder(
|
|
3243
|
+
kit.getStructEncoder([
|
|
3244
|
+
["discriminator", kit.fixEncoderSize(kit.getBytesEncoder(), 8)],
|
|
3245
|
+
["owners", kit.getArrayEncoder(kit.getAddressEncoder())],
|
|
3246
|
+
["threshold", kit.getU64Encoder()]
|
|
3247
|
+
]),
|
|
3248
|
+
(value) => ({ ...value, discriminator: INITIALIZE_MULTISIG_DISCRIMINATOR })
|
|
3249
|
+
);
|
|
3250
|
+
}
|
|
3251
|
+
function getInitializeMultisigInstructionDataDecoder() {
|
|
3252
|
+
return kit.getStructDecoder([
|
|
3253
|
+
["discriminator", kit.fixDecoderSize(kit.getBytesDecoder(), 8)],
|
|
3254
|
+
["owners", kit.getArrayDecoder(kit.getAddressDecoder())],
|
|
3255
|
+
["threshold", kit.getU64Decoder()]
|
|
3256
|
+
]);
|
|
3257
|
+
}
|
|
3258
|
+
function getInitializeMultisigInstructionDataCodec() {
|
|
3259
|
+
return kit.combineCodec(
|
|
3260
|
+
getInitializeMultisigInstructionDataEncoder(),
|
|
3261
|
+
getInitializeMultisigInstructionDataDecoder()
|
|
3262
|
+
);
|
|
3263
|
+
}
|
|
3264
|
+
async function getInitializeMultisigInstructionAsync(input, config2) {
|
|
3265
|
+
const programAddress = config2?.programAddress ?? POLICY_MULTISIG_PROGRAM_ADDRESS;
|
|
3266
|
+
const originalAccounts = {
|
|
3267
|
+
vault: { value: input.vault ?? null, isWritable: false },
|
|
3268
|
+
policyAccount: { value: input.policyAccount ?? null, isWritable: true },
|
|
3269
|
+
signer: { value: input.signer ?? null, isWritable: true },
|
|
3270
|
+
systemProgram: { value: input.systemProgram ?? null, isWritable: false }
|
|
3271
|
+
};
|
|
3272
|
+
const accounts = originalAccounts;
|
|
3273
|
+
const args = { ...input };
|
|
3274
|
+
if (!accounts.policyAccount.value) {
|
|
3275
|
+
accounts.policyAccount.value = await kit.getProgramDerivedAddress({
|
|
3276
|
+
programAddress,
|
|
3277
|
+
seeds: [kit.getAddressEncoder().encode(expectAddress(accounts.vault.value))]
|
|
3278
|
+
});
|
|
3279
|
+
}
|
|
3280
|
+
if (!accounts.systemProgram.value) {
|
|
3281
|
+
accounts.systemProgram.value = "11111111111111111111111111111111";
|
|
3282
|
+
}
|
|
3283
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
3284
|
+
const instruction = {
|
|
3285
|
+
accounts: [
|
|
3286
|
+
getAccountMeta(accounts.vault),
|
|
3287
|
+
getAccountMeta(accounts.policyAccount),
|
|
3288
|
+
getAccountMeta(accounts.signer),
|
|
3289
|
+
getAccountMeta(accounts.systemProgram)
|
|
3290
|
+
],
|
|
3291
|
+
programAddress,
|
|
3292
|
+
data: getInitializeMultisigInstructionDataEncoder().encode(
|
|
3293
|
+
args
|
|
3294
|
+
)
|
|
3295
|
+
};
|
|
3296
|
+
return instruction;
|
|
3297
|
+
}
|
|
3298
|
+
function getInitializeMultisigInstruction(input, config2) {
|
|
3299
|
+
const programAddress = config2?.programAddress ?? POLICY_MULTISIG_PROGRAM_ADDRESS;
|
|
3300
|
+
const originalAccounts = {
|
|
3301
|
+
vault: { value: input.vault ?? null, isWritable: false },
|
|
3302
|
+
policyAccount: { value: input.policyAccount ?? null, isWritable: true },
|
|
3303
|
+
signer: { value: input.signer ?? null, isWritable: true },
|
|
3304
|
+
systemProgram: { value: input.systemProgram ?? null, isWritable: false }
|
|
3305
|
+
};
|
|
3306
|
+
const accounts = originalAccounts;
|
|
3307
|
+
const args = { ...input };
|
|
3308
|
+
if (!accounts.systemProgram.value) {
|
|
3309
|
+
accounts.systemProgram.value = "11111111111111111111111111111111";
|
|
3310
|
+
}
|
|
3311
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
3312
|
+
const instruction = {
|
|
3313
|
+
accounts: [
|
|
3314
|
+
getAccountMeta(accounts.vault),
|
|
3315
|
+
getAccountMeta(accounts.policyAccount),
|
|
3316
|
+
getAccountMeta(accounts.signer),
|
|
3317
|
+
getAccountMeta(accounts.systemProgram)
|
|
3318
|
+
],
|
|
3319
|
+
programAddress,
|
|
3320
|
+
data: getInitializeMultisigInstructionDataEncoder().encode(
|
|
3321
|
+
args
|
|
3322
|
+
)
|
|
3323
|
+
};
|
|
3324
|
+
return instruction;
|
|
3325
|
+
}
|
|
3326
|
+
function parseInitializeMultisigInstruction(instruction) {
|
|
3327
|
+
if (instruction.accounts.length < 4) {
|
|
3328
|
+
throw new Error("Not enough accounts");
|
|
3329
|
+
}
|
|
3330
|
+
let accountIndex = 0;
|
|
3331
|
+
const getNextAccount = () => {
|
|
3332
|
+
const accountMeta = instruction.accounts[accountIndex];
|
|
3333
|
+
accountIndex += 1;
|
|
3334
|
+
return accountMeta;
|
|
3335
|
+
};
|
|
3336
|
+
return {
|
|
3337
|
+
programAddress: instruction.programAddress,
|
|
3338
|
+
accounts: {
|
|
3339
|
+
vault: getNextAccount(),
|
|
3340
|
+
policyAccount: getNextAccount(),
|
|
3341
|
+
signer: getNextAccount(),
|
|
3342
|
+
systemProgram: getNextAccount()
|
|
3343
|
+
},
|
|
3344
|
+
data: getInitializeMultisigInstructionDataDecoder().decode(
|
|
3345
|
+
instruction.data
|
|
3346
|
+
)
|
|
3347
|
+
};
|
|
3348
|
+
}
|
|
3349
|
+
var INITIALIZE_OWNERS_DISCRIMINATOR = new Uint8Array([
|
|
3350
|
+
145,
|
|
3351
|
+
41,
|
|
3352
|
+
103,
|
|
3353
|
+
101,
|
|
3354
|
+
229,
|
|
3355
|
+
205,
|
|
3356
|
+
135,
|
|
3357
|
+
157
|
|
3358
|
+
]);
|
|
3359
|
+
function getInitializeOwnersDiscriminatorBytes() {
|
|
3360
|
+
return kit.fixEncoderSize(kit.getBytesEncoder(), 8).encode(
|
|
3361
|
+
INITIALIZE_OWNERS_DISCRIMINATOR
|
|
3362
|
+
);
|
|
3363
|
+
}
|
|
3364
|
+
function getInitializeOwnersInstructionDataEncoder() {
|
|
3365
|
+
return kit.transformEncoder(
|
|
3366
|
+
kit.getStructEncoder([
|
|
3367
|
+
["discriminator", kit.fixEncoderSize(kit.getBytesEncoder(), 8)],
|
|
3368
|
+
["owners", kit.getArrayEncoder(kit.getAddressEncoder())]
|
|
3369
|
+
]),
|
|
3370
|
+
(value) => ({ ...value, discriminator: INITIALIZE_OWNERS_DISCRIMINATOR })
|
|
3371
|
+
);
|
|
3372
|
+
}
|
|
3373
|
+
function getInitializeOwnersInstructionDataDecoder() {
|
|
3374
|
+
return kit.getStructDecoder([
|
|
3375
|
+
["discriminator", kit.fixDecoderSize(kit.getBytesDecoder(), 8)],
|
|
3376
|
+
["owners", kit.getArrayDecoder(kit.getAddressDecoder())]
|
|
3377
|
+
]);
|
|
3378
|
+
}
|
|
3379
|
+
function getInitializeOwnersInstructionDataCodec() {
|
|
3380
|
+
return kit.combineCodec(
|
|
3381
|
+
getInitializeOwnersInstructionDataEncoder(),
|
|
3382
|
+
getInitializeOwnersInstructionDataDecoder()
|
|
3383
|
+
);
|
|
3384
|
+
}
|
|
3385
|
+
async function getInitializeOwnersInstructionAsync(input, config2) {
|
|
3386
|
+
const programAddress = config2?.programAddress ?? POLICY_OWNERS_PROGRAM_ADDRESS;
|
|
3387
|
+
const originalAccounts = {
|
|
3388
|
+
vault: { value: input.vault ?? null, isWritable: false },
|
|
3389
|
+
policyAccount: { value: input.policyAccount ?? null, isWritable: true },
|
|
3390
|
+
signer: { value: input.signer ?? null, isWritable: true },
|
|
3391
|
+
systemProgram: { value: input.systemProgram ?? null, isWritable: false }
|
|
3392
|
+
};
|
|
3393
|
+
const accounts = originalAccounts;
|
|
3394
|
+
const args = { ...input };
|
|
3395
|
+
if (!accounts.policyAccount.value) {
|
|
3396
|
+
accounts.policyAccount.value = await kit.getProgramDerivedAddress({
|
|
3397
|
+
programAddress,
|
|
3398
|
+
seeds: [kit.getAddressEncoder().encode(expectAddress(accounts.vault.value))]
|
|
3399
|
+
});
|
|
3400
|
+
}
|
|
3401
|
+
if (!accounts.systemProgram.value) {
|
|
3402
|
+
accounts.systemProgram.value = "11111111111111111111111111111111";
|
|
3403
|
+
}
|
|
3404
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
3405
|
+
const instruction = {
|
|
3406
|
+
accounts: [
|
|
3407
|
+
getAccountMeta(accounts.vault),
|
|
3408
|
+
getAccountMeta(accounts.policyAccount),
|
|
3409
|
+
getAccountMeta(accounts.signer),
|
|
3410
|
+
getAccountMeta(accounts.systemProgram)
|
|
3411
|
+
],
|
|
3412
|
+
programAddress,
|
|
3413
|
+
data: getInitializeOwnersInstructionDataEncoder().encode(
|
|
3414
|
+
args
|
|
3415
|
+
)
|
|
3416
|
+
};
|
|
3417
|
+
return instruction;
|
|
3418
|
+
}
|
|
3419
|
+
function getInitializeOwnersInstruction(input, config2) {
|
|
3420
|
+
const programAddress = config2?.programAddress ?? POLICY_OWNERS_PROGRAM_ADDRESS;
|
|
3421
|
+
const originalAccounts = {
|
|
3422
|
+
vault: { value: input.vault ?? null, isWritable: false },
|
|
3423
|
+
policyAccount: { value: input.policyAccount ?? null, isWritable: true },
|
|
3424
|
+
signer: { value: input.signer ?? null, isWritable: true },
|
|
3425
|
+
systemProgram: { value: input.systemProgram ?? null, isWritable: false }
|
|
3426
|
+
};
|
|
3427
|
+
const accounts = originalAccounts;
|
|
3428
|
+
const args = { ...input };
|
|
3429
|
+
if (!accounts.systemProgram.value) {
|
|
3430
|
+
accounts.systemProgram.value = "11111111111111111111111111111111";
|
|
3431
|
+
}
|
|
3432
|
+
const getAccountMeta = getAccountMetaFactory(programAddress);
|
|
3433
|
+
const instruction = {
|
|
3434
|
+
accounts: [
|
|
3435
|
+
getAccountMeta(accounts.vault),
|
|
3436
|
+
getAccountMeta(accounts.policyAccount),
|
|
3437
|
+
getAccountMeta(accounts.signer),
|
|
3438
|
+
getAccountMeta(accounts.systemProgram)
|
|
3439
|
+
],
|
|
3440
|
+
programAddress,
|
|
3441
|
+
data: getInitializeOwnersInstructionDataEncoder().encode(
|
|
3442
|
+
args
|
|
3443
|
+
)
|
|
3444
|
+
};
|
|
3445
|
+
return instruction;
|
|
3446
|
+
}
|
|
3447
|
+
function parseInitializeOwnersInstruction(instruction) {
|
|
3448
|
+
if (instruction.accounts.length < 4) {
|
|
3449
|
+
throw new Error("Not enough accounts");
|
|
3450
|
+
}
|
|
3451
|
+
let accountIndex = 0;
|
|
3452
|
+
const getNextAccount = () => {
|
|
3453
|
+
const accountMeta = instruction.accounts[accountIndex];
|
|
3454
|
+
accountIndex += 1;
|
|
3455
|
+
return accountMeta;
|
|
3456
|
+
};
|
|
3457
|
+
return {
|
|
3458
|
+
programAddress: instruction.programAddress,
|
|
3459
|
+
accounts: {
|
|
3460
|
+
vault: getNextAccount(),
|
|
3461
|
+
policyAccount: getNextAccount(),
|
|
3462
|
+
signer: getNextAccount(),
|
|
3463
|
+
systemProgram: getNextAccount()
|
|
3464
|
+
},
|
|
3465
|
+
data: getInitializeOwnersInstructionDataDecoder().decode(instruction.data)
|
|
3466
|
+
};
|
|
3467
|
+
}
|
|
3468
|
+
function bigIntToSeed(value, byteLength = 8) {
|
|
3469
|
+
const buf = Buffer.alloc(byteLength);
|
|
3470
|
+
buf.writeBigUInt64LE(BigInt(value));
|
|
3471
|
+
return buf;
|
|
3472
|
+
}
|
|
3473
|
+
async function getPda(programAddress, seeds) {
|
|
3474
|
+
const addressEncoder = kit.getAddressEncoder();
|
|
3475
|
+
const seedsUint8Array = seeds.map((seed) => {
|
|
3476
|
+
if (typeof seed === "bigint" || typeof seed === "number") {
|
|
3477
|
+
return bigIntToSeed(BigInt(seed), 8);
|
|
3478
|
+
}
|
|
3479
|
+
try {
|
|
3480
|
+
const encoded = addressEncoder.encode(seed);
|
|
3481
|
+
return encoded;
|
|
3482
|
+
} catch {
|
|
3483
|
+
return new TextEncoder().encode(seed);
|
|
3484
|
+
}
|
|
3485
|
+
});
|
|
3486
|
+
return kit.getProgramDerivedAddress({
|
|
3487
|
+
seeds: seedsUint8Array,
|
|
3488
|
+
programAddress
|
|
3489
|
+
});
|
|
3490
|
+
}
|
|
3491
|
+
async function getVaultPda(seed) {
|
|
3492
|
+
const vault = await getPda(HYRO_PROTOCOL_PROGRAM_ADDRESS, [seed]);
|
|
3493
|
+
const authority = await getPda(HYRO_PROTOCOL_PROGRAM_ADDRESS, [vault[0]]);
|
|
3494
|
+
return [vault, authority];
|
|
3495
|
+
}
|
|
3496
|
+
async function getTransactionPda(vault, nonce) {
|
|
3497
|
+
return getPda(HYRO_PROTOCOL_PROGRAM_ADDRESS, [vault[0], nonce]);
|
|
3498
|
+
}
|
|
3499
|
+
async function getShareSignerPda(vault) {
|
|
3500
|
+
const addressEncoder = kit.getAddressEncoder();
|
|
3501
|
+
return kit.getProgramDerivedAddress({
|
|
3502
|
+
programAddress: HYRO_PROTOCOL_PROGRAM_ADDRESS,
|
|
3503
|
+
seeds: [
|
|
3504
|
+
new Uint8Array([
|
|
3505
|
+
118,
|
|
3506
|
+
97,
|
|
3507
|
+
117,
|
|
3508
|
+
108,
|
|
3509
|
+
116,
|
|
3510
|
+
95,
|
|
3511
|
+
115,
|
|
3512
|
+
104,
|
|
3513
|
+
97,
|
|
3514
|
+
114,
|
|
3515
|
+
101,
|
|
3516
|
+
95,
|
|
3517
|
+
115,
|
|
3518
|
+
105,
|
|
3519
|
+
103,
|
|
3520
|
+
110,
|
|
3521
|
+
101,
|
|
3522
|
+
114
|
|
3523
|
+
]),
|
|
3524
|
+
addressEncoder.encode(vault[0])
|
|
3525
|
+
]
|
|
3526
|
+
});
|
|
3527
|
+
}
|
|
3528
|
+
async function getShareMintPda(vault) {
|
|
3529
|
+
const addressEncoder = kit.getAddressEncoder();
|
|
3530
|
+
return kit.getProgramDerivedAddress({
|
|
3531
|
+
programAddress: HYRO_PROTOCOL_PROGRAM_ADDRESS,
|
|
3532
|
+
seeds: [
|
|
3533
|
+
new Uint8Array([
|
|
3534
|
+
118,
|
|
3535
|
+
97,
|
|
3536
|
+
117,
|
|
3537
|
+
108,
|
|
3538
|
+
116,
|
|
3539
|
+
95,
|
|
3540
|
+
115,
|
|
3541
|
+
104,
|
|
3542
|
+
97,
|
|
3543
|
+
114,
|
|
3544
|
+
101,
|
|
3545
|
+
95,
|
|
3546
|
+
109,
|
|
3547
|
+
105,
|
|
3548
|
+
110,
|
|
3549
|
+
116
|
|
3550
|
+
]),
|
|
3551
|
+
addressEncoder.encode(vault[0])
|
|
3552
|
+
]
|
|
3553
|
+
});
|
|
3554
|
+
}
|
|
3555
|
+
async function getVaultTokenAccountPda(vault, underlyingMint) {
|
|
3556
|
+
const addressEncoder = kit.getAddressEncoder();
|
|
3557
|
+
return kit.getProgramDerivedAddress({
|
|
3558
|
+
programAddress: HYRO_PROTOCOL_PROGRAM_ADDRESS,
|
|
3559
|
+
seeds: [
|
|
3560
|
+
new Uint8Array([
|
|
3561
|
+
118,
|
|
3562
|
+
97,
|
|
3563
|
+
117,
|
|
3564
|
+
108,
|
|
3565
|
+
116,
|
|
3566
|
+
95,
|
|
3567
|
+
116,
|
|
3568
|
+
111,
|
|
3569
|
+
107,
|
|
3570
|
+
101,
|
|
3571
|
+
110,
|
|
3572
|
+
95,
|
|
3573
|
+
97,
|
|
3574
|
+
99,
|
|
3575
|
+
99,
|
|
3576
|
+
111,
|
|
3577
|
+
117,
|
|
3578
|
+
110,
|
|
3579
|
+
116
|
|
3580
|
+
]),
|
|
3581
|
+
addressEncoder.encode(vault[0]),
|
|
3582
|
+
addressEncoder.encode(underlyingMint)
|
|
3583
|
+
]
|
|
3584
|
+
});
|
|
3585
|
+
}
|
|
3586
|
+
async function getPolicyAccountPda(vault, policyprogram) {
|
|
3587
|
+
const pda = await getPda(policyprogram, [vault[0]]);
|
|
3588
|
+
return pda[0];
|
|
3589
|
+
}
|
|
3590
|
+
|
|
3591
|
+
// src/sdk/vault.ts
|
|
3592
|
+
var Vault = class {
|
|
3593
|
+
/**
|
|
3594
|
+
* Create a new Vault instance
|
|
3595
|
+
* @param seed - The seed string for the vault
|
|
3596
|
+
* @param signer - The transaction signer
|
|
3597
|
+
* @param policy - The policy type to use for the vault
|
|
3598
|
+
*/
|
|
3599
|
+
constructor(seed, signer, policy, params) {
|
|
3600
|
+
this._seed = seed;
|
|
3601
|
+
this._signer = signer;
|
|
3602
|
+
this._policy = policy;
|
|
3603
|
+
this._policyProgram = this.getPolicyProgramAddress(policy);
|
|
3604
|
+
this._params = params;
|
|
3605
|
+
}
|
|
3606
|
+
getPolicyProgramAddress(policy) {
|
|
3607
|
+
switch (policy) {
|
|
3608
|
+
case "AllowAny" /* AllowAny */:
|
|
3609
|
+
return POLICY_ALLOW_ANY_PROGRAM_ADDRESS;
|
|
3610
|
+
case "DenyAll" /* DenyAll */:
|
|
3611
|
+
return POLICY_DENY_ALL_PROGRAM_ADDRESS;
|
|
3612
|
+
case "LimitTransfer" /* LimitTransfer */:
|
|
3613
|
+
return POLICY_LIMIT_TRANSFER_PROGRAM_ADDRESS;
|
|
3614
|
+
case "Owners" /* Owners */:
|
|
3615
|
+
return POLICY_OWNERS_PROGRAM_ADDRESS;
|
|
3616
|
+
case "Multisig" /* Multisig */:
|
|
3617
|
+
return POLICY_MULTISIG_PROGRAM_ADDRESS;
|
|
3618
|
+
}
|
|
3619
|
+
}
|
|
3620
|
+
/**
|
|
3621
|
+
* Initialize the vault (lazy loads PDAs)
|
|
3622
|
+
*/
|
|
3623
|
+
async initialize() {
|
|
3624
|
+
if (!this._vaultPda || !this._authorityPda || !this._policyAccount) {
|
|
3625
|
+
[this._vaultPda, this._authorityPda] = await getVaultPda(
|
|
3626
|
+
this._seed
|
|
3627
|
+
);
|
|
3628
|
+
this._policyAccount = await this.getPolicyAccountAddress();
|
|
3629
|
+
}
|
|
3630
|
+
}
|
|
3631
|
+
/**
|
|
3632
|
+
* Get the vault PDA
|
|
3633
|
+
*/
|
|
3634
|
+
async getVaultPda() {
|
|
3635
|
+
await this.initialize();
|
|
3636
|
+
return this._vaultPda;
|
|
3637
|
+
}
|
|
3638
|
+
/**
|
|
3639
|
+
* Get the authority PDA
|
|
3640
|
+
*/
|
|
3641
|
+
async getAuthorityPda() {
|
|
3642
|
+
await this.initialize();
|
|
3643
|
+
return this._authorityPda;
|
|
3644
|
+
}
|
|
3645
|
+
/**
|
|
3646
|
+
* Get the vault address
|
|
3647
|
+
*/
|
|
3648
|
+
async getAddress() {
|
|
3649
|
+
const pda = await this.getVaultPda();
|
|
3650
|
+
return pda[0];
|
|
3651
|
+
}
|
|
3652
|
+
/**
|
|
3653
|
+
* Get the authority address
|
|
3654
|
+
*/
|
|
3655
|
+
async getAuthorityAddress() {
|
|
3656
|
+
const pda = await this.getAuthorityPda();
|
|
3657
|
+
return pda[0];
|
|
3658
|
+
}
|
|
3659
|
+
/**
|
|
3660
|
+
* Get share signer PDA
|
|
3661
|
+
*/
|
|
3662
|
+
async getShareSignerPda() {
|
|
3663
|
+
const vaultPda = await this.getVaultPda();
|
|
3664
|
+
return getShareSignerPda(vaultPda);
|
|
3665
|
+
}
|
|
3666
|
+
/**
|
|
3667
|
+
* Get share mint PDA
|
|
3668
|
+
*/
|
|
3669
|
+
async getShareMintPda() {
|
|
3670
|
+
const vaultPda = await this.getVaultPda();
|
|
3671
|
+
return getShareMintPda(vaultPda);
|
|
3672
|
+
}
|
|
3673
|
+
/**
|
|
3674
|
+
* Get vault token account PDA
|
|
3675
|
+
*/
|
|
3676
|
+
async getVaultTokenAccountPda(underlyingMint) {
|
|
3677
|
+
const vaultPda = await this.getVaultPda();
|
|
3678
|
+
return getVaultTokenAccountPda(vaultPda, underlyingMint);
|
|
3679
|
+
}
|
|
3680
|
+
/**
|
|
3681
|
+
* Get transaction PDA for a given nonce
|
|
3682
|
+
*/
|
|
3683
|
+
async getTransactionPda(nonce) {
|
|
3684
|
+
const vaultPda = await this.getVaultPda();
|
|
3685
|
+
return getTransactionPda(vaultPda, nonce);
|
|
3686
|
+
}
|
|
3687
|
+
/**
|
|
3688
|
+
* Get policy account address
|
|
3689
|
+
*/
|
|
3690
|
+
async getPolicyAccountAddress() {
|
|
3691
|
+
switch (this._policy) {
|
|
3692
|
+
case "AllowAny" /* AllowAny */:
|
|
3693
|
+
return POLICY_ALLOW_ANY_PROGRAM_ADDRESS;
|
|
3694
|
+
case "DenyAll" /* DenyAll */:
|
|
3695
|
+
return POLICY_DENY_ALL_PROGRAM_ADDRESS;
|
|
3696
|
+
case "LimitTransfer" /* LimitTransfer */:
|
|
3697
|
+
return await getPolicyAccountPda(this._vaultPda, POLICY_LIMIT_TRANSFER_PROGRAM_ADDRESS);
|
|
3698
|
+
case "Owners" /* Owners */:
|
|
3699
|
+
return await getPolicyAccountPda(this._vaultPda, POLICY_OWNERS_PROGRAM_ADDRESS);
|
|
3700
|
+
case "Multisig" /* Multisig */:
|
|
3701
|
+
return await getPolicyAccountPda(this._vaultPda, POLICY_MULTISIG_PROGRAM_ADDRESS);
|
|
3702
|
+
default:
|
|
3703
|
+
return POLICY_ALLOW_ANY_PROGRAM_ADDRESS;
|
|
3704
|
+
}
|
|
3705
|
+
}
|
|
3706
|
+
/**
|
|
3707
|
+
* Build initialize vault instruction
|
|
3708
|
+
*/
|
|
3709
|
+
async buildInitializeVaultInstruction(params) {
|
|
3710
|
+
await this.initialize();
|
|
3711
|
+
const ixs = [];
|
|
3712
|
+
const shareSignerPda = await this.getShareSignerPda();
|
|
3713
|
+
const shareMintPda = await this.getShareMintPda();
|
|
3714
|
+
const vaultTokenAccountPda = await this.getVaultTokenAccountPda(params.underlyingMint);
|
|
3715
|
+
if (this._policy === "LimitTransfer" /* LimitTransfer */) {
|
|
3716
|
+
ixs.push(await getInitializeLimitTransferInstructionAsync({
|
|
3717
|
+
signer: this._signer,
|
|
3718
|
+
vault: this._vaultPda[0],
|
|
3719
|
+
min: decimal_default(this._params?.min).mul(10 ** 9).floor().toNumber(),
|
|
3720
|
+
max: decimal_default(this._params?.max).mul(10 ** 9).floor().toNumber(),
|
|
3721
|
+
policyAccount: this._policyAccount
|
|
3722
|
+
}));
|
|
3723
|
+
}
|
|
3724
|
+
if (this._policy === "Owners" /* Owners */) {
|
|
3725
|
+
ixs.push(await getInitializeOwnersInstructionAsync({
|
|
3726
|
+
vault: this._vaultPda[0],
|
|
3727
|
+
owners: this._params?.owners,
|
|
3728
|
+
policyAccount: this._policyAccount,
|
|
3729
|
+
signer: this._signer
|
|
3730
|
+
}));
|
|
3731
|
+
}
|
|
3732
|
+
if (this._policy === "Multisig" /* Multisig */) {
|
|
3733
|
+
ixs.push(await getInitializeMultisigInstructionAsync({
|
|
3734
|
+
vault: this._vaultPda[0],
|
|
3735
|
+
owners: this._params?.owners,
|
|
3736
|
+
threshold: this._params?.threshold,
|
|
3737
|
+
policyAccount: this._policyAccount,
|
|
3738
|
+
signer: this._signer
|
|
3739
|
+
}));
|
|
3740
|
+
}
|
|
3741
|
+
ixs.push(await getInitializeVaultInstructionAsync({
|
|
3742
|
+
seed: this._seed,
|
|
3743
|
+
policyProgram: this._policyProgram,
|
|
3744
|
+
underlyingMint: params.underlyingMint,
|
|
3745
|
+
signer: this._signer,
|
|
3746
|
+
vault: this._vaultPda[0],
|
|
3747
|
+
authority: this._authorityPda[0],
|
|
3748
|
+
shareSignerPda: shareSignerPda[0],
|
|
3749
|
+
shareMint: shareMintPda[0],
|
|
3750
|
+
vaultTokenAccount: vaultTokenAccountPda[0]
|
|
3751
|
+
}));
|
|
3752
|
+
return ixs;
|
|
3753
|
+
}
|
|
3754
|
+
/**
|
|
3755
|
+
* Create a transaction from an instruction (high-level method)
|
|
3756
|
+
* Automatically maps accounts and adds required program account
|
|
3757
|
+
*/
|
|
3758
|
+
async createTx(instruction) {
|
|
3759
|
+
await this.initialize();
|
|
3760
|
+
const nonce = Math.floor(Math.random() * 1e4) + 1;
|
|
3761
|
+
const vaultPda = this._vaultPda;
|
|
3762
|
+
const authorityPda = this._authorityPda;
|
|
3763
|
+
const transactionPda = await this.getTransactionPda(nonce);
|
|
3764
|
+
if (!instruction.accounts || !instruction.data) {
|
|
3765
|
+
throw new Error("Instruction must have accounts and data");
|
|
3766
|
+
}
|
|
3767
|
+
const accounts = [
|
|
3768
|
+
...instruction.accounts.map((account) => ({
|
|
3769
|
+
pubkey: account.address,
|
|
3770
|
+
// always false since it is meta tx
|
|
3771
|
+
isSigner: false,
|
|
3772
|
+
isWritable: account.role === kit.AccountRole.WRITABLE || account.role === kit.AccountRole.WRITABLE_SIGNER
|
|
3773
|
+
})),
|
|
3774
|
+
{
|
|
3775
|
+
pubkey: DROPPER_PROGRAM_ADDRESS,
|
|
3776
|
+
isSigner: false,
|
|
3777
|
+
isWritable: false
|
|
3778
|
+
}
|
|
3779
|
+
];
|
|
3780
|
+
const remainingAccounts = [
|
|
3781
|
+
{
|
|
3782
|
+
address: transactionPda[0],
|
|
3783
|
+
role: kit.AccountRole.READONLY
|
|
3784
|
+
},
|
|
3785
|
+
{
|
|
3786
|
+
address: this._policyAccount,
|
|
3787
|
+
role: kit.AccountRole.READONLY
|
|
3788
|
+
},
|
|
3789
|
+
{
|
|
3790
|
+
address: this._signer.address,
|
|
3791
|
+
role: kit.AccountRole.READONLY
|
|
3792
|
+
},
|
|
3793
|
+
{
|
|
3794
|
+
address: authorityPda[0],
|
|
3795
|
+
role: kit.AccountRole.READONLY
|
|
3796
|
+
}
|
|
3797
|
+
];
|
|
3798
|
+
const createTxInstruction = await getCreateTxInstructionAsync({
|
|
3799
|
+
vault: vaultPda[0],
|
|
3800
|
+
nonce,
|
|
3801
|
+
pid: DROPPER_PROGRAM_ADDRESS,
|
|
3802
|
+
accs: accounts,
|
|
3803
|
+
data: instruction.data,
|
|
3804
|
+
policyAccount: this._policyAccount || this._policyProgram,
|
|
3805
|
+
policyProgram: this._policyProgram,
|
|
3806
|
+
signer: this._signer,
|
|
3807
|
+
transaction: transactionPda[0],
|
|
3808
|
+
vaultSigner: authorityPda[0]
|
|
3809
|
+
});
|
|
3810
|
+
createTxInstruction.accounts.push(...remainingAccounts);
|
|
3811
|
+
return {
|
|
3812
|
+
ix: createTxInstruction,
|
|
3813
|
+
nonce: typeof nonce === "bigint" ? Number(nonce) : nonce,
|
|
3814
|
+
transferInstruction: instruction
|
|
3815
|
+
};
|
|
3816
|
+
}
|
|
3817
|
+
/**
|
|
3818
|
+
* Execute a transaction (high-level method)
|
|
3819
|
+
* Automatically resolves all required accounts
|
|
3820
|
+
*/
|
|
3821
|
+
async executeTx(createOp) {
|
|
3822
|
+
await this.initialize();
|
|
3823
|
+
const vaultPda = this._vaultPda;
|
|
3824
|
+
const authorityPda = this._authorityPda;
|
|
3825
|
+
const transactionPda = await this.getTransactionPda(createOp.nonce);
|
|
3826
|
+
if (!createOp.transferInstruction.accounts) {
|
|
3827
|
+
throw new Error("Original instruction must have accounts");
|
|
3828
|
+
}
|
|
3829
|
+
const remainingAccounts = [
|
|
3830
|
+
{
|
|
3831
|
+
address: transactionPda[0],
|
|
3832
|
+
role: kit.AccountRole.READONLY
|
|
3833
|
+
},
|
|
3834
|
+
{
|
|
3835
|
+
address: this._policyAccount,
|
|
3836
|
+
role: kit.AccountRole.READONLY
|
|
3837
|
+
},
|
|
3838
|
+
{
|
|
3839
|
+
address: this._signer.address,
|
|
3840
|
+
role: kit.AccountRole.READONLY
|
|
3841
|
+
},
|
|
3842
|
+
{
|
|
3843
|
+
address: authorityPda[0],
|
|
3844
|
+
role: kit.AccountRole.READONLY
|
|
3845
|
+
},
|
|
3846
|
+
...createOp.transferInstruction.accounts.map((account) => ({
|
|
3847
|
+
address: account.address,
|
|
3848
|
+
role: account.role === kit.AccountRole.WRITABLE_SIGNER || account.role === kit.AccountRole.READONLY_SIGNER ? kit.AccountRole.READONLY : account.role
|
|
3849
|
+
})),
|
|
3850
|
+
{
|
|
3851
|
+
address: DROPPER_PROGRAM_ADDRESS,
|
|
3852
|
+
role: kit.AccountRole.READONLY
|
|
3853
|
+
}
|
|
3854
|
+
];
|
|
3855
|
+
const executeTxInstruction = await getExecuteTxInstructionAsync({
|
|
3856
|
+
vault: vaultPda[0],
|
|
3857
|
+
transaction: transactionPda[0],
|
|
3858
|
+
policyAccount: this._policyAccount || this._policyProgram,
|
|
3859
|
+
policyProgram: this._policyProgram,
|
|
3860
|
+
signer: this._signer,
|
|
3861
|
+
vaultSigner: authorityPda[0]
|
|
3862
|
+
});
|
|
3863
|
+
executeTxInstruction.accounts.push(...remainingAccounts);
|
|
3864
|
+
return {
|
|
3865
|
+
ix: executeTxInstruction,
|
|
3866
|
+
nonce: createOp.nonce
|
|
3867
|
+
};
|
|
3868
|
+
}
|
|
3869
|
+
/**
|
|
3870
|
+
* Fetch vault account data
|
|
3871
|
+
*/
|
|
3872
|
+
async fetch(rpc) {
|
|
3873
|
+
const address = await this.getAddress();
|
|
3874
|
+
return fetchVault(rpc, address);
|
|
3875
|
+
}
|
|
3876
|
+
/**
|
|
3877
|
+
* Fetch transaction account data
|
|
3878
|
+
*/
|
|
3879
|
+
async fetchTransaction(rpc, nonce) {
|
|
3880
|
+
const transactionPda = await this.getTransactionPda(nonce);
|
|
3881
|
+
return fetchTransaction(rpc, transactionPda[0]);
|
|
3882
|
+
}
|
|
3883
|
+
};
|
|
3884
|
+
/*! Bundled license information:
|
|
3885
|
+
|
|
3886
|
+
decimal.js/decimal.mjs:
|
|
3887
|
+
(*!
|
|
3888
|
+
* decimal.js v10.6.0
|
|
3889
|
+
* An arbitrary-precision Decimal type for JavaScript.
|
|
3890
|
+
* https://github.com/MikeMcl/decimal.js
|
|
3891
|
+
* Copyright (c) 2025 Michael Mclaughlin <M8ch88l@gmail.com>
|
|
3892
|
+
* MIT Licence
|
|
3893
|
+
*)
|
|
3894
|
+
*/
|
|
3895
|
+
|
|
3896
|
+
exports.APPROVE_DISCRIMINATOR = APPROVE_DISCRIMINATOR;
|
|
3897
|
+
exports.CREATE_TX_DISCRIMINATOR = CREATE_TX_DISCRIMINATOR;
|
|
3898
|
+
exports.DROPPER_PROGRAM_ADDRESS = DROPPER_PROGRAM_ADDRESS;
|
|
3899
|
+
exports.EXECUTE_TX_DISCRIMINATOR = EXECUTE_TX_DISCRIMINATOR;
|
|
3900
|
+
exports.FeeCollectionFrequency = FeeCollectionFrequency;
|
|
3901
|
+
exports.HYRO_PROTOCOL_PROGRAM_ADDRESS = HYRO_PROTOCOL_PROGRAM_ADDRESS;
|
|
3902
|
+
exports.INITIALIZE_LIMIT_TRANSFER_DISCRIMINATOR = INITIALIZE_LIMIT_TRANSFER_DISCRIMINATOR;
|
|
3903
|
+
exports.INITIALIZE_MULTISIG_DISCRIMINATOR = INITIALIZE_MULTISIG_DISCRIMINATOR;
|
|
3904
|
+
exports.INITIALIZE_OWNERS_DISCRIMINATOR = INITIALIZE_OWNERS_DISCRIMINATOR;
|
|
3905
|
+
exports.INITIALIZE_VAULT_DISCRIMINATOR = INITIALIZE_VAULT_DISCRIMINATOR;
|
|
3906
|
+
exports.POLICY_ALLOW_ANY_PROGRAM_ADDRESS = POLICY_ALLOW_ANY_PROGRAM_ADDRESS;
|
|
3907
|
+
exports.POLICY_CHALLENGES_PROGRAM_ADDRESS = POLICY_CHALLENGES_PROGRAM_ADDRESS;
|
|
3908
|
+
exports.POLICY_DENY_ALL_PROGRAM_ADDRESS = POLICY_DENY_ALL_PROGRAM_ADDRESS;
|
|
3909
|
+
exports.POLICY_LIMIT_TRANSFER_PROGRAM_ADDRESS = POLICY_LIMIT_TRANSFER_PROGRAM_ADDRESS;
|
|
3910
|
+
exports.POLICY_MULTISIG_PROGRAM_ADDRESS = POLICY_MULTISIG_PROGRAM_ADDRESS;
|
|
3911
|
+
exports.POLICY_OWNERS_PROGRAM_ADDRESS = POLICY_OWNERS_PROGRAM_ADDRESS;
|
|
3912
|
+
exports.Policy = Policy;
|
|
3913
|
+
exports.TRANSFER_LAMPORTS_DISCRIMINATOR = TRANSFER_LAMPORTS_DISCRIMINATOR;
|
|
3914
|
+
exports.Vault = Vault;
|
|
3915
|
+
exports.getApproveDiscriminatorBytes = getApproveDiscriminatorBytes;
|
|
3916
|
+
exports.getApproveInstruction = getApproveInstruction;
|
|
3917
|
+
exports.getApproveInstructionDataCodec = getApproveInstructionDataCodec;
|
|
3918
|
+
exports.getApproveInstructionDataDecoder = getApproveInstructionDataDecoder;
|
|
3919
|
+
exports.getApproveInstructionDataEncoder = getApproveInstructionDataEncoder;
|
|
3920
|
+
exports.getCreateTxDiscriminatorBytes = getCreateTxDiscriminatorBytes;
|
|
3921
|
+
exports.getCreateTxInstruction = getCreateTxInstruction;
|
|
3922
|
+
exports.getCreateTxInstructionAsync = getCreateTxInstructionAsync;
|
|
3923
|
+
exports.getCreateTxInstructionDataCodec = getCreateTxInstructionDataCodec;
|
|
3924
|
+
exports.getCreateTxInstructionDataDecoder = getCreateTxInstructionDataDecoder;
|
|
3925
|
+
exports.getCreateTxInstructionDataEncoder = getCreateTxInstructionDataEncoder;
|
|
3926
|
+
exports.getExecuteTxDiscriminatorBytes = getExecuteTxDiscriminatorBytes;
|
|
3927
|
+
exports.getExecuteTxInstruction = getExecuteTxInstruction;
|
|
3928
|
+
exports.getExecuteTxInstructionAsync = getExecuteTxInstructionAsync;
|
|
3929
|
+
exports.getExecuteTxInstructionDataCodec = getExecuteTxInstructionDataCodec;
|
|
3930
|
+
exports.getExecuteTxInstructionDataDecoder = getExecuteTxInstructionDataDecoder;
|
|
3931
|
+
exports.getExecuteTxInstructionDataEncoder = getExecuteTxInstructionDataEncoder;
|
|
3932
|
+
exports.getFeeCollectionFrequencyCodec = getFeeCollectionFrequencyCodec;
|
|
3933
|
+
exports.getFeeCollectionFrequencyDecoder = getFeeCollectionFrequencyDecoder;
|
|
3934
|
+
exports.getFeeCollectionFrequencyEncoder = getFeeCollectionFrequencyEncoder;
|
|
3935
|
+
exports.getInitializeLimitTransferDiscriminatorBytes = getInitializeLimitTransferDiscriminatorBytes;
|
|
3936
|
+
exports.getInitializeLimitTransferInstruction = getInitializeLimitTransferInstruction;
|
|
3937
|
+
exports.getInitializeLimitTransferInstructionAsync = getInitializeLimitTransferInstructionAsync;
|
|
3938
|
+
exports.getInitializeLimitTransferInstructionDataCodec = getInitializeLimitTransferInstructionDataCodec;
|
|
3939
|
+
exports.getInitializeLimitTransferInstructionDataDecoder = getInitializeLimitTransferInstructionDataDecoder;
|
|
3940
|
+
exports.getInitializeLimitTransferInstructionDataEncoder = getInitializeLimitTransferInstructionDataEncoder;
|
|
3941
|
+
exports.getInitializeMultisigDiscriminatorBytes = getInitializeMultisigDiscriminatorBytes;
|
|
3942
|
+
exports.getInitializeMultisigInstruction = getInitializeMultisigInstruction;
|
|
3943
|
+
exports.getInitializeMultisigInstructionAsync = getInitializeMultisigInstructionAsync;
|
|
3944
|
+
exports.getInitializeMultisigInstructionDataCodec = getInitializeMultisigInstructionDataCodec;
|
|
3945
|
+
exports.getInitializeMultisigInstructionDataDecoder = getInitializeMultisigInstructionDataDecoder;
|
|
3946
|
+
exports.getInitializeMultisigInstructionDataEncoder = getInitializeMultisigInstructionDataEncoder;
|
|
3947
|
+
exports.getInitializeOwnersDiscriminatorBytes = getInitializeOwnersDiscriminatorBytes;
|
|
3948
|
+
exports.getInitializeOwnersInstruction = getInitializeOwnersInstruction;
|
|
3949
|
+
exports.getInitializeOwnersInstructionAsync = getInitializeOwnersInstructionAsync;
|
|
3950
|
+
exports.getInitializeOwnersInstructionDataCodec = getInitializeOwnersInstructionDataCodec;
|
|
3951
|
+
exports.getInitializeOwnersInstructionDataDecoder = getInitializeOwnersInstructionDataDecoder;
|
|
3952
|
+
exports.getInitializeOwnersInstructionDataEncoder = getInitializeOwnersInstructionDataEncoder;
|
|
3953
|
+
exports.getInitializeVaultDiscriminatorBytes = getInitializeVaultDiscriminatorBytes;
|
|
3954
|
+
exports.getInitializeVaultInstruction = getInitializeVaultInstruction;
|
|
3955
|
+
exports.getInitializeVaultInstructionAsync = getInitializeVaultInstructionAsync;
|
|
3956
|
+
exports.getInitializeVaultInstructionDataCodec = getInitializeVaultInstructionDataCodec;
|
|
3957
|
+
exports.getInitializeVaultInstructionDataDecoder = getInitializeVaultInstructionDataDecoder;
|
|
3958
|
+
exports.getInitializeVaultInstructionDataEncoder = getInitializeVaultInstructionDataEncoder;
|
|
3959
|
+
exports.getManagerFeeStructureCodec = getManagerFeeStructureCodec;
|
|
3960
|
+
exports.getManagerFeeStructureDecoder = getManagerFeeStructureDecoder;
|
|
3961
|
+
exports.getManagerFeeStructureEncoder = getManagerFeeStructureEncoder;
|
|
3962
|
+
exports.getPolicyAccountPda = getPolicyAccountPda;
|
|
3963
|
+
exports.getShareMintPda = getShareMintPda;
|
|
3964
|
+
exports.getShareSignerPda = getShareSignerPda;
|
|
3965
|
+
exports.getTransactionAccountCodec = getTransactionAccountCodec;
|
|
3966
|
+
exports.getTransactionAccountDecoder = getTransactionAccountDecoder;
|
|
3967
|
+
exports.getTransactionAccountEncoder = getTransactionAccountEncoder;
|
|
3968
|
+
exports.getTransactionPda = getTransactionPda;
|
|
3969
|
+
exports.getTransferLamportsDiscriminatorBytes = getTransferLamportsDiscriminatorBytes;
|
|
3970
|
+
exports.getTransferLamportsInstruction = getTransferLamportsInstruction;
|
|
3971
|
+
exports.getTransferLamportsInstructionDataCodec = getTransferLamportsInstructionDataCodec;
|
|
3972
|
+
exports.getTransferLamportsInstructionDataDecoder = getTransferLamportsInstructionDataDecoder;
|
|
3973
|
+
exports.getTransferLamportsInstructionDataEncoder = getTransferLamportsInstructionDataEncoder;
|
|
3974
|
+
exports.getVaultPda = getVaultPda;
|
|
3975
|
+
exports.getVaultTokenAccountPda = getVaultTokenAccountPda;
|
|
3976
|
+
exports.parseApproveInstruction = parseApproveInstruction;
|
|
3977
|
+
exports.parseCreateTxInstruction = parseCreateTxInstruction;
|
|
3978
|
+
exports.parseExecuteTxInstruction = parseExecuteTxInstruction;
|
|
3979
|
+
exports.parseInitializeLimitTransferInstruction = parseInitializeLimitTransferInstruction;
|
|
3980
|
+
exports.parseInitializeMultisigInstruction = parseInitializeMultisigInstruction;
|
|
3981
|
+
exports.parseInitializeOwnersInstruction = parseInitializeOwnersInstruction;
|
|
3982
|
+
exports.parseInitializeVaultInstruction = parseInitializeVaultInstruction;
|
|
3983
|
+
exports.parseTransferLamportsInstruction = parseTransferLamportsInstruction;
|
|
3984
|
+
//# sourceMappingURL=index.js.map
|
|
3985
|
+
//# sourceMappingURL=index.js.map
|