Skip to content

Plugins AVB

Andrei Vlad Birgaoanu edited this page Jul 5, 2023 · 2 revisions

Plugin contracts consist of stateless scripts and are the key in automating interactions with the proxy. The execution of these plugins is facilitated by the proxy's fallback function, which can be called by any user - a point worth noting.

Every plugin is required to implement IPRBProxyPlugin.

An important use case for plugins includes hooks that invoke calls on the proxy, here is an example:

interface Hook {
    function onFoo(address asset) external;
}

interface IERC20 {
    function transfer(address _to, uint256 _value) public returns (bool success);
}
contract C {
    address public dai = 0x6B175474E89094C44Da98b954EedeAC495271d0F;

    function foo(address proxy) external {
        IERC20(dai).transfer(proxy, 100); // transfer 100 DAI to proxy
        Hook(proxy).onFoo(dai); // run the hook on proxy
    }
}
contract ProxyPlugin is IPRBProxyPlugin, Hook {
    // Implement the IPRBProxyPlugin interface
    function getMethods() external pure returns (bytes4[] memory methods) {
        methods = new bytes4[](1);
        methods[0] = this.onFoo.selector;
    }

    // Implement the Hook interface
    function onFoo(address asset) external {
        address owner = IPRBProxy(address(this)).owner();
        IERC20(asset).transfer(owner, 100); // transfer the funds received from {C.foo} to the owner of the proxy
    }
}

To make the flow work, the ProxyPlugin must be installed via {PRBProxyRegistry.installPlugin}.

The example from above simply redirects the funds, originally sent from contract C to the proxy contract, to the owner of the proxy.

You can see another good example in the Sablier's v2-periphery.

Clone this wiki locally