@portal-hq/utils 0.2.14 → 0.2.15

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.
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.convert = void 0;
12
+ exports.convertLocalToCrypto = exports.convertCryptoToLocal = exports.convert = void 0;
13
13
  const countries_1 = require("./countries");
14
14
  const convert = (value, from = 'ETH', countryCode) => __awaiter(void 0, void 0, void 0, function* () {
15
15
  const country = countries_1.countryCodes[countryCode || 'US'];
@@ -23,7 +23,44 @@ const convert = (value, from = 'ETH', countryCode) => __awaiter(void 0, void 0,
23
23
  const url = `https://min-api.cryptocompare.com/data/price?fsym=${from}&tsyms=${from},${to}`;
24
24
  const response = yield fetch(url);
25
25
  const data = yield response.json();
26
- const convertedValue = Math.round((value / (1 / data[to]) + Number.EPSILON) * 100) / 100;
26
+ const convertedValue = Math.round((value / (1 / data[to]) + Number.EPSILON) * 10000) / 10000;
27
27
  return convertedValue;
28
28
  });
29
29
  exports.convert = convert;
30
+ const convertCryptoToLocal = (value, from = 'ETH', countryCode) => __awaiter(void 0, void 0, void 0, function* () {
31
+ console.log("to local");
32
+ const country = countries_1.countryCodes[countryCode || 'US'];
33
+ if (!country) {
34
+ throw new Error(`Unable to find country name for country code: ${countryCode}.`);
35
+ }
36
+ const to = countries_1.currencyCodes[country];
37
+ if (!to) {
38
+ throw new Error(`Unable to find currency code for country: ${country}.`);
39
+ }
40
+ const url = `https://min-api.cryptocompare.com/data/price?fsym=${from}&tsyms=${to}`;
41
+ const response = yield fetch(url);
42
+ const data = yield response.json();
43
+ const convertedValue = Math.round((value * data[to]) * 10000) / 10000;
44
+ console.log("Converted value: ", convertedValue);
45
+ return convertedValue;
46
+ });
47
+ exports.convertCryptoToLocal = convertCryptoToLocal;
48
+ const convertLocalToCrypto = (value, to = 'ETH', countryCode) => __awaiter(void 0, void 0, void 0, function* () {
49
+ console.log("to Crypto");
50
+ const country = countries_1.countryCodes[countryCode || 'US'];
51
+ if (!country) {
52
+ throw new Error(`Unable to find country name for country code: ${countryCode}.`);
53
+ }
54
+ const from = countries_1.currencyCodes[country];
55
+ if (!from) {
56
+ throw new Error(`Unable to find currency code for country: ${country}.`);
57
+ }
58
+ const url = `https://min-api.cryptocompare.com/data/price?fsym=${from}&tsyms=${to}`;
59
+ const response = yield fetch(url);
60
+ const data = yield response.json();
61
+ console.log(data);
62
+ const convertedValue = Math.round((value / (1 / data[to]) + Number.EPSILON) * 10000) / 10000;
63
+ console.log("Converted value: ", convertedValue);
64
+ return convertedValue;
65
+ });
66
+ exports.convertLocalToCrypto = convertLocalToCrypto;
@@ -3,8 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.HttpRequester = exports.HttpRequest = exports.parseRequest = exports.RpcErrorCodes = exports.ProviderRpcError = exports.NoAddressError = exports.MpcSigningError = exports.MissingOptionError = exports.InvalidGatewayConfigError = exports.InvalidChainIdError = exports.InvalidApiKeyError = exports.HttpError = exports.Storage = exports.PortalApi = exports.KeychainAdapter = exports.convert = void 0;
6
+ exports.HttpRequester = exports.HttpRequest = exports.parseRequest = exports.RpcErrorCodes = exports.ProviderRpcError = exports.NoAddressError = exports.MpcSigningError = exports.MissingOptionError = exports.InvalidGatewayConfigError = exports.InvalidChainIdError = exports.InvalidApiKeyError = exports.HttpError = exports.Storage = exports.PortalApi = exports.KeychainAdapter = exports.convert = exports.convertCryptoToLocal = exports.convertLocalToCrypto = void 0;
7
7
  var currency_1 = require("./currency");
8
+ Object.defineProperty(exports, "convertLocalToCrypto", { enumerable: true, get: function () { return currency_1.convertLocalToCrypto; } });
9
+ Object.defineProperty(exports, "convertCryptoToLocal", { enumerable: true, get: function () { return currency_1.convertCryptoToLocal; } });
8
10
  Object.defineProperty(exports, "convert", { enumerable: true, get: function () { return currency_1.convert; } });
9
11
  var definitions_1 = require("./definitions");
10
12
  Object.defineProperty(exports, "KeychainAdapter", { enumerable: true, get: function () { return definitions_1.KeychainAdapter; } });
@@ -20,6 +20,41 @@ export const convert = (value, from = 'ETH', countryCode) => __awaiter(void 0, v
20
20
  const url = `https://min-api.cryptocompare.com/data/price?fsym=${from}&tsyms=${from},${to}`;
21
21
  const response = yield fetch(url);
22
22
  const data = yield response.json();
23
- const convertedValue = Math.round((value / (1 / data[to]) + Number.EPSILON) * 100) / 100;
23
+ const convertedValue = Math.round((value / (1 / data[to]) + Number.EPSILON) * 10000) / 10000;
24
+ return convertedValue;
25
+ });
26
+ export const convertCryptoToLocal = (value, from = 'ETH', countryCode) => __awaiter(void 0, void 0, void 0, function* () {
27
+ console.log("to local");
28
+ const country = countryCodes[countryCode || 'US'];
29
+ if (!country) {
30
+ throw new Error(`Unable to find country name for country code: ${countryCode}.`);
31
+ }
32
+ const to = currencyCodes[country];
33
+ if (!to) {
34
+ throw new Error(`Unable to find currency code for country: ${country}.`);
35
+ }
36
+ const url = `https://min-api.cryptocompare.com/data/price?fsym=${from}&tsyms=${to}`;
37
+ const response = yield fetch(url);
38
+ const data = yield response.json();
39
+ const convertedValue = Math.round((value * data[to]) * 10000) / 10000;
40
+ console.log("Converted value: ", convertedValue);
41
+ return convertedValue;
42
+ });
43
+ export const convertLocalToCrypto = (value, to = 'ETH', countryCode) => __awaiter(void 0, void 0, void 0, function* () {
44
+ console.log("to Crypto");
45
+ const country = countryCodes[countryCode || 'US'];
46
+ if (!country) {
47
+ throw new Error(`Unable to find country name for country code: ${countryCode}.`);
48
+ }
49
+ const from = currencyCodes[country];
50
+ if (!from) {
51
+ throw new Error(`Unable to find currency code for country: ${country}.`);
52
+ }
53
+ const url = `https://min-api.cryptocompare.com/data/price?fsym=${from}&tsyms=${to}`;
54
+ const response = yield fetch(url);
55
+ const data = yield response.json();
56
+ console.log(data);
57
+ const convertedValue = Math.round((value / (1 / data[to]) + Number.EPSILON) * 10000) / 10000;
58
+ console.log("Converted value: ", convertedValue);
24
59
  return convertedValue;
25
60
  });
package/lib/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { convert } from './currency';
1
+ export { convertLocalToCrypto, convertCryptoToLocal, convert } from './currency';
2
2
  export { KeychainAdapter, PortalApi, Storage } from './definitions';
3
3
  export { HttpError, InvalidApiKeyError, InvalidChainIdError, InvalidGatewayConfigError, MissingOptionError, MpcSigningError, NoAddressError, ProviderRpcError, RpcErrorCodes, } from './errors';
4
4
  export { parseRequest } from './eth-requests';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portal-hq/utils",
3
- "version": "0.2.14",
3
+ "version": "0.2.15",
4
4
  "main": "lib/commonjs/index",
5
5
  "module": "lib/esm/index",
6
6
  "source": "src/index",
@@ -22,7 +22,70 @@ export const convert = async (
22
22
  const data = await response.json()
23
23
 
24
24
  const convertedValue =
25
- Math.round((value / (1 / data[to]) + Number.EPSILON) * 100) / 100
25
+ Math.round((value / (1 / data[to]) + Number.EPSILON) * 10000) / 10000
26
+
27
+ return convertedValue
28
+ }
29
+
30
+ export const convertCryptoToLocal = async (
31
+ value: number,
32
+ from: string = 'ETH',
33
+ countryCode?: string
34
+ ): Promise<number> => {
35
+ console.log("to local");
36
+
37
+ const country = countryCodes[countryCode || 'US']
38
+ if (!country) {
39
+ throw new Error(
40
+ `Unable to find country name for country code: ${countryCode}.`
41
+ )
42
+ }
43
+
44
+ const to = currencyCodes[country]
45
+ if (!to) {
46
+ throw new Error(`Unable to find currency code for country: ${country}.`)
47
+ }
48
+
49
+ const url = `https://min-api.cryptocompare.com/data/price?fsym=${from}&tsyms=${to}`
50
+ const response = await fetch(url)
51
+ const data = await response.json()
52
+
53
+ const convertedValue =
54
+ Math.round((value * data[to]) * 10000) / 10000
55
+ console.log("Converted value: ", convertedValue);
56
+
57
+ return convertedValue
58
+ }
59
+
60
+
61
+ export const convertLocalToCrypto = async (
62
+ value: number,
63
+ to: string = 'ETH',
64
+ countryCode: string
65
+ ): Promise<number> => {
66
+ console.log("to Crypto");
67
+
68
+ const country = countryCodes[countryCode || 'US']
69
+ if (!country) {
70
+ throw new Error(
71
+ `Unable to find country name for country code: ${countryCode}.`
72
+ )
73
+ }
74
+
75
+ const from = currencyCodes[country]
76
+ if (!from) {
77
+ throw new Error(`Unable to find currency code for country: ${country}.`)
78
+ }
79
+
80
+ const url = `https://min-api.cryptocompare.com/data/price?fsym=${from}&tsyms=${to}`
81
+ const response = await fetch(url)
82
+ const data = await response.json()
83
+ console.log(data);
84
+
85
+
86
+ const convertedValue =
87
+ Math.round((value / (1 / data[to]) + Number.EPSILON) * 10000) / 10000
88
+ console.log("Converted value: ", convertedValue);
26
89
 
27
90
  return convertedValue
28
91
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { convert } from './currency'
1
+ export { convertLocalToCrypto, convertCryptoToLocal, convert } from './currency'
2
2
 
3
3
  export { KeychainAdapter, PortalApi, Storage } from './definitions'
4
4
 
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class InvalidRpcConfigError extends Error {
4
- constructor() {
5
- super('[Portal] A valid RPC configuration was not provided.');
6
- // Set the prototype explicitly.
7
- Object.setPrototypeOf(this, InvalidRpcConfigError.prototype);
8
- }
9
- }
10
- exports.default = InvalidRpcConfigError;
@@ -1,26 +0,0 @@
1
- var __extends = (this && this.__extends) || (function () {
2
- var extendStatics = function (d, b) {
3
- extendStatics = Object.setPrototypeOf ||
4
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
- return extendStatics(d, b);
7
- };
8
- return function (d, b) {
9
- if (typeof b !== "function" && b !== null)
10
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
- extendStatics(d, b);
12
- function __() { this.constructor = d; }
13
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
- };
15
- })();
16
- var InvalidRpcConfigError = /** @class */ (function (_super) {
17
- __extends(InvalidRpcConfigError, _super);
18
- function InvalidRpcConfigError() {
19
- var _this = _super.call(this, '[Portal] A valid RPC configuration was not provided.') || this;
20
- // Set the prototype explicitly.
21
- Object.setPrototypeOf(_this, InvalidRpcConfigError.prototype);
22
- return _this;
23
- }
24
- return InvalidRpcConfigError;
25
- }(Error));
26
- export default InvalidRpcConfigError;