Skip to content

Commit

Permalink
Completed front end
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-crypto committed Jan 10, 2022
1 parent 06a863a commit 8d74287
Show file tree
Hide file tree
Showing 23 changed files with 33,854 additions and 31,788 deletions.
64,721 changes: 33,033 additions & 31,688 deletions package-lock.json

Large diffs are not rendered by default.

23 changes: 7 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Binary file added src/Uniswap_Logo.svg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 18 additions & 10 deletions src/abis/DexAggregator.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
}
],
"stateMutability": "view",
"type": "function"
"type": "function",
"constant": true
},
{
"inputs": [],
Expand All @@ -103,7 +104,8 @@
}
],
"stateMutability": "view",
"type": "function"
"type": "function",
"constant": true
},
{
"inputs": [],
Expand All @@ -116,7 +118,8 @@
}
],
"stateMutability": "view",
"type": "function"
"type": "function",
"constant": true
},
{
"inputs": [],
Expand All @@ -129,11 +132,13 @@
}
],
"stateMutability": "view",
"type": "function"
"type": "function",
"constant": true
},
{
"stateMutability": "payable",
"type": "receive"
"type": "receive",
"payable": true
},
{
"inputs": [
Expand Down Expand Up @@ -162,7 +167,8 @@
}
],
"stateMutability": "view",
"type": "function"
"type": "function",
"constant": true
},
{
"inputs": [
Expand All @@ -175,7 +181,8 @@
"name": "buyUSDCAtBestPrice",
"outputs": [],
"stateMutability": "payable",
"type": "function"
"type": "function",
"payable": true
},
{
"inputs": [
Expand Down Expand Up @@ -22125,12 +22132,13 @@
"1": {
"events": {},
"links": {},
"address": "0xfa8896E18d38a8ba9Fe3d184B07b2b5B81AE025F",
"transactionHash": "0xd8297d98a2293168b2d4135361bcf1e778c89d5956de24a68bf764e20701b200"
"address": "0x4C3D14a6b8BCBA11DF697a60CBbd2463E138C01F",
"transactionHash": "0x04fe71a7e2b55d73ca94bb7c8b640383613367f6c6c66ce6497d1555f1e4c015"
}
},
"schemaVersion": "3.4.3",
"updatedAt": "2022-01-09T03:21:32.915Z",
"updatedAt": "2022-01-10T20:28:05.623Z",
"networkType": "ethereum",
"devdoc": {
"kind": "dev",
"methods": {},
Expand Down
6 changes: 3 additions & 3 deletions src/abis/Migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -2311,8 +2311,8 @@
"1": {
"events": {},
"links": {},
"address": "0x445cD7fE2A56987a79F640B39d0De47129e15D40",
"transactionHash": "0x658a0988144d4610ae092a849aa02c95df75cb8a6f6121987828f3f9ad8c99e1"
"address": "0x3a8A9276Dc70C7fA991e6AdAd1d9E6d40cF0e3Ec",
"transactionHash": "0x3804d38f8d840a8138fb2bc4860f00a8e6b4246e867596745d35f02a514dddfc"
},
"1640296764925": {
"events": {},
Expand All @@ -2322,7 +2322,7 @@
}
},
"schemaVersion": "3.4.3",
"updatedAt": "2022-01-08T03:18:49.104Z",
"updatedAt": "2022-01-10T20:28:05.628Z",
"networkType": "ethereum",
"devdoc": {
"kind": "dev",
Expand Down
27 changes: 27 additions & 0 deletions src/components/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,30 @@
transform: rotate(360deg);
}
}

.arrow {
border: solid black;
border-width: 0 3px 3px 0;
display: inline-block;
padding: 3px;
}

.right {
transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
}

.left {
transform: rotate(135deg);
-webkit-transform: rotate(135deg);
}

.up {
transform: rotate(-135deg);
-webkit-transform: rotate(-135deg);
}

.down {
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}
183 changes: 160 additions & 23 deletions src/components/App.js
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;

Loading

0 comments on commit 8d74287

Please sign in to comment.