Skip to content

Instantly share code, notes, and snippets.

@piavgh
Created April 18, 2024 06:07
Show Gist options
  • Save piavgh/491861be99836556570c171a4247ecc7 to your computer and use it in GitHub Desktop.
Save piavgh/491861be99836556570c171a4247ecc7 to your computer and use it in GitHub Desktop.
Test delegate call
// 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;
}
}
// 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);
}
}
// 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