2016-08-10 22:43:01 +02:00
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const inherits = require('util').inherits
|
2018-03-29 17:00:44 +02:00
|
|
|
const PropTypes = require('prop-types')
|
2018-01-12 20:48:18 +01:00
|
|
|
const Component = require('react').Component
|
2018-03-29 17:00:44 +02:00
|
|
|
const connect = require('react-redux').connect
|
2018-01-12 20:48:18 +01:00
|
|
|
const classnames = require('classnames')
|
|
|
|
const { qrcode } = require('qrcode-npm')
|
|
|
|
const { shapeShiftSubview, pairUpdate, buyWithShapeShift } = require('../actions')
|
|
|
|
const { isValidAddress } = require('../util')
|
|
|
|
const SimpleDropdown = require('./dropdowns/simple-dropdown')
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2018-08-28 05:58:40 +02:00
|
|
|
import Button from './button'
|
|
|
|
|
2016-11-11 19:26:12 +01:00
|
|
|
function mapStateToProps (state) {
|
2018-01-12 20:48:18 +01:00
|
|
|
const {
|
|
|
|
coinOptions,
|
|
|
|
tokenExchangeRates,
|
|
|
|
selectedAddress,
|
|
|
|
} = state.metamask
|
2018-03-05 18:53:00 +01:00
|
|
|
const { warning } = state.appState
|
2018-01-23 10:48:03 +01:00
|
|
|
|
2016-08-10 22:43:01 +02:00
|
|
|
return {
|
2018-01-12 20:48:18 +01:00
|
|
|
coinOptions,
|
|
|
|
tokenExchangeRates,
|
|
|
|
selectedAddress,
|
2018-03-05 18:53:00 +01:00
|
|
|
warning,
|
2016-08-10 22:43:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
function mapDispatchToProps (dispatch) {
|
|
|
|
return {
|
|
|
|
shapeShiftSubview: () => dispatch(shapeShiftSubview()),
|
|
|
|
pairUpdate: coin => dispatch(pairUpdate(coin)),
|
|
|
|
buyWithShapeShift: data => dispatch(buyWithShapeShift(data)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
ShapeshiftForm.contextTypes = {
|
|
|
|
t: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
module.exports = connect(mapStateToProps, mapDispatchToProps)(ShapeshiftForm)
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
inherits(ShapeshiftForm, Component)
|
2016-08-13 00:41:59 +02:00
|
|
|
function ShapeshiftForm () {
|
2018-01-12 20:48:18 +01:00
|
|
|
Component.call(this)
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
depositCoin: 'btc',
|
|
|
|
refundAddress: '',
|
|
|
|
showQrCode: false,
|
|
|
|
depositAddress: '',
|
|
|
|
errorMessage: '',
|
|
|
|
isLoading: false,
|
|
|
|
bought: false,
|
|
|
|
}
|
2016-08-10 22:43:01 +02:00
|
|
|
}
|
2016-08-25 23:19:09 +02:00
|
|
|
|
2018-04-28 20:41:46 +02:00
|
|
|
ShapeshiftForm.prototype.getCoinPair = function () {
|
|
|
|
return `${this.state.depositCoin.toUpperCase()}_ETH`
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
ShapeshiftForm.prototype.componentWillMount = function () {
|
|
|
|
this.props.shapeShiftSubview()
|
2016-08-13 00:41:59 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 18:03:46 +01:00
|
|
|
ShapeshiftForm.prototype.onCoinChange = function (coin) {
|
2018-01-12 20:48:18 +01:00
|
|
|
this.setState({
|
|
|
|
depositCoin: coin,
|
|
|
|
errorMessage: '',
|
|
|
|
})
|
|
|
|
this.props.pairUpdate(coin)
|
|
|
|
}
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
ShapeshiftForm.prototype.onBuyWithShapeShift = function () {
|
|
|
|
this.setState({
|
|
|
|
isLoading: true,
|
|
|
|
showQrCode: true,
|
|
|
|
})
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
const {
|
|
|
|
buyWithShapeShift,
|
|
|
|
selectedAddress: withdrawal,
|
|
|
|
} = this.props
|
|
|
|
const {
|
|
|
|
refundAddress: returnAddress,
|
|
|
|
depositCoin,
|
|
|
|
} = this.state
|
|
|
|
const pair = `${depositCoin}_eth`
|
|
|
|
const data = {
|
|
|
|
withdrawal,
|
|
|
|
pair,
|
|
|
|
returnAddress,
|
|
|
|
// Public api key
|
|
|
|
'apiKey': '803d1f5df2ed1b1476e4b9e6bcd089e34d8874595dda6a23b67d93c56ea9cc2445e98a6748b219b2b6ad654d9f075f1f1db139abfa93158c04e825db122c14b6',
|
|
|
|
}
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
if (isValidAddress(withdrawal)) {
|
|
|
|
buyWithShapeShift(data)
|
|
|
|
.then(d => this.setState({
|
|
|
|
showQrCode: true,
|
|
|
|
depositAddress: d.deposit,
|
|
|
|
isLoading: false,
|
|
|
|
}))
|
|
|
|
.catch(() => this.setState({
|
|
|
|
showQrCode: false,
|
2018-03-29 17:00:44 +02:00
|
|
|
errorMessage: this.context.t('invalidRequest'),
|
2018-01-12 20:48:18 +01:00
|
|
|
isLoading: false,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
ShapeshiftForm.prototype.renderMetadata = function (label, value) {
|
|
|
|
return h('div', {className: 'shapeshift-form__metadata-wrapper'}, [
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
h('div.shapeshift-form__metadata-label', {}, [
|
|
|
|
h('span', `${label}:`),
|
2016-08-10 22:43:01 +02:00
|
|
|
]),
|
2017-08-09 00:17:52 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
h('div.shapeshift-form__metadata-value', {}, [
|
|
|
|
h('span', value),
|
2016-08-10 22:43:01 +02:00
|
|
|
]),
|
|
|
|
|
2017-08-09 00:17:52 +02:00
|
|
|
])
|
|
|
|
}
|
2017-04-18 18:21:24 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
ShapeshiftForm.prototype.renderMarketInfo = function () {
|
|
|
|
const { tokenExchangeRates } = this.props
|
|
|
|
const {
|
|
|
|
limit,
|
|
|
|
rate,
|
|
|
|
minimum,
|
2018-04-28 20:41:46 +02:00
|
|
|
} = tokenExchangeRates[this.getCoinPair()] || {}
|
2018-01-12 20:48:18 +01:00
|
|
|
|
|
|
|
return h('div.shapeshift-form__metadata', {}, [
|
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
this.renderMetadata(this.context.t('status'), limit ? this.context.t('available') : this.context.t('unavailable')),
|
|
|
|
this.renderMetadata(this.context.t('limit'), limit),
|
|
|
|
this.renderMetadata(this.context.t('exchangeRate'), rate),
|
|
|
|
this.renderMetadata(this.context.t('min'), minimum),
|
2018-01-12 20:48:18 +01:00
|
|
|
|
2016-08-10 22:43:01 +02:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
ShapeshiftForm.prototype.renderQrCode = function () {
|
2018-03-05 18:03:46 +01:00
|
|
|
const { depositAddress, isLoading, depositCoin } = this.state
|
2018-01-12 20:48:18 +01:00
|
|
|
const qrImage = qrcode(4, 'M')
|
|
|
|
qrImage.addData(depositAddress)
|
|
|
|
qrImage.make()
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
return h('div.shapeshift-form', {}, [
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
h('div.shapeshift-form__deposit-instruction', [
|
2018-03-29 17:00:44 +02:00
|
|
|
this.context.t('depositCoin', [depositCoin.toUpperCase()]),
|
2018-01-12 20:48:18 +01:00
|
|
|
]),
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
h('div', depositAddress),
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
h('div.shapeshift-form__qr-code', [
|
|
|
|
isLoading
|
|
|
|
? h('img', {
|
|
|
|
src: 'images/loading.svg',
|
|
|
|
style: { width: '60px'},
|
|
|
|
})
|
|
|
|
: h('div', {
|
|
|
|
dangerouslySetInnerHTML: { __html: qrImage.createTableTag(4) },
|
|
|
|
}),
|
|
|
|
]),
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
this.renderMarketInfo(),
|
2016-08-10 22:43:01 +02:00
|
|
|
|
2016-08-13 00:41:59 +02:00
|
|
|
])
|
2016-08-10 22:43:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-12 20:48:18 +01:00
|
|
|
ShapeshiftForm.prototype.render = function () {
|
2018-03-05 18:53:00 +01:00
|
|
|
const { coinOptions, btnClass, warning } = this.props
|
2018-04-28 20:41:46 +02:00
|
|
|
const { errorMessage, showQrCode, depositAddress } = this.state
|
2018-01-12 20:48:18 +01:00
|
|
|
const { tokenExchangeRates } = this.props
|
2018-04-28 20:41:46 +02:00
|
|
|
const token = tokenExchangeRates[this.getCoinPair()]
|
2018-01-12 20:48:18 +01:00
|
|
|
|
|
|
|
return h('div.shapeshift-form-wrapper', [
|
|
|
|
showQrCode
|
|
|
|
? this.renderQrCode()
|
2018-06-21 19:34:24 +02:00
|
|
|
: h('div.modal-shapeshift-form', [
|
2018-01-12 20:48:18 +01:00
|
|
|
h('div.shapeshift-form__selectors', [
|
|
|
|
|
|
|
|
h('div.shapeshift-form__selector', [
|
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
h('div.shapeshift-form__selector-label', this.context.t('deposit')),
|
2018-01-12 20:48:18 +01:00
|
|
|
|
|
|
|
h(SimpleDropdown, {
|
|
|
|
selectedOption: this.state.depositCoin,
|
2018-03-05 18:03:46 +01:00
|
|
|
onSelect: (coin) => this.onCoinChange(coin),
|
2018-01-12 20:48:18 +01:00
|
|
|
options: Object.entries(coinOptions).map(([coin]) => ({
|
|
|
|
value: coin.toLowerCase(),
|
|
|
|
displayValue: coin,
|
|
|
|
})),
|
|
|
|
}),
|
|
|
|
|
|
|
|
]),
|
|
|
|
|
|
|
|
h('div.icon.shapeshift-form__caret', {
|
|
|
|
style: { backgroundImage: 'url(images/caret-right.svg)'},
|
|
|
|
}),
|
|
|
|
|
|
|
|
h('div.shapeshift-form__selector', [
|
|
|
|
|
|
|
|
h('div.shapeshift-form__selector-label', [
|
2018-03-29 17:00:44 +02:00
|
|
|
this.context.t('receive'),
|
2018-01-12 20:48:18 +01:00
|
|
|
]),
|
|
|
|
|
|
|
|
h('div.shapeshift-form__selector-input', ['ETH']),
|
|
|
|
|
|
|
|
]),
|
|
|
|
|
|
|
|
]),
|
|
|
|
|
2018-03-05 18:53:00 +01:00
|
|
|
warning && h('div.shapeshift-form__address-input-label', warning),
|
|
|
|
|
|
|
|
!warning && h('div', {
|
2018-01-12 20:48:18 +01:00
|
|
|
className: classnames('shapeshift-form__address-input-wrapper', {
|
|
|
|
'shapeshift-form__address-input-wrapper--error': errorMessage,
|
|
|
|
}),
|
|
|
|
}, [
|
|
|
|
|
|
|
|
h('div.shapeshift-form__address-input-label', [
|
2018-03-29 17:00:44 +02:00
|
|
|
this.context.t('refundAddress'),
|
2018-01-12 20:48:18 +01:00
|
|
|
]),
|
|
|
|
|
|
|
|
h('input.shapeshift-form__address-input', {
|
|
|
|
type: 'text',
|
|
|
|
onChange: e => this.setState({
|
|
|
|
refundAddress: e.target.value,
|
|
|
|
errorMessage: '',
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
|
|
|
|
h('divshapeshift-form__address-input-error-message', [errorMessage]),
|
|
|
|
]),
|
|
|
|
|
2018-03-05 18:53:00 +01:00
|
|
|
!warning && this.renderMarketInfo(),
|
2018-01-12 20:48:18 +01:00
|
|
|
|
|
|
|
]),
|
|
|
|
|
2018-08-28 05:58:40 +02:00
|
|
|
!depositAddress && h(Button, {
|
|
|
|
type: 'primary',
|
|
|
|
large: true,
|
|
|
|
className: `${btnClass} shapeshift-form__shapeshift-buy-btn`,
|
2018-01-12 20:48:18 +01:00
|
|
|
disabled: !token,
|
|
|
|
onClick: () => this.onBuyWithShapeShift(),
|
2018-03-29 17:00:44 +02:00
|
|
|
}, [this.context.t('buy')]),
|
2018-01-12 20:48:18 +01:00
|
|
|
|
|
|
|
])
|
2016-08-10 22:43:01 +02:00
|
|
|
}
|