@swapkit/helpers 1.0.0-rc.70 → 1.0.0-rc.71
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/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.es.js +167 -144
- package/dist/index.es.js.map +1 -1
- package/package.json +1 -1
- package/src/modules/__tests__/assetValue.test.ts +87 -0
- package/src/modules/assetValue.ts +39 -1
|
@@ -176,6 +176,27 @@ describe('AssetValue', () => {
|
|
|
176
176
|
});
|
|
177
177
|
});
|
|
178
178
|
|
|
179
|
+
describe('fromStringWithBase', () => {
|
|
180
|
+
test('creates AssetValue from string with base', async () => {
|
|
181
|
+
const fakeAvaxAssetString = 'AVAX.ASDF-1234';
|
|
182
|
+
const fakeAvaxAsset = await AssetValue.fromStringWithBase(fakeAvaxAssetString, 1, 8);
|
|
183
|
+
|
|
184
|
+
expect(fakeAvaxAsset).toEqual(
|
|
185
|
+
expect.objectContaining({
|
|
186
|
+
address: '1234',
|
|
187
|
+
chain: Chain.Avalanche,
|
|
188
|
+
decimal: 18,
|
|
189
|
+
isGasAsset: false,
|
|
190
|
+
isSynthetic: false,
|
|
191
|
+
symbol: 'ASDF-1234',
|
|
192
|
+
ticker: 'ASDF',
|
|
193
|
+
}),
|
|
194
|
+
);
|
|
195
|
+
expect(fakeAvaxAsset.getValue('string')).toBe('100000000');
|
|
196
|
+
expect(fakeAvaxAsset.getBaseValue('string')).toBe('100000000000000000000000000');
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
179
200
|
describe('fromUrl', () => {
|
|
180
201
|
test('creates AssetValue from url like format', async () => {
|
|
181
202
|
const synthETHString = 'THOR.ETH.ETH';
|
|
@@ -275,6 +296,72 @@ describe('AssetValue', () => {
|
|
|
275
296
|
});
|
|
276
297
|
});
|
|
277
298
|
|
|
299
|
+
describe('fromStringWithBaseSync', () => {
|
|
300
|
+
test('creates AssetValue from string with base decimals via `@swapkit/tokens` lists', async () => {
|
|
301
|
+
await AssetValue.loadStaticAssets();
|
|
302
|
+
const btc = AssetValue.fromStringWithBaseSync('BTC.BTC', 5200000000000, 8);
|
|
303
|
+
|
|
304
|
+
expect(btc).toBeDefined();
|
|
305
|
+
expect(btc).toEqual(
|
|
306
|
+
expect.objectContaining({
|
|
307
|
+
chain: Chain.Bitcoin,
|
|
308
|
+
decimal: 8,
|
|
309
|
+
isGasAsset: true,
|
|
310
|
+
isSynthetic: false,
|
|
311
|
+
symbol: 'BTC',
|
|
312
|
+
ticker: 'BTC',
|
|
313
|
+
}),
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
expect(btc.getValue('string')).toBe('52000');
|
|
317
|
+
expect(btc.getBaseValue('string')).toBe('5200000000000');
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test('returns safe decimals if string is not in `@swapkit/tokens` lists', async () => {
|
|
321
|
+
await AssetValue.loadStaticAssets();
|
|
322
|
+
const fakeAvaxUSDCAssetString = 'AVAX.USDC-1234';
|
|
323
|
+
const fakeAvaxUSDCAsset = AssetValue.fromStringWithBaseSync(fakeAvaxUSDCAssetString, 1, 8);
|
|
324
|
+
|
|
325
|
+
expect(fakeAvaxUSDCAsset).toBeDefined();
|
|
326
|
+
expect(fakeAvaxUSDCAsset).toEqual(
|
|
327
|
+
expect.objectContaining({
|
|
328
|
+
address: '1234',
|
|
329
|
+
chain: Chain.Avalanche,
|
|
330
|
+
decimal: 18,
|
|
331
|
+
isGasAsset: false,
|
|
332
|
+
isSynthetic: false,
|
|
333
|
+
symbol: 'USDC-1234',
|
|
334
|
+
ticker: 'USDC',
|
|
335
|
+
}),
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
expect(fakeAvaxUSDCAsset.getValue('string')).toBe('0.00000001');
|
|
339
|
+
expect(fakeAvaxUSDCAsset.getBaseValue('string')).toBe('10000000000');
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
test('returns proper avax string with address from `@swapkit/tokens` lists', async () => {
|
|
343
|
+
await AssetValue.loadStaticAssets();
|
|
344
|
+
const avaxUSDC = 'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e';
|
|
345
|
+
const AvaxUSDC = AssetValue.fromStringWithBaseSync(avaxUSDC, 100000000, 8);
|
|
346
|
+
|
|
347
|
+
expect(AvaxUSDC).toBeDefined();
|
|
348
|
+
expect(AvaxUSDC).toEqual(
|
|
349
|
+
expect.objectContaining({
|
|
350
|
+
address: '0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
351
|
+
chain: Chain.Avalanche,
|
|
352
|
+
decimal: 6,
|
|
353
|
+
isGasAsset: false,
|
|
354
|
+
isSynthetic: false,
|
|
355
|
+
symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
356
|
+
ticker: 'USDC',
|
|
357
|
+
}),
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
expect(AvaxUSDC.getValue('string')).toBe('1');
|
|
361
|
+
expect(AvaxUSDC.getBaseValue('string')).toBe('1000000');
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
|
|
278
365
|
describe('fromChainOrSignature', () => {
|
|
279
366
|
test('creates AssetValue from common asset string or chain', () => {
|
|
280
367
|
const customBaseAsset = [Chain.Cosmos, Chain.BinanceSmartChain, Chain.THORChain, Chain.Maya];
|
|
@@ -7,7 +7,7 @@ import type { TokenNames, TokenTax } from '../types.ts';
|
|
|
7
7
|
|
|
8
8
|
import type { NumberPrimitives } from './bigIntArithmetics.ts';
|
|
9
9
|
import { BigIntArithmetics, formatBigIntToSafeValue } from './bigIntArithmetics.ts';
|
|
10
|
-
import type
|
|
10
|
+
import { SwapKitNumber, type SwapKitValueType } from './swapKitNumber.ts';
|
|
11
11
|
|
|
12
12
|
const staticTokensMap = new Map<
|
|
13
13
|
TokenNames,
|
|
@@ -117,6 +117,44 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
117
117
|
});
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
+
static async fromStringWithBase(
|
|
121
|
+
assetString: string,
|
|
122
|
+
value: NumberPrimitives = 0,
|
|
123
|
+
baseDecimal: number = BaseDecimal.THOR,
|
|
124
|
+
) {
|
|
125
|
+
const shiftedAmount = BigIntArithmetics.shiftDecimals({
|
|
126
|
+
value: SwapKitNumber.fromBigInt(BigInt(value)),
|
|
127
|
+
from: 0,
|
|
128
|
+
to: baseDecimal,
|
|
129
|
+
}).getBaseValue('string');
|
|
130
|
+
const assetValue = await AssetValue.fromString(assetString, value);
|
|
131
|
+
|
|
132
|
+
return assetValue.set(shiftedAmount);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static fromStringWithBaseSync(
|
|
136
|
+
assetString: string,
|
|
137
|
+
value: NumberPrimitives = 0,
|
|
138
|
+
baseDecimal: number = BaseDecimal.THOR,
|
|
139
|
+
) {
|
|
140
|
+
const { chain, isSynthetic } = getAssetInfo(assetString);
|
|
141
|
+
const tokenInfo = staticTokensMap.get(assetString.toUpperCase() as TokenNames);
|
|
142
|
+
|
|
143
|
+
if (isSynthetic) return createSyntheticAssetValue(assetString, value);
|
|
144
|
+
|
|
145
|
+
const { tax, decimal, identifier } = tokenInfo || {
|
|
146
|
+
decimal: BaseDecimal[chain],
|
|
147
|
+
identifier: assetString,
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
return new AssetValue({
|
|
151
|
+
tax,
|
|
152
|
+
value: safeValue(BigInt(value), baseDecimal),
|
|
153
|
+
identifier,
|
|
154
|
+
decimal,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
120
158
|
static fromIdentifierSync(assetString: TokenNames, value: NumberPrimitives = 0) {
|
|
121
159
|
const { chain, isSynthetic } = getAssetInfo(assetString);
|
|
122
160
|
const tokenInfo = staticTokensMap.get(assetString);
|