From 52d95711a34e18cec151d1eaec157bdb4edbf096 Mon Sep 17 00:00:00 2001 From: petarTxFusion Date: Tue, 27 Aug 2024 16:35:59 +0200 Subject: [PATCH] feat(java): add `ContractDeployer` --- .../02.api/02.contract/00.contracts.md | 148 ++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/content/40.java/02.api/02.contract/00.contracts.md b/content/40.java/02.api/02.contract/00.contracts.md index 7ef0ec1..87a7af1 100644 --- a/content/40.java/02.api/02.contract/00.contracts.md +++ b/content/40.java/02.api/02.contract/00.contracts.md @@ -18,3 +18,151 @@ tags: ["web3", "blockchain", "zksync", "contracts"] - `INonceHolder` - `IZkSync` - `ZkSyncContract` + +## ContractFactory + +The `ContractDeployer` class is a utility class for deploying smart contracts on ZKsync, +using `create` and `create2` methods. +It provides methods to compute contract addresses, +based on `EIP-1014` (create2) and extract contract addresses from transaction receipts. + +### `computeL2Create2Address` + +Compute contract address according [EIP-1014]("https://eips.ethereum.org/EIPS/eip-1014") + +#### Inputs + +| Parameter | Type | Description | +|---------------| ----------- | ----------------------------------------------------------- | +| `sender` | Address | The address of the transaction sender.| +| `bytecode` | byte[] | The compiled contract bytecode.| +| `constructor` | byte[] | The encoded constructor parameters.| +| `salt` | byte[] | A 32-byte value used for the address computation.| + +#### Example + +```java +String result = ContractDeployer.computeL2Create2Address(new Address(credentials.getAddress()), Numeric.hexStringToByteArray(CounterContract.BINARY), new byte[]{}, salt).getValue(); +``` + +### `computeL2CreateAddress` + +Computes the contract address using the create method. + +#### Inputs + +| Parameter | Type | Description | +|---------------|---------| ----------------------------------------------------------- | +| `sender` | Address | Address of a source of a transaction.| +| `nonce` | BigInteger | Deployment nonce.| + +#### Example + +```java +INonceHolder nonceHolder = INonceHolder.load(ZkSyncAddresses.NONCE_HOLDER_ADDRESS, zksync, new ReadonlyTransactionManager(zksync, credentials.getAddress()), new DefaultGasProvider()); +BigInteger deploymentNonce = nonceHolder.getDeploymentNonce(credentials.getAddress()).send(); +String precomputedAddress = ContractDeployer.computeL2CreateAddress(new Address(credentials.getAddress()), deploymentNonce).getValue(); +``` + +### `extractContractAddress` + +Extracts the deployed contract address from a transaction receipt. + +#### Inputs + +| Parameter | Type | Description | +|---------------|---------|--------------------------| +| `receipt` | TransactionReceipt | The transaction receipt. | + +#### Example + +```java +String correctContractAddress = ContractDeployer.extractContractAddress(response).getValue(); +``` + +### `hashBytecode` + +Generates the SHA-256 hash of the contract bytecode. + +#### Inputs + +| Parameter | Type | Description | +|------------|---------|--------------------------| +| `bytecode` | byte[] | The compiled contract bytecode. | + +#### Example + +```java +byte[] result = ContractDeployer.hashBytecode(Numeric.hexStringToByteArray(CounterContract.BINARY)); +``` + +### `encodeCreate2` + +Encodes the create2 deployment function with empty constructor parameters. +#### Inputs + +| Parameter | Type | Description | +|------------|---------|---------------------------------| +| `bytecode` | byte[] | The compiled contract bytecode. | +| `calldata` | byte[] | Encoded constructor parameters. | +| `salt` | byte[] | 32 bytes salt. | + +#### Example + +```java +byte[] salt = SecureRandom.getSeed(32); +ContractDeployer.encodeCreate2(bytecodeBytes, calldataBytes, salt) +``` + +### `encodeCreate` + +Encode `create` deployment function of default factory contract + +#### Inputs + +| Parameter | Type | Description | +|------------|---------|---------------------------------| +| `bytecode` | byte[] | The compiled contract bytecode. | +| `calldata` | byte[] | Encoded constructor parameters. | + +#### Example + +```java +ContractDeployer.encodeCreate(bytecodeBytes, calldataBytes) +``` + +### `encodeCreate2Account` + +Encodes the create2 deployment function for a custom account with empty constructor parameters [EIP-4337]("https://eips.ethereum.org/EIPS/eip-4337"). + +#### Inputs + +| Parameter | Type | Description | +|------------|---------|-------------------------------------| +| `bytecode` | byte[] | The compiled contract bytecode. | +| `calldata` | byte[] | Encoded constructor parameters. | +| `salt` | byte[] | 32 bytes salt. | +| `accountAbstractionVersion` | AccountAbstractionVersion | Version of the account abstraction. | + +#### Example + +```java +ContractDeployer.encodeCreate2Account(bytecodeBytes, calldataBytes, salt, AccountAbstractionVersion.Version1) +``` + +### `encodeCreateAccount` + +Encode `create` deployment custom account function of default factory contract [EIP-4337]("https://eips.ethereum.org/EIPS/eip-4337") + +#### Inputs + +| Parameter | Type | Description | +|------------|---------|-------------------------------------| +| `bytecode` | byte[] | The compiled contract bytecode. | +| `calldata` | byte[] | Encoded constructor parameters. | + +#### Example + +```java +ContractDeployer.encodeCreate2Account(bytecodeBytes, calldataBytes, AccountAbstractionVersion.Version1) +```