@imtbl/cryptofiat 2.0.0-alpha.6 → 2.0.0-alpha.7
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/browser/index.js +105 -3
- package/dist/node/index.cjs +107 -5
- package/dist/node/index.js +105 -3
- package/package.json +2 -2
package/dist/browser/index.js
CHANGED
|
@@ -1,6 +1,108 @@
|
|
|
1
1
|
import { Environment } from '@imtbl/config';
|
|
2
|
-
import
|
|
2
|
+
import axios from 'axios';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
// src/config/index.ts
|
|
5
|
+
var CryptoFiatConfiguration = class {
|
|
6
|
+
baseConfig;
|
|
7
|
+
/**
|
|
8
|
+
* Creates an instance of CryptoFiatConfiguration.
|
|
9
|
+
*/
|
|
10
|
+
constructor({ baseConfig }) {
|
|
11
|
+
this.baseConfig = baseConfig;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var CHECKOUT_API_BASE_URL = {
|
|
15
|
+
[Environment.SANDBOX]: "https://checkout-api.sandbox.immutable.com",
|
|
16
|
+
[Environment.PRODUCTION]: "https://checkout-api.immutable.com"
|
|
17
|
+
};
|
|
18
|
+
var DEFAULT_FIAT_SYMBOL = "usd";
|
|
19
|
+
var CryptoFiat = class {
|
|
20
|
+
coinsCache;
|
|
21
|
+
overridesCache;
|
|
22
|
+
config;
|
|
23
|
+
/**
|
|
24
|
+
* Creates an instance of CryptoFiat.
|
|
25
|
+
* @param {CryptoFiatConfiguration} config - configuration parameters for the module
|
|
26
|
+
*/
|
|
27
|
+
constructor(config) {
|
|
28
|
+
this.coinsCache = null;
|
|
29
|
+
this.overridesCache = null;
|
|
30
|
+
this.config = config.baseConfig;
|
|
31
|
+
}
|
|
32
|
+
urlWithPath(path) {
|
|
33
|
+
return CHECKOUT_API_BASE_URL[this.config.environment] + path;
|
|
34
|
+
}
|
|
35
|
+
// Given that we could have multiple coins with the same symbol
|
|
36
|
+
// and we do not have the contract address we are forcing the
|
|
37
|
+
// conversion because we are using coingecko under the hood.
|
|
38
|
+
async fetchOverrides() {
|
|
39
|
+
if (this.overridesCache !== null) return;
|
|
40
|
+
const url = this.urlWithPath("/v1/fiat/coins/overrides");
|
|
41
|
+
const response = await axios.get(url);
|
|
42
|
+
if (response.status !== 200) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`Error fetching coins overrides: ${response.status} ${response.statusText}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
this.overridesCache = new Map(Object.entries(response.data));
|
|
48
|
+
}
|
|
49
|
+
async fetchCoins() {
|
|
50
|
+
if (this.coinsCache !== null) return;
|
|
51
|
+
await this.fetchOverrides();
|
|
52
|
+
const url = this.urlWithPath("/v1/fiat/coins/all");
|
|
53
|
+
const response = await axios.get(url);
|
|
54
|
+
if (response.status !== 200) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Error fetching coins list: ${response.status} ${response.statusText}`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
const { data } = response;
|
|
60
|
+
this.coinsCache = /* @__PURE__ */ new Map();
|
|
61
|
+
for (const coin of data) {
|
|
62
|
+
const override = this.overridesCache.get(coin.symbol.toLowerCase());
|
|
63
|
+
this.coinsCache.set(
|
|
64
|
+
coin.symbol.toLowerCase(),
|
|
65
|
+
override || coin.id.toLowerCase()
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Converts tokens with fiat currencies.
|
|
71
|
+
* @param {CryptoFiatConvertParams} - object containing the token symbols to get a conversion
|
|
72
|
+
* for and the optional fiat symbols to convert to.
|
|
73
|
+
* @returns {Promise<CryptoFiatConvertReturn>} - promise to return the map that associates
|
|
74
|
+
* token symbol to its conversion value object
|
|
75
|
+
* with fiat currencies.
|
|
76
|
+
*/
|
|
77
|
+
async convert({
|
|
78
|
+
tokenSymbols,
|
|
79
|
+
fiatSymbols = []
|
|
80
|
+
}) {
|
|
81
|
+
if (!tokenSymbols || tokenSymbols.length === 0) {
|
|
82
|
+
throw new Error("Error missing token symbols to convert");
|
|
83
|
+
}
|
|
84
|
+
const currencies = fiatSymbols.filter((fiatSymbol) => fiatSymbol !== "");
|
|
85
|
+
if (currencies.length === 0) currencies.push(DEFAULT_FIAT_SYMBOL);
|
|
86
|
+
await this.fetchCoins();
|
|
87
|
+
const idsParam = tokenSymbols.map((tokenSymbol) => this.coinsCache.get(tokenSymbol.toLowerCase())).filter((tokenSymbol) => tokenSymbol !== "" && tokenSymbol !== void 0).join(",");
|
|
88
|
+
const currenciesParam = currencies.join(",").toLowerCase();
|
|
89
|
+
const url = this.urlWithPath(`/v1/fiat/conversion?ids=${idsParam}¤cies=${currenciesParam}`);
|
|
90
|
+
const response = await axios.get(url);
|
|
91
|
+
if (response.status !== 200) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`Error fetching prices: ${response.status} ${response.statusText}`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
const { data } = response;
|
|
97
|
+
const result = {};
|
|
98
|
+
for (const symbol of tokenSymbols) {
|
|
99
|
+
const symbolKey = symbol.toLowerCase();
|
|
100
|
+
const coinId = this.coinsCache.get(symbolKey);
|
|
101
|
+
result[symbolKey] = {};
|
|
102
|
+
if (coinId) result[symbolKey] = data[coinId] || {};
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
5
107
|
|
|
6
|
-
export {
|
|
108
|
+
export { CryptoFiat, CryptoFiatConfiguration };
|
package/dist/node/index.cjs
CHANGED
|
@@ -1,13 +1,115 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var config = require('@imtbl/config');
|
|
4
|
-
var
|
|
4
|
+
var axios = require('axios');
|
|
5
5
|
|
|
6
6
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var axios__default = /*#__PURE__*/_interopDefault(axios);
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
// src/config/index.ts
|
|
11
|
+
var CryptoFiatConfiguration = class {
|
|
12
|
+
baseConfig;
|
|
13
|
+
/**
|
|
14
|
+
* Creates an instance of CryptoFiatConfiguration.
|
|
15
|
+
*/
|
|
16
|
+
constructor({ baseConfig }) {
|
|
17
|
+
this.baseConfig = baseConfig;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
var CHECKOUT_API_BASE_URL = {
|
|
21
|
+
[config.Environment.SANDBOX]: "https://checkout-api.sandbox.immutable.com",
|
|
22
|
+
[config.Environment.PRODUCTION]: "https://checkout-api.immutable.com"
|
|
23
|
+
};
|
|
24
|
+
var DEFAULT_FIAT_SYMBOL = "usd";
|
|
25
|
+
var CryptoFiat = class {
|
|
26
|
+
coinsCache;
|
|
27
|
+
overridesCache;
|
|
28
|
+
config;
|
|
29
|
+
/**
|
|
30
|
+
* Creates an instance of CryptoFiat.
|
|
31
|
+
* @param {CryptoFiatConfiguration} config - configuration parameters for the module
|
|
32
|
+
*/
|
|
33
|
+
constructor(config) {
|
|
34
|
+
this.coinsCache = null;
|
|
35
|
+
this.overridesCache = null;
|
|
36
|
+
this.config = config.baseConfig;
|
|
37
|
+
}
|
|
38
|
+
urlWithPath(path) {
|
|
39
|
+
return CHECKOUT_API_BASE_URL[this.config.environment] + path;
|
|
40
|
+
}
|
|
41
|
+
// Given that we could have multiple coins with the same symbol
|
|
42
|
+
// and we do not have the contract address we are forcing the
|
|
43
|
+
// conversion because we are using coingecko under the hood.
|
|
44
|
+
async fetchOverrides() {
|
|
45
|
+
if (this.overridesCache !== null) return;
|
|
46
|
+
const url = this.urlWithPath("/v1/fiat/coins/overrides");
|
|
47
|
+
const response = await axios__default.default.get(url);
|
|
48
|
+
if (response.status !== 200) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Error fetching coins overrides: ${response.status} ${response.statusText}`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
this.overridesCache = new Map(Object.entries(response.data));
|
|
54
|
+
}
|
|
55
|
+
async fetchCoins() {
|
|
56
|
+
if (this.coinsCache !== null) return;
|
|
57
|
+
await this.fetchOverrides();
|
|
58
|
+
const url = this.urlWithPath("/v1/fiat/coins/all");
|
|
59
|
+
const response = await axios__default.default.get(url);
|
|
60
|
+
if (response.status !== 200) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`Error fetching coins list: ${response.status} ${response.statusText}`
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
const { data } = response;
|
|
66
|
+
this.coinsCache = /* @__PURE__ */ new Map();
|
|
67
|
+
for (const coin of data) {
|
|
68
|
+
const override = this.overridesCache.get(coin.symbol.toLowerCase());
|
|
69
|
+
this.coinsCache.set(
|
|
70
|
+
coin.symbol.toLowerCase(),
|
|
71
|
+
override || coin.id.toLowerCase()
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Converts tokens with fiat currencies.
|
|
77
|
+
* @param {CryptoFiatConvertParams} - object containing the token symbols to get a conversion
|
|
78
|
+
* for and the optional fiat symbols to convert to.
|
|
79
|
+
* @returns {Promise<CryptoFiatConvertReturn>} - promise to return the map that associates
|
|
80
|
+
* token symbol to its conversion value object
|
|
81
|
+
* with fiat currencies.
|
|
82
|
+
*/
|
|
83
|
+
async convert({
|
|
84
|
+
tokenSymbols,
|
|
85
|
+
fiatSymbols = []
|
|
86
|
+
}) {
|
|
87
|
+
if (!tokenSymbols || tokenSymbols.length === 0) {
|
|
88
|
+
throw new Error("Error missing token symbols to convert");
|
|
89
|
+
}
|
|
90
|
+
const currencies = fiatSymbols.filter((fiatSymbol) => fiatSymbol !== "");
|
|
91
|
+
if (currencies.length === 0) currencies.push(DEFAULT_FIAT_SYMBOL);
|
|
92
|
+
await this.fetchCoins();
|
|
93
|
+
const idsParam = tokenSymbols.map((tokenSymbol) => this.coinsCache.get(tokenSymbol.toLowerCase())).filter((tokenSymbol) => tokenSymbol !== "" && tokenSymbol !== void 0).join(",");
|
|
94
|
+
const currenciesParam = currencies.join(",").toLowerCase();
|
|
95
|
+
const url = this.urlWithPath(`/v1/fiat/conversion?ids=${idsParam}¤cies=${currenciesParam}`);
|
|
96
|
+
const response = await axios__default.default.get(url);
|
|
97
|
+
if (response.status !== 200) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
`Error fetching prices: ${response.status} ${response.statusText}`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
const { data } = response;
|
|
103
|
+
const result = {};
|
|
104
|
+
for (const symbol of tokenSymbols) {
|
|
105
|
+
const symbolKey = symbol.toLowerCase();
|
|
106
|
+
const coinId = this.coinsCache.get(symbolKey);
|
|
107
|
+
result[symbolKey] = {};
|
|
108
|
+
if (coinId) result[symbolKey] = data[coinId] || {};
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
11
113
|
|
|
12
|
-
exports.CryptoFiat =
|
|
13
|
-
exports.CryptoFiatConfiguration =
|
|
114
|
+
exports.CryptoFiat = CryptoFiat;
|
|
115
|
+
exports.CryptoFiatConfiguration = CryptoFiatConfiguration;
|
package/dist/node/index.js
CHANGED
|
@@ -1,6 +1,108 @@
|
|
|
1
1
|
import { Environment } from '@imtbl/config';
|
|
2
|
-
import
|
|
2
|
+
import axios from 'axios';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
// src/config/index.ts
|
|
5
|
+
var CryptoFiatConfiguration = class {
|
|
6
|
+
baseConfig;
|
|
7
|
+
/**
|
|
8
|
+
* Creates an instance of CryptoFiatConfiguration.
|
|
9
|
+
*/
|
|
10
|
+
constructor({ baseConfig }) {
|
|
11
|
+
this.baseConfig = baseConfig;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var CHECKOUT_API_BASE_URL = {
|
|
15
|
+
[Environment.SANDBOX]: "https://checkout-api.sandbox.immutable.com",
|
|
16
|
+
[Environment.PRODUCTION]: "https://checkout-api.immutable.com"
|
|
17
|
+
};
|
|
18
|
+
var DEFAULT_FIAT_SYMBOL = "usd";
|
|
19
|
+
var CryptoFiat = class {
|
|
20
|
+
coinsCache;
|
|
21
|
+
overridesCache;
|
|
22
|
+
config;
|
|
23
|
+
/**
|
|
24
|
+
* Creates an instance of CryptoFiat.
|
|
25
|
+
* @param {CryptoFiatConfiguration} config - configuration parameters for the module
|
|
26
|
+
*/
|
|
27
|
+
constructor(config) {
|
|
28
|
+
this.coinsCache = null;
|
|
29
|
+
this.overridesCache = null;
|
|
30
|
+
this.config = config.baseConfig;
|
|
31
|
+
}
|
|
32
|
+
urlWithPath(path) {
|
|
33
|
+
return CHECKOUT_API_BASE_URL[this.config.environment] + path;
|
|
34
|
+
}
|
|
35
|
+
// Given that we could have multiple coins with the same symbol
|
|
36
|
+
// and we do not have the contract address we are forcing the
|
|
37
|
+
// conversion because we are using coingecko under the hood.
|
|
38
|
+
async fetchOverrides() {
|
|
39
|
+
if (this.overridesCache !== null) return;
|
|
40
|
+
const url = this.urlWithPath("/v1/fiat/coins/overrides");
|
|
41
|
+
const response = await axios.get(url);
|
|
42
|
+
if (response.status !== 200) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`Error fetching coins overrides: ${response.status} ${response.statusText}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
this.overridesCache = new Map(Object.entries(response.data));
|
|
48
|
+
}
|
|
49
|
+
async fetchCoins() {
|
|
50
|
+
if (this.coinsCache !== null) return;
|
|
51
|
+
await this.fetchOverrides();
|
|
52
|
+
const url = this.urlWithPath("/v1/fiat/coins/all");
|
|
53
|
+
const response = await axios.get(url);
|
|
54
|
+
if (response.status !== 200) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Error fetching coins list: ${response.status} ${response.statusText}`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
const { data } = response;
|
|
60
|
+
this.coinsCache = /* @__PURE__ */ new Map();
|
|
61
|
+
for (const coin of data) {
|
|
62
|
+
const override = this.overridesCache.get(coin.symbol.toLowerCase());
|
|
63
|
+
this.coinsCache.set(
|
|
64
|
+
coin.symbol.toLowerCase(),
|
|
65
|
+
override || coin.id.toLowerCase()
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Converts tokens with fiat currencies.
|
|
71
|
+
* @param {CryptoFiatConvertParams} - object containing the token symbols to get a conversion
|
|
72
|
+
* for and the optional fiat symbols to convert to.
|
|
73
|
+
* @returns {Promise<CryptoFiatConvertReturn>} - promise to return the map that associates
|
|
74
|
+
* token symbol to its conversion value object
|
|
75
|
+
* with fiat currencies.
|
|
76
|
+
*/
|
|
77
|
+
async convert({
|
|
78
|
+
tokenSymbols,
|
|
79
|
+
fiatSymbols = []
|
|
80
|
+
}) {
|
|
81
|
+
if (!tokenSymbols || tokenSymbols.length === 0) {
|
|
82
|
+
throw new Error("Error missing token symbols to convert");
|
|
83
|
+
}
|
|
84
|
+
const currencies = fiatSymbols.filter((fiatSymbol) => fiatSymbol !== "");
|
|
85
|
+
if (currencies.length === 0) currencies.push(DEFAULT_FIAT_SYMBOL);
|
|
86
|
+
await this.fetchCoins();
|
|
87
|
+
const idsParam = tokenSymbols.map((tokenSymbol) => this.coinsCache.get(tokenSymbol.toLowerCase())).filter((tokenSymbol) => tokenSymbol !== "" && tokenSymbol !== void 0).join(",");
|
|
88
|
+
const currenciesParam = currencies.join(",").toLowerCase();
|
|
89
|
+
const url = this.urlWithPath(`/v1/fiat/conversion?ids=${idsParam}¤cies=${currenciesParam}`);
|
|
90
|
+
const response = await axios.get(url);
|
|
91
|
+
if (response.status !== 200) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`Error fetching prices: ${response.status} ${response.statusText}`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
const { data } = response;
|
|
97
|
+
const result = {};
|
|
98
|
+
for (const symbol of tokenSymbols) {
|
|
99
|
+
const symbolKey = symbol.toLowerCase();
|
|
100
|
+
const coinId = this.coinsCache.get(symbolKey);
|
|
101
|
+
result[symbolKey] = {};
|
|
102
|
+
if (coinId) result[symbolKey] = data[coinId] || {};
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
5
107
|
|
|
6
|
-
export {
|
|
108
|
+
export { CryptoFiat, CryptoFiatConfiguration };
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imtbl/cryptofiat",
|
|
3
3
|
"description": "Crypto to Fiat SDK powered by Coingecko",
|
|
4
|
-
"version": "2.0.0-alpha.
|
|
4
|
+
"version": "2.0.0-alpha.7",
|
|
5
5
|
"author": "Immutable",
|
|
6
6
|
"bugs": "https://github.com/immutable/ts-immutable-sdk/issues",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@imtbl/config": "2.0.0-alpha.
|
|
8
|
+
"@imtbl/config": "2.0.0-alpha.7",
|
|
9
9
|
"axios": "^1.6.5"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|