# PrimaryLendingPlatformAtomicRepaymentCore

## Overview

#### License: MIT

```solidity
abstract contract PrimaryLendingPlatformAtomicRepaymentCore is Initializable, AccessControlUpgradeable, ReentrancyGuardUpgradeable
```

Core contract for the atomic repayment functionality for the PrimaryLendingPlatform contract.

Abstract contract that implements the atomic repayment core functionality for the PrimaryLendingPlatform contract.

## Events info

### SetExchangeAggregator

```solidity
event SetExchangeAggregator(address indexed exchangeAggregator, address indexed registryAggregator)
```

Emitted when the exchange aggregator and registry aggregator addresses are set.

Parameters:

| Name               | Type    | Description                             |
| ------------------ | ------- | --------------------------------------- |
| exchangeAggregator | address | The address of the exchange aggregator. |
| registryAggregator | address | The address of the registry aggregator. |

### SetPrimaryLendingPlatform

```solidity
event SetPrimaryLendingPlatform(address indexed newPrimaryLendingPlatform)
```

Emitted when the primary lending platform address is set.

Parameters:

| Name                      | Type    | Description                                      |
| ------------------------- | ------- | ------------------------------------------------ |
| newPrimaryLendingPlatform | address | The new address of the primary lending platform. |

### AtomicRepayment

```solidity
event AtomicRepayment(address indexed user, address indexed collateral, address indexed lendingAsset, uint256 amountSold, uint256 amountReceive)
```

Emitted when an atomic repayment is executed, where a user sells collateral to repay a loan.

Parameters:

| Name          | Type    | Description                                                           |
| ------------- | ------- | --------------------------------------------------------------------- |
| user          | address | The address of the user who executed the atomic repayment.            |
| collateral    | address | The address of the collateral asset sold by the user.                 |
| lendingAsset  | address | The address of the lending asset that was repaid.                     |
| amountSold    | uint256 | The amount of collateral sold by the user.                            |
| amountReceive | uint256 | The amount of lending asset received by the user after the repayment. |

## Constants info

### MODERATOR\_ROLE (0x797669c9)

```solidity
bytes32 constant MODERATOR_ROLE = keccak256("MODERATOR_ROLE")
```

### BUFFER\_PERCENTAGE (0x952038c2)

```solidity
uint16 constant BUFFER_PERCENTAGE = 500
```

## State variables info

### primaryLendingPlatform (0x92641a7c)

```solidity
contract IPrimaryLendingPlatform primaryLendingPlatform
```

### exchangeAggregator (0x60df4f35)

```solidity
address exchangeAggregator
```

### registryAggregator (0xf38cb29a)

```solidity
address registryAggregator
```

## Modifiers info

### onlyModerator

```solidity
modifier onlyModerator()
```

Throws if the caller is not the moderator.

### isProjectTokenListed

```solidity
modifier isProjectTokenListed(address projectToken)
```

Throws if the project token is not listed.

Parameters:

| Name         | Type    | Description                |
| ------------ | ------- | -------------------------- |
| projectToken | address | The project token address. |

## Functions info

### initialize (0xc4d66de8)

```solidity
function initialize(address pit) public initializer
```

Sets up initial roles, initializes AccessControl, and sets the provided PIT address

Parameters:

| Name | Type    | Description                                         |
| ---- | ------- | --------------------------------------------------- |
| pit  | address | The address of the PrimaryLendingPlatform contract. |

### setExchangeAggregator (0x3c4841b4)

```solidity
function setExchangeAggregator(
    address exchangeAggregatorAddress,
    address registryAggregatorAddress
) external onlyModerator
```

Updates the Exchange Aggregator contract and registry contract addresses.

Requirements:

* The caller must be the moderator.
* `exchangeAggregatorAddress` must not be the zero address.
* `registryAggregatorAddress` must be a valid Augustus contract if it is not the zero address.

Parameters:

| Name                      | Type    | Description                                          |
| ------------------------- | ------- | ---------------------------------------------------- |
| exchangeAggregatorAddress | address | The new address of the Exchange Aggregator contract. |
| registryAggregatorAddress | address | The new address of the Aggregator registry contract. |

### setPrimaryLendingPlatform (0xe801734a)

```solidity
function setPrimaryLendingPlatform(address pit) external onlyModerator
```

Sets the address of the primary lending platform contract.

Parameters:

| Name | Type    | Description                                           |
| ---- | ------- | ----------------------------------------------------- |
| pit  | address | The address of the primary lending platform contract. |

Requirements:

* `pit` cannot be the zero address. |

### getTotalOutstanding (0x00fe5da3)

```solidity
function getTotalOutstanding(
    address user,
    address projectToken,
    address lendingAsset
) public view returns (uint256 outstanding)
```

Calculates the outstanding amount (i.e., loanBody + accrual) for a given user, project token, and lending token.

Parameters:

| Name         | Type    | Description                                                    |
| ------------ | ------- | -------------------------------------------------------------- |
| user         | address | The user for which to compute the outstanding amount.          |
| projectToken | address | The project token for which to compute the outstanding amount. |
| lendingAsset | address | The lending token for which to compute the outstanding amount. |

Return values:

| Name        | Type    | Description                                                            |
| ----------- | ------- | ---------------------------------------------------------------------- |
| outstanding | uint256 | The outstanding amount for the user, project token, and lending token. |

### getRemainingDeposit (0xf8f8b436)

```solidity
function getRemainingDeposit(
    address user,
    address projectToken
) public view returns (uint256 remainingDeposit)
```

Returns the remaining deposit of a user for a specific project token.

Parameters:

| Name         | Type    | Description                       |
| ------------ | ------- | --------------------------------- |
| user         | address | The address of the user.          |
| projectToken | address | The address of the project token. |

Return values:

| Name             | Type    | Description                                              |
| ---------------- | ------- | -------------------------------------------------------- |
| remainingDeposit | uint256 | The remaining deposit of the user for the project token. |
