bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 12쪽 중 4쪽
_
104
);
_
104
_
104
const swapRoute = new Route([pool], WETH, USDC);
_
104
_
104
const uncheckedTrade = Trade.createUncheckedTrade({
_
104
tradeType: TradeType.EXACT_INPUT,
_
104
route: swapRoute,
_
104
inputAmount: CurrencyAmount.fromRawAmount(WETH,
_
104
INPUT_AMOUNT
_
104
),
_
104
outputAmount: CurrencyAmount.fromRawAmount(USDC, OUTOUT_AMOUNT),
_
104
});
_
104
_
104
const methodParameters = SwapRouter.swapCallParameters(
_
104
[uncheckedTrade],
_
104
options
_
104
);
_
104
_
104
const safeSwapTx: MetaTransactionData = {
_
104
to: SWAP_ROUTER_ADDRESS,
_
104
value: methodParameters.value,
_
104
data: methodParameters.calldata,
_
104
operation: OperationType.Call,
_
104
};
_
104
_
104
const safeTx = await preExistingSafe.createTransaction({
_
104
transactions: [safeApproveTx, safeSwapTx],
_
104
onlyCalls: true,
_
104
});
_
104
_
104
// You might need to collect more signatures here, depending on the threshold
_
104
_
104
const txResponse = await preExistingSafe.executeTransaction(safeTx);
_
104
await publicClient.waitForTransactionReceipt({
_
104
hash: txResponse.hash as `0x${string}`,
_
104
});
_
104
_
104
console.log(`Deposit and approve transaction: [${txResponse.hash}]`);
Now your AI agent executed a swap on Uniswap.
Next steps
You can find more information about Swaps on Uniswap in their
docs about swaps
(opens in a new tab)
.
If you have a technical question about Safe Smart Accounts, feel free to reach out on
Stack Exchange
(opens in a new tab)
with the safe-core tag.
AI agent swaps on CoW Swap
Was this page helpful?
Report issue
## /sdk/starter-kit
SDK
Starter Kit
Starter Kit
The Starter Kit is the starting point for interacting with the Safe smart account using a TypeScript interface.
The Starter Kit is built on top of several kits from the Safe{Core} SDK, leveraging and abstracting the complex logic. At the same time, it's modular and customizable, offering the most simplified way to deploy new accounts and handle the Safe transaction flow in all its different forms:
User operations
Multi-signature transactions
Off-chain and on-chain messages
@safe-global/sdk-starter-kit
The following guides show how to use the Starter Kit and integrate it into your project:
Send transactions
Send user operations
Resources
Starter Kit on GitHub
(opens in a new tab)
Overview
Send Transactions
Was this page helpful?
Report issue
## /sdk/starter-kit/guides/send-transactions
SDK
Starter Kit
Guides
Send Transactions
Send Transactions
In this guide, you will learn how to create Safe transactions, sign them, collect the signatures from the different owners, and execute them.
For more detailed information, see the
Starter Kit Reference
.
Prerequisites
Node.js and npm
(opens in a new tab)
Install dependencies
First, you need to install some dependencies.
_
10
pnpm add @safe-global/sdk-starter-kit
Steps
Imports
Here are all the necessary imports for this guide.
_
10
import { createSafeClient } from '@safe-global/sdk-starter-kit'
Create a signer
Firstly, you need to get a signer, which will be the owner of a Safe account after it's deployed.
This example uses a private key, but any way to get an EIP-1193 compatible signer can be used.
_
10
const SIGNER_ADDRESS = // ...
_
10
const SIGNER_PRIVATE_KEY = // ...
_
10
const RPC_URL = 'https://rpc.ankr.com/eth_sepolia'
Initialize the
SafeClient
New Safe account
Existing Safe account
When deploying a new Safe account, you need to pass the configuration of the Safe in the
safeOptions
property. The Safe account is configured with your signer as the only owner in this case.
_
10
const safeClient = await createSafeClient({
_
10
provider: RPC_URL,
_
10
signer: SIGNER_PRIVATE_KEY,
_
10
safeOptions: {
_
10
owners: [SIGNER_ADDRESS],
_
10
threshold: 1
_
10
}
_
10
})
Create a Safe transaction
Create an array of Safe transactions to execute.
_
10
const transactions = [{
_
10
to: '0x...',
_
10
data: '0x',
_
10
value: '0'
_
10
}]
Send the Safe transaction
If you configured your Safe with
threshold
equal to
1
, calling the
send
method will execute the Safe transaction. However, if the
threshold
is greater than
1
the other owners of the Safe will need to confirm the transaction until the required number of signatures are collected.
_
10
const txResult = await safeClient.send({ transactions })
_
10
_
10
const safeTxHash = txResult.transactions?.safeTxHash
Confirm the Safe transaction
If the Safe transaction needs to be confirmed by other Safe owners, call the
confirm
method from a new
SafeClient
instance initialized with each of the signers that need to confirm it.
_
10
const newSafeClient = await createSafeClient({
_
10
provider: RPC_URL,
_
10
signer,
_
10
safeAddress: '0x...'
_
10
})
Finally, retrieve all the pending Safe transactions from the Safe Transaction Service and confirm the one you just created with each missing owner.
_
10
const pendingTransactions = await newSafeClient.getPendingTransactions()
_
10
_
10
for (const transaction of pendingTransactions.results) {
_
10
if (transaction.safeTxHash !== safeTxHash) {
_
10
return
_
10
}
_
10
_
10
const txResult = await safeClient.confirm({ safeTxHash })
_
10
}
Recap and further reading
After following this guide, you are able to deploy new Safe accounts and create, sign, and execute Safe transactions with the Starter Kit.
Starter Kit
Send User Operations
Was this page helpful?
Report issue
## /sdk/starter-kit/guides/send-user-operations
SDK
Starter Kit
Guides
Send User Operations
Send User Operations
In this guide, you will learn how to create Safe
user operations
, sign them, collect the signatures from the different owners, and execute them.
For more detailed information, see the
Starter Kit Reference
.
Pimlico
(opens in a new tab)
is used in this guide as the service provider, but any other provider compatible with the ERC-4337 can be used.
Prerequisites
Node.js and npm
(opens in a new tab)
A
Pimlico account
(opens in a new tab)
and an API key.
Install dependencies
First, you need to install some dependencies.
_
10
pnpm add @safe-global/sdk-starter-kit
Steps
Imports
Here are all the necessary imports for this guide.
_
10
import {
_
10
createSafeClient,
_
10
safeOperations,
_
10
BundlerOptions
_
10
} from '@safe-global/sdk-starter-kit'
Create a signer
Firstly, you need to get a signer, which will be the owner of a Safe account after it's deployed.
This example uses a private key, but any way to get an EIP-1193 compatible signer can be used.
_
10
const SIGNER_ADDRESS = // ...
_
10
const SIGNER_PRIVATE_KEY = // ...
_
10
const RPC_URL = 'https://rpc.ankr.com/eth_sepolia'
Initialize the
SafeClient
New Safe account
Existing Safe account
When deploying a new Safe account, you need to pass the configuration of the Safe in the
safeOptions
property. The Safe account is configured with your signer as the only owner in this case.
_
10
const safeClient = await createSafeClient({
_
10
provider: RPC_URL,
_
10
signer: SIGNER_PRIVATE_KEY,
_
10
safeOptions: {
_
10
owners: [SIGNER_ADDRESS],
_
10
threshold: 1
_
10
}
_
10
})
As this guide is related with ERC-4337 user operations, you need to add the
safeOperations
extension to the
safeClient
instance to support this functionality.
_
12
const bundlerOptions: BundlerOptions = {
_
12
bundlerUrl: `https://api.pimlico.io/v1/sepolia/rpc?add_balance_override&apikey=${PIMLICO_API_KEY}`
_
12
}
_
12
_
12
const paymasterOptions: PaymasterOptions = {
_
12
isSponsored: true,
_
12
paymasterUrl: `https://api.pimlico.io/v2/sepolia/rpc?add_balance_override&apikey=${PIMLICO_API_KEY}`
_
12
}
_
12
_
12
const safeClientWithSafeOperation = await safeClient.extend(
_
12
safeOperations(bundlerOptions, paymasterOptions)
_
12
)
The
safeClientWithSafeOperation
instance has now support for managing user operations.
Create a Safe transaction
Create an array of Safe transactions to execute.
_
10
const transactions = [{
_
10
to: '0x...',
_
10
data: '0x',
_
10
value: '0'
_
10
}]
Send the Safe operation
If you configured your Safe with
threshold
equal to
1
, calling the
sendSafeOperation
method will execute the user operation. However, if the
threshold
is greater than
1
the other owners of the Safe will need to confirm the user operation until the required number of signatures are collected.
_
10
const safeOperationResult = await safeClientWithSafeOperation.sendSafeOperation({
_
10
transactions
_
10
})
_
10
_
10
const safeOperationHash = safeOperationResult.safeOperations?.safeOperationHash
Confirm the Safe operations
If the user operation needs to be confirmed by other Safe owners, call the
confirmSafeOperation
method from a new
SafeClient
instance initialized with each of the signers that need to confirm it.
_
14
const newSafeClient = await createSafeClient({
_
14
provider: RPC_URL,
_
14
signer,
_
14
safeAddress: '0x...'
_
14
})
_
14
_
14
const newSafeClientWithSafeOperation = await newSafeClient.extend(
_
14
safeOperations({
_
14
bundlerUrl: `https://api.pimlico.io/v1/sepolia/rpc?add_balance_override&apikey=${PIMLICO_API_KEY}`
_
14
}, {
_
14
isSponsored: true,
_
14
paymasterUrl: `https://api.pimlico.io/v2/sepolia/rpc?add_balance_override&apikey=${PIMLICO_API_KEY}`
_
14
})
_
14
)
Finally, retrieve all the pending user operations from the Safe Transaction Service and confirm the one you just created with each missing owner.
_
11
const pendingSafeOperations =
_
11
await newSafeClientWithSafeOperation.getPendingSafeOperations()
_
11
_
11
for (const safeOperation of pendingSafeOperations.results) {
_
11
if (safeOperation.safeOperationHash !== safeOperationHash) {
_
11
return
_
11
}
_
11
_
11
const safeOperationResult =
_
11
await newSafeClientWithSafeOperation.confirmSafeOperation({ safeOperationHash })
_
11
}
Recap and further reading
After following this guide, you are able to deploy new Safe accounts and create, sign, and submit user operations with the Starter Kit.
Send Transactions
Reference
Was this page helpful?
Report issue
## /reference-sdk-starter-kit/overview
Starter Kit Reference
Overview
Starter Kit Reference
The
Starter Kit
is the starting point for integrating the Safe account in the front-end, as it provides the most simplified way to handle the Safe transaction flow in its different forms:
User operations
Multi-signature transactions
Off-chain and on-chain messages
Install dependencies
To add the Starter Kit to your project, run:
pnpm
npm
yarn
_
10
pnpm add @safe-global/sdk-starter-kit
← Go Back
constructor
Was this page helpful?
Report issue
## /sdk/protocol-kit
SDK
Protocol Kit
Protocol Kit
The Protocol Kit enables developers to interact with
Safe Smart Accounts
(opens in a new tab)
using a TypeScript interface. This kit can be used to create new Safe smart accounts, update the configuration of existing Safes, propose and execute transactions, among other features.
@safe-global/protocol-kit
The following guides show how to use the Protocol Kit and integrate it into your project:
Safe deployment
Multichain Safe deployment
Execute transactions
Transactions signatures
Message signatures
Resources
Protocol Kit on GitHub
(opens in a new tab)
Reference
Safe deployment
Was this page helpful?
Report issue
## /sdk/protocol-kit/guides/safe-deployment
SDK
Protocol Kit
Guides
Safe deployment
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
Node.js and npm
(opens in a new tab)
Install dependencies
First, you need to install some dependencies.
_
10
pnpm add @safe-global/protocol-kit viem
Steps
Imports
Here are all the necessary imports for this guide.
_
10
import Safe, {
_
10
PredictedSafeProps,
_
10
SafeAccountConfig,
_
10
SafeDeploymentConfig
_
10
} from '@safe-global/protocol-kit'
_
10
import { 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
.
_
10
const 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.
Optionally, you can
track your Safe deployments and transactions on-chain
by using the
onchainAnalytics
property.
_
17
const safeAccountConfig: SafeAccountConfig = {
_
17
owners: ['0x...', '0x...', '0x...'],
_
17
threshold: 2
_
17
// More optional properties
_
17
}
_
17
_
17
const predictedSafe: PredictedSafeProps = {
_
17
safeAccountConfig
_
17
// More optional properties
_
17
}
_
17
_
17
const protocolKit = await Safe.init({
_
17
provider: sepolia.rpcUrls.default.http[0],
_
17
signer: SIGNER_PRIVATE_KEY,
_
17
predictedSafe,
_
17
onchainAnalytics // Optional
_
17
})
Predict the Safe address
You can predict the Safe address using the
getAddress
method in the Protocol Kit.
_
10
const safeAddress = await protocolKit.getAddress()
Create the deployment transaction
Create the deployment transaction to deploy a new Safe smart account by calling the
createSafeDeploymentTransaction
method.
_
10
const 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.
_
12
const client = await protocolKit.getSafeProvider().getExternalSigner()
_
12
_
12
const 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
_
12
const 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.
_
10
const newProtocolKit = await protocolKit.connect({
_
10
safeAddress
_
10
})
_
10
_
10
const isSafeDeployed = await newProtocolKit.isSafeDeployed() // True
_
10
const safeAddress = await newProtocolKit.getAddress()
_
10
const safeOwners = await newProtocolKit.getOwners()
_
10
const 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.
Protocol Kit
Multichain Safe deployment
Was this page helpful?
Report issue
## /sdk/protocol-kit/guides/multichain-safe-deployment
SDK
Protocol Kit
Guides
Multichain Safe deployment
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
.
Prerequisites
Node.js and npm
(opens in a new tab)
Install dependencies
First, you need to install the Protocol Kit.
_
10
pnpm add @safe-global/protocol-kit viem
Steps
Imports
Here are all the necessary imports for this guide.
_
10
import Safe, {
_
10
PredictedSafeProps,
_
10
SafeAccountConfig,
_
10
SafeDeploymentConfig
_
10
} from '@safe-global/protocol-kit'
_
10
import { waitForTransactionReceipt } from 'viem/actions'
_
10
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
(opens in a new tab)
compatible signers are also supported. For detailed information about signers, please refer to the
Protocol Kit reference
.
_
10
const SIGNER_PRIVATE_KEY = // ...
Configure the Safe deployment
Define the
predictedSafe
object with the configuration for all the Safe accounts you will deploy. Check the reference to learn about all the different configuration options.
_
10
const safeAccountConfig: SafeAccountConfig = {
_
10
owners: ['0x...', '0x...', '0x...'],
_
10
threshold: 2
_
10
// ...
_
10
}
_
10
_
10
const predictedSafe: PredictedSafeProps = {
_
10
safeAccountConfig
_
10
// ...
_
10
}
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.
_
15
const protocolKitSepolia = await Safe.init({
_
15
provider: sepolia.rpcUrls.default.http[0],
_
15
signer: SIGNER_PRIVATE_KEY,
_
15
predictedSafe,
_
15
onchainAnalytics // Optional
_
15
// ...
_
15
})
_
15
_
15
const protocolKitChiado = await Safe.init({
_
15
provider: gnosisChiado.rpcUrls.default.http[0],
_
15
signer: PRIVATE_KEY,
_
15
predictedSafe,
_
15
onchainAnalytics // Optional
_
15
// ...
_
15
})
Optionally, you can
track your Safe deployments and transactions on-chain
by using the
onchainAnalytics
property.
Predict the Safe addresses
You can predict the Safe addresses by calling the
getAddress
method from each Protocol Kit instance and ensure that the result addresses are the same.
_
10
const sepoliaPredictedSafeAddress = await protocolKitSepolia.getAddress()
_
10
const chiadoPredictedSafeAddress = await protocolKitChiado.getAddress()
Deployment on Sepolia
Create the deployment transaction to deploy a new Safe account in Sepolia by calling the
createSafeDeploymentTransaction
method.
_
10
const sepoliaDeploymentTransaction =
_
10
await protocolKitSepolia.createSafeDeploymentTransaction()
Call the
sendTransaction
method from your Sepolia client instance and wait for the transaction to be executed.
_
14
const sepoliaClient =
_
14
await protocolKitSepolia.getSafeProvider().getExternalSigner()
_
14
_
14
const transactionHashSepolia = await sepoliaClient!.sendTransaction({
_
14
to: sepoliaDeploymentTransaction.to,
_
14
value: BigInt(sepoliaDeploymentTransaction.value),
_
14
data: sepoliaDeploymentTransaction.data as `0x${string}`,
_
14
chain: sepolia
_
14
})
_
14
_
14
await waitForTransactionReceipt(
_
14
sepoliaClient!,
_
14
{ hash: transactionHashSepolia }
_
14
)
Once the deployment transaction is executed, connect the new Safe address to the Protocol Kit instance by calling the
connect
method.
_
10
const newProtocolKitSepolia = await protocolKitSepolia.connect({
_
10
safeAddress: sepoliaPredictedSafeAddress
_
10
})
_
10
_
10
const isSepoliaSafeDeployed = await newProtocolKitSepolia.isSafeDeployed() // True
_
10
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.
_
24
const chiadoDeploymentTransaction =
_
24
await protocolKitChiado.createSafeDeploymentTransaction()
_
24
_
24
const chiadoClient =
_
24
await protocolKitChiado.getSafeProvider().getExternalSigner()
_
24
_
24
const transactionHashChiado = await chiadoClient!.sendTransaction({
_
24
to: chiadoDeploymentTransaction.to,
_
24
value: BigInt(chiadoDeploymentTransaction.value),
_
24
data: chiadoDeploymentTransaction.data as `0x${string}`,
_
24
chain: gnosisChiado
_
2프로젝트로 돌아가기