uniswap-interface-uncensored/src/components/Purchase.js

141 lines
6.5 KiB
JavaScript
Raw Normal View History

2018-03-22 04:06:18 +03:00
import React, { Component } from 'react';
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux';
import { setBlockTimestamp, setInteractionState } from '../ducks/web3';
import { setExchangeInputValue, setExchangeOutputValue } from '../ducks/exchange';
2018-03-22 04:06:18 +03:00
class Purchase extends Component {
purchaseTokens = async () => {
2018-03-23 21:24:45 +03:00
await this.props.setBlockTimestamp(this.props.web3Store.web3);
2018-03-22 04:06:18 +03:00
if (this.props.web3Store.exchangeType === 'ETH to Token') {
this.ethToTokenPurchase();
} else if (this.props.web3Store.exchangeType === 'Token to ETH') {
this.tokenToEthPurchase();
} else if (this.props.web3Store.exchangeType === 'Token to Token') {
this.tokenToTokenPurchase();
}
}
ethToTokenPurchase = () => {
2018-07-25 05:09:59 +03:00
// console.log('symbol: ', this.props.exchange.outputToken.value);
2018-03-22 04:06:18 +03:00
var exchange = this.props.symbolToExchangeContract(this.props.exchange.outputToken.value);
2018-07-25 05:09:59 +03:00
// console.log('exchange: ', exchange);
var estimatedTokens = this.props.exchange.outputValue
var minTokensInt = parseInt((estimatedTokens*0.9), 10).toString();
2018-03-22 04:06:18 +03:00
var ethSold = this.props.exchange.inputValue;
2018-03-23 21:24:45 +03:00
var weiSold = this.props.web3Store.web3.utils.toWei(ethSold);
2018-03-22 04:06:18 +03:00
var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins
2018-07-25 05:09:59 +03:00
// console.log('minTokensInt: ', minTokensInt);
// console.log('weiSold: ', weiSold);
// console.log('timeout: ', timeout);
2018-03-22 04:06:18 +03:00
exchange.methods.ethToTokenSwap(minTokensInt, timeout).send({from: this.props.web3Store.currentMaskAddress, value: weiSold})
.on('transactionHash', (result) => {
2018-03-25 18:29:25 +03:00
// console.log('Transaction Hash created'
2018-03-22 04:06:18 +03:00
// let transactions = this.state.transactions
// transactions.push(result);
// transactions is cookie stuff, we'll keep that in state
// this.setState({ transactions: transactions })
// any particular reason why there are initialized as 0, but get turned to empty strings after the transaction is over?
2018-03-25 04:16:07 +03:00
// this.props.setExchangeInputValue(0);
// this.props.setExchangeOutputValue(0);
2018-03-22 04:06:18 +03:00
this.props.setInteractionState('submitted');
// cookie.save('transactions', transactions, { path: '/' })
})
.on('receipt', (receipt) => {
console.log(receipt)
}) //Transaction Submitted to blockchain
.on('confirmation', (confirmationNumber, receipt) => {
2018-03-27 06:18:15 +03:00
console.log("Block Confirmations: " + confirmationNumber);
// if(confirmationNumber === 1) {
// this.getAccountInfo();
// }
2018-03-22 04:06:18 +03:00
}) //Transaction Mined
.on('error', console.error);
}
tokenToEthPurchase = () => {
var exchange = this.props.symbolToExchangeContract(this.props.exchange.inputToken.value);
2018-07-25 05:09:59 +03:00
var estimatedEth = this.props.exchange.outputValue
var minEthInt = parseInt((estimatedEth*0.9), 10).toString();
2018-03-22 04:06:18 +03:00
var tokensSold = this.props.exchange.inputValue;
2018-03-27 06:18:15 +03:00
// toWei needs to be replaced with *decimals
2018-03-23 21:24:45 +03:00
var tokensSoldInt = this.props.web3Store.web3.utils.toWei(tokensSold);
2018-03-22 04:06:18 +03:00
var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins
2018-03-25 18:29:25 +03:00
2018-03-22 04:06:18 +03:00
exchange.methods.tokenToEthSwap(tokensSoldInt, minEthInt, timeout).send({from: this.props.web3Store.currentMaskAddress})
.on('transactionHash', (result) => {
// console.log('Transaction Hash created')
// let transactions = this.state.transactions
// transactions.push(result)
// this.setState({ transactions: transactions });
2018-03-25 04:16:07 +03:00
// this.props.setExchangeInputValue(0);
// this.props.setExchangeOutputValue(0);
2018-03-22 04:06:18 +03:00
this.props.setInteractionState('submitted');
// cookie.save('transactions', transactions, { path: '/' })
})
.on('receipt', (receipt) => {console.log(receipt)}) //Transaction Submitted to blockchain
.on('confirmation', (confirmationNumber, receipt) => {console.log("Block Confirmations: " + confirmationNumber)}) //Transaction Mined
.on('error', console.error);
}
tokenToTokenPurchase = () => {
var exchange = this.props.symbolToExchangeContract(this.props.exchange.inputToken.value);
var tokenOutAddress = this.props.symbolToTokenAddress(this.props.exchange.outputToken.value);
2018-07-25 05:09:59 +03:00
var estimatedTokens = this.props.exchange.outputValue;
var minTokensInt = parseInt((estimatedTokens*0.9), 10).toString();
2018-03-22 04:06:18 +03:00
var tokensSold = this.props.exchange.inputValue;
2018-03-23 21:24:45 +03:00
var tokensSoldInt = this.props.web3Store.web3.utils.toWei(tokensSold);
2018-03-22 04:06:18 +03:00
var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins
2018-03-27 06:18:15 +03:00
// console.log('tokenOutAddress', tokenOutAddress);
// console.log('minTokensInt', minTokensInt);
// console.log('tokensSoldInt', tokensSoldInt);
// console.log('timeout', timeout);
2018-03-22 04:06:18 +03:00
exchange.methods.tokenToTokenSwap(tokenOutAddress, tokensSoldInt, minTokensInt, timeout).send({from: this.props.web3Store.currentMaskAddress})
.on('transactionHash', (result) => {
// console.log('Transaction Hash created')
// let transactions = this.state.transactions
// transactions.push(result)
// this.setState({ transactions: transactions });
2018-03-25 04:16:07 +03:00
// this.props.setExchangeInputValue(0);
// this.props.setExchangeOutputValue(0);
2018-03-22 04:06:18 +03:00
this.props.setInteractionState('submitted');
// cookie.save('transactions', transactions, { path: '/' })
})
.on('receipt', (receipt) => {console.log(receipt)}) //Transaction Submitted to blockchain
.on('confirmation', (confirmationNumber, receipt) => {console.log("Block Confirmations: " + confirmationNumber)}) //Transaction Mined
.on('error', console.error);
}
render() {
if (this.props.web3Store.interaction === 'input') {
return (
2018-05-27 08:34:24 +03:00
<a className="swap border pa2 blue-bg" role="button" onClick={() => {this.purchaseTokens()}}>
2018-04-03 04:08:27 +03:00
<b>{"I want to swap " + this.props.exchange.inputValue + " " + this.props.exchange.inputToken.value + " for " + (this.props.exchange.outputValue/10**18).toFixed(4) + " " + this.props.exchange.outputToken.value}</b>
2018-03-22 04:06:18 +03:00
</a>
)
} else {
2018-05-09 20:13:40 +03:00
// eslint-disable-next-line
2018-03-22 04:06:18 +03:00
return (<a className="swap grey-bg hidden border pa2"></a>)
}
}
}
const mapStateToProps = state => ({
global: state.global,
web3Store: state.web3Store,
exchange: state.exchange
})
const mapDispatchToProps = (dispatch) => {
return bindActionCreators({
setBlockTimestamp,
setExchangeInputValue,
setExchangeOutputValue,
setInteractionState
}, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(Purchase);