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() {
|
2018-03-05 03:31:09 +03:00
|
|
|
var convert = require('../utils/convert');
|
2018-04-14 23:10:26 +03:00
|
|
|
var utf8 = require('../utils/utf8');
|
2017-02-25 09:23:48 +03:00
|
|
|
return {
|
2018-03-05 03:31:09 +03:00
|
|
|
defineProperty: require('../utils/properties').defineProperty,
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2018-03-05 03:31:09 +03:00
|
|
|
getAddress: require('../utils/address').getAddress,
|
|
|
|
getContractAddress: require('../utils/contract-address').getContractAddress,
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2018-03-05 03:31:09 +03:00
|
|
|
bigNumberify: require('../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,
|
2018-03-05 10:52:53 +03:00
|
|
|
hexStripZeros: convert.hexStripZeros,
|
2017-11-22 03:05:34 +03:00
|
|
|
stripZeros: convert.stripZeros,
|
2017-05-20 22:42:16 +03:00
|
|
|
|
2018-04-14 23:10:26 +03:00
|
|
|
base64: require('../utils/base64'),
|
|
|
|
|
2018-03-05 03:31:09 +03:00
|
|
|
namehash: require('../utils/namehash'),
|
2017-05-20 22:42:16 +03:00
|
|
|
|
2018-04-14 23:10:26 +03:00
|
|
|
toUtf8String: utf8.toUtf8String,
|
|
|
|
toUtf8Bytes: utf8.toUtf8Bytes,
|
2017-10-04 02:37:06 +03:00
|
|
|
|
2018-03-05 03:31:09 +03:00
|
|
|
RLP: require('../utils/rlp'),
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2018-04-14 23:10:26 +03:00
|
|
|
var errors = require('../utils/errors');
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-04-05 22:31:31 +03:00
|
|
|
function checkDifficulty(number) {
|
|
|
|
var value = utils.bigNumberify(number);
|
|
|
|
|
|
|
|
try {
|
|
|
|
value = value.toNumber();
|
|
|
|
} catch (error) {
|
|
|
|
value = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-12-05 09:56:57 +03:00
|
|
|
function checkBoolean(value) {
|
|
|
|
if (typeof(value) === 'boolean') { return value; }
|
|
|
|
if (typeof(value) === 'string') {
|
|
|
|
if (value === 'true') { return true; }
|
|
|
|
if (value === 'false') { return false; }
|
|
|
|
}
|
|
|
|
throw new Error('invaid boolean - ' + value);
|
|
|
|
}
|
|
|
|
|
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') {
|
2018-03-05 10:52:53 +03:00
|
|
|
return utils.hexStripZeros(utils.hexlify(blockTag));
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
|
2018-03-05 10:52:53 +03:00
|
|
|
if (utils.isHexString(blockTag)) { return utils.hexStripZeros(blockTag); }
|
2017-07-06 03:26:03 +03:00
|
|
|
|
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),
|
2018-04-05 22:31:31 +03:00
|
|
|
difficulty: checkDifficulty,
|
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-10-28 00:03:26 +03:00
|
|
|
r: allowNull(checkUint256),
|
|
|
|
s: allowNull(checkUint256),
|
|
|
|
v: allowNull(checkNumber),
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
creates: allowNull(utils.getAddress, null),
|
2017-04-05 00:30:17 +03:00
|
|
|
|
2017-10-28 00:03:26 +03:00
|
|
|
raw: allowNull(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-11-19 02:00:36 +03:00
|
|
|
|
|
|
|
// Some clients (TestRPC) do strange things like return 0x0 for the
|
|
|
|
// 0 address; correct this to be a real address
|
2017-11-22 03:05:34 +03:00
|
|
|
if (transaction.to && utils.bigNumberify(transaction.to).isZero()) {
|
2017-11-19 02:00:36 +03:00
|
|
|
transaction.to = '0x0000000000000000000000000000000000000000';
|
|
|
|
}
|
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) {
|
2017-10-28 00:03:26 +03:00
|
|
|
// Very loose providers (e.g. TestRPC) don't provide a signature or raw
|
|
|
|
if (transaction.v && transaction.r && transaction.s) {
|
|
|
|
var raw = [
|
2017-11-22 03:05:34 +03:00
|
|
|
utils.stripZeros(utils.hexlify(transaction.nonce)),
|
|
|
|
utils.stripZeros(utils.hexlify(transaction.gasPrice)),
|
|
|
|
utils.stripZeros(utils.hexlify(transaction.gasLimit)),
|
2017-10-28 00:03:26 +03:00
|
|
|
(transaction.to || "0x"),
|
2017-11-22 03:05:34 +03:00
|
|
|
utils.stripZeros(utils.hexlify(transaction.value || '0x')),
|
2017-10-28 00:03:26 +03:00
|
|
|
utils.hexlify(transaction.data || '0x'),
|
2017-11-22 03:05:34 +03:00
|
|
|
utils.stripZeros(utils.hexlify(transaction.v || '0x')),
|
|
|
|
utils.stripZeros(utils.hexlify(transaction.r)),
|
|
|
|
utils.stripZeros(utils.hexlify(transaction.s)),
|
2017-10-28 00:03:26 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
transaction.raw = utils.RLP.encode(raw);
|
|
|
|
}
|
2017-04-05 01:23:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-10-28 00:03:26 +03:00
|
|
|
if (typeof(networkId) !== 'number' && result.v != null) {
|
2017-04-05 00:30:17 +03:00
|
|
|
networkId = (result.v - 35) / 2;
|
|
|
|
if (networkId < 0) { networkId = 0; }
|
|
|
|
networkId = parseInt(networkId);
|
|
|
|
}
|
|
|
|
|
2017-10-28 00:03:26 +03:00
|
|
|
if (typeof(networkId) !== 'number') { networkId = 0; }
|
|
|
|
|
2017-04-05 00:30:17 +03:00
|
|
|
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 = {
|
2017-11-03 09:30:56 +03:00
|
|
|
transactionLogIndex: allowNull(checkNumber),
|
|
|
|
transactionIndex: checkNumber,
|
2017-02-25 09:23:48 +03:00
|
|
|
blockNumber: checkNumber,
|
|
|
|
transactionHash: checkHash,
|
|
|
|
address: utils.getAddress,
|
|
|
|
topics: arrayOf(checkHash),
|
|
|
|
data: utils.hexlify,
|
|
|
|
logIndex: checkNumber,
|
|
|
|
blockHash: checkHash,
|
|
|
|
};
|
|
|
|
|
|
|
|
function checkTransactionReceiptLog(log) {
|
|
|
|
return check(formatTransactionReceiptLog, log);
|
|
|
|
}
|
|
|
|
|
|
|
|
var formatTransactionReceipt = {
|
|
|
|
contractAddress: allowNull(utils.getAddress, null),
|
|
|
|
transactionIndex: checkNumber,
|
2017-11-03 09:30:56 +03:00
|
|
|
root: allowNull(checkHash),
|
2017-02-25 09:23:48 +03:00
|
|
|
gasUsed: utils.bigNumberify,
|
2018-03-18 00:40:47 +03:00
|
|
|
logsBloom: allowNull(utils.hexlify),
|
2017-02-25 09:23:48 +03:00
|
|
|
blockHash: checkHash,
|
|
|
|
transactionHash: checkHash,
|
|
|
|
logs: arrayOf(checkTransactionReceiptLog),
|
|
|
|
blockNumber: checkNumber,
|
|
|
|
cumulativeGasUsed: utils.bigNumberify,
|
2017-11-03 09:30:56 +03:00
|
|
|
status: allowNull(checkNumber)
|
2017-02-25 09:23:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
function checkTransactionReceipt(transactionReceipt) {
|
2017-11-03 09:30:56 +03:00
|
|
|
var status = transactionReceipt.status;
|
|
|
|
var root = transactionReceipt.root;
|
2017-11-17 22:55:14 +03:00
|
|
|
|
2017-11-03 09:30:56 +03:00
|
|
|
var result = check(formatTransactionReceipt, transactionReceipt);
|
|
|
|
result.logs.forEach(function(entry, index) {
|
|
|
|
if (entry.transactionLogIndex == null) {
|
|
|
|
entry.transactionLogIndex = index;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (transactionReceipt.status != null) {
|
|
|
|
result.byzantium = true;
|
|
|
|
}
|
|
|
|
return result;
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 = {
|
2017-12-05 09:56:57 +03:00
|
|
|
blockNumber: allowNull(checkNumber),
|
|
|
|
blockHash: allowNull(checkHash),
|
2017-02-25 09:23:48 +03:00
|
|
|
transactionIndex: checkNumber,
|
|
|
|
|
2017-12-05 09:56:57 +03:00
|
|
|
removed: allowNull(checkBoolean),
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
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'); }
|
|
|
|
|
2018-03-05 03:31:09 +03:00
|
|
|
network = Provider.getNetwork(network);
|
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-02-25 09:23:48 +03:00
|
|
|
var events = {};
|
|
|
|
utils.defineProperty(this, '_events', events);
|
|
|
|
|
2018-03-07 02:40:11 +03:00
|
|
|
// We use this to track recent emitted events; for example, if we emit a "block" of 100
|
|
|
|
// and we get a `getBlock(100)` request which would result in null, we should retry
|
|
|
|
// until we get a response. This provides devs with a consistent view. Similarly for
|
|
|
|
// transaction hashes.
|
|
|
|
utils.defineProperty(this, '_emitted', { block: -1 });
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
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++) {
|
2018-03-07 02:40:11 +03:00
|
|
|
if (self._emitted.block < i) {
|
|
|
|
self._emitted.block = i;
|
|
|
|
|
|
|
|
// Evict any transaction hashes or block hashes over 12 blocks
|
|
|
|
// old, since they should not return null anyways
|
|
|
|
Object.keys(self._emitted).forEach(function(key) {
|
|
|
|
if (key === 'block') { return; }
|
|
|
|
|
|
|
|
if (self._emitted[key] > i + 12) {
|
|
|
|
delete self._emitted[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-02-25 09:23:48 +03:00
|
|
|
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) {
|
2018-03-07 02:40:11 +03:00
|
|
|
if (!transaction || transaction.blockNumber == null) { return; }
|
|
|
|
self._emitted['t:' + transaction.hash.toLowerCase()] = transaction.blockNumber;
|
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) {
|
2018-03-07 02:40:11 +03:00
|
|
|
self._emitted['b:' + log.blockHash.toLowerCase()] = log.blockNumber;
|
|
|
|
self._emitted['t:' + log.transactionHash.toLowerCase()] = log.blockNumber;
|
2017-03-01 10:30:37 +03:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2018-03-15 23:03:20 +03:00
|
|
|
var pollingInterval = 4000;
|
|
|
|
|
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) {
|
2018-03-15 23:03:20 +03:00
|
|
|
poller = setInterval(doPoll, pollingInterval);
|
2017-03-01 10:30:37 +03:00
|
|
|
|
|
|
|
} else if (!value && poller) {
|
|
|
|
clearInterval(poller);
|
|
|
|
poller = null;
|
|
|
|
}
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
});
|
2018-03-15 23:03:20 +03:00
|
|
|
|
|
|
|
Object.defineProperty(this, 'pollingInterval', {
|
|
|
|
get: function() { return pollingInterval; },
|
|
|
|
set: function(value) {
|
|
|
|
if (typeof(value) !== 'number' || value <= 0 || parseInt(value) != value) {
|
|
|
|
throw new Error('invalid polling interval');
|
|
|
|
}
|
|
|
|
|
|
|
|
pollingInterval = value;
|
|
|
|
|
|
|
|
if (poller) {
|
|
|
|
clearInterval(poller);
|
|
|
|
poller = setInterval(doPoll, pollingInterval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// @TODO: Add .poller which must be an event emitter with a 'start', 'stop' and 'block' event;
|
|
|
|
// this will be used once we move to the WebSocket or other alternatives to polling
|
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
|
|
|
|
2018-03-05 03:31:09 +03:00
|
|
|
utils.defineProperty(Provider, 'getNetwork', function(network) {
|
2017-10-26 02:58:21 +03:00
|
|
|
|
2018-03-05 03:31:09 +03:00
|
|
|
if (typeof(network) === 'string') {
|
2017-10-26 02:58:21 +03:00
|
|
|
network = networks[network];
|
|
|
|
if (!network) { throw new Error('unknown network'); }
|
2017-10-29 00:00:59 +03:00
|
|
|
|
|
|
|
} else if (network == null) {
|
|
|
|
network = networks['homestead'];
|
2017-10-26 02:58:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(network.chainId) !== 'number') { throw new Error('invalid chainId'); }
|
|
|
|
|
|
|
|
return network;
|
|
|
|
});
|
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) {
|
2018-04-14 23:10:26 +03:00
|
|
|
var headers = [ ];
|
|
|
|
|
|
|
|
if (typeof(url) === 'object' && url.url != null && url.user != null && url.password != null) {
|
|
|
|
if (url.url.substring(0, 6) !== 'https:' && url.forceInsecure !== true) {
|
|
|
|
errors.throwError('basic authentication requires a secure https url', errors.INVALID_ARGUMENT, { arg: 'url', url: url.url, user: url.user, password: '[REDACTED]' });
|
|
|
|
}
|
|
|
|
|
|
|
|
var authorization = url.user + ':' + url.password;
|
|
|
|
headers.push({
|
|
|
|
key: 'Authorization',
|
|
|
|
value: 'Basic ' + utils.base64.encode(utils.toUtf8Bytes(authorization))
|
|
|
|
});
|
|
|
|
|
|
|
|
url = url.url;
|
|
|
|
}
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
|
|
|
|
if (json) {
|
|
|
|
request.open('POST', url, true);
|
2018-04-14 23:10:26 +03:00
|
|
|
headers.push({ key: 'Content-Type', value: 'application/json' });
|
2017-02-25 09:23:48 +03:00
|
|
|
} else {
|
|
|
|
request.open('GET', url, true);
|
|
|
|
}
|
|
|
|
|
2018-04-14 23:10:26 +03:00
|
|
|
headers.forEach(function(header) {
|
|
|
|
request.setRequestHeader(header.key, header.value);
|
|
|
|
});
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
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;
|
2018-03-28 00:19:46 +03:00
|
|
|
jsonError.url = url;
|
2017-02-25 09:23:48 +03:00
|
|
|
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),
|
2018-03-05 10:52:53 +03:00
|
|
|
position: utils.hexStripZeros(utils.hexlify(position)),
|
2017-05-22 01:01:23 +03:00
|
|
|
};
|
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;
|
2017-12-30 04:41:16 +03:00
|
|
|
return this._resolveNames(transaction, [ 'to', 'from' ]).then(function(transaction) {
|
2017-05-20 22:42:16 +03:00
|
|
|
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;
|
2017-12-30 04:41:16 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2018-03-07 02:40:11 +03:00
|
|
|
function stallPromise(allowNullFunc, executeFunc) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var attempt = 0;
|
|
|
|
function check() {
|
|
|
|
executeFunc().then(function(result) {
|
|
|
|
// If we have a result, or are allowed null then we're done
|
|
|
|
if (result || allowNullFunc()) {
|
|
|
|
resolve(result);
|
|
|
|
|
|
|
|
// Otherwise, exponential back-off (up to 10s) our next request
|
|
|
|
} else {
|
|
|
|
attempt++;
|
|
|
|
var timeout = 500 + 250 * parseInt(Math.random() * (1 << attempt));
|
|
|
|
if (timeout > 10000) { timeout = 10000; }
|
|
|
|
setTimeout(check, timeout);
|
|
|
|
}
|
|
|
|
}, function(error) {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
check();
|
|
|
|
});
|
|
|
|
}
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'getBlock', function(blockHashOrBlockTag) {
|
2018-03-07 02:40:11 +03:00
|
|
|
var self = this;
|
2017-02-25 09:23:48 +03:00
|
|
|
try {
|
|
|
|
var blockHash = utils.hexlify(blockHashOrBlockTag);
|
|
|
|
if (blockHash.length === 66) {
|
2018-03-07 02:40:11 +03:00
|
|
|
return stallPromise(function() {
|
|
|
|
return (self._emitted['b:' + blockHash.toLowerCase()] == null);
|
|
|
|
}, function() {
|
|
|
|
return self.perform('getBlock', {blockHash: blockHash}).then(function(block) {
|
|
|
|
if (block == null) { return null; }
|
|
|
|
return checkBlock(block);
|
|
|
|
});
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
}
|
2018-03-07 02:40:11 +03:00
|
|
|
} catch (error) { }
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
var blockTag = checkBlockTag(blockHashOrBlockTag);
|
2018-03-07 02:40:11 +03:00
|
|
|
return stallPromise(function() {
|
|
|
|
if (utils.isHexString(blockTag)) {
|
|
|
|
var blockNumber = parseInt(blockTag.substring(2), 16);
|
|
|
|
return blockNumber > self._emitted.block;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}, function() {
|
|
|
|
return self.perform('getBlock', { blockTag: blockTag }).then(function(block) {
|
|
|
|
if (block == null) { return null; }
|
|
|
|
return checkBlock(block);
|
|
|
|
});
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
2018-03-07 02:40:11 +03:00
|
|
|
} catch (error) { }
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
return Promise.reject(new Error('invalid block hash or block tag'));
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, 'getTransaction', function(transactionHash) {
|
2018-03-07 02:40:11 +03:00
|
|
|
var self = this;
|
2017-02-25 09:23:48 +03:00
|
|
|
try {
|
2017-05-22 01:01:23 +03:00
|
|
|
var params = { transactionHash: checkHash(transactionHash) };
|
2018-03-07 02:40:11 +03:00
|
|
|
return stallPromise(function() {
|
|
|
|
return (self._emitted['t:' + transactionHash.toLowerCase()] == null);
|
|
|
|
}, function() {
|
|
|
|
return self.perform('getTransaction', params).then(function(result) {
|
|
|
|
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) {
|
2018-03-07 02:40:11 +03:00
|
|
|
var self = this;
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
try {
|
2017-05-22 01:01:23 +03:00
|
|
|
var params = { transactionHash: checkHash(transactionHash) };
|
2018-03-07 02:40:11 +03:00
|
|
|
return stallPromise(function() {
|
|
|
|
return (self._emitted['t:' + transactionHash.toLowerCase()] == null);
|
|
|
|
}, function() {
|
|
|
|
return self.perform('getTransactionReceipt', params).then(function(result) {
|
|
|
|
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) { }
|
|
|
|
|
2018-01-10 02:43:09 +03:00
|
|
|
if (!this.ensAddress) { throw new Error('network does not have ENS deployed'); }
|
|
|
|
|
2017-10-04 02:37:06 +03:00
|
|
|
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) {
|
2018-01-10 02:43:09 +03:00
|
|
|
if (!this.ensAddress) { throw new Error('network does not have ENS deployed'); }
|
|
|
|
|
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';
|
|
|
|
|
2017-12-30 04:41:16 +03:00
|
|
|
} else if (object === 'pending') {
|
|
|
|
return 'pending';
|
|
|
|
|
2017-03-01 10:30:37 +03:00
|
|
|
} 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-12-30 04:41:16 +03:00
|
|
|
} else if (string === 'pending') {
|
|
|
|
return {type: 'pending'};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-12-30 04:41:16 +03:00
|
|
|
utils.defineProperty(Provider.prototype, '_startPending', function() {
|
|
|
|
console.log('WARNING: this provider does not support pending events');
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Provider.prototype, '_stopPending', function() {
|
|
|
|
});
|
|
|
|
|
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-12-30 04:41:16 +03:00
|
|
|
if (key === 'pending') { this._startPending(); }
|
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'});
|
2017-12-30 04:41:16 +03:00
|
|
|
if (key === 'pending') { this._startPending(); }
|
2017-03-01 10:30:37 +03:00
|
|
|
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-12-30 04:41:16 +03:00
|
|
|
if (listeners.length === 0) {
|
|
|
|
delete this._events[key];
|
|
|
|
if (key === 'pending') { this._stopPending(); }
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-03-05 03:31:09 +03:00
|
|
|
var eventNameString = getEventString(eventName);
|
|
|
|
var listeners = this._events[eventNameString];
|
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);
|
2018-03-05 03:31:09 +03:00
|
|
|
break;
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
}
|
2018-03-05 03:31:09 +03:00
|
|
|
|
|
|
|
if (listeners.length === 0) {
|
|
|
|
this.removeAllListeners(eventName);
|
|
|
|
}
|
2017-02-25 09:23:48 +03:00
|
|
|
});
|
|
|
|
|
2017-12-30 04:41:16 +03:00
|
|
|
utils.defineProperty(Provider, '_formatters', {
|
|
|
|
checkTransactionResponse: checkTransaction
|
|
|
|
});
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
module.exports = Provider;
|