web3-core-method
Advanced tools
Comparing version 1.0.0-beta.11 to 1.0.0-beta.12
{ | ||
"name": "web3-core-method", | ||
"version": "1.0.0-beta.11", | ||
"version": "1.0.0-beta.12", | ||
"description": "Creates the methods on the web3 modules. This is an internal package.", | ||
@@ -10,6 +10,7 @@ "repository": "https://github.com/ethereum/web3.js/tree/master/packages/web3-core-method", | ||
"underscore": "1.8.3", | ||
"web3-core-helpers": "^1.0.0-beta.11", | ||
"web3-core-promievent": "^1.0.0-beta.11", | ||
"web3-utils": "^1.0.0-beta.11" | ||
"web3-core-helpers": "^1.0.0-beta.12", | ||
"web3-core-promievent": "^1.0.0-beta.12", | ||
"web3-core-subscriptions": "^1.0.0-beta.12", | ||
"web3-utils": "^1.0.0-beta.12" | ||
} | ||
} |
134
src/index.js
@@ -28,4 +28,6 @@ /* | ||
var errors = require('web3-core-helpers').errors; | ||
var formatters = require('web3-core-helpers').formatters; | ||
var utils = require('web3-utils'); | ||
var promiEvent = require('web3-core-promievent'); | ||
var Subscriptions = require('web3-core-subscriptions').subscriptions; | ||
@@ -47,11 +49,56 @@ var TIMEOUTBLOCK = 50; | ||
this.transformPayload = options.transformPayload; | ||
this.requestManager = null; | ||
this.requestManager = options.requestManager; | ||
// reference to eth.accounts | ||
this.accounts = options.accounts; | ||
}; | ||
Method.prototype.setRequestManager = function (rm, eth) { | ||
this.requestManager = rm; | ||
Method.prototype.setRequestManager = function (requestManager, accounts) { | ||
var _this = this; | ||
this.requestManager = requestManager; | ||
if (eth) { | ||
this.eth = eth; | ||
// reference to eth.accounts | ||
if (accounts) { | ||
this.accounts = accounts; | ||
} | ||
// add custom send Methods | ||
var _ethereumCall = [ | ||
new Method({ | ||
name: 'getTransactionReceipt', | ||
call: 'eth_getTransactionReceipt', | ||
params: 1, | ||
inputFormatter: [null], | ||
outputFormatter: formatters.outputTransactionReceiptFormatter | ||
}), | ||
new Method({ | ||
name: 'getCode', | ||
call: 'eth_getCode', | ||
params: 2, | ||
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter] | ||
}), | ||
new Method({ | ||
name: 'getGasPrice', | ||
call: 'eth_gasPrice', | ||
params: 0 | ||
}), | ||
new Subscriptions({ | ||
name: 'subscribe', | ||
type: 'eth', | ||
subscriptions: { | ||
'newBlockHeaders': { | ||
subscriptionName: 'newHeads', // replace subscription with this name | ||
params: 0, | ||
outputFormatter: formatters.outputBlockFormatter | ||
} | ||
} | ||
}) | ||
]; | ||
// attach methods to this._ethereumCall | ||
this._ethereumCall = {}; | ||
_.each(_ethereumCall, function (method) { | ||
method.attachToObject(_this._ethereumCall); | ||
method.requestManager = _this.requestManager; // assign rather than call setRequestManager() | ||
}); | ||
}; | ||
@@ -184,3 +231,2 @@ | ||
// fire "receipt" and confirmation events and resolve after | ||
@@ -198,4 +244,3 @@ var checkConfirmation = function (err, block, sub) { | ||
method.eth.getTransactionReceipt(result) | ||
method._ethereumCall.getTransactionReceipt(result) | ||
// catch error from requesting receipt | ||
@@ -241,8 +286,12 @@ .catch(function (err) { | ||
if (!receipt.contractAddress) { | ||
promiseResolved = true; | ||
utils._fireError(new Error('The transaction receipt didn\'t contain a contract address.'), defer.eventEmitter, defer.reject); | ||
return; | ||
if (canUnsubscribe) { | ||
sub.unsubscribe(); | ||
promiseResolved = true; | ||
} | ||
return utils._fireError(new Error('The transaction receipt didn\'t contain a contract address.'), defer.eventEmitter, defer.reject); | ||
} | ||
method.eth.getCode(receipt.contractAddress, function (e, code) { | ||
method._ethereumCall.getCode(receipt.contractAddress, function (e, code) { | ||
@@ -264,2 +313,6 @@ if (!code) { | ||
// need to remove listeners, as they aren't removed automatically when succesfull | ||
if (canUnsubscribe) { | ||
defer.eventEmitter.removeAllListeners(); | ||
} | ||
@@ -272,3 +325,2 @@ } else { | ||
sub.unsubscribe(); | ||
defer.eventEmitter.removeAllListeners(); | ||
} | ||
@@ -291,2 +343,7 @@ promiseResolved = true; | ||
// need to remove listeners, as they aren't removed automatically when succesfull | ||
if (canUnsubscribe) { | ||
defer.eventEmitter.removeAllListeners(); | ||
} | ||
} else { | ||
@@ -301,3 +358,2 @@ if(receipt) { | ||
sub.unsubscribe(); | ||
defer.eventEmitter.removeAllListeners(); | ||
} | ||
@@ -310,9 +366,9 @@ promiseResolved = true; | ||
.catch(function () { | ||
if (timeoutCount >= TIMEOUTBLOCK) { | ||
timeoutCount++; | ||
if (timeoutCount - 1 >= TIMEOUTBLOCK) { | ||
sub.unsubscribe(); | ||
promiseResolved = true; | ||
utils._fireError(new Error('Transaction was not mined within 50 blocks, please make sure your transaction was properly send. Be aware that it might still be mined!'), defer.eventEmitter, defer.reject); | ||
return utils._fireError(new Error('Transaction was not mined within 50 blocks, please make sure your transaction was properly send. Be aware that it might still be mined!'), defer.eventEmitter, defer.reject); | ||
} | ||
timeoutCount++; | ||
}); | ||
@@ -324,3 +380,3 @@ | ||
promiseResolved = true; | ||
utils._fireError({message: 'Failed to subscribe to new newBlockHeaders to confirm the transactions receipt. Are you using HttpProvider? Please switch to Websockets.', data: err}, defer.eventEmitter, defer.reject); | ||
return utils._fireError({message: 'Failed to subscribe to new newBlockHeaders to confirm the transaction receipts.', data: err}, defer.eventEmitter, defer.reject); | ||
} | ||
@@ -331,3 +387,3 @@ }; | ||
if (_.isFunction(this.requestManager.provider.on)) { | ||
method.eth.subscribe('newBlockHeaders', checkConfirmation); | ||
method._ethereumCall.subscribe('newBlockHeaders', checkConfirmation); | ||
} else { | ||
@@ -360,8 +416,7 @@ intervalId = setInterval(checkConfirmation, 1000); | ||
var method = this, | ||
call = (_.isString(method.call)) ? method.call.toLowerCase() : Method.call, | ||
isSendTx = (call === 'eth_sendtransaction' || call === 'eth_sendrawtransaction'); | ||
isSendTx = (method.call === 'eth_sendTransaction' || method.call === 'eth_sendRawTransaction'); // || method.call === 'personal_sendTransaction' | ||
// actual send function | ||
var send = function () { | ||
var extraFromatters = this; | ||
var extraFormatters = this; | ||
var defer = promiEvent(!isSendTx), | ||
@@ -375,3 +430,2 @@ payload = method.toPayload(Array.prototype.slice.call(arguments)); | ||
if (!err) { | ||
@@ -386,4 +440,3 @@ if (payload.callback) { | ||
utils._fireError(err, defer.eventEmitter, defer.reject, payload.callback); | ||
return; | ||
return utils._fireError(err, defer.eventEmitter, defer.reject, payload.callback); | ||
} | ||
@@ -400,7 +453,7 @@ | ||
// return PROMIEVENT | ||
} else if (method.eth) { | ||
} else { | ||
defer.eventEmitter.emit('transactionHash', result); | ||
method._confirmTransaction(defer, result, payload, extraFromatters); | ||
method._confirmTransaction(defer, result, payload, extraFormatters); | ||
} | ||
@@ -422,9 +475,9 @@ | ||
if (method && method.eth && method.eth.accounts && method.eth.accounts.wallet.length) { | ||
if (method && method.accounts && method.accounts.wallet && method.accounts.wallet.length) { | ||
var wallet; | ||
// ETH_SENDTRANSACTION | ||
if (payload.method.toLowerCase() === 'eth_sendtransaction') { | ||
if (payload.method === 'eth_sendTransaction') { | ||
var tx = payload.params[0]; | ||
wallet = getWallet((_.isObject(tx)) ? tx.from : null, method.eth.accounts); | ||
wallet = getWallet((_.isObject(tx)) ? tx.from : null, method.accounts); | ||
@@ -436,3 +489,3 @@ | ||
var signature = method.eth.accounts.signTransaction(tx, wallet.privateKey); | ||
var signature = method.accounts.signTransaction(tx, wallet.privateKey); | ||
@@ -443,9 +496,9 @@ return (_.isFunction(signature.then)) ? signature.then(sendSignedTx) : sendSignedTx(signature); | ||
// ETH_SIGN | ||
} else if (payload.method.toLowerCase() === 'eth_sign') { | ||
} else if (payload.method === 'eth_sign') { | ||
var data = payload.params[1]; | ||
wallet = getWallet(payload.params[0], method.eth.accounts); | ||
wallet = getWallet(payload.params[0], method.accounts); | ||
// If wallet was found, sign tx, and send using sendRawTransaction | ||
if (wallet && wallet.privateKey) { | ||
var sign = method.eth.accounts.sign(data, wallet.privateKey); | ||
var sign = method.accounts.sign(data, wallet.privateKey); | ||
@@ -467,9 +520,9 @@ if (payload.callback) { | ||
// Send the actual transaction | ||
if(isSendTx && method.eth && _.isObject(payload.params[0]) && !payload.params[0].gasPrice) { | ||
if(isSendTx && _.isObject(payload.params[0]) && !payload.params[0].gasPrice) { | ||
method.eth.getGasPrice(function (err, gasPrice) { | ||
method._ethereumCall.getGasPrice(function (err, gasPrice) { | ||
if (gasPrice) { | ||
payload.params[0].gasPrice = utils.numberToHex(gasPrice); | ||
payload.params[0].gasPrice = gasPrice; | ||
} | ||
@@ -487,2 +540,5 @@ sendRequest(payload, method); | ||
// necessary to attach things to the method | ||
send.method = method; | ||
// necessary for batch requests | ||
send.request = this.request.bind(this); | ||
@@ -489,0 +545,0 @@ return send; |
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
18894
433
5
+ Addedweb3-core-subscriptions@1.10.4(transitive)
Updatedweb3-utils@^1.0.0-beta.12