@chainflip/utils 2.2.0-alpha.2 → 2.2.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
3
+ const require_number = require('./number.cjs');
4
+ let bignumber_js = require("bignumber.js");
5
+ bignumber_js = require_runtime.__toESM(bignumber_js, 1);
6
+
7
+ //#region src/lending.ts
8
+ const ppmToBps = (ppmAmount) => Math.trunc(ppmAmount / 100);
9
+ const ppmToDecimal = (ppmAmount) => ppmAmount / 1e6;
10
+ const calculateLoanToValueBps = ({ totalBorrowAmountUsd, totalCollateralBalanceUsd }) => {
11
+ if (totalCollateralBalanceUsd === 0) return 0;
12
+ return Math.trunc(totalBorrowAmountUsd / totalCollateralBalanceUsd * 1e4);
13
+ };
14
+ const calculateBorrowPowerUsedBps = ({ totalBorrowAmountUsd, totalCollateralBalanceUsd, thresholdDecimal }) => {
15
+ if (totalCollateralBalanceUsd === 0 || thresholdDecimal === 0) return 0;
16
+ return Math.trunc(totalBorrowAmountUsd / (totalCollateralBalanceUsd * thresholdDecimal) * 1e4);
17
+ };
18
+ const calculateTotalEffectiveBorrowableAmount = ({ totalAmount, totalAvailableAmount, utilisationCapBps }) => {
19
+ const totalBorrowedAmount = totalAmount - totalAvailableAmount;
20
+ const totalEffectiveBorrowableAmount = new bignumber_js.default(totalAmount).multipliedBy(utilisationCapBps ?? 1e4).div(1e4).toFixed();
21
+ return require_number.bigintMax(BigInt(totalEffectiveBorrowableAmount) - totalBorrowedAmount, 0n);
22
+ };
23
+
24
+ //#endregion
25
+ exports.calculateBorrowPowerUsedBps = calculateBorrowPowerUsedBps;
26
+ exports.calculateLoanToValueBps = calculateLoanToValueBps;
27
+ exports.calculateTotalEffectiveBorrowableAmount = calculateTotalEffectiveBorrowableAmount;
28
+ exports.ppmToBps = ppmToBps;
29
+ exports.ppmToDecimal = ppmToDecimal;
@@ -0,0 +1,30 @@
1
+ //#region src/lending.d.ts
2
+ declare const ppmToBps: (ppmAmount: number) => number;
3
+ declare const ppmToDecimal: (ppmAmount: number) => number;
4
+ declare const calculateLoanToValueBps: ({
5
+ totalBorrowAmountUsd,
6
+ totalCollateralBalanceUsd
7
+ }: {
8
+ totalBorrowAmountUsd: number;
9
+ totalCollateralBalanceUsd: number;
10
+ }) => number;
11
+ declare const calculateBorrowPowerUsedBps: ({
12
+ totalBorrowAmountUsd,
13
+ totalCollateralBalanceUsd,
14
+ thresholdDecimal
15
+ }: {
16
+ totalBorrowAmountUsd: number;
17
+ totalCollateralBalanceUsd: number;
18
+ thresholdDecimal: number;
19
+ }) => number;
20
+ declare const calculateTotalEffectiveBorrowableAmount: ({
21
+ totalAmount,
22
+ totalAvailableAmount,
23
+ utilisationCapBps
24
+ }: {
25
+ totalAmount: bigint;
26
+ totalAvailableAmount: bigint;
27
+ utilisationCapBps?: number | null;
28
+ }) => bigint;
29
+ //#endregion
30
+ export { calculateBorrowPowerUsedBps, calculateLoanToValueBps, calculateTotalEffectiveBorrowableAmount, ppmToBps, ppmToDecimal };
@@ -0,0 +1,30 @@
1
+ //#region src/lending.d.ts
2
+ declare const ppmToBps: (ppmAmount: number) => number;
3
+ declare const ppmToDecimal: (ppmAmount: number) => number;
4
+ declare const calculateLoanToValueBps: ({
5
+ totalBorrowAmountUsd,
6
+ totalCollateralBalanceUsd
7
+ }: {
8
+ totalBorrowAmountUsd: number;
9
+ totalCollateralBalanceUsd: number;
10
+ }) => number;
11
+ declare const calculateBorrowPowerUsedBps: ({
12
+ totalBorrowAmountUsd,
13
+ totalCollateralBalanceUsd,
14
+ thresholdDecimal
15
+ }: {
16
+ totalBorrowAmountUsd: number;
17
+ totalCollateralBalanceUsd: number;
18
+ thresholdDecimal: number;
19
+ }) => number;
20
+ declare const calculateTotalEffectiveBorrowableAmount: ({
21
+ totalAmount,
22
+ totalAvailableAmount,
23
+ utilisationCapBps
24
+ }: {
25
+ totalAmount: bigint;
26
+ totalAvailableAmount: bigint;
27
+ utilisationCapBps?: number | null;
28
+ }) => bigint;
29
+ //#endregion
30
+ export { calculateBorrowPowerUsedBps, calculateLoanToValueBps, calculateTotalEffectiveBorrowableAmount, ppmToBps, ppmToDecimal };
@@ -0,0 +1,22 @@
1
+ import { bigintMax } from "./number.mjs";
2
+ import BigNumber from "bignumber.js";
3
+
4
+ //#region src/lending.ts
5
+ const ppmToBps = (ppmAmount) => Math.trunc(ppmAmount / 100);
6
+ const ppmToDecimal = (ppmAmount) => ppmAmount / 1e6;
7
+ const calculateLoanToValueBps = ({ totalBorrowAmountUsd, totalCollateralBalanceUsd }) => {
8
+ if (totalCollateralBalanceUsd === 0) return 0;
9
+ return Math.trunc(totalBorrowAmountUsd / totalCollateralBalanceUsd * 1e4);
10
+ };
11
+ const calculateBorrowPowerUsedBps = ({ totalBorrowAmountUsd, totalCollateralBalanceUsd, thresholdDecimal }) => {
12
+ if (totalCollateralBalanceUsd === 0 || thresholdDecimal === 0) return 0;
13
+ return Math.trunc(totalBorrowAmountUsd / (totalCollateralBalanceUsd * thresholdDecimal) * 1e4);
14
+ };
15
+ const calculateTotalEffectiveBorrowableAmount = ({ totalAmount, totalAvailableAmount, utilisationCapBps }) => {
16
+ const totalBorrowedAmount = totalAmount - totalAvailableAmount;
17
+ const totalEffectiveBorrowableAmount = new BigNumber(totalAmount).multipliedBy(utilisationCapBps ?? 1e4).div(1e4).toFixed();
18
+ return bigintMax(BigInt(totalEffectiveBorrowableAmount) - totalBorrowedAmount, 0n);
19
+ };
20
+
21
+ //#endregion
22
+ export { calculateBorrowPowerUsedBps, calculateLoanToValueBps, calculateTotalEffectiveBorrowableAmount, ppmToBps, ppmToDecimal };
package/dist/number.cjs CHANGED
@@ -2,6 +2,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
 
3
3
  //#region src/number.ts
4
4
  const hexEncodeNumber = (num) => `0x${num.toString(16)}`;
5
+ const bigintMin = (...args) => args.reduce((min, current) => current < min ? current : min);
6
+ const bigintMax = (...args) => args.reduce((max, current) => current > max ? current : max);
5
7
 
6
8
  //#endregion
9
+ exports.bigintMax = bigintMax;
10
+ exports.bigintMin = bigintMin;
7
11
  exports.hexEncodeNumber = hexEncodeNumber;
package/dist/number.d.cts CHANGED
@@ -2,5 +2,7 @@ import { HexString } from "./types.cjs";
2
2
 
3
3
  //#region src/number.d.ts
4
4
  declare const hexEncodeNumber: (num: number | bigint) => HexString;
5
+ declare const bigintMin: (...args: bigint[]) => bigint;
6
+ declare const bigintMax: (...args: bigint[]) => bigint;
5
7
  //#endregion
6
- export { hexEncodeNumber };
8
+ export { bigintMax, bigintMin, hexEncodeNumber };
package/dist/number.d.mts CHANGED
@@ -2,5 +2,7 @@ import { HexString } from "./types.mjs";
2
2
 
3
3
  //#region src/number.d.ts
4
4
  declare const hexEncodeNumber: (num: number | bigint) => HexString;
5
+ declare const bigintMin: (...args: bigint[]) => bigint;
6
+ declare const bigintMax: (...args: bigint[]) => bigint;
5
7
  //#endregion
6
- export { hexEncodeNumber };
8
+ export { bigintMax, bigintMin, hexEncodeNumber };
package/dist/number.mjs CHANGED
@@ -1,5 +1,7 @@
1
1
  //#region src/number.ts
2
2
  const hexEncodeNumber = (num) => `0x${num.toString(16)}`;
3
+ const bigintMin = (...args) => args.reduce((min, current) => current < min ? current : min);
4
+ const bigintMax = (...args) => args.reduce((max, current) => current > max ? current : max);
3
5
 
4
6
  //#endregion
5
- export { hexEncodeNumber };
7
+ export { bigintMax, bigintMin, hexEncodeNumber };
package/dist/tron.cjs CHANGED
@@ -15,6 +15,11 @@ const isValidTronAddress = (address) => {
15
15
  return false;
16
16
  }
17
17
  };
18
+ const tronAddressToHex = (address) => {
19
+ const bytes = require_base58.decode(address);
20
+ (0, assert.default)(bytes.length === 25, "Invalid tron address length");
21
+ return (0, _noble_hashes_utils_js.bytesToHex)(bytes.slice(1, 21));
22
+ };
18
23
  const hexToTronAddress = (hex) => {
19
24
  const bytes = (0, _noble_hashes_utils_js.hexToBytes)(hex.startsWith("0x") ? hex.slice(2) : hex);
20
25
  (0, assert.default)(bytes.length === 20, "Invalid hex length");
@@ -30,4 +35,5 @@ const hexToTronAddress = (hex) => {
30
35
 
31
36
  //#endregion
32
37
  exports.hexToTronAddress = hexToTronAddress;
33
- exports.isValidTronAddress = isValidTronAddress;
38
+ exports.isValidTronAddress = isValidTronAddress;
39
+ exports.tronAddressToHex = tronAddressToHex;
package/dist/tron.d.cts CHANGED
@@ -1,5 +1,6 @@
1
1
  //#region src/tron.d.ts
2
2
  declare const isValidTronAddress: (address: string) => boolean;
3
+ declare const tronAddressToHex: (address: string) => string;
3
4
  declare const hexToTronAddress: (hex: string) => string;
4
5
  //#endregion
5
- export { hexToTronAddress, isValidTronAddress };
6
+ export { hexToTronAddress, isValidTronAddress, tronAddressToHex };
package/dist/tron.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  //#region src/tron.d.ts
2
2
  declare const isValidTronAddress: (address: string) => boolean;
3
+ declare const tronAddressToHex: (address: string) => string;
3
4
  declare const hexToTronAddress: (hex: string) => string;
4
5
  //#endregion
5
- export { hexToTronAddress, isValidTronAddress };
6
+ export { hexToTronAddress, isValidTronAddress, tronAddressToHex };
package/dist/tron.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { decode, encode } from "./base58.mjs";
2
2
  import { sha256 } from "@noble/hashes/sha2.js";
3
- import { hexToBytes } from "@noble/hashes/utils.js";
3
+ import { bytesToHex, hexToBytes } from "@noble/hashes/utils.js";
4
4
  import assert from "assert";
5
5
 
6
6
  //#region src/tron.ts
@@ -12,6 +12,11 @@ const isValidTronAddress = (address) => {
12
12
  return false;
13
13
  }
14
14
  };
15
+ const tronAddressToHex = (address) => {
16
+ const bytes = decode(address);
17
+ assert(bytes.length === 25, "Invalid tron address length");
18
+ return bytesToHex(bytes.slice(1, 21));
19
+ };
15
20
  const hexToTronAddress = (hex) => {
16
21
  const bytes = hexToBytes(hex.startsWith("0x") ? hex.slice(2) : hex);
17
22
  assert(bytes.length === 20, "Invalid hex length");
@@ -26,4 +31,4 @@ const hexToTronAddress = (hex) => {
26
31
  };
27
32
 
28
33
  //#endregion
29
- export { hexToTronAddress, isValidTronAddress };
34
+ export { hexToTronAddress, isValidTronAddress, tronAddressToHex };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainflip/utils",
3
- "version": "2.2.0-alpha.2",
3
+ "version": "2.2.0-alpha.5",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",