@talismn/token-rates 0.1.5 → 0.1.6
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 +8 -0
- package/dist/TokenRates.d.ts +7 -31
- package/dist/TokenRates.js +78 -9
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +32 -0
- package/dist/types.js +19 -0
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
package/dist/TokenRates.d.ts
CHANGED
@@ -1,31 +1,7 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
export declare
|
4
|
-
export
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
};
|
1
|
+
import { Token, TokenList } from "@talismn/chaindata-provider";
|
2
|
+
import { TokenRatesList } from "./types";
|
3
|
+
export declare function fetchTokenRates(tokens: TokenList): Promise<TokenRatesList>;
|
4
|
+
export interface WithCoingeckoId {
|
5
|
+
coingeckoId: string;
|
6
|
+
}
|
7
|
+
export declare function hasCoingeckoId(token: Token): token is Token & WithCoingeckoId;
|
package/dist/TokenRates.js
CHANGED
@@ -1,12 +1,81 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
|
+
};
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
+
exports.hasCoingeckoId = exports.fetchTokenRates = void 0;
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
17
|
+
const types_1 = require("./types");
|
18
|
+
// the base url of the v3 coingecko api
|
3
19
|
const coingeckoApiUrl = "https://api.coingecko.com/api/v3";
|
4
|
-
//
|
5
|
-
//
|
6
|
-
|
7
|
-
|
8
|
-
//
|
9
|
-
//
|
10
|
-
//
|
11
|
-
//
|
12
|
-
|
20
|
+
// every currency in this list will be fetched from coingecko
|
21
|
+
// comment out unused currencies to save some bandwidth!
|
22
|
+
const coingeckoCurrencies = [
|
23
|
+
"usd",
|
24
|
+
// 'aud',
|
25
|
+
// 'nzd',
|
26
|
+
// 'cud',
|
27
|
+
// 'hkd',
|
28
|
+
"eur",
|
29
|
+
// 'gbp',
|
30
|
+
// 'jpy',
|
31
|
+
// 'krw',
|
32
|
+
// 'cny',
|
33
|
+
// 'btc',
|
34
|
+
// 'eth',
|
35
|
+
// 'dot',
|
36
|
+
];
|
37
|
+
// export function tokenRates(tokens: WithCoingeckoId[]): TokenRatesList {}
|
38
|
+
function fetchTokenRates(tokens) {
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
40
|
+
// create a map from `coingeckoId` -> `tokenId` for each token
|
41
|
+
const coingeckoIdToTokenId = Object.fromEntries(Object.values(tokens)
|
42
|
+
// ignore testnet tokens
|
43
|
+
.filter(({ isTestnet }) => !isTestnet)
|
44
|
+
// ignore tokens which don't have a coingeckoId
|
45
|
+
.filter(hasCoingeckoId)
|
46
|
+
// get each token's coingeckoId
|
47
|
+
.map(({ id, coingeckoId }) => [coingeckoId, id]));
|
48
|
+
// create a list of coingeckoIds we want to fetch
|
49
|
+
const coingeckoIds = Object.keys(coingeckoIdToTokenId);
|
50
|
+
// construct a coingecko request
|
51
|
+
const idsSerialized = coingeckoIds.join(",");
|
52
|
+
const currenciesSerialized = coingeckoCurrencies.join(",");
|
53
|
+
const queryUrl = `${coingeckoApiUrl}/simple/price?ids=${idsSerialized}&vs_currencies=${currenciesSerialized}`;
|
54
|
+
// fetch the token prices from coingecko
|
55
|
+
// the response should be in the format:
|
56
|
+
// {
|
57
|
+
// [coingeckoId]: {
|
58
|
+
// [currency]: rate
|
59
|
+
// }
|
60
|
+
// }
|
61
|
+
const coingeckoPrices = yield axios_1.default
|
62
|
+
.get(queryUrl)
|
63
|
+
.then((response) => response.data);
|
64
|
+
// build a TokenRatesList from the token prices result
|
65
|
+
const ratesList = Object.fromEntries(coingeckoIds.map((coingeckoId) => {
|
66
|
+
const tokenId = coingeckoIdToTokenId[coingeckoId];
|
67
|
+
const rates = (0, types_1.NewTokenRates)();
|
68
|
+
for (const currency of coingeckoCurrencies) {
|
69
|
+
rates[currency] = ((coingeckoPrices || {})[coingeckoId] || {})[currency] || null;
|
70
|
+
}
|
71
|
+
return [tokenId, rates];
|
72
|
+
}));
|
73
|
+
// return the TokenRatesList
|
74
|
+
return ratesList;
|
75
|
+
});
|
76
|
+
}
|
77
|
+
exports.fetchTokenRates = fetchTokenRates;
|
78
|
+
function hasCoingeckoId(token) {
|
79
|
+
return "coingeckoId" in token && typeof token.coingeckoId === "string";
|
80
|
+
}
|
81
|
+
exports.hasCoingeckoId = hasCoingeckoId;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/types.d.ts
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
import { TokenId } from "@talismn/chaindata-provider";
|
2
|
+
export declare type TokenRatesList = Record<TokenId, TokenRates>;
|
3
|
+
export declare type TokenRateCurrency = keyof TokenRates;
|
4
|
+
export declare 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;
|
package/dist/types.js
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.NewTokenRates = void 0;
|
4
|
+
const NewTokenRates = () => ({
|
5
|
+
usd: null,
|
6
|
+
aud: null,
|
7
|
+
nzd: null,
|
8
|
+
cud: null,
|
9
|
+
hkd: null,
|
10
|
+
eur: null,
|
11
|
+
gbp: null,
|
12
|
+
jpy: null,
|
13
|
+
krw: null,
|
14
|
+
cny: null,
|
15
|
+
btc: null,
|
16
|
+
eth: null,
|
17
|
+
dot: null,
|
18
|
+
});
|
19
|
+
exports.NewTokenRates = NewTokenRates;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@talismn/token-rates",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.6",
|
4
4
|
"author": "Talisman",
|
5
5
|
"homepage": "https://talisman.xyz",
|
6
6
|
"license": "UNLICENSED",
|
@@ -30,7 +30,8 @@
|
|
30
30
|
"clean": "rm -rf dist && rm -rf .turbo rm -rf node_modules"
|
31
31
|
},
|
32
32
|
"dependencies": {
|
33
|
-
"@talismn/chaindata-provider": "^0.1.
|
33
|
+
"@talismn/chaindata-provider": "^0.1.6",
|
34
|
+
"axios": "^0.27.2"
|
34
35
|
},
|
35
36
|
"devDependencies": {
|
36
37
|
"@talismn/eslint-config": "^0.0.0",
|