# Keeper contract architecture

### MarginlyKeeper

Contract helper for Marginly position liquidators.

*It makes liquidations utilizing AAVE flashloans*

#### Profit

```solidity
event Profit(address liquidatedPosition, address token, uint256 amount)
```

*Emitted when liquidation occurs*

**Parameters**

| Name               | Type    | Description         |
| ------------------ | ------- | ------------------- |
| liquidatedPosition | address | liquidated position |
| token              | address | profit token        |
| amount             | uint256 | profit amount       |

#### LiquidationParams

```solidity
struct LiquidationParams {
  address marginlyPool;
  address positionToLiquidate;
  address liquidator;
  uint256 minProfit;
}
```

#### ADDRESSES\_PROVIDER

```solidity
contract IPoolAddressesProvider ADDRESSES_PROVIDER
```

#### POOL

```solidity
contract IPool POOL
```

#### constructor

```solidity
constructor(address addressesProvider) public
```

#### flashLoan

```solidity
function flashLoan(address asset, uint256 amount, uint16 referralCode, address marginlyPool, address positionToLiquidate, uint256 minProfit) external
```

Takes a simple flashloan in AAVE v3 protocol to liquidate a position in Marginly

**Parameters**

| Name                | Type    | Description                                       |
| ------------------- | ------- | ------------------------------------------------- |
| asset               | address | borrow asset                                      |
| amount              | uint256 | borrow amount                                     |
| referralCode        | uint16  | referral code to get rewards in AAVE              |
| marginlyPool        | address | address of marginly pool                          |
| positionToLiquidate | address | address of liquidatable position in Marginly pool |
| minProfit           | uint256 | amount of minimum profit worth in borrow asset    |

#### executeOperation

```solidity
function executeOperation(address asset, uint256 amount, uint256 premium, address initiator, bytes data) external returns (bool)
```

Executes an operation after receiving the flash-borrowed asset

*Ensure that the contract can return the debt + premium, e.g., has enough funds to repay and has approved the Pool to pull the total amount*

**Parameters**

| Name      | Type    | Description                             |
| --------- | ------- | --------------------------------------- |
| asset     | address | The address of the flash-borrowed asset |
| amount    | uint256 | The amount of the flash-borrowed asset  |
| premium   | uint256 | The fee of the flash-borrowed asset     |
| initiator | address | The address of the flashloan initiator  |
| data      | bytes   |                                         |

**Return Values**

| Name | Type | Description                                                      |
| ---- | ---- | ---------------------------------------------------------------- |
| \[0] | bool | True if the execution of the operation succeeds, false otherwise |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.marginly.com/risk-management/keeper-contract-architecture.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
