Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #83 from xavierlepretre/contract_factory_promise
Browse files Browse the repository at this point in the history
Suggested promisifying of a contract factory.
  • Loading branch information
Arachnid committed Mar 27, 2017
2 parents 2603c38 + f053c85 commit 9d0ffbf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
16 changes: 3 additions & 13 deletions test/fifsregistrar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,16 @@ describe('FIFSRegistrar', function() {
return utils.deployENSAsync(accounts[0])
.then(function(_ens) {
ens = _ens;
return new Promise(function(resolve, reject) {
web3.eth.contract(JSON.parse(registrarCode.interface)).new(
return utils.promisifyContractFactory(
web3.eth.contract(JSON.parse(registrarCode.interface)))
.newAsync(
ens.address,
0,
{
from: accounts[0],
data: registrarCode.bytecode,
gas: 4700000
},
function(err, contract) {
if (err) {
reject(err);
} else if (typeof contract.address !== "undefined") {
resolve(contract);
} else {
// There is to hope that reject or resolve is called
}
});

});
})
.then(contract => {
registrar = Promise.promisifyAll(contract);
Expand Down
21 changes: 20 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,24 @@ module.exports = {
});
},
web3: web3,
TestRPC: TestRPC
TestRPC: TestRPC,
promisifyContractFactory: function(contractFactory) {
contractFactory.newAsync = function() {
var args = arguments;
return new Promise(function(resolve, reject) {
args[args.length] = function(err, contract) {
if (err) {
reject(err);
} else if (typeof contract.address !== "undefined") {
resolve(contract);
} else {
// There is to hope that reject or resolve is called
}
};
args.length++;
contractFactory.new.apply(contractFactory, args);
});
};
return contractFactory;
}
};

0 comments on commit 9d0ffbf

Please sign in to comment.