Ethereum: Creating an embedded function call using createProxyWithNonce
When working with the Safe Proxy Factory on Ethereum, deploying a new Gnosis Safe can be a complex process. One common issue occurs when trying to create a proxy for a contract or function using the createProxyWithNonce
method.
In this article, we will discuss the specifics of using the createProxyWithNonce
method with embedded functions and nonces.
The issue: embedded function calls
Let’s look at an example of a simple Gnosis Safe implementation that uses two contracts:
Secure Contract
: This contract provides basic security features, such as adding users to the whitelist.
MyContract
: This is the main contract that we want to proxy, which implements custom logic.
To implement both contracts with the Safe Proxy Factory using `createProxyWithNonce'', we need to create nested function calls:
MySafe contract {
function myFunction() internal return () {
// Custom logic here...
}
}
contract A SafeContract a SafeProxyFactory {
function safeFunction() public nonces[0] {
return myFunction();
}
function createProxyWithNonce(
_singleton,
initializer,
saltNonce
) internal exchange return (MySafe) {
MySafe proxy = new MySafe(address(_singleton));
// Initialize the proxy with some data...
return proxy;
}
}
As you can see, we are creating nested function calls: createProxyWithNonceis called recursively inside another function. There are several problems with this approach:
- No nested function calls allowed: Solidity does not support recursive function calls.
- Nonces only valid for the first call: SincecreateProxyWithNonce
creates a new instance of
MySafe, we lose all nonces from the previous call. To fix this, we need to explicitly pass the nonce.
Solution: explicit negation
To solve the problems mentioned above, we can change our approach as follows:
MySafe contract {
function myFunction() internal return () {
// Custom logic here...
}
}
contract SafeContract a SafeProxyFactory {
function createProxyWithNonce(
_singleton,
initializer,
saltNonce
) public disposable[0] {
return new MySafe(address(_singleton));
}
function getProxy() innerView return (MySafe) {
// Returns the proxy instance
}
}
In this revised approach:
- We pass nonce[0]
to the
createProxyWithNoncemethod, which gets a nonce on the first call.
- We also define a separategetProxy()
function that returns the proxy instance. This allows us to return the proxy instance without having to rely on nonces.
Conclusion
If you want to create nested function calls withcreateProxyWithNonce’, you must explicitly pass the nonces, either as a parameter or using an innerView property. This avoids recursive function calls and ensures that the contract is initialized properly.
When implementing Gnosis Safe via Safe Proxy Factory, be sure to follow these guidelines when creating nested function calls:
- Explicitly pass nonces in “createProxyWithNonce” methods.
- Use internal view properties to retrieve proxy instances.
Leave a Reply