-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06a863a
commit 8d74287
Showing
23 changed files
with
33,854 additions
and
31,788 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,32 +5,23 @@ | |
"author": "[email protected]", | ||
"dependencies": { | ||
"@openzeppelin/contracts": "^4.4.1", | ||
"@openzeppelin/test-helpers": "^0.5.15", | ||
"@testing-library/jest-dom": "^5.16.1", | ||
"@testing-library/react": "^12.1.2", | ||
"@testing-library/user-event": "^13.5.0", | ||
"@uniswap/v2-periphery": "^1.1.0-beta.0", | ||
"babel-polyfill": "6.26.0", | ||
"babel-preset-env": "1.7.0", | ||
"babel-preset-es2015": "6.24.1", | ||
"babel-preset-stage-2": "6.24.1", | ||
"babel-preset-stage-3": "6.24.1", | ||
"babel-register": "6.26.0", | ||
"bootstrap": "^5.1.1", | ||
"bootstrap": "4.3.1", | ||
"chai": "4.2.0", | ||
"chai-as-promised": "7.1.1", | ||
"chai-bignumber": "3.0.0", | ||
"dotenv": "8.2.0", | ||
"lodash": "^4.17.21", | ||
"moment": "^2.29.1", | ||
"identicon.js": "^2.3.3", | ||
"react": "^17.0.2", | ||
"react-bootstrap": "^2.0.0-rc.1", | ||
"react-dom": "^17.0.2", | ||
"react-scripts": "5.0.0", | ||
"solidity-coverage": "^0.7.17", | ||
"truffle": "^5.1.33", | ||
"truffle-flattener": "^1.5.0", | ||
"web-vitals": "^2.1.2", | ||
"web3": "^1.5.2" | ||
"react-dom": "17.0.2", | ||
"react-scripts": "^4.0.3", | ||
"truffle": "^5.3.6", | ||
"web3": "^1.3.6" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,163 @@ | ||
import logo from '../logo.svg'; | ||
import './App.css'; | ||
|
||
function App() { | ||
|
||
return ( | ||
<div className="App"> | ||
<header className="App-header"> | ||
<img src={logo} className="App-logo" alt="logo" /> | ||
<p> | ||
Edit <code>src/App.js</code> and save to reload. | ||
</p> | ||
<a | ||
className="App-link" | ||
href="https://reactjs.org" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Learn React | ||
</a> | ||
</header> | ||
</div> | ||
); | ||
import React, { Component } from 'react' | ||
import Web3 from 'web3' | ||
import IERC20 from '../abis/IERC20.json' | ||
import DexAggregator from '../abis/DexAggregator.json' | ||
import Navbar from './Navbar' | ||
import Main from './Main' | ||
import './App.css' | ||
import { futureTime } from '../helpers' | ||
|
||
const usdcAddress = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" | ||
const exchangeDataReset = { | ||
outputsLoading: false, | ||
uniOutput: '0', | ||
sushiOutput: '0', | ||
} | ||
class App extends Component { | ||
|
||
async componentWillMount() { | ||
await this.loadWeb3() | ||
await this.loadBlockchainData() | ||
} | ||
|
||
async loadBlockchainData() { | ||
const web3 = window.web3 | ||
|
||
const accounts = await web3.eth.getAccounts() | ||
this.setState({ account: accounts[0] }) | ||
|
||
const ethBalance = await web3.eth.getBalance(this.state.account) | ||
this.setState({ ethBalance }) | ||
|
||
// Load USDC | ||
try { | ||
// Create new web3 usdc contract instance | ||
const usdc = new web3.eth.Contract(IERC20.abi, usdcAddress) | ||
this.setState({ usdc }) | ||
let usdcBalance = await usdc.methods.balanceOf(this.state.account).call() | ||
this.setState({ usdcBalance: usdcBalance.toString() }) | ||
} catch (error) { | ||
console.log('USDC contract not deployed to the current network. Please select another network with Metamask.') | ||
} | ||
|
||
// Load Aggregator | ||
const networkId = await web3.eth.net.getId() | ||
const dexAggregatorData = DexAggregator.networks[networkId] | ||
if(dexAggregatorData) { | ||
const dexAggregator = new web3.eth.Contract(DexAggregator.abi, dexAggregatorData.address) | ||
this.setState({ dexAggregator }) | ||
this.setState({ dexAddress: dexAggregatorData.address}) | ||
} else { | ||
window.alert('DexAggregator contract not deployed to detected network.') | ||
} | ||
|
||
this.setState({ loading: false }) | ||
} | ||
|
||
async loadWeb3() { | ||
if (window.ethereum) { | ||
window.web3 = new Web3(window.ethereum) | ||
await window.ethereum.enable() | ||
} | ||
else if (window.web3) { | ||
window.web3 = new Web3(window.web3.currentProvider) | ||
} | ||
else { | ||
window.alert('Non-Ethereum browser detected. You should consider trying MetaMask!') | ||
} | ||
} | ||
|
||
buyUsdc = (etherAmount) => { | ||
this.setState({ loading: true }) | ||
this.state.dexAggregator.methods.buyUSDCAtBestPrice(futureTime(15)).send({ value: etherAmount, from: this.state.account }).on('transactionHash', (hash) => { | ||
this.setState({ loading: false }) | ||
this.setState({exchangeData : exchangeDataReset}) | ||
}) | ||
} | ||
|
||
sellUsdc = async (usdcAmount) => { | ||
this.setState({ loading: true }) | ||
console.log(this.state.dexAggregator) | ||
await this.state.usdc.methods.approve(this.state.dexAddress, usdcAmount).send({ from: this.state.account }) | ||
await this.state.dexAggregator.methods.sellUSDCAtBestPrice(usdcAmount, futureTime(15)).send({ from: this.state.account }) | ||
this.setState({ loading: false }) | ||
this.setState({exchangeData : exchangeDataReset}) | ||
} | ||
|
||
getOutputs = async (input, pairArray) => { | ||
let data = this.state.exchangeData | ||
if(input !== '0' ){ | ||
this.setState({ exchangeData: {...data, outputsLoading: true }}) | ||
const outputs = await this.state.dexAggregator.methods.getReturnAmounts(input, pairArray).call() | ||
console.log(outputs) | ||
this.setState({ | ||
exchangeData: { | ||
...data, | ||
outputsLoading: false, | ||
uniOutput: outputs[0], | ||
sushiOutput: outputs[1] | ||
} | ||
}) | ||
} else { | ||
this.setState({ | ||
exchangeData: exchangeDataReset | ||
}) | ||
} | ||
} | ||
|
||
constructor(props) { | ||
super(props) | ||
this.state = { | ||
account: '', | ||
usdc: {}, | ||
dexAggregator: {}, | ||
ethBalance: '0', | ||
usdcBalance: '0', | ||
loading: true, | ||
dexAddress: "", | ||
exchangeData: exchangeDataReset | ||
} | ||
} | ||
|
||
render() { | ||
let content | ||
if(this.state.loading) { | ||
content = <p id="loader" className="text-center">Loading...</p> | ||
} else { | ||
content = <Main | ||
ethBalance={this.state.ethBalance} | ||
usdcBalance={this.state.usdcBalance} | ||
exchangeData={this.state.exchangeData} | ||
buyUsdc={this.buyUsdc} | ||
sellUsdc={this.sellUsdc} | ||
getOutputs={this.getOutputs} | ||
/> | ||
} | ||
|
||
return ( | ||
<div> | ||
<Navbar account={this.state.account} /> | ||
<div className="container-fluid mt-5"> | ||
<div className="row"> | ||
<main role="main" className="col-lg-12 ml-auto mr-auto" style={{ maxWidth: '600px' }}> | ||
<div className="content mr-auto ml-auto"> | ||
<a | ||
href="http://www.dappuniversity.com/bootcamp" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
</a> | ||
|
||
{content} | ||
|
||
</div> | ||
</main> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default App; | ||
|
Oops, something went wrong.