Fixed getDeployTransaction with ABI objects and added support for indexed parameters in event logs.

This commit is contained in:
ricmoo 2017-05-09 21:48:56 -04:00
parent 4b41c0e1a2
commit 1531793084
3 changed files with 59 additions and 10 deletions

View File

@ -147,8 +147,19 @@ function Contract(address, contractInterface, signerOrProvider) {
var noncePromise = null; var noncePromise = null;
if (transaction.nonce) { if (transaction.nonce) {
noncePromise = Promise.resolve(transaction.nonce) noncePromise = Promise.resolve(transaction.nonce)
} else if (signer.getTransactionCount) {
noncePromise = signer.getTransactionCount;
if (!(noncePromise instanceof Promise)) {
noncePromise = Promise.resolve(noncePromise);
}
} else { } else {
noncePromise = provider.getTransactionCount(signer.address, 'pending'); var addressPromise = signer.getAddress();
if (!(addressPromise instanceof Promise)) {
addressPromise = Promise.resolve(addressPromise);
}
noncePromise = addressPromise.then(function(address) {
return provider.getTransactionCount(address, 'pending');
});
} }
var gasPricePromise = null; var gasPricePromise = null;
@ -207,7 +218,7 @@ function Contract(address, contractInterface, signerOrProvider) {
function handleEvent(log) { function handleEvent(log) {
try { try {
var result = eventInfo.parse(log.data); var result = eventInfo.parse(log.topics, log.data);
eventCallback.apply(log, Array.prototype.slice.call(result)); eventCallback.apply(log, Array.prototype.slice.call(result));
} catch (error) { } catch (error) {
console.log(error); console.log(error);
@ -244,7 +255,8 @@ function Contract(address, contractInterface, signerOrProvider) {
} }
utils.defineProperty(Contract, 'getDeployTransaction', function(bytecode, contractInterface) { utils.defineProperty(Contract, 'getDeployTransaction', function(bytecode, contractInterface) {
if (typeof(contractInterface) === 'string') {
if (!(contractInterface instanceof Interface)) {
contractInterface = new Interface(contractInterface); contractInterface = new Interface(contractInterface);
} }

View File

@ -432,7 +432,6 @@ function Interface(abi) {
case 'event': case 'event':
var func = (function() { var func = (function() {
var inputTypes = getKeys(method.inputs, 'type'); var inputTypes = getKeys(method.inputs, 'type');
var inputNames = getKeys(method.inputs, 'name', true);
var func = function() { var func = function() {
var signature = method.name + '(' + getKeys(method.inputs, 'type').join(',') + ')'; var signature = method.name + '(' + getKeys(method.inputs, 'type').join(',') + ')';
var result = { var result = {
@ -441,12 +440,50 @@ function Interface(abi) {
signature: signature, signature: signature,
topics: [utils.keccak256(utils.toUtf8Bytes(signature))], topics: [utils.keccak256(utils.toUtf8Bytes(signature))],
}; };
result.parse = function(data) {
return Interface.decodeParams( result.parse = function(topics, data) {
inputNames,
inputTypes, // Strip the signature off of non-anonymous topics
if (!method.anonymous) { topics = topics.slice(1); }
var inputNamesIndexed = [], inputNamesNonIndexed = [];
var inputTypesIndexed = [], inputTypesNonIndexed = [];
method.inputs.forEach(function(input) {
if (input.indexed) {
inputNamesIndexed.push(input.name);
inputTypesIndexed.push(input.type);
} else {
inputNamesNonIndexed.push(input.name);
inputTypesNonIndexed.push(input.type);
}
});
var resultIndexed = Interface.decodeParams(
inputNamesIndexed,
inputTypesIndexed,
utils.concat(topics)
);
var resultNonIndexed = Interface.decodeParams(
inputNamesNonIndexed,
inputTypesNonIndexed,
utils.arrayify(data) utils.arrayify(data)
); );
var result = new Result();
var nonIndexedIndex = 0, indexedIndex = 0;
method.inputs.forEach(function(input, i) {
if (input.indexed) {
result[i] = resultIndexed[indexedIndex++];
} else {
result[i] = resultNonIndexed[nonIndexedIndex++];
}
if (input.name) { result[input.name] = result[i]; }
});
result.length = method.inputs.length;
return result;
}; };
return populateDescription(new EventDescription(), result); return populateDescription(new EventDescription(), result);
} }
@ -470,7 +507,7 @@ function Interface(abi) {
} }
}; };
abi.forEach(addMethod, this); this.abi.forEach(addMethod, this);
// If there wasn't a constructor, create the default constructor // If there wasn't a constructor, create the default constructor
if (!deploy) { if (!deploy) {

View File

@ -1,6 +1,6 @@
{ {
"name": "ethers-contracts", "name": "ethers-contracts",
"version": "2.0.0", "version": "2.0.1",
"description": "Contract and Interface (ABI) library for Ethereum.", "description": "Contract and Interface (ABI) library for Ethereum.",
"bugs": { "bugs": {
"url": "http://github.com/ethers-io/ethers.js/issues", "url": "http://github.com/ethers-io/ethers.js/issues",