API
Guides
Delegates

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

  1. Node.js and npm (opens in a new tab) when using the Safe{Core} SDK.
  2. Python (opens in a new tab) >= 3.9 when using safe-eth-py.
  3. Have a Safe account.

Steps

Install dependencies


_10
yarn add ethers @safe-global/api-kit @safe-global/protocol-kit @safe-global/safe-core-sdk-types

Imports


_10
import { ethers } from 'ethers'
_10
import SafeApiKit, { AddSafeDelegateProps } from '@safe-global/api-kit'
_10
import { EthersAdapter } from '@safe-global/protocol-kit'

Get the delegates from a Safe


_20
const ethProvider = new ethers.JsonRpcProvider(config.RPC_URL)
_20
_20
// Instantiate an EthAdapter with Owner A
_20
const ownerA = new ethers.Wallet(config.OWNER_A_PRIVATE_KEY, ethProvider)
_20
const ethAdapterOwnerA = new EthersAdapter({
_20
ethers,
_20
signerOrProvider: ownerA
_20
})
_20
_20
// Initialize the API Kit
_20
const apiKit = new SafeApiKit({
_20
chainId: 11155111n
_20
})
_20
_20
const ownerAAddress = await ethAdapterOwnerA.getSignerAddress()
_20
_20
// Get the Safe delegates
_20
const delegates = await apiKit.getSafeDelegates({
_20
delegatorAddress: config.SAFE_ADDRESS
_20
})

Add a delegate to a delegator


_19
const ownerB = new ethers.Wallet(config.OWNER_B_PRIVATE_KEY, ethProvider)
_19
_19
// Instantiate an EthAdapter with Owner B
_19
const ethAdapterOwnerB = new EthersAdapter({
_19
ethers,
_19
signerOrProvider: ownerB
_19
})
_19
_19
const ownerBAddress = await ethAdapterOwnerB.getSignerAddress()
_19
_19
const 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)
_19
const safeDelegateAddResponse = await apiKit.addSafeDelegate(delegateConfig)

Delete a delegate of a delegator


_10
const delegateConfig: DeleteSafeDelegateProps = {
_10
delegateAddress: ownerBAddress || '0x',
_10
delegatorAddress: ownerAAddress || '0x',
_10
signer: ownerA,
_10
}
_10
_10
// Remove Owner B as delegate of Owner A
_10
await apiKit.removeSafeDelegate(delegateConfig)

Was this page helpful?