Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ethgasstation to predict tx wait times #178

Merged
merged 5 commits into from
Jul 6, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,22 @@ export default class App extends Component {
)
}

if (view.includes("loader_")) {
const network = view.replace("loader_");
return (
<div>
<div
style={{zIndex:1,position:"relative",color:"#dddddd"}}>

<NavCard
title={"Sending..."}
goBack={this.goBack.bind(this)} />
</div>
<Loader loaderImage={LOADERIMAGE} network={network} />
</div>
);
}

const sendByScan = (
<SendByScan
parseAndCleanPath={this.parseAndCleanPath.bind(this)}
Expand Down
6 changes: 3 additions & 3 deletions src/components/Bity.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { format } from "@tammo/react-iban";
import styled from "styled-components";
import { isValid } from "iban";
import { placeOrder, getOrder, getEstimate } from "../services/bity";
import { gasPrice } from "../services/core";
import { price } from "../services/ethgasstation";

const P = styled.p`
color: gray;
Expand Down Expand Up @@ -126,7 +126,7 @@ class Bity extends Component {
changeAlert
} = this.props;
if (IBAN.valid) {
changeView("loader");
changeView("loader_ROOTCHAIN");

let order;
const amountInEth = (amount.value / ethPrice).toString();
Expand All @@ -152,7 +152,7 @@ class Bity extends Component {
if (metaAccount) {
let gwei;
try {
gwei = await gasPrice();
gwei = await price();
} catch (err) {
changeAlert(
"warning",
Expand Down
14 changes: 7 additions & 7 deletions src/components/Exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { bi, add, divide } from 'jsbi-utils';
import getConfig from "../config";
import { PrimaryButton, BorderButton } from "./Buttons";
import bityLogo from '../assets/bity.png';
import { gasPrice } from "../services/core";
import { price } from "../services/ethgasstation";

const CONFIG = getConfig();
const BN = Web3.utils.BN
Expand Down Expand Up @@ -311,7 +311,7 @@ export default class Exchange extends React.Component {

let gwei;
try {
gwei = await gasPrice();
gwei = await price();
} catch(err) {
console.log("Error getting gas price",err)
}
Expand Down Expand Up @@ -375,7 +375,7 @@ export default class Exchange extends React.Component {
async depositDai(destination, amount, message, cb) {
let gwei
try {
gwei = await gasPrice();
gwei = await price();
} catch(err) {
console.log("Error getting gas price",err)
}
Expand Down Expand Up @@ -565,7 +565,7 @@ export default class Exchange extends React.Component {

let gwei;
try {
gwei = await gasPrice();
gwei = await price();
} catch(err) {
// TODO: Propagate error to user
console.log("Error getting gas price",err)
Expand Down Expand Up @@ -630,7 +630,7 @@ export default class Exchange extends React.Component {
if(this.state.mainnetMetaAccount){
//send funds using metaaccount on mainnet

gasPrice()
price()
.catch((err)=>{
console.log("Error getting gas price",err)
})
Expand Down Expand Up @@ -1159,7 +1159,7 @@ export default class Exchange extends React.Component {
if(this.state.mainnetMetaAccount){
//send funds using metaaccount on mainnet

gasPrice()
price()
.catch((err)=>{
console.log("Error getting gas price",err)
})
Expand Down Expand Up @@ -1514,7 +1514,7 @@ export default class Exchange extends React.Component {
ml={2}
onClick={() => {
console.log("Getting gas price...")
gasPrice()
price()
.catch((err)=>{
console.log("Error getting gas price",err)
})
Expand Down
80 changes: 66 additions & 14 deletions src/components/Loader.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,84 @@
import React, { Component } from 'react';
let interval
// @format
import React, { Component } from "react";
import { time } from "../services/ethgasstation";
import getConfig from "../config";

let interval;
MaxStalker marked this conversation as resolved.
Show resolved Hide resolved
const CONFIG = getConfig();
const RATE = {
PERCENT: 3,
EACH: 250
};

class Loader extends Component {
constructor(props) {
super(props);

this.state = {
percent: 5,
progress: 0,
rate: {
percent: RATE.PERCENT,
each: RATE.EACH
}
};

this.progress = this.progress.bind(this);
}
componentDidMount(){
interval = setInterval(this.loadMore.bind(this),250)

async componentDidMount() {
const { network } = this.props;

let t;
if (network === "ROOTCHAIN") {
t = await time();
} else if (network === "SIDECHAIN") {
t = CONFIG.SIDECHAIN.TIME_ESTIMATES.TX; //ms
throw new Error("Network not yet supported");
} else {
// 8ms was Loader's default value.
t = 8333; // ms
}

const rate = {
// NOTE: We "round" the result of "each"'s calculation
each: parseInt(t / 100 / RATE.PERCENT, 10),
MaxStalker marked this conversation as resolved.
Show resolved Hide resolved
percent: RATE.PERCENT
};
this.setState({ rate }, () => {
interval = setInterval(this.progress, rate.each);
});
}
componentWillUnmount(){
clearInterval(interval)

componentWillUnmount() {
clearInterval(interval);
}
loadMore(){
let newPercent = this.state.percent+3
if(newPercent>100) newPercent=100
this.setState({percent:newPercent})

progress() {
const { rate, progress } = this.state;

let newProgress = progress + rate.percent;
if (newProgress > 100) {
newProgress = 100;
clearInterval(interval);
}
this.setState({ progress: newProgress });
}

render() {
const { progress } = this.state;
const { loaderImage } = this.props;

return (
<div className="loader">
<img src ={this.props.loaderImage} className="loader__logo" alt=""/>
<img src={loaderImage} className="loader__logo" alt="" />
iamonuwa marked this conversation as resolved.
Show resolved Hide resolved
<div className="loader__progress">
<div className="loader__progress_fill" style={{width:this.state.percent+"%"}}/>
<div
className="loader__progress_fill"
style={{ width: progress + "%" }}
/>
</div>
</div>
)
);
}
}
export default Loader;
2 changes: 1 addition & 1 deletion src/components/SendToAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default class SendToAddress extends React.Component {
this.props.changeAlert({type: 'warning', message: 'Not enough funds: '+currencyDisplay(Math.floor((parseFloat(this.props.balance))*100)/100)})
}else{
console.log("SWITCH TO LOADER VIEW...",amount)
this.props.changeView('loader')
this.props.changeView('loader_SIDECHAIN')
setTimeout(()=>{window.scrollTo(0,0)},60)

console.log("web3",this.props.web3)
Expand Down
2 changes: 1 addition & 1 deletion src/components/WithdrawFromPrivate.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class SendToAddress extends React.Component {

if(this.state.canWithdraw){
console.log("SWITCH TO LOADER VIEW...")
this.props.changeView('loader')
this.props.changeView('loader_SIDECHAIN')
setTimeout(()=>{window.scrollTo(0,0)},60)
//console.log("metaAccount",this.state.metaAccount,"amount",this.props.web3.utils.toWei(amount,'ether'))

Expand Down
9 changes: 6 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const configs = [
// is dependent on how often blocks are submitted to the network.
// Ultimately, we want to remove this value from the code base somehow.
EXIT: 160000,
DEPOSIT: 330000
DEPOSIT: 330000,
TX: 1000,
}
},
ROOTCHAIN: {
Expand Down Expand Up @@ -57,7 +58,8 @@ const configs = [
// is dependent on how often blocks are submitted to the network.
// Ultimately, we want to remove this value from the code base somehow.
EXIT: 160000,
DEPOSIT: 330000
DEPOSIT: 330000,
TX: 1000,
}
},
ROOTCHAIN: {
Expand Down Expand Up @@ -92,7 +94,8 @@ const configs = [
// is dependent on how often blocks are submitted to the network.
// Ultimately, we want to remove this value from the code base somehow.
EXIT: 160000,
DEPOSIT: 330000
DEPOSIT: 330000,
TX: 1000,
}
},
ROOTCHAIN: {
Expand Down
19 changes: 0 additions & 19 deletions src/services/core.js

This file was deleted.

31 changes: 31 additions & 0 deletions src/services/ethgasstation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @format
import getConfig from "../config";

const CONFIG = getConfig();
const API = "https://ethgasstation.info/json/ethgasAPI.json";

function get() {
return fetch(API, {
mode: "cors",
method: "get"
}).then(r => r.json());
}

// NOTE: Both price() and time() currently default to average. In the future,
// these function could be adjusted to feature "fast" and "safe" too.
// NOTE2: Both functions can throw and should be try-catch'ed.
export async function price() {
const { average } = await get();
if (average > 0 && average < 200) {
const avg = average + average * CONFIG.ROOTCHAIN.GAS.BOOST_BY;
return Math.round(avg * 100) / 1000;
}
return Promise.reject("Average out of range (0–200)");
}

export async function time() {
// NOTE: avgWait is returned from ethgasstation in minutes (double)
const { avgWait } = await get();
// We convert minutes to milliseconds
return avgWait * 60 * 1000;
}