# Multichain Safe Deployment

This guide will teach you how to replicate a Safe address across different chains using the Protocol Kit. This process includes initializing the Protocol Kit, configuring the Safes to deploy, predicting its address on different chains, and executing the deployment transactions.

For more detailed information, see the [Protocol Kit Reference](../../../reference-sdk-protocol-kit/overview.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 viem
```


## Steps

<Steps>

  ### Imports

  Here are all the necessary imports for this guide.


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


  ### Create a signer

  You need a signer to instantiate the Protocol Kit. This example uses a private key to obtain a signer, but other [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) compatible signers are also supported. For detailed information about signers, please refer to the [Protocol Kit reference](../../../reference-sdk-protocol-kit/overview.mdx).


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


  ### Configure the Safe deployment

  Define the [`predictedSafe`](../../../reference-sdk-protocol-kit/initialization/init.mdx#predictedsafe-optional) object with the configuration for all the Safe accounts you will deploy. Check the reference to learn about all the different configuration options.


  ```typescript
  const safeAccountConfig: SafeAccountConfig = {
    owners: ['0x...', '0x...', '0x...'],
    threshold: 2
    // ...
  }

  const predictedSafe: PredictedSafeProps = {
    safeAccountConfig
    // ...
  }
  ```


  ### Initialize the Protocol Kit

  Initialize an instance of the Protocol Kit for each network where you want to deploy a new Safe smart account by calling the [`init`](../../../reference-sdk-protocol-kit/initialization/init.mdx) method. Pass the `provider` with its corresponding value depending on the network, the `signer` executing the deployment, and the [`predictedSafe`](../../../reference-sdk-protocol-kit/initialization/init.mdx#predictedsafe-optional) with the Safe account configuration.


  ```typescript
  const protocolKitSepolia = await Safe.init({
    provider: sepolia.rpcUrls.default.http[0],
    signer: SIGNER_PRIVATE_KEY,
    predictedSafe,
    onchainAnalytics // Optional
    // ...
  })
  
  const protocolKitChiado = await Safe.init({
    provider: gnosisChiado.rpcUrls.default.http[0],
    signer: PRIVATE_KEY,
    predictedSafe,
    onchainAnalytics // Optional
    // ...
  })
  ```


  Optionally, you can [track your Safe deployments and transactions on-chain](../../onchain-tracking.mdx) by using the `onchainAnalytics` property.

  ### Predict the Safe addresses

  You can predict the Safe addresses by calling the [`getAddress`](../../../reference-sdk-protocol-kit/safe-info/getaddress.mdx) method from each Protocol Kit instance and ensure that the result addresses are the same.


  ```typescript
  const sepoliaPredictedSafeAddress = await protocolKitSepolia.getAddress()
  const chiadoPredictedSafeAddress = await protocolKitChiado.getAddress()
  ```


  ### Deployment on Sepolia

  Create the deployment transaction to deploy a new Safe account in Sepolia by calling the [`createSafeDeploymentTransaction`](../../../reference-sdk-protocol-kit/deployment/createsafedeploymenttransaction.mdx) method.
  

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

  Call the `sendTransaction` method from your Sepolia client instance and wait for the transaction to be executed.


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

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

  await waitForTransactionReceipt(
    sepoliaClient!,
    { hash: transactionHashSepolia }
  )
  ```


  Once the deployment transaction is executed, connect the new Safe address to the Protocol Kit instance by calling the [`connect`](../../../reference-sdk-protocol-kit/initialization/connect.mdx) method.


  ```typescript
  const newProtocolKitSepolia = await protocolKitSepolia.connect({
    safeAddress: sepoliaPredictedSafeAddress
  })

  const isSepoliaSafeDeployed = await newProtocolKitSepolia.isSafeDeployed() // True
  const sepoliaDeployedSafeAddress = await newProtocolKitSepolia.getAddress()
  ```


  If everything went well, `isSepoliaSafeDeployed` should be `true`, and the `sepoliaDeployedSafeAddress` should equal the `sepoliaPredictedSafeAddress`.

  ### Deployment on Chiado

  Repeat the same steps to deploy a Safe with the same configuration on Chiado testnet.


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

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

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

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

  const newProtocolKitChiado = await protocolKitChiado.connect({
    safeAddress: chiadoPredictedSafeAddress
  })

  const isChiadoSafeDeployed = await newProtocolKitChiado.isSafeDeployed() // True
  const chiadoDeployedSafeAddress = await newProtocolKitChiado.getAddress()
  ```


  If everything went well, `isChiadoSafeDeployed` should be `true`, and the `chiadoDeployedSafeAddress` should equal the `chiadoPredictedSafeAddress`.

  In addition, `chiadoDeployedSafeAddress` and `sepoliaDeployedSafeAddress` should have the same value.

</Steps>

## Recap and further reading

After following this guide, you are able to deploy multiple Safe accounts with the same address on different chains using the Protocol Kit.
