Skip to content

Instantly share code, notes, and snippets.

@simondlr
Last active June 30, 2021 02:01
Show Gist options
  • Save simondlr/48b77206b2483932f0f5 to your computer and use it in GitHub Desktop.
Save simondlr/48b77206b2483932f0f5 to your computer and use it in GitHub Desktop.
Function hooks in Solidity
contract Function_hook_example {
function Function_hook_example() {
owner = msg.sender;
}
modifier isOwner {
if (msg.sender == owner) {
_
}
}
//do a hook after normal exeuction.
modifier post_hook {
_
if (post_function_hooks[msg.sig] != 0x0) {
post_function_hooks[msg.sig].call(msg.data);
}
}
//do a hook before normal execution.
modifier pre_hook {
if (pre_function_hooks[msg.sig] != 0x0) {
pre_function_hooks[msg.sig].call(msg.data);
}
_
}
function doSomething(bytes32 _text) pre_hook post_hook {
//do basic stuff here.
}
function changeOwner(address _newOwner) isOwner {
owner = _newOwner;
}
function changeHook(bytes32 _type, bytes4 _functionSig, address _contract) isOwner {
if (_type == "pre") {
pre_function_hooks[_functionSig] = _contract;
} else if (_type == "post") {
post_function_hooks[_functionSig] = _contract;
}
}
address public owner;
mapping (bytes4 => address) pre_function_hooks;
mapping (bytes4 => address) post_function_hooks;
}
contract otherContract {
//called from hook example.
function doSomething(bytes32 _text) {
//do extra stuff here. Use _text or not.
//ie, notify etherex of a deposit.
}
}
@jambestwick
Copy link

I dont think it can be use when the contract has been deploy。just a proxy mode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment