classic-ui/components/withdraw/WithdrawTotal.vue

135 lines
4.5 KiB
Vue
Raw Normal View History

2022-04-22 06:05:56 +03:00
<template>
<div class="field">
<div class="label">
{{ $t('total') }}
</div>
<div class="withdraw-data">
<div v-if="isTokenSelected" class="withdraw-data-item">
{{ $t('noteBalance') }}
<span>
{{ selectedStatistic.amount }}
{{ currency }}
</span>
</div>
<div v-if="withdrawType === 'relayer'" class="withdraw-data-item">
{{ $t('gasPrice') }}
2022-06-24 18:56:03 +03:00
<span>{{ gasPriceInGwei }} Gwei</span>
2022-04-22 06:05:56 +03:00
</div>
<div v-if="withdrawType === 'relayer'" class="withdraw-data-item">
{{ $t('networkFee') }}
<span data-test="label_network_fee"
>{{ toDecimals(withdrawalNetworkFee, 18, 6) }} {{ networkCurrency }}</span
>
2022-04-22 06:05:56 +03:00
</div>
<div v-if="withdrawType === 'relayer'" class="withdraw-data-item">
{{ $t('relayerFee') }}
2022-05-16 15:08:17 +03:00
<span data-test="label_relayer_fee">{{ toDecimals(relayerFee, null, 6) }} {{ currency }}</span>
2022-04-22 06:05:56 +03:00
</div>
<div v-if="withdrawType === 'relayer'" class="withdraw-data-item">
{{ $t('totalFee') }}
2022-05-16 15:08:17 +03:00
<span data-test="label_total_fee">{{ toDecimals(totalRelayerFee, null, 6) }} {{ currency }}</span>
2022-04-22 06:05:56 +03:00
</div>
<div v-if="isTokenSelected" class="withdraw-data-item">
{{ $t('ethPurchase', { currency: networkCurrency }) }}
<span>{{ toDecimals(ethToReceiveInToken, null, 6) }} {{ currency }}</span>
</div>
<hr v-if="withdrawType === 'relayer'" />
<div class="withdraw-data-item">
{{ $t('tokensToReceive') }}
2022-05-16 15:08:17 +03:00
<span data-test="label_tokens_to_receive">{{ total }} {{ currency }}</span>
2022-04-22 06:05:56 +03:00
</div>
<div v-if="isTokenSelected" class="withdraw-data-item">
<span class="is-alone">{{ ethToReceiveFromWei }} {{ networkCurrency }}</span>
</div>
</div>
</div>
</template>
<script>
import { mapState, mapGetters } from 'vuex'
import { decimalPlaces } from '@/utils'
const { fromWei, toBN } = require('web3-utils')
export default {
props: {
currency: {
type: String,
default: 'ETH'
},
withdrawType: {
type: String,
default: 'wallet'
},
ethToReceive: {
type: String,
default: '20000000000000000'
},
serviceFee: {
type: Number,
default: null
}
},
computed: {
...mapState('application', ['selectedStatistic']),
...mapState('fees', ['withdrawalNetworkFee', 'withdrawalFeeViaRelayer']),
2022-04-22 06:05:56 +03:00
...mapGetters('metamask', ['networkConfig', 'nativeCurrency']),
...mapGetters('metamask', {
networkCurrency: 'currency'
}),
...mapGetters('fees', ['gasPriceInGwei']),
2022-04-22 06:05:56 +03:00
...mapGetters('token', ['toDecimals', 'fromDecimals']),
...mapGetters('price', ['tokenRate']),
relayerFee() {
const { amount } = this.selectedStatistic
const total = toBN(this.fromDecimals(amount.toString()))
const fee = this.serviceFee || this.$store.state.relayer.selectedRelayer.tornadoServiceFee
const decimalsPoint = decimalPlaces(fee)
const roundDecimal = 10 ** decimalsPoint
const aroundFee = toBN(parseInt(fee * roundDecimal, 10))
const tornadoServiceFee = total.mul(toBN(aroundFee)).div(toBN(roundDecimal * 100))
return tornadoServiceFee
},
totalRelayerFee() {
const tornadoServiceFee = this.relayerFee
const { currency } = this.selectedStatistic
const { decimals } = this.networkConfig.tokens[currency]
const ethFee = this.withdrawalNetworkFee
2022-04-22 06:05:56 +03:00
if (currency === this.nativeCurrency) {
return ethFee.add(tornadoServiceFee)
}
const tokenFee = ethFee.mul(toBN(10 ** decimals)).div(toBN(this.tokenRate))
return tokenFee.add(tornadoServiceFee)
},
isTokenSelected() {
return (
this.withdrawType === 'relayer' &&
this.selectedStatistic.currency !== this.nativeCurrency &&
this.currency !== 'TORN'
)
},
ethToReceiveInToken() {
const { currency } = this.selectedStatistic
const { decimals } = this.networkConfig.tokens[currency]
const price = this.tokenRate
return toBN(this.ethToReceive)
.mul(toBN(10 ** decimals))
.div(toBN(price))
},
ethToReceiveFromWei() {
return fromWei(this.ethToReceive)
},
total() {
const { amount } = this.selectedStatistic
2022-04-22 06:05:56 +03:00
let total = toBN(this.fromDecimals(amount.toString()))
if (this.withdrawType === 'relayer') {
const relayerFee = this.withdrawalFeeViaRelayer
total = total.sub(relayerFee)
2022-04-22 06:05:56 +03:00
}
return this.toDecimals(total, null, 6)
}
}
}
</script>