Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(java): add ContractDeployer #89

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
```
Loading