diff options
author | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-10-26 04:18:17 +0800 |
---|---|---|
committer | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-10-26 04:18:17 +0800 |
commit | acefeff5f08f123c7c5d372088f2af016e31c599 (patch) | |
tree | a6c30ee817baac36df47a84402e671fcf7595a19 /packages/instant | |
parent | 45d828e154b5400710582d1f5414f7446e8d3688 (diff) | |
download | dexon-sol-tools-acefeff5f08f123c7c5d372088f2af016e31c599.tar.gz dexon-sol-tools-acefeff5f08f123c7c5d372088f2af016e31c599.tar.zst dexon-sol-tools-acefeff5f08f123c7c5d372088f2af016e31c599.zip |
new try/catch pattern and new prop names per code review feedback
Diffstat (limited to 'packages/instant')
-rw-r--r-- | packages/instant/src/components/buy_button.tsx | 33 | ||||
-rw-r--r-- | packages/instant/src/containers/selected_asset_buy_button.ts | 8 |
2 files changed, 28 insertions, 13 deletions
diff --git a/packages/instant/src/components/buy_button.tsx b/packages/instant/src/components/buy_button.tsx index f70f2f5be..7004b6a18 100644 --- a/packages/instant/src/components/buy_button.tsx +++ b/packages/instant/src/components/buy_button.tsx @@ -12,10 +12,10 @@ export interface BuyButtonProps { buyQuote?: BuyQuote; assetBuyer?: AssetBuyer; onAwaitingSignature: (buyQuote: BuyQuote) => void; - onProcessingTransaction: (buyQuote: BuyQuote, txnHash: string) => void; + onSignatureDenied: (buyQuote: BuyQuote, preventedError: Error) => void; + onBuyProcessing: (buyQuote: BuyQuote, txnHash: string) => void; onBuySuccess: (buyQuote: BuyQuote, txnHash: string) => void; - onBuyFailure: (buyQuote: BuyQuote, tnxHash?: string) => void; - onBuyPrevented: (buyQuote: BuyQuote, preventedError: Error) => void; + onBuyFailure: (buyQuote: BuyQuote, txnHash?: string) => void; // TODO: make this non-optional } export class BuyButton extends React.Component<BuyButtonProps> { @@ -40,19 +40,34 @@ export class BuyButton extends React.Component<BuyButtonProps> { if (_.isUndefined(buyQuote) || _.isUndefined(assetBuyer)) { return; } + + let txnHash: string | undefined; this.props.onAwaitingSignature(buyQuote); - let txnHash; try { txnHash = await assetBuyer.executeBuyQuoteAsync(buyQuote); - this.props.onProcessingTransaction(buyQuote, txnHash); - await web3Wrapper.awaitTransactionSuccessAsync(txnHash); - this.props.onBuySuccess(buyQuote, txnHash); } catch (e) { if (e instanceof Error && e.message === AssetBuyerError.SignatureRequestDenied) { - this.props.onBuyPrevented(buyQuote, e); + this.props.onSignatureDenied(buyQuote, e); + } else { + throw e; + } + } + + // Have to let TS know that txHash is definitely defined now + if (!txnHash) { + throw new Error('No txHash available'); + } + + this.props.onBuyProcessing(buyQuote, txnHash); + try { + await web3Wrapper.awaitTransactionSuccessAsync(txnHash); + } catch (e) { + if (e instanceof Error && e.message.startsWith('Transaction failed')) { + this.props.onBuyFailure(buyQuote, txnHash); return; } - this.props.onBuyFailure(buyQuote, txnHash); + throw e; } + this.props.onBuySuccess(buyQuote, txnHash); }; } diff --git a/packages/instant/src/containers/selected_asset_buy_button.ts b/packages/instant/src/containers/selected_asset_buy_button.ts index f55254daf..17cc1a770 100644 --- a/packages/instant/src/containers/selected_asset_buy_button.ts +++ b/packages/instant/src/containers/selected_asset_buy_button.ts @@ -19,10 +19,10 @@ interface ConnectedState { interface ConnectedDispatch { onAwaitingSignature: (buyQuote: BuyQuote) => void; - onProcessingTransaction: (buyQuote: BuyQuote, txnHash: string) => void; + onSignatureDenied: (buyQuote: BuyQuote, error: Error) => void; + onBuyProcessing: (buyQuote: BuyQuote, txnHash: string) => void; onBuySuccess: (buyQuote: BuyQuote, txnHash: string) => void; onBuyFailure: (buyQuote: BuyQuote) => void; - onBuyPrevented: (buyQuote: BuyQuote, error: Error) => void; } const mapStateToProps = (state: State, _ownProps: SelectedAssetBuyButtonProps): ConnectedState => ({ @@ -35,14 +35,14 @@ const mapDispatchToProps = (dispatch: Dispatch<Action>, ownProps: SelectedAssetB const newOrderState: OrderState = { processState: OrderProcessState.AWAITING_SIGNATURE }; dispatch(actions.updateBuyOrderState(newOrderState)); }, - onProcessingTransaction: (buyQuote: BuyQuote, txnHash: string) => { + onBuyProcessing: (buyQuote: BuyQuote, txnHash: string) => { const newOrderState: OrderState = { processState: OrderProcessState.PROCESSING, txnHash }; dispatch(actions.updateBuyOrderState(newOrderState)); }, onBuySuccess: (buyQuote: BuyQuote, txnHash: string) => dispatch(actions.updateBuyOrderState({ processState: OrderProcessState.SUCCESS, txnHash })), onBuyFailure: buyQuote => dispatch(actions.updateBuyOrderState({ processState: OrderProcessState.FAILURE })), - onBuyPrevented: (buyQuote, error) => { + onSignatureDenied: (buyQuote, error) => { dispatch(actions.resetAmount()); dispatch(actions.setError(error)); }, |