web3 improvements
This commit is contained in:
parent
6101352e25
commit
65ea8cdabf
194
src/App.js
194
src/App.js
@ -49,6 +49,7 @@ class App extends Component {
|
|||||||
swapExchange: swapExchangeContract,
|
swapExchange: swapExchangeContract,
|
||||||
swapToken: swapTokenContract,
|
swapToken: swapTokenContract,
|
||||||
factory: factoryContract,
|
factory: factoryContract,
|
||||||
|
blockTimestamp: 0,
|
||||||
ethBalance: 0,
|
ethBalance: 0,
|
||||||
tokenBalance: 0,
|
tokenBalance: 0,
|
||||||
tokenAllowance: null,
|
tokenAllowance: null,
|
||||||
@ -87,23 +88,26 @@ class App extends Component {
|
|||||||
componentDidMount(){
|
componentDidMount(){
|
||||||
this.getAccountInfo();
|
this.getAccountInfo();
|
||||||
this.getMarketInfo('output', 'UNI');
|
this.getMarketInfo('output', 'UNI');
|
||||||
// setInterval(this.helloWorld, 2000);
|
setInterval(this.getBlock, 3000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// helloWorld = () => {
|
getBlock = () => {
|
||||||
// console.log('Hello World')
|
localweb3.eth.getBlock('latest', (error, blockInfo) => {
|
||||||
// }
|
this.setState({blockTimestamp: blockInfo.timestamp})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
getAccountInfo = () => {
|
getAccountInfo = () => {
|
||||||
this.getEthBalance();
|
this.getEthBalance();
|
||||||
this.getTokenBalance();
|
this.getTokenBalance();
|
||||||
this.getAllowance();
|
this.getExchangeAllowance();
|
||||||
}
|
}
|
||||||
|
|
||||||
getMarketInfo = (type, symbol) => {
|
getMarketInfo = (type, symbol) => {
|
||||||
this.getInvarient(type, symbol);
|
var exchangeContract = this.symbolToExchangeContract(symbol);
|
||||||
this.getMarketEth(type, symbol);
|
this.getInvarient(type, exchangeContract);
|
||||||
this.getMarketTokens(type, symbol);
|
this.getMarketEth(type, exchangeContract);
|
||||||
|
this.getMarketTokens(type, exchangeContract);
|
||||||
}
|
}
|
||||||
|
|
||||||
getMetaMaskAddress = () => {
|
getMetaMaskAddress = () => {
|
||||||
@ -115,6 +119,30 @@ class App extends Component {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
symbolToTokenAddress = (symbol) => {
|
||||||
|
if(symbol === 'UNI') {
|
||||||
|
return this.state.uniTokenAddress;
|
||||||
|
} else if(symbol === 'SWAP') {
|
||||||
|
return this.state.swapTokenAddress;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
symbolToTokenContract = (symbol) => {
|
||||||
|
if(symbol === 'UNI') {
|
||||||
|
return this.state.uniToken;
|
||||||
|
} else if(symbol === 'SWAP') {
|
||||||
|
return this.state.swapToken;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
symbolToExchangeContract = (symbol) => {
|
||||||
|
if(symbol === 'UNI') {
|
||||||
|
return this.state.uniExchange;
|
||||||
|
} else if(symbol === 'SWAP') {
|
||||||
|
return this.state.swapExchange;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getEthBalance = () => {
|
getEthBalance = () => {
|
||||||
localweb3.eth.getBalance(this.state.currentMaskAddress, (error, balance) => {
|
localweb3.eth.getBalance(this.state.currentMaskAddress, (error, balance) => {
|
||||||
this.setState({ethBalance: balance});
|
this.setState({ethBalance: balance});
|
||||||
@ -123,16 +151,18 @@ class App extends Component {
|
|||||||
|
|
||||||
getTokenBalance = () => {
|
getTokenBalance = () => {
|
||||||
this.state.uniToken.methods.balanceOf(this.state.currentMaskAddress).call((error, balance) => {
|
this.state.uniToken.methods.balanceOf(this.state.currentMaskAddress).call((error, balance) => {
|
||||||
var amount = balance;
|
this.setState({tokenBalance: balance});
|
||||||
this.setState({tokenBalance: amount});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getAllowance = () => {
|
getExchangeAllowance = () => {
|
||||||
// this.state.uniToken.methods.allowance(this.state.currentMaskAddress, this.uniExchangeAddress).call().then(function(result, error){
|
this.state.uniToken.methods.allowance(this.state.currentMaskAddress, this.state.uniExchangeAddress).call().then((result, error) => {
|
||||||
// var amount = result
|
this.setState({tokenAllowance: result});
|
||||||
// this.setState({tokenAllowance: amount});
|
})
|
||||||
// })
|
}
|
||||||
|
|
||||||
|
approveAllowance = () => {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenToExchangeFactoryLookup = (tokenAddress) => {
|
tokenToExchangeFactoryLookup = (tokenAddress) => {
|
||||||
@ -161,77 +191,48 @@ class App extends Component {
|
|||||||
this.setState({networkMessage: 'Kovan testnet', connected: false, interaction: 'disconnected'});
|
this.setState({networkMessage: 'Kovan testnet', connected: false, interaction: 'disconnected'});
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
this.setState({networkMessage: 'an Unknown network', connected: false, interaction: 'disconnected'});
|
this.setState({networkMessage: 'an unknown network', connected: false, interaction: 'disconnected'});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
getInvarient = (type, symbol) => {
|
getInvarient = (type, exchange) => {
|
||||||
var exchange;
|
|
||||||
if(symbol === 'UNI') {
|
|
||||||
exchange = this.state.uniExchange;
|
|
||||||
} else if(symbol === 'SWAP') {
|
|
||||||
exchange = this.state.swapExchange;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === 'input') {
|
if (type === 'input') {
|
||||||
exchange.methods.invariant().call().then((result, error) => {
|
exchange.methods.invariant().call().then((result, error) => {this.setState({invariant1: result});
|
||||||
this.setState({invariant1: result});
|
|
||||||
// console.log("invariant1: " + result);
|
// console.log("invariant1: " + result);
|
||||||
})
|
})
|
||||||
} else if (type === 'output') {
|
} else if (type === 'output') {
|
||||||
exchange.methods.invariant().call().then((result, error) => {
|
exchange.methods.invariant().call().then((result, error) => {this.setState({invariant2: result});
|
||||||
this.setState({invariant2: result});
|
|
||||||
// console.log("invariant2: " + result);
|
// console.log("invariant2: " + result);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getMarketEth = (type, symbol) => {
|
getMarketEth = (type, exchange) => {
|
||||||
var exchange;
|
|
||||||
if(symbol === 'UNI') {
|
|
||||||
exchange = this.state.uniExchange;
|
|
||||||
} else if(symbol === 'SWAP') {
|
|
||||||
exchange = this.state.swapExchange;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === 'input') {
|
if (type === 'input') {
|
||||||
exchange.methods.ethInMarket().call().then((result, error) => {
|
exchange.methods.ethInMarket().call().then((result, error) => {this.setState({marketEth1: result});
|
||||||
this.setState({marketEth1: result});
|
|
||||||
// console.log("marketEth1: " + result);
|
// console.log("marketEth1: " + result);
|
||||||
})
|
})
|
||||||
} else if (type === 'output') {
|
} else if (type === 'output') {
|
||||||
exchange.methods.ethInMarket().call().then((result, error) => {
|
exchange.methods.ethInMarket().call().then((result, error) => {this.setState({marketEth2: result});
|
||||||
this.setState({marketEth2: result});
|
|
||||||
// console.log("marketEth2: " + result);
|
// console.log("marketEth2: " + result);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getMarketTokens = (type, symbol) => {
|
getMarketTokens = (type, exchange) => {
|
||||||
var exchange;
|
|
||||||
if(symbol === 'UNI') {
|
|
||||||
exchange = this.state.uniExchange;
|
|
||||||
} else if(symbol === 'SWAP') {
|
|
||||||
exchange = this.state.swapExchange;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === 'input') {
|
if (type === 'input') {
|
||||||
exchange.methods.tokensInMarket().call().then((result, error) => {
|
exchange.methods.tokensInMarket().call().then((result, error) => {this.setState({marketTokens1: result});
|
||||||
this.setState({marketTokens1: result});
|
|
||||||
// console.log("marketTokens1: " + result);
|
// console.log("marketTokens1: " + result);
|
||||||
})
|
})
|
||||||
} else if (type === 'output') {
|
} else if (type === 'output') {
|
||||||
exchange.methods.tokensInMarket().call().then((result, error) => {
|
exchange.methods.tokensInMarket().call().then((result, error) => {this.setState({marketTokens2: result});
|
||||||
this.setState({marketTokens2: result});
|
|
||||||
// console.log("marketTokens2: " + result);
|
// console.log("marketTokens2: " + result);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onSelectToken = (selected, type) => {
|
onSelectToken = (selected, type) => {
|
||||||
// console.log(selected)
|
|
||||||
// console.log(type)
|
|
||||||
this.setState({input: 0, output:0, rate:0, fee: 0, interaction: 'connected'})
|
this.setState({input: 0, output:0, rate:0, fee: 0, interaction: 'connected'})
|
||||||
var marketType = '';
|
var marketType = '';
|
||||||
if (type === 'input') {
|
if (type === 'input') {
|
||||||
@ -374,90 +375,49 @@ class App extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ethToTokenPurchase = () => {
|
ethToTokenPurchase = () => {
|
||||||
var exchange;
|
var exchange = this.symbolToExchangeContract(this.state.outputToken.value);
|
||||||
if(this.state.outputToken.value === 'UNI') {
|
|
||||||
exchange = this.state.uniExchange;
|
|
||||||
console.log('ETH to UNI purchase');
|
|
||||||
} else if(this.state.outputToken.value === 'SWAP') {
|
|
||||||
exchange = this.state.swapExchange;
|
|
||||||
console.log('ETH to SWAP purchase');
|
|
||||||
}
|
|
||||||
|
|
||||||
var minTokens = this.state.output/10**18;
|
var minTokens = this.state.output/10**18;
|
||||||
var minTokensInt = localweb3.utils.toWei(minTokens);
|
var minTokensInt = localweb3.utils.toWei(minTokens);
|
||||||
var ethSold = this.state.input;
|
var ethSold = this.state.input;
|
||||||
var weiSold = localweb3.utils.toWei(ethSold);
|
var weiSold = localweb3.utils.toWei(ethSold);
|
||||||
|
var timeout = this.state.blockTimestamp + 300; //current block time + 5mins
|
||||||
|
|
||||||
localweb3.eth.getBlock('latest', (error, blockInfo) => {
|
exchange.methods.ethToTokenSwap(minTokensInt, timeout).send({from: this.state.currentMaskAddress, value: weiSold})
|
||||||
var time = blockInfo.timestamp;
|
.on('transactionHash', console.log('Transaction Hash created'))
|
||||||
var timeout = time + 300; //current block time + 5mins
|
.on('receipt', (receipt) => {console.log(receipt)}) //Transaction Submitted to blockchain
|
||||||
|
.on('confirmation', (confirmationNumber, receipt) => {console.log("Block Confirmations: " + confirmationNumber)}) //Transaction Mined
|
||||||
exchange.methods.ethToTokenSwap(minTokensInt, timeout).send(
|
.on('error', console.error);
|
||||||
{from: this.state.currentMaskAddress, value: weiSold})
|
|
||||||
.on('transactionHash', console.log('Transaction Hash created'))
|
|
||||||
.on('receipt', (receipt) => {console.log(receipt)}) //Success
|
|
||||||
.on('confirmation', (confirmationNumber, receipt) => {console.log(confirmationNumber)}) //Transaction Mined - Not working?
|
|
||||||
.on('error', console.error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenToEthPurchase = () => {
|
tokenToEthPurchase = () => {
|
||||||
var exchange;
|
var exchange = this.symbolToExchangeContract(this.state.inputToken.value);
|
||||||
if(this.state.inputToken.value === 'UNI') {
|
|
||||||
exchange = this.state.uniExchange;
|
|
||||||
console.log('ETH to UNI purchase');
|
|
||||||
} else if(this.state.inputToken.value === 'SWAP') {
|
|
||||||
exchange = this.state.swapExchange;
|
|
||||||
console.log('ETH to SWAP purchase');
|
|
||||||
}
|
|
||||||
|
|
||||||
var minEth = this.state.output/10**18;
|
var minEth = this.state.output/10**18;
|
||||||
var minEthInt = localweb3.utils.toWei(minEth);
|
var minEthInt = localweb3.utils.toWei(minEth);
|
||||||
var tokensSold = this.state.input;
|
var tokensSold = this.state.input;
|
||||||
var tokensSoldInt = localweb3.utils.toWei(tokensSold);
|
var tokensSoldInt = localweb3.utils.toWei(tokensSold);
|
||||||
|
var timeout = this.state.blockTimestamp + 300; //current block time + 5mins
|
||||||
|
|
||||||
localweb3.eth.getBlock('latest', (error, blockInfo) => {
|
exchange.methods.tokenToEthSwap(tokensSoldInt, minEthInt, timeout).send({from: this.state.currentMaskAddress})
|
||||||
var time = blockInfo.timestamp;
|
.on('transactionHash', console.log('Transaction Hash created'))
|
||||||
var timeout = time + 300; //current block time + 5mins
|
.on('receipt', (receipt) => {console.log(receipt)}) //Transaction Submitted to blockchain
|
||||||
|
.on('confirmation', (confirmationNumber, receipt) => {console.log("Block Confirmations: " + confirmationNumber)}) //Transaction Mined
|
||||||
exchange.methods.tokenToEthSwap(tokensSoldInt, minEthInt, timeout).send(
|
.on('error', console.error);
|
||||||
{from: this.state.currentMaskAddress})
|
|
||||||
.on('transactionHash', console.log('Transaction Hash created'))
|
|
||||||
.on('receipt', (receipt) => {console.log(receipt)}) //Transaction submitted to blockchain
|
|
||||||
.on('confirmation', (confirmationNumber, receipt) => {console.log(confirmationNumber)}) //Transaction Mined - Not working?
|
|
||||||
.on('error', console.error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenToTokenPurchase = () => {
|
tokenToTokenPurchase = () => {
|
||||||
var exchange;
|
var exchange = this.symbolToExchangeContract(this.state.inputToken.value);
|
||||||
var tokenOutAddress;
|
var tokenOutAddress = this.symbolToTokenAddress(this.state.outputToken.value);
|
||||||
if(this.state.inputToken.value === 'UNI') {
|
|
||||||
exchange = this.state.uniExchange;
|
|
||||||
tokenOutAddress = this.state.swapTokenAddress;
|
|
||||||
console.log('UNI to SWAP purchase');
|
|
||||||
} else if(this.state.inputToken.value === 'SWAP') {
|
|
||||||
exchange = this.state.swapExchange;
|
|
||||||
tokenOutAddress = this.state.uniTokenAddress;
|
|
||||||
console.log('SWAP to UNI purchase');
|
|
||||||
}
|
|
||||||
|
|
||||||
var minTokens = this.state.output/10**18;
|
var minTokens = this.state.output/10**18;
|
||||||
var minTokensInt = localweb3.utils.toWei(minTokens);
|
var minTokensInt = localweb3.utils.toWei(minTokens);
|
||||||
var tokensSold = this.state.input;
|
var tokensSold = this.state.input;
|
||||||
var tokensSoldInt = localweb3.utils.toWei(tokensSold);
|
var tokensSoldInt = localweb3.utils.toWei(tokensSold);
|
||||||
|
var timeout = this.state.blockTimestamp + 300; //current block time + 5mins
|
||||||
|
|
||||||
localweb3.eth.getBlock('latest', (error, blockInfo) => {
|
exchange.methods.tokenToTokenSwap(tokenOutAddress, tokensSoldInt, minTokensInt, timeout).send({from: this.state.currentMaskAddress})
|
||||||
var time = blockInfo.timestamp;
|
.on('transactionHash', console.log('Transaction Hash created'))
|
||||||
var timeout = time + 300; //current block time + 5mins
|
.on('receipt', (receipt) => {console.log(receipt)}) //Transaction Submitted to blockchain
|
||||||
|
.on('confirmation', (confirmationNumber, receipt) => {console.log("Block Confirmations: " + confirmationNumber)}) //Transaction Mined
|
||||||
exchange.methods.tokenToTokenSwap(tokenOutAddress, tokensSoldInt, minTokensInt, timeout).send(
|
.on('error', console.error);
|
||||||
{from: this.state.currentMaskAddress})
|
|
||||||
.on('transactionHash', console.log('Transaction Hash created'))
|
|
||||||
.on('receipt', (receipt) => {console.log(receipt)}) //Success
|
|
||||||
.on('confirmation', (confirmationNumber, receipt) => {console.log(confirmationNumber)}) //Transaction Mined - Not working?
|
|
||||||
.on('error', console.error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
Loading…
Reference in New Issue
Block a user