Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit ae99434

Browse files
authored
doc: add docs of createContractAddress (#7421)
* doc: add docs of createContractAddress * docs: add detail refer to nonce
1 parent fa5ce5b commit ae99434

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

packages/web3-eth-contract/src/utils.ts

+23
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,29 @@ export const getCreateAccessListParams = ({
227227
return txParams;
228228
};
229229

230+
/**
231+
* Generates the Ethereum address of a contract created via a regular transaction.
232+
*
233+
* This function calculates the contract address based on the sender's address and nonce,
234+
* following Ethereum's address generation rules.
235+
*
236+
* @param from The sender’s Ethereum {@link Address}, from which the contract will be deployed.
237+
* @param nonce The transaction count (or {@link Numbers}) of the sender account at the time of contract creation.
238+
* You can get it here: https://docs.web3js.org/api/web3/class/Web3Eth#getTransactionCount.
239+
* @returns An Ethereum {@link Address} of the contract in checksum address format.
240+
* @throws An {@link InvalidAddressError} if the provided address ('from') is invalid.
241+
* @throws An {@link InvalidNumberError} if the provided nonce value is not in a valid format.
242+
* @example
243+
* ```ts
244+
* const from = "0x1234567890abcdef1234567890abcdef12345678";
245+
* const nonce = (await web3.eth.getTransactionCount(from)) + 1; // The nonce value for the transaction
246+
*
247+
* const res = createContractAddress(from, nonce);
248+
*
249+
* console.log(res);
250+
* // > "0x604f1ECbA68f4B4Da57D49C2b945A75bAb331208"
251+
* ```
252+
*/
230253
export const createContractAddress = (from: Address, nonce: Numbers): Address => {
231254
if (!isAddress(from)) throw new InvalidAddressError(`Invalid address given ${from}`);
232255

0 commit comments

Comments
 (0)