Skip to content
This repository has been archived by the owner on Apr 28, 2023. It is now read-only.

Commit

Permalink
fix: fee calculation (#192)
Browse files Browse the repository at this point in the history
* fix: fee calculation

* fix: correct base amount on voila page for normal swaps
  • Loading branch information
michael1011 authored Jun 3, 2019
1 parent 2c7843a commit a38a159
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 44 deletions.
50 changes: 39 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"url": "git+https://github.com/BoltzExchange/boltz-frontend.git"
},
"dependencies": {
"axios": "^0.18.0",
"bignumber.js": "^8.1.1",
"bitcoinjs-lib": "^5.0.4",
"axios": "^0.19.0",
"bignumber.js": "^9.0.0",
"bitcoinjs-lib": "^5.0.5",
"boltz-core": "^0.0.7",
"eventsource": "^1.0.7",
"lodash.throttle": "^4.1.1",
Expand Down
17 changes: 14 additions & 3 deletions src/__test__/unit/reducers/SwapReducer.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import swapReducer, { initialState } from '../../../reducers/swapReducer';
import { toWholeCoins } from '../../../utils';
import * as actions from '../../../constants/actions';
import swapReducer, { initialState } from '../../../reducers/swapReducer';

describe('swap reducer', () => {
it('should return the initial state', () => {
Expand All @@ -23,18 +24,28 @@ describe('swap reducer', () => {
});

it(`should handle ${actions.SWAP_RESPONSE}`, () => {
const payload = {
response: {
someOther: 'data',
expectedAmount: 123456789,
},
};

expect(
swapReducer(
{},
{
payload,
type: actions.SWAP_RESPONSE,
payload: 'payload',
}
)
).toEqual({
retry: true,
isFetching: false,
swapResponse: 'payload',
swapInfo: {
baseAmount: toWholeCoins(payload.response.expectedAmount),
},
swapResponse: payload,
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/actions/reverseActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export const startReverseSwap = (swapInfo, nextStage, timelockExpired) => {
dispatch(reverseSwapRequest());
axios
.post(url, {
amount,
pairId: pair.id,
invoiceAmount: amount,
orderSide: pair.orderSide,
claimPublicKey: keys.publicKey,
})
Expand Down
6 changes: 3 additions & 3 deletions src/components/swaptab/desktopswaptab.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ const DeskTopSwapTabContent = ({
}) => (
<View className={classes.wrapper}>
<View className={classes.stats}>
<InfoText title="Min amount" text={`${minAmount}`} />
<InfoText title="Max amount" text={`${maxAmount}`} />
<InfoText title="Fee" text={`${feeAmount}`} />
<InfoText title="Min amount" text={`${minAmount} ${base}`} />
<InfoText title="Max amount" text={`${maxAmount} ${base}`} />
<InfoText title="Fee" text={`${feeAmount} ${base}`} />
<InfoText title="Rate" text={`${rate}`} />
</View>
<View className={classes.options}>
Expand Down
14 changes: 7 additions & 7 deletions src/components/swaptab/mobileswaptab.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import PropTypes from 'prop-types';
import injectSheet from 'react-jss';
import SwapTabWrapper from './swaptabwrapper';
import { MdCompareArrows } from 'react-icons/md';
import View from '../view';
import Text, { InfoText } from '../text';
import Input from '../input';
import DropDown from '../dropdown';
import { MdCompareArrows } from 'react-icons/md';
import Controls from '../controls';
import DropDown from '../dropdown';
import Text, { InfoText } from '../text';
import SwapTabWrapper from './swaptabwrapper';

const MobileSwapTabContent = ({
classes,
Expand All @@ -34,9 +34,9 @@ const MobileSwapTabContent = ({
}) => (
<View className={classes.wrapper}>
<View className={classes.info}>
<InfoText title="Min amount" text={`${minAmount}`} />
<InfoText title="Max amount" text={`${maxAmount}`} />
<InfoText title="Fee" text={`${feeAmount}`} />
<InfoText title="Min amount" text={`${minAmount} ${base}`} />
<InfoText title="Max amount" text={`${maxAmount} ${base}`} />
<InfoText title="Fee" text={`${feeAmount} ${base}`} />
<InfoText title="Rate" text={`${rate}`} />
</View>
<View className={classes.inputs}>
Expand Down
38 changes: 22 additions & 16 deletions src/components/swaptab/swaptabwrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ class SwapTabWrapper extends React.Component {
}
};

/**
* @returns the miner fee in the smallest denomination of the currency
*/
calculateMinerFee = () => {
const { minerFees } = this.props.fees;

Expand All @@ -180,20 +183,24 @@ class SwapTabWrapper extends React.Component {

/**
* @param { BigNumber } baseAmount
* @param { BigNumber } rate
*/
calculateFee = baseAmount => {
calculateFee = (baseAmount, rate) => {
const feePercentage = new BigNumber(this.state.feePercentage);

const percentageFee = feePercentage.times(baseAmount);
const minerFee = new BigNumber(this.calculateMinerFee()).dividedBy(
decimals
);

let fee = percentageFee.plus(minerFee);
if (isNaN(fee.toNumber())) {
fee = new BigNumber('0');
let minerFee = new BigNumber(this.calculateMinerFee()).dividedBy(decimals);

if (this.baseAsset.isLightning) {
minerFee = minerFee.times(new BigNumber(1).dividedBy(rate));
}

if (isNaN(percentageFee.toNumber())) {
return new BigNumber(0);
}
return fee;

return percentageFee.plus(minerFee);
};

/**
Expand Down Expand Up @@ -223,11 +230,11 @@ class SwapTabWrapper extends React.Component {
const rate = new BigNumber(this.state.rate.rate);

const newBase = amount.dividedBy(rate);
const fee = this.calculateFee(newBase);
const fee = this.calculateFee(newBase, rate);

const newBaseWithFee = fee.plus(newBase);

const inputError = !this.checkBaseAmount(newBaseWithFee);

this.setState({
quoteAmount: amount,
baseAmount: new BigNumber(newBaseWithFee.toFixed(8)),
Expand All @@ -239,24 +246,23 @@ class SwapTabWrapper extends React.Component {

updateQuoteAmount = baseAmount => {
if (!this.state.rate) return;

const amount = new BigNumber(baseAmount.toString());
const rate = new BigNumber(this.state.rate.rate);
const { orderSide } = this.state.rate;
let fee = this.calculateFee(amount);

if (orderSide === 'sell') {
fee = fee.times(rate);
}
let fee = this.calculateFee(amount, rate);

const quote = amount
.times(rate)
.minus(fee)
.minus(fee.times(rate))
.toFixed(8);

let newQuote = new BigNumber(quote);

if (newQuote.isLessThanOrEqualTo(0)) {
newQuote = new BigNumber('0');
}

const inputError = !this.checkBaseAmount(amount);
this.setState({
quoteAmount: newQuote,
Expand Down
5 changes: 5 additions & 0 deletions src/reducers/swapReducer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as actionTypes from '../constants/actions';
import { toWholeCoins } from '../utils';

export const initialState = {
retry: true,
Expand Down Expand Up @@ -36,6 +37,10 @@ const reducer = (state = initialState, action) => {
...state,
isFetching: false,
retry: true,
swapInfo: {
...state.swapInfo,
baseAmount: toWholeCoins(action.payload.response.expectedAmount),
},
swapResponse: action.payload,
};

Expand Down

0 comments on commit a38a159

Please sign in to comment.