Skip to content

Latest commit

 

History

History
398 lines (343 loc) · 12.5 KB

ApprovalReceiver.md

File metadata and controls

398 lines (343 loc) · 12.5 KB

Base contract for receiving approval from SOV token. (ApprovalReceiver.sol)

View Source: contracts/governance/ApprovalReceiver.sol

↗ Extends: ErrorDecoder, IApproveAndCall ↘ Derived Contracts: FourYearVestingLogic, StakingStakeModule, VestingLogic

ApprovalReceiver contract

Modifiers

onlyThisContract

modifier onlyThisContract() internal

Functions


receiveApproval

⤾ overrides IApproveAndCall.receiveApproval

Receives approval from SOV token.

function receiveApproval(address _sender, uint256 _amount, address _token, bytes _data) external nonpayable

Arguments

Name Type Description
_sender address
_amount uint256
_token address
_data bytes The data will be used for low level call.
Source Code
function receiveApproval(
        address _sender,
        uint256 _amount,
        address _token,
        bytes calldata _data
    ) external {
        // Accepts calls only from SOV token.
        require(msg.sender == _getToken(), "unauthorized");
        require(msg.sender == _token, "unauthorized");

        // Only allowed methods.
        bool isAllowed = false;
        bytes4[] memory selectors = _getSelectors();
        bytes4 sig = _getSig(_data);
        for (uint256 i = 0; i < selectors.length; i++) {
            if (sig == selectors[i]) {
                isAllowed = true;
                break;
            }
        }
        require(isAllowed, "method is not allowed");

        // Check sender and amount.
        address sender;
        uint256 amount;
        (, sender, amount) = abi.decode(
            abi.encodePacked(bytes28(0), _data),
            (bytes32, address, uint256)
        );
        require(sender == _sender, "sender mismatch");
        require(amount == _amount, "amount mismatch");

        _call(_data);
    }

_getToken

⤿ Overridden Implementation(s): FourYearVestingLogic._getToken,VestingLogic._getToken

Returns token address, only this address can be a sender for receiveApproval.

function _getToken() internal view
returns(address)
Source Code
function _getToken() internal view returns (address) {
        return address(0);
    }

_getSelectors

⤿ Overridden Implementation(s): FourYearVestingLogic._getSelectors,VestingLogic._getSelectors

Returns list of function selectors allowed to be invoked.

function _getSelectors() internal pure
returns(bytes4[])
Source Code
function _getSelectors() internal pure returns (bytes4[] memory) {
        return new bytes4[](0);
    }

_call

Makes call and reverts w/ enhanced error message.

function _call(bytes _data) internal nonpayable

Arguments

Name Type Description
_data bytes Error message as bytes.
Source Code
function _call(bytes memory _data) internal {
        (bool success, bytes memory returnData) = address(this).call(_data);
        if (!success) {
            if (returnData.length <= ERROR_MESSAGE_SHIFT) {
                revert("receiveApproval: Transaction execution reverted.");
            } else {
                revert(_addErrorMessage("receiveApproval: ", string(returnData)));
            }
        }
    }

_getSig

Extracts the called function selector, a hash of the signature.

function _getSig(bytes _data) internal pure
returns(sig bytes4)

Arguments

Name Type Description
_data bytes The msg.data from the low level call.

Returns

sig First 4 bytes of msg.data i.e. the selector, hash of the signature.

Source Code
function _getSig(bytes memory _data) internal pure returns (bytes4 sig) {
        assembly {
            sig := mload(add(_data, 32))
        }
    }

Contracts