From a3430636a9fba44c54961bd2e89718aafa09917c Mon Sep 17 00:00:00 2001 From: Muhammad-Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> Date: Tue, 28 May 2024 13:31:54 +0200 Subject: [PATCH] move constants to a separate file --- src/constants.ts | 157 +++++++++++++++++++++++++++++++++++++-- src/utils.ts | 158 +++------------------------------------- test/unit/utils.test.ts | 9 ++- 3 files changed, 167 insertions(+), 157 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index f475531..739a3b9 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,8 +1,151 @@ -export const ETH_ADDRESS = '0x0000000000000000000000000000000000000000'; +import * as web3Types from 'web3-types'; + export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; -export const BOOTLOADER_FORMAL_ADDRESS = '0x0000000000000000000000000000000000008001'; -export const CONTRACT_DEPLOYER_ADDRESS = '0x0000000000000000000000000000000000008006'; -export const L1_MESSENGER_ADDRESS = '0x0000000000000000000000000000000000008008'; -export const L2_ETH_TOKEN_ADDRESS = '0x000000000000000000000000000000000000800a'; -export const NONCE_HOLDER_ADDRESS = '0x0000000000000000000000000000000000008003'; -export const L1_TO_L2_ALIAS_OFFSET = '0x1111000000000000000000000000000000001111'; + +/** + * The address of the L1 `ETH` token. + * @constant + */ +export const ETH_ADDRESS: web3Types.Address = '0x0000000000000000000000000000000000000000'; + +/** + * The address of the L1 `ETH` token. + * @constant + */ +export const LEGACY_ETH_ADDRESS: web3Types.Address = '0x0000000000000000000000000000000000000000'; + +/** + * In the contracts the zero address can not be used, use one instead + * @constant + */ +export const ETH_ADDRESS_IN_CONTRACTS: web3Types.Address = + '0x0000000000000000000000000000000000000001'; + +/** + * The formal address for the `Bootloader`. + * @constant + */ +export const BOOTLOADER_FORMAL_ADDRESS: web3Types.Address = + '0x0000000000000000000000000000000000008001'; + +/** + * The address of the Contract deployer. + * @constant + */ +export const CONTRACT_DEPLOYER_ADDRESS: web3Types.Address = + '0x0000000000000000000000000000000000008006'; + +/** + * The address of the L1 messenger. + * @constant + */ +export const L1_MESSENGER_ADDRESS: web3Types.Address = '0x0000000000000000000000000000000000008008'; + +/** + * The address of the L2 `ETH` token. + * @constant + * @deprecated In favor of {@link L2_BASE_TOKEN_ADDRESS}. + */ +export const L2_ETH_TOKEN_ADDRESS: web3Types.Address = '0x000000000000000000000000000000000000800a'; + +/** + * The address of the base token. + * @constant + */ +export const L2_BASE_TOKEN_ADDRESS = '0x000000000000000000000000000000000000800a'; + +/** + * The address of the Nonce holder. + * @constant + */ +export const NONCE_HOLDER_ADDRESS: web3Types.Address = '0x0000000000000000000000000000000000008003'; + +/** + * Used for applying and undoing aliases on addresses during bridging from L1 to L2. + * @constant + */ +export const L1_TO_L2_ALIAS_OFFSET: web3Types.Address = + '0x1111000000000000000000000000000000001111'; + +/** + * The EIP1271 magic value used for signature validation in smart contracts. + * This predefined constant serves as a standardized indicator to signal successful + * signature validation by the contract. + * + * @constant + */ +export const EIP1271_MAGIC_VALUE = '0x1626ba7e'; + +/** + * Represents an EIP712 transaction type. + * + * @constant + */ +export const EIP712_TX_TYPE = 0x71; + +/** + * Represents a priority transaction operation on L2. + * + * @constant + */ +export const PRIORITY_OPERATION_L2_TX_TYPE = 0xff; + +/** + * The maximum bytecode length in bytes that can be deployed. + * + * @constant + */ +export const MAX_BYTECODE_LEN_BYTES: number = ((1 << 16) - 1) * 32; + +/** + * Numerator used in scaling the gas limit to ensure acceptance of `L1->L2` transactions. + * + * This constant is part of a coefficient calculation to adjust the gas limit to account for variations + * in the SDK estimation, ensuring the transaction will be accepted. + * + * @constant + */ +export const L1_FEE_ESTIMATION_COEF_NUMERATOR = 12; + +/** + * Denominator used in scaling the gas limit to ensure acceptance of `L1->L2` transactions. + * + * This constant is part of a coefficient calculation to adjust the gas limit to account for variations + * in the SDK estimation, ensuring the transaction will be accepted. + * + * @constant + */ +export const L1_FEE_ESTIMATION_COEF_DENOMINATOR = 10; + +/** + * Gas limit used for displaying the error messages when the + * users do not have enough fee when depositing ERC20 token from L1 to L2. + * + * @constant + */ +export const L1_RECOMMENDED_MIN_ERC20_DEPOSIT_GAS_LIMIT = 400_000; + +/** + * Gas limit used for displaying the error messages when the + * users do not have enough fee when depositing `ETH` token from L1 to L2. + * + * @constant + */ +export const L1_RECOMMENDED_MIN_ETH_DEPOSIT_GAS_LIMIT = 200_000; + +/** + * Default gas per pubdata byte for L2 transactions. + * This value is utilized when inserting a default value for type 2 + * and EIP712 type transactions. + * + * @constant + */ +// It is a realistic value, but it is large enough to fill into any batch regardless of the pubdata price. +export const DEFAULT_GAS_PER_PUBDATA_LIMIT = 50_000; + +/** + * The `L1->L2` transactions are required to have the following gas per pubdata byte. + * + * @constant + */ +export const REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_LIMIT = 800; diff --git a/src/utils.ts b/src/utils.ts index d3bdb9e..3177fdb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -34,6 +34,18 @@ import { IERC1271ABI } from './contracts/IERC1271'; import { IL1BridgeABI } from './contracts/IL1ERC20Bridge'; import { IL2BridgeABI } from './contracts/IL2Bridge'; import { INonceHolderABI } from './contracts/INonceHolder'; +import { + LEGACY_ETH_ADDRESS, + L2_BASE_TOKEN_ADDRESS, + ETH_ADDRESS_IN_CONTRACTS, + L1_MESSENGER_ADDRESS, + CONTRACT_DEPLOYER_ADDRESS, + MAX_BYTECODE_LEN_BYTES, + L1_TO_L2_ALIAS_OFFSET, + EIP1271_MAGIC_VALUE, + L1_FEE_ESTIMATION_COEF_NUMERATOR, + L1_FEE_ESTIMATION_COEF_DENOMINATOR, +} from './constants'; // import { RpcMethods } from './rpc.methods'; // to be used instead of the one at zksync-ethers: Provider from ./provider @@ -95,150 +107,6 @@ export const L2Bridge = new web3.Contract(IL2BridgeABI); */ export const NonceHolderContract = new web3.Contract(INonceHolderABI); -/** - * The address of the L1 `ETH` token. - * @constant - */ -export const ETH_ADDRESS: web3.Address = '0x0000000000000000000000000000000000000000'; - -/** - * The address of the L1 `ETH` token. - * @constant - */ -export const LEGACY_ETH_ADDRESS: web3.Address = '0x0000000000000000000000000000000000000000'; - -/** - * In the contracts the zero address can not be used, use one instead - * @constant - */ -export const ETH_ADDRESS_IN_CONTRACTS: web3.Address = '0x0000000000000000000000000000000000000001'; - -/** - * The formal address for the `Bootloader`. - * @constant - */ -export const BOOTLOADER_FORMAL_ADDRESS: web3.Address = '0x0000000000000000000000000000000000008001'; - -/** - * The address of the Contract deployer. - * @constant - */ -export const CONTRACT_DEPLOYER_ADDRESS: web3.Address = '0x0000000000000000000000000000000000008006'; - -/** - * The address of the L1 messenger. - * @constant - */ -export const L1_MESSENGER_ADDRESS: web3.Address = '0x0000000000000000000000000000000000008008'; - -/** - * The address of the L2 `ETH` token. - * @constant - * @deprecated In favor of {@link L2_BASE_TOKEN_ADDRESS}. - */ -export const L2_ETH_TOKEN_ADDRESS: web3.Address = '0x000000000000000000000000000000000000800a'; - -/** - * The address of the base token. - * @constant - */ -export const L2_BASE_TOKEN_ADDRESS = '0x000000000000000000000000000000000000800a'; - -/** - * The address of the Nonce holder. - * @constant - */ -export const NONCE_HOLDER_ADDRESS: web3.Address = '0x0000000000000000000000000000000000008003'; - -/** - * Used for applying and undoing aliases on addresses during bridging from L1 to L2. - * @constant - */ -export const L1_TO_L2_ALIAS_OFFSET: web3.Address = '0x1111000000000000000000000000000000001111'; - -/** - * The EIP1271 magic value used for signature validation in smart contracts. - * This predefined constant serves as a standardized indicator to signal successful - * signature validation by the contract. - * - * @constant - */ -export const EIP1271_MAGIC_VALUE = '0x1626ba7e'; - -/** - * Represents an EIP712 transaction type. - * - * @constant - */ -export const EIP712_TX_TYPE = 0x71; - -/** - * Represents a priority transaction operation on L2. - * - * @constant - */ -export const PRIORITY_OPERATION_L2_TX_TYPE = 0xff; - -/** - * The maximum bytecode length in bytes that can be deployed. - * - * @constant - */ -export const MAX_BYTECODE_LEN_BYTES: number = ((1 << 16) - 1) * 32; - -/** - * Numerator used in scaling the gas limit to ensure acceptance of `L1->L2` transactions. - * - * This constant is part of a coefficient calculation to adjust the gas limit to account for variations - * in the SDK estimation, ensuring the transaction will be accepted. - * - * @constant - */ -export const L1_FEE_ESTIMATION_COEF_NUMERATOR = 12; - -/** - * Denominator used in scaling the gas limit to ensure acceptance of `L1->L2` transactions. - * - * This constant is part of a coefficient calculation to adjust the gas limit to account for variations - * in the SDK estimation, ensuring the transaction will be accepted. - * - * @constant - */ -export const L1_FEE_ESTIMATION_COEF_DENOMINATOR = 10; - -/** - * Gas limit used for displaying the error messages when the - * users do not have enough fee when depositing ERC20 token from L1 to L2. - * - * @constant - */ -export const L1_RECOMMENDED_MIN_ERC20_DEPOSIT_GAS_LIMIT = 400_000; - -/** - * Gas limit used for displaying the error messages when the - * users do not have enough fee when depositing `ETH` token from L1 to L2. - * - * @constant - */ -export const L1_RECOMMENDED_MIN_ETH_DEPOSIT_GAS_LIMIT = 200_000; - -/** - * Default gas per pubdata byte for L2 transactions. - * This value is utilized when inserting a default value for type 2 - * and EIP712 type transactions. - * - * @constant - */ -// It is a realistic value, but it is large enough to fill into any batch regardless of the pubdata price. -export const DEFAULT_GAS_PER_PUBDATA_LIMIT = 50_000; - -/** - * The `L1->L2` transactions are required to have the following gas per pubdata byte. - * - * @constant - */ -export const REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_LIMIT = 800; - /** * ------------------------------------------------------------ * consider adding the next few functions to web3.js: @@ -606,9 +474,7 @@ export function hashBytecode(bytecode: web3Types.Bytes): Uint8Array { } const hashStr = web3Utils.toHex(sha256(Buffer.from(bytecodeAsArray))); - console.log('hashStr:', hashStr); const hash = web3Utils.bytesToUint8Array(hashStr); - console.log('hash:', hash); // Note that the length of the bytecode // should be provided in 32-byte words. diff --git a/test/unit/utils.test.ts b/test/unit/utils.test.ts index 3e20363..686865b 100644 --- a/test/unit/utils.test.ts +++ b/test/unit/utils.test.ts @@ -6,6 +6,7 @@ import { ADDRESS1, // ADDRESS2 } from '../utils'; +import * as constants from '../../src/constants'; describe('utils', () => { describe('#getHashedL2ToL1Msg()', () => { @@ -21,17 +22,17 @@ describe('utils', () => { describe('#isETH()', () => { it('should return true for legacy L1 ETH address', async () => { - const result = utils.isETH(utils.LEGACY_ETH_ADDRESS); + const result = utils.isETH(constants.LEGACY_ETH_ADDRESS); expect(result).toBeTruthy(); }); it('should return true for L1 ETH address', async () => { - const result = utils.isETH(utils.ETH_ADDRESS_IN_CONTRACTS); + const result = utils.isETH(constants.ETH_ADDRESS_IN_CONTRACTS); expect(result).toBeTruthy(); }); it('should return true for L2 ETH address', async () => { - const result = utils.isETH(utils.L2_BASE_TOKEN_ADDRESS); + const result = utils.isETH(constants.L2_BASE_TOKEN_ADDRESS); expect(result).toBeTruthy(); }); }); @@ -214,7 +215,7 @@ describe('utils', () => { utils.hashBytecode(`0x${'00020000000000020009000000000002'.repeat(2)}`); } catch (e) { expect((e as Error).message).toBe( - `Bytecode can not be longer than ${utils.MAX_BYTECODE_LEN_BYTES} bytes`, + `Bytecode can not be longer than ${constants.MAX_BYTECODE_LEN_BYTES} bytes`, ); } });