From ff6a4b9d58dd20c67c5e05f61b00a3ed445d10ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Mon, 29 Apr 2019 16:35:41 +0200 Subject: [PATCH] Remove Exchange --- src/App.js | 54 - src/components/Exchange.js | 2556 --------------------------------- src/components/MoreButtons.js | 35 +- 3 files changed, 1 insertion(+), 2644 deletions(-) delete mode 100644 src/components/Exchange.js diff --git a/src/App.js b/src/App.js index 91e1fd1d2..e00dcae0a 100644 --- a/src/App.js +++ b/src/App.js @@ -38,7 +38,6 @@ import Footer from './components/Footer'; import Loader from './components/Loader'; import burnerlogo from './burnerwallet.png'; import BurnWallet from './components/BurnWallet' -import Exchange from './components/Exchange' import Bottom from './components/Bottom'; import customRPCHint from './customRPCHint.png'; import namehash from 'eth-ens-namehash' @@ -1531,10 +1530,6 @@ export default class App extends Component { - - - - {badgeDisplay} ); - - case 'exchange': - return ( -
- {this.state.scannerOpen ? sendByScan : null} - - - - - -
- ); - case 'vendors': return (
diff --git a/src/components/Exchange.js b/src/components/Exchange.js deleted file mode 100644 index 36ca4b9ca..000000000 --- a/src/components/Exchange.js +++ /dev/null @@ -1,2556 +0,0 @@ -import React from 'react'; -import Ruler from "./Ruler"; -import Blockies from 'react-blockies'; -import { Scaler } from "dapparatus"; - -//import wyre from '../wyre.jpg'; -//import coinbase from '../coinbase.jpg'; -//import localeth from '../localeth.png'; - -import Web3 from 'web3'; -import axios from "axios" -import i18n from '../i18n'; - -import Wyre from '../services/wyre'; -import wyrelogo from '../wyre.png'; - -import InputRange from 'react-input-range'; -import 'react-input-range/lib/css/index.css'; - -import { - Flex, - Box, - Button, - OutlineButton, - Icon, - Input as RInput, - Field -} from 'rimble-ui' - -import { - Unspent, - Tx, - Input, - Output, - Outpoint, - OutpointJSON, - Type, - LeapTransaction, - helpers, - Exit, -} from 'leap-core'; - -import { fromRpcSig } from 'ethereumjs-util'; - -import { bi, add, divide } from 'jsbi-utils'; - -const BN = Web3.utils.BN - -const GASBOOSTPRICE = 0.25 - -const logoStyle = { - maxWidth:50, - maxHeight:50, -} - -const colStyle = { - textAlign:"center", - whiteSpace:"nowrap" -} - -const dendaiToxDaiEstimatedTime = 7000 -const xdaiToDaiEstimatedTime = 160000 -const daiToxDaiEstimatedTime = 330000 - -const sendDaiEstimatedTime = 160000 - -const exchangeEstimatedTime = 300000 - -const toXdaiBridgeAccount = "0x4aa42145Aa6Ebf72e164C9bBC74fbD3788045016" -const toDaiBridgeAccount = "0x7301cfa0e1756b71869e93d4e4dca5c7d0eb0aa6" - -const uniswapExchangeAccount = "0x09cabec1ead1c0ba254b09efb3ee13841712be14" -const uniswapContractObject = { - address:uniswapExchangeAccount, - abi:require("../contracts/Exchange.abi.js"), - blocknumber:6627956, -} - -let interval -let intervalLong -let metaReceiptTracker = {} - -export default class Exchange extends React.Component { - - constructor(props) { - super(props); - - let xdaiweb3 = this.props.xdaiweb3 - //let mainnetweb3 = new Web3("https://mainnet.infura.io/v3/e0ea6e73570246bbb3d4bd042c4b5dac") - let mainnetweb3 = props.mainnetweb3 - let pk = localStorage.getItem('metaPrivateKey') - let mainnetMetaAccount = false - let xdaiMetaAccount = false - let daiAddress = false - let xdaiAddress = false - if(pk&&pk!="0"){ - mainnetMetaAccount = mainnetweb3.eth.accounts.privateKeyToAccount(pk) - daiAddress = mainnetMetaAccount.address.toLowerCase(); - xdaiMetaAccount = xdaiweb3.eth.accounts.privateKeyToAccount(pk) - xdaiAddress = xdaiMetaAccount.address.toLowerCase(); - }else{ - daiAddress = this.props.address - xdaiAddress = this.props.address - } - - let dendaiContract - let vendorContract - console.log("NETWORK:",this.props.network) - if(props.ERC20TOKEN&&this.props.network=="xDai"){ - try{ - console.log("Loading "+props.ERC20TOKEN+" Contract...") - dendaiContract = new this.props.web3.eth.Contract(require("../contracts/"+props.ERC20TOKEN+".abi.js"),require("../contracts/"+props.ERC20TOKEN+".address.js")) - vendorContract = new this.props.web3.eth.Contract(require("../contracts/"+props.ERC20VENDOR+".abi.js"),require("../contracts/"+props.ERC20VENDOR+".address.js")) - console.log("SET vendorContract",vendorContract) - }catch(e){ - console.log("ERROR LOADING dendaiContract Contract",e) - } - } - - this.state = { - extraGasUpDisplay: "", - daiAddress: daiAddress, - xdaiAddress: xdaiAddress, - xdaiSendToAddress: "", - wyreBalance: 0, - denDaiBalance:0, - mainnetweb3: mainnetweb3, - mainnetMetaAccount: mainnetMetaAccount, - xdaiweb3:xdaiweb3, - xdaiMetaAccount: xdaiMetaAccount, - dendaiContract: dendaiContract, - vendorContract: vendorContract, - daiToXdaiMode: false, - ethToDaiMode: false, - loaderBarStatusText: i18n.t('loading'), - loaderBarStartTime:Date.now(), - loaderBarPercent: 2, - loaderBarColor: "#aaaaaa", - gwei: 5, - maxWithdrawlAmount: 0.00, - withdrawalExplanation: i18n.t('exchange.withdrawal_explanation'), - gettingGas:false, - wyreFundAmount: 5, - wyreWidgetOpen: false, - } - - setInterval(() => this.updatePendingExits(daiAddress, xdaiweb3), 5000); - } - - async maybeApprovePDai(amountWei) { - - const pDaiAllowance = await this.props.daiContract.methods.allowance( - this.state.daiAddress, - this.props.pdaiContract._address, - ).call({ from: this.state.daiAddress }) - - // Only trigger allowance dialogue when amount is more than allowance - if (new BN(pDaiAllowance).lt(new BN(amountWei))) { - this.setState({ - loaderBarColor:"#f5eb4a", - loaderBarStatusText: "Approving MNY amount for Plasma bridge" - }) - - const metaMaskDaiContract = new this.props.web3.eth.Contract(this.props.daiContract._jsonInterface,this.props.daiContract._address) - - const receipt = await this.props.pTx( - metaMaskDaiContract.methods.approve(this.props.pdaiContract._address, amountWei), - 150000, 0, 0, - ); - - console.log(receipt); - return receipt; - } - } - - updatePendingExits(daiAddress, xdaiweb3) { - const account = daiAddress; - const tokenAddr = this.props.pdaiContract._address; - - xdaiweb3.getColor(tokenAddr) - .then(color => { - return fetch( - `https://yxygzjw6s4.execute-api.eu-west-1.amazonaws.com/testnet/exits/${account}/${color}`, - { method: "GET", mode: "cors" } - ); - }) - .then(response => response.json()) - .then(rsp => { - console.log(rsp); - const pendingValue = rsp.reduce((sum, v) => add(sum, bi(v.value)), bi(0)); - const pendingTokens = parseInt(String(divide(pendingValue, bi(10 ** 16)))) / 100; - const pendingMsg = "Pending exits of " + pendingTokens.toString() + " MNY"; - this.setState({ - pendingMsg - }); - }); - }; - - - updateState = (key, value) => { - this.setState({ [key]: value },()=>{ - this.setState({ canSendDai: this.canSendDai(), canSendEth: this.canSendEth(), canSendXdai: this.canSendXdai() }) - }); - }; - async componentDidMount(){ - this.setState({ canSendDai: this.canSendDai(), canSendEth: this.canSendEth(), canSendXdai: this.canSendXdai() }) - interval = setInterval(this.poll.bind(this),1500) - setTimeout(this.poll.bind(this),250) - } - async getWrappedDaiBalance() { - // not a sundai, return immediatelly - if (this.state.notSundai) return true; - - const rootPdai = new this.state.mainnetweb3.eth.Contract( - require("../contracts/SunDai.abi.js"), - this.props.pdaiContract._address - ); - - try { - const daiBalance = await rootPdai.methods.daiBalance(this.state.daiAddress).call(); - return parseInt(daiBalance); - } catch (e) { - // no daiBalance function = not a sundai contract, so skipping this check - if (e.message.indexOf('Returned values aren\'t valid') >= 0) { - this.setState({ notSundai: true }); - return 0; - } - throw e; - } - } - async poll(){ - let { vendorContract, dendaiContract, mainnetweb3, xdaiweb3, xdaiAddress} = this.state - /*let { daiContract } = this.props - if(daiContract){ - let daiBalance = await daiContract.methods.balanceOf(this.state.daiAddress).call() - daiBalance = mainnetweb3.utils.fromWei(daiBalance,"ether") - if(daiBalance!=this.state.daiBalance){ - this.setState({daiBalance}) - } - }*/ - - - if(this.state.gettingGas){ - if(this.state.ethBalanceShouldBe){ - console.log("ethBalanceShouldBe:",parseFloat(this.state.ethBalanceShouldBe)," needs to be less than ",parseFloat(this.props.ethBalance)) - if(parseFloat(this.state.ethBalanceShouldBe)0 && parseFloat(this.props.ethBalance)<=0.001 && parseFloat(this.props.xdaiBalance) > 0){ - let getGasText = ( -
- ⛽ xDai -> ETH -
- ) - if(this.state.gettingGas){ - getGasText = ( -
- ⛽ -
- ) - } - - - let extraGasUpDisplay = ( -
-
You have DAI but no ETH for gas:
- -
- - ) - this.setState({extraGasUpDisplay}) - } - - this.updatePendingExits(this.state.daiAddress, xdaiweb3) - - if (!this.state.notSundai) { - const exitableSunDaiBalance = await this.getWrappedDaiBalance() - .catch(e => console.error('Failed to read extable daiBalance for MNY', e)); - this.setState({ exitableSunDaiBalance }); - } - - if(this.props.ERC20TOKEN&&dendaiContract){ - let denDaiBalance = await dendaiContract.methods.balanceOf(this.state.daiAddress).call() - denDaiBalance = mainnetweb3.utils.fromWei(denDaiBalance,"ether") - if(denDaiBalance!=this.state.denDaiBalance){ - this.setState({denDaiBalance}) - } - - console.log("vendorContract",vendorContract) - let maxWithdrawlAmount = await vendorContract.methods.allowance(this.state.daiAddress).call() - maxWithdrawlAmount = mainnetweb3.utils.fromWei(maxWithdrawlAmount,"ether") - if(maxWithdrawlAmount!=this.state.maxWithdrawlAmount){ - this.setState({maxWithdrawlAmount}) - } - - //dendaiToxDaiEstimatedTime - if(this.state.xdaiToDendaiMode=="withdrawing"){ - let txAge = Date.now() - this.state.loaderBarStartTime - let percentDone = Math.min(100,((txAge * 100) / dendaiToxDaiEstimatedTime)+5) - - let xdaiBalanceShouldBe = parseFloat(this.state.xdaiBalanceShouldBe)-0.0005 - console.log("watching for ",this.props.xdaiBalance,"to be ",xdaiBalanceShouldBe) - if(this.props.xdaiBalance>=(xdaiBalanceShouldBe)){ - this.setState({loaderBarPercent:100,loaderBarStatusText: i18n.t('exchange.funds_transferred'),loaderBarColor:"#62f54a"}) - setTimeout(()=>{ - this.setState({ - xdaiToDendaiMode: false, - loaderBarStatusText: i18n.t('loading'), - loaderBarStartTime:0, - loaderBarPercent: 1, - loaderBarColor: "#FFFFFF" - }) - },3500) - }else{ - this.setState({loaderBarPercent:percentDone}) - } - - }else if(this.state.xdaiToDendaiMode=="depositing"){ - let txAge = Date.now() - this.state.loaderBarStartTime - let percentDone = Math.min(100,((txAge * 100) / daiToxDaiEstimatedTime)+5) - - console.log("watching for ",this.props.xdaiBalance,"to be less than ",this.state.xdaiBalanceShouldBe+0.0005) - if(this.props.xdaiBalance<=(this.state.xdaiBalanceShouldBe+0.00005)){ - this.setState({loaderBarPercent:100,loaderBarStatusText: i18n.t('exchange.funds_bridged'),loaderBarColor:"#62f54a"}) - setTimeout(()=>{ - this.setState({ - xdaiToDendaiMode: false, - loaderBarStatusText: i18n.t('loading'), - loaderBarStartTime:0, - loaderBarPercent: 1, - loaderBarColor: "#FFFFFF" - }) - },3500) - }else{ - this.setState({loaderBarPercent:percentDone}) - } - } - - } - /* - console.log("SETTING ETH BALANCE OF "+this.state.daiAddress) - this.setState({ethBalance:mainnetweb3.utils.fromWei(await mainnetweb3.eth.getBalance(this.state.daiAddress),'ether') }) - if(xdaiweb3){ - //console.log("xdaiweb3:",xdaiweb3,"xdaiAddress",xdaiAddress) - let xdaiBalance = await xdaiweb3.eth.getBalance(this.state.daiAddress) - //console.log("!! xdaiBalance:",xdaiBalance) - this.setState({xdaiBalance:xdaiweb3.utils.fromWei(xdaiBalance,'ether')}) - }*/ - if(this.state.daiToXdaiMode=="withdrawing"){ - let txAge = Date.now() - this.state.loaderBarStartTime - let percentDone = Math.min(100,((txAge * 100) / xdaiToDaiEstimatedTime)+5) - - console.log("watching for ",this.props.daiBalance,"to be ",this.state.daiBalanceShouldBe-0.0005) - if(this.props.daiBalance>=(this.state.daiBalanceShouldBe-0.0005)){ - this.setState({loaderBarPercent:100,loaderBarStatusText: i18n.t('exchange.funds_bridged'),loaderBarColor:"#62f54a"}) - setTimeout(()=>{ - this.setState({ - daiToXdaiMode: false, - loaderBarStatusText: i18n.t('loading'), - loaderBarStartTime:0, - loaderBarPercent: 1, - loaderBarColor: "#FFFFFF" - }) - },3500) - }else{ - this.setState({loaderBarPercent:percentDone}) - } - - }else if(this.state.daiToXdaiMode=="depositing"){ - let txAge = Date.now() - this.state.loaderBarStartTime - let percentDone = Math.min(100,((txAge * 100) / daiToxDaiEstimatedTime)+5) - - //console.log("watching for ",this.state.xdaiBalance,"to be ",this.state.xdaiBalanceShouldBe-0.0005) - if(this.props.xdaiBalance>=(this.state.xdaiBalanceShouldBe-0.0005)){ - this.setState({loaderBarPercent:100,loaderBarStatusText: i18n.t('exchange.funds_bridged'),loaderBarColor:"#62f54a"}) - setTimeout(()=>{ - this.setState({ - daiToXdaiMode: false, - loaderBarStatusText: i18n.t('loading'), - loaderBarStartTime:0, - loaderBarPercent: 1, - loaderBarColor: "#FFFFFF" - }) - },3500) - }else{ - this.setState({loaderBarPercent:percentDone}) - } - }else if(this.state.daiToXdaiMode=="sending"){ - let txAge = Date.now() - this.state.loaderBarStartTime - let percentDone = Math.min(100,((txAge * 100) / sendDaiEstimatedTime)+5) - - console.log("watching for ",this.props.daiBalance,"to be ",this.state.daiBalanceShouldBe-0.0005) - if(this.props.daiBalance<=(this.state.daiBalanceShouldBe-0.0005)){ - this.setState({loaderBarPercent:100,loaderBarStatusText: i18n.t('exchange.funds_bridged'),loaderBarColor:"#62f54a"}) - setTimeout(()=>{ - this.setState({ - daiToXdaiMode: false, - loaderBarStatusText: i18n.t('loading'), - loaderBarStartTime:0, - loaderBarPercent: 1, - loaderBarColor: "#FFFFFF" - }) - },3500) - }else{ - this.setState({loaderBarPercent:percentDone}) - } - } - - - if(this.state.ethToDaiMode=="withdrawing"){ - let txAge = Date.now() - this.state.loaderBarStartTime - let percentDone = Math.min(100,((txAge * 100) / exchangeEstimatedTime) + 5) - //ethBalanceAtStart:this.state.ethBalance, - //ethBalanceShouldBe:this.state.ethBalance+amountOfChange, - console.log("watching for ",this.props.ethBalance,"to be ",this.state.ethBalanceShouldBe-0.001) - if(parseFloat(this.props.ethBalance)>=(this.state.ethBalanceShouldBe-0.001)){ - this.setState({loaderBarPercent:100,loaderBarStatusText: i18n.t('exchange.funds_bridged'),loaderBarColor:"#62f54a"}) - setTimeout(()=>{ - this.setState({ - ethToDaiMode: false, - loaderBarStatusText: i18n.t('loading'), - loaderBarStartTime:0, - loaderBarPercent: 1, - loaderBarColor: "#FFFFFF" - }) - },3500) - }else{ - this.setState({loaderBarPercent:percentDone}) - } - - }else if(this.state.ethToDaiMode=="depositing"){ - let txAge = Date.now() - this.state.loaderBarStartTime - let percentDone = Math.min(100,((txAge * 100) / exchangeEstimatedTime)+5) - - //console.log("watching for ",this.state.xdaiBalance,"to be ",this.state.xdaiBalanceShouldBe-0.0005) - if(this.props.daiBalance>=(this.state.daiBalanceShouldBe-0.0005)){ - this.setState({loaderBarPercent:100,loaderBarStatusText: i18n.t('exchange.funds_bridged'),loaderBarColor:"#62f54a"}) - setTimeout(()=>{ - this.setState({ - ethToDaiMode: false, - loaderBarStatusText: i18n.t('loading'), - loaderBarStartTime:0, - loaderBarPercent: 1, - loaderBarColor: "#FFFFFF" - }) - },3500) - }else{ - this.setState({loaderBarPercent:percentDone}) - } - }else if(this.state.ethToDaiMode=="sending"){ - let txAge = Date.now() - this.state.loaderBarStartTime - let percentDone = Math.min(100,((txAge * 100) / exchangeEstimatedTime) + 5) - //ethBalanceAtStart:this.state.ethBalance, - //ethBalanceShouldBe:this.state.ethBalance+amountOfChange, - console.log("watching for ",this.props.ethBalance,"to be ",this.state.ethBalanceShouldBe-0.001) - if(parseFloat(this.props.ethBalance)<=(this.state.ethBalanceShouldBe-0.001)){ - this.setState({loaderBarPercent:100,loaderBarStatusText: i18n.t('exchange.funds_bridged'),loaderBarColor:"#62f54a"}) - setTimeout(()=>{ - this.setState({ - ethToDaiMode: false, - loaderBarStatusText: i18n.t('loading'), - loaderBarStartTime:0, - loaderBarPercent: 1, - loaderBarColor: "#FFFFFF" - }) - },3500) - }else{ - this.setState({loaderBarPercent:percentDone}) - } - - } - - } - sendDai(){ - if(parseFloat(this.props.daiBalance) 0)){ - this.props.changeAlert({type: 'warning',message: i18n.t('exchange.invalid_to_amount')}); - }else{ - this.setState({ - daiToXdaiMode:"sending", - daiBalanceAtStart:this.props.daiBalance, - daiBalanceShouldBe:parseFloat(this.props.daiBalance)-parseFloat(this.state.daiSendAmount), - loaderBarColor:"#f5eb4a", - loaderBarStatusText: i18n.t('exchange.calculate_gas_price'), - loaderBarPercent:0, - loaderBarStartTime: Date.now(), - loaderBarClick:()=>{ - alert(i18n.t('exchange.go_to_etherscan')) - } - }) - this.setState({sendDai:false}) - this.transferDai(this.state.daiSendToAddress,this.state.daiSendAmount,"Sending "+this.state.daiSendAmount+" DAI to "+this.state.daiSendToAddress+"...",()=>{ - this.props.changeAlert({type: 'success',message: "Sent "+this.state.daiSendAmount+" DAI to "+this.state.daiSendToAddress}); - this.setState({ - daiToXdaiMode:false, - daiSendAmount:"", - daiSendToAddress:"", - loaderBarColor:"#FFFFFF", - loaderBarStatusText:"", - }) - }) - - } - } - canSendDai() { - return (this.state.daiSendToAddress && this.state.daiSendToAddress.length === 42 && parseFloat(this.state.daiSendAmount)>0 && parseFloat(this.state.daiSendAmount) <= parseFloat(this.props.daiBalance)) - } - - async transferDai(destination,amount,message,cb) { - axios.get("https://ethgasstation.info/json/ethgasAPI.json", { crossdomain: true }) - .catch((err)=>{ - console.log("Error getting gas price",err) - }) - .then((response)=>{ - if(response && response.data.average>0&&response.data.average<200){ - - this.setState({ - loaderBarColor:"#f5eb4a", - loaderBarStatusText:message, - }) - - response.data.average=response.data.average + (response.data.average*GASBOOSTPRICE) - let gwei = Math.round(response.data.average*100)/1000 - if(this.state.mainnetMetaAccount){ - //send funds using metaaccount on mainnet - - let paramsObject = { - from: this.state.daiAddress, - value: 0, - gas: 100000, - gasPrice: Math.round(gwei * 1000000000) - } - console.log("====================== >>>>>>>>> paramsObject!!!!!!!",paramsObject) - - paramsObject.to = this.props.daiContract._address - paramsObject.data = this.props.daiContract.methods.transfer( - destination, - this.state.mainnetweb3.utils.toWei(""+amount,"ether") - ).encodeABI() - - console.log("TTTTTTTTTTTTTTTTTTTTTX",paramsObject) - - this.state.mainnetweb3.eth.accounts.signTransaction(paramsObject, this.state.mainnetMetaAccount.privateKey).then(signed => { - console.log("========= >>> SIGNED",signed) - this.state.mainnetweb3.eth.sendSignedTransaction(signed.rawTransaction).on('receipt', (receipt)=>{ - console.log("META RECEIPT",receipt) - if(receipt&&receipt.transactionHash&&!metaReceiptTracker[receipt.transactionHash]){ - metaReceiptTracker[receipt.transactionHash] = true - cb(receipt) - } - }).on('error', (err)=>{ - console.log("EEEERRRRRRRROOOOORRRRR ======== >>>>>",err) - this.props.changeAlert({type: 'danger',message: err.toString()}); - }).then(console.log) - }); - - }else{ - //send funds using metamask (or other injected web3 ... should be checked and on mainnet) - console.log("Depositing to ",toDaiBridgeAccount) - - this.setState({ - loaderBarColor:"#f5eb4a", - loaderBarStatusText:message, - }) - - let metaMaskDaiContract = new this.props.web3.eth.Contract(this.props.daiContract._jsonInterface,this.props.daiContract._address) - console.log("CURRENT DAI CONTRACT YOU NEED TO GET ABI FROM:",this.props.daiContract) - this.props.tx(metaMaskDaiContract.methods.transfer( - destination, - this.state.mainnetweb3.utils.toWei(""+amount,"ether") - ///TODO LET ME PASS IN A CERTAIN AMOUNT OF GAS INSTEAD OF LEANING BACK ON THE COMPONENT!!!!! - ),120000,0,0,(receipt)=>{ - if(receipt){ - console.log("SESSION WITHDRAWN:",receipt) - cb(receipt) - } - }) - } - }else{ - console.log("ERRORed RESPONSE FROM ethgasstation",response) - } - }) - } - - async depositDaiToXdai(destination,amount,message,cb) { - let response - try { - response = await axios.get("https://ethgasstation.info/json/ethgasAPI.json", { crossdomain: true }) - } catch(err) { - console.log("Error getting gas price",err) - } - - if(response && response.data.average>0&&response.data.average<200){ - - response.data.average=response.data.average + (response.data.average*GASBOOSTPRICE) - let gwei = Math.round(response.data.average*100)/1000 - - const color = await this.state.xdaiweb3.getColor(this.props.pdaiContract._address); - if(this.state.mainnetMetaAccount){ - //send funds using metaaccount on mainnet - const amountWei = this.state.mainnetweb3.utils.toWei(""+amount,"ether") - - let paramsObject - if (this.props.network === "LeapTestnet" || this.props.network == "LeapMainnet") { - await this.maybeApprovePDai(amountWei); - - const allowance = await this.props.daiContract.methods.allowance( - this.state.daiAddress, - this.props.bridgeContract._address - ).call({from: this.state.daiAddress}) - - // Only trigger allowance dialogue when amount is more than allowance - if (new BN(allowance).lt(new BN(amountWei))) { - this.setState({ - loaderBarColor:"#f5eb4a", - loaderBarStatusText: "Approving token amount for Plasma bridge" - }) - paramsObject = { - from: this.state.daiAddress, - value: 0, - // TODO: Calculate gas estimate appropriately - gas: 100000, - gasPrice: Math.round(gwei * 1000000000) - } - console.log("====================== >>>>>>>>> paramsObject!!!!!!!",paramsObject) - - paramsObject.to = this.props.daiContract._address - paramsObject.data = this.props.daiContract.methods.approve( - this.props.bridgeContract._address, - amountWei - ).encodeABI() - - const signedApprove = await this.state.mainnetweb3.eth.accounts.signTransaction(paramsObject, this.state.mainnetMetaAccount.privateKey) - console.log("========= >>> SIGNED",signedApprove) - let receiptApprove - try { - // Here we send the approve transaction to the network - receiptApprove = await this.state.mainnetweb3.eth.sendSignedTransaction(signedApprove.rawTransaction) - } catch(err) { - console.log("EEEERRRRRRRROOOOORRRRR ======== >>>>>",err) - this.props.changeAlert({type: 'danger',message: err.toString()}); - } - console.log("META RECEIPT Approve",receiptApprove) - if(receiptApprove&&receiptApprove.transactionHash&&!metaReceiptTracker[receiptApprove.transactionHash]){ - metaReceiptTracker[receiptApprove.transactionHash] = true - } - } - } - - this.setState({ - loaderBarColor:"#f5eb4a", - loaderBarStatusText:message, - }) - - paramsObject = { - from: this.state.daiAddress, - value: 0, - // TODO: I guess this should be calculated by web3's gas - // estimate? - gas: 200000, - gasPrice: Math.round(gwei * 1000000000) - } - paramsObject.to = this.props.bridgeContract._address - paramsObject.data = this.props.bridgeContract.methods.deposit( - this.state.daiAddress, - amountWei, - color, - ).encodeABI() - console.log("====================== >>>>>>>>> paramsObject!!!!!!!",paramsObject) - - const signedDeposit = await this.state.mainnetweb3.eth.accounts.signTransaction(paramsObject, this.state.mainnetMetaAccount.privateKey) - console.log("========= >>> SIGNED",signedDeposit) - - let receiptDeposit - try { - receiptDeposit = await this.state.mainnetweb3.eth.sendSignedTransaction(signedDeposit.rawTransaction) - } catch(err) { - console.log("EEEERRRRRRRROOOOORRRRR ======== >>>>>",err) - this.props.changeAlert({type: 'danger',message: err.toString()}); - } - if(receiptDeposit&&receiptDeposit.transactionHash&&!metaReceiptTracker[receiptDeposit.transactionHash]){ - metaReceiptTracker[receiptDeposit.transactionHash] = true - console.log("receipt", receiptDeposit) - cb(receiptDeposit) - } - }else{ - //send funds using metamask (or other injected web3 ... should be checked and on mainnet) - const { pTx, web3 } = this.props - console.log("Depositing to ",toDaiBridgeAccount) - let bridgeContract = new web3.eth.Contract(this.props.bridgeContract._jsonInterface,this.props.bridgeContract._address) - console.log("CURRENT BRIDGE CONTRACT YOU NEED TO GET ABI FROM:",this.props.bridgeContract, this.state.daiAddress) - let daiContract = new web3.eth.Contract(this.props.daiContract._jsonInterface,this.props.daiContract._address) - console.log("CURRENT BRIDGE CONTRACT YOU NEED TO GET ABI FROM:",this.props.bridgeContract, this.state.daiAddress) - const amountWei = web3.utils.toWei(""+amount,"ether") - - if (this.props.network === "LeapTestnet" || this.props.network === "LeapMainnet") { - await this.maybeApprovePDai(amountWei); - - const allowance = await daiContract.methods.allowance( - this.state.daiAddress, - bridgeContract._address - ).call({from: this.state.daiAddress}) - - if (new BN(allowance).lt(new BN(amountWei))) { - this.setState({ - loaderBarColor:"#f5eb4a", - loaderBarStatusText: "Approving token amount for Plasma bridge" - }) - const approveReceipt = await pTx( - daiContract.methods.approve( - bridgeContract._address, - amountWei - ), - ///TODO LET ME PASS IN A CERTAIN AMOUNT OF GAS INSTEAD OF LEANING BACK ON THE COMPONENT!!!!! - 150000, - 0, - 0 - ) - } - } - - this.setState({ - loaderBarColor:"#f5eb4a", - loaderBarStatusText:message, - }) - - const depositReceipt = await pTx( - bridgeContract.methods.deposit( - this.state.daiAddress, - amountWei, - color, - ), - ///TODO LET ME PASS IN A CERTAIN AMOUNT OF GAS INSTEAD OF LEANING BACK ON THE COMPONENT!!!!! - 200000, - 0, - 0 - ) - if (depositReceipt) { - console.log("SESSION WITHDRAWN:",depositReceipt) - cb(depositReceipt) - } - } - } else { - console.log("ERRORed RESPONSE FROM ethgasstation",response) - } - } - canSendXdai() { - console.log("canSendXdai",this.state.xdaiSendToAddress,this.state.xdaiSendToAddress.length,parseFloat(this.state.xdaiSendAmount),parseFloat(this.props.xdaiBalance)) - return (this.state.xdaiSendToAddress && this.state.xdaiSendToAddress.length === 42 && parseFloat(this.state.xdaiSendAmount)>0 && parseFloat(this.state.xdaiSendAmount) <= parseFloat(this.props.xdaiBalance)) - } - - sendEth(){ - - let actualEthSendAmount = parseFloat(this.state.ethSendAmount)/parseFloat(this.props.ethprice) - - if(parseFloat(this.props.ethBalance)0)){ - this.props.changeAlert({type: 'warning',message: i18n.t('exchange.invalid_to_amount')}); - }else{ - this.setState({ - ethToDaiMode:"sending", - ethBalanceAtStart:this.props.ethBalance, - ethBalanceShouldBe:parseFloat(this.props.ethBalance)-actualEthSendAmount, - loaderBarColor:"#f5eb4a", - loaderBarStatusText: i18n.t('exchange.calculate_gas_price'), - loaderBarPercent:0, - loaderBarStartTime: Date.now(), - loaderBarClick:()=>{ - alert(i18n.t('exchange.go_to_etherscan')) - } - }) - this.setState({sendEth:false}) - //i think without inject meta mask this needs to be adjusted?!?! - if(this.state.mainnetMetaAccount){ - actualEthSendAmount = this.state.mainnetweb3.utils.toWei(""+Math.round(actualEthSendAmount*10000)/10000,'ether') - } - ////for some reason I needed this in and now I dont?!?!? - this.transferEth(this.state.ethSendToAddress,false,actualEthSendAmount,"Sending $"+this.state.ethSendAmount+" of ETH to "+this.state.ethSendToAddress+"...",()=>{ - this.props.changeAlert({type: 'success',message: "Sent $"+this.state.ethSendAmount+" of ETH to "+this.state.ethSendToAddress}); - this.setState({ - ethToDaiMode:false, - ethSendAmount:"", - ethSendToAddress:"", - loaderBarColor:"#FFFFFF", - loaderBarStatusText:"", - }) - }) - - } - } - canSendEth() { - let actualEthSendAmount = parseFloat(this.state.ethSendAmount)/parseFloat(this.props.ethprice) - return (this.state.ethSendToAddress && this.state.ethSendToAddress.length === 42 && actualEthSendAmount>0 && actualEthSendAmount <= parseFloat(this.props.ethBalance)) - } - transferEth(destination,call,amount,message,cb){ - if(this.state.mainnetMetaAccount){ - //send funds using metaaccount on mainnet - - axios.get("https://ethgasstation.info/json/ethgasAPI.json", { crossdomain: true }) - .catch((err)=>{ - console.log("Error getting gas price",err) - }) - .then((response)=>{ - if(response && response.data.average>0&&response.data.average<200){ - - this.setState({ - loaderBarColor:"#f5eb4a", - loaderBarStatusText:message, - }) - - response.data.average=response.data.average + (response.data.average*GASBOOSTPRICE) - let gwei = Math.round(response.data.average*100)/1000 - let paramsObject = { - from: this.state.daiAddress, - value: amount, - gas: 240000, - gasPrice: Math.round(gwei * 1000000000) - } - console.log("====================== >>>>>>>>> paramsObject!!!!!!!",paramsObject) - - paramsObject.to = destination - if(call){ - paramsObject.data = call.encodeABI() - }else{ - paramsObject.data = "0x00" - } - - console.log("TTTTTTTTTTTTTTTTTTTTTX",paramsObject) - - this.state.mainnetweb3.eth.accounts.signTransaction(paramsObject, this.state.mainnetMetaAccount.privateKey).then(signed => { - console.log("========= >>> SIGNED",signed) - this.state.mainnetweb3.eth.sendSignedTransaction(signed.rawTransaction).on('receipt', (receipt)=>{ - console.log("META RECEIPT",receipt) - if(receipt&&receipt.transactionHash&&!metaReceiptTracker[receipt.transactionHash]){ - metaReceiptTracker[receipt.transactionHash] = true - cb(receipt) - } - }).on('error', (err)=>{ - console.log("EEEERRRRRRRROOOOORRRRR ======== >>>>>",err) - this.props.changeAlert({type: 'danger',message: err.toString()}); - }).then(console.log) - }); - - }else{ - console.log("ERRORed RESPONSE FROM ethgasstation",response) - } - }) - - }else{ - console.log("Using uniswap exchange to move ETH to DAI") - - - //send funds using metamask (or other injected web3 ... should be checked and on mainnet) - this.setState({ - amount:"", - loaderBarColor:"#4ab3f5", - loaderBarStatusText:message, - loaderBarClick:()=>{ - alert(i18n.t('exchange.idk')); - } - }) - if(call){ - this.props.tx( - call - ,240000,0,amount,(receipt)=>{ - if(receipt){ - console.log("EXCHANGE COMPLETE?!?",receipt) - cb(receipt) - } - }) - }else{ - this.props.send( - destination, - amount, - 240000, - (err, receipt)=>{ - if(receipt){ - console.log("SEND COMPLETE?!?",receipt) - cb(receipt) - } - }) - } - - } - } - render() { - let {xdaiToDendaiMode,daiToXdaiMode,ethToDaiMode} = this.state - - let ethCancelButton = ( - - { - this.setState({amount:"",ethToDaiMode:false}) - }}> - {i18n.t('cancel')} - - - ) - let daiCancelButton = ( - - { - this.setState({amount:"",daiToXdaiMode:false}) - }}> - {i18n.t('cancel')} - - - ) - let xdaiCancelButton = ( - - { - this.setState({amount:"",xdaiToDendaiMode:false}) - }}> - {i18n.t('cancel')} - - - ) - - let buttonsDisabled = ( - xdaiToDendaiMode=="sending" || xdaiToDendaiMode=="withdrawing" || xdaiToDendaiMode=="depositing" || - daiToXdaiMode=="sending" || daiToXdaiMode=="withdrawing" || daiToXdaiMode=="depositing" || - ethToDaiMode=="sending" || ethToDaiMode=="depositing" || ethToDaiMode=="withdrawing" - ) - - let adjustedFontSize = Math.round((Math.min(document.documentElement.clientWidth,600)/600)*24) - let adjustedTop = Math.round((Math.min(document.documentElement.clientWidth,600)/600)*-20)+9 - - let xdaiToDendaiDisplay = i18n.t('loading') - - let tokenDisplay = "" - if(this.props.ERC20TOKEN){ - if(xdaiToDendaiMode=="sending" || xdaiToDendaiMode=="withdrawing" || xdaiToDendaiMode=="depositing"){ - xdaiToDendaiDisplay = ( -
- -
- {this.state.loaderBarStatusText} -
-
- ) - - }else if(xdaiToDendaiMode=="deposit"){ - - //console.log("CHECKING META ACCOUNT ",this.state.xdaiMetaAccount,this.props.network) - if(!this.state.xdaiMetaAccount && (this.props.network!="xDai"&&this.props.network!="Unknown")){ - xdaiToDendaiDisplay = ( - - ) - }else{ - xdaiToDendaiDisplay = ( -
- -
- -
-
- -
-
-
$
-
- this.updateState('amount', event.target.value)} /> -
-
-
-
- - {xdaiCancelButton} - -
-
- - -
-
- ) - } - }else if(xdaiToDendaiMode=="withdraw"){ - console.log("CHECKING META ACCOUNT ",this.state.xdaiMetaAccount,this.props.network) - if(!this.state.xdaiMetaAccount && (this.props.network!="xDai"&&this.props.network!="Unknown")){ - xdaiToDendaiDisplay = ( - - ) - }else{ - - let extraWithdrawInfo = "" - - if(!this.props.isAdmin && (!this.props.isVendor || !this.props.isVendor.isAllowed)){ - extraWithdrawInfo = ( -
-
- Maximum withdrawal amount: {this.props.dollarDisplay(this.state.maxWithdrawlAmount)} -
-
- ({this.state.withdrawalExplanation}) -
-
- ) - } - - - xdaiToDendaiDisplay = ( -
-
- -
- -
-
- -
-
-
$
-
- this.updateState('amount', event.target.value)} /> -
-
-
-
- - {xdaiCancelButton} - -
-
- - -
-
- {extraWithdrawInfo} -
- ) - } - }else{ - - - - - xdaiToDendaiDisplay = ( -
- -
- -
- -
- -
-
- ) - } - - let link = "" - if(this.props.contracts){ - link = "https://blockscout.com/poa/dai/address/"+this.props.contracts[this.props.ERC20TOKEN]._address+"/contracts" - } - - tokenDisplay = ( -
-
-
- - - -
-
- {this.props.ERC20NAME} -
-
- - {this.props.dollarDisplay(this.state.denDaiBalance)} - -
-
- -
- -
- -
- {xdaiToDendaiDisplay} -
-
- ) - } - - let daiToXdaiDisplay = i18n.t('loading') - - //console.log("daiToXdaiMode",daiToXdaiMode) - if(daiToXdaiMode=="sending" || daiToXdaiMode=="withdrawing" || daiToXdaiMode=="depositing"){ - daiToXdaiDisplay = ( -
- -
- {this.state.loaderBarStatusText} -
-
- ) - - }else if(daiToXdaiMode=="deposit"){ - // if(!this.state.mainnetMetaAccount && this.props.network!="Mainnet"){ - // daiToXdaiDisplay = ( - //
- // - //
- // ) - //}else - if(this.props.ethBalance<=0){ - daiToXdaiDisplay = ( - - ) - }else{ - daiToXdaiDisplay = ( -
-
- -
-
- -
- this.updateState('amount', event.target.value)} /> -
-
-
-
- - {daiCancelButton} - -
-
- - - -
-
- ) - } - } else if(daiToXdaiMode=="withdraw"){ - console.log("CHECKING META ACCOUNT ",this.state.xdaiMetaAccount,this.props.network) - if(!this.state.xdaiMetaAccount && this.props.network!="LeapTestnet"){ - daiToXdaiDisplay = ( - - ) - }else{ - daiToXdaiDisplay = ( -
- -
- -
-
- -
- this.updateState('amount', event.target.value)} /> -
-
-
-
- - {daiCancelButton} - -
-
- - -
-
- ) - } - } else { - daiToXdaiDisplay = ( - - - - - - ) - } - - let ethToDaiDisplay = i18n.t('loading') - - if(ethToDaiMode=="sending" || ethToDaiMode=="depositing" || ethToDaiMode=="withdrawing"){ - ethToDaiDisplay = ( -
- -
- {this.state.loaderBarStatusText} -
-
- ) - - }else if(ethToDaiMode=="deposit"){ - - if(this.props.ethBalance<=0){ - ethToDaiDisplay = ( - - ); - }else { - ethToDaiDisplay = ( -
- -
- -
-
- -
- this.updateState('amount', event.target.value)} /> -
-
-
-
- - {ethCancelButton} - -
-
- - -
-
- ) - } - - }else if(ethToDaiMode=="withdraw"){ - if(!this.state.mainnetMetaAccount && this.props.network!="Mainnet"){ - ethToDaiDisplay = ( - - ) - }else if(this.props.ethBalance<=0){ - ethToDaiDisplay = ( - - ) - }else{ - ethToDaiDisplay = ( -
- -
- -
-
- -
- this.updateState('amount', event.target.value)} /> -
-
-
-
- - {ethCancelButton} - -
-
- -
-
- ) - } - - }else{ - ethToDaiDisplay = ( - - - - - ) - - } - - - - - let sendDaiButton = ( - { - this.setState({sendDai:true})}} - /> - ) - - //style={{marginTop:40,backgroundColor:this.props.mainStyle.mainColor}} - let sendDaiRow = "" - if(this.state.sendDai){ - sendDaiRow = ( - - - this.updateState('daiSendToAddress', event.target.value)} - width={1} - /> - -
- { this.state.daiSendToAddress && this.state.daiSendToAddress.length==42 && } -
- - - this.updateState('daiSendAmount', event.target.value)} - width={1} - /> - { - this.setState({ - daiSendAmount: Math.floor((this.props.daiBalance)*100)/100 - }, - () => { - this.setState({ - canSendDai: this.canSendDai(), - canSendEth: this.canSendEth(), - canSendXdai: this.canSendXdai() - }) - }) - }} - > - max - - - - -
- ) - sendDaiButton = ( - - ) - } - - let sendEthButton = ( - {this.setState({sendEth:true})}} - /> - ) - - let fundByWyreButton = ( - - ) - - - let sendEthRow = "" - if(this.state.sendEth){ - sendEthRow = ( - - - this.updateState('ethSendToAddress', event.target.value)} - width={1} - /> - -
- { this.state.ethSendToAddress && this.state.ethSendToAddress.length==42 && } -
- - - this.updateState('ethSendAmount', event.target.value)} - width={1} - /> - { - console.log("Getting gas price...") - axios.get("https://ethgasstation.info/json/ethgasAPI.json", { crossdomain: true }) - .catch((err)=>{ - console.log("Error getting gas price",err) - }) - .then((response)=>{ - if(response && response.data.average>0&&response.data.average<200){ - response.data.average=response.data.average + (response.data.average*GASBOOSTPRICE) - let gwei = Math.round(response.data.average*100)/1000 - - console.log(gwei) - - - let IDKAMOUNTTOLEAVE = gwei*(1111000000*2) * 201000 // idk maybe enough for a couple transactions? - - console.log("let's leave ",IDKAMOUNTTOLEAVE,this.props.ethBalance) - - let gasInEth = this.props.web3.utils.fromWei(""+IDKAMOUNTTOLEAVE,'ether') - console.log("gasInEth",gasInEth) - - let adjustedEthBalance = (parseFloat(this.props.ethBalance) - parseFloat(gasInEth)) - console.log(adjustedEthBalance) - - this.setState({ethSendAmount: Math.floor(this.props.ethprice*adjustedEthBalance*100)/100 },()=>{ - this.setState({ canSendDai: this.canSendDai(), canSendEth: this.canSendEth(), canSendXdai: this.canSendXdai() }) - }) - } - }) - }} - > - max - - - - -
- ) - sendEthButton = ( - - ) - } - - let sendXdaiButton - - if(this.props.ERC20TOKEN){ - sendXdaiButton = ( - {this.setState({sendXdai:true})}} - /> - ) - }else{ - sendXdaiButton = ( - this.props.goBack("send_to_address")} - /> - ) - } - - let sendXdaiRow = "" - if(this.state.sendXdai){ - sendXdaiRow = ( - - - this.updateState('xdaiSendToAddress', event.target.value)} - /> - -
- { this.state.xdaiSendToAddress && this.state.xdaiSendToAddress.length==42 && } -
- - - this.updateState('xdaiSendAmount', event.target.value)} - /> - { - this.setState({xdaiSendAmount: Math.floor((this.props.xdaiBalance-0.01)*100)/100 },()=>{ - this.setState({ canSendDai: this.canSendDai(), canSendEth: this.canSendEth(), canSendXdai: this.canSendXdai() }) - }) - }} - > - max - - - - - -
- ) - sendXdaiButton = ( - - ) - } - - //console.log("eth price ",this.props.ethBalance,this.props.ethprice) - return ( - - {this.state.pendingMsg &&
{this.state.pendingMsg}
} - - {tokenDisplay} - - -
-
- -
-
- MNY -
-
- - {this.props.dollarDisplay(this.props.xdaiBalance)} - -
-
- {sendXdaiButton} -
- -
- {sendXdaiRow} - -
- {daiToXdaiDisplay} -
- - - -
-
- -
-
- DAI -
-
- - {this.props.dollarDisplay(this.props.daiBalance)} - -
-
- {sendDaiButton} -
-
- {sendDaiRow} - - -
- {ethToDaiDisplay} -
- - -
-
- -
-
- ETH -
-
- - {this.props.dollarDisplay(this.props.ethBalance*this.props.ethprice)} - -
-
- {this.state.mainnetMetaAccount ? sendEthButton : null} -
-
- - {sendEthRow} - {this.state.extraGasUpDisplay} - - - { (window.location.hostname.indexOf("localhost") >= 0 || window.location.hostname.indexOf("wyre.xdai.io") >= 0 || window.location.hostname.indexOf("s.xdai.io") >= 0) && ( -
-
-
- -
-
- Wyre -
-
- {/*
-
-
$
-
- - this.updateState('wyreFundAmount', event.target.value) - } - /> -
*/} -
- `$${value}`} - step={1} - onChange={value => - this.updateState('wyreFundAmount', value) - } - /> -
-
-
- {fundByWyreButton} -
-
-
- )} -
- ) - } -} diff --git a/src/components/MoreButtons.js b/src/components/MoreButtons.js index 7b5ad3e7c..ce701a161 100644 --- a/src/components/MoreButtons.js +++ b/src/components/MoreButtons.js @@ -16,41 +16,9 @@ export default ({ dollarDisplay, subBalanceDisplay }) => { - let exchangeButton; - - if (!isVendor) { - exchangeButton = ( - { - changeView("exchange"); - }} - > - - - {i18n.t("more_buttons.exchange")} - - - ); - } else { - exchangeButton = ( - { - changeView("cash_out"); - }} - > - - - {"Cash Out"} - - - ); - } - return ( - + { @@ -63,7 +31,6 @@ export default ({
- {exchangeButton} ); };