Safe Deployment
This guide will teach you 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.
Prerequisites
Install dependencies
First, you need to install some dependencies.
_10pnpm add @safe-global/protocol-kit viem
Steps
Imports
Here are all the necessary imports for this guide.
_10import Safe, {_10 PredictedSafeProps,_10 SafeAccountConfig,_10 SafeDeploymentConfig_10} from '@safe-global/protocol-kit'_10import { 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 EIP-1193 (opens in a new tab) compatible signers are also supported. For detailed information about signers, please refer to the Protocol Kit reference.
_10const SIGNER_PRIVATE_KEY = // ...
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
method. Pass the provider
with its corresponding value depending on the network, the signer
executing the deployment, and the predictedSafe
with the Safe account configuration.
_16const safeAccountConfig: SafeAccountConfig = {_16 owners: ['0x...', '0x...', '0x...'],_16 threshold: 2_16 // More optional properties_16}_16_16const predictedSafe: PredictedSafeProps = {_16 safeAccountConfig_16 // More optional properties_16}_16_16const protocolKit = await Safe.init({_16 provider: sepolia.rpcUrls.default.http[0],_16 signer: SIGNER_PRIVATE_KEY,_16 predictedSafe_16})
Predict the Safe address
You can predict the Safe address using the getAddress
method in the Protocol Kit.
_10const safeAddress = await protocolKit.getAddress()
Create the deployment transaction
Create the deployment transaction to deploy a new Safe smart account by calling the createSafeDeploymentTransaction
method.
_10const deploymentTransaction = await protocolKit.createSafeDeploymentTransaction()
Execute the deployment transaction
Once the deployment transaction object is ready, execute it using the provided signer or your preferred external Ethereum client.
_12const client = await protocolKit.getSafeProvider().getExternalSigner()_12_12const transactionHash = await client.sendTransaction({_12 to: deploymentTransaction.to,_12 value: BigInt(deploymentTransaction.value),_12 data: deploymentTransaction.data as `0x${string}`,_12 chain: sepolia_12})_12_12const transactionReceipt = await client.waitForTransactionReceipt({_12 hash: transactionHash_12})
Reinitialize the Protocol Kit
Once the deployment transaction is executed, connect the new Safe address to the Protocol Kit instance by calling the connect
method.
_10const newProtocolKit = await protocolKit.connect({_10 safeAddress_10})_10_10const isSafeDeployed = await newProtocolKit.isSafeDeployed() // True_10const safeAddress = await newProtocolKit.getAddress()_10const safeOwners = await newProtocolKit.getOwners()_10const safeThreshold = await newProtocolKit.getThreshold()
Recap and further reading
After following this guide, you are able to deploy new Safe smart accounts with the Protocol Kit.