Skip to content

Instantly share code, notes, and snippets.

@ZigBalthazar
Created October 11, 2023 05:04
Show Gist options
  • Save ZigBalthazar/977864a5110d18380f07ca4685f4c260 to your computer and use it in GitHub Desktop.
Save ZigBalthazar/977864a5110d18380f07ca4685f4c260 to your computer and use it in GitHub Desktop.
implementation of a sample contract deploy and raw Call
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract UserContractFactory {
mapping(address => address) public userContracts;
address public storageContractAddr;
address public owner;
bytes constant GET_NUMBER_SIG = abi.encodeWithSelector(bytes4(keccak256("getNumber()")));
bytes4 constant SET_NUMBER_SIG = bytes4(keccak256("setNumber(uint256)"));
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can call this");
_;
}
modifier hasNoContract() {
require(userContracts[msg.sender] == address(0), "User already has a contract");
_;
}
modifier hasContract() {
require(userContracts[msg.sender] != address(0), "User has not a contract");
_;
}
function setStorageContractAddress(address addr) public onlyOwner {
storageContractAddr = addr;
}
function deployContractForUser() public hasNoContract {
UserContract newUserContract = new UserContract();
require(address(newUserContract) != address(0), "Failed to deploy contract");
userContracts[msg.sender] = address(newUserContract);
}
function getUserContractAddr() public view returns (address) {
return userContracts[msg.sender];
}
function callStorageContract(bytes memory data) public hasContract returns (bool, bytes memory) {
require(checkSomeThing(), "SomeThing Went Wrong!!");
bytes4 functionSignature = bytes4(keccak256("rawCall(address,bytes)"));
return getUserContractAddr().call(abi.encodeWithSelector(functionSignature, storageContractAddr, data));
}
function callSetNumber(uint256 number) public hasContract returns (bool, bytes memory) {
bytes memory storageFunctionData = abi.encodeWithSelector(SET_NUMBER_SIG, number);
return callStorageContract(storageFunctionData);
}
function callGetNumber() public hasContract returns (bool, bytes memory) {
return callStorageContract(GET_NUMBER_SIG);
}
function checkSomeThing() internal pure returns(bool){
return true;
}
}
contract UserContract {
address public creator;
constructor() {
creator = msg.sender;
}
modifier onlyCreator() {
require(msg.sender == creator, "Only the creator can call this");
_;
}
function rawCall(address target, bytes memory data) public onlyCreator returns (bytes memory result) {
(bool success, bytes memory returnData) = target.call(data);
require(success, "Raw call failed");
return returnData;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract Storage {
mapping(address => uint256) public numbersMapping;
function setNumber(uint256 number) public {
numbersMapping[msg.sender] = number;
}
function getNumber() public view returns (uint256) {
return numbersMapping[msg.sender];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment