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

View File

@ -26,14 +26,16 @@ export interface AbiEncoderByFunctionSignature {
* Not used by BaseContract, but generated contracts will return it in
* `awaitTransactionSuccessAsync()`.
* 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> {
public txHash: string = '';
public readonly txHashPromise: Promise<string>;
private readonly _promise: Promise<T>;
constructor(handler: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) {
this._promise = new Promise(handler);
constructor(
txHashPromise: Promise<string>,
promise: Promise<T>,
) {
this.txHashPromise = txHashPromise;
this._promise = promise;
}
public then<TResult>(
onFulfilled?: (v: T) => TResult | PromiseLike<TResult>,