ethereum-web3-plus
Advanced tools
Comparing version 0.2.1 to 0.2.2
59
index.js
@@ -121,2 +121,6 @@ | ||
var isArray = function (object) { | ||
return object instanceof Array; | ||
}; | ||
var isFunction = function (object) { | ||
@@ -142,3 +146,4 @@ return typeof object === 'function'; | ||
if( opt.canonicalAfter ) options.canonicalAfter=opt.canonicalAfter; | ||
if( options.dropAfter ) options.dropAfter=opt.dropAfter; | ||
if( opt.dropAfter ) options.dropAfter=opt.dropAfter; | ||
if( opt.groupId ) options.groupId=opt.groupId; | ||
} | ||
@@ -152,2 +157,3 @@ return options; | ||
this.web3 = web3 | ||
this.tx_group={}; // dictionnary with group of tx in the form uid : {txhash1:true, txhash2:true, count:2} | ||
this.tx_wait={}; // dictionary txHash => {gas: <requested gas>, args: [p1, p2], cb:<callback>} | ||
@@ -178,3 +184,10 @@ this.filter; | ||
var txwait = self.tx_wait[tx]; | ||
var txgroup = self.tx_group; | ||
// to protect against a case where the receipt would not be loaded while available, load it if 2 blocks have passed | ||
if(!txwait.receipt && block.number >= txwait.startBlock + 2) { | ||
txwait.receipt=self.web3.eth.getTransactionReceipt(tx); | ||
if(txwait.receipt) txwait.startBlock=txwait.receipt.blockNumber; // update the startBlock | ||
} | ||
// process the normal situation where the tx is mined and the tx is cannonical | ||
if(txwait.receipt && block.number >= txwait.startBlock + txwait.canonicalAfter) { | ||
@@ -191,8 +204,17 @@ | ||
&& self.web3.eth.getCode(receipt.contractAddress)=="0x") | ||
cb_args.push("created contract has no bytecodes"); | ||
cb_args.push("created contract has no bytecodes"); | ||
else cb_args.push(null); // No error | ||
// remove the txHash from the wait dictionary | ||
delete self.tx_wait[tx]; | ||
// manage the case of a group | ||
if(txwait.group && txgroup[txwait.group] && txgroup[txwait.group][tx]) { | ||
txgroup[txwait.group].count--; // reduce the number of tx in the group | ||
delete txgroup[txwait.group][tx]; // remove the tx from the group | ||
cb_args.push(txgroup[txwait.group].count); // adds the nb of remaining tx in the group to the callback args | ||
if(txgroup[txwait.group].count==0) delete txgroup[txwait.group]; // remove the group when done | ||
} | ||
// call the callback | ||
callback.apply(null, cb_args); | ||
// process the case where the receipt is not available and we have waited for enough block to consider it dead | ||
} else if(block.number >= self.tx_wait[tx].startBlock+self.tx_wait[tx].dropAfter) { | ||
@@ -207,2 +229,9 @@ console.log("transaction watched timeout", txHash, "after",self.tx_wait[tx].dropAfter,"blocks"); | ||
delete self.tx_wait[tx]; | ||
// manage the case of a group | ||
if(txwait.group && txgroup[txwait.group] && txgroup[txwait.group][tx]) { | ||
txgroup[txwait.group].count--; // reduce the number of tx in the group | ||
delete txgroup[txwait.group][tx]; // remove the tx from the group | ||
cb_args.push(txgroup[txwait.group].count); // adds the nb of remaining tx in the group to the callback args | ||
if(txgroup[txwait.group].count==0) delete txgroup[txwait.group]; // remove the group when done | ||
} | ||
// call the callback | ||
@@ -230,3 +259,10 @@ callback.apply(null, cb_args); | ||
dropAfter:options.dropAfter || 99999999 }; | ||
console.log("Transaction",txHash, "added to the watch"); | ||
if(options.groupId) { | ||
if(!this.tx_group[options.groupId]) this.tx_group[options.groupId]={count:0}; // init the group | ||
var group=this.tx_group[options.groupId]; | ||
group[txHash]=true; // add the tx to the group | ||
group.count++; // increase by one. | ||
this.tx_wait[txHash].group=options.groupId; // record the link to the group | ||
} | ||
console.log("Transaction",txHash, "added to the watch", (options.groupId?"in group":"")); | ||
return txHash; | ||
@@ -254,2 +290,3 @@ } | ||
var txHash = args.shift() ; // first param is the tx Hash | ||
if(isArray(txHash)) return this.waitForAll.apply(this, arguments); | ||
var tx=this.eth.getTransaction(this.toHex(txHash)); | ||
@@ -272,3 +309,19 @@ if(!tx) { // the first argument is not an existing txHash !! | ||
} | ||
Web3.prototype.waitForAll = function() { | ||
var args=Array.prototype.slice.call(arguments); | ||
var options = extractOptions(args); | ||
var callback = extractCallback(args); | ||
if(!callback) return false; // no point in doing something there is no callback | ||
var txHashs = args.shift() ; // first param is the tx Hash array | ||
if(!isArray(txHashs)) {console.log("waitForAll expects an array of txHash"); return null;} | ||
if(txHashs.length<1) {console.log("waitForAll expects a non empty array of txHash"); return null;} | ||
options.groupId = txHashs[0]; | ||
args.push(callback); | ||
args.push(options); | ||
var result=[]; | ||
for(var i=0; i<txHashs.length; i++) result.push(this.waitFor.apply(this,[txHashs[i]].concat(args) )); | ||
return result; | ||
} | ||
Web3.prototype.BlockWatcherStart = function(bindEnvironment) { | ||
@@ -275,0 +328,0 @@ // in case no environment binding is provided, use an identity one |
{ | ||
"name": "ethereum-web3-plus", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Adds some simplifications to the web3 package such as compilation, instance creation, call sequencing", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,3 +6,3 @@ # Documentation page for ethereum-web3-plus | ||
- The Solidity compilation simplified with automatic linkeage of libraries and contract deployment simplification | ||
- A block watcher and a waitFor function to get notified when a submitted transaction is completed and considered canonical. | ||
- A block watcher and a waitFor (and waitForAll from version 0.2.2)function to get notified when a submitted transaction is completed and considered canonical. | ||
Note that in this version the solidity compiler is the one loaded by the geth node. | ||
@@ -60,3 +60,3 @@ | ||
compiler.compile("./sources/example.sol"); // path either absolute or relative to geth running node. | ||
compiler.compileInline("contract MyContract {uint i;}"); // from version 0.2.1, allow to compile source from a string. If import are required, specify them in the inline code with same path constraints as above. | ||
compiler.displaySizes(); // display all contract found and compiled and their code size (important to check they are as small as possible) | ||
@@ -107,2 +107,3 @@ compiler.displayMissingLibs(); // display libraries that have not been found in the deployed libs and that are required. | ||
``` | ||
waitFor returns the txHash if successfully registered in the watcher and in case of error, the callback is called and the return of that callback (if any) is returned. | ||
waitFor takes the following parameters: | ||
@@ -122,2 +123,19 @@ ``` | ||
waitForAll (from version 0.2.2) works like waitFor except that it takes an array of txHash as first parameter | ||
```js | ||
var E = web3.instanceAt("Example", contract); // is a compiled contract | ||
var tx=[]; | ||
tx.push(E.function1({gas:200000}); | ||
tx.push(E.function2({gas:100000}); | ||
tx.push(E.function3({gas:300000}); | ||
web3.waitForAll(tx, "Your param", | ||
function(tx, p1, contract, err, remaining) { | ||
console.log("callback:", tx, p1, contract, err, "remaining txs:", remaining); | ||
if(remaining==0) console.log("All tx have been mined. Proceed to something else"); | ||
}, {canonicalAfter:5 } ); | ||
``` | ||
The function returns an array with the result of waitFor for each txHash. | ||
The callback passed to waitForAll is expected to receive an additional parameter being the number of remaining transactions waiting to be mined after this one. Note that the same callback is called once for each transaction in the array. | ||
[npm-image]: https://badge.fury.io/js/web3.png | ||
@@ -124,0 +142,0 @@ [npm-url]: https://npmjs.org/package/web3 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22331
292
150