Skip to content

Commit

Permalink
Fix transaction processing to happen immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
generalpiston committed Mar 26, 2018
1 parent 70b2b94 commit b9aaac7
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 88 deletions.
6 changes: 3 additions & 3 deletions App/Startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class Startup extends Component {
return <Err error={this.props.error} />;
}

if (this.props.txhash) {
return <TransactionsProcessing txhash={this.props.txhash} />;
if (this.props.processing) {
return <TransactionsProcessing />;
}

if (!this.props.web3 || !this.state.finished) {
Expand All @@ -49,4 +49,4 @@ class Startup extends Component {
}
}

export default connect((state) => ({ web3: state.wallet.web3, txhash: state.wallet.txhash, error: state.error }), dispatch => ({ dispatch }))(Startup);
export default connect((state) => ({ web3: state.wallet.web3, processing: state.wallet.processing, error: state.error }), dispatch => ({ dispatch }))(Startup);
4 changes: 2 additions & 2 deletions actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as Actions from "../constants/actions";
export const addOrders = createAction(Actions.ADD_ORDERS);
export const addTransactions = createAction(Actions.ADD_TRANSACTIONS);
export const addAssets = createAction(Actions.ADD_ASSETS);
export const addProcessing = createAction(Actions.ADD_PROCESSING);
export const removeProcessing = createAction(Actions.REMOVE_PROCESSING);
export const processing = createAction(Actions.PROCESSING);
export const notProcessing = createAction(Actions.NOT_PROCESSING);
export const setBaseToken = createAction(Actions.SET_BASE_TOKEN);
export const setError = createAction(Actions.SET_ERROR);
export const setNetwork = createAction(Actions.SET_NETWORK);
Expand Down
4 changes: 2 additions & 2 deletions constants/actions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const ADD_ASSETS = "ADD_ASSETS";
export const ADD_ORDERS = "ADD_ORDERS";
export const ADD_TRANSACTIONS = "ADD_TRANSACTIONS";
export const ADD_PROCESSING = "ADD_PROCESSING";
export const REMOVE_PROCESSING = "REMOVE_PROCESSING";
export const NOT_PROCESSING = "NOT_PROCESSING";
export const PROCESSING = "PROCESSING";
export const SET_BASE_TOKEN = "SET_BASE_TOKEN";
export const SET_ERROR = "SET_ERROR";
export const SET_NETWORK = "SET_NETWORK";
Expand Down
18 changes: 6 additions & 12 deletions reducers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,24 @@ const initialState = {
address: null,
assets: [],
transactions: [],
processing: [],
txhash: null
processing: false
};

export default handleActions({
[Actions.ADD_ASSETS]: (state, action) => {
let assets = _.unionBy(action.payload, state.assets, "address");
return { ...state, assets };
},
[Actions.ADD_PROCESSING]: (state, action) => {
let processing = _.union(state.processing, action.payload);
return { ...state, processing };
[Actions.PROCESSING]: (state) => {
return { ...state, processing: true };
},
[Actions.NOT_PROCESSING]: (state) => {
return { ...state, processing: false };
},
[Actions.ADD_TRANSACTIONS]: (state, action) => {
let transactions = _.unionBy(action.payload, state.transactions, "id");
return { ...state, transactions };
},
[Actions.REMOVE_PROCESSING]: (state, action) => {
let processing = _.difference(state.processing, action.payload);
return { ...state, processing };
},
[Actions.SET_TRANSACTION_HASH]: (state, action) => {
return { ...state, txhash: action.payload };
},
[Actions.SET_WALLET]: (state, action) => {
let { network, wallet } = action.payload;
let privateKey = `0x${ethUtil.stripHexPrefix(wallet.getPrivateKey().toString("hex"))}`;
Expand Down
71 changes: 35 additions & 36 deletions thunks/relayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import BigNumber from "bignumber.js";
import moment from "moment";
import { NavigationActions } from "react-navigation";
import { setError } from "../actions";
import { loadTransactions, setTxHash } from "../thunks";
import { loadTransactions } from "../thunks";
import {
getZeroExClient,
getZeroExContractAddress,
getTokenAllowance,
getTokenByAddress,
guaranteeWETHAmount,
guaranteeWETHInWeiAmount,
setTokenUnlimitedAllowance,
isWETHAddress
Expand All @@ -24,8 +23,8 @@ import {
} from "../utils/orders";
import {
addOrders,
addProcessingOrders,
removeProcessingOrders,
notProcessing,
processing,
setOrders,
setProducts,
setBaseToken,
Expand Down Expand Up @@ -87,35 +86,35 @@ export function loadProductsAndTokens(force = false) {

export function createSignSubmitOrder(side, price, amount) {
return async (dispatch, getState) => {
let { wallet: { web3, address }, settings: { relayerEndpoint, quoteToken, baseToken } } = getState();
let zeroEx = await getZeroExClient(web3);
let relayerClient = new HttpClient(relayerEndpoint);
let order = {
...convertLimitOrderToZeroExOrder(quoteToken, baseToken, side, price, amount),
"maker": address.toLowerCase(quoteToken, baseToken, side, price, amount),
"makerFee": new BigNumber(0),
"taker": ZeroEx.NULL_ADDRESS,
"takerFee": new BigNumber(0),
"expirationUnixTimestampSec": new BigNumber(moment().unix() + 60*60*24),
"feeRecipient": ZeroEx.NULL_ADDRESS,
"salt": ZeroEx.generatePseudoRandomSalt(),
"exchangeContractAddress": await getZeroExContractAddress(web3)
};
let allowance = await getTokenAllowance(web3, order.makerTokenAddress);

try {
dispatch(processing());

let { wallet: { web3, address }, settings: { relayerEndpoint, quoteToken, baseToken } } = getState();
let zeroEx = await getZeroExClient(web3);
let relayerClient = new HttpClient(relayerEndpoint);
let order = {
...convertLimitOrderToZeroExOrder(quoteToken, baseToken, side, price, amount),
"maker": address.toLowerCase(quoteToken, baseToken, side, price, amount),
"makerFee": new BigNumber(0),
"taker": ZeroEx.NULL_ADDRESS,
"takerFee": new BigNumber(0),
"expirationUnixTimestampSec": new BigNumber(moment().unix() + 60*60*24),
"feeRecipient": ZeroEx.NULL_ADDRESS,
"salt": ZeroEx.generatePseudoRandomSalt(),
"exchangeContractAddress": await getZeroExContractAddress(web3)
};
let allowance = await getTokenAllowance(web3, order.makerTokenAddress);

// Make sure allowance is available.
if (order.makerTokenAmount.gt(allowance)) {
let txhash = await setTokenUnlimitedAllowance(web3, order.makerTokenAddress);
dispatch(setTxHash(txhash));
let receipt = await zeroEx.awaitTransactionMinedAsync(txhash);
}

// Guarantee WETH is available.
if ((await isWETHAddress(web3, order.makerTokenAddress))) {
let txhash = await guaranteeWETHInWeiAmount(web3, order.makerTokenAmount);
if (txhash) {
dispatch(setTxHash(txhash));
let receipt = await zeroEx.awaitTransactionMinedAsync(txhash);
}
}
Expand All @@ -130,67 +129,67 @@ export function createSignSubmitOrder(side, price, amount) {
} catch(err) {
await dispatch(setError(err));
} finally {
dispatch(setTxHash(null));
dispatch(notProcessing());
dispatch(NavigationActions.push({ routeName: "Trading" }));
}
};
}

export function cancelOrder(order) {
return async (dispatch, getState) => {
let { wallet: { web3, address } } = getState();
let zeroEx = await getZeroExClient(web3);

try {
dispatch(processing());

let { wallet: { web3, address } } = getState();
let zeroEx = await getZeroExClient(web3);

if (order.maker !== address) {
throw new Error("Cannot cancel order that is not yours");
}

let txhash = await cancelOrderUtil(web3, order, order.makerTokenAmount);
dispatch(setTxHash(txhash));
let receipt = await zeroEx.awaitTransactionMinedAsync(txhash);
} catch(err) {
dispatch(setError(err));
} finally {
dispatch(setTxHash(null));
dispatch(notProcessing());
dispatch(NavigationActions.push({ routeName: "Trading" }));
}
};
}

export function fillOrder(order) {
return async (dispatch, getState) => {
let { wallet: { web3 } } = getState();
let zeroEx = await getZeroExClient(web3);
let allowance = await getTokenAllowance(web3, order.takerTokenAddress);
let fillAmount = new BigNumber(order.takerTokenAmount);
dispatch(processing());

try {
let { wallet: { web3 } } = getState();
let zeroEx = await getZeroExClient(web3);
let allowance = await getTokenAllowance(web3, order.takerTokenAddress);
let fillAmount = new BigNumber(order.takerTokenAmount);

// Make sure allowance is available.
if (fillAmount.gt(allowance)) {
let txhash = await setTokenUnlimitedAllowance(web3, order.takerTokenAddress);
dispatch(setTxHash(txhash));
let receipt = await zeroEx.awaitTransactionMinedAsync(txhash);
}

// Guarantee WETH is available.
if ((await isWETHAddress(web3, order.takerTokenAddress))) {
let txhash = await guaranteeWETHInWeiAmount(web3, order.takerTokenAmount);
if (txhash) {
dispatch(setTxHash(txhash));
let receipt = await zeroEx.awaitTransactionMinedAsync(txhash);
}
}

let txhash = await fillOrderUtil(web3, order);
dispatch(setTxHash(txhash));
let receipt = await zeroEx.awaitTransactionMinedAsync(txhash);

dispatch(loadTransactions());
} catch(err) {
dispatch(setError(err));
} finally {
dispatch(setTxHash(null));
dispatch(notProcessing());
}
};
}
55 changes: 23 additions & 32 deletions thunks/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import ethUtil from "ethereumjs-util";
import {
addAssets,
addTransactions,
addProcessing,
notProcessing,
processing,
setError,
setWallet,
setTransactionHash
setWallet
} from "../actions";
import {
getBalance,
Expand Down Expand Up @@ -98,8 +98,8 @@ export function loadTransactions() {
}
};
let promises = [
fetch(`http://inf0x.com:9200/0x-kovan-fills/logs/_search?q=maker:${address}%20OR%20taker:${address}`, options),
fetch(`http://inf0x.com:9200/0x-kovan-cancels/logs/_search?q=maker:${address}%20OR%20taker:${address}`, options)
fetch(`http://inf0x.com:9200/0x-kovan-fills/logs/_search?q=maker:${address}%20OR%20taker:${address}&sort=timestamp:desc`, options),
fetch(`http://inf0x.com:9200/0x-kovan-cancels/logs/_search?q=maker:${address}%20OR%20taker:${address}&sort=timestamp:desc`, options)
];
let [ fills, cancels ] = await Promise.all(promises);
let fillsJSON = await fills.json();
Expand All @@ -124,11 +124,10 @@ export function loadTransactions() {

export function sendTokens(token, to, amount) {
return async (dispatch, getState) => {
let { wallet: { web3 } } = getState();
let zeroEx = await getZeroExClient(web3);
try {
dispatch(processing());

let txhash = await sendTokensUtil(web3, token, to, amount);
dispatch(setTxHash(txhash));
let receipt = await zeroEx.awaitTransactionMinedAsync(txhash);
await Promise.all([
dispatch(loadAssets(true)),
Expand All @@ -137,66 +136,58 @@ export function sendTokens(token, to, amount) {
} catch(err) {
await dispatch(setError(err));
} finally {
await dispatch(setTxHash(null));
dispatch(notProcessing());
}
};
}

export function sendEther(to, amount) {
return async (dispatch, getState) => {
let { wallet: { web3 } } = getState();
let zeroEx = await getZeroExClient(web3);
try {
dispatch(processing());

let { wallet: { web3 } } = getState();
let zeroEx = await getZeroExClient(web3);
let txhash = await sendEtherUtil(web3, to, amount);
dispatch(setTxHash(txhash));
let receipt = await zeroEx.awaitTransactionMinedAsync(txhash);
} catch(err) {
await dispatch(setError(err));
} finally {
await dispatch(setTxHash(null));
}
};
}

export function setTxHash(txhash) {
return (dispatch) => {
if (txhash) {
dispatch(addProcessing([txhash]));
dispatch(setTransactionHash(txhash));
} else {
dispatch(setTransactionHash(null));
dispatch(notProcessing());
}
};
}

export function wrapEther(amount) {
return async (dispatch, getState) => {
let { wallet: { web3 } } = getState();
let zeroEx = await getZeroExClient(web3);
try {
dispatch(processing());

let { wallet: { web3 } } = getState();
let zeroEx = await getZeroExClient(web3);
let txhash = await wrapEtherUtil(web3, amount);
dispatch(setTxHash(txhash));
let receipt = await zeroEx.awaitTransactionMinedAsync(txhash);
} catch(err) {
await dispatch(setError(err));
} finally {
await dispatch(setTxHash(null));
dispatch(notProcessing());
}
};
}

export function unwrapEther(amount) {
return async (dispatch, getState) => {
let { wallet: { web3 } } = getState();
let zeroEx = await getZeroExClient(web3);
try {
dispatch(processing());

let { wallet: { web3 } } = getState();
let zeroEx = await getZeroExClient(web3);
let txhash = await unwrapEtherUtil(web3, amount);
dispatch(setTxHash(txhash));
let receipt = await zeroEx.awaitTransactionMinedAsync(txhash);
} catch(err) {
await dispatch(setError(err));
} finally {
await dispatch(setTxHash(null));
dispatch(notProcessing());
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion utils/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ export function getImage(symbol) {
}

export function formatTimestamp(timestamp) {
return moment(timestamp).format("MMMM Do YYYY, h:mm:ss a");
return moment.unix(timestamp).format("MMMM Do YYYY, h:mm:ss a");
}

0 comments on commit b9aaac7

Please sign in to comment.