@carrot-protocol/clend-rpc 0.1.20-small1-dev-645a517 → 0.1.20-small1-dev-695fef7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/math.d.ts +6 -0
- package/dist/math.js +49 -0
- package/dist/math.js.map +1 -0
- package/package.json +1 -1
package/dist/math.d.ts
ADDED
package/dist/math.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bigNumberToWrappedI80F48 = bigNumberToWrappedI80F48;
|
|
4
|
+
exports.wrappedI80F48toBigNumber = wrappedI80F48toBigNumber;
|
|
5
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
6
|
+
/* ───────── Fixed-point params ───────── */
|
|
7
|
+
const FRAC_BITS = 48; // fractional bits
|
|
8
|
+
const TOTAL_BITS = 128; // signed i128
|
|
9
|
+
const TWO_POW_48 = new anchor_1.BN(1).shln(FRAC_BITS);
|
|
10
|
+
/* ───────── PUBLIC: encode ───────── */
|
|
11
|
+
function bigNumberToWrappedI80F48(v) {
|
|
12
|
+
const s = v instanceof anchor_1.BN ? v.toString() : v.toString().trim();
|
|
13
|
+
const negative = s.startsWith("-");
|
|
14
|
+
// integer fast-path
|
|
15
|
+
if (!s.includes(".")) {
|
|
16
|
+
return bnToWrapped(new anchor_1.BN(s).mul(TWO_POW_48));
|
|
17
|
+
}
|
|
18
|
+
// fractional path
|
|
19
|
+
const { int, frac, len } = splitDecimal(s);
|
|
20
|
+
const intPart = int.mul(TWO_POW_48);
|
|
21
|
+
const fracPart = frac.mul(TWO_POW_48).div(pow10(len));
|
|
22
|
+
const scaled = negative ? intPart.sub(fracPart) : intPart.add(fracPart);
|
|
23
|
+
return bnToWrapped(scaled);
|
|
24
|
+
}
|
|
25
|
+
/* ───────── PUBLIC: decode ───────── */
|
|
26
|
+
function wrappedI80F48toBigNumber(w) {
|
|
27
|
+
// caller decides whether to keep ×2^48 or >>48, or format for UI
|
|
28
|
+
return new anchor_1.BN(w.value, "le").fromTwos(TOTAL_BITS);
|
|
29
|
+
}
|
|
30
|
+
// internal
|
|
31
|
+
function bnToWrapped(scaled) {
|
|
32
|
+
if (scaled.bitLength() > TOTAL_BITS - 1) {
|
|
33
|
+
throw new Error("Value does not fit in signed 128 bits");
|
|
34
|
+
}
|
|
35
|
+
const bytes = scaled.toTwos(TOTAL_BITS).toArrayLike(Buffer, "le", 16);
|
|
36
|
+
return { value: Uint8Array.from(bytes) };
|
|
37
|
+
}
|
|
38
|
+
function splitDecimal(str) {
|
|
39
|
+
const [whole, frac = ""] = str.split(".");
|
|
40
|
+
return {
|
|
41
|
+
int: new anchor_1.BN(whole || "0"),
|
|
42
|
+
frac: new anchor_1.BN(frac || "0"),
|
|
43
|
+
len: frac.length,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function pow10(k) {
|
|
47
|
+
return new anchor_1.BN(10).pow(new anchor_1.BN(k));
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=math.js.map
|
package/dist/math.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"math.js","sourceRoot":"","sources":["../src/math.ts"],"names":[],"mappings":";;AAaA,4DAkBC;AAGD,4DAGC;AArCD,8CAAuC;AAEvC,4CAA4C;AAC5C,MAAM,SAAS,GAAG,EAAE,CAAC,CAAC,kBAAkB;AACxC,MAAM,UAAU,GAAG,GAAG,CAAC,CAAC,cAAc;AACtC,MAAM,UAAU,GAAG,IAAI,WAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;AAO7C,wCAAwC;AACxC,SAAgB,wBAAwB,CACtC,CAAuB;IAEvB,MAAM,CAAC,GAAG,CAAC,YAAY,WAAE,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC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