Created
April 18, 2024 06:07
-
-
Save piavgh/491861be99836556570c171a4247ecc7 to your computer and use it in GitHub Desktop.
Test delegate call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.19; | |
import "forge-std/console.sol"; | |
contract Mind { | |
/// @notice This could be useful... | |
address private deploymentAddress; | |
constructor() { | |
deploymentAddress = address(this); | |
} | |
/// @notice Checks if the current call is a delegate call. | |
/// @return isDelegate true if this is a delegate call, false otherwise. | |
function isDelegateCall() public view returns (bool isDelegate) { | |
console.log( | |
"Deployment address in isDelegateCall: %s", | |
deploymentAddress | |
); | |
return address(this) != deploymentAddress; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.19; | |
import "forge-std/console.sol"; | |
import "./Mind.sol"; | |
contract MindDelegateCaller { | |
/// @notice This could be useful... | |
address private deploymentAddress; | |
Mind mind; | |
constructor(address mindAddress) { | |
deploymentAddress = address(this); | |
mind = Mind(mindAddress); | |
} | |
/// @notice Checks if the current call is a delegate call. | |
/// @return isDelegate true if this is a delegate call, false otherwise. | |
function isDelegateCall() public returns (bool isDelegate) { | |
(, bytes memory _returnData) = address(mind).delegatecall( | |
abi.encodeWithSignature("isDelegateCall()") | |
); | |
isDelegate = abi.decode(_returnData, (bool)); | |
console.log("isDelegate: %s", isDelegate); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.19; | |
import "forge-std/Test.sol"; | |
import "src/standalone-quests/delegate-call-detection/Mind.sol"; | |
import "src/standalone-quests/delegate-call-detection/MindDelegateCaller.sol"; | |
contract MindTest is Test { | |
Mind mind; | |
MindDelegateCaller mindDelegateCaller; | |
constructor() { | |
mind = new Mind(); | |
console.log("Mind address: %s", address(mind)); | |
mindDelegateCaller = new MindDelegateCaller(address(mind)); | |
console.log( | |
"MindDelegateCaller address: %s", | |
address(mindDelegateCaller) | |
); | |
} | |
function test_detect_delegate_calls() external { | |
mindDelegateCaller.isDelegateCall(); | |
} | |
// function test_detect_non_delegate_calls() external { | |
// bool isDelegate = mind.isDelegateCall(); | |
// assertTrue(!isDelegate, "Failed to detect non-delegate call"); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment