Manage user delegates
This guide shows how to interact with the Safe Transaction Service API to manage user delegates.
The different steps are implemented using Curl (opens in a new tab) requests, the Safe{Core} SDK (opens in a new tab) TypeScript library and the safe-eth-py (opens in a new tab) Python library.
Prerequisites
- Node.js and npm (opens in a new tab) when using the Safe{Core} SDK.
- Python (opens in a new tab) >= 3.9 when using
safe-eth-py
. - Have a Safe account.
Steps
Install dependencies
_10yarn add ethers @safe-global/api-kit @safe-global/protocol-kit @safe-global/safe-core-sdk-types
Imports
_10import { ethers } from 'ethers'_10import SafeApiKit, { AddSafeDelegateProps } from '@safe-global/api-kit'_10import { EthersAdapter } from '@safe-global/protocol-kit'
Get the delegates from a Safe
_20const ethProvider = new ethers.JsonRpcProvider(config.RPC_URL)_20_20// Instantiate an EthAdapter with Owner A_20const ownerA = new ethers.Wallet(config.OWNER_A_PRIVATE_KEY, ethProvider)_20const ethAdapterOwnerA = new EthersAdapter({_20 ethers,_20 signerOrProvider: ownerA_20})_20_20// Initialize the API Kit_20const apiKit = new SafeApiKit({_20 chainId: 11155111n_20})_20_20const ownerAAddress = await ethAdapterOwnerA.getSignerAddress()_20_20// Get the Safe delegates_20const delegates = await apiKit.getSafeDelegates({_20 delegatorAddress: config.SAFE_ADDRESS_20})
Add a delegate to a delegator
_19const ownerB = new ethers.Wallet(config.OWNER_B_PRIVATE_KEY, ethProvider)_19_19// Instantiate an EthAdapter with Owner B_19const ethAdapterOwnerB = new EthersAdapter({_19 ethers,_19 signerOrProvider: ownerB_19})_19_19const ownerBAddress = await ethAdapterOwnerB.getSignerAddress()_19_19const delegateConfig: AddSafeDelegateProps = {_19 delegateAddress: ownerBAddress || '0x',_19 delegatorAddress: ownerAAddress || '0x',_19 signer: ownerA,_19 label: 'Label'_19}_19_19// Add Owner B as a delegate of Owner A for all Safes accounts (safeAddress = null)_19const safeDelegateAddResponse = await apiKit.addSafeDelegate(delegateConfig)
Delete a delegate of a delegator
_10const delegateConfig: DeleteSafeDelegateProps = {_10 delegateAddress: ownerBAddress || '0x',_10 delegatorAddress: ownerAAddress || '0x',_10 signer: ownerA,_10}_10_10// Remove Owner B as delegate of Owner A_10await apiKit.removeSafeDelegate(delegateConfig)