This repository was archived by the owner on Mar 5, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
packages/web3-eth-contract/src Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -227,6 +227,29 @@ export const getCreateAccessListParams = ({
227
227
return txParams ;
228
228
} ;
229
229
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
+ */
230
253
export const createContractAddress = ( from : Address , nonce : Numbers ) : Address => {
231
254
if ( ! isAddress ( from ) ) throw new InvalidAddressError ( `Invalid address given ${ from } ` ) ;
232
255
You can’t perform that action at this time.
0 commit comments