@talismn/token-rates 0.0.0-pr447-20230213021041

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/CHANGELOG.md ADDED
@@ -0,0 +1,106 @@
1
+ # @talismn/token-rates
2
+
3
+ ## 0.0.0-pr447-20230213021041
4
+
5
+ ### Patch Changes
6
+
7
+ - 8adc7f0: feat: switched build tool to preconstruct
8
+ - Updated dependencies [8adc7f0]
9
+ - @talismn/chaindata-provider@0.0.0-pr447-20230213021041
10
+
11
+ ## 0.1.12
12
+
13
+ ### Patch Changes
14
+
15
+ - 4aa691d: feat: new balance modules
16
+ - Updated dependencies [4aa691d]
17
+ - @talismn/chaindata-provider@0.2.1
18
+
19
+ ## 0.1.11
20
+
21
+ ### Patch Changes
22
+
23
+ - @talismn/chaindata-provider@0.2.0
24
+
25
+ ## 0.1.10
26
+
27
+ ### Patch Changes
28
+
29
+ - fix: a variety of improvements from the wallet integration
30
+ - Updated dependencies
31
+ - @talismn/chaindata-provider@0.1.10
32
+
33
+ ## 0.1.9
34
+
35
+ ### Patch Changes
36
+
37
+ - Updated dependencies [8ecb8214]
38
+ - @talismn/chaindata-provider@0.1.9
39
+
40
+ ## 0.1.8
41
+
42
+ ### Patch Changes
43
+
44
+ - @talismn/chaindata-provider@0.1.8
45
+
46
+ ## 0.1.7
47
+
48
+ ### Patch Changes
49
+
50
+ - db04d0d: fix: missing token rates and empty token rates requests
51
+ - @talismn/chaindata-provider@0.1.7
52
+
53
+ ## 0.1.6
54
+
55
+ ### Patch Changes
56
+
57
+ - ca50757: feat: implemented token fiat rates in @talismn/balances
58
+ - Updated dependencies [ca50757]
59
+ - @talismn/chaindata-provider@0.1.6
60
+
61
+ ## 0.1.5
62
+
63
+ ### Patch Changes
64
+
65
+ - Updated dependencies [d66c5bc]
66
+ - @talismn/chaindata-provider@0.1.5
67
+
68
+ ## 0.1.4
69
+
70
+ ### Patch Changes
71
+
72
+ - @talismn/chaindata-provider@0.1.4
73
+
74
+ ## 0.1.3
75
+
76
+ ### Patch Changes
77
+
78
+ - Updated dependencies [d5f69f7]
79
+ - @talismn/chaindata-provider@0.1.3
80
+
81
+ ## 0.1.2
82
+
83
+ ### Patch Changes
84
+
85
+ - 5af305c: switched build output from esm to commonjs for ecosystem compatibility
86
+ - Updated dependencies [5af305c]
87
+ - @talismn/chaindata-provider@0.1.2
88
+
89
+ ## 0.1.1
90
+
91
+ ### Patch Changes
92
+
93
+ - Fixed publish config
94
+ - Updated dependencies
95
+ - @talismn/chaindata-provider@0.1.1
96
+
97
+ ## 0.1.0
98
+
99
+ ### Minor Changes
100
+
101
+ - 43c1a3a: Initial release
102
+
103
+ ### Patch Changes
104
+
105
+ - Updated dependencies [43c1a3a]
106
+ - @talismn/chaindata-provider@0.1.0
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @talismn/token-rates
@@ -0,0 +1,7 @@
1
+ import { IToken, TokenId } from "@talismn/chaindata-provider";
2
+ import { TokenRatesList } from "./types";
3
+ export declare function fetchTokenRates(tokens: Record<TokenId, IToken>): Promise<TokenRatesList>;
4
+ export interface WithCoingeckoId {
5
+ coingeckoId: string;
6
+ }
7
+ export declare function hasCoingeckoId(token: IToken): token is IToken & WithCoingeckoId;
@@ -0,0 +1,2 @@
1
+ export * from "./TokenRates";
2
+ export * from "./types";
@@ -0,0 +1,32 @@
1
+ import { TokenId } from "@talismn/chaindata-provider";
2
+ export type TokenRatesList = Record<TokenId, TokenRates>;
3
+ export type TokenRateCurrency = keyof TokenRates;
4
+ export type TokenRates = {
5
+ /** us dollar rate */
6
+ usd: number | null;
7
+ /** australian dollar rate */
8
+ aud: number | null;
9
+ /** new zealand dollar rate */
10
+ nzd: number | null;
11
+ /** canadian dollar rate */
12
+ cud: number | null;
13
+ /** hong kong dollar rate */
14
+ hkd: number | null;
15
+ /** euro rate */
16
+ eur: number | null;
17
+ /** british pound sterling rate */
18
+ gbp: number | null;
19
+ /** japanese yen rate */
20
+ jpy: number | null;
21
+ /** south korean won rate */
22
+ krw: number | null;
23
+ /** chinese yuan rate */
24
+ cny: number | null;
25
+ /** btc rate */
26
+ btc: number | null;
27
+ /** eth rate */
28
+ eth: number | null;
29
+ /** dot rate */
30
+ dot: number | null;
31
+ };
32
+ export declare const NewTokenRates: () => TokenRates;
@@ -0,0 +1 @@
1
+ export * from "./declarations/src/index";
@@ -0,0 +1,108 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var axios = require('axios');
6
+
7
+ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
8
+
9
+ var axios__default = /*#__PURE__*/_interopDefault(axios);
10
+
11
+ const NewTokenRates = () => ({
12
+ usd: null,
13
+ aud: null,
14
+ nzd: null,
15
+ cud: null,
16
+ hkd: null,
17
+ eur: null,
18
+ gbp: null,
19
+ jpy: null,
20
+ krw: null,
21
+ cny: null,
22
+ btc: null,
23
+ eth: null,
24
+ dot: null
25
+ });
26
+
27
+ // the base url of the v3 coingecko api
28
+ const coingeckoApiUrl = "https://api.coingecko.com/api/v3";
29
+
30
+ // every currency in this list will be fetched from coingecko
31
+ // comment out unused currencies to save some bandwidth!
32
+ const coingeckoCurrencies = ["usd",
33
+ // 'aud',
34
+ // 'nzd',
35
+ // 'cud',
36
+ // 'hkd',
37
+ "eur"
38
+ // 'gbp',
39
+ // 'jpy',
40
+ // 'krw',
41
+ // 'cny',
42
+ // 'btc',
43
+ // 'eth',
44
+ // 'dot',
45
+ ];
46
+
47
+ // export function tokenRates(tokens: WithCoingeckoId[]): TokenRatesList {}
48
+ async function fetchTokenRates(tokens) {
49
+ // create a map from `coingeckoId` -> `tokenId` for each token
50
+ const coingeckoIdToTokenIds = Object.values(tokens)
51
+ // ignore testnet tokens
52
+ .filter(({
53
+ isTestnet
54
+ }) => !isTestnet)
55
+
56
+ // ignore tokens which don't have a coingeckoId
57
+ .filter(hasCoingeckoId)
58
+
59
+ // get each token's coingeckoId
60
+ .reduce((coingeckoIdToTokenIds, {
61
+ id,
62
+ coingeckoId
63
+ }) => {
64
+ if (!coingeckoIdToTokenIds[coingeckoId]) coingeckoIdToTokenIds[coingeckoId] = [];
65
+ coingeckoIdToTokenIds[coingeckoId].push(id);
66
+ return coingeckoIdToTokenIds;
67
+ }, {});
68
+
69
+ // create a list of coingeckoIds we want to fetch
70
+ const coingeckoIds = Object.keys(coingeckoIdToTokenIds);
71
+
72
+ // skip network request if there is nothing for us to fetch
73
+ if (coingeckoIds.length < 1) return {};
74
+
75
+ // construct a coingecko request
76
+ const idsSerialized = coingeckoIds.join(",");
77
+ const currenciesSerialized = coingeckoCurrencies.join(",");
78
+ const queryUrl = `${coingeckoApiUrl}/simple/price?ids=${idsSerialized}&vs_currencies=${currenciesSerialized}`;
79
+
80
+ // fetch the token prices from coingecko
81
+ // the response should be in the format:
82
+ // {
83
+ // [coingeckoId]: {
84
+ // [currency]: rate
85
+ // }
86
+ // }
87
+ const coingeckoPrices = await axios__default["default"].get(queryUrl).then(response => response.data);
88
+
89
+ // build a TokenRatesList from the token prices result
90
+ const ratesList = Object.fromEntries(coingeckoIds.flatMap(coingeckoId => {
91
+ const tokenIds = coingeckoIdToTokenIds[coingeckoId];
92
+ const rates = NewTokenRates();
93
+ for (const currency of coingeckoCurrencies) {
94
+ rates[currency] = ((coingeckoPrices || {})[coingeckoId] || {})[currency] || null;
95
+ }
96
+ return tokenIds.map(tokenId => [tokenId, rates]);
97
+ }));
98
+
99
+ // return the TokenRatesList
100
+ return ratesList;
101
+ }
102
+ function hasCoingeckoId(token) {
103
+ return "coingeckoId" in token && typeof token.coingeckoId === "string";
104
+ }
105
+
106
+ exports.NewTokenRates = NewTokenRates;
107
+ exports.fetchTokenRates = fetchTokenRates;
108
+ exports.hasCoingeckoId = hasCoingeckoId;
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ if (process.env.NODE_ENV === "production") {
4
+ module.exports = require("./talismn-token-rates.cjs.prod.js");
5
+ } else {
6
+ module.exports = require("./talismn-token-rates.cjs.dev.js");
7
+ }
@@ -0,0 +1,108 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var axios = require('axios');
6
+
7
+ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
8
+
9
+ var axios__default = /*#__PURE__*/_interopDefault(axios);
10
+
11
+ const NewTokenRates = () => ({
12
+ usd: null,
13
+ aud: null,
14
+ nzd: null,
15
+ cud: null,
16
+ hkd: null,
17
+ eur: null,
18
+ gbp: null,
19
+ jpy: null,
20
+ krw: null,
21
+ cny: null,
22
+ btc: null,
23
+ eth: null,
24
+ dot: null
25
+ });
26
+
27
+ // the base url of the v3 coingecko api
28
+ const coingeckoApiUrl = "https://api.coingecko.com/api/v3";
29
+
30
+ // every currency in this list will be fetched from coingecko
31
+ // comment out unused currencies to save some bandwidth!
32
+ const coingeckoCurrencies = ["usd",
33
+ // 'aud',
34
+ // 'nzd',
35
+ // 'cud',
36
+ // 'hkd',
37
+ "eur"
38
+ // 'gbp',
39
+ // 'jpy',
40
+ // 'krw',
41
+ // 'cny',
42
+ // 'btc',
43
+ // 'eth',
44
+ // 'dot',
45
+ ];
46
+
47
+ // export function tokenRates(tokens: WithCoingeckoId[]): TokenRatesList {}
48
+ async function fetchTokenRates(tokens) {
49
+ // create a map from `coingeckoId` -> `tokenId` for each token
50
+ const coingeckoIdToTokenIds = Object.values(tokens)
51
+ // ignore testnet tokens
52
+ .filter(({
53
+ isTestnet
54
+ }) => !isTestnet)
55
+
56
+ // ignore tokens which don't have a coingeckoId
57
+ .filter(hasCoingeckoId)
58
+
59
+ // get each token's coingeckoId
60
+ .reduce((coingeckoIdToTokenIds, {
61
+ id,
62
+ coingeckoId
63
+ }) => {
64
+ if (!coingeckoIdToTokenIds[coingeckoId]) coingeckoIdToTokenIds[coingeckoId] = [];
65
+ coingeckoIdToTokenIds[coingeckoId].push(id);
66
+ return coingeckoIdToTokenIds;
67
+ }, {});
68
+
69
+ // create a list of coingeckoIds we want to fetch
70
+ const coingeckoIds = Object.keys(coingeckoIdToTokenIds);
71
+
72
+ // skip network request if there is nothing for us to fetch
73
+ if (coingeckoIds.length < 1) return {};
74
+
75
+ // construct a coingecko request
76
+ const idsSerialized = coingeckoIds.join(",");
77
+ const currenciesSerialized = coingeckoCurrencies.join(",");
78
+ const queryUrl = `${coingeckoApiUrl}/simple/price?ids=${idsSerialized}&vs_currencies=${currenciesSerialized}`;
79
+
80
+ // fetch the token prices from coingecko
81
+ // the response should be in the format:
82
+ // {
83
+ // [coingeckoId]: {
84
+ // [currency]: rate
85
+ // }
86
+ // }
87
+ const coingeckoPrices = await axios__default["default"].get(queryUrl).then(response => response.data);
88
+
89
+ // build a TokenRatesList from the token prices result
90
+ const ratesList = Object.fromEntries(coingeckoIds.flatMap(coingeckoId => {
91
+ const tokenIds = coingeckoIdToTokenIds[coingeckoId];
92
+ const rates = NewTokenRates();
93
+ for (const currency of coingeckoCurrencies) {
94
+ rates[currency] = ((coingeckoPrices || {})[coingeckoId] || {})[currency] || null;
95
+ }
96
+ return tokenIds.map(tokenId => [tokenId, rates]);
97
+ }));
98
+
99
+ // return the TokenRatesList
100
+ return ratesList;
101
+ }
102
+ function hasCoingeckoId(token) {
103
+ return "coingeckoId" in token && typeof token.coingeckoId === "string";
104
+ }
105
+
106
+ exports.NewTokenRates = NewTokenRates;
107
+ exports.fetchTokenRates = fetchTokenRates;
108
+ exports.hasCoingeckoId = hasCoingeckoId;
@@ -0,0 +1,98 @@
1
+ import axios from 'axios';
2
+
3
+ const NewTokenRates = () => ({
4
+ usd: null,
5
+ aud: null,
6
+ nzd: null,
7
+ cud: null,
8
+ hkd: null,
9
+ eur: null,
10
+ gbp: null,
11
+ jpy: null,
12
+ krw: null,
13
+ cny: null,
14
+ btc: null,
15
+ eth: null,
16
+ dot: null
17
+ });
18
+
19
+ // the base url of the v3 coingecko api
20
+ const coingeckoApiUrl = "https://api.coingecko.com/api/v3";
21
+
22
+ // every currency in this list will be fetched from coingecko
23
+ // comment out unused currencies to save some bandwidth!
24
+ const coingeckoCurrencies = ["usd",
25
+ // 'aud',
26
+ // 'nzd',
27
+ // 'cud',
28
+ // 'hkd',
29
+ "eur"
30
+ // 'gbp',
31
+ // 'jpy',
32
+ // 'krw',
33
+ // 'cny',
34
+ // 'btc',
35
+ // 'eth',
36
+ // 'dot',
37
+ ];
38
+
39
+ // export function tokenRates(tokens: WithCoingeckoId[]): TokenRatesList {}
40
+ async function fetchTokenRates(tokens) {
41
+ // create a map from `coingeckoId` -> `tokenId` for each token
42
+ const coingeckoIdToTokenIds = Object.values(tokens)
43
+ // ignore testnet tokens
44
+ .filter(({
45
+ isTestnet
46
+ }) => !isTestnet)
47
+
48
+ // ignore tokens which don't have a coingeckoId
49
+ .filter(hasCoingeckoId)
50
+
51
+ // get each token's coingeckoId
52
+ .reduce((coingeckoIdToTokenIds, {
53
+ id,
54
+ coingeckoId
55
+ }) => {
56
+ if (!coingeckoIdToTokenIds[coingeckoId]) coingeckoIdToTokenIds[coingeckoId] = [];
57
+ coingeckoIdToTokenIds[coingeckoId].push(id);
58
+ return coingeckoIdToTokenIds;
59
+ }, {});
60
+
61
+ // create a list of coingeckoIds we want to fetch
62
+ const coingeckoIds = Object.keys(coingeckoIdToTokenIds);
63
+
64
+ // skip network request if there is nothing for us to fetch
65
+ if (coingeckoIds.length < 1) return {};
66
+
67
+ // construct a coingecko request
68
+ const idsSerialized = coingeckoIds.join(",");
69
+ const currenciesSerialized = coingeckoCurrencies.join(",");
70
+ const queryUrl = `${coingeckoApiUrl}/simple/price?ids=${idsSerialized}&vs_currencies=${currenciesSerialized}`;
71
+
72
+ // fetch the token prices from coingecko
73
+ // the response should be in the format:
74
+ // {
75
+ // [coingeckoId]: {
76
+ // [currency]: rate
77
+ // }
78
+ // }
79
+ const coingeckoPrices = await axios.get(queryUrl).then(response => response.data);
80
+
81
+ // build a TokenRatesList from the token prices result
82
+ const ratesList = Object.fromEntries(coingeckoIds.flatMap(coingeckoId => {
83
+ const tokenIds = coingeckoIdToTokenIds[coingeckoId];
84
+ const rates = NewTokenRates();
85
+ for (const currency of coingeckoCurrencies) {
86
+ rates[currency] = ((coingeckoPrices || {})[coingeckoId] || {})[currency] || null;
87
+ }
88
+ return tokenIds.map(tokenId => [tokenId, rates]);
89
+ }));
90
+
91
+ // return the TokenRatesList
92
+ return ratesList;
93
+ }
94
+ function hasCoingeckoId(token) {
95
+ return "coingeckoId" in token && typeof token.coingeckoId === "string";
96
+ }
97
+
98
+ export { NewTokenRates, fetchTokenRates, hasCoingeckoId };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@talismn/token-rates",
3
+ "version": "0.0.0-pr447-20230213021041",
4
+ "author": "Talisman",
5
+ "homepage": "https://talisman.xyz",
6
+ "license": "UNLICENSED",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "repository": {
11
+ "directory": "packages/token-rates",
12
+ "type": "git",
13
+ "url": "https://github.com/talismansociety/talisman.git"
14
+ },
15
+ "main": "dist/talismn-token-rates.cjs.js",
16
+ "module": "dist/talismn-token-rates.esm.js",
17
+ "files": [
18
+ "/dist"
19
+ ],
20
+ "engines": {
21
+ "node": ">=14"
22
+ },
23
+ "scripts": {
24
+ "test": "jest",
25
+ "lint": "eslint . --max-warnings 0",
26
+ "clean": "rm -rf dist && rm -rf .turbo rm -rf node_modules"
27
+ },
28
+ "dependencies": {
29
+ "@talismn/chaindata-provider": "^0.0.0-pr447-20230213021041",
30
+ "axios": "^0.27.2"
31
+ },
32
+ "devDependencies": {
33
+ "@talismn/eslint-config": "^0.0.0-pr447-20230213021041",
34
+ "@talismn/tsconfig": "^0.0.0-pr447-20230213021041",
35
+ "@types/jest": "^27.5.1",
36
+ "eslint": "^8.4.0",
37
+ "jest": "^28.1.0",
38
+ "ts-jest": "^28.0.2",
39
+ "typescript": "^4.6.4"
40
+ },
41
+ "eslintConfig": {
42
+ "root": true,
43
+ "extends": [
44
+ "@talismn/eslint-config/base"
45
+ ]
46
+ }
47
+ }