> ## Documentation Index
> Fetch the complete documentation index at: https://thrackle.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Token Min Tx Size

export const RuleApplicabilityLegend = ({tokens, actions}) => {
  return <AccordionGroup>
      <Accordion title="Applies to Tokens" icon="sliders-up">
        <CardGroup cols={4}>
          <Card title="ERC-20" icon={tokens.erc20 ? `square-check` : `square-xmark`} iconType="solid" color={tokens.erc20 ? `#57be39` : `gray`} horizontal />
          <Card title="ERC-721" icon={tokens.erc721 ? `square-check` : `square-xmark`} iconType="solid" color="#57be39" horizontal />
        </CardGroup>
      </Accordion>
      <Accordion title="Applies to Actions" icon="sliders">
        <CardGroup cols={4}>
          <Card title="MINT" icon={actions.mint ? `square-check` : `square-xmark`} iconType="solid" color={actions.mint ? `#57be39` : `gray`} horizontal />
          <Card title="BURN" icon={actions.burn ? `square-check` : `square-xmark`} iconType="solid" color={actions.burn ? `#57be39` : `gray`} horizontal />
          <Card title="BUY" icon={actions.buy ? `square-check` : `square-xmark`} iconType="solid" color={actions.buy ? `#57be39` : `gray`} horizontal />
          <Card title="SELL" icon={actions.sell ? `square-check` : `square-xmark`} iconType="solid" color={actions.sell ? `#57be39` : `gray`} horizontal />
        </CardGroup>
        <Card title="TRANSFER (Peer to Peer)" icon={actions.transfer ? `square-check` : `square-xmark`} iconType="solid" color={actions.transfer ? `#57be39` : `gray`} horizontal />
      </Accordion>
    </AccordionGroup>;
};

## Purpose

The purpose of the token-min-tx-size rule is to prevent micro-trades or dust trades within an ecosystem. A developer can set the minimum number of tokens required per transfer to prevent these types of trades.

<RuleApplicabilityLegend tokens={{ erc20: true }} actions={{ mint: true, burn: true, buy: true, sell: true, transfer: true }} />

## Scope

This rule works at a token level. It must be activated and configured for each desired token in the corresponding token handler.

## Data Structure

A token-min-tx-size rule is composed of 1 component:

* **minSize** (uint256): minimum number of tokens that must be transferred for each transaction.

```c
    /// ******** Token Minimum Transfer Rules ********
    struct TokenMinTxSize {
        uint256 minSize;
    }
```

###### *see [RuleDataInterfaces](/v1/reference/protocol/economic/ruleProcessor/RuleDataInterfaces.sol/interface.IApplicationRules)*

The token-min-tx-size rules are stored in a mapping indexed by ruleId(uint32) in order of creation:

```c
   /// ******** Minimum Transaction ********
   struct TokenMinTxSizeS {
       mapping(uint32 => INonTaggedRules.TokenMinTxSize) tokenMinTxSizeRules;
       uint32 tokenMinTxSizeIndex; /// increments every time someone adds a rule
   }
```

###### *see [IRuleStorage](/v1/reference/protocol/economic/ruleProcessor/IRuleStorage.sol/interface.IRuleStorage)*

## Configuration and Enabling/Disabling

* This rule can only be configured in the protocol by a **rule administrator**.
* This rule can only be set in the asset handler by a **rule administrator**.
* This rule can only be activated/deactivated in the asset handler by a **rule administrator**.
* This rule can only be updated in the asset handler by a **rule administrator**.

## Rule Evaluation

The rule will be evaluated with the following logic:

1. The handler determines if the rule is active from the supplied action. If not, processing does not continue past this step.
2. The processor receives the ID of the token-min-tx-size rule set in the token handler.
3. The processor receives the `amount` of tokens from the handler.
4. The processor evaluates the `amount` against the rule `minSize` and reverts if the `amount` less than the rule minimum.
5. If it's a non-custodial style [Buy](/v1/rules/action-types#buy)
   1. When the [Sell](/v1/rules/action-types#sell) action is also active, checks steps 1-4 for from address.
6. If it's a non-custodial style [Sell](/v1/rules/action-types#sell)
   1. When the [Buy](/v1/rules/action-types#buy) action is also active, checks steps 1-4 for to address.

**The list of available actions rules can be applied to can be found at [ACTION\_TYPES](/v1/rules/action-types)**

###### *see [ERC20RuleProcessorFacet](/v1/reference/protocol/economic/ruleProcessor/ERC20RuleProcessorFacet.sol/contract.ERC20RuleProcessorFacet) -> checkTokenMinTxSize*

## Evaluation Exceptions

* This rule doesn't apply when a **treasuryAccount** address is in either the *from* or the *to* side of the transaction. This doesn't necessarily mean that if an treasury account is the one executing the transaction it will bypass the rule, unless the aforementioned condition is true.

### Revert Message

The rule processor will revert with the following error if the rule check fails:

```
error UnderMinTxSize();
```

The selector for this error is `0x7a78c901`.

## Create Function

Adding a token-min-tx-size rule is done through the function:

```c
function addTokenMinTxSize(
    address _appManagerAddr,
    uint256 _minSize
) external ruleAdministratorOnly(_appManagerAddr) returns (uint32);
```

###### *see [RuleDataFacet](/v1/reference/protocol/economic/ruleProcessor/RuleDataFacet.sol/contract.RuleDataFacet)*

The add function will return the protocol ID of the rule.

### Parameters:

* **\_appManagerAddr** (address): The address of the application manager to verify that the caller has Rule administrator privileges.
* **\_minSize** (uint256): Minimum number of tokens for the transaction.

### Parameter Optionality:

There is no parameter optionality for this rule.

### Parameter Validation:

The following validation will be carried out by the add function in order to ensure that these parameters are valid and make sense:

* `_appManagerAddr` is not the zero address.
* `_minSize` is greater than 0.

###### *see [RuleDataFacet](/v1/reference/protocol/economic/ruleProcessor/RuleDataFacet.sol/contract.RuleDataFacet)*

## Other Functions:

* In Protocol [Rule Processor](/v1/reference/protocol/economic/ruleProcessor/ERC20RuleProcessorFacet.sol/contract.ERC20RuleProcessorFacet):
  * Function to get a rule by its ID:
    ```c
    function getTokenMinTxSize(
                uint32 _index
            )
            external
            view
            returns
            (NonTaggedRules.TokenMinTxSize memory);
    ```
  * Function to get current amount of rules in the protocol:
    ```c
    function getTotalTokenMinTxSize() public view returns (uint32);
    ```
* In Protocol [Rule Processor](/v1/reference/protocol/economic/ruleProcessor/ERC20RuleProcessorFacet.sol/contract.ERC20RuleProcessorFacet):
  * Function that evaluates the rule:
    ```c
    function checkTokenMinTxSize(
                uint32 _ruleId,
                uint256 _amountToTransfer
            )
            external
            view;
    ```
* in Asset Handler:
  * Function to set and activate at the same time the rule for the supplied actions in an asset handler:
    ```c
    function setTokenMinTxSizeId(ActionTypes[] calldata _actions, uint32 _ruleId) external ruleAdministratorOnly(appManagerAddress);
    ```
  * Function to activate/deactivate the rule for the supplied actions actions in an asset handler:
    ```c
    function activateTokenMinTxSize(ActionTypes[] calldata _action, bool _on) external ruleAdministratorOnly(appManagerAddress);
    ```
  * Function to know the activation state of the rule for the supplied action in an asset handler:
    ```c
    function isTokenMinTxSizeActive(ActionTypes _action) external view returns (bool);
    ```
  * Function to get the rule Id for the supplied action from an asset handler:
    ```c
    function getTokenMinTxSizeId(ActionTypes _action) external view returns (uint32);
    ```

## Return Data

This rule does not return any data.

## Data Recorded

This rule does not require any data to be recorded.

## Events

* **event AD1467\_ProtocolRuleCreated(bytes32 indexed ruleType, uint32 indexed ruleId, bytes32\[] extraTags)**:

  * Emitted when: the rule has been created in the protocol.
  * Parameters:
    * ruleType: "TOKEN\_MIN\_TX\_SIZE".
    * ruleId: the index of the rule created in the protocol by rule type.
    * extraTags: an empty array.

* **event AD1467\_ApplicationHandlerActionApplied(bytes32 indexed ruleType, ActionTypes action, uint32 indexed ruleId)**:

  * Emitted when: rule has been applied in an asset handler.
  * parameters:
    * ruleType: "TOKEN\_MIN\_TX\_SIZE".
    * action: the protocol action the rule is being applied to.
    * ruleId: the index of the rule created in the protocol by rule type.

* **event AD1467\_ApplicationHandlerActionActivated(bytes32 indexed ruleType, ActionTypes actions, uint256 indexed ruleId)**
  * Emitted when: A Token Min Transaction Size rule has been activated in an asset handler:
  * Parameters:
    * ruleType: "TOKEN\_MIN\_TX\_SIZE".
    * actions: the protocol actions for which the rule is being activated.
    * ruleId: a placeholder of 0 will be passed for ruleId

* **event AD1467\_ApplicationHandlerActionDeactivated(bytes32 indexed ruleType, ActionTypes actions, uint256 indexed ruleId)**
  * Emitted when: A Token Min Transaction Size rule has been deactivated in an asset handler:
  * Parameters:
    * ruleType: "TOKEN\_MIN\_TX\_SIZE".
    * actions: the protocol actions for which the rule is being deactivated.
    * ruleId: a placeholder of 0 will be passed for ruleId

## Dependencies

* This rule has no dependencies.
