# ERC20PaymentClientBase\_v1.sol

## ERC20PaymentClientBase\_v1

[Git Source](https://github.com/InverterNetwork/inverter-contracts/blob/649b450f02fc8b735c128ff0821467e71966c666/src/modules/logicModule/abstracts/ERC20PaymentClientBase_v1.sol)

**Inherits:** IERC20PaymentClientBase\_v1, Module\_v1

**Author:** Inverter Network

Enables modules within the Inverter Network to create and manage payment orders that can be processed by authorized payment processors, ensuring efficient and secure transactions.

*Utilizes {SafeERC20} for token operations and integrates with {IPaymentProcessor\_v1} to handle token payments. This abstract contract must be extended by modules that manage {ERC20} payment orders, supporting complex payment scenarios.*

### State Variables

#### \_orders

*The list of oustanding orders.*

*Emptied whenever orders are collected.*

```solidity
PaymentOrder[] internal _orders;
```

#### \_outstandingTokenAmounts

*The current cumulative amount of tokens outstanding.*

```solidity
mapping(address => uint) internal _outstandingTokenAmounts;
```

#### \_flagCount

*The number of payment processor flags used by this payment client.*

```solidity
uint8 internal _flagCount;
```

#### \_flags

*The payment processor flags used by this payment client.*

```solidity
bytes32 internal _flags;
```

#### \_\_gap

*Storage gap for future upgrades.*

```solidity
uint[48] private __gap;
```

### Functions

#### supportsInterface

*See {IERC165-supportsInterface}.*

```solidity
function supportsInterface(bytes4 interfaceId)
    public
    view
    virtual
    override(Module_v1)
    returns (bool);
```

#### validRecipient

*Modifier to guarantee the recipient is valid.*

```solidity
modifier validRecipient(address recipient);
```

#### validAmount

*Modifier to guarantee the amount is valid.*

```solidity
modifier validAmount(uint amount);
```

#### validPaymentOrder

*Modifier to guarantee the payment order is valid.*

```solidity
modifier validPaymentOrder(PaymentOrder memory order);
```

#### \_\_ERC20PaymentClientBase\_v1\_init

*Initializes the staking contract.*

```solidity
function __ERC20PaymentClientBase_v1_init(uint8[] memory flags_)
    internal
    onlyInitializing;
```

**Parameters**

| Name     | Type      | Description                                                                            |
| -------- | --------- | -------------------------------------------------------------------------------------- |
| `flags_` | `uint8[]` | The flags, represented as an array of uint8 containing the flag IDs between 0 and 255. |

#### \_addPaymentOrder

*Adds a new {PaymentOrder} to the list of outstanding orders.*

```solidity
function _addPaymentOrder(PaymentOrder memory order)
    internal
    virtual
    validPaymentOrder(order);
```

**Parameters**

| Name    | Type           | Description            |
| ------- | -------------- | ---------------------- |
| `order` | `PaymentOrder` | The new payment order. |

#### \_addPaymentOrders

*Adds a set of new {PaymentOrder}s to the list of outstanding orders.*

```solidity
function _addPaymentOrders(PaymentOrder[] memory orders) internal virtual;
```

**Parameters**

| Name     | Type             | Description                     |
| -------- | ---------------- | ------------------------------- |
| `orders` | `PaymentOrder[]` | The list of new Payment Orders. |

#### \_setFlags

*Sets the flags for the PaymentOrders.*

```solidity
function _setFlags(uint8 flagCount_, uint8[] memory flags_) internal virtual;
```

**Parameters**

| Name         | Type      | Description                                                                            |
| ------------ | --------- | -------------------------------------------------------------------------------------- |
| `flagCount_` | `uint8`   | The number of flags.                                                                   |
| `flags_`     | `uint8[]` | The flags, represented as an array of uint8 containing the flag IDs between 0 and 255. |

#### paymentOrders

Returns the list of outstanding payment orders.

```solidity
function paymentOrders()
    external
    view
    virtual
    returns (PaymentOrder[] memory);
```

**Returns**

| Name     | Type             | Description             |
| -------- | ---------------- | ----------------------- |
| `<none>` | `PaymentOrder[]` | list of payment orders. |

#### outstandingTokenAmount

Returns the total outstanding token payment amount.

```solidity
function outstandingTokenAmount(address token_)
    external
    view
    virtual
    returns (uint total_);
```

**Parameters**

| Name     | Type      | Description                |
| -------- | --------- | -------------------------- |
| `token_` | `address` | The token in which to pay. |

**Returns**

| Name     | Type      | Description             |
| -------- | --------- | ----------------------- |
| `total_` | `uint256` | amount of token to pay. |

#### collectPaymentOrders

Collects outstanding payment orders.

*Marks the orders as completed for the client.*

```solidity
function collectPaymentOrders()
    external
    virtual
    returns (
        PaymentOrder[] memory paymentOrders_,
        address[] memory tokens_,
        uint[] memory totalAmounts_
    );
```

**Returns**

| Name             | Type             | Description              |
| ---------------- | ---------------- | ------------------------ |
| `paymentOrders_` | `PaymentOrder[]` | list of payment orders.  |
| `tokens_`        | `address[]`      | list of token addresses. |
| `totalAmounts_`  | `uint256[]`      | list of amounts.         |

#### amountPaid

Notifies the PaymentClient, that tokens have been paid out accordingly.

*Payment Client will reduce the total amount of tokens it will stock up by the given amount.*

```solidity
function amountPaid(address token_, uint amount_) external virtual;
```

**Parameters**

| Name      | Type      | Description                               |
| --------- | --------- | ----------------------------------------- |
| `token_`  | `address` | The token in which the payment was made.  |
| `amount_` | `uint256` | amount of tokens that have been paid out. |

#### getFlags

Returns the flags used when creating payment orders in this client.

```solidity
function getFlags() public view returns (bytes32 flags_);
```

**Returns**

| Name     | Type      | Description                     |
| -------- | --------- | ------------------------------- |
| `flags_` | `bytes32` | The flags this client will use. |

#### getFlagCount

Returns the number of flags this client uses for PaymentOrders.

```solidity
function getFlagCount() public view returns (uint8 flagCount_);
```

**Returns**

| Name         | Type    | Description          |
| ------------ | ------- | -------------------- |
| `flagCount_` | `uint8` | The number of flags. |

#### \_ensureValidRecipient

*Ensures the recipient is valid.*

```solidity
function _ensureValidRecipient(address recipient) private view;
```

**Parameters**

| Name        | Type      | Description             |
| ----------- | --------- | ----------------------- |
| `recipient` | `address` | The recipient to check. |

#### \_ensureValidAmount

*Ensures the amount is valid.*

```solidity
function _ensureValidAmount(uint amount) private pure;
```

**Parameters**

| Name     | Type      | Description          |
| -------- | --------- | -------------------- |
| `amount` | `uint256` | The amount to check. |

#### \_ensureValidToken

*Ensures the token is valid.*

```solidity
function _ensureValidToken(address token) private pure;
```

**Parameters**

| Name    | Type      | Description         |
| ------- | --------- | ------------------- |
| `token` | `address` | The token to check. |

#### \_ensureValidPaymentOrder

*Ensures the payment order is valid.*

```solidity
function _ensureValidPaymentOrder(PaymentOrder memory order) private;
```

**Parameters**

| Name    | Type           | Description                 |
| ------- | -------------- | --------------------------- |
| `order` | `PaymentOrder` | The payment order to check. |

#### \_ensureTokenBalance

*Ensures `amount` of payment tokens exist in address(this). In case the token being paid out is the FundingManager token, it will trigger a callback to the FundingManager to transfer the tokens to*

```solidity
function _ensureTokenBalance(address token) internal virtual;
```

#### \_ensureTokenAllowance

*Ensures `amount` of token allowance for payment processor(s).*

```solidity
function _ensureTokenAllowance(IPaymentProcessor_v1 spender, address token)
    internal
    virtual;
```

#### \_isAuthorizedPaymentProcessor

*Returns whether address `who` is an authorized payment processor.*

```solidity
function _isAuthorizedPaymentProcessor(IPaymentProcessor_v1 who)
    internal
    view
    virtual
    returns (bool);
```

#### \_assemblePaymentConfig

*Returns the payment configuration from a list of supplied flag values. Can be overriden to add additional validation steps.*

```solidity
function _assemblePaymentConfig(bytes32[] memory flagValues_)
    internal
    view
    virtual
    returns (bytes32 flags_, bytes32[] memory data_);
```
