Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes defaultBlock property handling #3247

Merged
merged 7 commits into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,5 @@ Released with 1.0.0-beta.37 code base.
- ``getPendingTransactions`` added to web3-eth package (#3239)

### Fixed

- ``defaultBlock`` property handling fixed (#3247)
3 changes: 2 additions & 1 deletion docs/web3-eth-contract.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@ Property

The default block parameters can be one of the following:

- ``Number``: A block number
- ``Number|BN|BigNumber``: A block number
- ``"genesis"`` - ``String``: The genesis block
- ``"latest"`` - ``String``: The latest block (current head of the blockchain)
- ``"pending"`` - ``String``: The currently mined block (including pending transactions)
- ``"earliest"`` - ``String``: The genesis block

Default is ``"latest"``

Expand Down
3 changes: 2 additions & 1 deletion docs/web3-eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,11 @@ Property

Default block parameters can be one of the following:

- ``Number``: A block number
- ``Number|BN|BigNumber``: A block number
- ``"genesis"`` - ``String``: The genesis block
- ``"latest"`` - ``String``: The latest block (current head of the blockchain)
- ``"pending"`` - ``String``: The currently mined block (including pending transactions)
- ``"earliest"`` - ``String``: The genesis block

Default is ``"latest"``

Expand Down
19 changes: 12 additions & 7 deletions packages/web3-core-helpers/src/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,32 @@ var isPredefinedBlockNumber = function (blockNumber) {
*/
var inputDefaultBlockNumberFormatter = function (blockNumber) {
if (this && (blockNumber === undefined || blockNumber === null)) {
return this.defaultBlock;
}
if (blockNumber === 'genesis' || blockNumber === 'earliest') {
return '0x0';
return inputBlockNumberFormatter(this.defaultBlock);
}

return inputBlockNumberFormatter(blockNumber);
};

/**
* Returns the given block number as hex string or the predefined block number 'latest', 'pending', 'earliest', 'genesis'
*
* @param {String|Number|undefined} blockNumber
* @param {String|Number|BN|BigNumber} blockNumber
*
* @returns {String|Number|BN|BigNumber}
* @returns {String}
*/
var inputBlockNumberFormatter = function (blockNumber) {
if (blockNumber === undefined) {
return undefined;
} else if (isPredefinedBlockNumber(blockNumber)) {
}

if (isPredefinedBlockNumber(blockNumber)) {
return blockNumber;
}

if (blockNumber === 'genesis') {
return '0x0';
}

return (utils.isHexStrict(blockNumber)) ? ((_.isString(blockNumber)) ? blockNumber.toLowerCase() : blockNumber) : utils.numberToHex(blockNumber);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ export interface LogsOptions {
topics?: Array<string | string[] | null>;
}

export type BlockNumber = string | number | BN | BigNumber | 'latest' | 'pending' | 'earliest';
export type BlockNumber = string | number | BN | BigNumber | 'latest' | 'pending' | 'earliest' | 'genesis';

export type provider =
| HttpProvider
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-contract/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Contract {
private _address: string;
private _jsonInterface: AbiItem[];
defaultAccount: string | null;
defaultBlock: string | number;
defaultBlock: BlockNumber;
defaultCommon: Common;
defaultHardfork: hardfork;
defaultChain: chain;
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-contract/types/tests/contract-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const contract = new Contract([]);
// $ExpectType string | null
contract.defaultAccount;

// $ExpectType string | number
// $ExpectType BlockNumber
contract.defaultBlock;

// $ExpectType Common
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class Eth {
readonly givenProvider: any;
static readonly givenProvider: any;
defaultAccount: string | null;
defaultBlock: string | number;
defaultBlock: BlockNumber;
defaultCommon: Common;
defaultHardfork: hardfork;
defaultChain: chain;
Expand Down
20 changes: 7 additions & 13 deletions packages/web3-eth/types/tests/eth.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,16 @@ const eth_empty = new Eth();
// $ExpectType Eth
const eth = new Eth('http://localhost:8545');

// $ExpectType provider
eth.currentProvider;

// $ExpectType any
eth.givenProvider;

// $ExpectType string | null
eth.defaultAccount;

// $ExpectType string | number
// $ExpectType BlockNumber
eth.defaultBlock;

// $ExpectType Common
Expand Down Expand Up @@ -123,18 +129,6 @@ eth.subscribe(
(error: Error, transactionHash: string) => {}
);

// $ExpectType provider
eth.currentProvider;

// $ExpectType any
eth.givenProvider;

// $ExpectType string | null
eth.defaultAccount;

// $ExpectType string | number
eth.defaultBlock;

// $ExpectType boolean
eth.setProvider('https://localhost:2100');

Expand Down
2 changes: 1 addition & 1 deletion test/formatters.inputDefaultBlockFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var tests = [
{ value: 'genesis', expected: '0x0' },
{ value: 'latest', expected: 'latest' },
{ value: 'pending', expected: 'pending' },
{ value: 'earliest', expected: '0x0' },
{ value: 'earliest', expected: 'earliest' },
{ value: 1, expected: '0x1' },
{ value: '0x1', expected: '0x1' }
];
Expand Down