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/types-kit
Imports
_10import { ethers } from 'ethers'_10import SafeApiKit, { AddSafeDelegateProps } from '@safe-global/api-kit'
Get the delegates from a Safe
_10// Initialize the API Kit_10const apiKit = new SafeApiKit({_10 chainId: 11155111n_10})_10_10// Get the Safe delegates_10const delegates = await apiKit.getSafeDelegates({_10 delegatorAddress: config.SAFE_ADDRESS_10})
Add a delegate to a delegator
_17const provider = new ethers.JsonRpcProvider(config.RPC_URL)_17_17const ownerA = new ethers.Wallet(config.OWNER_A_PRIVATE_KEY, provider)_17const ownerAAddress = await ownerA.getAddress()_17_17const ownerB = new ethers.Wallet(config.OWNER_B_PRIVATE_KEY, provider)_17const ownerBAddress = await ownerB.getAddress()_17_17const delegateConfig: AddSafeDelegateProps = {_17 delegateAddress: ownerBAddress || '0x',_17 delegatorAddress: ownerAAddress || '0x',_17 signer: ownerA,_17 label: 'Label'_17}_17_17// Add Owner B as a delegate of Owner A for all Safes accounts (safeAddress = null)_17const 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)