Skip to content

Commit

Permalink
Added support for fetching block transactions with blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Sep 4, 2018
1 parent b5408bc commit 32a070d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src.ts/providers/abstract-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down
83 changes: 39 additions & 44 deletions src.ts/providers/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -292,7 +287,7 @@ function checkTransactionRequest(transaction: any): any {
return check(formatTransactionRequest, transaction);
}

var formatTransactionReceiptLog = {
const formatTransactionReceiptLog = {
transactionLogIndex: allowNull(checkNumber),
transactionIndex: checkNumber,
blockNumber: checkNumber,
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand Down Expand Up @@ -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 });

Expand All @@ -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) { }
Expand Down
6 changes: 5 additions & 1 deletion src.ts/providers/etherscan-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src.ts/providers/fallback-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src.ts/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down

0 comments on commit 32a070d

Please sign in to comment.