In @0x/base-contract: Expose txHashPromise in PromiseWithTransactionHash.

In `@0x/abi-gen-templates`: Update `awaitTransactionSuccessAsync()` partial to expose `txHashPromise`.
This commit is contained in:
Lawrence Forman 2019-04-26 20:48:45 -04:00
parent 4d493eeebd
commit fddad131a2
2 changed files with 23 additions and 17 deletions

View File

@ -25,7 +25,7 @@ public {{this.tsName}} = {
const txHash = await self._web3Wrapper.sendTransactionAsync(txDataWithDefaults); const txHash = await self._web3Wrapper.sendTransactionAsync(txDataWithDefaults);
return txHash; return txHash;
}, },
async awaitTransactionSuccessAsync( awaitTransactionSuccessAsync(
{{> typed_params inputs=inputs}} {{> typed_params inputs=inputs}}
{{#this.payable}} {{#this.payable}}
txData?: Partial<TxDataPayable> | number, txData?: Partial<TxDataPayable> | number,
@ -36,26 +36,30 @@ public {{this.tsName}} = {
pollingIntervalMs?: number, pollingIntervalMs?: number,
timeoutMs?: number, timeoutMs?: number,
): PromiseWithTransactionHash<TransactionReceiptWithDecodedLogs> { ): PromiseWithTransactionHash<TransactionReceiptWithDecodedLogs> {
// `txData` is optional, so it might be set to `pollingIntervalMs`. // `txData` may be omitted on its own, so it might be set to `pollingIntervalMs`.
if (typeof(txData) === 'number') { if (typeof(txData) === 'number') {
pollingIntervalMs = txData; pollingIntervalMs = txData;
timeoutMs = pollingIntervalMs; timeoutMs = pollingIntervalMs;
txData = {}; txData = {};
} }
//
const self = this as any as {{contractName}}Contract; const self = this as any as {{contractName}}Contract;
{{#if inputs}} {{#if inputs}}
const txHash = await self.{{this.tsName}}.sendTransactionAsync({{> params input=inputs}}, txData); const txHashPromise = self.{{this.tsName}}.sendTransactionAsync({{> params input=inputs}}, txData);
{{else}} {{else}}
const txHash = await self.{{this.tsName}}.sendTransactionAsync(txData); const txHashPromise = self.{{this.tsName}}.sendTransactionAsync(txData);
{{/if}} {{/if}}
// tslint:disable-next-line: no-unnecessary-type-assertion return new PromiseWithTransactionHash<TransactionReceiptWithDecodedLogs>(
const receiptPromise = self._web3Wrapper.awaitTransactionSuccessAsync( txHashPromise,
txHash, (async (): Promise<TransactionReceiptWithDecodedLogs> => {
pollingIntervalMs, // When the transaction hash resolves, wait for it to be mined.
timeoutMs, return self._web3Wrapper.awaitTransactionSuccessAsync(
) as any as PromiseWithTransactionHash<TransactionReceiptWithDecodedLogs>; await txHashPromise,
receiptPromise.txHash = txHash; pollingIntervalMs,
return receiptPromise; timeoutMs,
);
})(),
);
}, },
async estimateGasAsync( async estimateGasAsync(
{{> typed_params inputs=inputs}} {{> typed_params inputs=inputs}}

View File

@ -26,14 +26,16 @@ export interface AbiEncoderByFunctionSignature {
* Not used by BaseContract, but generated contracts will return it in * Not used by BaseContract, but generated contracts will return it in
* `awaitTransactionSuccessAsync()`. * `awaitTransactionSuccessAsync()`.
* Maybe there's a better place for this. * Maybe there's a better place for this.
* If you're wondering why this class is not simpler, it's to get around
* Typescript/ES5 transpiling issues.
*/ */
export class PromiseWithTransactionHash<T> implements PromiseLike<T> { export class PromiseWithTransactionHash<T> implements PromiseLike<T> {
public txHash: string = ''; public readonly txHashPromise: Promise<string>;
private readonly _promise: Promise<T>; private readonly _promise: Promise<T>;
constructor(handler: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) { constructor(
this._promise = new Promise(handler); txHashPromise: Promise<string>,
promise: Promise<T>,
) {
this.txHashPromise = txHashPromise;
this._promise = promise;
} }
public then<TResult>( public then<TResult>(
onFulfilled?: (v: T) => TResult | PromiseLike<TResult>, onFulfilled?: (v: T) => TResult | PromiseLike<TResult>,