2017-04-05 00:30:17 +03:00
|
|
|
'use strict';
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
var inherits = require('inherits');
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
|
|
|
|
|
2017-10-26 02:58:21 +03:00
|
|
|
var networks = require('./networks.json');
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
var utils = (function() {
|
2017-03-01 10:30:37 +03:00
|
|
|
var convert = require('ethers-utils/convert');
|
2017-02-25 09:23:48 +03:00
|
|
|
return {
|
2017-03-01 10:30:37 +03:00
|
|
|
defineProperty: require('ethers-utils/properties').defineProperty,
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
getAddress: require('ethers-utils/address').getAddress,
|
|
|
|
getContractAddress: require('ethers-utils/contract-address').getContractAddress,
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
bigNumberify: require('ethers-utils/bignumber').bigNumberify,
|
2017-02-25 09:23:48 +03:00
|
|
|
arrayify: convert.arrayify,
|
|
|
|
|
|
|
|
hexlify: convert.hexlify,
|
|
|
|
isHexString: convert.isHexString,
|
2017-03-01 10:30:37 +03:00
|
|
|
|
2017-05-20 22:42:16 +03:00
|
|
|
concat: convert.concat,
|
|
|
|
|
|
|
|
namehash: require('ethers-utils/namehash'),
|
|
|
|
|
2017-10-04 02:37:06 +03:00
|
|
|
toUtf8String: require('ethers-utils/utf8').toUtf8String,
|
|
|
|
|
2017-04-05 00:30:17 +03:00
|
|
|
RLP: require('ethers-utils/rlp'),
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2017-05-22 01:01:23 +03:00
|
|
|
function copyObject(obj) {
|
|
|
|
var result = {};
|
|
|
|
for (var key in obj) { result[key] = obj[key]; }
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
function check(format, object) {
|
|
|
|
var result = {};
|
|
|
|
for (var key in format) {
|
|
|
|
try {
|
2017-04-05 00:30:17 +03:00
|
|
|
var value = format[key](object[key]);
|
2017-02-25 09:23:48 +03:00
|
|
|
if (value !== undefined) { result[key] = value; }
|
|
|
|
} catch (error) {
|
|
|
|
error.checkKey = key;
|
|
|
|
error.checkValue = object[key];
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function allowNull(check, nullValue) {
|
|
|
|
return (function(value) {
|
|
|
|
if (value == null) { return nullValue; }
|
|
|
|
return check(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function allowFalsish(check, replaceValue) {
|
|
|
|
return (function(value) {
|
|
|
|
if (!value) { return replaceValue; }
|
|
|
|
return check(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function arrayOf(check) {
|
|
|
|
return (function(array) {
|
|
|
|
if (!Array.isArray(array)) { throw new Error('not an array'); }
|
|
|
|
|
|
|
|
var result = [];
|
|
|
|
|
|
|
|
array.forEach(function(value) {
|
|
|
|
result.push(check(value));
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkHash(hash) {
|
|
|
|
if (!utils.isHexString(hash) || hash.length !== 66) {
|
2017-03-01 10:30:37 +03:00
|
|
|
throw new Error('invalid hash - ' + hash);
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkNumber(number) {
|
|
|
|
return utils.bigNumberify(number).toNumber();
|
|
|
|
}
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
function checkUint256(uint256) {
|
|
|
|
if (!utils.isHexString(uint256)) {
|
|
|
|
throw new Error('invalid uint256');
|
|
|
|
}
|
|
|
|
while (uint256.length < 66) {
|
|
|
|
uint256 = '0x0' + uint256.substring(2);
|
|
|
|
}
|
|
|
|
return uint256;
|
|
|
|
}
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
function checkString(string) {
|
|
|
|
if (typeof(string) !== 'string') { throw new Error('invalid string'); }
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkBlockTag(blockTag) {
|
|
|
|
if (blockTag == null) { return 'latest'; }
|
|
|
|
|
2017-05-10 04:45:36 +03:00
|
|
|
if (blockTag === 'earliest') { return '0x0'; }
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2017-07-06 03:26:03 +03:00
|
|
|
if (blockTag === 'latest' || blockTag === 'pending') {
|
2017-05-10 04:45:36 +03:00
|
|
|
return blockTag;
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
|
2017-07-06 03:26:03 +03:00
|
|
|
if (typeof(blockTag) === 'number') {
|
|
|
|
return utils.hexlify(blockTag);
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
|
2017-07-06 03:26:03 +03:00
|
|
|
if (utils.isHexString(blockTag)) { return blockTag; }
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
throw new Error('invalid blockTag');
|
|
|
|
}
|
|
|
|
|
|
|
|
var formatBlock = {
|
|
|
|
hash: checkHash,
|
|
|
|
parentHash: checkHash,
|
|
|
|
number: checkNumber,
|
|
|
|
|
|
|
|
timestamp: checkNumber,
|
2017-10-26 02:58:21 +03:00
|
|
|
nonce: allowNull(utils.hexlify),
|
|
|
|
difficulty: allowNull(checkNumber),
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
gasLimit: utils.bigNumberify,
|
|
|
|
gasUsed: utils.bigNumberify,
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
miner: utils.getAddress,
|
2017-02-25 09:23:48 +03:00
|
|
|
extraData: utils.hexlify,
|
|
|
|
|
|
|
|
//transactions: allowNull(arrayOf(checkTransaction)),
|
|
|
|
transactions: allowNull(arrayOf(checkHash)),
|
|
|
|
|
|
|
|
//transactionRoot: checkHash,
|
|
|
|
//stateRoot: checkHash,
|
|
|
|
//sha3Uncles: checkHash,
|
|
|
|
|
|
|
|
//logsBloom: utils.hexlify,
|
|
|
|
};
|
|
|
|
|
|
|
|
function checkBlock(block) {
|
2017-03-01 10:30:37 +03:00
|
|
|
if (block.author != null && block.miner == null) {
|
|
|
|
block.miner = block.author;
|
|
|
|
}
|
2017-02-25 09:23:48 +03:00
|
|
|
return check(formatBlock, block);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var formatTransaction = {
|
|
|
|
hash: checkHash,
|
|
|
|
|
|
|
|
blockHash: allowNull(checkHash, null),
|
|
|
|
blockNumber: allowNull(checkNumber, null),
|
|
|
|
transactionIndex: allowNull(checkNumber, null),
|
|
|
|
|
|
|
|
from: utils.getAddress,
|
|
|
|
|
|
|
|
gasPrice: utils.bigNumberify,
|
|
|
|
gasLimit: utils.bigNumberify,
|
|
|
|
to: allowNull(utils.getAddress, null),
|
|
|
|
value: utils.bigNumberify,
|
|
|
|
nonce: checkNumber,
|
|
|
|
data: utils.hexlify,
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
r: checkUint256,
|
|
|
|
s: checkUint256,
|
2017-02-25 09:23:48 +03:00
|
|
|
v: checkNumber,
|
|
|
|
|
|
|
|
creates: allowNull(utils.getAddress, null),
|
2017-04-05 00:30:17 +03:00
|
|
|
|
|
|
|
raw: utils.hexlify,
|
2017-02-25 09:23:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
function checkTransaction(transaction) {
|
2017-04-05 00:30:17 +03:00
|
|
|
|
|
|
|
// Rename gas to gasLimit
|
2017-02-25 09:23:48 +03:00
|
|
|
if (transaction.gas != null && transaction.gasLimit == null) {
|
|
|
|
transaction.gasLimit = transaction.gas;
|
|
|
|
}
|
2017-04-05 00:30:17 +03:00
|
|
|
|
|
|
|
// Rename input to data
|
2017-02-25 09:23:48 +03:00
|
|
|
if (transaction.input != null && transaction.data == null) {
|
|
|
|
transaction.data = transaction.input;
|
|
|
|
}
|
2017-03-01 10:30:37 +03:00
|
|
|
|
2017-04-05 00:30:17 +03:00
|
|
|
// If to and creates are empty, populate the creates from the transaction
|
2017-03-01 10:30:37 +03:00
|
|
|
if (transaction.to == null && transaction.creates == null) {
|
|
|
|
transaction.creates = utils.getContractAddress(transaction);
|
|
|
|
}
|
|
|
|
|
2017-04-05 01:23:31 +03:00
|
|
|
if (!transaction.raw) {
|
|
|
|
var raw = [
|
|
|
|
utils.hexlify(transaction.nonce),
|
|
|
|
utils.hexlify(transaction.gasPrice),
|
|
|
|
utils.hexlify(transaction.gasLimit),
|
|
|
|
(transaction.to || "0x"),
|
|
|
|
utils.hexlify(transaction.value || '0x'),
|
|
|
|
utils.hexlify(transaction.data || '0x'),
|
|
|
|
utils.hexlify(transaction.v || '0x'),
|
|
|
|
utils.hexlify(transaction.r),
|
|
|
|
utils.hexlify(transaction.s),
|
|
|
|
];
|
|
|
|
|
|
|
|
transaction.raw = utils.RLP.encode(raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
var result = check(formatTransaction, transaction);
|
2017-04-05 00:30:17 +03:00
|
|
|
|
|
|
|
var networkId = transaction.networkId;
|
|
|
|
|
|
|
|
if (utils.isHexString(networkId)) {
|
|
|
|
networkId = utils.bigNumberify(networkId).toNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(networkId) !== 'number') {
|
|
|
|
networkId = (result.v - 35) / 2;
|
|
|
|
if (networkId < 0) { networkId = 0; }
|
|
|
|
networkId = parseInt(networkId);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.networkId = networkId;
|
|
|
|
|
|
|
|
// 0x0000... should actually be null
|
2017-03-01 10:30:37 +03:00
|
|
|
if (result.blockHash && result.blockHash.replace(/0/g, '') === 'x') {
|
|
|
|
result.blockHash = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
var formatTransactionRequest = {
|
2017-03-08 09:53:59 +03:00
|
|
|
from: allowNull(utils.getAddress),
|
2017-03-01 10:30:37 +03:00
|
|
|
nonce: allowNull(checkNumber),
|
|
|
|
gasLimit: allowNull(utils.bigNumberify),
|
|
|
|
gasPrice: allowNull(utils.bigNumberify),
|
|
|
|
to: allowNull(utils.getAddress),
|
|
|
|
value: allowNull(utils.bigNumberify),
|
2017-04-05 00:30:17 +03:00
|
|
|
data: allowNull(utils.hexlify),
|
2017-03-01 10:30:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
function checkTransactionRequest(transaction) {
|
|
|
|
return check(formatTransactionRequest, transaction);
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var formatTransactionReceiptLog = {
|
|
|
|
transactionLogIndex: checkNumber,
|
|
|
|
blockNumber: checkNumber,
|
|
|
|
transactionHash: checkHash,
|
|
|
|
address: utils.getAddress,
|
|
|
|
type: checkString,
|
|
|
|
topics: arrayOf(checkHash),
|
|
|
|
transactionIndex: checkNumber,
|
|
|
|
data: utils.hexlify,
|
|
|
|
logIndex: checkNumber,
|
|
|
|
blockHash: checkHash,
|
|
|
|
};
|
|
|
|
|
|
|
|
function checkTransactionReceiptLog(log) {
|
|
|
|
return check(formatTransactionReceiptLog, log);
|
|
|
|
}
|
|
|
|
|
|
|
|
var formatTransactionReceipt = {
|
|
|
|
contractAddress: allowNull(utils.getAddress, null),
|
|
|
|
transactionIndex: checkNumber,
|
|
|
|
root: checkHash,
|
|
|
|
gasUsed: utils.bigNumberify,
|
|
|
|
logsBloom: utils.hexlify,
|
|
|
|
blockHash: checkHash,
|
|
|
|
transactionHash: checkHash,
|
|
|
|
logs: arrayOf(checkTransactionReceiptLog),
|
|
|
|
blockNumber: checkNumber,
|
|
|
|
cumulativeGasUsed: utils.bigNumberify,
|
|
|
|
};
|
|
|
|
|
|
|
|
function checkTransactionReceipt(transactionReceipt) {
|
|
|
|
return check(formatTransactionReceipt, transactionReceipt);
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkTopics(topics) {
|
|
|
|
if (Array.isArray(topics)) {
|
|
|
|
topics.forEach(function(topic) {
|
|
|
|
checkTopics(topic);
|
|
|
|
});
|
|
|
|
|
|
|
|
} else if (topics != null) {
|
|
|
|
checkHash(topics);
|
|
|
|
}
|
|
|
|
|
|
|
|
return topics;
|
|
|
|
}
|
|
|
|
|
|
|
|
var formatFilter = {
|
|
|
|
fromBlock: allowNull(checkBlockTag, undefined),
|
|
|
|
toBlock: allowNull(checkBlockTag, undefined),
|
|
|
|
address: allowNull(utils.getAddress, undefined),
|
|
|
|
topics: allowNull(checkTopics, undefined),
|
|
|
|
};
|
|
|
|
|
|
|
|
function checkFilter(filter) {
|
|
|
|
return check(formatFilter, filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
var formatLog = {
|
|
|
|
blockNumber: checkNumber,
|
|
|
|
transactionIndex: checkNumber,
|
|
|
|
|
|
|
|
address: utils.getAddress,
|
|
|
|
data: allowFalsish(utils.hexlify, '0x'),
|
|
|
|
|
|
|
|
topics: arrayOf(checkHash),
|
|
|
|
|
|
|
|
transactionHash: checkHash,
|
|
|
|
logIndex: checkNumber,
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkLog(log) {
|
|
|
|
return check(formatLog, log);
|
|
|
|
}
|
|
|
|
|
2017-10-26 02:58:21 +03:00
|
|
|
function Provider(network) {
|
2017-02-25 09:23:48 +03:00
|
|
|
if (!(this instanceof Provider)) { throw new Error('missing new'); }
|
|
|
|
|
2017-10-26 02:58:21 +03:00
|
|
|
network = Provider._legacyConstructor(network, arguments.length, arguments[0], arguments[1]);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2017-10-26 02:58:21 +03:00
|
|
|
// Check the ensAddress (if any)
|
|
|
|
var ensAddress = null;
|
|
|
|
if (network.ensAddress) {
|
|
|
|
ensAddress = utils.getAddress(network.ensAddress);
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
|
2017-10-26 02:58:21 +03:00
|
|
|
// Setup our network properties
|
|
|
|
utils.defineProperty(this, 'chainId', network.chainId);
|
|
|
|
utils.defineProperty(this, 'ensAddress', ensAddress);
|
|
|
|
utils.defineProperty(this, 'name', network.name);
|
2017-05-20 22:42:16 +03:00
|
|
|
|
2017-10-26 02:58:21 +03:00
|
|
|
// @TODO: Remove in the next major release
|
|
|
|
utils.defineProperty(this, 'testnet', (network.name !== 'homestead'));
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
var events = {};
|
|
|
|
utils.defineProperty(this, '_events', events);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var lastBlockNumber = null;
|
2017-03-01 10:30:37 +03:00
|
|
|
|
2017-03-08 09:53:59 +03:00
|
|
|
var balances = {};
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
function doPoll() {
|
2017-02-25 09:23:48 +03:00
|
|
|
self.getBlockNumber().then(function(blockNumber) {
|
|
|
|
|
|
|
|
// If the block hasn't changed, meh.
|
|
|
|
if (blockNumber === lastBlockNumber) { return; }
|
|
|
|
|
|
|
|
if (lastBlockNumber === null) { lastBlockNumber = blockNumber - 1; }
|
|
|
|
|
|
|
|
// Notify all listener for each block that has passed
|
|
|
|
for (var i = lastBlockNumber + 1; i <= blockNumber; i++) {
|
|
|
|
self.emit('block', i);
|
|
|
|
}
|
|
|
|
|
2017-03-08 09:53:59 +03:00
|
|
|
// Sweep balances and remove addresses we no longer have events for
|
|
|
|
var newBalances = {};
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
// Find all transaction hashes we are waiting on
|
2017-03-01 10:30:37 +03:00
|
|
|
Object.keys(events).forEach(function(eventName) {
|
|
|
|
var event = parseEventString(eventName);
|
|
|
|
|
|
|
|
if (event.type === 'transaction') {
|
|
|
|
self.getTransaction(event.hash).then(function(transaction) {
|
2017-04-05 00:30:17 +03:00
|
|
|
if (!transaction || !transaction.blockNumber) { return; }
|
2017-03-01 10:30:37 +03:00
|
|
|
self.emit(event.hash, transaction);
|
|
|
|
});
|
|
|
|
|
2017-03-08 09:53:59 +03:00
|
|
|
} else if (event.type === 'address') {
|
|
|
|
if (balances[event.address]) {
|
|
|
|
newBalances[event.address] = balances[event.address];
|
|
|
|
}
|
|
|
|
self.getBalance(event.address, 'latest').then(function(balance) {
|
|
|
|
var lastBalance = balances[event.address];
|
|
|
|
if (lastBalance && balance.eq(lastBalance)) { return; }
|
|
|
|
balances[event.address] = balance;
|
|
|
|
self.emit(event.address, balance);
|
|
|
|
});
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
} else if (event.type === 'topic') {
|
|
|
|
self.getLogs({
|
|
|
|
fromBlock: lastBlockNumber + 1,
|
|
|
|
toBlock: blockNumber,
|
|
|
|
topics: event.topic
|
|
|
|
}).then(function(logs) {
|
|
|
|
if (logs.length === 0) { return; }
|
|
|
|
logs.forEach(function(log) {
|
|
|
|
self.emit(event.topic, log);
|
|
|
|
});
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
}
|
2017-03-01 10:30:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
lastBlockNumber = blockNumber;
|
2017-03-08 09:53:59 +03:00
|
|
|
|
|
|
|
balances = newBalances;
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
self.doPoll();
|
2017-03-01 10:30:37 +03:00
|
|
|
}
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2017-05-10 04:45:36 +03:00
|
|
|
utils.defineProperty(this, 'resetEventsBlock', function(blockNumber) {
|
|
|
|
lastBlockNumber = blockNumber;
|
|
|
|
self.doPoll();
|
|
|
|
});
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
var poller = null;
|
|
|
|
Object.defineProperty(this, 'polling', {
|
|
|
|
get: function() { return (poller != null); },
|
|
|
|
set: function(value) {
|
|
|
|
setTimeout(function() {
|
|
|
|
if (value && !poller) {
|
|
|
|
poller = setInterval(doPoll, 4000);
|
|
|
|
|
|
|
|
} else if (!value && poller) {
|
|
|
|
clearInterval(poller);
|
|
|
|
poller = null;
|
|
|
|
}
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
});
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
function inheritable(parent) {
|
|
|
|
return function(child) {
|
|
|
|
inherits(child, parent);
|
|
|
|
utils.defineProperty(child, 'inherits', inheritable(child));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.defineProperty(Provider, 'inherits', inheritable(Provider));
|
|
|
|
/*
|
|
|
|
function(child) {
|
|
|
|
inherits(child, Provider);
|
|
|
|
child.inherits = function(grandchild) {
|
|
|
|
inherits(grandchild, child)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
*/
|
2017-10-26 02:58:21 +03:00
|
|
|
|
|
|
|
utils.defineProperty(Provider, '_legacyConstructor', function(network, length, arg0, arg1) {
|
|
|
|
|
|
|
|
// Legacy parameters Provider(testnet:boolean, chainId:Number)
|
|
|
|
if (typeof(arg0) === 'boolean' || length === 2) {
|
|
|
|
var testnet = !!arg0;
|
|
|
|
var chainId = arg1;
|
|
|
|
|
|
|
|
// true => testnet, false => mainnet
|
|
|
|
network = networks[testnet ? 'ropsten': 'homestead'];
|
|
|
|
|
|
|
|
// Overriding chain ID
|
|
|
|
if (length === 2 && chainId != null) {
|
|
|
|
network = {
|
|
|
|
chainId: chainId,
|
|
|
|
ensAddress: network.ensAddress,
|
|
|
|
name: network.name
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (typeof(network) === 'string') {
|
|
|
|
network = networks[network];
|
|
|
|
if (!network) { throw new Error('unknown network'); }
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(network.chainId) !== 'number') { throw new Error('invalid chainId'); }
|
|
|
|
|
|
|
|
return network;
|
|
|
|
});
|
2017-02-25 09:23:48 +03:00
|
|
|
utils.defineProperty(Provider, 'chainId', {
|
|
|
|
homestead: 1,
|
|
|
|
morden: 2,
|
|
|
|
ropsten: 3,
|
|
|
|
});
|
|
|
|
|
2017-04-05 00:30:17 +03:00
|
|
|
//utils.defineProperty(Provider, 'isProvider', function(object) {
|
|
|
|
// return (object instanceof Provider);
|
|
|
|
//});
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2017-10-26 02:58:21 +03:00
|
|
|
utils.defineProperty(Provider, 'networks', networks);
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
utils.defineProperty(Provider, 'fetchJSON', function(url, json, processFunc) {
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
|
|
|
|
if (json) {
|
|
|
|
request.open('POST', url, true);
|
|
|
|
request.setRequestHeader('Content-Type','application/json');
|
|
|
|
} else {
|
|
|
|
request.open('GET', url, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
request.onreadystatechange = function() {
|
|
|
|
if (request.readyState !== 4) { return; }
|
|
|
|
|
|
|
|
try {
|
2017-03-01 10:30:37 +03:00
|
|
|
var result = JSON.parse(request.responseText);
|
2017-02-25 09:23:48 +03:00
|
|
|
} catch (error) {
|
|
|
|
var jsonError = new Error('invalid json response');
|
|
|
|
jsonError.orginialError = error;
|
|
|
|
jsonError.responseText = request.responseText;
|
|
|
|
reject(jsonError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (processFunc) {
|
|
|
|
try {
|
|
|
|
result = processFunc(result);
|
|
|
|
} catch (error) {
|
2017-03-01 10:30:37 +03:00
|
|
|
error.url = url;
|
|
|
|
error.body = json;
|
2017-02-25 09:23:48 +03:00
|
|
|
error.responseText = request.responseText;
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
if (request.status != 200) {
|
|
|
|
var error = new Error('invalid response - ' + request.status);
|
|
|
|
error.statusCode = request.statusCode;
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
resolve(result);
|
|
|
|
};
|
|
|
|
|
|
|
|
request.onerror = function(error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (json) {
|
|
|
|
request.send(json);
|
|
|
|
} else {
|
|
|
|
request.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
var connectionError = new Error('connection error');
|
|
|
|
connectionError.error = error;
|
|
|
|
reject(connectionError);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'waitForTransaction', function(transactionHash, timeout) {
|
2017-03-01 10:30:37 +03:00
|
|
|
var self = this;
|
|
|
|
return new Promise(function(resolve, reject) {
|
2017-02-25 09:23:48 +03:00
|
|
|
var timer = null;
|
|
|
|
|
|
|
|
function complete(transaction) {
|
2017-03-01 10:30:37 +03:00
|
|
|
if (timer) { clearTimeout(timer); }
|
2017-02-25 09:23:48 +03:00
|
|
|
resolve(transaction);
|
|
|
|
}
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
self.once(transactionHash, complete);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
if (typeof(timeout) === 'number' && timeout > 0) {
|
|
|
|
timer = setTimeout(function() {
|
2017-03-01 10:30:37 +03:00
|
|
|
self.removeListener(transactionHash, complete);
|
2017-02-25 09:23:48 +03:00
|
|
|
reject(new Error('timeout'));
|
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'getBlockNumber', function() {
|
|
|
|
try {
|
|
|
|
return this.perform('getBlockNumber').then(function(result) {
|
|
|
|
var value = parseInt(result);
|
2017-03-01 10:30:37 +03:00
|
|
|
if (value != result) { throw new Error('invalid response - getBlockNumber'); }
|
2017-02-25 09:23:48 +03:00
|
|
|
return value;
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'getGasPrice', function() {
|
|
|
|
try {
|
|
|
|
return this.perform('getGasPrice').then(function(result) {
|
|
|
|
return utils.bigNumberify(result);
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-20 22:42:16 +03:00
|
|
|
utils.defineProperty(Provider.prototype, 'getBalance', function(addressOrName, blockTag) {
|
|
|
|
var self = this;
|
|
|
|
return this.resolveName(addressOrName).then(function(address) {
|
|
|
|
var params = { address: address, blockTag: checkBlockTag(blockTag) };
|
|
|
|
return self.perform('getBalance', params).then(function(result) {
|
2017-02-25 09:23:48 +03:00
|
|
|
return utils.bigNumberify(result);
|
|
|
|
});
|
2017-05-20 22:42:16 +03:00
|
|
|
});
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
2017-05-20 22:42:16 +03:00
|
|
|
utils.defineProperty(Provider.prototype, 'getTransactionCount', function(addressOrName, blockTag) {
|
|
|
|
var self = this;
|
|
|
|
return this.resolveName(addressOrName).then(function(address) {
|
|
|
|
var params = { address: address, blockTag: checkBlockTag(blockTag) };
|
|
|
|
return self.perform('getTransactionCount', params).then(function(result) {
|
2017-02-25 09:23:48 +03:00
|
|
|
var value = parseInt(result);
|
2017-03-01 10:30:37 +03:00
|
|
|
if (value != result) { throw new Error('invalid response - getTransactionCount'); }
|
2017-02-25 09:23:48 +03:00
|
|
|
return value;
|
|
|
|
});
|
2017-05-20 22:42:16 +03:00
|
|
|
});
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
2017-05-20 22:42:16 +03:00
|
|
|
utils.defineProperty(Provider.prototype, 'getCode', function(addressOrName, blockTag) {
|
|
|
|
var self = this;
|
|
|
|
return this.resolveName(addressOrName).then(function(address) {
|
|
|
|
var params = {address: address, blockTag: checkBlockTag(blockTag)};
|
|
|
|
return self.perform('getCode', params).then(function(result) {
|
2017-02-25 09:23:48 +03:00
|
|
|
return utils.hexlify(result);
|
|
|
|
});
|
2017-05-20 22:42:16 +03:00
|
|
|
});
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
2017-05-20 22:42:16 +03:00
|
|
|
utils.defineProperty(Provider.prototype, 'getStorageAt', function(addressOrName, position, blockTag) {
|
|
|
|
var self = this;
|
2017-05-22 01:01:23 +03:00
|
|
|
return this.resolveName(addressOrName).then(function(address) {
|
|
|
|
var params = {
|
|
|
|
address: address,
|
|
|
|
blockTag: checkBlockTag(blockTag),
|
|
|
|
position: utils.hexlify(position),
|
|
|
|
};
|
2017-05-20 22:42:16 +03:00
|
|
|
return self.perform('getStorageAt', params).then(function(result) {
|
2017-02-25 09:23:48 +03:00
|
|
|
return utils.hexlify(result);
|
|
|
|
});
|
2017-05-20 22:42:16 +03:00
|
|
|
});
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'sendTransaction', function(signedTransaction) {
|
|
|
|
try {
|
|
|
|
var params = {signedTransaction: utils.hexlify(signedTransaction)};
|
|
|
|
return this.perform('sendTransaction', params).then(function(result) {
|
|
|
|
result = utils.hexlify(result);
|
2017-03-01 10:30:37 +03:00
|
|
|
if (result.length !== 66) { throw new Error('invalid response - sendTransaction'); }
|
2017-02-25 09:23:48 +03:00
|
|
|
return result;
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'call', function(transaction) {
|
2017-05-20 22:42:16 +03:00
|
|
|
var self = this;
|
|
|
|
return this._resolveNames(transaction, ['to', 'from']).then(function(transaction) {
|
|
|
|
var params = { transaction: checkTransactionRequest(transaction) };
|
|
|
|
return self.perform('call', params).then(function(result) {
|
2017-02-25 09:23:48 +03:00
|
|
|
return utils.hexlify(result);
|
|
|
|
});
|
2017-05-20 22:42:16 +03:00
|
|
|
});
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'estimateGas', function(transaction) {
|
2017-05-20 22:42:16 +03:00
|
|
|
var self = this;
|
|
|
|
return this._resolveNames(transaction, ['to', 'from']).then(function(transaction) {
|
2017-03-01 10:30:37 +03:00
|
|
|
var params = {transaction: checkTransactionRequest(transaction)};
|
2017-05-20 22:42:16 +03:00
|
|
|
return self.perform('estimateGas', params).then(function(result) {
|
2017-02-25 09:23:48 +03:00
|
|
|
return utils.bigNumberify(result);
|
|
|
|
});
|
2017-05-20 22:42:16 +03:00
|
|
|
});
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'getBlock', function(blockHashOrBlockTag) {
|
|
|
|
try {
|
|
|
|
var blockHash = utils.hexlify(blockHashOrBlockTag);
|
|
|
|
if (blockHash.length === 66) {
|
|
|
|
return this.perform('getBlock', {blockHash: blockHash}).then(function(block) {
|
|
|
|
return checkBlock(block);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log('DEBUG', error);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var blockTag = checkBlockTag(blockHashOrBlockTag);
|
|
|
|
return this.perform('getBlock', {blockTag: blockTag}).then(function(block) {
|
|
|
|
return checkBlock(block);
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.log('DEBUG', error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(new Error('invalid block hash or block tag'));
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'getTransaction', function(transactionHash) {
|
|
|
|
try {
|
2017-05-22 01:01:23 +03:00
|
|
|
var params = { transactionHash: checkHash(transactionHash) };
|
2017-02-25 09:23:48 +03:00
|
|
|
return this.perform('getTransaction', params).then(function(result) {
|
2017-03-01 10:30:37 +03:00
|
|
|
if (result != null) { result = checkTransaction(result); }
|
|
|
|
return result;
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'getTransactionReceipt', function(transactionHash) {
|
|
|
|
try {
|
2017-05-22 01:01:23 +03:00
|
|
|
var params = { transactionHash: checkHash(transactionHash) };
|
2017-02-25 09:23:48 +03:00
|
|
|
return this.perform('getTransactionReceipt', params).then(function(result) {
|
2017-03-01 10:30:37 +03:00
|
|
|
if (result != null) { result = checkTransactionReceipt(result); }
|
|
|
|
return result;
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'getLogs', function(filter) {
|
2017-05-22 01:01:23 +03:00
|
|
|
var self = this;
|
|
|
|
return this._resolveNames(filter, ['address']).then(function(filter) {
|
|
|
|
var params = { filter: checkFilter(filter) };
|
2017-05-22 03:29:09 +03:00
|
|
|
return self.perform('getLogs', params).then(function(result) {
|
2017-02-25 09:23:48 +03:00
|
|
|
return arrayOf(checkLog)(result);
|
|
|
|
});
|
2017-05-22 01:01:23 +03:00
|
|
|
});
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'getEtherPrice', function() {
|
|
|
|
try {
|
|
|
|
return this.perform('getEtherPrice', {}).then(function(result) {
|
|
|
|
// @TODO: Check valid float
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-05-20 22:42:16 +03:00
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, '_resolveNames', function(object, keys) {
|
|
|
|
var promises = [];
|
|
|
|
|
2017-05-22 01:01:23 +03:00
|
|
|
var result = copyObject(object);
|
2017-05-20 22:42:16 +03:00
|
|
|
|
|
|
|
keys.forEach(function(key) {
|
|
|
|
if (result[key] === undefined) { return; }
|
|
|
|
promises.push(this.resolveName(result[key]).then(function(address) {
|
|
|
|
result[key] = address;
|
|
|
|
}));
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
return Promise.all(promises).then(function() { return result; });
|
|
|
|
});
|
|
|
|
|
2017-10-04 02:37:06 +03:00
|
|
|
utils.defineProperty(Provider.prototype, '_getResolver', function(name) {
|
2017-05-20 22:42:16 +03:00
|
|
|
var nodeHash = utils.namehash(name);
|
|
|
|
|
|
|
|
// keccak256('resolver(bytes32)')
|
|
|
|
var data = '0x0178b8bf' + nodeHash.substring(2);
|
2017-05-22 03:29:09 +03:00
|
|
|
var transaction = { to: this.ensAddress, data: data };
|
2017-05-20 22:42:16 +03:00
|
|
|
|
|
|
|
// Get the resolver from the blockchain
|
|
|
|
return this.call(transaction).then(function(data) {
|
|
|
|
|
|
|
|
// extract the address from the data
|
|
|
|
if (data.length != 66) { return null; }
|
|
|
|
return utils.getAddress('0x' + data.substring(26));
|
2017-10-04 02:37:06 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'resolveName', function(name) {
|
|
|
|
// If it is already an address, nothing to resolve
|
|
|
|
try {
|
|
|
|
return Promise.resolve(utils.getAddress(name));
|
|
|
|
} catch (error) { }
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var nodeHash = utils.namehash(name);
|
2017-05-20 22:42:16 +03:00
|
|
|
|
|
|
|
// Get the addr from the resovler
|
2017-10-04 02:37:06 +03:00
|
|
|
return this._getResolver(name).then(function(resolverAddress) {
|
2017-05-22 03:29:09 +03:00
|
|
|
|
2017-05-20 22:42:16 +03:00
|
|
|
// keccak256('addr(bytes32)')
|
|
|
|
var data = '0x3b3b57de' + nodeHash.substring(2);
|
|
|
|
var transaction = { to: resolverAddress, data: data };
|
|
|
|
return self.call(transaction);
|
|
|
|
|
|
|
|
// extract the address from the data
|
|
|
|
}).then(function(data) {
|
|
|
|
if (data.length != 66) { return null; }
|
2017-05-22 03:29:09 +03:00
|
|
|
var address = utils.getAddress('0x' + data.substring(26));
|
|
|
|
if (address === '0x0000000000000000000000000000000000000000') { return null; }
|
|
|
|
return address;
|
2017-05-20 22:42:16 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-04 02:37:06 +03:00
|
|
|
utils.defineProperty(Provider.prototype, 'lookupAddress', function(address) {
|
2017-10-17 23:07:32 +03:00
|
|
|
address = utils.getAddress(address);
|
|
|
|
|
|
|
|
var name = address.substring(2) + '.addr.reverse'
|
2017-10-04 02:37:06 +03:00
|
|
|
var nodehash = utils.namehash(name);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return this._getResolver(name).then(function(resolverAddress) {
|
|
|
|
if (!resolverAddress) { return null; }
|
|
|
|
|
|
|
|
// keccak('name(bytes32)')
|
|
|
|
var data = '0x691f3431' + nodehash.substring(2);
|
|
|
|
var transaction = { to: resolverAddress, data: data };
|
|
|
|
return self.call(transaction);
|
|
|
|
|
|
|
|
}).then(function(data) {
|
|
|
|
// Strip off the "0x"
|
|
|
|
data = data.substring(2);
|
|
|
|
|
|
|
|
// Strip off the dynamic string pointer (0x20)
|
|
|
|
if (data.length < 64) { return null; }
|
|
|
|
data = data.substring(64);
|
|
|
|
|
|
|
|
if (data.length < 64) { return null; }
|
|
|
|
var length = utils.bigNumberify('0x' + data.substring(0, 64)).toNumber();
|
|
|
|
data = data.substring(64);
|
|
|
|
|
|
|
|
if (2 * length > data.length) { return null; }
|
|
|
|
|
|
|
|
var name = utils.toUtf8String('0x' + data.substring(0, 2 * length));
|
|
|
|
|
|
|
|
// Make sure the reverse record matches the foward record
|
|
|
|
return self.resolveName(name).then(function(addr) {
|
|
|
|
if (addr != address) { return null; }
|
|
|
|
return name;
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
utils.defineProperty(Provider.prototype, 'doPoll', function() {
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'perform', function(method, params) {
|
|
|
|
return Promise.reject(new Error('not implemented - ' + method));
|
|
|
|
});
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
function recurse(object, convertFunc) {
|
2017-02-25 09:23:48 +03:00
|
|
|
if (Array.isArray(object)) {
|
2017-03-01 10:30:37 +03:00
|
|
|
var result = [];
|
|
|
|
object.forEach(function(object) {
|
|
|
|
result.push(recurse(object, convertFunc));
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return convertFunc(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEventString(object) {
|
2017-03-08 09:53:59 +03:00
|
|
|
try {
|
|
|
|
return 'address:' + utils.getAddress(object);
|
|
|
|
} catch (error) { }
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
if (object === 'block') {
|
|
|
|
return 'block';
|
|
|
|
|
|
|
|
} else if (utils.isHexString(object)) {
|
|
|
|
if (object.length === 66) {
|
|
|
|
return 'tx:' + object;
|
|
|
|
}
|
|
|
|
} else if (Array.isArray(object)) {
|
|
|
|
object = recurse(object, function(object) {
|
|
|
|
if (object == null) { object = '0x'; }
|
|
|
|
return object;
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2017-04-05 00:30:17 +03:00
|
|
|
return 'topic:' + utils.RLP.encode(object);
|
2017-03-01 10:30:37 +03:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
2017-03-01 10:30:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('invalid event - ' + object);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseEventString(string) {
|
|
|
|
if (string.substring(0, 3) === 'tx:') {
|
|
|
|
return {type: 'transaction', hash: string.substring(3)};
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
} else if (string === 'block') {
|
|
|
|
return {type: 'block'};
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2017-03-08 09:53:59 +03:00
|
|
|
} else if (string.substring(0, 8) === 'address:') {
|
|
|
|
return {type: 'address', address: string.substring(8)};
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
} else if (string.substring(0, 6) === 'topic:') {
|
|
|
|
try {
|
2017-04-05 00:30:17 +03:00
|
|
|
var object = utils.RLP.decode(string.substring(6));
|
2017-03-01 10:30:37 +03:00
|
|
|
object = recurse(object, function(object) {
|
|
|
|
if (object === '0x') { object = null; }
|
|
|
|
return object;
|
|
|
|
});
|
|
|
|
return {type: 'topic', topic: object};
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
throw new Error('invalid event string');
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'on', function(eventName, listener) {
|
2017-03-01 10:30:37 +03:00
|
|
|
var key = getEventString(eventName);
|
|
|
|
if (!this._events[key]) { this._events[key] = []; }
|
2017-02-25 09:23:48 +03:00
|
|
|
this._events[key].push({eventName: eventName, listener: listener, type: 'on'});
|
2017-03-01 10:30:37 +03:00
|
|
|
this.polling = true;
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'once', function(eventName, listener) {
|
2017-03-01 10:30:37 +03:00
|
|
|
var key = getEventString(eventName);
|
|
|
|
if (!this._events[key]) { this._events[key] = []; }
|
|
|
|
this._events[key].push({eventName: eventName, listener: listener, type: 'once'});
|
|
|
|
this.polling = true;
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'emit', function(eventName) {
|
2017-03-01 10:30:37 +03:00
|
|
|
var key = getEventString(eventName);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
var listeners = this._events[key];
|
|
|
|
if (!listeners) { return; }
|
|
|
|
|
|
|
|
for (var i = 0; i < listeners.length; i++) {
|
|
|
|
var listener = listeners[i];
|
|
|
|
if (listener.type === 'once') {
|
2017-03-01 10:30:37 +03:00
|
|
|
listeners.splice(i, 1);
|
2017-02-25 09:23:48 +03:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
listener.listener.apply(this, args);
|
|
|
|
} catch (error) {
|
|
|
|
console.log('Event Listener Error: ' + error.message);
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 10:30:37 +03:00
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
if (listeners.length === 0) { delete this._events[key]; }
|
2017-03-01 10:30:37 +03:00
|
|
|
if (this.listenerCount() === 0) { this.polling = false; }
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'listenerCount', function(eventName) {
|
2017-03-01 10:30:37 +03:00
|
|
|
if (!eventName) {
|
|
|
|
var result = 0;
|
|
|
|
for (var key in this._events) {
|
|
|
|
result += this._events[key].length;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
var listeners = this._events[getEventString(eventName)];
|
2017-02-25 09:23:48 +03:00
|
|
|
if (!listeners) { return 0; }
|
|
|
|
return listeners.length;
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'listeners', function(eventName) {
|
2017-03-01 10:30:37 +03:00
|
|
|
var listeners = this._events[getEventString(eventName)];
|
2017-02-25 09:23:48 +03:00
|
|
|
if (!listeners) { return 0; }
|
|
|
|
var result = [];
|
|
|
|
for (var i = 0; i < listeners.length; i++) {
|
2017-03-01 10:30:37 +03:00
|
|
|
result.push(listeners[i].listener);
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'removeAllListeners', function(eventName) {
|
2017-03-01 10:30:37 +03:00
|
|
|
delete this._events[getEventString(eventName)];
|
|
|
|
if (this.listenerCount() === 0) { this.polling = false; }
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'removeListener', function(eventName, listener) {
|
2017-03-01 10:30:37 +03:00
|
|
|
var listeners = this._events[getEventString(eventName)];
|
2017-02-25 09:23:48 +03:00
|
|
|
if (!listeners) { return 0; }
|
|
|
|
for (var i = 0; i < listeners.length; i++) {
|
|
|
|
if (listeners[i].listener === listener) {
|
2017-03-01 10:30:37 +03:00
|
|
|
listeners.splice(i, 1);
|
2017-02-25 09:23:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 10:30:37 +03:00
|
|
|
if (this.listenerCount() === 0) { this.polling = false; }
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Provider;
|