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/src/components/buy_button.tsx | |
parent | 45d828e154b5400710582d1f5414f7446e8d3688 (diff) | |
download | dexon-0x-contracts-acefeff5f08f123c7c5d372088f2af016e31c599.tar.gz dexon-0x-contracts-acefeff5f08f123c7c5d372088f2af016e31c599.tar.zst dexon-0x-contracts-acefeff5f08f123c7c5d372088f2af016e31c599.zip |
new try/catch pattern and new prop names per code review feedback
Diffstat (limited to 'packages/instant/src/components/buy_button.tsx')
-rw-r--r-- | packages/instant/src/components/buy_button.tsx | 33 |
1 files changed, 24 insertions, 9 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); }; } |