Skip to content

Commit

Permalink
feat(java): add ContractDeployer (#89)
Browse files Browse the repository at this point in the history
* feat(java): add `ContractDeployer`

* chore: fix links
  • Loading branch information
petarTxFusion authored Aug 27, 2024
1 parent 6b856b4 commit 0f6bdbf
Showing 1 changed file with 148 additions and 0 deletions.
148 changes: 148 additions & 0 deletions content/40.java/02.api/02.contract/00.contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

0 comments on commit 0f6bdbf

Please sign in to comment.