Skip to content

Commit 379226c

Browse files
committed
Update @nimiq/utils for new FiatApi and make provider configurable
CoinGecko is becoming increasingly strict with unauthenticated access to their API without API key. For example, wallet.nimiq.com has already been blocked from unauthenticated access, thus the Wallet has been switched to CryptoCompare as fiat api provider, while hub.nimiq.com and keyguard.nimiq.com are not currently blocked. As the only vue-component that currently makes use of the FiatApi is PaymentInfoLine which is used only in the Hub, we don't have to switch the provider to CryptoCompare yet, and because otherwise we should also switch the provider in PaymentInfoLine in the Keyguard, which is a bit more work, we don't make the switch yet.
1 parent 8d6a63b commit 379226c

File tree

7 files changed

+36
-29
lines changed

7 files changed

+36
-29
lines changed

.storybook/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ crypto.createHash = (alg, opts) => {
99

1010
// Process source files and specified dependencies by babel to be able to use more modern js syntax than supported by
1111
// our Webpack version. Should be kept in sync with transpileDependencies in vue.config.js
12-
const transpileDependencies = [];
12+
const transpileDependencies = ['@nimiq/utils'];
1313
const babel = {
1414
loader: require.resolve('babel-loader'),
1515
// By default, options from babel.config.js are used. If you're encountering issues with some syntax not being

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"dependencies": {
2424
"@nimiq/iqons": "^1.6.0",
2525
"@nimiq/style": "^0.8.5",
26-
"@nimiq/utils": "^0.9.3",
26+
"@nimiq/utils": "^0.11.1",
2727
"big-integer": "^1.6.44",
2828
"input-format": "^0.2.8",
2929
"js-sha3": "^0.8.0",

src/components/AmountWithFee.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<script lang="ts">
1919
import { Component, Mixins, Prop, Watch } from 'vue-property-decorator';
20-
import { FiatApiSupportedFiatCurrency } from '@nimiq/utils';
20+
import { FiatCurrency } from '@nimiq/utils';
2121
import Amount from './Amount.vue';
2222
import AmountInput from './AmountInput.vue';
2323
import FiatAmount from './FiatAmount.vue';
@@ -38,7 +38,7 @@ export default class AmountWithFee extends Mixins(I18nMixin) {
3838
}) private value!: {amount: number, fee: number, isValid: boolean};
3939
@Prop({type: Number, default: 0}) private availableBalance!: number;
4040
@Prop(Number) private fiatAmount: number | null;
41-
@Prop(String) private fiatCurrency: FiatApiSupportedFiatCurrency | null;
41+
@Prop(String) private fiatCurrency: FiatCurrency | null;
4242
4343
private liveAmount: number = this.value.amount;
4444

src/components/PaymentInfoLine.vue

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
type BigInteger = import ('big-integer').BigInteger;
114114
115115
import { Component, Mixins, Prop, Watch } from 'vue-property-decorator';
116-
import { FiatApiSupportedFiatCurrency, FiatApiSupportedCryptoCurrency, getExchangeRates } from '@nimiq/utils';
116+
import { FiatCurrency, CryptoCurrency, Provider, getExchangeRates } from '@nimiq/utils';
117117
import Account from './Account.vue';
118118
import Timer from './Timer.vue';
119119
import Amount, { amountValidator } from './Amount.vue';
@@ -161,6 +161,11 @@ function fiatAmountInfoValidator(value: any) {
161161
},
162162
})
163163
class PaymentInfoLine extends Mixins(I18nMixin) {
164+
private static readonly FIAT_API_PROVIDER = Provider.CoinGecko;
165+
private static readonly FIAT_API_PROVIDER_URL = {
166+
[Provider.CoinGecko]: 'coingecko.com',
167+
[Provider.CryptoCompare]: 'cryptocompare.com',
168+
}[PaymentInfoLine.FIAT_API_PROVIDER];
164169
private static readonly REFERENCE_RATE_UPDATE_INTERVAL = 60000; // every minute
165170
private static readonly RATE_DEVIATION_THRESHOLD = .1;
166171
@@ -262,26 +267,30 @@ class PaymentInfoLine extends Mixins(I18nMixin) {
262267
|| (Math.abs(this.rateDeviation) < PaymentInfoLine.RATE_DEVIATION_THRESHOLD && !this.isBadRate)) {
263268
return null;
264269
}
270+
const translationVariables = {
271+
formattedRateDeviation: this.formattedRateDeviation,
272+
provider: PaymentInfoLine.FIAT_API_PROVIDER_URL,
273+
};
265274
if (this.rateDeviation < 0 && this.isBadRate) {
266275
// False discount
267276
return this.$t(
268277
'Your actual discount is approx. {formattedRateDeviation} compared '
269-
+ 'to the current market rate (coingecko.com).',
270-
{ formattedRateDeviation: this.formattedRateDeviation },
278+
+ 'to the current market rate ({provider}).',
279+
translationVariables,
271280
);
272281
}
273282
274283
if (this.rateDeviation > 0) {
275284
return this.$t(
276285
'You are paying approx. {formattedRateDeviation} more '
277-
+ 'than at the current market rate (coingecko.com).',
278-
{ formattedRateDeviation: this.formattedRateDeviation },
286+
+ 'than at the current market rate ({provider}).',
287+
translationVariables,
279288
);
280289
} else {
281290
return this.$t(
282291
'You are paying approx. {formattedRateDeviation} less '
283-
+ 'than at the current market rate (coingecko.com).',
284-
{ formattedRateDeviation: this.formattedRateDeviation },
292+
+ 'than at the current market rate ({provider}).',
293+
translationVariables,
285294
);
286295
}
287296
@@ -291,20 +300,18 @@ class PaymentInfoLine extends Mixins(I18nMixin) {
291300
@Watch('fiatAmount.currency')
292301
private async updateReferenceRate() {
293302
window.clearTimeout(this.referenceRateUpdateTimeout);
294-
const cryptoCurrency = this.cryptoAmount.currency.toLowerCase() as FiatApiSupportedCryptoCurrency;
295-
const fiatCurrency = this.fiatAmount
296-
? this.fiatAmount.currency.toLowerCase() as FiatApiSupportedFiatCurrency
297-
: null;
303+
const cryptoCurrency = this.cryptoAmount.currency.toLowerCase() as CryptoCurrency;
304+
const fiatCurrency = this.fiatAmount?.currency.toLowerCase() as FiatCurrency | undefined;
298305
if (!this.fiatAmount
299-
|| !Object.values(FiatApiSupportedFiatCurrency).includes(fiatCurrency)
300-
|| !Object.values(FiatApiSupportedCryptoCurrency).includes(cryptoCurrency)
306+
|| !Object.values(FiatCurrency).includes(fiatCurrency)
307+
|| !Object.values(CryptoCurrency).includes(cryptoCurrency)
301308
) {
302309
this.referenceRate = null;
303310
return;
304311
}
305312
306313
const { [cryptoCurrency]: { [fiatCurrency]: referenceRate }} =
307-
await getExchangeRates([cryptoCurrency], [fiatCurrency]);
314+
await getExchangeRates([cryptoCurrency], [fiatCurrency], PaymentInfoLine.FIAT_API_PROVIDER);
308315
this.referenceRate = referenceRate || null;
309316
310317
this.referenceRateUpdateTimeout = window.setTimeout(

src/i18n/en.po

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,16 @@ msgstr ""
168168
msgid "Vendor crypto markup"
169169
msgstr ""
170170

171-
#: src/components/PaymentInfoLine.vue:281
172-
msgid "You are paying approx. {formattedRateDeviation} less than at the current market rate (coingecko.com)."
171+
#: src/components/PaymentInfoLine.vue:291
172+
msgid "You are paying approx. {formattedRateDeviation} less than at the current market rate ({provider})."
173173
msgstr ""
174174

175-
#: src/components/PaymentInfoLine.vue:275
176-
msgid "You are paying approx. {formattedRateDeviation} more than at the current market rate (coingecko.com)."
175+
#: src/components/PaymentInfoLine.vue:285
176+
msgid "You are paying approx. {formattedRateDeviation} more than at the current market rate ({provider})."
177177
msgstr ""
178178

179-
#: src/components/PaymentInfoLine.vue:267
180-
msgid "Your actual discount is approx. {formattedRateDeviation} compared to the current market rate (coingecko.com)."
179+
#: src/components/PaymentInfoLine.vue:277
180+
msgid "Your actual discount is approx. {formattedRateDeviation} compared to the current market rate ({provider})."
181181
msgstr ""
182182

183183
#: src/components/QrScanner.vue:16

vue.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,5 @@ module.exports = {
101101
// Webpack version is too old to process some modern js syntax in the listed dependencies. Should be kept in sync with
102102
// transpileDependencies in storybook's webpack.config.js.
103103
// When changing to Webpack 5, some or all can probably be removed.
104-
transpileDependencies: [],
104+
transpileDependencies: ['@nimiq/utils'],
105105
};

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,10 +1264,10 @@
12641264
resolved "https://registry.yarnpkg.com/@nimiq/style/-/style-0.8.5.tgz#36a8bd8f58d55ee3e8ea5b249e51a116638e3b90"
12651265
integrity sha512-il2+hrodQ7iL4ARPwfGmGvrEYgtMHAoQSbcyapxEQEG7vDYj5r+ssuhKB+oYmdRUxA1/HvyGDnWG3XenKvQyIA==
12661266

1267-
"@nimiq/utils@^0.9.3":
1268-
version "0.9.3"
1269-
resolved "https://registry.yarnpkg.com/@nimiq/utils/-/utils-0.9.3.tgz#db402c8f42bb16dee8e5500c617cff18e1497960"
1270-
integrity sha512-g/9FpoNcJmU737TRQsNdU5/OGoXnc1UwunJBF/TGw4bx4cQ/pbYO7k0nsZ4RGUFn0jiy9xlxYH+yH1mvSY7+nQ==
1267+
"@nimiq/utils@^0.11.1":
1268+
version "0.11.1"
1269+
resolved "https://registry.yarnpkg.com/@nimiq/utils/-/utils-0.11.1.tgz#fe9c61fdead9b8e25a892a539b2b870cdd9cc51c"
1270+
integrity sha512-jwRInTkMPzyPsQN3iyf8s82mO/ChnbVtj5IzqXj9EO0PSOPd4Ix2ApIN/3NClfDfWo6HzE6NkSZOgry18p4jow==
12711271
dependencies:
12721272
big-integer "^1.6.44"
12731273

0 commit comments

Comments
 (0)