@suilend/sui-fe 0.2.72
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 +8 -0
- package/index.d.ts +1 -0
- package/index.js +17 -0
- package/lib/api.d.ts +5 -0
- package/lib/api.js +63 -0
- package/lib/coinMetadata.d.ts +6 -0
- package/lib/coinMetadata.js +86 -0
- package/lib/coinType.d.ts +148 -0
- package/lib/coinType.js +320 -0
- package/lib/constants.d.ts +32 -0
- package/lib/constants.js +66 -0
- package/lib/format.d.ts +41 -0
- package/lib/format.js +225 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +25 -0
- package/lib/indexedDB.d.ts +4 -0
- package/lib/indexedDB.js +72 -0
- package/lib/msafe.d.ts +3 -0
- package/lib/msafe.js +15 -0
- package/lib/track.d.ts +2 -0
- package/lib/track.js +20 -0
- package/lib/transactions.d.ts +5 -0
- package/lib/transactions.js +32 -0
- package/package.json +1 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import BigNumber from "bignumber.js";
|
|
2
|
+
export declare const API_URL = "https://global.suilend.fi";
|
|
3
|
+
export declare const TOAST_DURATION_MS: number;
|
|
4
|
+
export declare const TX_TOAST_DURATION: number;
|
|
5
|
+
export declare const SUI_GAS_MIN = 1;
|
|
6
|
+
export declare const MAX_U64: BigNumber;
|
|
7
|
+
export declare const MS_PER_YEAR = 31556952000;
|
|
8
|
+
export declare enum RpcId {
|
|
9
|
+
TRITON_ONE = "tritonOne",
|
|
10
|
+
FULL_NODE = "fullNode",
|
|
11
|
+
ALL_THAT_NODE = "allThatNode",
|
|
12
|
+
CUSTOM = "custom"
|
|
13
|
+
}
|
|
14
|
+
export type Rpc = {
|
|
15
|
+
id: RpcId;
|
|
16
|
+
name: string;
|
|
17
|
+
url: string;
|
|
18
|
+
};
|
|
19
|
+
export declare const RPCS: Rpc[];
|
|
20
|
+
export declare enum ExplorerId {
|
|
21
|
+
SUI_SCAN = "suiScan",
|
|
22
|
+
SUI_VISION = "suiVision"
|
|
23
|
+
}
|
|
24
|
+
export type Explorer = {
|
|
25
|
+
id: ExplorerId;
|
|
26
|
+
name: string;
|
|
27
|
+
buildAddressUrl: (address: string) => string;
|
|
28
|
+
buildObjectUrl: (id: string) => string;
|
|
29
|
+
buildCoinUrl: (coinType: string) => string;
|
|
30
|
+
buildTxUrl: (digest: string) => string;
|
|
31
|
+
};
|
|
32
|
+
export declare const EXPLORERS: Explorer[];
|
package/lib/constants.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
var _a;
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.EXPLORERS = exports.ExplorerId = exports.RPCS = exports.RpcId = exports.MS_PER_YEAR = exports.MAX_U64 = exports.SUI_GAS_MIN = exports.TX_TOAST_DURATION = exports.TOAST_DURATION_MS = exports.API_URL = void 0;
|
|
8
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
9
|
+
exports.API_URL = "https://global.suilend.fi";
|
|
10
|
+
exports.TOAST_DURATION_MS = 5 * 1000;
|
|
11
|
+
exports.TX_TOAST_DURATION = 10 * 1000;
|
|
12
|
+
exports.SUI_GAS_MIN = 1;
|
|
13
|
+
exports.MAX_U64 = new bignumber_js_1.default(2).pow(64).minus(1);
|
|
14
|
+
exports.MS_PER_YEAR = 31556952000; // Approx. 1000 * 60 * 60 * 24 * 365;
|
|
15
|
+
var RpcId;
|
|
16
|
+
(function (RpcId) {
|
|
17
|
+
RpcId["TRITON_ONE"] = "tritonOne";
|
|
18
|
+
RpcId["FULL_NODE"] = "fullNode";
|
|
19
|
+
RpcId["ALL_THAT_NODE"] = "allThatNode";
|
|
20
|
+
RpcId["CUSTOM"] = "custom";
|
|
21
|
+
})(RpcId || (exports.RpcId = RpcId = {}));
|
|
22
|
+
exports.RPCS = [
|
|
23
|
+
{
|
|
24
|
+
id: RpcId.TRITON_ONE,
|
|
25
|
+
name: "Triton One",
|
|
26
|
+
url: `https://solendf-suishar-0c55.mainnet.sui.rpcpool.com/${(_a = process.env.NEXT_PUBLIC_SUI_TRITON_ONE_DEV_API_KEY) !== null && _a !== void 0 ? _a : ""}`,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
id: RpcId.FULL_NODE,
|
|
30
|
+
name: "Full Node",
|
|
31
|
+
url: "https://fullnode.mainnet.sui.io:443",
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: RpcId.ALL_THAT_NODE,
|
|
35
|
+
name: "All That Node",
|
|
36
|
+
url: "https://sui-mainnet-rpc.allthatnode.com",
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: RpcId.CUSTOM,
|
|
40
|
+
name: "Custom",
|
|
41
|
+
url: "",
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
var ExplorerId;
|
|
45
|
+
(function (ExplorerId) {
|
|
46
|
+
ExplorerId["SUI_SCAN"] = "suiScan";
|
|
47
|
+
ExplorerId["SUI_VISION"] = "suiVision";
|
|
48
|
+
})(ExplorerId || (exports.ExplorerId = ExplorerId = {}));
|
|
49
|
+
exports.EXPLORERS = [
|
|
50
|
+
{
|
|
51
|
+
id: ExplorerId.SUI_SCAN,
|
|
52
|
+
name: "Suiscan",
|
|
53
|
+
buildAddressUrl: (address) => `https://suiscan.xyz/mainnet/account/${address}`,
|
|
54
|
+
buildObjectUrl: (id) => `https://suiscan.xyz/mainnet/object/${id}`,
|
|
55
|
+
buildCoinUrl: (coinType) => `https://suiscan.xyz/mainnet/coin/${coinType}`,
|
|
56
|
+
buildTxUrl: (digest) => `https://suiscan.xyz/mainnet/tx/${digest}`,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
id: ExplorerId.SUI_VISION,
|
|
60
|
+
name: "SuiVision",
|
|
61
|
+
buildAddressUrl: (address) => `https://suivision.xyz/account/${address}`,
|
|
62
|
+
buildObjectUrl: (id) => `https://suivision.xyz/object/${id}`,
|
|
63
|
+
buildCoinUrl: (coinType) => `https://suivision.xyz/coin/${coinType}`,
|
|
64
|
+
buildTxUrl: (digest) => `https://suivision.xyz/txblock/${digest}`,
|
|
65
|
+
},
|
|
66
|
+
];
|
package/lib/format.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import BigNumber from "bignumber.js";
|
|
2
|
+
export declare const replace0x: (value: string) => string;
|
|
3
|
+
export declare const formatAddress: (value: string, length?: number) => string;
|
|
4
|
+
export declare const formatId: (value: string, length?: number) => string;
|
|
5
|
+
export declare const formatType: (value: string, length?: number) => string;
|
|
6
|
+
export declare const formatList: (array: string[]) => string;
|
|
7
|
+
export declare const formatInteger: (value: number, useGrouping?: boolean) => string;
|
|
8
|
+
export declare const formatRank: (rank: number) => string;
|
|
9
|
+
export declare const formatPercent: (value: BigNumber, options?: {
|
|
10
|
+
dp?: number;
|
|
11
|
+
useAccountingSign?: boolean;
|
|
12
|
+
}) => string;
|
|
13
|
+
export declare const formatLtvPercent: (value: BigNumber) => string;
|
|
14
|
+
export declare const formatBorrowWeight: (valueBps: BigNumber) => string;
|
|
15
|
+
export declare const formatDuration: (seconds: BigNumber) => string;
|
|
16
|
+
export declare const formatNumber: (value: BigNumber, options?: {
|
|
17
|
+
prefix?: string;
|
|
18
|
+
dp?: number;
|
|
19
|
+
minDp?: number;
|
|
20
|
+
roundingMode?: BigNumber.RoundingMode;
|
|
21
|
+
exact?: boolean;
|
|
22
|
+
useGrouping?: boolean;
|
|
23
|
+
trimTrailingZeros?: boolean;
|
|
24
|
+
roundLtMinToZero?: boolean;
|
|
25
|
+
}) => string;
|
|
26
|
+
export declare const formatUsd: (value: BigNumber, options?: {
|
|
27
|
+
dp?: number;
|
|
28
|
+
exact?: boolean;
|
|
29
|
+
}) => string;
|
|
30
|
+
export declare const formatPrice: (value: BigNumber) => string;
|
|
31
|
+
export declare const formatPoints: (value: BigNumber, options?: {
|
|
32
|
+
dp?: number;
|
|
33
|
+
}) => string;
|
|
34
|
+
export declare const formatToken: (value: BigNumber, options?: {
|
|
35
|
+
prefix?: string;
|
|
36
|
+
dp?: number;
|
|
37
|
+
exact?: boolean;
|
|
38
|
+
useGrouping?: boolean;
|
|
39
|
+
trimTrailingZeros?: boolean;
|
|
40
|
+
roundLtMinToZero?: boolean;
|
|
41
|
+
}) => string;
|
package/lib/format.js
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.formatToken = exports.formatPoints = exports.formatPrice = exports.formatUsd = exports.formatNumber = exports.formatDuration = exports.formatBorrowWeight = exports.formatLtvPercent = exports.formatPercent = exports.formatRank = exports.formatInteger = exports.formatList = exports.formatType = exports.formatId = exports.formatAddress = exports.replace0x = void 0;
|
|
7
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
8
|
+
const constants_1 = require("./constants");
|
|
9
|
+
const LOCALE = "en-US";
|
|
10
|
+
const shorten = (value, start, end) => {
|
|
11
|
+
return value.length > start + end
|
|
12
|
+
? `${value.slice(0, start)}…${value.slice(-end)}`
|
|
13
|
+
: value;
|
|
14
|
+
};
|
|
15
|
+
const replace0x = (value) => value.replace("0x", "0×");
|
|
16
|
+
exports.replace0x = replace0x;
|
|
17
|
+
const formatAddress = (value, length = 4) => {
|
|
18
|
+
if (length === 0)
|
|
19
|
+
return (0, exports.replace0x)(value);
|
|
20
|
+
return shorten((0, exports.replace0x)(value), length + (value.startsWith("0x") ? 2 : 0), length);
|
|
21
|
+
};
|
|
22
|
+
exports.formatAddress = formatAddress;
|
|
23
|
+
const formatId = (value, length = 4) => {
|
|
24
|
+
return shorten((0, exports.replace0x)(value), length + (value.startsWith("0x") ? 2 : 0), length);
|
|
25
|
+
};
|
|
26
|
+
exports.formatId = formatId;
|
|
27
|
+
const formatType = (value, length = 4) => {
|
|
28
|
+
const [id, module, type] = value.split("::");
|
|
29
|
+
return [
|
|
30
|
+
shorten((0, exports.replace0x)(id), length + (id.startsWith("0x") ? 2 : 0), length),
|
|
31
|
+
module,
|
|
32
|
+
type,
|
|
33
|
+
].join("::");
|
|
34
|
+
};
|
|
35
|
+
exports.formatType = formatType;
|
|
36
|
+
const formatList = (array) => {
|
|
37
|
+
if (array.length === 1)
|
|
38
|
+
return array[0];
|
|
39
|
+
if (array.length === 2)
|
|
40
|
+
return array.join(" and ");
|
|
41
|
+
return `${array.slice(0, -1).join(", ")}, and ${array.slice(-1)}`;
|
|
42
|
+
};
|
|
43
|
+
exports.formatList = formatList;
|
|
44
|
+
const formatInteger = (value, useGrouping) => Intl.NumberFormat(LOCALE, { useGrouping }).format(value);
|
|
45
|
+
exports.formatInteger = formatInteger;
|
|
46
|
+
const formatRank = (rank) => `#${(0, exports.formatInteger)(rank)}`;
|
|
47
|
+
exports.formatRank = formatRank;
|
|
48
|
+
const formatPercent = (value, options) => {
|
|
49
|
+
var _a, _b;
|
|
50
|
+
const dp = (_a = options === null || options === void 0 ? void 0 : options.dp) !== null && _a !== void 0 ? _a : 2;
|
|
51
|
+
const useAccountingSign = (_b = options === null || options === void 0 ? void 0 : options.useAccountingSign) !== null && _b !== void 0 ? _b : false;
|
|
52
|
+
const formattedValue = Intl.NumberFormat(LOCALE, {
|
|
53
|
+
style: "percent",
|
|
54
|
+
minimumFractionDigits: dp,
|
|
55
|
+
maximumFractionDigits: dp,
|
|
56
|
+
}).format(+value.div(100));
|
|
57
|
+
return !useAccountingSign || value.gte(0)
|
|
58
|
+
? formattedValue
|
|
59
|
+
: `(${formattedValue[0] === "-" ? formattedValue.slice(1) : formattedValue})`;
|
|
60
|
+
};
|
|
61
|
+
exports.formatPercent = formatPercent;
|
|
62
|
+
const formatLtvPercent = (value) => (0, exports.formatPercent)(value, { dp: 0 });
|
|
63
|
+
exports.formatLtvPercent = formatLtvPercent;
|
|
64
|
+
const formatBorrowWeight = (valueBps) => {
|
|
65
|
+
if (valueBps.eq(constants_1.MAX_U64))
|
|
66
|
+
return "∞";
|
|
67
|
+
const [integers, decimals] = valueBps.div(10000).toFixed(1).split(".");
|
|
68
|
+
const integersFormatted = (0, exports.formatInteger)(parseInt(integers));
|
|
69
|
+
const decimalsFormatted = ![undefined, "0"].includes(decimals)
|
|
70
|
+
? `.${decimals}`
|
|
71
|
+
: "";
|
|
72
|
+
return `${integersFormatted}${decimalsFormatted}`;
|
|
73
|
+
};
|
|
74
|
+
exports.formatBorrowWeight = formatBorrowWeight;
|
|
75
|
+
const formatDuration = (seconds) => {
|
|
76
|
+
if (seconds.lt(1))
|
|
77
|
+
return "<1s";
|
|
78
|
+
if (seconds.lt(60))
|
|
79
|
+
return `${seconds}s`;
|
|
80
|
+
if (seconds.lt(60 * 60)) {
|
|
81
|
+
const m = seconds.div(60).integerValue(bignumber_js_1.default.ROUND_DOWN);
|
|
82
|
+
const s = new bignumber_js_1.default(seconds.minus(m.times(60))).div(1);
|
|
83
|
+
return `${m.toFixed(0)}m ${s.toFixed(0)}s`;
|
|
84
|
+
}
|
|
85
|
+
if (seconds.lt(60 * 60 * 24)) {
|
|
86
|
+
const h = seconds.div(60 * 60).integerValue(bignumber_js_1.default.ROUND_DOWN);
|
|
87
|
+
const m = new bignumber_js_1.default(seconds.minus(h.times(60 * 60))).div(60);
|
|
88
|
+
return `${h.toFixed(0)}h ${m.toFixed(0)}m`;
|
|
89
|
+
}
|
|
90
|
+
const d = seconds.div(60 * 60 * 24).integerValue(bignumber_js_1.default.ROUND_DOWN);
|
|
91
|
+
const h = new bignumber_js_1.default(seconds.minus(d.times(60 * 60 * 24))).div(60 * 60);
|
|
92
|
+
return `${d.toFixed(0)}d ${h.toFixed(0)}h`;
|
|
93
|
+
};
|
|
94
|
+
exports.formatDuration = formatDuration;
|
|
95
|
+
const formatNumber = (value, options) => {
|
|
96
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
97
|
+
const prefix = (_a = options === null || options === void 0 ? void 0 : options.prefix) !== null && _a !== void 0 ? _a : "";
|
|
98
|
+
const dp = (_b = options === null || options === void 0 ? void 0 : options.dp) !== null && _b !== void 0 ? _b : 2;
|
|
99
|
+
let minDp = (_c = options === null || options === void 0 ? void 0 : options.minDp) !== null && _c !== void 0 ? _c : 0;
|
|
100
|
+
const roundingMode = (_d = options === null || options === void 0 ? void 0 : options.roundingMode) !== null && _d !== void 0 ? _d : bignumber_js_1.default.ROUND_HALF_UP;
|
|
101
|
+
const exact = (_e = options === null || options === void 0 ? void 0 : options.exact) !== null && _e !== void 0 ? _e : false;
|
|
102
|
+
const useGrouping = (_f = options === null || options === void 0 ? void 0 : options.useGrouping) !== null && _f !== void 0 ? _f : false;
|
|
103
|
+
const trimTrailingZeros = (_g = options === null || options === void 0 ? void 0 : options.trimTrailingZeros) !== null && _g !== void 0 ? _g : false;
|
|
104
|
+
const roundLtMinToZero = (_h = options === null || options === void 0 ? void 0 : options.roundLtMinToZero) !== null && _h !== void 0 ? _h : false;
|
|
105
|
+
// 0
|
|
106
|
+
if (value.eq(0))
|
|
107
|
+
return `${prefix}${trimTrailingZeros ? "0" : new bignumber_js_1.default(0).toFixed(dp)}`;
|
|
108
|
+
// 0 < value < minValue
|
|
109
|
+
const minValue = new bignumber_js_1.default(10).pow(-dp);
|
|
110
|
+
if (value.lt(minValue)) {
|
|
111
|
+
if (!roundLtMinToZero)
|
|
112
|
+
return `<${prefix}${minValue.toFixed(dp)}`;
|
|
113
|
+
return `${prefix}${trimTrailingZeros ? "0" : new bignumber_js_1.default(0).toFixed(dp)}`;
|
|
114
|
+
}
|
|
115
|
+
if (!exact) {
|
|
116
|
+
let _value = value;
|
|
117
|
+
let suffix = "";
|
|
118
|
+
if (_value.lt(1000 * 10)) {
|
|
119
|
+
}
|
|
120
|
+
else if (_value.lt(1000 ** 2)) {
|
|
121
|
+
_value = _value.div(1000 ** 1);
|
|
122
|
+
suffix = "K";
|
|
123
|
+
minDp = 0;
|
|
124
|
+
}
|
|
125
|
+
else if (_value.lt(1000 ** 3)) {
|
|
126
|
+
_value = _value.div(1000 ** 2);
|
|
127
|
+
suffix = "M";
|
|
128
|
+
minDp = 0;
|
|
129
|
+
}
|
|
130
|
+
else if (_value.lt(1000 ** 4)) {
|
|
131
|
+
_value = _value.div(1000 ** 3);
|
|
132
|
+
suffix = "B";
|
|
133
|
+
minDp = 0;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
_value = _value.div(1000 ** 4);
|
|
137
|
+
suffix = "T";
|
|
138
|
+
minDp = 0;
|
|
139
|
+
}
|
|
140
|
+
const maxDigits = 3;
|
|
141
|
+
const digitsCount = _value
|
|
142
|
+
.integerValue(bignumber_js_1.default.ROUND_DOWN)
|
|
143
|
+
.toString().length; // 1 <= _value < 1000, so digitsCount is in {1,2,3}
|
|
144
|
+
const newDp = Math.max(minDp, Math.min(dp, maxDigits - digitsCount));
|
|
145
|
+
let [integers, decimals] = _value.toFixed(newDp, roundingMode).split(".");
|
|
146
|
+
if (integers.length > digitsCount) {
|
|
147
|
+
// Rounded up from 999.9xx to 1000
|
|
148
|
+
[integers, decimals] = _value
|
|
149
|
+
.toFixed(newDp, bignumber_js_1.default.ROUND_DOWN)
|
|
150
|
+
.split(".");
|
|
151
|
+
}
|
|
152
|
+
const integersFormatted = (0, exports.formatInteger)(parseInt(integers), useGrouping);
|
|
153
|
+
const decimalsFormatted = decimals !== undefined ? `.${decimals}` : "";
|
|
154
|
+
return `${prefix}${integersFormatted}${decimalsFormatted}${suffix}`;
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
const [integers, decimals] = value.toFixed(dp, roundingMode).split(".");
|
|
158
|
+
const integersFormatted = (0, exports.formatInteger)(integers !== "" ? parseInt(integers) : 0, useGrouping);
|
|
159
|
+
let decimalsFormatted = decimals;
|
|
160
|
+
if (trimTrailingZeros && decimalsFormatted !== undefined) {
|
|
161
|
+
while (decimalsFormatted[decimalsFormatted.length - 1] === "0")
|
|
162
|
+
decimalsFormatted = decimalsFormatted.slice(0, -1);
|
|
163
|
+
if (decimalsFormatted.length === 0)
|
|
164
|
+
decimalsFormatted = undefined;
|
|
165
|
+
}
|
|
166
|
+
decimalsFormatted =
|
|
167
|
+
decimalsFormatted !== undefined ? `.${decimalsFormatted}` : "";
|
|
168
|
+
return `${prefix}${integersFormatted}${decimalsFormatted}`;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
exports.formatNumber = formatNumber;
|
|
172
|
+
const formatUsd = (value, options) => {
|
|
173
|
+
var _a, _b;
|
|
174
|
+
const dp = (_a = options === null || options === void 0 ? void 0 : options.dp) !== null && _a !== void 0 ? _a : 2;
|
|
175
|
+
const minDp = dp;
|
|
176
|
+
const exact = (_b = options === null || options === void 0 ? void 0 : options.exact) !== null && _b !== void 0 ? _b : false;
|
|
177
|
+
return (0, exports.formatNumber)(value, {
|
|
178
|
+
prefix: "$",
|
|
179
|
+
dp,
|
|
180
|
+
minDp,
|
|
181
|
+
roundingMode: bignumber_js_1.default.ROUND_HALF_UP,
|
|
182
|
+
exact,
|
|
183
|
+
useGrouping: true,
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
exports.formatUsd = formatUsd;
|
|
187
|
+
const formatPrice = (value) => {
|
|
188
|
+
return (0, exports.formatNumber)(value, {
|
|
189
|
+
prefix: "$",
|
|
190
|
+
dp: Math.max(0, -Math.floor(Math.log10(+value)) - 1) + 2, // 2sf
|
|
191
|
+
roundingMode: bignumber_js_1.default.ROUND_HALF_UP,
|
|
192
|
+
exact: true,
|
|
193
|
+
useGrouping: true,
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
exports.formatPrice = formatPrice;
|
|
197
|
+
const formatPoints = (value, options) => {
|
|
198
|
+
var _a;
|
|
199
|
+
const dp = (_a = options === null || options === void 0 ? void 0 : options.dp) !== null && _a !== void 0 ? _a : 0;
|
|
200
|
+
return (0, exports.formatNumber)(value, {
|
|
201
|
+
dp,
|
|
202
|
+
roundingMode: bignumber_js_1.default.ROUND_HALF_UP,
|
|
203
|
+
exact: true,
|
|
204
|
+
useGrouping: true,
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
exports.formatPoints = formatPoints;
|
|
208
|
+
const formatToken = (value, options) => {
|
|
209
|
+
var _a, _b, _c, _d, _e;
|
|
210
|
+
const dp = (_a = options === null || options === void 0 ? void 0 : options.dp) !== null && _a !== void 0 ? _a : 2;
|
|
211
|
+
const exact = (_b = options === null || options === void 0 ? void 0 : options.exact) !== null && _b !== void 0 ? _b : true;
|
|
212
|
+
const useGrouping = (_c = options === null || options === void 0 ? void 0 : options.useGrouping) !== null && _c !== void 0 ? _c : true;
|
|
213
|
+
const trimTrailingZeros = (_d = options === null || options === void 0 ? void 0 : options.trimTrailingZeros) !== null && _d !== void 0 ? _d : false;
|
|
214
|
+
const roundLtMinToZero = (_e = options === null || options === void 0 ? void 0 : options.roundLtMinToZero) !== null && _e !== void 0 ? _e : false;
|
|
215
|
+
return (0, exports.formatNumber)(value, {
|
|
216
|
+
prefix: options === null || options === void 0 ? void 0 : options.prefix,
|
|
217
|
+
dp,
|
|
218
|
+
roundingMode: bignumber_js_1.default.ROUND_DOWN,
|
|
219
|
+
exact,
|
|
220
|
+
useGrouping,
|
|
221
|
+
trimTrailingZeros,
|
|
222
|
+
roundLtMinToZero,
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
exports.formatToken = formatToken;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./api";
|
|
2
|
+
export * from "./coinMetadata";
|
|
3
|
+
export * from "./coinType";
|
|
4
|
+
export * from "./constants";
|
|
5
|
+
export * from "./format";
|
|
6
|
+
export * from "./indexedDB";
|
|
7
|
+
export * from "./msafe";
|
|
8
|
+
export * from "./track";
|
|
9
|
+
export * from "./transactions";
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./api"), exports);
|
|
18
|
+
__exportStar(require("./coinMetadata"), exports);
|
|
19
|
+
__exportStar(require("./coinType"), exports);
|
|
20
|
+
__exportStar(require("./constants"), exports);
|
|
21
|
+
__exportStar(require("./format"), exports);
|
|
22
|
+
__exportStar(require("./indexedDB"), exports);
|
|
23
|
+
__exportStar(require("./msafe"), exports);
|
|
24
|
+
__exportStar(require("./track"), exports);
|
|
25
|
+
__exportStar(require("./transactions"), exports);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Token } from "./coinMetadata";
|
|
2
|
+
export declare const initDB: () => Promise<IDBDatabase>;
|
|
3
|
+
export declare const getTokenMapByCoinTypeFromIndexedDB: (coinTypes: string[]) => Promise<Record<string, Token>>;
|
|
4
|
+
export declare const setTokenMapToIndexedDB: (tokenMap: Record<string, Token>) => Promise<void>;
|
package/lib/indexedDB.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.setTokenMapToIndexedDB = exports.getTokenMapByCoinTypeFromIndexedDB = exports.initDB = void 0;
|
|
13
|
+
const DB_NAME = "suilend";
|
|
14
|
+
const DB_VERSION = 1;
|
|
15
|
+
const TOKEN_STORE = "token";
|
|
16
|
+
const initDB = () => {
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
|
19
|
+
request.onerror = () => reject(request.error);
|
|
20
|
+
request.onsuccess = () => resolve(request.result);
|
|
21
|
+
request.onupgradeneeded = (event) => {
|
|
22
|
+
const db = event.target.result;
|
|
23
|
+
if (!db.objectStoreNames.contains(TOKEN_STORE)) {
|
|
24
|
+
db.createObjectStore(TOKEN_STORE, { keyPath: "coinType" });
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
exports.initDB = initDB;
|
|
30
|
+
const getTokenMapByCoinTypeFromIndexedDB = (coinTypes) => __awaiter(void 0, void 0, void 0, function* () {
|
|
31
|
+
if (coinTypes.length === 0)
|
|
32
|
+
return {};
|
|
33
|
+
const db = yield (0, exports.initDB)();
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const transaction = db.transaction(TOKEN_STORE, "readonly");
|
|
36
|
+
const store = transaction.objectStore(TOKEN_STORE);
|
|
37
|
+
const result = {};
|
|
38
|
+
const promises = coinTypes.map((coinType) => new Promise((resolve, reject) => {
|
|
39
|
+
const request = store.get(coinType);
|
|
40
|
+
request.onsuccess = () => {
|
|
41
|
+
if (request.result)
|
|
42
|
+
result[coinType] = request.result;
|
|
43
|
+
resolve();
|
|
44
|
+
};
|
|
45
|
+
request.onerror = () => reject(request.error);
|
|
46
|
+
}));
|
|
47
|
+
Promise.all(promises)
|
|
48
|
+
.then(() => resolve(result))
|
|
49
|
+
.catch(reject)
|
|
50
|
+
.finally(() => db.close());
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
exports.getTokenMapByCoinTypeFromIndexedDB = getTokenMapByCoinTypeFromIndexedDB;
|
|
54
|
+
const setTokenMapToIndexedDB = (tokenMap) => __awaiter(void 0, void 0, void 0, function* () {
|
|
55
|
+
if (Object.keys(tokenMap).length === 0)
|
|
56
|
+
return;
|
|
57
|
+
const db = yield (0, exports.initDB)();
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
const transaction = db.transaction(TOKEN_STORE, "readwrite");
|
|
60
|
+
const store = transaction.objectStore(TOKEN_STORE);
|
|
61
|
+
const promises = Object.entries(tokenMap).map(([coinType, token]) => new Promise((resolve, reject) => {
|
|
62
|
+
const request = store.put(token);
|
|
63
|
+
request.onsuccess = () => resolve();
|
|
64
|
+
request.onerror = () => reject(request.error);
|
|
65
|
+
}));
|
|
66
|
+
Promise.all(promises)
|
|
67
|
+
.then(() => resolve())
|
|
68
|
+
.catch(reject)
|
|
69
|
+
.finally(() => db.close());
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
exports.setTokenMapToIndexedDB = setTokenMapToIndexedDB;
|
package/lib/msafe.d.ts
ADDED
package/lib/msafe.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMsafeAppStoreUrl = exports.getMsafeBaseUrl = exports.isInMsafeApp = void 0;
|
|
4
|
+
const isInMsafeApp = () => {
|
|
5
|
+
var _a, _b;
|
|
6
|
+
return typeof window !== "undefined" &&
|
|
7
|
+
((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.ancestorOrigins) &&
|
|
8
|
+
Array.from(window.location.ancestorOrigins).length > 0 &&
|
|
9
|
+
((_b = Array.from(window.location.ancestorOrigins)[0]) === null || _b === void 0 ? void 0 : _b.includes("m-safe.io"));
|
|
10
|
+
};
|
|
11
|
+
exports.isInMsafeApp = isInMsafeApp;
|
|
12
|
+
const getMsafeBaseUrl = () => Array.from(window.location.ancestorOrigins)[0];
|
|
13
|
+
exports.getMsafeBaseUrl = getMsafeBaseUrl;
|
|
14
|
+
const getMsafeAppStoreUrl = (appName) => `${(0, exports.getMsafeBaseUrl)()}/store/${appName}`;
|
|
15
|
+
exports.getMsafeAppStoreUrl = getMsafeAppStoreUrl;
|
package/lib/track.d.ts
ADDED
package/lib/track.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const mixpanel_browser_1 = __importDefault(require("mixpanel-browser"));
|
|
7
|
+
const track = (eventName, properties) => {
|
|
8
|
+
const projectToken = process.env.NEXT_PUBLIC_MIXPANEL_PROJECT_TOKEN;
|
|
9
|
+
if (!projectToken)
|
|
10
|
+
return;
|
|
11
|
+
try {
|
|
12
|
+
setTimeout(() => {
|
|
13
|
+
mixpanel_browser_1.default.track(eventName, properties);
|
|
14
|
+
}, 250);
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
console.error(err);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
exports.default = track;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { SuiTransactionBlockResponse } from "@mysten/sui/client";
|
|
2
|
+
import BigNumber from "bignumber.js";
|
|
3
|
+
import { Token } from "./coinMetadata";
|
|
4
|
+
export declare const getTotalGasFee: (res: SuiTransactionBlockResponse) => BigNumber;
|
|
5
|
+
export declare const getBalanceChange: (res: SuiTransactionBlockResponse, address: string, token: Token, multiplier?: -1 | 1) => BigNumber | undefined;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getBalanceChange = exports.getTotalGasFee = void 0;
|
|
7
|
+
const utils_1 = require("@mysten/sui/utils");
|
|
8
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
9
|
+
const coinType_1 = require("./coinType");
|
|
10
|
+
const getTotalGasFee = (res) => res.effects
|
|
11
|
+
? new bignumber_js_1.default(+res.effects.gasUsed.computationCost +
|
|
12
|
+
+res.effects.gasUsed.storageCost -
|
|
13
|
+
+res.effects.gasUsed.storageRebate).div(10 ** utils_1.SUI_DECIMALS)
|
|
14
|
+
: new bignumber_js_1.default(0);
|
|
15
|
+
exports.getTotalGasFee = getTotalGasFee;
|
|
16
|
+
const getBalanceChange = (res, address, token, multiplier = 1) => {
|
|
17
|
+
if (!res.balanceChanges)
|
|
18
|
+
return undefined;
|
|
19
|
+
const balanceChanges = res.balanceChanges.filter((bc) => {
|
|
20
|
+
var _a;
|
|
21
|
+
return (0, utils_1.normalizeStructTag)(bc.coinType) === token.coinType &&
|
|
22
|
+
((_a = bc.owner) === null || _a === void 0 ? void 0 : _a.AddressOwner) === address;
|
|
23
|
+
});
|
|
24
|
+
if (balanceChanges.length === 0)
|
|
25
|
+
return undefined;
|
|
26
|
+
return balanceChanges
|
|
27
|
+
.reduce((acc, balanceChange) => acc.plus(new bignumber_js_1.default(+balanceChange.amount)), new bignumber_js_1.default(0))
|
|
28
|
+
.div(10 ** token.decimals)
|
|
29
|
+
.plus((0, coinType_1.isSui)(token.coinType) ? (0, exports.getTotalGasFee)(res) : 0)
|
|
30
|
+
.times(multiplier);
|
|
31
|
+
};
|
|
32
|
+
exports.getBalanceChange = getBalanceChange;
|
package/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"@suilend/sui-fe","version":"0.2.72","private":false,"description":"A collection of TypeScript frontend libraries","author":"Suilend","license":"MIT","main":"./index.js","exports":{".":"./index.js","./lib/api":"./lib/api.js","./lib/coinMetadata":"./lib/coinMetadata.js","./lib/coinType":"./lib/coinType.js","./lib/constants":"./lib/constants.js","./lib/format":"./lib/format.js","./lib":"./lib/index.js","./lib/indexedDB":"./lib/indexedDB.js","./lib/msafe":"./lib/msafe.js","./lib/track":"./lib/track.js","./lib/transactions":"./lib/transactions.js"},"types":"./index.js","scripts":{"build":"rm -rf ./dist && bun tsc","eslint":"eslint --fix \"./src/**/*.ts\"","prettier":"prettier --write \"./src/**/*\"","lint":"bun eslint && bun prettier && bun tsc --noEmit","release":"bun run build && bun ts-node ./release.ts && cd ./dist && npm publish --access public"},"repository":{"type":"git","url":"git+https://github.com/suilend/sui-fe.git"},"bugs":{"url":"https://github.com/suilend/sui-fe/issues"},"dependencies":{"@pythnetwork/pyth-sui-js":"^2.1.0","bignumber.js":"^9.1.2","lodash":"^4.17.21","mixpanel-browser":"^2.56.0","next":"^15.0.3","p-limit":"3.1.0"},"devDependencies":{"@tsconfig/recommended":"^1.0.8","@types/lodash":"^4.17.13","@types/mixpanel-browser":"^2.50.2","@types/node":"^22.9.0","@typescript-eslint/eslint-plugin":"^8.14.0","@typescript-eslint/parser":"^8.14.0","eslint":"^9.14.0","eslint-config-next":"^15.0.3","eslint-config-prettier":"^9.1.0","eslint-plugin-import":"^2.31.0","eslint-plugin-prettier":"^5.2.1","prettier":"^3.3.3","ts-node":"^10.9.2","typescript":"^5.6.3"},"peerDependencies":{"@mysten/sui":"1.28.2","date-fns":"^4.1.0"}}
|