Skip to content

Commit b256e56

Browse files
committed
Update @nimiq/utils for new FiatApi and make provider customizable
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 make the provider customizable in PyamentInfoLine, but don't have to switch the default to CryptoCompare yet.
1 parent 8d6a63b commit b256e56

File tree

7 files changed

+43
-29
lines changed

7 files changed

+43
-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: 28 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 as FiatApiProvider, getExchangeRates } from '@nimiq/utils';
117117
import Account from './Account.vue';
118118
import Timer from './Timer.vue';
119119
import Amount, { amountValidator } from './Amount.vue';
@@ -134,6 +134,12 @@ interface FiatAmountInfo {
134134
currency: string;
135135
}
136136
137+
// As Record, such that ts will warn us if the url for a provider is missing.
138+
const FIAT_API_PROVIDER_URLS: Record<FiatApiProvider, string> = {
139+
[FiatApiProvider.CoinGecko]: 'coingecko.com',
140+
[FiatApiProvider.CryptoCompare]: 'cryptocompare.com',
141+
};
142+
137143
function cryptoAmountInfoValidator(value: any) {
138144
return 'amount' in value && 'currency' in value && 'decimals' in value
139145
&& amountValidator(value.amount)
@@ -166,6 +172,12 @@ class PaymentInfoLine extends Mixins(I18nMixin) {
166172
167173
@Prop({type: Object, required: true, validator: cryptoAmountInfoValidator}) public cryptoAmount!: CryptoAmountInfo;
168174
@Prop({type: Object, validator: fiatAmountInfoValidator}) public fiatAmount?: FiatAmountInfo;
175+
@Prop({
176+
type: String,
177+
validator: (value: any) => Object.values(FiatApiProvider).includes(value),
178+
default: FiatApiProvider.CoinGecko,
179+
})
180+
public fiatApiProvider!: FiatApiProvider;
169181
// Note that vendorMarkup and networkFee have no effect if fiatAmount is not set, as the tooltip in which they
170182
// appear is only visible when fiatAmount is set. As the fiatAmount was only introduced in the v2 checkout request
171183
// in the Hub, the tooltip and vendorMarkup and networkFee are thus never visible for v1 checkout requests. This
@@ -262,26 +274,30 @@ class PaymentInfoLine extends Mixins(I18nMixin) {
262274
|| (Math.abs(this.rateDeviation) < PaymentInfoLine.RATE_DEVIATION_THRESHOLD && !this.isBadRate)) {
263275
return null;
264276
}
277+
const translationVariables = {
278+
formattedRateDeviation: this.formattedRateDeviation,
279+
provider: FIAT_API_PROVIDER_URLS[this.fiatApiProvider],
280+
};
265281
if (this.rateDeviation < 0 && this.isBadRate) {
266282
// False discount
267283
return this.$t(
268284
'Your actual discount is approx. {formattedRateDeviation} compared '
269-
+ 'to the current market rate (coingecko.com).',
270-
{ formattedRateDeviation: this.formattedRateDeviation },
285+
+ 'to the current market rate ({provider}).',
286+
translationVariables,
271287
);
272288
}
273289
274290
if (this.rateDeviation > 0) {
275291
return this.$t(
276292
'You are paying approx. {formattedRateDeviation} more '
277-
+ 'than at the current market rate (coingecko.com).',
278-
{ formattedRateDeviation: this.formattedRateDeviation },
293+
+ 'than at the current market rate ({provider}).',
294+
translationVariables,
279295
);
280296
} else {
281297
return this.$t(
282298
'You are paying approx. {formattedRateDeviation} less '
283-
+ 'than at the current market rate (coingecko.com).',
284-
{ formattedRateDeviation: this.formattedRateDeviation },
299+
+ 'than at the current market rate ({provider}).',
300+
translationVariables,
285301
);
286302
}
287303
@@ -291,20 +307,18 @@ class PaymentInfoLine extends Mixins(I18nMixin) {
291307
@Watch('fiatAmount.currency')
292308
private async updateReferenceRate() {
293309
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;
310+
const cryptoCurrency = this.cryptoAmount.currency.toLowerCase() as CryptoCurrency;
311+
const fiatCurrency = this.fiatAmount?.currency.toLowerCase() as FiatCurrency | undefined;
298312
if (!this.fiatAmount
299-
|| !Object.values(FiatApiSupportedFiatCurrency).includes(fiatCurrency)
300-
|| !Object.values(FiatApiSupportedCryptoCurrency).includes(cryptoCurrency)
313+
|| !Object.values(FiatCurrency).includes(fiatCurrency)
314+
|| !Object.values(CryptoCurrency).includes(cryptoCurrency)
301315
) {
302316
this.referenceRate = null;
303317
return;
304318
}
305319
306320
const { [cryptoCurrency]: { [fiatCurrency]: referenceRate }} =
307-
await getExchangeRates([cryptoCurrency], [fiatCurrency]);
321+
await getExchangeRates([cryptoCurrency], [fiatCurrency], this.fiatApiProvider);
308322
this.referenceRate = referenceRate || null;
309323
310324
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:297
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:291
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:283
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)