@shapeshiftoss/utils 1.0.0
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/assertUnreachable.d.ts +1 -0
- package/dist/assertUnreachable.js +3 -0
- package/dist/assetData/assetData.test.d.ts +1 -0
- package/dist/assetData/assetData.test.js +120 -0
- package/dist/assetData/baseAssets.d.ts +20 -0
- package/dist/assetData/baseAssets.js +279 -0
- package/dist/assetData/constants.d.ts +1 -0
- package/dist/assetData/constants.js +10 -0
- package/dist/assetData/decodeAssetData.d.ts +7 -0
- package/dist/assetData/decodeAssetData.js +94 -0
- package/dist/assetData/decodeRelatedAssetIndex.d.ts +3 -0
- package/dist/assetData/decodeRelatedAssetIndex.js +10 -0
- package/dist/assetData/encodeAssetData.d.ts +4 -0
- package/dist/assetData/encodeAssetData.js +68 -0
- package/dist/assetData/encodeRelatedAssetIndex.d.ts +3 -0
- package/dist/assetData/encodeRelatedAssetIndex.js +16 -0
- package/dist/assetData/getBaseAsset.d.ts +3 -0
- package/dist/assetData/getBaseAsset.js +44 -0
- package/dist/assetData/index.d.ts +7 -0
- package/dist/assetData/index.js +7 -0
- package/dist/assetData/relatedAssetIndex.test.d.ts +1 -0
- package/dist/assetData/relatedAssetIndex.test.js +30 -0
- package/dist/assetData/types.d.ts +28 -0
- package/dist/assetData/types.js +1 -0
- package/dist/baseUnits/baseUnits.d.ts +5 -0
- package/dist/baseUnits/baseUnits.js +23 -0
- package/dist/baseUnits/baseUnits.test.d.ts +1 -0
- package/dist/baseUnits/baseUnits.test.js +30 -0
- package/dist/basisPoints.d.ts +23 -0
- package/dist/basisPoints.js +39 -0
- package/dist/bignumber/bignumber.d.ts +11 -0
- package/dist/bignumber/bignumber.js +16 -0
- package/dist/bignumber/bignumber.test.d.ts +1 -0
- package/dist/bignumber/bignumber.test.js +50 -0
- package/dist/chainIdToFeeAsset.d.ts +3 -0
- package/dist/chainIdToFeeAsset.js +5 -0
- package/dist/chainIdToFeeAssetId.d.ts +2 -0
- package/dist/chainIdToFeeAssetId.js +44 -0
- package/dist/createThrottle.d.ts +9 -0
- package/dist/createThrottle.js +32 -0
- package/dist/getAssetNamespaceFromChainId.d.ts +3 -0
- package/dist/getAssetNamespaceFromChainId.js +31 -0
- package/dist/getChainShortName.d.ts +2 -0
- package/dist/getChainShortName.js +43 -0
- package/dist/getNativeFeeAssetReference.d.ts +2 -0
- package/dist/getNativeFeeAssetReference.js +63 -0
- package/dist/historyTimeframe.d.ts +6 -0
- package/dist/historyTimeframe.js +29 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +40 -0
- package/dist/makeAsset/makeAsset.d.ts +10 -0
- package/dist/makeAsset/makeAsset.js +39 -0
- package/dist/makeAsset/makeAsset.test.d.ts +1 -0
- package/dist/makeAsset/makeAsset.test.js +45 -0
- package/dist/promises.d.ts +4 -0
- package/dist/promises.js +3 -0
- package/dist/sha256.d.ts +1 -0
- package/dist/sha256.js +2 -0
- package/dist/timeout.d.ts +3 -0
- package/dist/timeout.js +17 -0
- package/dist/treasury.d.ts +15 -0
- package/dist/treasury.js +29 -0
- package/dist/unfreeze.d.ts +4 -0
- package/dist/unfreeze.js +6 -0
- package/dist/utxo.d.ts +2 -0
- package/dist/utxo.js +2 -0
- package/package.json +24 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
2
|
+
import { assertUnreachable } from '../assertUnreachable';
|
|
3
|
+
import { arbitrum, arbitrumNova, atom, avax, base, bitcoin, bitcoincash, bnbsmartchain, dogecoin, ethereum, gnosis, litecoin, mayachain, optimism, polygon, solana, thorchain, } from './baseAssets';
|
|
4
|
+
export const getBaseAsset = (chainId) => {
|
|
5
|
+
const knownChainId = chainId;
|
|
6
|
+
switch (knownChainId) {
|
|
7
|
+
case KnownChainIds.EthereumMainnet:
|
|
8
|
+
return ethereum;
|
|
9
|
+
case KnownChainIds.AvalancheMainnet:
|
|
10
|
+
return avax;
|
|
11
|
+
case KnownChainIds.OptimismMainnet:
|
|
12
|
+
return optimism;
|
|
13
|
+
case KnownChainIds.BnbSmartChainMainnet:
|
|
14
|
+
return bnbsmartchain;
|
|
15
|
+
case KnownChainIds.PolygonMainnet:
|
|
16
|
+
return polygon;
|
|
17
|
+
case KnownChainIds.GnosisMainnet:
|
|
18
|
+
return gnosis;
|
|
19
|
+
case KnownChainIds.ArbitrumMainnet:
|
|
20
|
+
return arbitrum;
|
|
21
|
+
case KnownChainIds.ArbitrumNovaMainnet:
|
|
22
|
+
return arbitrumNova;
|
|
23
|
+
case KnownChainIds.BaseMainnet:
|
|
24
|
+
return base;
|
|
25
|
+
case KnownChainIds.SolanaMainnet:
|
|
26
|
+
return solana;
|
|
27
|
+
case KnownChainIds.BitcoinMainnet:
|
|
28
|
+
return bitcoin;
|
|
29
|
+
case KnownChainIds.BitcoinCashMainnet:
|
|
30
|
+
return bitcoincash;
|
|
31
|
+
case KnownChainIds.DogecoinMainnet:
|
|
32
|
+
return dogecoin;
|
|
33
|
+
case KnownChainIds.LitecoinMainnet:
|
|
34
|
+
return litecoin;
|
|
35
|
+
case KnownChainIds.CosmosMainnet:
|
|
36
|
+
return atom;
|
|
37
|
+
case KnownChainIds.ThorchainMainnet:
|
|
38
|
+
return thorchain;
|
|
39
|
+
case KnownChainIds.MayachainMainnet:
|
|
40
|
+
return mayachain;
|
|
41
|
+
default:
|
|
42
|
+
return assertUnreachable(knownChainId);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { decodeRelatedAssetIndex } from './decodeRelatedAssetIndex';
|
|
3
|
+
import { encodeRelatedAssetIndex } from './encodeRelatedAssetIndex';
|
|
4
|
+
// Order of these is deliberately shuffled to test sorting is preserved
|
|
5
|
+
const mockSortedAssetIds = [
|
|
6
|
+
'eip155:1/erc20:0xeeecd285f60e802ecb6d8d8d37790c887f9a4b33',
|
|
7
|
+
'eip155:1/erc20:0xeeee2a2e650697d2a8e8bc990c2f3d04203be06f',
|
|
8
|
+
'eip155:1/erc20:0xeed3ae7b0f8b5b9bb8c035a9941382b1822671cd',
|
|
9
|
+
'eip155:1/erc20:0xf073bac22dab7faf4a3dd6c6189a70d54110525c',
|
|
10
|
+
'eip155:1/erc20:0xeee0fe52299f2de8e2ed5111cd521ab67dcf0faf',
|
|
11
|
+
'eip155:1/erc20:0xeeda34a377dd0ca676b9511ee1324974fa8d980d',
|
|
12
|
+
'bip122:000000000019d6689c085ae165831e93/slip44:0', // BTC
|
|
13
|
+
];
|
|
14
|
+
const mockRelatedAssetIndex = {
|
|
15
|
+
'eip155:1/erc20:0xeeee2a2e650697d2a8e8bc990c2f3d04203be06f': [
|
|
16
|
+
'eip155:1/erc20:0xeeee2a2e650697d2a8e8bc990c2f3d04203be06f',
|
|
17
|
+
'eip155:1/erc20:0xf073bac22dab7faf4a3dd6c6189a70d54110525c',
|
|
18
|
+
],
|
|
19
|
+
'eip155:1/erc20:0xf073bac22dab7faf4a3dd6c6189a70d54110525c': [
|
|
20
|
+
'eip155:1/erc20:0xeeee2a2e650697d2a8e8bc990c2f3d04203be06f',
|
|
21
|
+
'eip155:1/erc20:0xf073bac22dab7faf4a3dd6c6189a70d54110525c',
|
|
22
|
+
],
|
|
23
|
+
};
|
|
24
|
+
describe('relatedAssetIndex', () => {
|
|
25
|
+
it('can encode and decode related asset index as a complete round trip', () => {
|
|
26
|
+
const encodedRelatedAssetIndex = encodeRelatedAssetIndex(mockRelatedAssetIndex, mockSortedAssetIds);
|
|
27
|
+
const decodedRelatedAssetIndex = decodeRelatedAssetIndex(encodedRelatedAssetIndex, mockSortedAssetIds);
|
|
28
|
+
expect(decodedRelatedAssetIndex).toEqual(mockRelatedAssetIndex);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { FIELDS } from './constants';
|
|
2
|
+
export type Field = (typeof FIELDS)[number];
|
|
3
|
+
export type FieldToType = {
|
|
4
|
+
assetIdx: number;
|
|
5
|
+
name: string;
|
|
6
|
+
precision: number;
|
|
7
|
+
color: string;
|
|
8
|
+
icon: string[];
|
|
9
|
+
symbol: string;
|
|
10
|
+
relatedAssetKey: number | null;
|
|
11
|
+
isPool: 0 | 1;
|
|
12
|
+
};
|
|
13
|
+
export type EncodedAsset = [
|
|
14
|
+
FieldToType['assetIdx'],
|
|
15
|
+
FieldToType['name'],
|
|
16
|
+
FieldToType['precision'],
|
|
17
|
+
FieldToType['color'],
|
|
18
|
+
FieldToType['icon'],
|
|
19
|
+
FieldToType['symbol'],
|
|
20
|
+
FieldToType['relatedAssetKey'],
|
|
21
|
+
FieldToType['isPool']
|
|
22
|
+
];
|
|
23
|
+
export type EncodedAssetData = {
|
|
24
|
+
assetIdPrefixes: string[];
|
|
25
|
+
encodedAssetIds: string[];
|
|
26
|
+
encodedAssets: EncodedAsset[];
|
|
27
|
+
};
|
|
28
|
+
export type EncodedRelatedAssetIndex = Record<number, number[]>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import type { BN } from '../bignumber/bignumber';
|
|
3
|
+
export declare const fromBaseUnit: (value: BigNumber.Value, precision: number, displayDecimals?: number) => string;
|
|
4
|
+
export declare const toBaseUnit: (amount: BigNumber.Value | undefined, precision: number, roundingMode?: BigNumber.RoundingMode) => string;
|
|
5
|
+
export declare const firstNonZeroDecimal: (number: BN) => string | undefined;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import { bn, bnOrZero } from '../bignumber/bignumber';
|
|
3
|
+
// Converts from base unit to a precision/ish number
|
|
4
|
+
// - If no displayDecimals are provided, it will use the full precision of the number being converted
|
|
5
|
+
// - If displayDecimals are provided, it will use that precision, to return e.g a human, precision, or number stripped to any arbitrary decimal places
|
|
6
|
+
// and use the ROUND_DOWN rounding mode
|
|
7
|
+
export const fromBaseUnit = (value, precision, displayDecimals) => {
|
|
8
|
+
const precisionNumber = bnOrZero(value).div(bn(10).pow(precision));
|
|
9
|
+
if (typeof displayDecimals === 'number') {
|
|
10
|
+
return precisionNumber
|
|
11
|
+
.decimalPlaces(displayDecimals, BigNumber.ROUND_DOWN)
|
|
12
|
+
.toFixed(displayDecimals);
|
|
13
|
+
}
|
|
14
|
+
return precisionNumber.toFixed();
|
|
15
|
+
};
|
|
16
|
+
export const toBaseUnit = (amount, precision, roundingMode) => {
|
|
17
|
+
return bnOrZero(amount)
|
|
18
|
+
.times(bn(10).exponentiatedBy(bnOrZero(precision)))
|
|
19
|
+
.toFixed(0, roundingMode);
|
|
20
|
+
};
|
|
21
|
+
export const firstNonZeroDecimal = (number) => {
|
|
22
|
+
return number.toFixed(10).match(/^-?\d*\.?0*\d{0,2}/)?.[0];
|
|
23
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { fromBaseUnit } from './baseUnits';
|
|
3
|
+
describe('@/lib/math', () => {
|
|
4
|
+
describe('fromBaseUnit', () => {
|
|
5
|
+
it('should correctly convert and round down with given displayDecimals', () => {
|
|
6
|
+
const result = fromBaseUnit(123456789, 4, 2);
|
|
7
|
+
expect(result).toBe('12345.67');
|
|
8
|
+
});
|
|
9
|
+
it('should correctly convert without displayDecimals and return full precision', () => {
|
|
10
|
+
const result = fromBaseUnit(123456789, 4);
|
|
11
|
+
expect(result).toBe('12345.6789');
|
|
12
|
+
});
|
|
13
|
+
it('should correctly convert and round down with given displayDecimals for another input', () => {
|
|
14
|
+
const result = fromBaseUnit(987654321, 6, 3);
|
|
15
|
+
expect(result).toBe('987.654');
|
|
16
|
+
});
|
|
17
|
+
it('should correctly convert without displayDecimals and return full precision for another input', () => {
|
|
18
|
+
const result = fromBaseUnit(987654321, 6);
|
|
19
|
+
expect(result).toBe('987.654321');
|
|
20
|
+
});
|
|
21
|
+
it('should correctly convert and round down with given displayDecimals for high precision input', () => {
|
|
22
|
+
const result = fromBaseUnit('182912819182912192', 18, 8);
|
|
23
|
+
expect(result).toBe('0.18291281');
|
|
24
|
+
});
|
|
25
|
+
it('should correctly convert without displayDecimals and return full precision for high precision input', () => {
|
|
26
|
+
const result = fromBaseUnit('182912819182912192', 18);
|
|
27
|
+
expect(result).toBe('0.182912819182912192');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { BigNumber } from './bignumber/bignumber';
|
|
2
|
+
export declare const convertBasisPointsToDecimalPercentage: (basisPoints: BigNumber.Value) => BigNumber;
|
|
3
|
+
export declare const convertDecimalPercentageToBasisPoints: (decimalPercentage: BigNumber.Value) => BigNumber;
|
|
4
|
+
export declare const convertBasisPointsToPercentage: (basisPoints: BigNumber.Value) => BigNumber;
|
|
5
|
+
export declare const convertPercentageToBasisPoints: (percentage: BigNumber.Value) => BigNumber;
|
|
6
|
+
/**
|
|
7
|
+
* Subtracts basis point amount from a given value.
|
|
8
|
+
*
|
|
9
|
+
* @param value The value to subtract basis points from.
|
|
10
|
+
* @param basisPoints The number of basis points to subtract.
|
|
11
|
+
* @param roundingMode
|
|
12
|
+
* @returns The new number that is the input value minus the basis points of the value.
|
|
13
|
+
*/
|
|
14
|
+
export declare const subtractBasisPointAmount: (value: string, basisPoints: BigNumber.Value, roundingMode?: BigNumber.RoundingMode) => string;
|
|
15
|
+
/**
|
|
16
|
+
* Adds basis point amount from a given value.
|
|
17
|
+
*
|
|
18
|
+
* @param value The value to subtract basis points from.
|
|
19
|
+
* @param basisPoints The number of basis points to subtract.
|
|
20
|
+
* @param roundingMode
|
|
21
|
+
* @returns The new number that is the input value minus the basis points of the value.
|
|
22
|
+
*/
|
|
23
|
+
export declare const addBasisPointAmount: (value: string, basisPoints: BigNumber.Value, roundingMode?: BigNumber.RoundingMode) => string;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { bn, bnOrZero } from './bignumber/bignumber';
|
|
2
|
+
export const convertBasisPointsToDecimalPercentage = (basisPoints) => bnOrZero(basisPoints).div(10000);
|
|
3
|
+
export const convertDecimalPercentageToBasisPoints = (decimalPercentage) => bnOrZero(decimalPercentage).times(10000);
|
|
4
|
+
export const convertBasisPointsToPercentage = (basisPoints) => bnOrZero(basisPoints).div(100);
|
|
5
|
+
export const convertPercentageToBasisPoints = (percentage) => bnOrZero(percentage).times(100);
|
|
6
|
+
/**
|
|
7
|
+
* Subtracts basis point amount from a given value.
|
|
8
|
+
*
|
|
9
|
+
* @param value The value to subtract basis points from.
|
|
10
|
+
* @param basisPoints The number of basis points to subtract.
|
|
11
|
+
* @param roundingMode
|
|
12
|
+
* @returns The new number that is the input value minus the basis points of the value.
|
|
13
|
+
*/
|
|
14
|
+
export const subtractBasisPointAmount = (value, basisPoints, roundingMode) => {
|
|
15
|
+
const bigNumValue = bn(value);
|
|
16
|
+
// Basis point is 1/100th of a percent
|
|
17
|
+
const percentValue = convertBasisPointsToDecimalPercentage(basisPoints);
|
|
18
|
+
const subtractValue = bigNumValue.times(percentValue);
|
|
19
|
+
// Subtract basis points from the original value
|
|
20
|
+
const resultValue = bigNumValue.minus(subtractValue);
|
|
21
|
+
return roundingMode !== undefined ? resultValue.toFixed(0, roundingMode) : resultValue.toFixed();
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Adds basis point amount from a given value.
|
|
25
|
+
*
|
|
26
|
+
* @param value The value to subtract basis points from.
|
|
27
|
+
* @param basisPoints The number of basis points to subtract.
|
|
28
|
+
* @param roundingMode
|
|
29
|
+
* @returns The new number that is the input value minus the basis points of the value.
|
|
30
|
+
*/
|
|
31
|
+
export const addBasisPointAmount = (value, basisPoints, roundingMode) => {
|
|
32
|
+
const bigNumValue = bn(value);
|
|
33
|
+
// Basis point is 1/100th of a percent
|
|
34
|
+
const percentValue = convertBasisPointsToDecimalPercentage(basisPoints);
|
|
35
|
+
const addValue = bigNumValue.times(percentValue);
|
|
36
|
+
// Subtract basis points from the original value
|
|
37
|
+
const resultValue = bigNumValue.plus(addValue);
|
|
38
|
+
return roundingMode !== undefined ? resultValue.toFixed(0, roundingMode) : resultValue.toFixed();
|
|
39
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
export * from 'bignumber.js';
|
|
3
|
+
export type BN = BigNumber;
|
|
4
|
+
export declare const bn: (n: BigNumber.Value, base?: number) => BN;
|
|
5
|
+
export declare const bnOrZero: (n: BigNumber.Value | null | undefined) => BN;
|
|
6
|
+
export declare const positiveOrZero: (n: BigNumber.Value | null | undefined) => BN;
|
|
7
|
+
export declare const convertPrecision: ({ value, inputExponent, outputExponent, }: {
|
|
8
|
+
value: BigNumber.Value;
|
|
9
|
+
inputExponent?: number | undefined;
|
|
10
|
+
outputExponent?: number | undefined;
|
|
11
|
+
}) => BigNumber;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
export * from 'bignumber.js';
|
|
3
|
+
export const bn = (n, base = 10) => new BigNumber(n, base);
|
|
4
|
+
export const bnOrZero = (n) => {
|
|
5
|
+
const value = bn(n || 0);
|
|
6
|
+
return value.isFinite() ? value : bn(0);
|
|
7
|
+
};
|
|
8
|
+
export const positiveOrZero = (n) => {
|
|
9
|
+
const value = bn(n || 0);
|
|
10
|
+
return value.isPositive() ? value : bn(0);
|
|
11
|
+
};
|
|
12
|
+
export const convertPrecision = ({ value, inputExponent = 0, outputExponent = 0, }) => {
|
|
13
|
+
return bnOrZero(value)
|
|
14
|
+
.dividedBy(bn(10).exponentiatedBy(inputExponent))
|
|
15
|
+
.multipliedBy(bn(10).exponentiatedBy(outputExponent));
|
|
16
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { BigNumber, bn, bnOrZero, convertPrecision } from './bignumber';
|
|
3
|
+
describe('bignumber', () => {
|
|
4
|
+
describe('bnOrZero', () => {
|
|
5
|
+
it('returns an instance of bignumber', () => {
|
|
6
|
+
const zero = bnOrZero(0);
|
|
7
|
+
expect(zero).toBeInstanceOf(BigNumber);
|
|
8
|
+
const one = bnOrZero(1);
|
|
9
|
+
expect(one).toBeInstanceOf(BigNumber);
|
|
10
|
+
const foo = bnOrZero('foo');
|
|
11
|
+
expect(foo).toBeInstanceOf(BigNumber);
|
|
12
|
+
expect(foo.toString()).toBe('0');
|
|
13
|
+
const empty = bnOrZero('');
|
|
14
|
+
expect(empty).toBeInstanceOf(BigNumber);
|
|
15
|
+
expect(empty.toString()).toBe('0');
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
describe('convertPrecision', () => {
|
|
19
|
+
it('can convert values for increased precision', () => {
|
|
20
|
+
const value = '1234';
|
|
21
|
+
const expectation = bn('1234000');
|
|
22
|
+
const result = convertPrecision({ value, inputExponent: 3, outputExponent: 6 });
|
|
23
|
+
expect(result).toEqual(expectation);
|
|
24
|
+
});
|
|
25
|
+
it('can convert values for decreased precision', () => {
|
|
26
|
+
const value = '1234000';
|
|
27
|
+
const expectation = bn('1234');
|
|
28
|
+
const result = convertPrecision({ value, inputExponent: 6, outputExponent: 3 });
|
|
29
|
+
expect(result).toEqual(expectation);
|
|
30
|
+
});
|
|
31
|
+
it('treats 0 as 0 when increasing precision', () => {
|
|
32
|
+
const value = '0';
|
|
33
|
+
const expectation = bn('0');
|
|
34
|
+
const result = convertPrecision({ value, inputExponent: 12, outputExponent: 18 });
|
|
35
|
+
expect(result).toEqual(expectation);
|
|
36
|
+
});
|
|
37
|
+
it('treats 0 as 0 when decreasing precision', () => {
|
|
38
|
+
const value = '0';
|
|
39
|
+
const expectation = bn('0');
|
|
40
|
+
const result = convertPrecision({ value, inputExponent: 18, outputExponent: 12 });
|
|
41
|
+
expect(result).toEqual(expectation);
|
|
42
|
+
});
|
|
43
|
+
it('does not remove decimals with rounding', () => {
|
|
44
|
+
const value = '1234567';
|
|
45
|
+
const expectation = bn('1234.567');
|
|
46
|
+
const result = convertPrecision({ value, inputExponent: 6, outputExponent: 3 });
|
|
47
|
+
expect(result).toEqual(expectation);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { arbitrumAssetId, arbitrumNovaAssetId, avalancheAssetId, baseAssetId, bchAssetId, bscAssetId, btcAssetId, cosmosAssetId, dogeAssetId, ethAssetId, gnosisAssetId, ltcAssetId, mayachainAssetId, optimismAssetId, polygonAssetId, solAssetId, thorchainAssetId, } from '@shapeshiftoss/caip';
|
|
2
|
+
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
3
|
+
import { assertUnreachable } from './assertUnreachable';
|
|
4
|
+
export const chainIdToFeeAssetId = (_chainId) => {
|
|
5
|
+
const chainId = _chainId;
|
|
6
|
+
switch (chainId) {
|
|
7
|
+
case KnownChainIds.ArbitrumMainnet:
|
|
8
|
+
return arbitrumAssetId;
|
|
9
|
+
case KnownChainIds.AvalancheMainnet:
|
|
10
|
+
return avalancheAssetId;
|
|
11
|
+
case KnownChainIds.ArbitrumNovaMainnet:
|
|
12
|
+
return arbitrumNovaAssetId;
|
|
13
|
+
case KnownChainIds.BaseMainnet:
|
|
14
|
+
return baseAssetId;
|
|
15
|
+
case KnownChainIds.BitcoinCashMainnet:
|
|
16
|
+
return bchAssetId;
|
|
17
|
+
case KnownChainIds.BitcoinMainnet:
|
|
18
|
+
return btcAssetId;
|
|
19
|
+
case KnownChainIds.BnbSmartChainMainnet:
|
|
20
|
+
return bscAssetId;
|
|
21
|
+
case KnownChainIds.CosmosMainnet:
|
|
22
|
+
return cosmosAssetId;
|
|
23
|
+
case KnownChainIds.DogecoinMainnet:
|
|
24
|
+
return dogeAssetId;
|
|
25
|
+
case KnownChainIds.EthereumMainnet:
|
|
26
|
+
return ethAssetId;
|
|
27
|
+
case KnownChainIds.GnosisMainnet:
|
|
28
|
+
return gnosisAssetId;
|
|
29
|
+
case KnownChainIds.LitecoinMainnet:
|
|
30
|
+
return ltcAssetId;
|
|
31
|
+
case KnownChainIds.OptimismMainnet:
|
|
32
|
+
return optimismAssetId;
|
|
33
|
+
case KnownChainIds.PolygonMainnet:
|
|
34
|
+
return polygonAssetId;
|
|
35
|
+
case KnownChainIds.ThorchainMainnet:
|
|
36
|
+
return thorchainAssetId;
|
|
37
|
+
case KnownChainIds.MayachainMainnet:
|
|
38
|
+
return mayachainAssetId;
|
|
39
|
+
case KnownChainIds.SolanaMainnet:
|
|
40
|
+
return solAssetId;
|
|
41
|
+
default:
|
|
42
|
+
return assertUnreachable(chainId);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export const createThrottle = ({ capacity, costPerReq, drainPerInterval, intervalMs, }) => {
|
|
2
|
+
let currentLevel = 0;
|
|
3
|
+
let pendingResolves = [];
|
|
4
|
+
const drain = () => {
|
|
5
|
+
const drainAmount = Math.min(currentLevel, drainPerInterval);
|
|
6
|
+
currentLevel -= drainAmount;
|
|
7
|
+
// Resolve pending promises if there's enough capacity
|
|
8
|
+
while (pendingResolves.length > 0 && currentLevel + costPerReq <= capacity) {
|
|
9
|
+
const resolve = pendingResolves.shift();
|
|
10
|
+
if (resolve) {
|
|
11
|
+
currentLevel += costPerReq;
|
|
12
|
+
resolve();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
// Start the interval to drain the capacity
|
|
17
|
+
const intervalId = setInterval(drain, intervalMs);
|
|
18
|
+
const throttle = async () => {
|
|
19
|
+
if (currentLevel + costPerReq <= capacity) {
|
|
20
|
+
// If adding another request doesn't exceed capacity, proceed immediately
|
|
21
|
+
currentLevel += costPerReq;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
// Otherwise, wait until there's enough capacity
|
|
25
|
+
await new Promise(resolve => {
|
|
26
|
+
pendingResolves.push(resolve);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const clear = () => clearInterval(intervalId);
|
|
31
|
+
return { throttle, clear };
|
|
32
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ASSET_NAMESPACE } from '@shapeshiftoss/caip';
|
|
2
|
+
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
3
|
+
import { assertUnreachable } from './assertUnreachable';
|
|
4
|
+
export const getAssetNamespaceFromChainId = (chainId) => {
|
|
5
|
+
switch (chainId) {
|
|
6
|
+
case KnownChainIds.BnbSmartChainMainnet:
|
|
7
|
+
return ASSET_NAMESPACE.bep20;
|
|
8
|
+
case KnownChainIds.SolanaMainnet:
|
|
9
|
+
return ASSET_NAMESPACE.splToken;
|
|
10
|
+
case KnownChainIds.EthereumMainnet:
|
|
11
|
+
case KnownChainIds.AvalancheMainnet:
|
|
12
|
+
case KnownChainIds.OptimismMainnet:
|
|
13
|
+
case KnownChainIds.PolygonMainnet:
|
|
14
|
+
case KnownChainIds.GnosisMainnet:
|
|
15
|
+
case KnownChainIds.ArbitrumMainnet:
|
|
16
|
+
case KnownChainIds.ArbitrumNovaMainnet:
|
|
17
|
+
case KnownChainIds.BaseMainnet:
|
|
18
|
+
return ASSET_NAMESPACE.erc20;
|
|
19
|
+
case KnownChainIds.CosmosMainnet:
|
|
20
|
+
return ASSET_NAMESPACE.ibc;
|
|
21
|
+
case KnownChainIds.BitcoinMainnet:
|
|
22
|
+
case KnownChainIds.BitcoinCashMainnet:
|
|
23
|
+
case KnownChainIds.DogecoinMainnet:
|
|
24
|
+
case KnownChainIds.LitecoinMainnet:
|
|
25
|
+
case KnownChainIds.ThorchainMainnet:
|
|
26
|
+
case KnownChainIds.MayachainMainnet:
|
|
27
|
+
throw Error(`Unhandled case '${chainId}'`);
|
|
28
|
+
default:
|
|
29
|
+
return assertUnreachable(chainId);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
2
|
+
export declare const getChainShortName: (chainId: KnownChainIds) => "AVA" | "OP" | "ETH" | "POLY" | "GNO" | "BNB" | "ARB" | "ARB-Nova" | "BAS" | "BTC" | "BCH" | "COSM" | "THOR" | "MAYA" | "DOGE" | "LTC" | "SOL" | undefined;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
2
|
+
import { assertUnreachable } from './assertUnreachable';
|
|
3
|
+
export const getChainShortName = (chainId) => {
|
|
4
|
+
switch (chainId) {
|
|
5
|
+
case KnownChainIds.AvalancheMainnet:
|
|
6
|
+
return 'AVA';
|
|
7
|
+
case KnownChainIds.OptimismMainnet:
|
|
8
|
+
return 'OP';
|
|
9
|
+
case KnownChainIds.EthereumMainnet:
|
|
10
|
+
return 'ETH';
|
|
11
|
+
case KnownChainIds.PolygonMainnet:
|
|
12
|
+
return 'POLY';
|
|
13
|
+
case KnownChainIds.GnosisMainnet:
|
|
14
|
+
return 'GNO';
|
|
15
|
+
case KnownChainIds.BnbSmartChainMainnet:
|
|
16
|
+
return 'BNB';
|
|
17
|
+
case KnownChainIds.ArbitrumMainnet:
|
|
18
|
+
return 'ARB';
|
|
19
|
+
case KnownChainIds.ArbitrumNovaMainnet:
|
|
20
|
+
return 'ARB-Nova';
|
|
21
|
+
case KnownChainIds.BaseMainnet:
|
|
22
|
+
return 'BAS';
|
|
23
|
+
case KnownChainIds.BitcoinMainnet:
|
|
24
|
+
return 'BTC';
|
|
25
|
+
case KnownChainIds.BitcoinCashMainnet:
|
|
26
|
+
return 'BCH';
|
|
27
|
+
case KnownChainIds.CosmosMainnet:
|
|
28
|
+
return 'COSM';
|
|
29
|
+
case KnownChainIds.ThorchainMainnet:
|
|
30
|
+
return 'THOR';
|
|
31
|
+
case KnownChainIds.MayachainMainnet:
|
|
32
|
+
return 'MAYA';
|
|
33
|
+
case KnownChainIds.DogecoinMainnet:
|
|
34
|
+
return 'DOGE';
|
|
35
|
+
case KnownChainIds.LitecoinMainnet:
|
|
36
|
+
return 'LTC';
|
|
37
|
+
case KnownChainIds.SolanaMainnet:
|
|
38
|
+
return 'SOL';
|
|
39
|
+
default: {
|
|
40
|
+
assertUnreachable(chainId);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ASSET_REFERENCE, CHAIN_NAMESPACE, CHAIN_REFERENCE } from '@shapeshiftoss/caip';
|
|
2
|
+
export const getNativeFeeAssetReference = (chainNamespace, chainReference) => {
|
|
3
|
+
return (() => {
|
|
4
|
+
switch (chainNamespace) {
|
|
5
|
+
case CHAIN_NAMESPACE.Utxo:
|
|
6
|
+
switch (chainReference) {
|
|
7
|
+
case CHAIN_REFERENCE.BitcoinMainnet:
|
|
8
|
+
return ASSET_REFERENCE.Bitcoin;
|
|
9
|
+
case CHAIN_REFERENCE.BitcoinCashMainnet:
|
|
10
|
+
return ASSET_REFERENCE.BitcoinCash;
|
|
11
|
+
case CHAIN_REFERENCE.DogecoinMainnet:
|
|
12
|
+
return ASSET_REFERENCE.Dogecoin;
|
|
13
|
+
case CHAIN_REFERENCE.LitecoinMainnet:
|
|
14
|
+
return ASSET_REFERENCE.Litecoin;
|
|
15
|
+
default:
|
|
16
|
+
throw new Error(`Chain namespace ${chainNamespace} on ${chainReference} not supported.`);
|
|
17
|
+
}
|
|
18
|
+
case CHAIN_NAMESPACE.Evm:
|
|
19
|
+
switch (chainReference) {
|
|
20
|
+
case CHAIN_REFERENCE.AvalancheCChain:
|
|
21
|
+
return ASSET_REFERENCE.AvalancheC;
|
|
22
|
+
case CHAIN_REFERENCE.EthereumMainnet:
|
|
23
|
+
return ASSET_REFERENCE.Ethereum;
|
|
24
|
+
case CHAIN_REFERENCE.OptimismMainnet:
|
|
25
|
+
return ASSET_REFERENCE.Optimism;
|
|
26
|
+
case CHAIN_REFERENCE.BnbSmartChainMainnet:
|
|
27
|
+
return ASSET_REFERENCE.BnbSmartChain;
|
|
28
|
+
case CHAIN_REFERENCE.PolygonMainnet:
|
|
29
|
+
return ASSET_REFERENCE.Polygon;
|
|
30
|
+
case CHAIN_REFERENCE.GnosisMainnet:
|
|
31
|
+
return ASSET_REFERENCE.Gnosis;
|
|
32
|
+
case CHAIN_REFERENCE.ArbitrumMainnet:
|
|
33
|
+
return ASSET_REFERENCE.Arbitrum;
|
|
34
|
+
case CHAIN_REFERENCE.ArbitrumNovaMainnet:
|
|
35
|
+
return ASSET_REFERENCE.ArbitrumNova;
|
|
36
|
+
case CHAIN_REFERENCE.BaseMainnet:
|
|
37
|
+
return ASSET_REFERENCE.Base;
|
|
38
|
+
default:
|
|
39
|
+
throw new Error(`Chain namespace ${chainNamespace} on ${chainReference} not supported.`);
|
|
40
|
+
}
|
|
41
|
+
case CHAIN_NAMESPACE.CosmosSdk:
|
|
42
|
+
switch (chainReference) {
|
|
43
|
+
case CHAIN_REFERENCE.CosmosHubMainnet:
|
|
44
|
+
return ASSET_REFERENCE.Cosmos;
|
|
45
|
+
case CHAIN_REFERENCE.ThorchainMainnet:
|
|
46
|
+
return ASSET_REFERENCE.Thorchain;
|
|
47
|
+
case CHAIN_REFERENCE.MayachainMainnet:
|
|
48
|
+
return ASSET_REFERENCE.Mayachain;
|
|
49
|
+
default:
|
|
50
|
+
throw new Error(`Chain namespace ${chainNamespace} on ${chainReference} not supported.`);
|
|
51
|
+
}
|
|
52
|
+
case CHAIN_NAMESPACE.Solana:
|
|
53
|
+
switch (chainReference) {
|
|
54
|
+
case CHAIN_REFERENCE.SolanaMainnet:
|
|
55
|
+
return ASSET_REFERENCE.Solana;
|
|
56
|
+
default:
|
|
57
|
+
throw new Error(`Chain namespace ${chainNamespace} on ${chainReference} not supported.`);
|
|
58
|
+
}
|
|
59
|
+
default:
|
|
60
|
+
throw new Error(`Chain namespace ${chainNamespace} on ${chainReference} not supported.`);
|
|
61
|
+
}
|
|
62
|
+
})();
|
|
63
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { HistoryTimeframe } from '@shapeshiftoss/types';
|
|
2
|
+
import dayjs from 'dayjs';
|
|
3
|
+
import { assertUnreachable } from './assertUnreachable';
|
|
4
|
+
export const getHistoryTimeframeBounds = (timeframe, end = dayjs().endOf('day')) => {
|
|
5
|
+
let start = end; // Shut up typescript.
|
|
6
|
+
switch (timeframe) {
|
|
7
|
+
case HistoryTimeframe.HOUR:
|
|
8
|
+
start = end.subtract(1, 'day');
|
|
9
|
+
break;
|
|
10
|
+
case HistoryTimeframe.DAY:
|
|
11
|
+
start = end.subtract(1, 'day');
|
|
12
|
+
break;
|
|
13
|
+
case HistoryTimeframe.WEEK:
|
|
14
|
+
start = end.subtract(1, 'week');
|
|
15
|
+
break;
|
|
16
|
+
case HistoryTimeframe.MONTH:
|
|
17
|
+
start = end.subtract(1, 'month');
|
|
18
|
+
break;
|
|
19
|
+
case HistoryTimeframe.YEAR:
|
|
20
|
+
start = end.subtract(1, 'year');
|
|
21
|
+
break;
|
|
22
|
+
case HistoryTimeframe.ALL:
|
|
23
|
+
start = end.subtract(5, 'years');
|
|
24
|
+
break;
|
|
25
|
+
default:
|
|
26
|
+
assertUnreachable(timeframe);
|
|
27
|
+
}
|
|
28
|
+
return { start, end };
|
|
29
|
+
};
|