@0xtorch/core 0.0.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 +15 -0
- package/dist/import/accounts/index.d.ts +2 -0
- package/dist/import/accounts/index.d.ts.map +1 -0
- package/dist/import/accounts/types/account.d.ts +6 -0
- package/dist/import/accounts/types/account.d.ts.map +1 -0
- package/dist/import/accounts/types/index.d.ts +2 -0
- package/dist/import/accounts/types/index.d.ts.map +1 -0
- package/dist/import/actions/index.d.ts +2 -0
- package/dist/import/actions/index.d.ts.map +1 -0
- package/dist/import/actions/types/addLiquidity.d.ts +7 -0
- package/dist/import/actions/types/addLiquidity.d.ts.map +1 -0
- package/dist/import/actions/types/atomicArbitrage.d.ts +5 -0
- package/dist/import/actions/types/atomicArbitrage.d.ts.map +1 -0
- package/dist/import/actions/types/bridgeFrom.d.ts +6 -0
- package/dist/import/actions/types/bridgeFrom.d.ts.map +1 -0
- package/dist/import/actions/types/bridgeTo.d.ts +6 -0
- package/dist/import/actions/types/bridgeTo.d.ts.map +1 -0
- package/dist/import/actions/types/buyNft.d.ts +7 -0
- package/dist/import/actions/types/buyNft.d.ts.map +1 -0
- package/dist/import/actions/types/common.d.ts +50 -0
- package/dist/import/actions/types/common.d.ts.map +1 -0
- package/dist/import/actions/types/deposit.d.ts +6 -0
- package/dist/import/actions/types/deposit.d.ts.map +1 -0
- package/dist/import/actions/types/depositWithBond.d.ts +7 -0
- package/dist/import/actions/types/depositWithBond.d.ts.map +1 -0
- package/dist/import/actions/types/freeMintNft.d.ts +8 -0
- package/dist/import/actions/types/freeMintNft.d.ts.map +1 -0
- package/dist/import/actions/types/income.d.ts +5 -0
- package/dist/import/actions/types/income.d.ts.map +1 -0
- package/dist/import/actions/types/index.d.ts +24 -0
- package/dist/import/actions/types/index.d.ts.map +1 -0
- package/dist/import/actions/types/move.d.ts +5 -0
- package/dist/import/actions/types/move.d.ts.map +1 -0
- package/dist/import/actions/types/nftRoyalty.d.ts +7 -0
- package/dist/import/actions/types/nftRoyalty.d.ts.map +1 -0
- package/dist/import/actions/types/removeLiquidity.d.ts +7 -0
- package/dist/import/actions/types/removeLiquidity.d.ts.map +1 -0
- package/dist/import/actions/types/sellNft.d.ts +6 -0
- package/dist/import/actions/types/sellNft.d.ts.map +1 -0
- package/dist/import/actions/types/spam.d.ts +6 -0
- package/dist/import/actions/types/spam.d.ts.map +1 -0
- package/dist/import/actions/types/swapIncome.d.ts +4 -0
- package/dist/import/actions/types/swapIncome.d.ts.map +1 -0
- package/dist/import/actions/types/swapNft.d.ts +6 -0
- package/dist/import/actions/types/swapNft.d.ts.map +1 -0
- package/dist/import/actions/types/swapTransfer.d.ts +4 -0
- package/dist/import/actions/types/swapTransfer.d.ts.map +1 -0
- package/dist/import/actions/types/trade.d.ts +7 -0
- package/dist/import/actions/types/trade.d.ts.map +1 -0
- package/dist/import/actions/types/transactionFee.d.ts +7 -0
- package/dist/import/actions/types/transactionFee.d.ts.map +1 -0
- package/dist/import/actions/types/transfer.d.ts +8 -0
- package/dist/import/actions/types/transfer.d.ts.map +1 -0
- package/dist/import/actions/types/withdraw.d.ts +7 -0
- package/dist/import/actions/types/withdraw.d.ts.map +1 -0
- package/dist/import/actions/types/withdrawWithBond.d.ts +7 -0
- package/dist/import/actions/types/withdrawWithBond.d.ts.map +1 -0
- package/dist/import/apps/index.d.ts +3 -0
- package/dist/import/apps/index.d.ts.map +1 -0
- package/dist/import/apps/types.d.ts +11 -0
- package/dist/import/apps/types.d.ts.map +1 -0
- package/dist/import/assets/constants.d.ts +2 -0
- package/dist/import/assets/constants.d.ts.map +1 -0
- package/dist/import/assets/creates/createCryptoCurrency.d.ts +3 -0
- package/dist/import/assets/creates/createCryptoCurrency.d.ts.map +1 -0
- package/dist/import/assets/creates/createFiatCurrency.d.ts +3 -0
- package/dist/import/assets/creates/createFiatCurrency.d.ts.map +1 -0
- package/dist/import/assets/creates/index.d.ts +3 -0
- package/dist/import/assets/creates/index.d.ts.map +1 -0
- package/dist/import/assets/definitions/astar.d.ts +13 -0
- package/dist/import/assets/definitions/astar.d.ts.map +1 -0
- package/dist/import/assets/definitions/avalanche.d.ts +13 -0
- package/dist/import/assets/definitions/avalanche.d.ts.map +1 -0
- package/dist/import/assets/definitions/bnb.d.ts +13 -0
- package/dist/import/assets/definitions/bnb.d.ts.map +1 -0
- package/dist/import/assets/definitions/coingeckoCryptoCurrencyDataSource.d.ts +18 -0
- package/dist/import/assets/definitions/coingeckoCryptoCurrencyDataSource.d.ts.map +1 -0
- package/dist/import/assets/definitions/ethereum.d.ts +13 -0
- package/dist/import/assets/definitions/ethereum.d.ts.map +1 -0
- package/dist/import/assets/definitions/eur.d.ts +8 -0
- package/dist/import/assets/definitions/eur.d.ts.map +1 -0
- package/dist/import/assets/definitions/fantom.d.ts +13 -0
- package/dist/import/assets/definitions/fantom.d.ts.map +1 -0
- package/dist/import/assets/definitions/index.d.ts +17 -0
- package/dist/import/assets/definitions/index.d.ts.map +1 -0
- package/dist/import/assets/definitions/jpy.d.ts +8 -0
- package/dist/import/assets/definitions/jpy.d.ts.map +1 -0
- package/dist/import/assets/definitions/kava.d.ts +13 -0
- package/dist/import/assets/definitions/kava.d.ts.map +1 -0
- package/dist/import/assets/definitions/mantle.d.ts +13 -0
- package/dist/import/assets/definitions/mantle.d.ts.map +1 -0
- package/dist/import/assets/definitions/maticNetwork.d.ts +13 -0
- package/dist/import/assets/definitions/maticNetwork.d.ts.map +1 -0
- package/dist/import/assets/definitions/metis.d.ts +13 -0
- package/dist/import/assets/definitions/metis.d.ts.map +1 -0
- package/dist/import/assets/definitions/moonbeam.d.ts +13 -0
- package/dist/import/assets/definitions/moonbeam.d.ts.map +1 -0
- package/dist/import/assets/definitions/moonriver.d.ts +13 -0
- package/dist/import/assets/definitions/moonriver.d.ts.map +1 -0
- package/dist/import/assets/definitions/ronin.d.ts +13 -0
- package/dist/import/assets/definitions/ronin.d.ts.map +1 -0
- package/dist/import/assets/definitions/usd.d.ts +8 -0
- package/dist/import/assets/definitions/usd.d.ts.map +1 -0
- package/dist/import/assets/externals/coingecko/constants.d.ts +2 -0
- package/dist/import/assets/externals/coingecko/constants.d.ts.map +1 -0
- package/dist/import/assets/externals/coingecko/getAutoRetry.d.ts +4 -0
- package/dist/import/assets/externals/coingecko/getAutoRetry.d.ts.map +1 -0
- package/dist/import/assets/externals/coingecko/getCoinMarketChartRange.d.ts +49 -0
- package/dist/import/assets/externals/coingecko/getCoinMarketChartRange.d.ts.map +1 -0
- package/dist/import/assets/externals/coingecko/getCoinsMarkets.d.ts +58 -0
- package/dist/import/assets/externals/coingecko/getCoinsMarkets.d.ts.map +1 -0
- package/dist/import/assets/externals/coingecko/index.d.ts +3 -0
- package/dist/import/assets/externals/coingecko/index.d.ts.map +1 -0
- package/dist/import/assets/externals/index.d.ts +2 -0
- package/dist/import/assets/externals/index.d.ts.map +1 -0
- package/dist/import/assets/index.d.ts +5 -0
- package/dist/import/assets/index.d.ts.map +1 -0
- package/dist/import/assets/types/cryptoCurrency.d.ts +17 -0
- package/dist/import/assets/types/cryptoCurrency.d.ts.map +1 -0
- package/dist/import/assets/types/cryptoCurrencyPrice.d.ts +10 -0
- package/dist/import/assets/types/cryptoCurrencyPrice.d.ts.map +1 -0
- package/dist/import/assets/types/dataSource.d.ts +18 -0
- package/dist/import/assets/types/dataSource.d.ts.map +1 -0
- package/dist/import/assets/types/fiatCurrency.d.ts +12 -0
- package/dist/import/assets/types/fiatCurrency.d.ts.map +1 -0
- package/dist/import/assets/types/index.d.ts +6 -0
- package/dist/import/assets/types/index.d.ts.map +1 -0
- package/dist/import/assets/types/nft.d.ts +5 -0
- package/dist/import/assets/types/nft.d.ts.map +1 -0
- package/dist/import/index.d.mts +12 -0
- package/dist/import/index.d.mts.map +1 -0
- package/dist/import/index.mjs +2 -0
- package/dist/import/portfolios/createPortfolio.d.ts +2 -0
- package/dist/import/portfolios/createPortfolio.d.ts.map +1 -0
- package/dist/import/portfolios/index.d.ts +3 -0
- package/dist/import/portfolios/index.d.ts.map +1 -0
- package/dist/import/portfolios/type.d.ts +4 -0
- package/dist/import/portfolios/type.d.ts.map +1 -0
- package/dist/import/types.d.ts +11 -0
- package/dist/import/types.d.ts.map +1 -0
- package/dist/import/utils/errors.d.ts +48 -0
- package/dist/import/utils/errors.d.ts.map +1 -0
- package/dist/import/utils/index.d.ts +5 -0
- package/dist/import/utils/index.d.ts.map +1 -0
- package/dist/import/utils/rest.d.ts +14 -0
- package/dist/import/utils/rest.d.ts.map +1 -0
- package/dist/import/utils/stringify.d.ts +2 -0
- package/dist/import/utils/stringify.d.ts.map +1 -0
- package/dist/import/utils/types.d.ts +12 -0
- package/dist/import/utils/types.d.ts.map +1 -0
- package/dist/require/index.cjs +2 -0
- package/dist/require/index.d.cts +2 -0
- package/dist/require/index.d.cts.map +1 -0
- package/package.json +79 -0
- package/src/accounts/index.ts +1 -0
- package/src/accounts/types/account.ts +5 -0
- package/src/accounts/types/index.ts +1 -0
- package/src/actions/index.ts +44 -0
- package/src/actions/schemas/constants.ts +16 -0
- package/src/actions/schemas/swap.ts +7 -0
- package/src/actions/types/addLiquidity.ts +23 -0
- package/src/actions/types/atomicArbitrage.ts +30 -0
- package/src/actions/types/bridgeFrom.ts +23 -0
- package/src/actions/types/bridgeTo.ts +23 -0
- package/src/actions/types/buyNft.ts +34 -0
- package/src/actions/types/common.ts +142 -0
- package/src/actions/types/deposit.ts +23 -0
- package/src/actions/types/depositWithBond.ts +23 -0
- package/src/actions/types/freeMintNft.ts +20 -0
- package/src/actions/types/income.ts +21 -0
- package/src/actions/types/index.ts +53 -0
- package/src/actions/types/move.ts +21 -0
- package/src/actions/types/nftRoyalty.ts +19 -0
- package/src/actions/types/removeLiquidity.ts +23 -0
- package/src/actions/types/sellNft.ts +22 -0
- package/src/actions/types/spam.ts +23 -0
- package/src/actions/types/swapIncome.ts +12 -0
- package/src/actions/types/swapNft.ts +12 -0
- package/src/actions/types/swapTransfer.ts +12 -0
- package/src/actions/types/trade.ts +34 -0
- package/src/actions/types/transactionFee.ts +19 -0
- package/src/actions/types/transfer.ts +36 -0
- package/src/actions/types/withdraw.ts +23 -0
- package/src/actions/types/withdrawWithBond.ts +23 -0
- package/src/apps/index.ts +2 -0
- package/src/apps/types.ts +20 -0
- package/src/assets/constants.ts +1 -0
- package/src/assets/creates/createCryptoCurrency.ts +7 -0
- package/src/assets/creates/createFiatCurrency.ts +10 -0
- package/src/assets/creates/index.ts +2 -0
- package/src/assets/definitions/astar.ts +14 -0
- package/src/assets/definitions/avalanche.ts +14 -0
- package/src/assets/definitions/bnb.ts +14 -0
- package/src/assets/definitions/coingeckoCryptoCurrencyDataSource.test.ts +53 -0
- package/src/assets/definitions/coingeckoCryptoCurrencyDataSource.ts +169 -0
- package/src/assets/definitions/ethereum.ts +14 -0
- package/src/assets/definitions/eur.ts +9 -0
- package/src/assets/definitions/fantom.ts +14 -0
- package/src/assets/definitions/index.ts +16 -0
- package/src/assets/definitions/jpy.ts +9 -0
- package/src/assets/definitions/kava.ts +14 -0
- package/src/assets/definitions/mantle.ts +14 -0
- package/src/assets/definitions/maticNetwork.ts +14 -0
- package/src/assets/definitions/metis.ts +14 -0
- package/src/assets/definitions/moonbeam.ts +14 -0
- package/src/assets/definitions/moonriver.ts +14 -0
- package/src/assets/definitions/ronin.ts +14 -0
- package/src/assets/definitions/usd.ts +9 -0
- package/src/assets/externals/coingecko/constants.ts +1 -0
- package/src/assets/externals/coingecko/getAutoRetry.ts +35 -0
- package/src/assets/externals/coingecko/getCoinMarketChartRange.test.ts +28 -0
- package/src/assets/externals/coingecko/getCoinMarketChartRange.ts +88 -0
- package/src/assets/externals/coingecko/getCoinsMarkets.test.ts +11 -0
- package/src/assets/externals/coingecko/getCoinsMarkets.ts +106 -0
- package/src/assets/externals/coingecko/index.ts +2 -0
- package/src/assets/externals/index.ts +1 -0
- package/src/assets/index.ts +27 -0
- package/src/assets/types/cryptoCurrency.ts +17 -0
- package/src/assets/types/cryptoCurrencyPrice.ts +14 -0
- package/src/assets/types/dataSource.ts +26 -0
- package/src/assets/types/fiatCurrency.ts +21 -0
- package/src/assets/types/index.ts +9 -0
- package/src/assets/types/nft.ts +5 -0
- package/src/index.cts +1 -0
- package/src/index.mts +79 -0
- package/src/portfolios/createPortfolio.test.ts +7 -0
- package/src/portfolios/createPortfolio.ts +3 -0
- package/src/portfolios/index.ts +2 -0
- package/src/portfolios/type.ts +3 -0
- package/src/tests/constants.ts +1 -0
- package/src/tests/index.ts +2 -0
- package/src/tests/utils.ts +26 -0
- package/src/types.ts +11 -0
- package/src/utils/errors.ts +130 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/rest.test.ts +19 -0
- package/src/utils/rest.ts +138 -0
- package/src/utils/stringify.test.ts +24 -0
- package/src/utils/stringify.ts +11 -0
- package/src/utils/types.ts +12 -0
- package/tsconfig.json +28 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Config } from '../../../types'
|
|
2
|
+
import type { Schema } from '../../../utils'
|
|
3
|
+
import { rest } from '../../../utils'
|
|
4
|
+
import { HttpRequestError } from '../../../utils/errors'
|
|
5
|
+
|
|
6
|
+
export const getAutoRetry = async <TData>(
|
|
7
|
+
url: string,
|
|
8
|
+
schema: Schema<TData>,
|
|
9
|
+
config?: Config,
|
|
10
|
+
): Promise<TData> => {
|
|
11
|
+
const timeoutTimestamp =
|
|
12
|
+
Date.now() + (config?.timeout === undefined ? 60_000 : config.timeout)
|
|
13
|
+
|
|
14
|
+
let mut_retryCount = 0
|
|
15
|
+
const loop = true
|
|
16
|
+
while (loop) {
|
|
17
|
+
try {
|
|
18
|
+
return await rest(url, {
|
|
19
|
+
schema,
|
|
20
|
+
timeout: timeoutTimestamp - Date.now(),
|
|
21
|
+
logger: config?.logger,
|
|
22
|
+
})
|
|
23
|
+
} catch (error) {
|
|
24
|
+
if (error instanceof HttpRequestError && error.status === 429) {
|
|
25
|
+
mut_retryCount += 1
|
|
26
|
+
await new Promise<void>((resolve) =>
|
|
27
|
+
setTimeout(resolve, mut_retryCount + 1 * 60_000),
|
|
28
|
+
)
|
|
29
|
+
} else {
|
|
30
|
+
throw error
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
throw new Error('Unreachable')
|
|
35
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { isIntegrationTest } from '../../../tests'
|
|
3
|
+
import { getCoinMarketChartRange } from './getCoinMarketChartRange'
|
|
4
|
+
|
|
5
|
+
describe('getCoinMarketChartRange', () => {
|
|
6
|
+
it.if(isIntegrationTest)('Should return validated object', async () => {
|
|
7
|
+
const to = Math.floor(Date.now() / 1000)
|
|
8
|
+
const days90Seconds = 60 * 60 * 24 * 90
|
|
9
|
+
const from = to - days90Seconds
|
|
10
|
+
const data = await getCoinMarketChartRange({ id: 'bitcoin', from, to })
|
|
11
|
+
|
|
12
|
+
// expected length is 90 days * 24 hours
|
|
13
|
+
const expectedLength = 90 * 24
|
|
14
|
+
expect(data.prices.length).toEqual(expectedLength)
|
|
15
|
+
expect(data.market_caps.length).toEqual(expectedLength)
|
|
16
|
+
|
|
17
|
+
// expected time difference between two prices is 1 hour
|
|
18
|
+
const hourMilliseconds = 1000 * 60 * 60
|
|
19
|
+
const expectedDiffTimeMin = hourMilliseconds * 0.9
|
|
20
|
+
const expectedDiffTimeMax = hourMilliseconds * 1.1
|
|
21
|
+
expect(data.prices[1][0] - data.prices[0][0]).toBeGreaterThan(
|
|
22
|
+
expectedDiffTimeMin,
|
|
23
|
+
)
|
|
24
|
+
expect(data.prices[1][0] - data.prices[0][0]).toBeLessThan(
|
|
25
|
+
expectedDiffTimeMax,
|
|
26
|
+
)
|
|
27
|
+
})
|
|
28
|
+
})
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ReadonlyDeep } from 'type-fest'
|
|
2
|
+
import { z } from 'zod'
|
|
3
|
+
import type { Config } from '../../../types'
|
|
4
|
+
import { rootUrl } from './constants'
|
|
5
|
+
import { getAutoRetry } from './getAutoRetry'
|
|
6
|
+
|
|
7
|
+
type GetCoinMarketChartRangeParameters = {
|
|
8
|
+
/**
|
|
9
|
+
* coin id
|
|
10
|
+
*
|
|
11
|
+
* @example "bitcoin"
|
|
12
|
+
* */
|
|
13
|
+
readonly id: string
|
|
14
|
+
/** The target currency of market data (usd, eur, jpy, etc.) */
|
|
15
|
+
readonly vs_currency?: string
|
|
16
|
+
/**
|
|
17
|
+
* From date in UNIX Timestamp
|
|
18
|
+
*
|
|
19
|
+
* @example 1392577232
|
|
20
|
+
*/
|
|
21
|
+
readonly from: number
|
|
22
|
+
/**
|
|
23
|
+
* To date in UNIX Timestamp
|
|
24
|
+
*
|
|
25
|
+
* @example 1422577232
|
|
26
|
+
*/
|
|
27
|
+
readonly to: number
|
|
28
|
+
/**
|
|
29
|
+
* **full** or any value between 0 - 18 to specify decimal place for currency price value
|
|
30
|
+
*/
|
|
31
|
+
readonly precision?:
|
|
32
|
+
| 'full'
|
|
33
|
+
| '0'
|
|
34
|
+
| '1'
|
|
35
|
+
| '2'
|
|
36
|
+
| '3'
|
|
37
|
+
| '4'
|
|
38
|
+
| '5'
|
|
39
|
+
| '6'
|
|
40
|
+
| '7'
|
|
41
|
+
| '8'
|
|
42
|
+
| '9'
|
|
43
|
+
| '10'
|
|
44
|
+
| '11'
|
|
45
|
+
| '12'
|
|
46
|
+
| '13'
|
|
47
|
+
| '14'
|
|
48
|
+
| '15'
|
|
49
|
+
| '16'
|
|
50
|
+
| '17'
|
|
51
|
+
| '18'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const getCoinMarketChartRangeResponseSchema = z.object({
|
|
55
|
+
/** [UNIXTIME milliseconds, price][] @example [[1703229613690, 43920.82996790591]] */
|
|
56
|
+
prices: z.array(z.tuple([z.number(), z.number()])),
|
|
57
|
+
/** [UNIXTIME milliseconds, market_cap][] @example [[1703229613690, 860219630538.0055]] */
|
|
58
|
+
market_caps: z.array(z.tuple([z.number(), z.number()])),
|
|
59
|
+
/** [UNIXTIME milliseconds, total_volume][] @example [[1703229613690, 21233021243.384785]] */
|
|
60
|
+
total_volumes: z.array(z.tuple([z.number(), z.number()])),
|
|
61
|
+
})
|
|
62
|
+
type GetCoinMarketChartRangeResponse = ReadonlyDeep<
|
|
63
|
+
z.infer<typeof getCoinMarketChartRangeResponseSchema>
|
|
64
|
+
>
|
|
65
|
+
|
|
66
|
+
export const getCoinMarketChartRange = async (
|
|
67
|
+
{
|
|
68
|
+
id,
|
|
69
|
+
vs_currency = 'usd',
|
|
70
|
+
from,
|
|
71
|
+
to,
|
|
72
|
+
precision = 'full',
|
|
73
|
+
}: GetCoinMarketChartRangeParameters,
|
|
74
|
+
config?: Config,
|
|
75
|
+
): Promise<GetCoinMarketChartRangeResponse> => {
|
|
76
|
+
const url = new URL(`${rootUrl}/coins/${id}/market_chart/range`)
|
|
77
|
+
url.searchParams.set('vs_currency', vs_currency)
|
|
78
|
+
url.searchParams.set('from', from.toString())
|
|
79
|
+
url.searchParams.set('to', to.toString())
|
|
80
|
+
url.searchParams.set('precision', precision)
|
|
81
|
+
|
|
82
|
+
const response = await getAutoRetry(
|
|
83
|
+
url.toString(),
|
|
84
|
+
getCoinMarketChartRangeResponseSchema,
|
|
85
|
+
config,
|
|
86
|
+
)
|
|
87
|
+
return response
|
|
88
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { isIntegrationTest } from '../../../tests'
|
|
3
|
+
import { getCoinsMarkets } from './getCoinsMarkets'
|
|
4
|
+
|
|
5
|
+
describe('getCoinsMarkets', () => {
|
|
6
|
+
it.if(isIntegrationTest)('Should return validated object', async () => {
|
|
7
|
+
const data = await getCoinsMarkets({ ids: ['bitcoin'] })
|
|
8
|
+
expect(data).toHaveLength(1)
|
|
9
|
+
expect(data[0].id).toEqual('bitcoin')
|
|
10
|
+
})
|
|
11
|
+
})
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { ReadonlyDeep } from 'type-fest'
|
|
2
|
+
import { z } from 'zod'
|
|
3
|
+
import type { Config } from '../../../types'
|
|
4
|
+
import { rootUrl } from './constants'
|
|
5
|
+
import { getAutoRetry } from './getAutoRetry'
|
|
6
|
+
|
|
7
|
+
type GetCoinMarketsParameters = {
|
|
8
|
+
readonly vs_currency?: string
|
|
9
|
+
readonly ids?: readonly string[]
|
|
10
|
+
readonly category?: string
|
|
11
|
+
readonly order?:
|
|
12
|
+
| 'market_cap_asc'
|
|
13
|
+
| 'market_cap_desc'
|
|
14
|
+
| 'volume_asc'
|
|
15
|
+
| 'volume_desc'
|
|
16
|
+
| 'id_asc'
|
|
17
|
+
| 'id_desc'
|
|
18
|
+
readonly per_page?: number
|
|
19
|
+
readonly page?: number
|
|
20
|
+
readonly sparkline?: boolean
|
|
21
|
+
readonly price_change_percentage?: string
|
|
22
|
+
readonly precision?:
|
|
23
|
+
| 'full'
|
|
24
|
+
| '0'
|
|
25
|
+
| '1'
|
|
26
|
+
| '2'
|
|
27
|
+
| '3'
|
|
28
|
+
| '4'
|
|
29
|
+
| '5'
|
|
30
|
+
| '6'
|
|
31
|
+
| '7'
|
|
32
|
+
| '8'
|
|
33
|
+
| '9'
|
|
34
|
+
| '10'
|
|
35
|
+
| '11'
|
|
36
|
+
| '12'
|
|
37
|
+
| '13'
|
|
38
|
+
| '14'
|
|
39
|
+
| '15'
|
|
40
|
+
| '16'
|
|
41
|
+
| '17'
|
|
42
|
+
| '18'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const getCoinMarketsResponseSchema = z.array(
|
|
46
|
+
z.object({
|
|
47
|
+
/** @example "bitcoin" */
|
|
48
|
+
id: z.string(),
|
|
49
|
+
/** @example "btc" */
|
|
50
|
+
symbol: z.string(),
|
|
51
|
+
/** @example "Bitcoin" */
|
|
52
|
+
name: z.string(),
|
|
53
|
+
/** @example "https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1696501400" */
|
|
54
|
+
image: z.string(),
|
|
55
|
+
/** @example 43639.197719840064 */
|
|
56
|
+
current_price: z.number(),
|
|
57
|
+
/** @example 854305524160 */
|
|
58
|
+
market_cap: z.number().nullable().optional(),
|
|
59
|
+
/** @example 1 */
|
|
60
|
+
market_cap_rank: z.number().int().nullable().optional(),
|
|
61
|
+
/** @example "2023-12-22T09:35:19.746Z" */
|
|
62
|
+
last_updated: z.string(),
|
|
63
|
+
}),
|
|
64
|
+
)
|
|
65
|
+
type CoinMarketsResponse = ReadonlyDeep<
|
|
66
|
+
z.infer<typeof getCoinMarketsResponseSchema>
|
|
67
|
+
>
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* List all supported coins price, market cap, volume, and market related data
|
|
71
|
+
* @link https://www.coingecko.com/api/documentation
|
|
72
|
+
* */
|
|
73
|
+
export const getCoinsMarkets = async (
|
|
74
|
+
{
|
|
75
|
+
vs_currency = 'usd',
|
|
76
|
+
ids,
|
|
77
|
+
category,
|
|
78
|
+
order = 'market_cap_desc',
|
|
79
|
+
per_page = 250,
|
|
80
|
+
page = 1,
|
|
81
|
+
sparkline,
|
|
82
|
+
price_change_percentage,
|
|
83
|
+
precision = 'full',
|
|
84
|
+
}: GetCoinMarketsParameters,
|
|
85
|
+
config?: Config,
|
|
86
|
+
): Promise<CoinMarketsResponse> => {
|
|
87
|
+
const url = new URL(`${rootUrl}/coins/markets`)
|
|
88
|
+
url.searchParams.append('vs_currency', vs_currency)
|
|
89
|
+
url.searchParams.append('order', order)
|
|
90
|
+
url.searchParams.append('per_page', per_page.toString())
|
|
91
|
+
url.searchParams.append('page', page.toString())
|
|
92
|
+
url.searchParams.append('precision', precision)
|
|
93
|
+
if (ids !== undefined) url.searchParams.append('ids', ids.join(','))
|
|
94
|
+
if (category !== undefined) url.searchParams.append('category', category)
|
|
95
|
+
if (sparkline !== undefined)
|
|
96
|
+
url.searchParams.append('sparkline', sparkline.toString())
|
|
97
|
+
if (price_change_percentage !== undefined)
|
|
98
|
+
url.searchParams.append('price_change_percentage', price_change_percentage)
|
|
99
|
+
|
|
100
|
+
const response = await getAutoRetry(
|
|
101
|
+
url.toString(),
|
|
102
|
+
getCoinMarketsResponseSchema,
|
|
103
|
+
config,
|
|
104
|
+
)
|
|
105
|
+
return response
|
|
106
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getCoinMarketChartRange, getCoinsMarkets } from './coingecko'
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export { fiatCurrencyDecimals } from './constants'
|
|
2
|
+
export { createCryptoCurrency, createFiatCurrency } from './creates'
|
|
3
|
+
export {
|
|
4
|
+
astar,
|
|
5
|
+
avalanche,
|
|
6
|
+
bnb,
|
|
7
|
+
coingeckoCryptoCurrencyDataSource,
|
|
8
|
+
ethereum,
|
|
9
|
+
eur,
|
|
10
|
+
fantom,
|
|
11
|
+
jpy,
|
|
12
|
+
kava,
|
|
13
|
+
mantle,
|
|
14
|
+
maticNetwork,
|
|
15
|
+
metis,
|
|
16
|
+
moonbeam,
|
|
17
|
+
moonriver,
|
|
18
|
+
ronin,
|
|
19
|
+
usd,
|
|
20
|
+
} from './definitions'
|
|
21
|
+
export type {
|
|
22
|
+
CryptoCurrency,
|
|
23
|
+
CryptoCurrencyDataSource,
|
|
24
|
+
CryptoCurrencyPrice,
|
|
25
|
+
FiatCurrency,
|
|
26
|
+
Nft,
|
|
27
|
+
} from './types'
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { BigDecimal } from '@0xtorch/big-decimal'
|
|
2
|
+
|
|
3
|
+
export type CryptoCurrency = {
|
|
4
|
+
readonly type: 'CryptoCurrency'
|
|
5
|
+
readonly id: string
|
|
6
|
+
readonly name: string
|
|
7
|
+
readonly symbol: string
|
|
8
|
+
/** icon image's url */
|
|
9
|
+
readonly icon: string | undefined
|
|
10
|
+
readonly market: CryptoCurrencyMarket
|
|
11
|
+
readonly updatedAt: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type CryptoCurrencyMarket = {
|
|
15
|
+
readonly coingeckoId: string | undefined
|
|
16
|
+
readonly marketCapUsd: BigDecimal | undefined
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { BigDecimal } from '@0xtorch/big-decimal'
|
|
2
|
+
import type { CryptoCurrency } from '.'
|
|
3
|
+
|
|
4
|
+
export type CryptoCurrencyPrice<
|
|
5
|
+
TCryptoCurrency extends CryptoCurrency | undefined =
|
|
6
|
+
| CryptoCurrency
|
|
7
|
+
| undefined,
|
|
8
|
+
> = {
|
|
9
|
+
readonly cryptoCurrencyId: string
|
|
10
|
+
readonly crypto: TCryptoCurrency
|
|
11
|
+
readonly fiatCurrencyId: string
|
|
12
|
+
readonly price: BigDecimal
|
|
13
|
+
readonly timestamp: number
|
|
14
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CryptoCurrency, FiatCurrency } from '.'
|
|
2
|
+
import type { Config } from '../../types'
|
|
3
|
+
import type { CryptoCurrencyPrice } from './cryptoCurrencyPrice'
|
|
4
|
+
|
|
5
|
+
export type GetCurrentPriceParameters = {
|
|
6
|
+
readonly targetCurrencies: readonly CryptoCurrency[]
|
|
7
|
+
readonly vsCurrency: FiatCurrency
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type GetHistoricalPricesParameters = {
|
|
11
|
+
readonly targetCurrencies: readonly CryptoCurrency[]
|
|
12
|
+
readonly vsCurrency: FiatCurrency
|
|
13
|
+
readonly from: number
|
|
14
|
+
readonly to: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type CryptoCurrencyDataSource = {
|
|
18
|
+
readonly getCurrentPrice: (
|
|
19
|
+
parameters: GetCurrentPriceParameters,
|
|
20
|
+
config?: Config,
|
|
21
|
+
) => Promise<readonly CryptoCurrencyPrice[]>
|
|
22
|
+
readonly getHistoricalPrices: (
|
|
23
|
+
parameters: GetHistoricalPricesParameters,
|
|
24
|
+
config?: Config,
|
|
25
|
+
) => Promise<readonly CryptoCurrencyPrice[]>
|
|
26
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type FiatCurrency<Id extends FiatCurrencyId = FiatCurrencyId> = {
|
|
2
|
+
readonly type: 'FiatCurrency'
|
|
3
|
+
// TODO implement
|
|
4
|
+
readonly id: Id
|
|
5
|
+
readonly symbol: Uppercase<Id>
|
|
6
|
+
readonly name: string
|
|
7
|
+
/** icon image's url */
|
|
8
|
+
readonly icon: string | undefined
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const fiatCurrencyIds = [
|
|
12
|
+
'cny',
|
|
13
|
+
'eur',
|
|
14
|
+
'idr',
|
|
15
|
+
'jpy',
|
|
16
|
+
'krw',
|
|
17
|
+
'rub',
|
|
18
|
+
'twd',
|
|
19
|
+
'usd',
|
|
20
|
+
] as const
|
|
21
|
+
export type FiatCurrencyId = (typeof fiatCurrencyIds)[number]
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type { CryptoCurrency } from './cryptoCurrency'
|
|
2
|
+
export type { CryptoCurrencyPrice } from './cryptoCurrencyPrice'
|
|
3
|
+
export type {
|
|
4
|
+
CryptoCurrencyDataSource,
|
|
5
|
+
GetCurrentPriceParameters,
|
|
6
|
+
GetHistoricalPricesParameters,
|
|
7
|
+
} from './dataSource'
|
|
8
|
+
export type { FiatCurrency, FiatCurrencyId } from './fiatCurrency'
|
|
9
|
+
export type { Nft } from './nft'
|
package/src/index.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./index.mts')
|
package/src/index.mts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export type { Account } from './accounts'
|
|
2
|
+
export type {
|
|
3
|
+
AccountAction,
|
|
4
|
+
AccountActionAddLiquidity,
|
|
5
|
+
AccountActionAtomicArbitrage,
|
|
6
|
+
AccountActionBridgeFrom,
|
|
7
|
+
AccountActionBridgeTo,
|
|
8
|
+
AccountActionBuyNft,
|
|
9
|
+
AccountActionDeposit,
|
|
10
|
+
AccountActionDepositWithBond,
|
|
11
|
+
AccountActionFreeMintNft,
|
|
12
|
+
AccountActionIncome,
|
|
13
|
+
AccountActionMove,
|
|
14
|
+
AccountActionNftRoyalty,
|
|
15
|
+
AccountActionRemoveLiquidity,
|
|
16
|
+
AccountActionSellNft,
|
|
17
|
+
AccountActionSpam,
|
|
18
|
+
AccountActionSwapIncome,
|
|
19
|
+
AccountActionSwapNft,
|
|
20
|
+
AccountActionSwapTransfer,
|
|
21
|
+
AccountActionTrade,
|
|
22
|
+
AccountActionTransactionFee,
|
|
23
|
+
AccountActionTransfer,
|
|
24
|
+
AccountActionType,
|
|
25
|
+
AccountActionWithdraw,
|
|
26
|
+
AccountActionWithdrawWithBond,
|
|
27
|
+
ActionType,
|
|
28
|
+
Evidence,
|
|
29
|
+
NormalAction,
|
|
30
|
+
NormalActionAddLiquidity,
|
|
31
|
+
NormalActionAtomicArbitrage,
|
|
32
|
+
NormalActionBridgeFrom,
|
|
33
|
+
NormalActionBridgeTo,
|
|
34
|
+
NormalActionBuyNft,
|
|
35
|
+
NormalActionDeposit,
|
|
36
|
+
NormalActionDepositWithBond,
|
|
37
|
+
NormalActionFreeMintNft,
|
|
38
|
+
NormalActionRemoveLiquidity,
|
|
39
|
+
NormalActionSpam,
|
|
40
|
+
NormalActionTrade,
|
|
41
|
+
NormalActionTransactionFee,
|
|
42
|
+
NormalActionTransfer,
|
|
43
|
+
NormalActionWithdraw,
|
|
44
|
+
NormalActionWithdrawWithBond,
|
|
45
|
+
} from './actions'
|
|
46
|
+
export { appCategories } from './apps'
|
|
47
|
+
export type { App, AppCategory } from './apps'
|
|
48
|
+
export {
|
|
49
|
+
astar,
|
|
50
|
+
avalanche,
|
|
51
|
+
bnb,
|
|
52
|
+
coingeckoCryptoCurrencyDataSource,
|
|
53
|
+
createCryptoCurrency,
|
|
54
|
+
ethereum,
|
|
55
|
+
eur,
|
|
56
|
+
fantom,
|
|
57
|
+
fiatCurrencyDecimals,
|
|
58
|
+
jpy,
|
|
59
|
+
kava,
|
|
60
|
+
mantle,
|
|
61
|
+
maticNetwork,
|
|
62
|
+
metis,
|
|
63
|
+
moonbeam,
|
|
64
|
+
moonriver,
|
|
65
|
+
ronin,
|
|
66
|
+
usd,
|
|
67
|
+
} from './assets'
|
|
68
|
+
export type {
|
|
69
|
+
CryptoCurrency,
|
|
70
|
+
CryptoCurrencyDataSource,
|
|
71
|
+
CryptoCurrencyPrice,
|
|
72
|
+
FiatCurrency,
|
|
73
|
+
Nft,
|
|
74
|
+
} from './assets'
|
|
75
|
+
export { createPortfolio } from './portfolios'
|
|
76
|
+
export type { Portfolio } from './portfolios'
|
|
77
|
+
export type { Config, Logger } from './types'
|
|
78
|
+
export { rest, stringify } from './utils'
|
|
79
|
+
export type { Schema } from './utils'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const isIntegrationTest = process.env.INTEGRATION_TEST === 'true'
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createBigDecimal } from '@0xtorch/big-decimal'
|
|
2
|
+
import type { CryptoCurrencyDataSource } from '../assets'
|
|
3
|
+
|
|
4
|
+
export const cryptoCurrencyDataSourceTest: CryptoCurrencyDataSource = {
|
|
5
|
+
getCurrentPrice: async ({ targetCurrencies, vsCurrency }) => {
|
|
6
|
+
await new Promise((resolve) => setTimeout(resolve))
|
|
7
|
+
const timestamp = Date.now()
|
|
8
|
+
return targetCurrencies.map((currency) => ({
|
|
9
|
+
cryptoCurrencyId: currency.id,
|
|
10
|
+
crypto: currency,
|
|
11
|
+
fiatCurrencyId: vsCurrency.id,
|
|
12
|
+
price: createBigDecimal(100n),
|
|
13
|
+
timestamp,
|
|
14
|
+
}))
|
|
15
|
+
},
|
|
16
|
+
getHistoricalPrices: ({ targetCurrencies, vsCurrency, from, to }) => {
|
|
17
|
+
// TODO implement
|
|
18
|
+
console.debug('getHistoricalPrices', {
|
|
19
|
+
targetCurrencies,
|
|
20
|
+
vsCurrency,
|
|
21
|
+
from,
|
|
22
|
+
to,
|
|
23
|
+
})
|
|
24
|
+
throw new Error('Not implemented')
|
|
25
|
+
},
|
|
26
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type Logger = {
|
|
2
|
+
debug: (...data: unknown[]) => void
|
|
3
|
+
info: (...data: unknown[]) => void
|
|
4
|
+
warn: (...data: unknown[]) => void
|
|
5
|
+
error: (...data: unknown[]) => void
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type Config = {
|
|
9
|
+
readonly timeout?: number
|
|
10
|
+
readonly logger?: Logger
|
|
11
|
+
}
|