Skip to content

Commit

Permalink
Fix dapp initiated transactions (#434)
Browse files Browse the repository at this point in the history
* fix dapp initiated transactions

* update snapshot
  • Loading branch information
Bruno Barbieri committed Feb 24, 2019
1 parent 69cd64d commit 09505e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
20 changes: 13 additions & 7 deletions app/components/Views/Approval/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getTransactionOptionsTitle } from '../../UI/Navbar';
import { colors } from '../../../styles/common';
import { newTransaction, setTransactionObject } from '../../../actions/transaction';
import { connect } from 'react-redux';
import { toChecksumAddress } from 'ethereumjs-util';

const styles = StyleSheet.create({
wrapper: {
Expand Down Expand Up @@ -44,8 +45,6 @@ class Approval extends Component {
};

componentWillUnmount = () => {
const { transaction } = this.props;
Engine.context.TransactionController.cancelTransaction(transaction.id);
this.clear();
};

Expand All @@ -57,6 +56,8 @@ class Approval extends Component {
};

onCancel = () => {
const { transaction } = this.props;
Engine.context.TransactionController.cancelTransaction(transaction.id);
this.props.navigation.goBack();
};

Expand All @@ -65,18 +66,21 @@ class Approval extends Component {
*/
onConfirm = async () => {
const { TransactionController } = Engine.context;
const { transactions } = this.props;
let { transaction } = this.props;
try {
transaction = this.prepareTransaction(transaction);
TransactionController.hub.once(`${transaction.id}:finished`, transactionMeta => {
if (transactionMeta.status === 'submitted') {
const hash = transactionMeta.transactionHash;
this.props.navigation.push('TransactionSubmitted', { hash });
this.props.navigation.goBack();
} else {
throw transactionMeta.error;
}
});
await TransactionController.updateTransaction({ id: transaction.id, transaction });

const fullTx = transactions.find(({ id }) => id === transaction.id);
const updatedTx = { ...fullTx, transaction };
await TransactionController.updateTransaction(updatedTx);
await TransactionController.approveTransaction(transaction.id);
} catch (error) {
Alert.alert('Transaction error', JSON.stringify(error), [{ text: 'OK' }]);
Expand All @@ -97,7 +101,8 @@ class Approval extends Component {
...transaction,
gas: BNToHex(transaction.gas),
gasPrice: BNToHex(transaction.gasPrice),
value: BNToHex(transaction.value)
value: BNToHex(transaction.value),
to: toChecksumAddress(transaction.to)
});

/**
Expand Down Expand Up @@ -140,7 +145,8 @@ class Approval extends Component {
}

const mapStateToProps = state => ({
transaction: state.transaction
transaction: state.transaction,
transactions: state.engine.backgroundState.TransactionController.transactions
});

const mapDispatchToProps = dispatch => ({
Expand Down
7 changes: 7 additions & 0 deletions app/components/Views/Approval/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ describe('Approval', () => {
to: '0x2',
selectedAsset: undefined,
assetType: undefined
},
engine: {
backgroundState: {
TransactionController: {
transactions: []
}
}
}
};

Expand Down

0 comments on commit 09505e4

Please sign in to comment.