Skip to content

Commit

Permalink
fix: new format / content structure
Browse files Browse the repository at this point in the history
  • Loading branch information
xBalbinus committed Sep 18, 2024
1 parent 54ac19f commit 12a3978
Showing 1 changed file with 66 additions and 56 deletions.
122 changes: 66 additions & 56 deletions smart-contracts/advanced/deterministic-deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,95 @@

Deterministic deployment refers to the ability to deploy smart contracts to the same address across different blockchain networks. This consistency can be particularly useful when deploying the same contract on multiple chains to streamline interactions or ensure interoperability. There are two primary methods to achieve deterministic deployment:

- Using a **factory contract that has been deployed keylessly** to deploy your contracts.
- **Building your own factory contract** to deploy your contracts deterministically.
- The factory usually uses the `CREATE2` opcode to deploy your contracts, precomputing the deployment address and ensuring consistency across all chains.
- **Deploying your contracts keylessly** (without using any factory) onto all blockchains.
- You must keep the code and constructor arguments unchanged to get the same address.
- **Using supported pre-built tools** to deploy your contracts deterministically.

## Creating a deterministic deployment
## **Deploying a factory contract to Filecoin**

Contract address can be precomputed, before the contract is deployed, using a create opcode. This is one of the options for deterministic deployment.
Other people may have already deployed the factory contract onto Filecoin, in which case you won't need to redeploy it. You can just use the factory to deploy your contracts. So first check the expected address on a blockchain explorer to see if a factory contract already exists there.

### **`CREATE`**
If there isn't one yet then you'll need to deploy the factory contract. The factory contract will then have the same address as on other blockchains (as long as the transaction bytecode stays the same).

If deploying using a `CREATE` factory contract, contract addresses are calculated using the:
### Creating a deterministic deployment factory contract

- Address of the factory contract (that calls `CREATE` opcode) itself;
- Nonce of the factory contract ([EIP-161](https://eips.ethereum.org/EIPS/eip-161) specifies that contract nonce starts at 1, not 0 like EOAs).
Contract address get precomputed by the factory contract, before the contract is deployed, typically using the create2 opcode.

### **`CREATE2`**
#### **`CREATE2`**

Similar to `CREATE`, `CREATE2` is used to deploy contracts to the same address across different blockchain networks in factory contract implementations. Contract addresses are precomputed using the:
`CREATE2` is used to deploy contracts to the same address across different blockchain networks in factory contract implementations. Contract addresses are precomputed using the:

- Address of the factory contract (that calls `CREATE2` opcode) itself;
- Bytecode of the contract;
- Salt (a chosen value).

See this [code snippet](https://solidity-by-example.org/app/create2/) for an example of how to call `CREATE2` from a factory contract.

## **Deploying a factory contract to Filecoin**

Other people may have already deployed the factory contract onto Filecoin, in which case you won't need to redeploy it. You can just use the factory to deploy your contracts. So first check the expected address on a blockchain explorer to see if a factory contract already exists there.

If there isn't one yet then you'll need to deploy the factory contract via a **reusable signed raw deployment transaction**. The factory contract will then have the same address as on other blockchains (as long as the transaction bytecode stays the same). See the steps below to deploy the factory.
See this [code snippet](https://solidity-by-example.org/app/create2/) for an example of how to call `CREATE2` from a factory contract. Alternatively, you can base your own on [this popular proxy](https://github.com/Arachnid/deterministic-deployment-proxy).

1. **Prepare the deployment transaction:**
- Write the smart contract code for the factory contract.
- Compile the contract to get the bytecode.
- Create a deployment transaction with the bytecode.
## **Supported Tools on Filecoin**

2. **Sign the deployment transaction:**
- Use a private key to sign the deployment transaction.
- Ensure the private key is securely stored and not exposed.
There exists popular tools that have already deployed factory contracts on Filecoin. You can use these tools to deploy your contracts deterministically instead of writing your own factory contract.

3. **Broadcast the signed transaction:**
- Send the signed transaction to the desired blockchain network.
- Wait for the transaction to be mined and confirmed.
### **Safe Singleton Factory**
- **Address:** `0x914d7Fec6aaC8cd542e72Bca78B30650d45643d7`
- **How-to:**
- Import the Safe Singleton Factory information via. the [library](https://github.com/safe-global/safe-singleton-factory).
- Use the `getSingletonFactoryInfo` function in hardhat config to get the factory address, deployer address, and signed transaction.

4. **Verify the deployment:**
- Check the [blockchain explorer](https://docs.filecoin.io/networks/mainnet/explorers) to verify that the factory contract has been deployed to the expected address.
- Ensure the contract code matches the expected bytecode.
```ts
import { getSingletonFactoryInfo } from '@safe-global/safe-singleton-factory'

By following these steps, you can deploy the factory contract to multiple blockchains, ensuring it has the same address on each one. This allows for consistent and deterministic deployment of other contracts using the factory contract.
## **Using Popular Tools on Filecoin**
...

### **1. Deterministic Deployment Proxy by Arachnid**
- **Address:** `0x4e59b44847b379578588920ca78fbf26c0b4956c`
- **How-to:**
- Signing the Proxy deployment transaction is hidden, only recover the public key from the signed transaction.
- The signing key and address are associated with the deployment transaction. If anything related to the deployment transaction changes, the signing key and address will be different, resulting in a different proxy address.
- [Repository Link](https://github.com/Arachnid/deterministic-deployment-proxy/tree/master)
function deterministicDeployment(network: string): DeterministicDeploymentInfo {
const info = getSingletonFactoryInfo(parseInt(network))

### **2. Safe Singleton Factory**
- **Address:** `0x914d7Fec6aaC8cd542e72Bca78B30650d45643d7`
- **How-to:**
- Safe Multisig Wallet
- Deployed by Masa Finance, Starlings lab, and align.network on many blockchains.
- [Masa Finance](https://www.masa.ai/)
- [align.network](http://align.network)
- [Repository Link](https://github.com/safe-global/safe-singleton-factory)
...
return {
factory: info.address,
deployer: info.signerAddress,
funding: String(gasLimit * gasPrice),
signedTx: info.transaction,
}
}
```

### **3. CreateX / xdeployer by pcaversaccio**
### **CreateX / xdeployer by pcaversaccio**
- **Address:** `0xba5Ed099633D3B313e4D5F7bdc1305d3c28ba5Ed`
- **How-to:**
- Deploy it yourself using one of the pre-signed transactions. Details can be found in the repository.
- xdeployer is a hardhat plugin that allows you to deploy your smart contracts across multiple EVM chains with the same deterministic address.
- It is deployed on Filecoin mainnet and testnet.
- Request a deployment by opening an [issue](https://github.com/pcaversaccio/createx/issues/new?assignees=pcaversaccio&labels=new+deployment+%E2%9E%95&projects=&template=deployment_request.yml&title=%5BNew-Deployment-Request%5D%3A+).
- [CreateX Repository](https://github.com/pcaversaccio/createx), [xdeployer Repository](https://github.com/pcaversaccio/xdeployer)
- [Deployment Result from Dune](https://dune.com/patronumlabs/createx)

### **4. OpenZeppelin Defender**
- CreateX is meant to be a wrapper around the create opcodes that you can integrate into your own factory contract. Details can be found in the [CreateX Repository](https://github.com/pcaversaccio/createx).
- [xdeployer](https://github.com/pcaversaccio/xdeployer) is a hardhat plugin that allows you to deploy your smart contracts across multiple EVM chains with the same deterministic address. You can import and configure in your hardhat configuration file like so:

```ts
import "xdeployer";

const config: HardhatUserConfig = {
networks: {
mainnet: { ... }
},
xdeploy: {
contract: "YOUR_CONTRACT_NAME_TO_BE_DEPLOYED",
constructorArgsPath: "PATH_TO_CONSTRUCTOR_ARGS", // optional; default value is `undefined`
salt: "YOUR_SALT_MESSAGE",
signer: "SIGNER_PRIVATE_KEY",
networks: ["LIST_OF_NETWORKS"],
rpcUrls: ["LIST_OF_RPCURLS"],
gasLimit: 1_500_000, // optional; default value is `1.5e6`
},
};
```

or in the case of solidity files:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
import { CreateX } from "xdeployer/src/contracts/CreateX.sol";
contract Create2DeployerLocal is CreateX {}
```

### **OpenZeppelin Defender**
- **Address:** N/A
- **How-to:**
- Follow the tutorial to use OpenZeppelin Defender for deterministic deployments.
Expand Down

0 comments on commit 12a3978

Please sign in to comment.