Creating Gnosis Safes via the API

Is it possible to create a Gnosis Safe via API instead of the UI? Or at least is there a factory contract that can be used to easily deploy safes and then have them linked to defender (I’m interested in polygon and mumbai networks)?

1 Like

Hi George, thanks for reaching out. Out of curiosity, may I ask why you need to create a Safe via the API? The information could help us design better ways of supporting your use scenarios from Defender.

As regards Safe factory contracts, we deployed them to the following addresses:

Polygon/Matic:

  • Safe Master Contract: ‘0xC4180A6A9EeCc46Ae83b6281C07EC1c6544fe581’
  • Safe Contract Factory: ‘0x971497458154c42a3d96B8c03453cB97219ce7Ee’

Mumbai:

  • Safe Master Contract: ‘0xC4180A6A9EeCc46Ae83b6281C07EC1c6544fe581’
  • Safe Contract Factory: ‘0x971497458154c42a3d96B8c03453cB97219ce7Ee’

To deploy one, you need to execute the Contract Factory’s createProxy function, which receives arguments to execute the setup function on the Master Contract.

The code using Ethers JS would look (approximately) like this:

const masterContractAddress = '0x...';
const safeOwners: string[] =  ['0x...', '0x...', etc];
const threshold = 3;

// gnosisSafeAbi is the Gnosis Safe ABI in JSON format,
// you can find an example here: https://github.com/gnosis/safe-deployments/blob/main/src/assets/v1.1.1/gnosis_safe.json#L16
const gnosisInterface = new Interface(gnosisSafeAbi);
const safeSetupData = gnosisInterface.encodeFunctionData('setup', [
  safeOwners, 
  threshold,
  '0x0000000000000000000000000000000000000000',
  '0x',
  '0x0000000000000000000000000000000000000000',
  '0x0000000000000000000000000000000000000000',
  '0',
  '0x0000000000000000000000000000000000000000',
]);

// safeContractFactory is an instance of the "Contract" type from Ethers JS
// see https://docs.ethers.io/v5/getting-started/#getting-started--contracts
// for more details.
// You're going to need the address of a Safe contract factory and the ABI,
// which can be found here: https://github.com/gnosis/safe-deployments/blob/main/src/assets/v1.1.1/proxy_factory.json#L16
const proxy = await safeContractFactory.functions.createProxy(masterContractAddress, safeSetupData);

Once you’ve deployed a Safe, you can use https://www.npmjs.com/package/defender-admin-client#user-content-adding-contracts to programmatically import it to Defender via the API. It’s worth noting that you don’t need to do this though, you can “just use it” by pasting its address when you create an Admin proposal.

Hope this helps!
Martín

1 Like

Hi Martin! Thank you for your response, it’s really helpful. We have a use case where we need to trigger the deployment of a safe on demand (once an API call is made) - this is where deployment via API would come handy (but given all your info that’s not really needed at all since we can trigger the deployment directly via the code you provided).

Glad it helped! For your use case, you might want to consider writing an autotask that takes care of the deploy for you through a relayer. You can configure the autotask to be triggered via a webhook, and invoke it from your API call. That way you can take advantage of Defender’s secure key management, its gas estimation heuristics, and you can also keep a single relayer as the funding account for those deployments.

Can you add the Safe Master Contract and Safe Master Factory addresses for ETH as well?

We're using the v1.3.0 deployments:

safeMasterContract: "0xd9Db270c1B5E3Bd161E8c8503c55cEABeE709552"
safeContractFactory: "0xa6B71E26C5e0845f74c812102Ca7114b6a896AB2"