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'; class Purchase extends Component { purchaseTokens = async () => { await this.props.setBlockTimestamp(this.props.web3Store.web3); 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 = () => { // console.log('symbol: ', this.props.exchange.outputToken.value); var exchange = this.props.symbolToExchangeContract(this.props.exchange.outputToken.value); // console.log('exchange: ', exchange); var estimatedTokens = this.props.exchange.outputValue var minTokensInt = parseInt((estimatedTokens*0.9), 10).toString(); var ethSold = this.props.exchange.inputValue; var weiSold = this.props.web3Store.web3.utils.toWei(ethSold); var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins // console.log('minTokensInt: ', minTokensInt); // console.log('weiSold: ', weiSold); // console.log('timeout: ', timeout); exchange.methods.ethToTokenSwap(minTokensInt, timeout).send({from: this.props.web3Store.currentMaskAddress, value: weiSold}) .on('transactionHash', (result) => { // console.log('Transaction Hash created' // 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? // this.props.setExchangeInputValue(0); // this.props.setExchangeOutputValue(0); 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); // if(confirmationNumber === 1) { // this.getAccountInfo(); // } }) //Transaction Mined .on('error', console.error); } tokenToEthPurchase = () => { var exchange = this.props.symbolToExchangeContract(this.props.exchange.inputToken.value); var estimatedEth = this.props.exchange.outputValue var minEthInt = parseInt((estimatedEth*0.9), 10).toString(); var tokensSold = this.props.exchange.inputValue; // toWei needs to be replaced with *decimals var tokensSoldInt = this.props.web3Store.web3.utils.toWei(tokensSold); var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins 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 }); // this.props.setExchangeInputValue(0); // this.props.setExchangeOutputValue(0); 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); var estimatedTokens = this.props.exchange.outputValue; var minTokensInt = parseInt((estimatedTokens*0.9), 10).toString(); var tokensSold = this.props.exchange.inputValue; var tokensSoldInt = this.props.web3Store.web3.utils.toWei(tokensSold); var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins // console.log('tokenOutAddress', tokenOutAddress); // console.log('minTokensInt', minTokensInt); // console.log('tokensSoldInt', tokensSoldInt); // console.log('timeout', timeout); 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 }); // this.props.setExchangeInputValue(0); // this.props.setExchangeOutputValue(0); 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 ( {this.purchaseTokens()}}> {"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} ) } else { // eslint-disable-next-line return () } } } 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);