Skip to content

Commit

Permalink
fix: use maxUint256 instead of 2n ** 256n - 1n (#2692)
Browse files Browse the repository at this point in the history
* fix: Always use maxUint256 instead of 2n ** 256n - 1n

- Improve code readability
- Fix error: *identifier starts immediately after numeric literal*

* Create seven-emus-fetch.md

---------

Co-authored-by: jxom <[email protected]>
  • Loading branch information
frapsMatheus and jxom authored Sep 5, 2024
1 parent 764f259 commit 70814af
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-emus-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Replaced instances of `2n ** 256n - 1n` with the `maxUint256` constant.
3 changes: 2 additions & 1 deletion src/actions/public/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
createClient,
decodeFunctionResult,
encodeAbiParameters,
maxUint256,
multicall3Abi,
pad,
parseEther,
Expand Down Expand Up @@ -247,7 +248,7 @@ describe('errors', () => {
data: `${mintWithParams4bytes}${fourTwenty}`,
account: sourceAccount.address,
to: wagmiContractAddress,
maxFeePerGas: 2n ** 256n - 1n + 1n,
maxFeePerGas: maxUint256 + 1n,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`
[CallExecutionError: The fee cap (\`maxFeePerGas\` = 115792089237316195423570985008687907853269984665640564039457584007913.129639936 gwei) cannot be higher than the maximum allowed value (2^256-1).
Expand Down
2 changes: 1 addition & 1 deletion src/actions/public/estimateGas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ describe('errors', () => {
account: accounts[0].address,
to: accounts[1].address,
value: parseEther('1'),
maxFeePerGas: 2n ** 256n - 1n + 1n,
maxFeePerGas: maxUint256 + 1n,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`
[EstimateGasExecutionError: The fee cap (\`maxFeePerGas\` = 115792089237316195423570985008687907853269984665640564039457584007913.129639936 gwei) cannot be higher than the maximum allowed value (2^256-1).
Expand Down
3 changes: 2 additions & 1 deletion src/actions/public/simulateContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { parseEther } from '../../utils/unit/parseEther.js'
import { parseGwei } from '../../utils/unit/parseGwei.js'
import { mine } from '../test/mine.js'

import { maxUint256 } from '~viem/constants/number.js'
import { simulateContract } from './simulateContract.js'

const client = anvilMainnet
Expand Down Expand Up @@ -545,7 +546,7 @@ describe('node errors', () => {
account: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
functionName: 'mint',
args: [13371337n],
maxFeePerGas: 2n ** 256n - 1n + 1n,
maxFeePerGas: maxUint256 + 1n,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`
[ContractFunctionExecutionError: The fee cap (\`maxFeePerGas\` = 115792089237316195423570985008687907853269984665640564039457584007913.129639936 gwei) cannot be higher than the maximum allowed value (2^256-1).
Expand Down
5 changes: 3 additions & 2 deletions src/actions/wallet/sendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { anvilMainnet } from '../../../test/src/anvil.js'
import { privateKeyToAccount } from '../../accounts/privateKeyToAccount.js'
import { celo, localhost, mainnet, optimism } from '../../chains/index.js'

import { maxUint256 } from '~viem/constants/number.js'
import { BatchCallInvoker } from '../../../contracts/generated.js'
import { getSmartAccounts_07 } from '../../../test/src/account-abstraction.js'
import { deploy } from '../../../test/src/utils.js'
Expand Down Expand Up @@ -1174,7 +1175,7 @@ describe('errors', () => {
sendTransaction(client, {
to: targetAccount.address,
value: parseEther('1'),
maxFeePerGas: 2n ** 256n - 1n + 1n,
maxFeePerGas: maxUint256 + 1n,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`
[AccountNotFoundError: Could not find an Account to execute with this Action.
Expand All @@ -1193,7 +1194,7 @@ describe('errors', () => {
account: sourceAccount.address,
to: targetAccount.address,
value: parseEther('1'),
maxFeePerGas: 2n ** 256n - 1n + 1n,
maxFeePerGas: maxUint256 + 1n,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`
[TransactionExecutionError: The fee cap (\`maxFeePerGas\` = 115792089237316195423570985008687907853269984665640564039457584007913.129639936 gwei) cannot be higher than the maximum allowed value (2^256-1).
Expand Down
5 changes: 3 additions & 2 deletions src/celo/serializers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { maxUint256 } from '~viem/constants/number.js'

Check failure on line 1 in src/celo/serializers.ts

View workflow job for this annotation

GitHub Actions / Verify / Test Environments (bun)

Cannot find module '~viem/constants/number.js' or its corresponding type declarations.

Check failure on line 1 in src/celo/serializers.ts

View workflow job for this annotation

GitHub Actions / Verify / Build

Cannot find module '~viem/constants/number.js' or its corresponding type declarations.
import { InvalidAddressError } from '../errors/address.js'
import { BaseError } from '../errors/base.js'
import { InvalidChainIdError } from '../errors/chain.js'
Expand Down Expand Up @@ -75,8 +76,8 @@ function serializeTransactionCIP64(
]) as SerializeTransactionCIP64ReturnType
}

// maxFeePerGas must be less than 2^256 - 1
const MAX_MAX_FEE_PER_GAS = 2n ** 256n - 1n
// maxFeePerGas must be less than maxUint256
const MAX_MAX_FEE_PER_GAS = maxUint256

export function assertTransactionCIP42(
transaction: TransactionSerializableCIP42,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/transaction/assertRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, test } from 'vitest'

import { parseGwei } from '../unit/parseGwei.js'

import { maxUint256 } from '~viem/constants/number.js'
import { assertRequest } from './assertRequest.js'

test('invalid address', () => {
Expand All @@ -19,7 +20,7 @@ test('invalid address', () => {

test('fee cap too high', () => {
expect(() =>
assertRequest({ maxFeePerGas: 2n ** 256n - 1n + 1n }),
assertRequest({ maxFeePerGas: maxUint256 + 1n }),
).toThrowErrorMatchingInlineSnapshot(`
[FeeCapTooHighError: The fee cap (\`maxFeePerGas\` = 115792089237316195423570985008687907853269984665640564039457584007913.129639936 gwei) cannot be higher than the maximum allowed value (2^256-1).
Expand Down
3 changes: 2 additions & 1 deletion src/utils/transaction/assertRequest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { maxUint256 } from '~viem/constants/number.js'

Check failure on line 1 in src/utils/transaction/assertRequest.ts

View workflow job for this annotation

GitHub Actions / Verify / Test Environments (bun)

Cannot find module '~viem/constants/number.js' or its corresponding type declarations.

Check failure on line 1 in src/utils/transaction/assertRequest.ts

View workflow job for this annotation

GitHub Actions / Verify / Build

Cannot find module '~viem/constants/number.js' or its corresponding type declarations.
import {
type ParseAccountErrorType,
parseAccount,
Expand Down Expand Up @@ -53,7 +54,7 @@ export function assertRequest(args: AssertRequestParameters) {
)
throw new FeeConflictError()

if (maxFeePerGas && maxFeePerGas > 2n ** 256n - 1n)
if (maxFeePerGas && maxFeePerGas > maxUint256)
throw new FeeCapTooHighError({ maxFeePerGas })
if (
maxPriorityFeePerGas &&
Expand Down
11 changes: 6 additions & 5 deletions src/utils/transaction/assertTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, test } from 'vitest'

import { parseGwei } from '../unit/parseGwei.js'

import { maxUint256 } from '~viem/constants/number.js'
import {
assertTransactionEIP1559,
assertTransactionEIP2930,
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('eip7702', () => {
yParity: 0,
},
],
maxFeePerGas: 2n ** 256n - 1n + 1n,
maxFeePerGas: maxUint256 + 1n,
chainId: 1,
}),
).toThrowErrorMatchingInlineSnapshot(`
Expand Down Expand Up @@ -136,7 +137,7 @@ describe('eip4844', () => {
blobVersionedHashes: [
'0x01febabecafebabecafebabecafebabecafebabecafebabecafebabecafebabe',
],
maxFeePerGas: 2n ** 256n - 1n + 1n,
maxFeePerGas: maxUint256 + 1n,
chainId: 1,
}),
).toThrowErrorMatchingInlineSnapshot(`
Expand All @@ -151,7 +152,7 @@ describe('eip1559', () => {
test('fee cap too high', () => {
expect(() =>
assertTransactionEIP1559({
maxFeePerGas: 2n ** 256n - 1n + 1n,
maxFeePerGas: maxUint256 + 1n,
chainId: 1,
}),
).toThrowErrorMatchingInlineSnapshot(`
Expand Down Expand Up @@ -203,7 +204,7 @@ describe('eip2930', () => {
test('fee cap too high', () => {
expect(() =>
assertTransactionEIP2930({
gasPrice: 2n ** 256n - 1n + 1n,
gasPrice: maxUint256 + 1n,
chainId: 1,
}),
).toThrowErrorMatchingInlineSnapshot(`
Expand Down Expand Up @@ -254,7 +255,7 @@ describe('legacy', () => {
test('fee cap too high', () => {
expect(() =>
assertTransactionLegacy({
gasPrice: 2n ** 256n - 1n + 1n,
gasPrice: maxUint256 + 1n,
chainId: 1,
}),
).toThrowErrorMatchingInlineSnapshot(`
Expand Down
7 changes: 4 additions & 3 deletions src/utils/transaction/assertTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { maxUint256 } from '~viem/constants/number.js'

Check failure on line 1 in src/utils/transaction/assertTransaction.ts

View workflow job for this annotation

GitHub Actions / Verify / Test Environments (bun)

Cannot find module '~viem/constants/number.js' or its corresponding type declarations.

Check failure on line 1 in src/utils/transaction/assertTransaction.ts

View workflow job for this annotation

GitHub Actions / Verify / Build

Cannot find module '~viem/constants/number.js' or its corresponding type declarations.
import { versionedHashVersionKzg } from '../../constants/kzg.js'
import {
InvalidAddressError,
Expand Down Expand Up @@ -99,7 +100,7 @@ export function assertTransactionEIP1559(
const { chainId, maxPriorityFeePerGas, maxFeePerGas, to } = transaction
if (chainId <= 0) throw new InvalidChainIdError({ chainId })
if (to && !isAddress(to)) throw new InvalidAddressError({ address: to })
if (maxFeePerGas && maxFeePerGas > 2n ** 256n - 1n)
if (maxFeePerGas && maxFeePerGas > maxUint256)
throw new FeeCapTooHighError({ maxFeePerGas })
if (
maxPriorityFeePerGas &&
Expand Down Expand Up @@ -128,7 +129,7 @@ export function assertTransactionEIP2930(
throw new BaseError(
'`maxFeePerGas`/`maxPriorityFeePerGas` is not a valid EIP-2930 Transaction attribute.',
)
if (gasPrice && gasPrice > 2n ** 256n - 1n)
if (gasPrice && gasPrice > maxUint256)
throw new FeeCapTooHighError({ maxFeePerGas: gasPrice })
}

Expand All @@ -152,6 +153,6 @@ export function assertTransactionLegacy(
throw new BaseError(
'`maxFeePerGas`/`maxPriorityFeePerGas` is not a valid Legacy Transaction attribute.',
)
if (gasPrice && gasPrice > 2n ** 256n - 1n)
if (gasPrice && gasPrice > maxUint256)
throw new FeeCapTooHighError({ maxFeePerGas: gasPrice })
}

0 comments on commit 70814af

Please sign in to comment.