aftermath-ts-sdk 1.2.63 → 1.2.64-docs.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 +61 -27
- package/dist/general/dynamicGas/dynamicGas.d.ts +34 -0
- package/dist/general/dynamicGas/dynamicGas.d.ts.map +1 -1
- package/dist/general/dynamicGas/dynamicGas.js +34 -0
- package/dist/general/dynamicGas/dynamicGasTypes.d.ts +25 -0
- package/dist/general/dynamicGas/dynamicGasTypes.d.ts.map +1 -1
- package/dist/general/prices/prices.d.ts +73 -0
- package/dist/general/prices/prices.d.ts.map +1 -1
- package/dist/general/prices/prices.js +73 -0
- package/dist/general/providers/aftermath.d.ts +106 -25
- package/dist/general/providers/aftermath.d.ts.map +1 -1
- package/dist/general/providers/aftermath.js +108 -27
- package/dist/general/providers/aftermathApi.d.ts +127 -8
- package/dist/general/providers/aftermathApi.d.ts.map +1 -1
- package/dist/general/providers/aftermathApi.js +132 -12
- package/dist/general/types/castingTypes.d.ts +0 -7
- package/dist/general/types/castingTypes.d.ts.map +1 -1
- package/dist/general/types/castingTypes.js +6 -0
- package/dist/general/types/generalTypes.d.ts +228 -7
- package/dist/general/types/generalTypes.d.ts.map +1 -1
- package/dist/general/types/suiTypes.d.ts +0 -16
- package/dist/general/types/suiTypes.d.ts.map +1 -1
- package/dist/general/types/suiTypes.js +3 -25
- package/dist/general/utils/casting.d.ts +159 -3
- package/dist/general/utils/casting.d.ts.map +1 -1
- package/dist/general/utils/casting.js +162 -16
- package/dist/general/utils/fixedUtils.d.ts +89 -2
- package/dist/general/utils/fixedUtils.d.ts.map +1 -1
- package/dist/general/utils/fixedUtils.js +81 -4
- package/dist/general/utils/helpers.d.ts +320 -9
- package/dist/general/utils/helpers.d.ts.map +1 -1
- package/dist/general/utils/helpers.js +350 -89
- package/dist/general/utils/iFixedUtils.d.ts +64 -0
- package/dist/general/utils/iFixedUtils.d.ts.map +1 -1
- package/dist/general/utils/iFixedUtils.js +64 -0
- package/dist/general/wallet/wallet.d.ts +75 -0
- package/dist/general/wallet/wallet.d.ts.map +1 -1
- package/dist/general/wallet/wallet.js +75 -5
- package/dist/packages/auth/auth.d.ts +113 -0
- package/dist/packages/auth/auth.d.ts.map +1 -1
- package/dist/packages/auth/auth.js +129 -15
- package/dist/packages/auth/authTypes.d.ts +66 -0
- package/dist/packages/auth/authTypes.d.ts.map +1 -1
- package/dist/packages/coin/api/coinApi.d.ts.map +1 -1
- package/dist/packages/coin/api/coinApi.js +28 -20
- package/dist/packages/coin/coin.d.ts +250 -0
- package/dist/packages/coin/coin.d.ts.map +1 -1
- package/dist/packages/coin/coin.js +238 -14
- package/dist/packages/coin/coinTypes.d.ts +93 -0
- package/dist/packages/coin/coinTypes.d.ts.map +1 -1
- package/dist/packages/dca/dca.d.ts +124 -35
- package/dist/packages/dca/dca.d.ts.map +1 -1
- package/dist/packages/dca/dca.js +127 -37
- package/dist/packages/dca/dcaTypes.d.ts +213 -28
- package/dist/packages/dca/dcaTypes.d.ts.map +1 -1
- package/dist/packages/farms/farms.d.ts +184 -1
- package/dist/packages/farms/farms.d.ts.map +1 -1
- package/dist/packages/farms/farms.js +185 -11
- package/dist/packages/farms/farmsStakedPosition.d.ts +140 -6
- package/dist/packages/farms/farmsStakedPosition.d.ts.map +1 -1
- package/dist/packages/farms/farmsStakedPosition.js +189 -144
- package/dist/packages/farms/farmsStakingPool.d.ts +161 -1
- package/dist/packages/farms/farmsStakingPool.d.ts.map +1 -1
- package/dist/packages/farms/farmsStakingPool.js +176 -70
- package/dist/packages/farms/farmsTypes.d.ts +283 -2
- package/dist/packages/farms/farmsTypes.d.ts.map +1 -1
- package/dist/packages/farms/farmsTypes.js +18 -0
- package/dist/packages/limitOrders/limitOrders.d.ts +108 -21
- package/dist/packages/limitOrders/limitOrders.d.ts.map +1 -1
- package/dist/packages/limitOrders/limitOrders.js +110 -22
- package/dist/packages/limitOrders/limitOrdersTypes.d.ts +142 -0
- package/dist/packages/limitOrders/limitOrdersTypes.d.ts.map +1 -1
- package/dist/packages/multisig/multisig.d.ts +33 -4
- package/dist/packages/multisig/multisig.d.ts.map +1 -1
- package/dist/packages/multisig/multisig.js +33 -4
- package/dist/packages/multisig/multisigTypes.d.ts +17 -0
- package/dist/packages/multisig/multisigTypes.d.ts.map +1 -1
- package/dist/packages/perpetuals/api/perpetualsApi.d.ts.map +1 -1
- package/dist/packages/pools/pool.d.ts +327 -85
- package/dist/packages/pools/pool.d.ts.map +1 -1
- package/dist/packages/pools/pool.js +333 -91
- package/dist/packages/pools/pools.d.ts +299 -37
- package/dist/packages/pools/pools.d.ts.map +1 -1
- package/dist/packages/pools/pools.js +306 -52
- package/dist/packages/pools/poolsTypes.d.ts +267 -2
- package/dist/packages/pools/poolsTypes.d.ts.map +1 -1
- package/dist/packages/referralVault/referralVault.d.ts +37 -0
- package/dist/packages/referralVault/referralVault.d.ts.map +1 -1
- package/dist/packages/referralVault/referralVault.js +37 -0
- package/dist/packages/router/router.d.ts +213 -17
- package/dist/packages/router/router.d.ts.map +1 -1
- package/dist/packages/router/router.js +214 -18
- package/dist/packages/router/routerTypes.d.ts +198 -14
- package/dist/packages/router/routerTypes.d.ts.map +1 -1
- package/dist/packages/staking/staking.d.ts +301 -43
- package/dist/packages/staking/staking.d.ts.map +1 -1
- package/dist/packages/staking/staking.js +308 -51
- package/dist/packages/staking/stakingTypes.d.ts +426 -6
- package/dist/packages/staking/stakingTypes.d.ts.map +1 -1
- package/dist/packages/staking/stakingTypes.js +19 -0
- package/dist/packages/sui/sui.d.ts +39 -0
- package/dist/packages/sui/sui.d.ts.map +1 -1
- package/dist/packages/sui/sui.js +39 -0
- package/dist/packages/userData/userData.d.ts +66 -12
- package/dist/packages/userData/userData.d.ts.map +1 -1
- package/dist/packages/userData/userData.js +66 -12
- package/dist/packages/userData/userDataTypes.d.ts +19 -0
- package/dist/packages/userData/userDataTypes.d.ts.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +0 -1
- package/package.json +1 -1
- package/dist/general/historicalData/historicalData.d.ts +0 -15
- package/dist/general/historicalData/historicalData.d.ts.map +0 -1
- package/dist/general/historicalData/historicalData.js +0 -45
- package/dist/general/historicalData/historicalDataTypes.d.ts +0 -10
- package/dist/general/historicalData/historicalDataTypes.d.ts.map +0 -1
- package/dist/general/historicalData/historicalDataTypes.js +0 -2
|
@@ -13,11 +13,33 @@ exports.Coin = void 0;
|
|
|
13
13
|
const caller_1 = require("../../general/utils/caller");
|
|
14
14
|
const helpers_1 = require("../../general/utils/helpers");
|
|
15
15
|
const prices_1 = require("../../general/prices/prices");
|
|
16
|
+
/**
|
|
17
|
+
* The `Coin` class provides functionality to manage and inspect coin types,
|
|
18
|
+
* retrieve metadata and prices, and convert balances with respect to coin decimals.
|
|
19
|
+
* It can be instantiated with or without a specific `coinType` for convenience.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
*
|
|
24
|
+
* const afSdk = new Aftermath("MAINNET");
|
|
25
|
+
* await afSdk.init(); // initialize provider
|
|
26
|
+
*
|
|
27
|
+
* const coin = afSdk.Coin("0x2::sui::SUI");
|
|
28
|
+
*
|
|
29
|
+
* const metadata = await coin.getCoinMetadata(); // fetch metadata for SUI coin
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
16
32
|
class Coin extends caller_1.Caller {
|
|
17
33
|
// =========================================================================
|
|
18
34
|
// Constructor
|
|
19
35
|
// =========================================================================
|
|
20
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new instance of `Coin`.
|
|
38
|
+
*
|
|
39
|
+
* @param coinType - The coin's type string (e.g., "0x2::sui::SUI"). If omitted, methods that require a type will need it passed in manually.
|
|
40
|
+
* @param config - Optional caller configuration (network, access token).
|
|
41
|
+
* @param Provider - An optional `AftermathApi` instance for coin-specific API calls.
|
|
42
|
+
*/
|
|
21
43
|
constructor(coinType = undefined, config, Provider) {
|
|
22
44
|
super(config, "coins");
|
|
23
45
|
this.coinType = coinType;
|
|
@@ -25,6 +47,10 @@ class Coin extends caller_1.Caller {
|
|
|
25
47
|
// =========================================================================
|
|
26
48
|
// Private Helpers
|
|
27
49
|
// =========================================================================
|
|
50
|
+
/**
|
|
51
|
+
* Internal method to retrieve a specialized coin-related API from `AftermathApi`.
|
|
52
|
+
* Throws an error if no provider is set.
|
|
53
|
+
*/
|
|
28
54
|
this.useProvider = () => {
|
|
29
55
|
var _a;
|
|
30
56
|
const provider = (_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Coin();
|
|
@@ -33,6 +59,7 @@ class Coin extends caller_1.Caller {
|
|
|
33
59
|
return provider;
|
|
34
60
|
};
|
|
35
61
|
this.coinType = coinType;
|
|
62
|
+
// Pre-extract segments for convenience
|
|
36
63
|
this.coinTypePackageName = this.coinType
|
|
37
64
|
? Coin.getCoinTypePackageName(this.coinType)
|
|
38
65
|
: "";
|
|
@@ -49,6 +76,19 @@ class Coin extends caller_1.Caller {
|
|
|
49
76
|
// =========================================================================
|
|
50
77
|
// Inspections
|
|
51
78
|
// =========================================================================
|
|
79
|
+
/**
|
|
80
|
+
* Retrieves the decimals for multiple coins by calling the Aftermath API for metadata
|
|
81
|
+
* and extracting the `decimals` property.
|
|
82
|
+
*
|
|
83
|
+
* @param inputs - An object containing an array of coin types.
|
|
84
|
+
* @returns An object mapping each coin type to a numeric decimal count.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const decimals = await coin.getCoinsToDecimals({ coins: ["0x2::sui::SUI", "0x<...>"] });
|
|
89
|
+
* console.log(decimals); // { "0x2::sui::SUI": 9, "0x<...>": 6 }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
52
92
|
getCoinsToDecimals(inputs) {
|
|
53
93
|
return __awaiter(this, void 0, void 0, function* () {
|
|
54
94
|
const { coins } = inputs;
|
|
@@ -61,6 +101,20 @@ class Coin extends caller_1.Caller {
|
|
|
61
101
|
return coinsToDecimals;
|
|
62
102
|
});
|
|
63
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Fetches the metadata (name, symbol, decimals) for this coin type or a provided one,
|
|
106
|
+
* caching it if already requested.
|
|
107
|
+
*
|
|
108
|
+
* @param coin - Optionally override the constructor coinType.
|
|
109
|
+
* @returns The `CoinMetadaWithInfo` object containing metadata and optional external references.
|
|
110
|
+
* @throws If neither constructor nor argument coinType is available.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const metadata = await coin.getCoinMetadata("0x2::sui::SUI");
|
|
115
|
+
* console.log(metadata.name, metadata.symbol, metadata.decimals);
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
64
118
|
getCoinMetadata(coin) {
|
|
65
119
|
var _a;
|
|
66
120
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -74,14 +128,48 @@ class Coin extends caller_1.Caller {
|
|
|
74
128
|
return metadata;
|
|
75
129
|
});
|
|
76
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Fetches metadata for multiple coins at once, returning an array in the same order
|
|
133
|
+
* as the coin types requested.
|
|
134
|
+
*
|
|
135
|
+
* @param inputs - An object with `coins`, an array of coin types.
|
|
136
|
+
* @returns An array of `CoinMetadaWithInfo` with length matching `coins`.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```typescript
|
|
140
|
+
* const metas = await coin.getCoinMetadatas({
|
|
141
|
+
* coins: ["0x2::sui::SUI", "0x<custom::TOKEN>"]
|
|
142
|
+
* });
|
|
143
|
+
* console.log(metas[0].symbol, metas[1].symbol);
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
77
146
|
getCoinMetadatas(inputs) {
|
|
78
147
|
return __awaiter(this, void 0, void 0, function* () {
|
|
79
148
|
return this.fetchApi("metadata", inputs);
|
|
80
149
|
});
|
|
81
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Manually sets the metadata in this Coin instance, storing it in `this.metadata`.
|
|
153
|
+
*
|
|
154
|
+
* @param metadata - A `CoinMetadaWithInfo` object to cache in this instance.
|
|
155
|
+
*/
|
|
82
156
|
setCoinMetadata(metadata) {
|
|
83
157
|
this.metadata = metadata;
|
|
84
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Retrieves price information (including current price and 24h change) for this coin or a provided coin.
|
|
161
|
+
* If already fetched, it returns the cached data.
|
|
162
|
+
*
|
|
163
|
+
* @param coin - Optionally override the constructor coinType.
|
|
164
|
+
* @returns A `CoinPriceInfo` with `price` and `priceChange24HoursPercentage`.
|
|
165
|
+
* @throws If no valid coin type is present.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```typescript
|
|
169
|
+
* const priceInfo = await coin.getPrice("0x2::sui::SUI");
|
|
170
|
+
* console.log(priceInfo.price, priceInfo.priceChange24HoursPercentage);
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
85
173
|
getPrice(coin) {
|
|
86
174
|
var _a;
|
|
87
175
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -99,9 +187,26 @@ class Coin extends caller_1.Caller {
|
|
|
99
187
|
return priceInfo;
|
|
100
188
|
});
|
|
101
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* Manually sets the price info in this Coin instance, storing it in `this.priceInfo`.
|
|
192
|
+
*
|
|
193
|
+
* @param priceInfo - A `CoinPriceInfo` object to cache in this instance.
|
|
194
|
+
*/
|
|
102
195
|
setPriceInfo(priceInfo) {
|
|
103
196
|
this.priceInfo = priceInfo;
|
|
104
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* Fetches a list of "verified" coin types from the Aftermath backend. Verified coins
|
|
200
|
+
* typically pass certain safety or liquidity checks.
|
|
201
|
+
*
|
|
202
|
+
* @returns An array of `CoinType` strings that are considered verified.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* const verified = await coin.getVerifiedCoins();
|
|
207
|
+
* console.log(verified); // e.g. ["0x2::sui::SUI", "0x...::MYCOIN", ...]
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
105
210
|
getVerifiedCoins() {
|
|
106
211
|
return __awaiter(this, void 0, void 0, function* () {
|
|
107
212
|
return this.fetchApi("verified");
|
|
@@ -112,10 +217,27 @@ exports.Coin = Coin;
|
|
|
112
217
|
// =========================================================================
|
|
113
218
|
// Constants
|
|
114
219
|
// =========================================================================
|
|
220
|
+
/**
|
|
221
|
+
* Static configuration and defaults for Sui coin types, including the standard
|
|
222
|
+
* SUI coin type, default decimals, and coin object type path.
|
|
223
|
+
*/
|
|
115
224
|
Coin.constants = {
|
|
225
|
+
/**
|
|
226
|
+
* The canonical coin type string for SUI.
|
|
227
|
+
*/
|
|
116
228
|
suiCoinType: "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI",
|
|
229
|
+
/**
|
|
230
|
+
* The default number of decimals for SUI (9).
|
|
231
|
+
*/
|
|
117
232
|
suiCoinDecimals: 9,
|
|
233
|
+
/**
|
|
234
|
+
* The canonical coin object type path for Sui's Move module, used in verifying coin objects.
|
|
235
|
+
*/
|
|
118
236
|
coinObjectType: "0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin",
|
|
237
|
+
/**
|
|
238
|
+
* Default decimals for various blockchains or ecosystems. For instance,
|
|
239
|
+
* "sui" => 9, "evm" => 18, etc.
|
|
240
|
+
*/
|
|
119
241
|
defaultCoinDecimals: {
|
|
120
242
|
sui: 9,
|
|
121
243
|
evm: 18,
|
|
@@ -128,7 +250,13 @@ Coin.constants = {
|
|
|
128
250
|
// =========================================================================
|
|
129
251
|
// Coin Type
|
|
130
252
|
// =========================================================================
|
|
131
|
-
|
|
253
|
+
/**
|
|
254
|
+
* Extracts the Move package name portion from a coin type string.
|
|
255
|
+
* E.g., "0x2::sui::SUI" => "sui".
|
|
256
|
+
*
|
|
257
|
+
* @param coin - The coin type string (e.g., "0x2::sui::SUI").
|
|
258
|
+
* @returns The middle segment of the type or empty string if not parseable.
|
|
259
|
+
*/
|
|
132
260
|
Coin.getCoinTypePackageName = (coin) => {
|
|
133
261
|
const splitCoin = coin.split("::");
|
|
134
262
|
if (splitCoin.length !== 3)
|
|
@@ -138,7 +266,13 @@ Coin.getCoinTypePackageName = (coin) => {
|
|
|
138
266
|
return "";
|
|
139
267
|
return packageName;
|
|
140
268
|
};
|
|
141
|
-
|
|
269
|
+
/**
|
|
270
|
+
* Extracts the final part of the coin type (the symbol or short name).
|
|
271
|
+
* For example, "0x2::sui::SUI" => "SUI".
|
|
272
|
+
*
|
|
273
|
+
* @param coin - The coin type string.
|
|
274
|
+
* @returns The extracted symbol or empty string if not found.
|
|
275
|
+
*/
|
|
142
276
|
Coin.getCoinTypeSymbol = (coin) => {
|
|
143
277
|
const startIndex = coin.lastIndexOf("::") + 2;
|
|
144
278
|
// NOTE: should error if coin is not a valid coin type instead of empty string ?
|
|
@@ -149,32 +283,87 @@ Coin.getCoinTypeSymbol = (coin) => {
|
|
|
149
283
|
const displayType = coin.slice(startIndex, endIndex);
|
|
150
284
|
return displayType;
|
|
151
285
|
};
|
|
286
|
+
/**
|
|
287
|
+
* Extracts the inner generic argument of a coin type if present. E.g.,
|
|
288
|
+
* "0x2::coin::Coin<0x2::sui::SUI>" => "0x2::sui::SUI".
|
|
289
|
+
*
|
|
290
|
+
* @param coin - The coin type with a possible `<...>` suffix.
|
|
291
|
+
* @returns The inner type or an empty string if not found.
|
|
292
|
+
*/
|
|
152
293
|
Coin.getInnerCoinType = (coin) => coin.includes("<") ? coin.split("<")[1].slice(0, -1) : "";
|
|
294
|
+
/**
|
|
295
|
+
* If a `KeyType` string references a type in angle brackets, extracts the type
|
|
296
|
+
* inside. Typically for "0x2::coin::Coin<0x2::mycoin::MYCOIN>" -> "0x2::mycoin::MYCOIN".
|
|
297
|
+
*
|
|
298
|
+
* @param keyType - The key type string to parse.
|
|
299
|
+
* @returns The substring inside `<...>` or the original if no brackets found.
|
|
300
|
+
*/
|
|
153
301
|
Coin.coinTypeFromKeyType = (keyType) => {
|
|
154
302
|
const startIndex = keyType.lastIndexOf("<") + 1;
|
|
155
303
|
const endIndex = keyType.indexOf(">", startIndex);
|
|
156
304
|
return keyType.slice(startIndex, endIndex);
|
|
157
305
|
};
|
|
306
|
+
/**
|
|
307
|
+
* Checks if a coin type string corresponds to the canonical SUI coin.
|
|
308
|
+
*
|
|
309
|
+
* @param coin - A coin type string.
|
|
310
|
+
* @returns `true` if it matches "0x2::sui::SUI", otherwise `false`.
|
|
311
|
+
*/
|
|
158
312
|
Coin.isSuiCoin = (coin) => helpers_1.Helpers.stripLeadingZeroesFromType(coin) ===
|
|
159
313
|
helpers_1.Helpers.stripLeadingZeroesFromType(Coin.constants.suiCoinType);
|
|
314
|
+
/**
|
|
315
|
+
* Checks if an object type string is a `Coin<...>` object from the standard Sui Move module.
|
|
316
|
+
*
|
|
317
|
+
* @param objectType - The object type to test.
|
|
318
|
+
* @returns `true` if it matches "0x2::coin::Coin<...>", otherwise `false`.
|
|
319
|
+
*/
|
|
160
320
|
Coin.isCoinObjectType = (objectType) => helpers_1.Helpers.stripLeadingZeroesFromType(objectType).startsWith(helpers_1.Helpers.stripLeadingZeroesFromType(Coin.constants.coinObjectType));
|
|
161
321
|
// =========================================================================
|
|
162
322
|
// Helpers
|
|
163
323
|
// =========================================================================
|
|
324
|
+
/**
|
|
325
|
+
* Given a record of coin types => numeric amounts, filters out those
|
|
326
|
+
* with zero or negative amounts, returning only the positive pairs.
|
|
327
|
+
*
|
|
328
|
+
* @param coinAmounts - A record mapping coin types to numeric amounts.
|
|
329
|
+
* @returns An object with `coins` array and `amounts` array in matching indexes.
|
|
330
|
+
*/
|
|
164
331
|
Coin.coinsAndAmountsOverZero = (coinAmounts) => {
|
|
165
|
-
// NOTE: will these loops always run in same order (is this a js
|
|
332
|
+
// NOTE: will these loops always run in same order (is this a js guarantee or not) ?
|
|
166
333
|
const coins = Object.keys(coinAmounts).filter((key) => coinAmounts[key] > 0);
|
|
167
334
|
const amounts = Object.values(coinAmounts).filter((amount) => amount > 0);
|
|
168
335
|
return { coins, amounts };
|
|
169
336
|
};
|
|
337
|
+
/**
|
|
338
|
+
* Given a record of coin types => bigint balances, filters out those with zero
|
|
339
|
+
* or negative balances, returning only the positive pairs.
|
|
340
|
+
*
|
|
341
|
+
* @param coinsToBalance - A record mapping coin types to bigints.
|
|
342
|
+
* @returns An object with `coins` array and `balances` array in matching indexes.
|
|
343
|
+
*/
|
|
170
344
|
Coin.coinsAndBalancesOverZero = (coinsToBalance) => {
|
|
171
|
-
// NOTE: will these loops always run in same order (is this a js
|
|
345
|
+
// NOTE: will these loops always run in same order (is this a js guarantee or not) ?
|
|
172
346
|
const coins = Object.keys(coinsToBalance).filter((key) => BigInt(coinsToBalance[key]) > BigInt(0));
|
|
173
347
|
const balances = Object.values(coinsToBalance)
|
|
174
348
|
.map(BigInt)
|
|
175
349
|
.filter((amount) => amount > BigInt(0));
|
|
176
350
|
return { coins, balances };
|
|
177
351
|
};
|
|
352
|
+
/**
|
|
353
|
+
* Filters a list of `coinTypes` by a textual query, matching against both zero-padded
|
|
354
|
+
* and non-padded forms as well as substring checks.
|
|
355
|
+
*
|
|
356
|
+
* @param inputs - Contains `filter` (the search string) and `coinTypes`.
|
|
357
|
+
* @returns An array of coin types that match the filter in either raw or zero-padded form.
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* ```typescript
|
|
361
|
+
* const filtered = Coin.filterCoinsByType({
|
|
362
|
+
* filter: "sui",
|
|
363
|
+
* coinTypes: ["0x2::sui::SUI", "0x<...>"]
|
|
364
|
+
* });
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
178
367
|
Coin.filterCoinsByType = (inputs) => {
|
|
179
368
|
var _a;
|
|
180
369
|
const filter = inputs.filter.toLowerCase().trim();
|
|
@@ -193,6 +382,13 @@ Coin.filterCoinsByType = (inputs) => {
|
|
|
193
382
|
.includes(filter) || coinType.toLowerCase().includes(filter));
|
|
194
383
|
});
|
|
195
384
|
};
|
|
385
|
+
/**
|
|
386
|
+
* Filters a record of coin metadata by a textual query, matching both the coin type
|
|
387
|
+
* and the metadata's name/symbol fields.
|
|
388
|
+
*
|
|
389
|
+
* @param inputs - An object containing `filter` and a record of `coinMetadatas`.
|
|
390
|
+
* @returns An array of coin types that match the search criteria.
|
|
391
|
+
*/
|
|
196
392
|
Coin.filterCoinsByMetadata = (inputs) => {
|
|
197
393
|
var _a;
|
|
198
394
|
return (_a = Object.entries(inputs.coinMetadatas)) === null || _a === void 0 ? void 0 : _a.filter(([coin, metadata]) => {
|
|
@@ -207,20 +403,48 @@ Coin.filterCoinsByMetadata = (inputs) => {
|
|
|
207
403
|
// =========================================================================
|
|
208
404
|
// Conversions
|
|
209
405
|
// =========================================================================
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
406
|
+
/**
|
|
407
|
+
* Converts a user-friendly decimal number (e.g., 1.5) to a raw on-chain
|
|
408
|
+
* integer representation by scaling with the given coin decimals.
|
|
409
|
+
* For example, `1.5` with `decimals = 9` => `1500000000n`.
|
|
410
|
+
*
|
|
411
|
+
* @param balance - The user-friendly balance as a number.
|
|
412
|
+
* @param decimals - Number of decimal places for this coin.
|
|
413
|
+
* @returns A bigint representing the raw on-chain balance.
|
|
414
|
+
*/
|
|
415
|
+
Coin.normalizeBalance = (balance, decimals) => BigInt(Math.floor(balance * Math.pow(10, decimals)));
|
|
416
|
+
/**
|
|
417
|
+
* Scales a raw bigint or numeric `amount` down by `decimals` to get a display-friendly float.
|
|
418
|
+
* For example, `1500000000n` with `decimals = 9` => `1.5`.
|
|
419
|
+
*
|
|
420
|
+
* @param amount - The raw on-chain amount as `bigint` or `number`.
|
|
421
|
+
* @param decimals - Number of decimal places for this coin.
|
|
422
|
+
* @returns The resulting float as an easily readable balance.
|
|
423
|
+
*/
|
|
217
424
|
Coin.balanceWithDecimals = (amount, decimals) => {
|
|
218
|
-
//
|
|
425
|
+
// TODO: make this conversion via string so no overflow or loss when bigint to number ?
|
|
219
426
|
return Number(amount) / Number(Math.pow(10, decimals));
|
|
220
427
|
};
|
|
428
|
+
/**
|
|
429
|
+
* Scales a raw `amount` down by `decimals` and multiplies by a `price` in USD,
|
|
430
|
+
* returning a final USD value. E.g., `1500000000n`, `decimals=9`, `price=2.0` => `3.0`.
|
|
431
|
+
*
|
|
432
|
+
* @param amount - The raw balance as bigint or number.
|
|
433
|
+
* @param decimals - The coin decimals.
|
|
434
|
+
* @param price - The coin's price in USD.
|
|
435
|
+
* @returns The computed float in USD.
|
|
436
|
+
*/
|
|
221
437
|
Coin.balanceWithDecimalsUsd = (amount, decimals, price) => {
|
|
222
438
|
return Coin.balanceWithDecimals(amount, decimals) * price;
|
|
223
439
|
};
|
|
440
|
+
/**
|
|
441
|
+
* Looks up a coin's symbol if it is known in a provided `coinSymbolToCoinTypes`
|
|
442
|
+
* record. For instance, if "SUI" => `["0x2::sui::SUI"]`, we can find "SUI" from
|
|
443
|
+
* the coin type "0x2::sui::SUI".
|
|
444
|
+
*
|
|
445
|
+
* @param inputs - An object with `coinType` and `coinSymbolToCoinTypes`.
|
|
446
|
+
* @returns The coin symbol string or `undefined` if not found.
|
|
447
|
+
*/
|
|
224
448
|
Coin.coinSymbolForCoinType = (inputs) => {
|
|
225
449
|
const { coinType, coinSymbolToCoinTypes } = inputs;
|
|
226
450
|
try {
|
|
@@ -231,7 +455,7 @@ Coin.coinSymbolForCoinType = (inputs) => {
|
|
|
231
455
|
const foundCoinSymbol = foundCoinData === null || foundCoinData === void 0 ? void 0 : foundCoinData[0];
|
|
232
456
|
return foundCoinSymbol;
|
|
233
457
|
}
|
|
234
|
-
catch (
|
|
458
|
+
catch (_a) {
|
|
235
459
|
return undefined;
|
|
236
460
|
}
|
|
237
461
|
};
|
|
@@ -1,36 +1,121 @@
|
|
|
1
1
|
import { CoinMetadata } from "@mysten/sui/client";
|
|
2
2
|
import { Balance, ObjectId, Percentage } from "../../general/types/generalTypes";
|
|
3
3
|
import { CoinGeckoCoinApiId } from "../../types";
|
|
4
|
+
/**
|
|
5
|
+
* Represents the decimal precision of a coin (e.g., 9 or 18).
|
|
6
|
+
*/
|
|
4
7
|
export type CoinDecimal = number;
|
|
8
|
+
/**
|
|
9
|
+
* A string that uniquely identifies a coin type in the Sui network
|
|
10
|
+
* (e.g., "0x2::sui::SUI").
|
|
11
|
+
*/
|
|
5
12
|
export type CoinType = string;
|
|
13
|
+
/**
|
|
14
|
+
* Represents a short symbol or ticker for a coin (e.g., "SUI", "BTC").
|
|
15
|
+
*/
|
|
6
16
|
export type CoinSymbol = string;
|
|
17
|
+
/**
|
|
18
|
+
* Represents a coin with an amount in integer or floating form, typically used
|
|
19
|
+
* to specify a user’s holding or a transaction amount.
|
|
20
|
+
*/
|
|
7
21
|
export interface CoinWithAmount {
|
|
22
|
+
/**
|
|
23
|
+
* The coin type, e.g. "0x2::sui::SUI".
|
|
24
|
+
*/
|
|
8
25
|
coin: CoinType;
|
|
26
|
+
/**
|
|
27
|
+
* The amount of the coin, typically expressed as an integer number of smallest units.
|
|
28
|
+
*/
|
|
9
29
|
amount: number;
|
|
10
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Represents a coin with an amount that can be `undefined`, typically for optional or
|
|
33
|
+
* deferred usage scenarios.
|
|
34
|
+
*/
|
|
11
35
|
export interface CoinWithAmountOrUndefined {
|
|
36
|
+
/**
|
|
37
|
+
* The coin type, e.g. "0x2::sui::SUI".
|
|
38
|
+
*/
|
|
12
39
|
coin: CoinType;
|
|
40
|
+
/**
|
|
41
|
+
* The amount of the coin, which can be `undefined`.
|
|
42
|
+
*/
|
|
13
43
|
amount: number | undefined;
|
|
14
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Represents an amount in both coin denomination and USD value for reference.
|
|
47
|
+
*/
|
|
15
48
|
export interface AmountInCoinAndUsd {
|
|
49
|
+
/**
|
|
50
|
+
* The amount of the coin in smallest units.
|
|
51
|
+
*/
|
|
16
52
|
amount: number;
|
|
53
|
+
/**
|
|
54
|
+
* The USD equivalent of that coin amount.
|
|
55
|
+
*/
|
|
17
56
|
amountUsd: number;
|
|
18
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Maps a coin type to a numerical balance. Typically used to store multiple
|
|
60
|
+
* coin balances under their respective coin types.
|
|
61
|
+
*/
|
|
19
62
|
export type CoinsToBalance = Record<CoinType, Balance>;
|
|
63
|
+
/**
|
|
64
|
+
* Maps a coin type to a numerical balance, which may be `undefined`.
|
|
65
|
+
*/
|
|
20
66
|
export type CoinsToBalanceOrUndefined = Record<CoinType, Balance | undefined>;
|
|
67
|
+
/**
|
|
68
|
+
* Maps a coin type to its price, typically as a number in USD or another fiat currency.
|
|
69
|
+
*/
|
|
21
70
|
export type CoinsToPrice = Record<CoinType, number>;
|
|
71
|
+
/**
|
|
72
|
+
* Maps a coin type to its on-chain decimal precision.
|
|
73
|
+
*/
|
|
22
74
|
export type CoinsToDecimals = Record<CoinType, CoinDecimal>;
|
|
75
|
+
/**
|
|
76
|
+
* Maps a coin type to price information, typically containing a price and a 24-hour change.
|
|
77
|
+
*/
|
|
23
78
|
export type CoinsToPriceInfo = Record<CoinType, CoinPriceInfo>;
|
|
79
|
+
/**
|
|
80
|
+
* Maps a coin symbol (e.g., "SUI") to its price information, typically containing a price and a 24-hour change.
|
|
81
|
+
*/
|
|
24
82
|
export type CoinSymbolsToPriceInfo = Record<CoinSymbol, CoinPriceInfo>;
|
|
83
|
+
/**
|
|
84
|
+
* Maps a coin symbol (e.g., "SUI") to an array of possible coin types (e.g., "0x2::sui::SUI").
|
|
85
|
+
*/
|
|
25
86
|
export type CoinSymbolToCoinTypes = Record<CoinSymbol, CoinType[]>;
|
|
87
|
+
/**
|
|
88
|
+
* Represents pricing information for a coin, including current price and 24-hour percentage change.
|
|
89
|
+
*/
|
|
26
90
|
export interface CoinPriceInfo {
|
|
91
|
+
/**
|
|
92
|
+
* The current price in USD or another currency.
|
|
93
|
+
*/
|
|
27
94
|
price: number;
|
|
95
|
+
/**
|
|
96
|
+
* The 24-hour percentage change of the coin price.
|
|
97
|
+
* @remarks 0.54 = 54%
|
|
98
|
+
*/
|
|
28
99
|
priceChange24HoursPercentage: Percentage;
|
|
29
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Extends the Sui `CoinMetadata` with optional properties relevant to external data
|
|
103
|
+
* sources (e.g., CoinGecko).
|
|
104
|
+
*/
|
|
30
105
|
export type CoinMetadaWithInfo = CoinMetadata & {
|
|
106
|
+
/**
|
|
107
|
+
* Indicates whether this coin's metadata was generated automatically.
|
|
108
|
+
*/
|
|
31
109
|
isGenerated?: boolean;
|
|
110
|
+
/**
|
|
111
|
+
* The associated CoinGecko API ID, if available.
|
|
112
|
+
*/
|
|
32
113
|
coingeckoId?: CoinGeckoCoinApiId;
|
|
33
114
|
};
|
|
115
|
+
/**
|
|
116
|
+
* Represents a coin reference in the Move environment, using either an on-chain ObjectId
|
|
117
|
+
* or an input index or result index from a transaction.
|
|
118
|
+
*/
|
|
34
119
|
export type ServiceCoinData = {
|
|
35
120
|
Coin: ObjectId;
|
|
36
121
|
} | {
|
|
@@ -40,6 +125,10 @@ export type ServiceCoinData = {
|
|
|
40
125
|
} | {
|
|
41
126
|
NestedResult: [number, number];
|
|
42
127
|
};
|
|
128
|
+
/**
|
|
129
|
+
* **Legacy type** representing a coin reference in the Move environment, using
|
|
130
|
+
* older transaction output indexing structures.
|
|
131
|
+
*/
|
|
43
132
|
export type ServiceCoinDataV2 = "gas" | {
|
|
44
133
|
input: number;
|
|
45
134
|
} | {
|
|
@@ -47,6 +136,10 @@ export type ServiceCoinDataV2 = "gas" | {
|
|
|
47
136
|
} | {
|
|
48
137
|
result: [number, number];
|
|
49
138
|
};
|
|
139
|
+
/**
|
|
140
|
+
* **Legacy type** representing how a transaction argument is encoded in the older
|
|
141
|
+
* Sui Transaction format.
|
|
142
|
+
*/
|
|
50
143
|
export type CoinTransactionObjectArgumentV0 = {
|
|
51
144
|
kind: "Input";
|
|
52
145
|
index: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coinTypes.d.ts","sourceRoot":"","sources":["../../../src/packages/coin/coinTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EACN,OAAO,EACP,QAAQ,EACR,UAAU,EACV,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"coinTypes.d.ts","sourceRoot":"","sources":["../../../src/packages/coin/coinTypes.ts"],"names":[],"mappings":"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"}
|