Skip to content

Commit

Permalink
doc: add docs of createContractAddress (#7421)
Browse files Browse the repository at this point in the history
* doc: add docs of createContractAddress

* docs: add detail refer to nonce
  • Loading branch information
jasonandjay authored Dec 11, 2024
1 parent fa5ce5b commit ae99434
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/web3-eth-contract/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* 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 = (await web3.eth.getTransactionCount(from)) + 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}`);

Expand Down

1 comment on commit ae99434

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: ae99434 Previous: fa5ce5b Ratio
processingTx 21624 ops/sec (±6.47%) 21928 ops/sec (±6.24%) 1.01
processingContractDeploy 34957 ops/sec (±9.82%) 37222 ops/sec (±9.30%) 1.06
processingContractMethodSend 14678 ops/sec (±8.29%) 14625 ops/sec (±8.78%) 1.00
processingContractMethodCall 25364 ops/sec (±7.90%) 25971 ops/sec (±7.32%) 1.02
abiEncode 38399 ops/sec (±8.36%) 39972 ops/sec (±8.07%) 1.04
abiDecode 28131 ops/sec (±6.44%) 28300 ops/sec (±7.27%) 1.01
sign 1471 ops/sec (±3.50%) 1459 ops/sec (±4.39%) 0.99
verify 357 ops/sec (±0.66%) 361 ops/sec (±0.60%) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.