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

Safe deployment guide v2 #617

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions pages/sdk/protocol-kit/guides/_meta.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"safe-deployment": "Safe deployment",
"execute-transactions": "Execute transactions",
"signatures": "Signatures",
"migrate-to-v1": "Migrate to v1",
Expand Down
4 changes: 4 additions & 0 deletions pages/sdk/protocol-kit/guides/safe-deployment/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"deploy-safe": "Deploy a Safe",
"address-replication": "Address replication"
}
173 changes: 173 additions & 0 deletions pages/sdk/protocol-kit/guides/safe-deployment/address-replication.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { Steps } from "nextra/components";

# Safe Address Replication Across Chains

In this guide, you will learn how to replicate a Safe address across different chains using the Protocol Kit. This process includes initializing the Protocol Kit, configuring the Safe, predicting the Safe address on different chains, and executing the deployment.

We will demonstrate how to deploy a Safe account on both the Sepolia and Chiado chains with the same address using the Protocol Kit.

For more detailed information, see the [Protocol Kit Reference](../../../protocol-kit/reference/safe.mdx).

## Prerequisites

- [Node.js and npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)

## Install dependencies

First, you need to install the Protocol Kit.

```bash
pnpm add @safe-global/protocol-kit
```

## Steps

<Steps>

### Imports

Begin by importing the `Safe` class from the Protocol Kit and chain configuration from viem.

{/* <!-- vale off --> */}

```typescript
import Safe, { SafeDeploymentConfig, SafeAccountConfig } from '@safe-global/protocol-kit'
import { waitForTransactionReceipt } from 'viem/actions'
import { sepolia, gnosisChiado } from 'viem/chains'
```

{/* <!-- vale on --> */}

### Create a signer

Firstly, you need to get a signer to execute the deployment transaction in each chain. this example uses a private key, but any way to get an `EIP-1193` compatible signer can be used.

{/* <!-- vale off --> */}

```typescript
const SIGNER_PRIVATE_KEY = // ...
```

{/* <!-- vale on --> */}

### Define the Safe Configuration

{/* <!-- vale off --> */}

Define the configuration for your Safe. This includes the `owners`, `threshold`, deployment type, salt nonce for address predictability, and the version of the Safe.

```typescript
// Safe account config
const safeAccountConfig: SafeAccountConfig = {
owners: ['0xOwnerAddress'],
threshold: 1
}

// Safe deployment config
const safeDeploymentConfig: SafeDeploymentConfig = {
deploymentType: 'canonical',
saltNonce: '12345',
safeVersion: '1.3.0'
}
```

{/* <!-- vale on --> */}

### Initialize the `ProtocolKit` for Sepolia and Chiado chain

{/* <!-- vale off --> */}


Initialize the Protocol Kit for deploying on Sepolia and Chiado. Set up the provider and signer along with the predicted Safe configuration.

You can use the `getAddress` method to ensure that the Safe account has the same address across chains.

```typescript
let protocolKitSepolia = await Safe.init({
provider: sepolia.rpcUrls.default.http[0],
signer: SIGNER_PRIVATE_KEY,
predictedSafe: {
safeAccountConfig,
safeDeploymentConfig
}
})

let protocolKitChiado = await Safe.init({
provider: gnosisChiado.rpcUrls.default.http[0],
signer: PRIVATE_KEY,
predictedSafe: {
safeAccountConfig,
safeDeploymentConfig
}
})

const safeAddressInSepolia = await protocolKitSepolia.getAddress()
const safeAddressInChiado = await protocolKitChiado.getAddress()

console.log('safeAddressInSepolia: ', safeAddressInSepolia)
console.log('safeAddressInChiado: ', safeAddressInChiado)
```

{/* <!-- vale on --> */}

### Deploy the Safe on Sepolia

Create and send the deployment transaction on Sepolia.

{/* <!-- vale off --> */}

```typescript
const deploymentTransactionInSepolia = await protocolKitSepolia.createSafeDeploymentTransaction()

const sepoliaClient = await protocolKitSepolia.getSafeProvider().getExternalSigner()

const txHashSepolia = await sepoliaClient!.sendTransaction({
to: deploymentTransactionInSepolia.to,
value: BigInt(deploymentTransactionInSepolia.value),
data: deploymentTransactionInSepolia.data as `0x${string}`,
chain: sepolia
})

await waitForTransactionReceipt(sepoliaClient!, { hash: txHashSepolia })

protocolKitSepolia = await protocolKitSepolia.connect({ safeAddress: safeAddressInSepolia })

// Confirm the Safe is deployed in Sepolia and fetch Safe info
console.log('Is Safe deployed:', await protocolKitSepolia.isSafeDeployed())
console.log('Safe Address:', await protocolKitSepolia.getAddress())
```

{/* <!-- vale on --> */}

### Repeat Deployment for Chiado

Repeat the initialization and deployment for the Chiado chain using the same predictedSafe configuration to ensure the Safe address matches the one deployed on Sepolia.

Check failure on line 144 in pages/sdk/protocol-kit/guides/safe-deployment/address-replication.mdx

View workflow job for this annotation

GitHub Actions / vale-docs

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'predictedSafe'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'predictedSafe'?", "location": {"path": "pages/sdk/protocol-kit/guides/safe-deployment/address-replication.mdx", "range": {"start": {"line": 144, "column": 78}}}, "severity": "ERROR"}

{/* <!-- vale off --> */}

```typescript
const deploymentTransactionInChiado = await protocolKitChiado.createSafeDeploymentTransaction()

const chiadoClient = await protocolKitChiado.getSafeProvider().getExternalSigner()

const txHashChiado = await chiadoClient!.sendTransaction({
to: deploymentTransactionInChiado.to,
value: BigInt(deploymentTransactionInChiado.value),
data: deploymentTransactionInChiado.data as `0x${string}`,
chain: gnosisChiado
})

await waitForTransactionReceipt(chiadoClient!, { hash: txHashChiado })

protocolKitChiado = await protocolKitChiado.connect({ safeAddress: safeAddressInChiado })

// Confirm the Safe is deployed in Chiado and fetch properties
console.log('Is Safe deployed:', await protocolKitChiado.isSafeDeployed())
console.log('Safe Address:', await protocolKitChiado.getAddress())
```

</Steps>

## Recap and further reading

By following this guide, you've learned how to deploy Safes with the same address on multiple chains using the Protocol Kit.
139 changes: 139 additions & 0 deletions pages/sdk/protocol-kit/guides/safe-deployment/deploy-safe.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { Steps } from 'nextra/components'

# Deploy a Safe

In this guide, you will learn how to deploy a new Safe using the Protocol Kit. This process includes initializing the Protocol Kit, setting up your Safe configuration, and executing the deployment.

For more detailed information, see the [Protocol Kit Reference](../../../protocol-kit/reference/safe.mdx).

## Prerequisites

- [Node.js and npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)

## Install dependencies

First, you need to install the Protocol Kit.

```bash
pnpm add @safe-global/protocol-kit
```

## Steps

<Steps>

### Imports

Begin by importing the `Safe` class from the Protocol Kit.

{/* <!-- vale off --> */}

```typescript
import Safe from '@safe-global/protocol-kit'
```

{/* <!-- vale on --> */}

### Create a signer

Firstly, you need to get a signer, this example uses a private key, but any way to get an `EIP-1193` compatible signer can be used.

{/* <!-- vale off --> */}

```typescript
const SIGNER_PRIVATE_KEY = // ...
const RPC_URL = 'https://rpc.ankr.com/eth_sepolia'
```

{/* <!-- vale on --> */}

### Initialize the `ProtocolKit`

{/* <!-- vale off --> */}

Initialize the Protocol Kit with the `provider`, `signer`, and the `predictedSafe` configuration.

```typescript
// Define your Safe configuration
const predictedSafe = {
safeAccountConfig: {
owners: ['0xOwner1', '0xOwner2', '0xOwner3'], // In this example, 3 owners
threshold: 2 // Requires at least 2 signatures to approve a transaction
}
}

// Initialize the Protocol Kit with the predictedSafe
const protocolKit = await Safe.init({
provider: RPC_URL,
signer: SIGNER_PRIVATE_KEY,
predictedSafe
})
```

{/* <!-- vale on --> */}

### Predict your Safe address

you can predict your Safe address using the `getAddress` method in the Protocol Kit.

{/* <!-- vale off --> */}

```typescript
const safeAddress = await protocolKit.getAddress()
```

{/* <!-- vale on --> */}

### Create the deployement transaction

Check failure on line 87 in pages/sdk/protocol-kit/guides/safe-deployment/deploy-safe.mdx

View workflow job for this annotation

GitHub Actions / vale-docs

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'deployement'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'deployement'?", "location": {"path": "pages/sdk/protocol-kit/guides/safe-deployment/deploy-safe.mdx", "range": {"start": {"line": 87, "column": 18}}}, "severity": "ERROR"}

Now, generate the transaction to deploy the Safe and execute it using the Protocol Kit.

{/* <!-- vale off --> */}

```typescript
const deploymentTransaction = await protocolKit.createSafeDeploymentTransaction()
```

{/* <!-- vale on --> */}

### Execute the deployement transaction

Check failure on line 99 in pages/sdk/protocol-kit/guides/safe-deployment/deploy-safe.mdx

View workflow job for this annotation

GitHub Actions / vale-docs

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'deployement'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'deployement'?", "location": {"path": "pages/sdk/protocol-kit/guides/safe-deployment/deploy-safe.mdx", "range": {"start": {"line": 99, "column": 19}}}, "severity": "ERROR"}

You can execute the deployement transaction using the integrated signer or your preferred external Ethereum client.

Check failure on line 101 in pages/sdk/protocol-kit/guides/safe-deployment/deploy-safe.mdx

View workflow job for this annotation

GitHub Actions / vale-docs

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'deployement'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'deployement'?", "location": {"path": "pages/sdk/protocol-kit/guides/safe-deployment/deploy-safe.mdx", "range": {"start": {"line": 101, "column": 23}}}, "severity": "ERROR"}

{/* <!-- vale off --> */}

```typescript
const client = await protocolKit.getSafeProvider().getExternalSigner()

const txHash = await client.sendTransaction({
to: deploymentTransaction.to,
value: BigInt(deploymentTransaction.value),
data: deploymentTransaction.data as `0x${string}`,
chain: sepolia
})

const txReceipt = await client.waitForTransactionReceipt({ hash: txHash })
```

### Reconnect the `protocolKit` to the newly deployed Safe

Once the deployment transaction has been successfully executed, it's necessary to reconnect your Protocol Kit instance to the address of the newly created Safe using the `connect` Method

```typescript
// Reconnect to the newly deployed Safe using the protocol-kit
protocolKit = await protocolKit.connect({ safeAddress })

// Confirm the Safe is deployed and fetch properties
console.log("Is Safe deployed:", await protocolKit.isSafeDeployed())
console.log("Safe Address:", await protocolKit.getAddress())
console.log("Safe Owners:", await protocolKit.getOwners())
console.log("Safe Threshold:", await protocolKit.getThreshold())
```


</Steps>


## Recap and further reading

After following this guide, you are able to deploy new Safe accounts with the Protocol Kit.
Loading