2
0
mirror of synced 2025-02-24 20:18:07 +00:00

Added support for fetching block transactions with blocks.

This commit is contained in:
Richard Moore 2018-09-04 10:08:50 -04:00
parent b5408bcbd0
commit 32a070d909
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
5 changed files with 48 additions and 49 deletions

View File

@ -125,7 +125,7 @@ export abstract class Provider implements OnceBlockable {
abstract call(transaction: TransactionRequest): Promise<string>;
abstract estimateGas(transaction: TransactionRequest): Promise<BigNumber>;
abstract getBlock(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>): Promise<Block>;
abstract getBlock(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>, includeTransactions?: boolean): Promise<Block>;
abstract getTransaction(transactionHash: string): Promise<TransactionResponse>;
abstract getTransactionReceipt(transactionHash: string): Promise<TransactionReceipt>;

View File

@ -149,40 +149,7 @@ function checkBlockTag(blockTag: BlockTag): string {
throw new Error('invalid blockTag');
}
var formatBlock = {
hash: checkHash,
parentHash: checkHash,
number: checkNumber,
timestamp: checkNumber,
nonce: allowNull(hexlify),
difficulty: checkDifficulty,
gasLimit: bigNumberify,
gasUsed: bigNumberify,
miner: getAddress,
extraData: hexlify,
//transactions: allowNull(arrayOf(checkTransaction)),
transactions: allowNull(arrayOf(checkHash)),
//transactionRoot: checkHash,
//stateRoot: checkHash,
//sha3Uncles: checkHash,
//logsBloom: hexlify,
};
function checkBlock(block: any): Block {
if (block.author != null && block.miner == null) {
block.miner = block.author;
}
return check(formatBlock, block);
}
var formatTransaction = {
const formatTransaction = {
hash: checkHash,
blockHash: allowNull(checkHash, null),
@ -278,7 +245,35 @@ function checkTransactionResponse(transaction: any): TransactionResponse {
return result;
}
var formatTransactionRequest = {
const formatBlock = {
hash: checkHash,
parentHash: checkHash,
number: checkNumber,
timestamp: checkNumber,
nonce: allowNull(hexlify),
difficulty: checkDifficulty,
gasLimit: bigNumberify,
gasUsed: bigNumberify,
miner: getAddress,
extraData: hexlify,
transactions: allowNull(arrayOf(checkHash)),
};
const formatBlockWithTransactions = shallowCopy(formatBlock);
formatBlockWithTransactions.transactions = allowNull(arrayOf(checkTransactionResponse));
function checkBlock(block: any, includeTransactions: boolean): Block {
if (block.author != null && block.miner == null) {
block.miner = block.author;
}
return check(includeTransactions ? formatBlockWithTransactions: formatBlock, block);
}
const formatTransactionRequest = {
from: allowNull(getAddress),
nonce: allowNull(checkNumber),
gasLimit: allowNull(bigNumberify),
@ -292,7 +287,7 @@ function checkTransactionRequest(transaction: any): any {
return check(formatTransactionRequest, transaction);
}
var formatTransactionReceiptLog = {
const formatTransactionReceiptLog = {
transactionLogIndex: allowNull(checkNumber),
transactionIndex: checkNumber,
blockNumber: checkNumber,
@ -308,7 +303,7 @@ function checkTransactionReceiptLog(log: any): any {
return check(formatTransactionReceiptLog, log);
}
var formatTransactionReceipt = {
const formatTransactionReceipt = {
contractAddress: allowNull(getAddress, null),
transactionIndex: checkNumber,
root: allowNull(checkHash),
@ -351,7 +346,7 @@ function checkTopics(topics: any): any {
return topics;
}
var formatFilter = {
const formatFilter = {
fromBlock: allowNull(checkBlockTag, undefined),
toBlock: allowNull(checkBlockTag, undefined),
address: allowNull(getAddress, undefined),
@ -362,7 +357,7 @@ function checkFilter(filter: any): any {
return check(formatFilter, filter);
}
var formatLog = {
const formatLog = {
blockNumber: allowNull(checkNumber),
blockHash: allowNull(checkHash),
transactionIndex: checkNumber,
@ -832,21 +827,21 @@ export class BaseProvider extends Provider {
});
}
getBlock(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>): Promise<Block> {
getBlock(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>, includeTransactions?: boolean): Promise<Block> {
return this.ready.then(() => {
return resolveProperties({ blockHashOrBlockTag: blockHashOrBlockTag }).then(({ blockHashOrBlockTag }) => {
try {
var blockHash = hexlify(blockHashOrBlockTag);
if (hexDataLength(blockHash) === 32) {
return poll(() => {
return this.perform('getBlock', { blockHash: blockHash }).then((block) => {
return this.perform('getBlock', { blockHash: blockHash, includeTransactions: !!includeTransactions }).then((block) => {
if (block == null) {
if (this._emitted['b:' + blockHash] == null) {
return null;
}
return undefined;
}
return checkBlock(block);
return checkBlock(block, includeTransactions);
});
}, { onceBlock: this });
@ -862,14 +857,14 @@ export class BaseProvider extends Provider {
}
return poll(() => {
return this.perform('getBlock', { blockTag: blockTag }).then((block) => {
return this.perform('getBlock', { blockTag: blockTag, includeTransactions: !!includeTransactions }).then((block) => {
if (block == null) {
if (blockNumber > this._emitted.block) {
return undefined;
}
return null;
}
return checkBlock(block);
return checkBlock(block, includeTransactions);
});
}, { onceBlock: this });
} catch (error) { }

View File

@ -170,7 +170,11 @@ export class EtherscanProvider extends BaseProvider{
case 'getBlock':
if (params.blockTag) {
url += '/api?module=proxy&action=eth_getBlockByNumber&tag=' + params.blockTag;
url += '&boolean=false';
if (params.includeTransactions) {
url += '&boolean=true';
} else {
url += '&boolean=false';
}
url += apiKey;
return fetchJson(url, null, getJsonResult);
}

View File

@ -78,7 +78,7 @@ export class FallbackProvider extends BaseProvider {
return this._providers.slice(0);
}
perform(method: string, params: any): any {
perform(method: string, params: { [name: string]: any }): any {
// Creates a copy of the providers array
var providers = this.providers;

View File

@ -283,9 +283,9 @@ export class JsonRpcProvider extends BaseProvider {
case 'getBlock':
if (params.blockTag) {
return this.send('eth_getBlockByNumber', [ params.blockTag, false ]);
return this.send('eth_getBlockByNumber', [ params.blockTag, !!params.includeTransactions ]);
} else if (params.blockHash) {
return this.send('eth_getBlockByHash', [ params.blockHash, false ]);
return this.send('eth_getBlockByHash', [ params.blockHash, !!params.includeTransactions ]);
}
return Promise.reject(new Error('invalid block tag or block hash'));