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

Commit

Permalink
feat: get limits from middleware api
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Feb 12, 2019
1 parent 034f671 commit ca28e9b
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 38 deletions.
41 changes: 30 additions & 11 deletions package-lock.json

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

52 changes: 34 additions & 18 deletions src/components/swaptab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import DropDown from '../dropdown';
import Text, { InfoText } from '../text';
import { MIN, MAX } from '../../constants/fees';
import Controls from '../controls';
import { decimals } from '../../scripts/utils';

const styles = theme => ({
wrapper: {
Expand Down Expand Up @@ -90,7 +91,9 @@ class SwapTab extends React.Component {
inputError: false,
base: 'LTC',
quote: 'BTC ⚡',
baseAmount: MIN,
minAmount: 0,
maxAmount: 0,
baseAmount: 0.001,
quoteAmount: 0,
errorMessage: '',
};
Expand Down Expand Up @@ -121,19 +124,22 @@ class SwapTab extends React.Component {
return symbol;
};

getRate = () => {
return this.props.rates[
`${this.parseBoltSuffix(this.state.base, true)}/${this.parseBoltSuffix(
this.state.quote,
false
)}`
];
getSymbol = () => {
return `${this.parseBoltSuffix(
this.state.base,
true
)}/${this.parseBoltSuffix(this.state.quote, false)}`;
};

componentDidMount = () => {
const symbol = this.getSymbol();
const limits = this.props.limits[symbol];

this.setState(
{
rate: this.getRate(),
minAmount: limits.minimal,
maxAmount: limits.maximal,
rate: this.props.rates[symbol],
},
() => this.updateQuoteAmount(this.state.baseAmount)
);
Expand All @@ -145,7 +151,7 @@ class SwapTab extends React.Component {
prevState.base !== this.state.base ||
prevState.quote !== this.state.quote
) {
const rate = this.getRate();
const symbol = this.getSymbol();

// Swapping from chain to chain or from Lightning to Lightning is not supported right now
if (
Expand All @@ -169,9 +175,16 @@ class SwapTab extends React.Component {
return;
}

const rate = this.props.rates[symbol];
const limits = this.props.limits[symbol];

console.log(this.props);

this.setState(
{
rate,
minAmount: limits.minimal,
maxAmount: limits.maximal,
error: false,
},
() => this.updateQuoteAmount(this.state.baseAmount)
Expand Down Expand Up @@ -243,6 +256,8 @@ class SwapTab extends React.Component {
error,
base,
quote,
minAmount,
maxAmount,
baseAmount,
quoteAmount,
errorMessage,
Expand All @@ -252,18 +267,18 @@ class SwapTab extends React.Component {
return (
<View className={classes.wrapper}>
<View className={classes.stats}>
<InfoText title="Min amount:" text={`${MIN}`} />
<InfoText title="Max amount:" text={`${MAX}`} />
<InfoText title="Min amount:" text={`${minAmount}`} />
<InfoText title="Max amount:" text={`${maxAmount}`} />
<InfoText title="Fee:" text={'0'} />
<InfoText title="Rate:" text={`${this.parseRate(rates)}`} />
</View>
<View className={classes.options}>
<View className={classes.select}>
<Text text="You send:" className={classes.text} />
<Input
min={MIN}
max={MAX}
step={MIN}
min={minAmount}
max={maxAmount}
step={0.001}
error={inputError}
value={baseAmount}
onChange={e => this.updateQuoteAmount(e)}
Expand All @@ -277,9 +292,9 @@ class SwapTab extends React.Component {
<View className={classes.select}>
<Text text="You receive:" className={classes.text} />
<Input
min={0.00000001}
max={MAX}
step={0.00000001}
min={1 / decimals}
max={Number.MAX_SAFE_INTEGER}
step={1 / decimals}
error={inputError}
value={quoteAmount}
onChange={e => this.updateBaseAmount(e)}
Expand Down Expand Up @@ -310,6 +325,7 @@ SwapTab.propTypes = {
onPress: PropTypes.func,
rates: PropTypes.object.isRequired,
currencies: PropTypes.array.isRequired,
limits: PropTypes.object.isRequired,
};

export default injectSheet(styles)(SwapTab);
2 changes: 2 additions & 0 deletions src/constants/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const NAVIGATE = 'NAVIGATE';
export const SET_PAIRS = 'SET_PAIRS';
export const PAIRS_REQUEST = 'PAIRS_REQUEST';
export const PAIRS_RESPONSE = 'PAIRS_RESPONSE';
export const LIMITS_REQUEST = 'LIMITS_REQUEST';
export const LIMITS_RESPONSE = 'LIMITS_RESPONSE';

/**
* Swap actions
Expand Down
11 changes: 9 additions & 2 deletions src/scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../constants';

// Number satohis and litoshis in a whole coin
const decimals = 100000000;
export const decimals = 100000000;

/**
* Get a hex encoded string from a Buffer
Expand Down Expand Up @@ -60,11 +60,18 @@ export const splitPairId = pairId => {
};
};

/**
* Round a amount to 8 decimals and trims unnecessary zeros
*/
export const roundWholeCoins = coins => {
return Number(coins.toFixed(8));
};

/**
* Convert satoshis and litoshis to whole coins
*/
export const toWholeCoins = satoshis => {
return (satoshis / decimals).toFixed(8);
return roundWholeCoins(satoshis / decimals);
};

/**
Expand Down
2 changes: 2 additions & 0 deletions src/views/landingpage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as actions from './landingpageActions';
const mapStateToProps = state => ({
rates: state.landingpageReducer.rates,
currencies: state.landingpageReducer.currencies,
limits: state.landingpageReducer.limits,
});

const mapDispatchToProps = dispatch => ({
Expand All @@ -18,6 +19,7 @@ const mapDispatchToProps = dispatch => ({
initSwap: state => dispatch(initSwap(state)),
initReverseSwap: state => dispatch(initReverseSwap(state)),
getPairs: cb => dispatch(actions.getPairs(cb)),
getLimits: (rates, cb) => dispatch(actions.getLimits(rates, cb)),
});

export default connect(
Expand Down
10 changes: 8 additions & 2 deletions src/views/landingpage/landingpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class LandingPage extends React.Component {

componentDidMount() {
this.props.getPairs(() => {
this.forceUpdate();
this.props.getLimits(this.props.rates, () => {});
});

try {
Expand All @@ -91,6 +91,7 @@ class LandingPage extends React.Component {
initReverseSwap,
rates,
currencies,
limits,
} = this.props;

return (
Expand All @@ -115,7 +116,9 @@ class LandingPage extends React.Component {
onClose={this.toggleModal}
/>
</View>
{Object.keys(rates).length === 0 || currencies.length === 0 ? (
{Object.keys(rates).length === 0 ||
currencies.length === 0 ||
Object.keys(limits).length === 0 ? (
<View className={classes.loading}>
<img
src={boltz_logo}
Expand Down Expand Up @@ -152,6 +155,7 @@ class LandingPage extends React.Component {
}}
rates={rates}
currencies={currencies}
limits={limits}
/>
)}
</View>
Expand All @@ -169,8 +173,10 @@ LandingPage.propTypes = {
initSwap: PropTypes.func.isRequired,
initReverseSwap: PropTypes.func.isRequired,
getPairs: PropTypes.func.isRequired,
getLimits: PropTypes.func.isRequired,
rates: PropTypes.object.isRequired,
currencies: PropTypes.array.isRequired,
limits: PropTypes.object.isRequired,
};

export default injectSheet(styles)(LandingPage);
Loading

0 comments on commit ca28e9b

Please sign in to comment.