Commit 9f2d5ee9 authored by Uciel's avatar Uciel

globalWeb3 redux logic ready

parent fbd78a5a
...@@ -84,6 +84,7 @@ class App extends Component { ...@@ -84,6 +84,7 @@ class App extends Component {
// lets start with what's in the componentDidMount // lets start with what's in the componentDidMount
componentWillMount(){ componentWillMount(){
this.props.putWeb3InStore(localweb3);
console.log('props', this.props) console.log('props', this.props)
if(localweb3 === 'undefined') { if(localweb3 === 'undefined') {
this.props.web3ConnectionUnsuccessful(); this.props.web3ConnectionUnsuccessful();
...@@ -225,12 +226,6 @@ class App extends Component { ...@@ -225,12 +226,6 @@ class App extends Component {
}) })
} }
getBlock = () => {
localweb3.eth.getBlock('latest', (error, blockInfo) => {
this.props.setBlockTimestamp(blockInfo.timestamp);
});
}
symbolToTokenAddress = (symbol) => { symbolToTokenAddress = (symbol) => {
if(symbol === 'UNI') { if(symbol === 'UNI') {
return this.props.web3Store.exchangeAddresses.UNI; return this.props.web3Store.exchangeAddresses.UNI;
...@@ -469,6 +464,7 @@ class App extends Component { ...@@ -469,6 +464,7 @@ class App extends Component {
} }
getExchangeRate = (input) => { getExchangeRate = (input) => {
console.log(this.props.web3Store.globalWeb3, 'this better be defined')
if (this.props.web3Store.exchangeType === 'ETH to Token') { if (this.props.web3Store.exchangeType === 'ETH to Token') {
console.log('Getting Rate: ETH to ' + this.props.exchange.outputToken.value); console.log('Getting Rate: ETH to ' + this.props.exchange.outputToken.value);
this.ethToTokenRate(input); this.ethToTokenRate(input);
...@@ -481,16 +477,6 @@ class App extends Component { ...@@ -481,16 +477,6 @@ class App extends Component {
} }
} }
purchaseTokens = () => {
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();
}
}
ethToTokenRate = (ethInput) => { ethToTokenRate = (ethInput) => {
var ethInMarket = +this.props.exchange.marketEth2; var ethInMarket = +this.props.exchange.marketEth2;
var tokensInMarket = +this.props.exchange.marketTokens2; var tokensInMarket = +this.props.exchange.marketTokens2;
...@@ -552,16 +538,26 @@ class App extends Component { ...@@ -552,16 +538,26 @@ class App extends Component {
this.props.setExchangeOutputValue(adjustedTokensOut); this.props.setExchangeOutputValue(adjustedTokensOut);
} }
purchaseTokens = async () => {
await this.props.setBlockTimestamp(this.props.web3Store.globalWeb3);
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();
}
}
// YOU ARE HERE NOW // YOU ARE HERE NOW
ethToTokenPurchase = async () => { ethToTokenPurchase = () => {
await this.getBlock();
var exchange = this.symbolToExchangeContract(this.props.exchange.outputToken.value); var exchange = this.symbolToExchangeContract(this.props.exchange.outputToken.value);
var minTokens = (this.props.exchange.outputValue/10**18).toString(); var minTokens = (this.props.exchange.outputValue/10**18).toString();
var minTokensInt = localweb3.utils.toWei(minTokens); var minTokensInt = localweb3.utils.toWei(minTokens);
var ethSold = this.props.exchange.inputValue; var ethSold = this.props.exchange.inputValue;
var weiSold = localweb3.utils.toWei(ethSold); var weiSold = localweb3.utils.toWei(ethSold);
var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins
console.log(minTokensInt, weiSold, timeout); // console.log(minTokensInt, weiSold, timeout);
exchange.methods.ethToTokenSwap(minTokensInt, timeout).send({from: this.props.web3Store.currentMaskAddress, value: weiSold}) exchange.methods.ethToTokenSwap(minTokensInt, timeout).send({from: this.props.web3Store.currentMaskAddress, value: weiSold})
.on('transactionHash', (result) => { .on('transactionHash', (result) => {
...@@ -590,8 +586,7 @@ class App extends Component { ...@@ -590,8 +586,7 @@ class App extends Component {
// tokenToEth and EthToToken purchase functions are very similar structurally // tokenToEth and EthToToken purchase functions are very similar structurally
// maybe we can make this more DRY in refactor // maybe we can make this more DRY in refactor
tokenToEthPurchase = async () => { tokenToEthPurchase = () => {
await this.getBlock();
var exchange = this.symbolToExchangeContract(this.props.exchange.inputToken.value); var exchange = this.symbolToExchangeContract(this.props.exchange.inputToken.value);
var minEth = (this.props.exchange.outputValue/10**18).toString(); var minEth = (this.props.exchange.outputValue/10**18).toString();
var minEthInt = localweb3.utils.toWei(minEth); var minEthInt = localweb3.utils.toWei(minEth);
...@@ -615,8 +610,7 @@ class App extends Component { ...@@ -615,8 +610,7 @@ class App extends Component {
.on('error', console.error); .on('error', console.error);
} }
tokenToTokenPurchase = async () => { tokenToTokenPurchase = () => {
await this.getBlock();
var exchange = this.symbolToExchangeContract(this.props.exchange.inputToken.value); var exchange = this.symbolToExchangeContract(this.props.exchange.inputToken.value);
var tokenOutAddress = this.symbolToTokenAddress(this.props.exchange.outputToken.value); var tokenOutAddress = this.symbolToTokenAddress(this.props.exchange.outputToken.value);
var minTokens = (this.props.exchange.outputValue/10**18).toString(); var minTokens = (this.props.exchange.outputValue/10**18).toString();
......
...@@ -55,10 +55,17 @@ export const setNetworkMessage = (networkMessage) => ({ ...@@ -55,10 +55,17 @@ export const setNetworkMessage = (networkMessage) => ({
networkMessage networkMessage
}); });
export const setBlockTimestamp = (timestamp) => ({ export const setBlockTimestamp = (globalWeb3) => {
type: SET_BLOCK_TIMESTAMP, return async (dispatch, getState) => {
timestamp await globalWeb3.eth.getBlock('latest', (error, blockInfo) => {
}); console.log(blockInfo.timestamp, 'BLOCKTIMESTAMP');
dispatch({
type: SET_BLOCK_TIMESTAMP,
timestamp: blockInfo.timestamp
})
});
}
}
export const setExchangeType = (exchangeType) => ({ export const setExchangeType = (exchangeType) => ({
type: SET_EXCHANGE_TYPE, type: SET_EXCHANGE_TYPE,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment