Skip to content

Instantly share code, notes, and snippets.

@gitpusha
Created March 14, 2020 11:28
Show Gist options
  • Save gitpusha/dfa3c5b966d08e72b0f760c7d4cf7467 to your computer and use it in GitHub Desktop.
Save gitpusha/dfa3c5b966d08e72b0f760c7d4cf7467 to your computer and use it in GitHub Desktop.
function createTwoGelatoUserProxy(
address _mastercopy,
bytes memory _initializer,
uint256 _saltNonce
)
public
payable
override
returns(address userProxy)
{
// Salt used by GnosisSafeProxyFactory
bytes32 salt = keccak256(abi.encodePacked(keccak256(_initializer), _saltNonce));
// Get GnosisSafeProxy.CreationCode used by deployed Factory
bytes memory creationCode = factory.proxyCreationCode();
// Derive undeployed userProxy address
address predictedAddress = address(uint(keccak256(abi.encodePacked(
byte(0xff),
address(factory),
salt,
keccak256(abi.encodePacked(creationCode, _mastercopy))
))));
// Prefund undeployed proxy
if (msg.value > 0) payable(predictedAddress).sendValue(msg.value);
// Deploy userProxy with create2
userProxy = address(factory.createProxyWithNonce(
_mastercopy,
_initializer,
_saltNonce
));
require(
userProxy == predictedAddress,
"GelatoCore.createTwoGelatoUserProxy: wrong address prediction"
);
// Map userProxy onto User
userByGelatoProxy[userProxy] = msg.sender;
gelatoProxyByUser[msg.sender] = userProxy;
// Success
emit LogGelatoUserProxyCreation(msg.sender, userProxy, msg.value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment