Safe
The Safe{Core} SDK is designed to simplify the integration of Safe smart accounts into your projects, providing a modular and customizable approach to account abstraction. It offers several integration options to cater to different development needs:
Starter Kit: A simplified entry point for deploying new accounts and managing Safe transaction flows, including user operations, multi-signature transactions, and message handling.
Protocol Kit: Enables interaction with Safe smart accounts, allowing for the creation of new accounts, configuration updates, and transaction execution.
API Kit: Facilitates communication with the Safe Transaction Service API, enabling transaction sharing among signers and retrieval of account information such as configurations and transaction histories.
Relay Kit: Supports transaction relaying, allowing users to pay transaction fees from their Safe account using native tokens, ERC-20 tokens, or through sponsored transactions.
Creating a Safe
The guide will walkthrough the process of deploying a 2 of 3 Safe Smart Account to Lens Testnet using the Protocol Kit SDK.
Install the required dependencies with your package manager of choice:
npm install @safe-global/protocol-kit @lens-network/sdk@canary
import Safe, { PredictedSafeProps, SafeAccountConfig,} from "@safe-global/protocol-kit";import {chains} from "@lens-network/sdk";
const safeAccountConfig = { owners: [ "0x81EdcF8e0a72c3300087891Bb3E992FAf285b2FC", "0x482c27532517af746358D8E35AfCb3b2ca90A72B", "0x28f875a08F320Cb5Fb6317c6C948fCA8663aC7e9", ], // replace with owner addresses threshold: 2, // replace with multi-signature threshold};
const predictedSafe = { safeAccountConfig,};
const protocolKit = await Safe.init({ provider: chains.testnet.rpcUrls.default, signer: process.env.PRIVATE_KEY, // replace with deployer private key predictedSafe,});
You can predict the Safe addres before deployment with the following method:
const safeAddress = await protocolKit.getAddress();
Create the deployment transaction to deploy a new Safe smart account:
const deploymentTransaction = await protocolKit.createSafeDeploymentTransaction();
const client = await protocolKit.getSafeProvider().getExternalSigner();
const transactionHash = await client.sendTransaction({ to: deploymentTransaction.to, value: BigInt(deploymentTransaction.value), data: deploymentTransaction.data, chain: chains.testnet,});
const transactionReceipt = await client.waitForTransactionReceipt({ hash: transactionHash,});
Once the deployment transaction is executed, connect the new Safe address to the Protocol Kit instance by calling the connect method. Once connected, operations can be performed by following the Protocol Kit SDK reference.
const newProtocolKit = await protocolKit.connect({ safeAddress,});
const isSafeDeployed = await newProtocolKit.isSafeDeployed();const safeAddress = await newProtocolKit.getAddress();const safeOwners = await newProtocolKit.getOwners();const safeThreshold = await newProtocolKit.getThreshold();