Skip to content

Commit

Permalink
Added confirmations to TransactionResponse (#156, #238).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Oct 4, 2018
1 parent 731f189 commit 9797b36
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src.ts/providers/abstract-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export interface TransactionResponse extends Transaction {
blockHash?: string,
timestamp?: number,

confirmations: number,

// Not optional (as it is in Transaction)
from: string;

Expand Down
68 changes: 65 additions & 3 deletions src.ts/providers/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ const formatTransaction = {
blockNumber: allowNull(checkNumber, null),
transactionIndex: allowNull(checkNumber, null),

confirmations: allowNull(checkNumber, null),

from: getAddress,

gasPrice: bigNumberify,
Expand Down Expand Up @@ -236,7 +238,7 @@ function checkTransactionResponse(transaction: any): TransactionResponse {
if (typeof(networkId) !== 'number') { networkId = 0; }

result.networkId = networkId;

// 0x0000... should actually be null
if (result.blockHash && result.blockHash.replace(/0/g, '') === 'x') {
result.blockHash = null;
Expand Down Expand Up @@ -377,6 +379,7 @@ function checkLog(log: any): any {
return check(formatLog, log);
}


//////////////////////////////
// Event Serializeing

Expand Down Expand Up @@ -438,6 +441,13 @@ function getEventTag(eventName: EventType): string {
throw new Error('invalid event - ' + eventName);
}

//////////////////////////////
// Helper Object

function getTime() {
return (new Date()).getTime();
}

//////////////////////////////
// Provider Object

Expand Down Expand Up @@ -473,6 +483,10 @@ export class BaseProvider extends Provider {
// string => BigNumber
private _balances: any;

private _fastBlockNumber: number;
private _fastBlockNumberPromise: Promise<number>;
private _fastQueryDate: number;


/**
* ready
Expand Down Expand Up @@ -521,10 +535,13 @@ export class BaseProvider extends Provider {
// until we get a response. This provides devs with a consistent view. Similarly for
// transaction hashes.
this._emitted = { block: this._lastBlockNumber };

this._fastQueryDate = 0;
}

private _doPoll(): void {
this.getBlockNumber().then((blockNumber) => {
this._setFastBlockNumber(blockNumber);

// If the block hasn't changed, meh.
if (blockNumber === this._lastBlockNumber) { return; }
Expand Down Expand Up @@ -665,6 +682,38 @@ export class BaseProvider extends Provider {
}
}

_getFastBlockNumber(): Promise<number> {
let now = getTime();

// Stale block number, request a newer value
if ((now - this._fastQueryDate) > 2 * this._pollingInterval) {
console.log('re-poll fbn');
this._fastQueryDate = now;
this._fastBlockNumberPromise = this.getBlockNumber().then((blockNumber) => {
if (blockNumber > this._fastBlockNumber) {
this._fastBlockNumber = blockNumber;
}
return this._fastBlockNumber;
});
}

return this._fastBlockNumberPromise;
}

_setFastBlockNumber(blockNumber: number): void {
// Older block, maybe a stale request
if (blockNumber < this._fastBlockNumber) { return; }

// Update the time we updated the blocknumber
this._fastQueryDate = getTime();

// Newer block number, use it
if (blockNumber > this._fastBlockNumber) {
this._fastBlockNumber = blockNumber;
this._fastBlockNumberPromise = Promise.resolve(blockNumber);
}
}

// @TODO: Add .poller which must be an event emitter with a 'start', 'stop' and 'block' event;
// this will be used once we move to the WebSocket or other alternatives to polling

Expand All @@ -680,8 +729,9 @@ export class BaseProvider extends Provider {
getBlockNumber(): Promise<number> {
return this.ready.then(() => {
return this.perform('getBlockNumber', { }).then((result) => {
var value = parseInt(result);
let value = parseInt(result);
if (value != result) { throw new Error('invalid response - getBlockNumber'); }
this._setFastBlockNumber(value);
return value;
});
});
Expand Down Expand Up @@ -892,7 +942,19 @@ export class BaseProvider extends Provider {
}
return undefined;
}
return BaseProvider.checkTransactionResponse(result);

let seq = Promise.resolve();
if (result.blockNumber != null && result.confirmations == null) {
seq = this._getFastBlockNumber().then((blockNumber) => {
let confirmations = (blockNumber - result.blockNumber) + 1;
if (confirmations <= 0) { confirmations = 1; }
result.confirmations = confirmations;
});
}

return seq.then(() => {
return BaseProvider.checkTransactionResponse(result);
});
});
}, { onceBlock: this });
});
Expand Down

0 comments on commit 9797b36

Please sign in to comment.