Skip to content

Instantly share code, notes, and snippets.

@PatrickAlphaC
Last active April 21, 2022 20:39
Show Gist options
  • Save PatrickAlphaC/ad3c73dc5b392ce8130115dd1f486256 to your computer and use it in GitHub Desktop.
Save PatrickAlphaC/ad3c73dc5b392ce8130115dd1f486256 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract SelectorsAndSignatures {
address public s_someAddress;
uint256 public s_amount;
function getSelectorOne() public pure returns(bytes4 selector){
selector = bytes4(keccak256(bytes("transfer(address,uint256)")));
}
function getSelectorTwo() public view returns(bytes4 selector){
bytes memory functionCallData = abi.encodeWithSignature("transfer(address,uint256)", address(this), 123);
selector = bytes4(bytes.concat(functionCallData[0], functionCallData[1], functionCallData[2], functionCallData[3]));
}
function getCallData() public view returns(bytes memory){
return abi.encodeWithSignature("transfer(address,uint256)", address(this), 123);
}
// Pass this:
// 0xa9059cbb000000000000000000000000d7acd2a9fd159e69bb102a1ca21c9a3e3a5f771b000000000000000000000000000000000000000000000000000000000000007b
// This is output of `getCallData()`
function getSelectorThree(bytes calldata functionCallData) public pure returns(bytes4 selector){
// offset is a special attribute of calldata
assembly {
selector := calldataload(functionCallData.offset)
}
}
function transfer(address someAddress, uint256 amount) public {
// Some code
s_someAddress = someAddress;
s_amount = amount;
}
function getSelectorFour() public pure returns(bytes4 selector){
return this.transfer.selector;
}
function getSignatureOne() public pure returns(string memory){
return "transfer(address,uint256)";
}
}
contract CallFunctionWithoutContract {
address public s_selectorsAndSignaturesAddress;
constructor(address selectorsAndSignaturesAddress){
s_selectorsAndSignaturesAddress = selectorsAndSignaturesAddress;
}
// pass in 0xa9059cbb000000000000000000000000d7acd2a9fd159e69bb102a1ca21c9a3e3a5f771b000000000000000000000000000000000000000000000000000000000000007b
// you could use this to change state
function callFunctionDirectly(bytes calldata callData) public returns(bytes4, bool) {
(bool success, bytes memory returnData) = s_selectorsAndSignaturesAddress.call(
abi.encodeWithSignature("getSelectorThree(bytes)", callData)
);
return (bytes4(returnData), success);
}
// with a staticcall, we can have this be a view function!
function staticCallFunctionDirectly() public view returns(bytes4, bool){
(bool success, bytes memory returnData) = s_selectorsAndSignaturesAddress.staticcall(
abi.encodeWithSignature("getSelectorOne()")
);
return (bytes4(returnData), success);
}
function callTransferFunctionDirectly(address someAddress, uint256 amount) public returns(bytes4, bool) {
(bool success, bytes memory returnData) = s_selectorsAndSignaturesAddress.call(
abi.encodeWithSignature("transfer(address,uint256)", someAddress, amount)
);
return (bytes4(returnData), success);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment