Skip to content

Commit b3853b3

Browse files
ryanioholgerd77
authored andcommitted
simplify latest block check
1 parent cef7c8c commit b3853b3

File tree

1 file changed

+48
-42
lines changed
  • packages/client/lib/rpc/modules

1 file changed

+48
-42
lines changed

packages/client/lib/rpc/modules/eth.ts

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class Eth {
103103

104104
/**
105105
* Executes a new message call immediately without creating a transaction on the block chain.
106-
* Currently only "latest" block number is supported.
106+
* Currently only "latest" block is supported.
107107
* @param params An array of two parameters:
108108
* 1. The transaction object
109109
* * from (optional) - The address the transaction is sent from
@@ -118,12 +118,13 @@ export class Eth {
118118
async call(params: [RpcCallTx, string]) {
119119
const [transaction, blockOpt] = params
120120

121-
const latestBlockNumber = await this.blockNumber()
122-
if (blockOpt !== 'latest' && blockOpt !== latestBlockNumber) {
123-
// todo: this can be resolved with some kind of functionality of stateAt(blockNumber)
124-
return {
125-
code: INVALID_PARAMS,
126-
message: `Currently only block option "latest" supported`,
121+
if (blockOpt !== 'latest') {
122+
const latest = await this.blockNumber()
123+
if (blockOpt !== latest) {
124+
return {
125+
code: INVALID_PARAMS,
126+
message: `Currently only "latest" block supported`,
127+
}
127128
}
128129
}
129130

@@ -156,7 +157,7 @@ export class Eth {
156157
* The transaction will not be added to the blockchain.
157158
* Note that the estimate may be significantly more than the amount of gas actually used by the transaction,
158159
* for a variety of reasons including EVM mechanics and node performance.
159-
* Currently only "latest" block number is supported.
160+
* Currently only "latest" block is supported.
160161
* @param params An array of two parameters:
161162
* 1. The transaction object
162163
* * from (optional) - The address the transaction is sent from
@@ -171,12 +172,13 @@ export class Eth {
171172
async estimateGas(params: [RpcCallTx, string]) {
172173
const [transaction, blockOpt] = params
173174

174-
const latestBlockNumber = await this.blockNumber()
175-
if (blockOpt !== 'latest' && blockOpt !== latestBlockNumber) {
176-
// todo: this can be resolved with some kind of functionality of stateAt(blockNumber)
177-
return {
178-
code: INVALID_PARAMS,
179-
message: `Currently only block option "latest" supported`,
175+
if (blockOpt !== 'latest') {
176+
const latest = await this.blockNumber()
177+
if (blockOpt !== latest) {
178+
return {
179+
code: INVALID_PARAMS,
180+
message: `Currently only "latest" block supported`,
181+
}
180182
}
181183
}
182184

@@ -211,20 +213,21 @@ export class Eth {
211213

212214
/**
213215
* Returns the balance of the account at the given address.
214-
* Currently only "latest" block number is supported.
216+
* Currently only "latest" block is supported.
215217
* @param params An array of two parameters:
216218
* 1. address of the account
217219
* 2. integer block number, or the string "latest", "earliest" or "pending"
218220
*/
219221
async getBalance(params: [string, string]) {
220222
const [addressHex, blockOpt] = params
221223

222-
const latestBlockNumber = await this.blockNumber()
223-
if (blockOpt !== 'latest' && blockOpt !== latestBlockNumber) {
224-
// todo: this can be resolved with some kind of functionality of stateAt(blockNumber)
225-
return {
226-
code: INVALID_PARAMS,
227-
message: `Currently only block option "latest" supported`,
224+
if (blockOpt !== 'latest') {
225+
const latest = await this.blockNumber()
226+
if (blockOpt !== latest) {
227+
return {
228+
code: INVALID_PARAMS,
229+
message: `Currently only "latest" block supported`,
230+
}
228231
}
229232
}
230233

@@ -284,20 +287,21 @@ export class Eth {
284287

285288
/**
286289
* Returns code of the account at the given address.
287-
* Currently only "latest" block number is supported.
290+
* Currently only "latest" block is supported.
288291
* @param params An array of two parameters:
289292
* 1. address of the account
290293
* 2. integer block number, or the string "latest", "earliest" or "pending"
291294
*/
292295
async getCode(params: [string, string]) {
293296
const [addressHex, blockOpt] = params
294297

295-
const latestBlockNumber = await this.blockNumber()
296-
if (blockOpt !== 'latest' && blockOpt !== latestBlockNumber) {
297-
// todo: this can be resolved with some kind of functionality of stateAt(blockNumber)
298-
return {
299-
code: INVALID_PARAMS,
300-
message: `Currently only block option "latest" supported`,
298+
if (blockOpt !== 'latest') {
299+
const latest = await this.blockNumber()
300+
if (blockOpt !== latest) {
301+
return {
302+
code: INVALID_PARAMS,
303+
message: `Currently only "latest" block supported`,
304+
}
301305
}
302306
}
303307

@@ -311,7 +315,7 @@ export class Eth {
311315

312316
/**
313317
* Returns the value from a storage position at a given address.
314-
* Currently only "latest" block number is supported.
318+
* Currently only "latest" block is supported.
315319
* @param params An array of three parameters:
316320
* 1. address of the storage
317321
* 2. integer of the position in the storage
@@ -320,12 +324,13 @@ export class Eth {
320324
async getStorageAt(params: [string, string, string]) {
321325
const [addressHex, positionHex, blockOpt] = params
322326

323-
const latestBlockNumber = await this.blockNumber()
324-
if (blockOpt !== 'latest' && blockOpt !== latestBlockNumber) {
325-
// todo: this can be resolved with some kind of functionality of stateAt(blockNumber)
326-
return {
327-
code: INVALID_PARAMS,
328-
message: `Currently only block option "latest" supported`,
327+
if (blockOpt !== 'latest') {
328+
const latest = await this.blockNumber()
329+
if (blockOpt !== latest) {
330+
return {
331+
code: INVALID_PARAMS,
332+
message: `Currently only "latest" block supported`,
333+
}
329334
}
330335
}
331336

@@ -342,20 +347,21 @@ export class Eth {
342347

343348
/**
344349
* Returns the number of transactions sent from an address.
345-
* Currently only "latest" block number is supported.
350+
* Currently only "latest" block is supported.
346351
* @param params An array of two parameters:
347352
* 1. address of the account
348353
* 2. integer block number, or the string "latest", "earliest" or "pending"
349354
*/
350355
async getTransactionCount(params: [string, string]) {
351356
const [addressHex, blockOpt] = params
352357

353-
const latestBlockNumber = await this.blockNumber()
354-
if (blockOpt !== 'latest' && blockOpt !== latestBlockNumber) {
355-
// todo: this can be resolved with some kind of functionality of stateAt(blockNumber)
356-
return {
357-
code: INVALID_PARAMS,
358-
message: `Currently only block option "latest" supported`,
358+
if (blockOpt !== 'latest') {
359+
const latest = await this.blockNumber()
360+
if (blockOpt !== latest) {
361+
return {
362+
code: INVALID_PARAMS,
363+
message: `Currently only "latest" block supported`,
364+
}
359365
}
360366
}
361367

0 commit comments

Comments
 (0)