@swapkit/helpers 1.0.0-rc.2 → 1.0.0-rc.22

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.
@@ -17,9 +17,15 @@ import type { CommonAssetString } from '../helpers/asset.ts';
17
17
  import { getAssetType, getCommonAssetInfo, getDecimal, isGasAsset } from '../helpers/asset.ts';
18
18
  import { validateIdentifier } from '../helpers/validators.ts';
19
19
 
20
- import { BigIntArithmetics } from './bigIntArithmetics.ts';
20
+ import type { NumberPrimitives } from './bigIntArithmetics.ts';
21
+ import { BigIntArithmetics, formatBigIntToSafeValue } from './bigIntArithmetics.ts';
21
22
  import type { SwapKitValueType } from './swapKitNumber.ts';
22
23
 
24
+ const safeValue = (value: NumberPrimitives, decimal: number) =>
25
+ typeof value === 'bigint'
26
+ ? formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal })
27
+ : value;
28
+
23
29
  type AssetValueParams = { decimal: number; value: SwapKitValueType } & (
24
30
  | { chain: Chain; symbol: string }
25
31
  | { identifier: string }
@@ -51,63 +57,102 @@ const getStaticToken = (identifier: TokenNames) => {
51
57
  return tokenInfo || { decimal: BaseDecimal.THOR, identifier: '' };
52
58
  };
53
59
 
54
- const createAssetValue = async (assetString: string, value: number | string = 0) => {
60
+ const createAssetValue = async (assetString: string, value: NumberPrimitives = 0) => {
55
61
  validateIdentifier(assetString);
56
62
 
57
63
  const decimal = await getDecimal(getAssetInfo(assetString));
58
- return new AssetValue({ decimal, value, identifier: assetString });
64
+ const parsedValue = safeValue(value, decimal);
65
+
66
+ return new AssetValue({ decimal, value: parsedValue, identifier: assetString });
59
67
  };
60
68
 
61
69
  export class AssetValue extends BigIntArithmetics {
62
- static async fromString(assetString: string, value: number | string = 0) {
70
+ address?: string;
71
+ chain: Chain;
72
+ isSynthetic = false;
73
+ isGasAsset = false;
74
+ symbol: string;
75
+ ticker: string;
76
+ type: ReturnType<typeof getAssetType>;
77
+
78
+ constructor(params: AssetValueParams) {
79
+ super(
80
+ params.value instanceof BigIntArithmetics
81
+ ? params.value
82
+ : { decimal: params.decimal, value: params.value },
83
+ );
84
+
85
+ const identifier =
86
+ 'identifier' in params ? params.identifier : `${params.chain}.${params.symbol}`;
87
+ const assetInfo = getAssetInfo(identifier);
88
+
89
+ this.type = getAssetType(assetInfo);
90
+ this.chain = assetInfo.chain;
91
+ this.ticker = assetInfo.ticker;
92
+ this.symbol = assetInfo.symbol;
93
+ this.address = assetInfo.address;
94
+ this.isSynthetic = assetInfo.isSynthetic;
95
+ this.isGasAsset = assetInfo.isGasAsset;
96
+ }
97
+
98
+ get assetValue() {
99
+ return `${this.getValue('string')} ${this.ticker}`;
100
+ }
101
+
102
+ toString(short = false) {
103
+ // THOR.RUNE | ETH/ETH
104
+ const shortFormat = this.isSynthetic
105
+ ? this.symbol.split('-')[0]
106
+ : `${this.chain}.${this.ticker}`;
107
+
108
+ return short
109
+ ? shortFormat
110
+ : // THOR.ETH/ETH | ETH.USDT-0x1234567890
111
+ `${this.chain}.${this.symbol}`;
112
+ }
113
+
114
+ eq({ chain, symbol }: { chain: Chain; symbol: string }) {
115
+ return this.chain === chain && this.symbol === symbol;
116
+ }
117
+
118
+ static async fromString(assetString: string, value: NumberPrimitives = 0) {
63
119
  return createAssetValue(assetString, value);
64
120
  }
65
121
 
66
- static fromStringSync(assetString: string, value: number | string = 0) {
122
+ static fromStringSync(assetString: string, value: NumberPrimitives = 0) {
123
+ const { isSynthetic } = getAssetInfo(assetString);
67
124
  const { decimal, identifier: tokenIdentifier } = getStaticToken(
68
125
  assetString as unknown as TokenNames,
69
126
  );
70
127
 
128
+ const parsedValue = safeValue(value, decimal);
129
+
71
130
  return tokenIdentifier
72
- ? new AssetValue({ decimal, identifier: tokenIdentifier, value })
131
+ ? new AssetValue({ decimal, identifier: tokenIdentifier, value: parsedValue })
132
+ : isSynthetic
133
+ ? new AssetValue({ decimal: 8, identifier: assetString, value: parsedValue })
73
134
  : undefined;
74
135
  }
75
136
 
76
137
  static async fromIdentifier(
77
138
  assetString: `${Chain}.${string}` | `${Chain}/${string}` | `${Chain}.${string}-${string}`,
78
- value: number | string = 0,
139
+ value: NumberPrimitives = 0,
79
140
  ) {
80
141
  return createAssetValue(assetString, value);
81
142
  }
82
143
 
83
- static fromIdentifierSync(identifier: TokenNames, value: number | string = 0) {
144
+ static fromIdentifierSync(identifier: TokenNames, value: NumberPrimitives = 0) {
84
145
  const { decimal, identifier: tokenIdentifier } = getStaticToken(identifier);
146
+ const parsedValue = safeValue(value, decimal);
85
147
 
86
- return new AssetValue({ decimal, identifier: tokenIdentifier, value });
148
+ return new AssetValue({ decimal, identifier: tokenIdentifier, value: parsedValue });
87
149
  }
88
150
 
89
- static fromChainOrSignature(assetString: CommonAssetString, value: number | string = 0) {
151
+ static fromChainOrSignature(assetString: CommonAssetString, value: NumberPrimitives = 0) {
90
152
  const { decimal, identifier } = getCommonAssetInfo(assetString);
153
+ const parsedValue = safeValue(value, decimal);
91
154
 
92
- return new AssetValue({ value, decimal, identifier });
93
- }
94
-
95
- static async fromTCQuote(identifier: TCTokenNames, value: number | string = 0) {
96
- const decimal = await getDecimal(getAssetInfo(identifier));
97
- const shiftedValue = this.shiftDecimals({ value, from: BaseDecimal.THOR, to: decimal });
98
-
99
- return new AssetValue({ value: shiftedValue, identifier, decimal });
100
- }
101
-
102
- static fromTCQuoteStatic(identifier: TCTokenNames, value: number | string = 0) {
103
- const tokenInfo = getStaticToken(identifier);
104
- const shiftedValue = this.shiftDecimals({
105
- value,
106
- from: BaseDecimal.THOR,
107
- to: tokenInfo.decimal,
108
- });
109
-
110
- return new AssetValue({ ...tokenInfo, value: shiftedValue });
155
+ return new AssetValue({ value: parsedValue, decimal, identifier });
111
156
  }
112
157
 
113
158
  static async loadStaticAssets() {
@@ -149,46 +194,6 @@ export class AssetValue extends BigIntArithmetics {
149
194
  },
150
195
  );
151
196
  }
152
-
153
- address?: string;
154
- chain: Chain;
155
- isSynthetic = false;
156
- isGasAsset = false;
157
- symbol: string;
158
- ticker: string;
159
- type: ReturnType<typeof getAssetType>;
160
-
161
- constructor(params: AssetValueParams) {
162
- super(
163
- params.value instanceof BigIntArithmetics
164
- ? params.value
165
- : { decimal: params.decimal, value: params.value },
166
- );
167
-
168
- const identifier =
169
- 'identifier' in params ? params.identifier : `${params.chain}.${params.symbol}`;
170
- const assetInfo = getAssetInfo(identifier);
171
-
172
- this.type = getAssetType(assetInfo);
173
- this.chain = assetInfo.chain;
174
- this.ticker = assetInfo.ticker;
175
- this.symbol = assetInfo.symbol;
176
- this.address = assetInfo.address;
177
- this.isSynthetic = assetInfo.isSynthetic;
178
- this.isGasAsset = assetInfo.isGasAsset;
179
- }
180
-
181
- get assetValue() {
182
- return `${this.value} ${this.ticker}`;
183
- }
184
-
185
- toString() {
186
- return `${this.chain}.${this.symbol}`;
187
- }
188
-
189
- eq({ chain, symbol }: { chain: Chain; symbol: string }) {
190
- return this.chain === chain && this.symbol === symbol;
191
- }
192
197
  }
193
198
 
194
199
  export const getMinAmountByChain = (chain: Chain) => {
@@ -198,39 +203,41 @@ export const getMinAmountByChain = (chain: Chain) => {
198
203
  case Chain.Bitcoin:
199
204
  case Chain.Litecoin:
200
205
  case Chain.BitcoinCash:
201
- return asset.add(10001);
206
+ return asset.set(0.00010001);
202
207
 
203
208
  case Chain.Dogecoin:
204
- return asset.add(100000001);
209
+ return asset.set(1.00000001);
205
210
 
206
211
  case Chain.Avalanche:
207
212
  case Chain.Ethereum:
208
- return asset.add(10 * 10 ** 9);
213
+ return asset.set(0.00000001);
209
214
 
210
215
  case Chain.THORChain:
211
216
  case Chain.Maya:
212
- return asset.add(0);
217
+ return asset.set(0);
213
218
 
214
219
  default:
215
- return asset.add(1);
220
+ return asset.set(0.00000001);
216
221
  }
217
222
  };
218
223
 
219
224
  const getAssetInfo = (identifier: string) => {
220
225
  const isSynthetic = identifier.slice(0, 14).includes('/');
221
- const adjustedIdentifier = identifier.includes('.')
222
- ? identifier
223
- : `${Chain.THORChain}.${identifier}`;
226
+ const [synthChain, synthSymbol] = identifier.split('.').pop()!.split('/');
227
+ const adjustedIdentifier =
228
+ identifier.includes('.') && !isSynthetic ? identifier : `${Chain.THORChain}.${synthSymbol}`;
224
229
 
225
230
  const [chain, symbol] = adjustedIdentifier.split('.') as [Chain, string];
226
- const [ticker, address] = symbol.split('-') as [string, string?];
231
+ const [ticker, address] = (isSynthetic ? synthSymbol : symbol).split('-') as [string, string?];
227
232
 
228
233
  return {
229
234
  address: address?.toLowerCase(),
230
235
  chain,
231
236
  isGasAsset: isGasAsset({ chain, symbol }),
232
237
  isSynthetic,
233
- symbol: address ? `${ticker}-${address?.toLowerCase() ?? ''}` : symbol,
234
- ticker: isSynthetic ? symbol : ticker,
238
+ symbol:
239
+ (isSynthetic ? `${synthChain}/` : '') +
240
+ (address ? `${ticker}-${address?.toLowerCase() ?? ''}` : symbol),
241
+ ticker: ticker,
235
242
  };
236
243
  };