I read online and understand the solution to this challenge. I decided to experiment and solve this challenge using a proxy instead. As per my understanding of proxies and delegate call, the following code I wrote should be able to solve this challenge. However, I am not able to understand why the following code does not work.
Code:
contract GateKeeper3Proxy {
address public owner = address(this);
address public entrant = 0xD1729E3101b87B7032848C8157896d2FED18C46;
bool public allowEntrance = true;
constructor() payable {}
function execute(address payable _gatekeeperAddress) public {
GatekeeperThree(_gatekeeperAddress).construct0r();
_gatekeeperAddress.transfer(0.001 ether);
(bool success, bytes memory data) = _gatekeeperAddress.delegatecall(
abi.encodeWithSignature("enter()")
);
require(success);
}
receive () external payable {
revert();
}
}
contract GatekeeperThree {
address public owner;
address public entrant;
bool public allowEntrance;
SimpleTrick public trick;
function construct0r() public {
owner = msg.sender;
}
modifier gateOne() {
require(msg.sender == owner);
require(tx.origin != owner);
_;
}
modifier gateTwo() {
require(allowEntrance == true);
_;
}
modifier gateThree() {
if (address(this).balance > 0.001 ether && payable(owner).send(0.001 ether) == false) {
_;
}
}
function getAllowance(uint _password) public {
if (trick.checkPassword(_password)) {
allowEntrance = true;
}
}
function createTrick() public {
trick = new SimpleTrick(payable(address(this)));
trick.trickInit();
}
function enter() public gateOne gateTwo gateThree {
entrant = tx.origin;
}
receive () external payable {}
}
I created a GateKeeper3Proxy contract which has an execute method. The proxy correctly imitates the storage layout of the gatekeeper contract. If I transfer the correct amount and execute enter function with delegatecall, the values specified in the proxy contract for the variables should be executed. However, when I debug my code, I see that the modifiers are not using the correct value, for example, in gate one, the value of owner variable is still the zero address. This makes the solution fail and confuses me about proxies. I was expecting that owner value in gate one should have been the value I specified in the proxy contract.
Please help me in understanding this concept one more time and tell me what is wrong with my reasoning.