From 8b2f924babfa0a67a99c5218ccdb0613abc6bbc4 Mon Sep 17 00:00:00 2001 From: jasonandjay <342690199@qq.com> Date: Wed, 11 Dec 2024 01:37:54 +0800 Subject: [PATCH 1/2] doc: add docs of createContractAddress --- packages/web3-eth-contract/src/utils.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/web3-eth-contract/src/utils.ts b/packages/web3-eth-contract/src/utils.ts index 5ef2de6d594..253ae4a3d4b 100644 --- a/packages/web3-eth-contract/src/utils.ts +++ b/packages/web3-eth-contract/src/utils.ts @@ -227,6 +227,29 @@ export const getCreateAccessListParams = ({ return txParams; }; +/** + * Generates the Ethereum address of a contract created via a regular transaction. + * + * This function calculates the contract address based on the sender's address and nonce, + * following Ethereum's address generation rules. + * + * @param from The sender’s Ethereum {@link Address}, from which the contract will be deployed. + * @param nonce The transaction count (or {@link Numbers}) of the sender account at the time of contract creation. + * This is used to ensure uniqueness of the generated address. + * @returns An Ethereum {@link Address} of the contract in checksum address format. + * @throws An {@link InvalidAddressError} if the provided address ('from') is invalid. + * @throws An {@link InvalidNumberError} if the provided nonce value is not in a valid format. + * @example + * ```ts + * const from = "0x1234567890abcdef1234567890abcdef12345678"; + * const nonce = 1; // The nonce value for the transaction + * + * const res = createContractAddress(from, nonce); + * + * console.log(res); + * // > "0x604f1ECbA68f4B4Da57D49C2b945A75bAb331208" + * ``` + */ export const createContractAddress = (from: Address, nonce: Numbers): Address => { if (!isAddress(from)) throw new InvalidAddressError(`Invalid address given ${from}`); From ae1936aa92f288e9623fb80a1c9489bbc9cdfedb Mon Sep 17 00:00:00 2001 From: jasonandjay <342690199@qq.com> Date: Wed, 11 Dec 2024 10:58:00 +0800 Subject: [PATCH 2/2] docs: add detail refer to nonce --- packages/web3-eth-contract/src/utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/web3-eth-contract/src/utils.ts b/packages/web3-eth-contract/src/utils.ts index 253ae4a3d4b..b5f5fc75fbf 100644 --- a/packages/web3-eth-contract/src/utils.ts +++ b/packages/web3-eth-contract/src/utils.ts @@ -235,14 +235,14 @@ export const getCreateAccessListParams = ({ * * @param from The sender’s Ethereum {@link Address}, from which the contract will be deployed. * @param nonce The transaction count (or {@link Numbers}) of the sender account at the time of contract creation. - * This is used to ensure uniqueness of the generated address. + * You can get it here: https://docs.web3js.org/api/web3/class/Web3Eth#getTransactionCount. * @returns An Ethereum {@link Address} of the contract in checksum address format. * @throws An {@link InvalidAddressError} if the provided address ('from') is invalid. * @throws An {@link InvalidNumberError} if the provided nonce value is not in a valid format. * @example * ```ts * const from = "0x1234567890abcdef1234567890abcdef12345678"; - * const nonce = 1; // The nonce value for the transaction + * const nonce = (await web3.eth.getTransactionCount(from)) + 1; // The nonce value for the transaction * * const res = createContractAddress(from, nonce); *