Git Source

Inherits: ActionTypesArray, Ownable, RuleAdministratorOnly, IApplicationHandlerEvents, ICommonApplicationHandlerEvents, IInputErrors, IZeroAddressError, IAppHandlerErrors, ProtocolApplicationHandlerCommon

Author: @ShaneDuncan602, @oscarsernarosero, @TJ-Everett

This contract is the rules handler for all application level rules. It is implemented via the AppManager

This contract is injected into the appManagers.

State Variables

VERSION

string private constant VERSION = "2.1.0";

appManager

IAppManager immutable appManager;

appManagerAddress

address public immutable appManagerAddress;

ruleProcessor

IRuleProcessor immutable ruleProcessor;

ruleProcessorAddress

address public immutable ruleProcessorAddress;

accountMaxValueByAccessLevel

Rule mappings

mapping(ActionTypes => Rule) accountMaxValueByAccessLevel;

accountMaxValueByRiskScore

mapping(ActionTypes => Rule) accountMaxValueByRiskScore;

accountMaxTxValueByRiskScore

mapping(ActionTypes => Rule) accountMaxTxValueByRiskScore;

accountMaxValueOutByAccessLevel

mapping(ActionTypes => Rule) accountMaxValueOutByAccessLevel;

accountDenyForNoAccessLevel

mapping(ActionTypes => Rule) accountDenyForNoAccessLevel;

pauseRuleActive

Pause Rule on-off switch

bool private pauseRuleActive;

erc20Pricer

Pricing Module interfaces

IProtocolERC20Pricing erc20Pricer;

nftPricer

IProtocolERC721Pricing nftPricer;

erc20PricingAddress

address public erc20PricingAddress;

nftPricingAddress

address public nftPricingAddress;

usdValueTransactedInRiskPeriod

MaxTxSizePerPeriodByRisk data

mapping(address => uint128) usdValueTransactedInRiskPeriod;

lastTxDateRiskRule

mapping(address => uint64) lastTxDateRiskRule;

usdValueTotalWithrawals

mapping(address => uint128) usdValueTotalWithrawals;

Functions

constructor

Initializes the contract setting the AppManager address as the one provided and setting the ruleProcessor for protocol access

constructor(address _ruleProcessorProxyAddress, address _appManagerAddress);

Parameters

NameTypeDescription
_ruleProcessorProxyAddressaddressof the protocol’s Rule Processor contract.
_appManagerAddressaddressaddress of the application AppManager.

_checkWhichApplicationRulesActive

function _checkWhichApplicationRulesActive(ActionTypes _action) internal view returns (bool);

_checkNonCustodialRules

function _checkNonCustodialRules(ActionTypes _action) internal view returns (bool);

requireApplicationRulesChecked

checks if any of the Application level rules are active

function requireApplicationRulesChecked(ActionTypes _action, address _sender) public view returns (bool);

Parameters

NameTypeDescription
_actionActionTypesthe current action type
_senderaddress

Returns

NameTypeDescription
<none>booltrue if one or more rules are active

checkApplicationRules

Check Application Rules for valid transaction.

function checkApplicationRules(
    address _tokenAddress,
    address _sender,
    address _from,
    address _to,
    uint256 _amount,
    uint16 _nftValuationLimit,
    uint256 _tokenId,
    ActionTypes _action,
    HandlerTypes _handlerType
) external onlyOwner;

Parameters

NameTypeDescription
_tokenAddressaddressaddress of the token
_senderaddressaddress of the calling account passed through from the token
_fromaddressaddress of the from account
_toaddressaddress of the to account
_amountuint256amount of tokens to be transferred
_nftValuationLimituint16number of tokenID’s per collection before checking collection price vs individual token price
_tokenIduint256tokenId of the NFT token
_actionActionTypesAction to be checked. This param is intentially added for future enhancements.
_handlerTypeHandlerTypesthe type of handler, used to direct to correct token pricing

_checkRiskRules

Based on the Handler Type retrieve pricing valuations

This function consolidates all the Risk rule checks.

function _checkRiskRules(
    address _from,
    address _to,
    address _sender,
    uint128 _balanceValuation,
    uint128 _transferValuation,
    ActionTypes _action
) internal;

Parameters

NameTypeDescription
_fromaddressaddress of the from account
_toaddressaddress of the to account
_senderaddressaddress of the caller
_balanceValuationuint128recepient address current total application valuation in USD with 18 decimals of precision
_transferValuationuint128valuation of the token being transferred in USD with 18 decimals of precision
_actionActionTypesthe current user action

_checkAccessLevelRules

non custodial buy non custodial sell

This function consolidates all the Access Level rule checks.

function _checkAccessLevelRules(
    address _from,
    address _to,
    address _sender,
    uint128 _balanceValuation,
    uint128 _transferValuation,
    ActionTypes _action
) internal;

Parameters

NameTypeDescription
_fromaddressaddress of the from account
_toaddressaddress of the to account
_senderaddressaddress of the to caller
_balanceValuationuint128recepient address current total application valuation in USD with 18 decimals of precision
_transferValuationuint128valuation of the token being transferred in USD with 18 decimals of precision
_actionActionTypesthe current user action

_checkAccountMaxTxValueByRiskScore

Non custodial buy Non custodial sell

This function consolidates the MaxTXValueByRiskScore rule checks for the from address.

function _checkAccountMaxTxValueByRiskScore(
    ActionTypes _action,
    address _address,
    uint8 _riskScoreFrom,
    uint128 _transferValuation
) internal;

Parameters

NameTypeDescription
_actionActionTypesthe current user action
_addressaddressaddress of the account
_riskScoreFromuint8sender address risk score
_transferValuationuint128valuation of the token being transferred in USD with 18 decimals of precision

setNFTPricingAddress

-------------- Pricing Module Configurations ---------------

Sets the address of the nft pricing contract and loads the contract.

function setNFTPricingAddress(address _address) external ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_addressaddressNft Pricing Contract address.

setERC20PricingAddress

Sets the address of the erc20 pricing contract and loads the contract.

function setERC20PricingAddress(address _address) external ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_addressaddressERC20 Pricing Contract address.

getAccTotalValuation

This gets the account’s balance in dollars.

Get the account’s balance in dollars. It uses the registered tokens in the app manager.

function getAccTotalValuation(address _account, uint256 _nftValuationLimit)
    public
    view
    returns (uint256 totalValuation);

Parameters

NameTypeDescription
_accountaddressaddress to get the balance for
_nftValuationLimituint256

Returns

NameTypeDescription
totalValuationuint256of the account in dollars

_getERC20Price

check if _account is zero address. If zero address we return a valuation of zero to allow for burning tokens when rules that need valuations are active. Loop through all Nfts and ERC20s and add values to balance for account valuation Check to see if user owns the asset

This gets the token’s value in dollars.

Get the value for a specific ERC20. This is done by interacting with the pricing module

function _getERC20Price(address _tokenAddress) internal view returns (uint256);

Parameters

NameTypeDescription
_tokenAddressaddressthe address of the token

Returns

NameTypeDescription
<none>uint256price the price of 1 in dollars

_getNFTValuePerCollection

This gets the token’s value in dollars.

Get the value for a specific ERC721. This is done by interacting with the pricing module

function _getNFTValuePerCollection(address _tokenAddress, address _account, uint256 _tokenAmount)
    internal
    view
    returns (uint256 totalValueInThisContract);

Parameters

NameTypeDescription
_tokenAddressaddressthe address of the token
_accountaddressof the token holder
_tokenAmountuint256amount of NFTs from _tokenAddress contract

Returns

NameTypeDescription
totalValueInThisContractuint256in whole USD

_getNFTCollectionValue

This function gets the total token value in dollars of all tokens owned in each collection by address.

Get the total value for all tokens held by a wallet for a specific collection. This is done by interacting with the pricing module

function _getNFTCollectionValue(address _tokenAddress, uint256 _tokenAmount)
    private
    view
    returns (uint256 totalValueInThisContract);

Parameters

NameTypeDescription
_tokenAddressaddressthe address of the token
_tokenAmountuint256amount of NFTs from _tokenAddress contract

Returns

NameTypeDescription
totalValueInThisContractuint256total valuation of tokens by collection in whole USD

setAccountMaxValueByRiskScoreId

that setting a rule will automatically activate it.

Set the accountMaxValueByRiskScoreRule. Restricted to app administrators only.

function setAccountMaxValueByRiskScoreId(ActionTypes[] calldata _actions, uint32 _ruleId)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]action types in which to apply the rules
_ruleIduint32Rule Id to set

setAccountMaxValueByRiskScoreIdFull

that setting a rule will automatically activate it.

Set the accountMaxValueByRiskScoreRule. Restricted to app administrators only.

function setAccountMaxValueByRiskScoreIdFull(ActionTypes[] calldata _actions, uint32[] calldata _ruleIds)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]actions to have the rule applied to
_ruleIdsuint32[]Rule Id corresponding to the actions

clearAccountMaxValueByRiskScore

Clear the rule data structure

function clearAccountMaxValueByRiskScore() internal;

setAccountMaxValueByRiskScoreIdUpdate

that setting a rule will automatically activate it.

Set the AccountMaxValuebyRiskSCoreRuleId.

function setAccountMaxValueByRiskScoreIdUpdate(ActionTypes _action, uint32 _ruleId) internal;

Parameters

NameTypeDescription
_actionActionTypesthe action type to set the rule
_ruleIduint32Rule Id to set

activateAccountMaxValueByRiskScore

enable/disable rule. Disabling a rule will save gas on transfer transactions.

function activateAccountMaxValueByRiskScore(ActionTypes[] calldata _actions, bool _on)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]action types
_onboolboolean representing if a rule must be checked or not.

isAccountMaxValueByRiskScoreActive

Tells you if the accountMaxValueByRiskScore Rule is active or not.

function isAccountMaxValueByRiskScoreActive(ActionTypes _action) external view returns (bool);

Parameters

NameTypeDescription
_actionActionTypesthe action type

Returns

NameTypeDescription
<none>boolboolean representing if the rule is active

getAccountMaxValueByRiskScoreId

Retrieve the accountMaxValueByRiskScore rule id

function getAccountMaxValueByRiskScoreId(ActionTypes _action) external view returns (uint32);

Parameters

NameTypeDescription
_actionActionTypesaction type

Returns

NameTypeDescription
<none>uint32accountMaxValueByRiskScoreId rule id

setAccountDenyForNoAccessLevelId

that setting a rule will automatically activate it.

Set the activateAccountDenyForNoAccessLevel. Restricted to app administrators only.

function setAccountDenyForNoAccessLevelId(ActionTypes[] calldata _actions)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]action types in which to apply the rules

setAccountDenyForNoAccessLevelIdFull

that setting a rule will automatically activate it.

Set the activateAccountDenyForNoAccessLevel. Restricted to app administrators only.

function setAccountDenyForNoAccessLevelIdFull(ActionTypes[] calldata _actions)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]actions to have the rule applied to

clearAccountDenyForNoAccessLevel

Clear the rule data structure

function clearAccountDenyForNoAccessLevel() internal;

setAccountDenyForNoAccessLevelIdUpdate

that setting a rule will automatically activate it.

Set the AccountDenyForNoAccessLevelRuleId.

function setAccountDenyForNoAccessLevelIdUpdate(ActionTypes _action) internal;

Parameters

NameTypeDescription
_actionActionTypesthe action type to set the rule

activateAccountDenyForNoAccessLevelRule

enable/disable rule. Disabling a rule will save gas on transfer transactions.

function activateAccountDenyForNoAccessLevelRule(ActionTypes[] calldata _actions, bool _on)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]action types
_onboolboolean representing if a rule must be checked or not.

isAccountDenyForNoAccessLevelActive

Tells you if the AccountDenyForNoAccessLevel Rule is active or not.

function isAccountDenyForNoAccessLevelActive(ActionTypes _action) external view returns (bool);

Parameters

NameTypeDescription
_actionActionTypesthe action type

Returns

NameTypeDescription
<none>boolboolean representing if the rule is active

setAccountMaxValueByAccessLevelId

that setting a rule will automatically activate it.

Set the accountMaxValueByAccessLevelRule. Restricted to app administrators only.

function setAccountMaxValueByAccessLevelId(ActionTypes[] calldata _actions, uint32 _ruleId)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]action types in which to apply the rules
_ruleIduint32Rule Id to set

setAccountMaxValueByAccessLevelIdFull

that setting a rule will automatically activate it.

Set the accountMaxValueByAccessLevelRule. Restricted to app administrators only.

function setAccountMaxValueByAccessLevelIdFull(ActionTypes[] calldata _actions, uint32[] calldata _ruleIds)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]actions to have the rule applied to
_ruleIdsuint32[]Rule Id corresponding to the actions

clearAccountMaxValueByAccessLevel

Clear the rule data structure

function clearAccountMaxValueByAccessLevel() internal;

setAccountMaxValuebyAccessLevelIdUpdate

that setting a rule will automatically activate it.

Set the AccountMaxValuebyAccessLevelRuleId.

function setAccountMaxValuebyAccessLevelIdUpdate(ActionTypes _action, uint32 _ruleId) internal;

Parameters

NameTypeDescription
_actionActionTypesthe action type to set the rule
_ruleIduint32Rule Id to set

activateAccountMaxValueByAccessLevel

enable/disable rule. Disabling a rule will save gas on transfer transactions.

function activateAccountMaxValueByAccessLevel(ActionTypes[] calldata _actions, bool _on)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]action types
_onboolboolean representing if a rule must be checked or not.

isAccountMaxValueByAccessLevelActive

Tells you if the accountMaxValueByAccessLevel Rule is active or not.

function isAccountMaxValueByAccessLevelActive(ActionTypes _action) external view returns (bool);

Parameters

NameTypeDescription
_actionActionTypesthe action type

Returns

NameTypeDescription
<none>boolboolean representing if the rule is active

getAccountMaxValueByAccessLevelId

Retrieve the accountMaxValueByAccessLevel rule id

function getAccountMaxValueByAccessLevelId(ActionTypes _action) external view returns (uint32);

Parameters

NameTypeDescription
_actionActionTypesaction type

Returns

NameTypeDescription
<none>uint32accountMaxValueByAccessLevelId rule id

setAccountMaxValueOutByAccessLevelId

that setting a rule will automatically activate it.

Set the AccountMaxValueOutByAccessLevel. Restricted to app administrators only.

function setAccountMaxValueOutByAccessLevelId(ActionTypes[] calldata _actions, uint32 _ruleId)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]action types in which to apply the rules
_ruleIduint32Rule Id to set

setAccountMaxValueOutByAccessLevelIdFull

that setting a rule will automatically activate it.

Set the AccountMaxValueOutByAccessLevel. Restricted to app administrators only.

function setAccountMaxValueOutByAccessLevelIdFull(ActionTypes[] calldata _actions, uint32[] calldata _ruleIds)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]actions to have the rule applied to
_ruleIdsuint32[]Rule Id corresponding to the actions

clearAccountMaxValueOutByAccessLevel

Clear the rule data structure

function clearAccountMaxValueOutByAccessLevel() internal;

setAccountMaxValueOutByAccessLevelIdUpdate

that setting a rule will automatically activate it.

Set the AccountMaxValueOutByAccessLevelRuleId.

function setAccountMaxValueOutByAccessLevelIdUpdate(ActionTypes _action, uint32 _ruleId) internal;

Parameters

NameTypeDescription
_actionActionTypesthe action type to set the rule
_ruleIduint32Rule Id to set

activateAccountMaxValueOutByAccessLevel

enable/disable rule. Disabling a rule will save gas on transfer transactions.

function activateAccountMaxValueOutByAccessLevel(ActionTypes[] calldata _actions, bool _on)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]action types
_onboolboolean representing if a rule must be checked or not.

isAccountMaxValueOutByAccessLevelActive

Tells you if the AccountMaxValueOutByAccessLevel Rule is active or not.

function isAccountMaxValueOutByAccessLevelActive(ActionTypes _action) external view returns (bool);

Parameters

NameTypeDescription
_actionActionTypesthe action type

Returns

NameTypeDescription
<none>boolboolean representing if the rule is active

getAccountMaxValueOutByAccessLevelId

Retrieve the accountMaxValueOutByAccessLevel rule id

function getAccountMaxValueOutByAccessLevelId(ActionTypes _action) external view returns (uint32);

Parameters

NameTypeDescription
_actionActionTypesaction type

Returns

NameTypeDescription
<none>uint32accountMaxValueOutByAccessLevelId rule id

setAccountMaxTxValueByRiskScoreId

that setting a rule will automatically activate it.

Set the accountMaxTxValueByRiskScore. Restricted to app administrators only.

function setAccountMaxTxValueByRiskScoreId(ActionTypes[] calldata _actions, uint32 _ruleId)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]action types in which to apply the rules
_ruleIduint32Rule Id to set

setAccountMaxTxValueByRiskScoreIdFull

that setting a rule will automatically activate it.

Set the accountMaxTxValueByRiskScore. Restricted to app administrators only.

function setAccountMaxTxValueByRiskScoreIdFull(ActionTypes[] calldata _actions, uint32[] calldata _ruleIds)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]actions to have the rule applied to
_ruleIdsuint32[]Rule Id corresponding to the actions

clearAccountMaxTxValueByRiskScore

Clear the rule data structure

function clearAccountMaxTxValueByRiskScore() internal;

setAccountMaxTxValueByRiskScoreIdUpdate

that setting a rule will automatically activate it.

Set the AccountMaxTxValueByRiskScoreRuleId.

function setAccountMaxTxValueByRiskScoreIdUpdate(ActionTypes _action, uint32 _ruleId) internal;

Parameters

NameTypeDescription
_actionActionTypesthe action type to set the rule
_ruleIduint32Rule Id to set

activateAccountMaxTxValueByRiskScore

enable/disable rule. Disabling a rule will save gas on transfer transactions.

function activateAccountMaxTxValueByRiskScore(ActionTypes[] calldata _actions, bool _on)
    external
    ruleAdministratorOnly(appManagerAddress);

Parameters

NameTypeDescription
_actionsActionTypes[]action types
_onboolboolean representing if a rule must be checked or not.

isAccountMaxTxValueByRiskScoreActive

Tells you if the accountMaxTxValueByRiskScore Rule is active or not.

function isAccountMaxTxValueByRiskScoreActive(ActionTypes _action) external view returns (bool);

Parameters

NameTypeDescription
_actionActionTypesthe action type

Returns

NameTypeDescription
<none>boolboolean representing if the rule is active

getAccountMaxTxValueByRiskScoreId

Retrieve the AccountMaxTxValueByRiskScore rule id

function getAccountMaxTxValueByRiskScoreId(ActionTypes _action) external view returns (uint32);

Parameters

NameTypeDescription
_actionActionTypesaction type

Returns

NameTypeDescription
<none>uint32accountMaxTxValueByRiskScoreId rule id

activatePauseRule

This function uses the onlyOwner modifier since the appManager contract is calling this function when adding a pause rule or removing the final pause rule of the array.

enable/disable rule. Disabling a rule will save gas on transfer transactions. This function does not use ruleAdministratorOnly modifier, the onlyOwner modifier checks that the caller is the appManager contract.

function activatePauseRule(bool _on) external onlyOwner;

Parameters

NameTypeDescription
_onboolboolean representing if a rule must be checked or not.

isPauseRuleActive

Tells you if the pause rule check is active or not.

function isPauseRuleActive() external view returns (bool);

Returns

NameTypeDescription
<none>boolboolean representing if the rule is active for specified token

version

gets the version of the contract

function version() external pure returns (string memory);

Returns

NameTypeDescription
<none>stringVERSION

isContract

Check if the addresss is a contract

function isContract(address account) internal view returns (bool);

Parameters

NameTypeDescription
accountaddressaddress to check

Returns

NameTypeDescription
<none>boolbool

getRuleProcessAddress

Getter for rule processor address

function getRuleProcessAddress() external view returns (address);

Returns

NameTypeDescription
<none>addressruleProcessorAddress