Skip to content

Commit 60e87c6

Browse files
authored
Merge pull request #725 from Adamant-im/feat/fee-estimate-in-usd
feat: estimate currency commission in tx details
2 parents a65b73b + c1b2327 commit 60e87c6

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

src/components/transactions/TransactionTemplate.vue

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@
8585
<v-divider />
8686

8787
<TransactionListItem :title="t('transaction.commission')">
88-
{{
89-
typeof fee === 'number' ? formatAmount(fee) + ` ${feeCrypto ?? crypto}` : placeholder
90-
}}
88+
{{ calculatedFee }}
9189
</TransactionListItem>
9290

9391
<v-divider />
@@ -279,7 +277,7 @@ export default defineComponent({
279277
if (!transaction.value) return
280278
281279
return store.getters['rate/historyRate'](
282-
timestampInSec(props.crypto, transaction.value.timestamp),
280+
calculatedTimestampInSec.value,
283281
transaction.value.amount,
284282
props.crypto
285283
)
@@ -290,6 +288,39 @@ export default defineComponent({
290288
return store.getters['rate/rate'](transaction.value.amount, props.crypto)
291289
})
292290
291+
const calculatedTimestampInSec = computed(() => {
292+
if (!transaction.value) {
293+
return null;
294+
}
295+
296+
return timestampInSec(props.crypto, transaction.value.timestamp!)
297+
})
298+
299+
const calculatedFee = computed(() => {
300+
const commissionTokenLabel = (props.feeCrypto ?? props.crypto) as CryptoSymbol;
301+
302+
const { cryptoTransferDecimals, decimals } = CryptosInfo[commissionTokenLabel]
303+
304+
const tokenFee = typeof props.fee === 'number'
305+
? `${formatAmount(props.fee, cryptoTransferDecimals ?? decimals)} ${commissionTokenLabel}`
306+
: placeholder.value;
307+
308+
if (!props.fee || !calculatedTimestampInSec.value) return tokenFee;
309+
310+
311+
const commissionUsdAmount = store.getters['rate/historyRate'](
312+
calculatedTimestampInSec.value,
313+
props.fee,
314+
commissionTokenLabel,
315+
);
316+
317+
318+
if (!commissionUsdAmount) return tokenFee;
319+
320+
return tokenFee + ` ~${commissionUsdAmount}`;
321+
});
322+
323+
293324
const handleCopyToClipboard = (text?: string) => {
294325
if (!text) return
295326
@@ -318,24 +349,24 @@ export default defineComponent({
318349
}
319350
320351
const getHistoryRates = () => {
321-
if (!transaction.value) return
352+
if (!calculatedTimestampInSec.value) return
322353
323354
store.dispatch('rate/getHistoryRates', {
324-
timestamp: timestampInSec(props.crypto, transaction.value.timestamp!)
355+
timestamp: calculatedTimestampInSec.value
325356
})
326357
}
327358
328359
watch(
329-
() => props.transaction,
360+
calculatedTimestampInSec,
330361
() => {
331362
getHistoryRates()
332363
},
333364
{ immediate: true }
334365
)
335366
336-
const formatAmount = (amount: number) => {
367+
const formatAmount = (amount: number, decimals = CryptosInfo[props.crypto].decimals) => {
337368
return BigNumber(amount)
338-
.decimalPlaces(CryptosInfo[props.crypto].decimals, BigNumber.ROUND_DOWN)
369+
.decimalPlaces(decimals, BigNumber.ROUND_DOWN)
339370
.toFixed()
340371
}
341372
@@ -357,6 +388,7 @@ export default defineComponent({
357388
statusUpdatable,
358389
historyRate,
359390
rate,
391+
calculatedFee,
360392
formatAmount,
361393
mdiAlertOutline,
362394
mdiChevronRight,

src/store/modules/rate/rate-getters.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@ import { RootState } from '@/store/types'
33
import { RateState } from '@/store/modules/rate/types'
44

55
export const getters: GetterTree<RateState, RootState> = {
6+
ratesBySeconds: (state) => (timestamp: number) => {
7+
return state.historyRates[timestamp];
8+
},
9+
historyRateAmount: (state, { ratesBySeconds }) => (timestamp: number, amount: number, crypto: string, currency: string) => {
10+
return ratesBySeconds(timestamp)[`${crypto}/${currency}`] * amount
11+
},
612
historyRate:
7-
(state, getters, rootState) => (timestamp: number, amount: number, crypto: string) => {
13+
(state, { ratesBySeconds, historyRateAmount }, rootState) => (timestamp: number, amount: number, crypto: string) => {
814
let historyRate
915
const currentCurrency = rootState.options.currentRate
10-
const store = state.historyRates[timestamp]
11-
if (store) {
12-
historyRate = `${(store[`${crypto}/${currentCurrency}`] * amount).toFixed(
16+
17+
const calculatedRatesBySeconds = ratesBySeconds(timestamp);
18+
19+
if (calculatedRatesBySeconds) {
20+
historyRate = `${historyRateAmount(timestamp, amount, crypto, currentCurrency).toFixed(
1321
2
1422
)} ${currentCurrency}`
1523
} else {

0 commit comments

Comments
 (0)