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

Commit

Permalink
Merge pull request #78 from BoltzExchange/joule-integration
Browse files Browse the repository at this point in the history
feat: Lightning Joule integration
  • Loading branch information
michael1011 authored Feb 6, 2019
2 parents 27b1542 + b4adfed commit de20e75
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 62 deletions.
48 changes: 34 additions & 14 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"react-scripts": "^2.1.3",
"redux": "^4.0.1",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0"
"redux-thunk": "^2.3.0",
"webln": "^0.1.0"
},
"devDependencies": {
"@babel/plugin-syntax-decorators": "^7.2.0",
Expand Down
3 changes: 3 additions & 0 deletions src/components/inputarea/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ const InputArea = ({
height,
width,
onChange,
value,
placeholder,
}) => (
<textarea
autoFocus={autoFocus}
value={value}
placeholder={placeholder}
className={classes.wrapper}
rows={height}
Expand All @@ -41,6 +43,7 @@ InputArea.propTypes = {
onChange: PropTypes.func.isRequired,
error: PropTypes.bool.isRequired,
autoFocus: PropTypes.bool,
value: PropTypes.string,
placeholder: PropTypes.string,
};

Expand Down
55 changes: 26 additions & 29 deletions src/components/swaptab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,45 +148,42 @@ class SwapTab extends React.Component {
const rate = this.getRate();

// Swapping from chain to chain or from Lightning to Lightning is not supported right now
if (this.baseAsset.isLightning !== this.quoteAsset.isLightning) {
this.setState(
{
rate,
error: false,
},
() => this.updateQuoteAmount(this.state.baseAmount)
);
} else {
if (
this.state.base === this.state.quote ||
(this.baseAsset.isLightning && this.quoteAsset.isLightning)
) {
this.setState({
rate: undefined,
error: true,
errorMessage: 'Choose a different asset',
});
return;
}

if (!this.baseAsset.isLightning && !this.quoteAsset.isLightning) {
this.setState({
rate: undefined,
error: true,
errorMessage: 'Coming soon',
});
return;
}

this.setState(
{
rate,
error: false,
},
() => this.updateQuoteAmount(this.state.baseAmount)
);
}
};

checkBaseAmount = baseAmount => {
return baseAmount <= MAX && baseAmount >= MIN;
};

checkValidPair = (quote, base) => {
if (quote === base) {
this.setState({
error: true,
errorMessage: 'Choose a different asset',
});
return;
}

const pair = `${quote}/${base}`;
if (pair === 'BTC/LTC' || pair === 'LTC/BTC') {
this.setState({
error: true,
errorMessage: 'Coming soon',
});
return;
}

updatePair = (quote, base) => {
this.setState({ base, quote, error: false, errorMessage: '' });
};

Expand Down Expand Up @@ -274,7 +271,7 @@ class SwapTab extends React.Component {
<DropDown
defaultValue={base}
fields={currencies}
onChange={e => this.checkValidPair(quote, e)}
onChange={e => this.updatePair(quote, e)}
/>
</View>
<View className={classes.select}>
Expand All @@ -290,7 +287,7 @@ class SwapTab extends React.Component {
<DropDown
defaultValue={quote}
fields={currencies}
onChange={e => this.checkValidPair(e, base)}
onChange={e => this.updatePair(e, base)}
/>
</View>
</View>
Expand Down
11 changes: 11 additions & 0 deletions src/views/landingpage/landingpage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import injectSheet from 'react-jss';
import { requestProvider } from 'webln';
import BackGround from '../../components/background';
import View from '../../components/view';
import { LinkButton } from '../../components/button';
Expand Down Expand Up @@ -65,6 +66,14 @@ class LandingPage extends React.Component {
this.props.getPairs(() => {
this.forceUpdate();
});

try {
requestProvider().then(provider => {
this.webln = provider;
});
} catch (error) {
console.log(`Could not enable webln: ${error}`);
}
}

toggleModal = () => {
Expand Down Expand Up @@ -127,13 +136,15 @@ class LandingPage extends React.Component {
initReverseSwap({
...state,
keys,
webln: this.webln,
});

goReverseSwap();
} else {
initSwap({
...state,
keys,
webln: this.webln,
});

goSwap();
Expand Down
1 change: 1 addition & 0 deletions src/views/reverse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { nav } from '../../action/index';
import ReverseSwap from './reverse';

const mapStateToProps = state => ({
webln: state.reverseSwapReducer.webln,
inSwapMode: state.reverseSwapReducer.inSwapMode,
isFetching: state.reverseSwapReducer.isFetching,
swapInfo: state.reverseSwapReducer.swapInfo,
Expand Down
3 changes: 3 additions & 0 deletions src/views/reverse/reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const ReverseSwap = ({
startReverseSwap,
completeSwap,
goHome,
webln,
nextStage,
swapInfo,
swapResponse,
Expand Down Expand Up @@ -60,6 +61,7 @@ const ReverseSwap = ({
<PayInvoice
asset={swapInfo.base}
invoice={swapResponse.invoice}
webln={webln}
/>
)}
/>
Expand Down Expand Up @@ -111,6 +113,7 @@ ReverseSwap.propTypes = {
history: PropTypes.object.isRequired,
isFetching: PropTypes.bool.isRequired,
goHome: PropTypes.func.isRequired,
webln: PropTypes.object,
swapInfo: PropTypes.object,
swapResponse: PropTypes.object,
completeSwap: PropTypes.func,
Expand Down
1 change: 1 addition & 0 deletions src/views/reverse/reverseActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { toSatoshi, getHexBuffer, getNetwork } from '../../scripts/utils';
export const initReverseSwap = state => ({
type: actionTypes.INIT_REVERSE_SWAP,
payload: {
webln: state.webln,
base: state.base,
quote: state.quote,
baseAmount: state.baseAmount,
Expand Down
2 changes: 2 additions & 0 deletions src/views/reverse/reverseReducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as actionTypes from '../../constants/actions';

const initalState = {
webln: null,
isFetching: false,
swapInfo: {
base: null,
Expand Down Expand Up @@ -33,6 +34,7 @@ const reducer = (state = initalState, action) => {
case actionTypes.INIT_REVERSE_SWAP:
return {
...state,
webln: action.payload.webln,
swapInfo: {
...state.swapInfo,
base: action.payload.base,
Expand Down
48 changes: 32 additions & 16 deletions src/views/reverse/steps/payInvoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,43 @@ const payInvoiceStyles = () => ({
},
});

const StyledPayInvoice = ({ classes, asset, invoice }) => (
<View className={classes.wrapper}>
<View className={classes.qrcode}>
<QrCode size={300} link={invoice} />
</View>
<View className={classes.info}>
<p className={classes.title}>Pay this {asset} Lightning invoice</p>
<p className={classes.invoice} id="copy">
{invoice}
</p>
<span className={classes.action} onClick={() => copyToClipBoard()}>
Copy
</span>
</View>
</View>
);
class StyledPayInvoice extends React.Component {
componentDidMount() {
const { invoice, webln } = this.props;

console.log(webln);
if (webln) {
webln.sendPayment(invoice);
}
}

render() {
const { classes, asset, invoice } = this.props;

return (
<View className={classes.wrapper}>
<View className={classes.qrcode}>
<QrCode size={300} link={invoice} />
</View>
<View className={classes.info}>
<p className={classes.title}>Pay this {asset} Lightning invoice</p>
<p className={classes.invoice} id="copy">
{invoice}
</p>
<span className={classes.action} onClick={() => copyToClipBoard()}>
Copy
</span>
</View>
</View>
);
}
}

StyledPayInvoice.propTypes = {
classes: PropTypes.object.isRequired,
asset: PropTypes.string.isRequired,
invoice: PropTypes.string.isRequired,
webln: PropTypes.object,
};

const PayInvoice = injectSheet(payInvoiceStyles)(StyledPayInvoice);
Expand Down
1 change: 1 addition & 0 deletions src/views/swap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { nav } from '../../action/index';
import Swap from './swap';

const mapStateToProps = state => ({
webln: state.swapReducer.webln,
inSwapMode: state.swapReducer.inSwapMode,
swapInfo: state.swapReducer.swapInfo,
swapResponse: state.swapReducer.swapResponse.response,
Expand Down
Loading

0 comments on commit de20e75

Please sign in to comment.