Fixed constructor calls.

This commit is contained in:
ricmoo 2017-03-08 01:52:53 -05:00
parent 637a6d7f40
commit 83d099b37c

View File

@ -55,6 +55,8 @@ function Contract(address, contractInterface, signerOrProvider) {
if (typeof(transaction) !== 'object') { if (typeof(transaction) !== 'object') {
throw new Error('invalid transaction overrides'); throw new Error('invalid transaction overrides');
} }
// Check for unexpected keys (e.g. using "gas" instead of "gasLimit")
for (var key in transaction) { for (var key in transaction) {
if (!allowedTransactionKeys[key]) { if (!allowedTransactionKeys[key]) {
throw new Error('unknown transaction override ' + key); throw new Error('unknown transaction override ' + key);
@ -62,8 +64,21 @@ function Contract(address, contractInterface, signerOrProvider) {
} }
} }
// Check overrides make sense
['data', 'to'].forEach(function(key) {
if (transaction[key] != null) {
throw new Error('cannot override ' + key) ;
}
});
var call = method.apply(contractInterface, params); var call = method.apply(contractInterface, params);
// Send to the contract address
transaction.to = address;
// Set the transaction data
transaction.data = call.data;
switch (call.type) { switch (call.type) {
case 'call': case 'call':
@ -73,18 +88,16 @@ function Contract(address, contractInterface, signerOrProvider) {
} }
// Check overrides make sense // Check overrides make sense
['data', 'gasLimit', 'gasPrice', 'to', 'value'].forEach(function(key) { ['gasLimit', 'gasPrice', 'value'].forEach(function(key) {
if (transaction[key] != null) { if (transaction[key] != null) {
throw new Error('call cannot override ' + key) ; throw new Error('call cannot override ' + key) ;
} }
}); });
transaction.data = call.data;
var fromPromise = null; var fromPromise = null;
if (transaction.from == null && signer) { if (transaction.from == null && signer && signer.getAddress) {
fromPromise = new Promise(function(resolve, reject) { fromPromise = new Promise(function(resolve, reject) {
var address = signer.address; var address = signer.getAddress();
if (address instanceof Promise) { return address; } if (address instanceof Promise) { return address; }
resolve(address); resolve(address);
}); });
@ -92,8 +105,6 @@ function Contract(address, contractInterface, signerOrProvider) {
fromPromise = Promise.resolve(null); fromPromise = Promise.resolve(null);
} }
transaction.to = address;
return fromPromise.then(function(address) { return fromPromise.then(function(address) {
if (address) { if (address) {
transaction.from = utils.getAddress(address); transaction.from = utils.getAddress(address);
@ -106,25 +117,25 @@ function Contract(address, contractInterface, signerOrProvider) {
case 'transaction': case 'transaction':
if (!signer) { return Promise.reject(new Error('missing signer')); } if (!signer) { return Promise.reject(new Error('missing signer')); }
['data', 'from', 'to'].forEach(function(key) { // Make sure they aren't overriding something they shouldn't
if (transaction[key] != null) { if (transaction.from != null) {
throw new Error('transaction cannot override ' + key) ; throw new Error('transaction cannot override from') ;
} }
});
transaction.data = call.data;
transaction.to = address;
// Only computing the transaction estimate
if (estimateOnly) { if (estimateOnly) {
return provider.estimateGas(transaction) return provider.estimateGas(transaction)
} }
if (transaction.gasLimit == null) { // If the signer supports sendTrasaction, use it
transaction.gasLimit = signer.defaultGasRate || 2000000; if (signer.sendTransaction) {
return signer.sendTransaction(transaction);
}
if (transaction.gasLimit == null) {
transaction.gasLimit = signer.defaultGasLimit || 2000000;
} }
var gasPricePromise = null;
var noncePromise = null; var noncePromise = null;
if (transaction.nonce) { if (transaction.nonce) {
noncePromise = Promise.resolve(transaction.nonce) noncePromise = Promise.resolve(transaction.nonce)
@ -132,6 +143,7 @@ function Contract(address, contractInterface, signerOrProvider) {
noncePromise = provider.getTransactionCount(signer.address, 'pending'); noncePromise = provider.getTransactionCount(signer.address, 'pending');
} }
var gasPricePromise = null;
if (transaction.gasPrice) { if (transaction.gasPrice) {
gasPricePromise = Promise.resolve(transaction.gasPrice); gasPricePromise = Promise.resolve(transaction.gasPrice);
} else { } else {
@ -145,7 +157,6 @@ function Contract(address, contractInterface, signerOrProvider) {
]).then(function(results) { ]).then(function(results) {
transaction.nonce = results[0]; transaction.nonce = results[0];
transaction.gasPrice = results[1]; transaction.gasPrice = results[1];
return signer.sign(transaction); return signer.sign(transaction);
}).then(function(signedTransaction) { }).then(function(signedTransaction) {