-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- automine and Instant Seal support added - default value of transactionConfirmationBlocks changed to 0 - TransactionObserver splitted up to HttpTransactionObserver and SocketTransactionObserver - Property handling for the Ens module fixed
- Loading branch information
Showing
39 changed files
with
1,688 additions
and
630 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/web3-core-method/lib/observers/AbstractTransactionObserver.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/** | ||
* @file AbstractTransactionObserver.js | ||
* @author Samuel Furter <samuel@ethereum.org> | ||
* @author Josh Stevens <joshstevens19@hotmail.co.uk> | ||
* @date 2019 | ||
*/ | ||
|
||
export default class AbstractTransactionObserver { | ||
/** | ||
* @param {AbstractSocketProvider|HttpProvider|CustomProvider} provider | ||
* @param {Number} timeout | ||
* @param {Number} blockConfirmations | ||
* @param {GetTransactionReceiptMethod} getTransactionReceiptMethod | ||
* | ||
* @constructor | ||
*/ | ||
constructor(provider, timeout, blockConfirmations, getTransactionReceiptMethod) { | ||
this.provider = provider; | ||
this.timeout = timeout; | ||
this.blockConfirmations = blockConfirmations; | ||
this.getTransactionReceiptMethod = getTransactionReceiptMethod; | ||
|
||
this.confirmations = 0; | ||
this.confirmationChecks = 0; | ||
} | ||
|
||
/** | ||
* Observes the transaction by the given transactionHash | ||
* | ||
* @method observe | ||
* | ||
* @param {String} transactionHash | ||
* | ||
* @returns {Observable} | ||
*/ | ||
observe(transactionHash) {} | ||
|
||
/** | ||
* Calls the next callback method of the Observer | ||
* | ||
* @method emitNext | ||
* | ||
* @param {Object} receipt | ||
* @param {Observer} observer | ||
*/ | ||
emitNext(receipt, observer) { | ||
observer.next({receipt, confirmations: this.confirmations}); | ||
} | ||
|
||
/** | ||
* Calls the error callback method of the Observer | ||
* | ||
* @method emitError | ||
* | ||
* @param {Error} error | ||
* @param {Object} receipt | ||
* @param {Observer} observer | ||
*/ | ||
emitError(error, receipt, observer) { | ||
observer.error({ | ||
error, | ||
receipt, | ||
confirmations: this.confirmations, | ||
confirmationChecks: this.confirmationChecks | ||
}); | ||
} | ||
|
||
/** | ||
* Checks if enough confirmations happened | ||
* | ||
* @method isConfirmed | ||
* | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
isConfirmed() { | ||
return this.confirmations === this.blockConfirmations; | ||
} | ||
|
||
/** | ||
* Checks if the timeout time is reached | ||
* | ||
* @method isTimeoutTimeExceeded | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
isTimeoutTimeExceeded() { | ||
return this.confirmationChecks === this.timeout; | ||
} | ||
|
||
/** | ||
* Returns the transaction receipt | ||
* | ||
* @method getTransactionReceipt | ||
* | ||
* @param {String} transactionHash | ||
* | ||
* @returns {Promise<Object|null>} | ||
*/ | ||
getTransactionReceipt(transactionHash) { | ||
this.getTransactionReceiptMethod.parameters = [transactionHash]; | ||
|
||
return this.getTransactionReceiptMethod.execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
packages/web3-core-method/src/observers/HttpTransactionObserver.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/** | ||
* @file TransactionHttpObserver.js | ||
* @author Samuel Furter <samuel@ethereum.org> | ||
* @author Josh Stevens <joshstevens19@hotmail.co.uk> | ||
* @date 2019 | ||
*/ | ||
|
||
import {Observable} from 'rxjs'; | ||
import AbstractTransactionObserver from '../../lib/observers/AbstractTransactionObserver'; | ||
|
||
export default class HttpTransactionObserver extends AbstractTransactionObserver { | ||
/** | ||
* @param {AbstractSocketProvider|HttpProvider|CustomProvider} provider | ||
* @param {Number} timeout | ||
* @param {Number} blockConfirmations | ||
* @param {GetTransactionReceiptMethod} getTransactionReceiptMethod | ||
* @param {GetBlockByNumberMethod} getBlockByNumberMethod | ||
* | ||
* @constructor | ||
*/ | ||
constructor(provider, timeout, blockConfirmations, getTransactionReceiptMethod, getBlockByNumberMethod) { | ||
super(provider, timeout, blockConfirmations, getTransactionReceiptMethod); | ||
|
||
this.getBlockByNumberMethod = getBlockByNumberMethod; | ||
this.lastBlock = false; | ||
} | ||
|
||
/** | ||
* Observes the transaction by the given transactionHash | ||
* | ||
* @method observe | ||
* | ||
* @param {String} transactionHash | ||
* | ||
* @returns {Observable} | ||
*/ | ||
observe(transactionHash) { | ||
return Observable.create((observer) => { | ||
this.getTransactionReceipt(transactionHash) | ||
.then((receipt) => { | ||
if (this.blockConfirmations === 0) { | ||
if (receipt) { | ||
this.emitNext(receipt, observer); | ||
observer.complete(); | ||
|
||
return; | ||
} | ||
|
||
this.emitError( | ||
new Error( | ||
'No transaction receipt found! Increase the transactionConfirmationBlocks property or be sure automine is activated in your development environment.' | ||
), | ||
false, | ||
observer | ||
); | ||
|
||
return; | ||
} | ||
|
||
const interval = setInterval(async () => { | ||
receipt = await this.getTransactionReceipt(transactionHash); | ||
|
||
if (observer.closed) { | ||
clearInterval(interval); | ||
|
||
return; | ||
} | ||
|
||
// on parity nodes you can get the receipt without it being mined | ||
// so the receipt may not have a block number at this point | ||
if (receipt && receipt.blockNumber) { | ||
if (this.lastBlock) { | ||
const block = await this.getBlockByNumber(this.lastBlock.number + 1); | ||
if (block) { | ||
this.lastBlock = block; | ||
this.confirmations++; | ||
this.emitNext(receipt, observer); | ||
} | ||
} else { | ||
this.lastBlock = await this.getBlockByNumber(receipt.blockNumber); | ||
this.confirmations++; | ||
this.emitNext(receipt, observer); | ||
} | ||
|
||
if (this.isConfirmed()) { | ||
observer.complete(); | ||
clearInterval(interval); | ||
} | ||
} | ||
|
||
this.confirmationChecks++; | ||
|
||
if (this.isTimeoutTimeExceeded()) { | ||
clearInterval(interval); | ||
|
||
this.emitError( | ||
new Error( | ||
'Timeout exceeded during the transaction confirmation process. Be aware the transaction could still get confirmed!' | ||
), | ||
receipt, | ||
observer | ||
); | ||
} | ||
}, 1000); | ||
}) | ||
.catch((error) => { | ||
this.emitError(error, false, observer); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Returns a block by the given blockNumber | ||
* | ||
* @method getBlockByNumber | ||
* | ||
* @param {String} blockNumber | ||
* | ||
* @returns {Promise<Object>} | ||
*/ | ||
getBlockByNumber(blockNumber) { | ||
this.getBlockByNumberMethod.parameters = [blockNumber]; | ||
|
||
return this.getBlockByNumberMethod.execute(); | ||
} | ||
} |
Oops, something went wrong.