> For the complete documentation index, see [llms.txt](https://developers.term.finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.term.finance/periphery-contracts/blue-sheets/solidity-api-latest/repotokenlinkedlist.sol-repotokenlinkedlist.md).

# RepoTokenLinkedList.sol#RepoTokenLinkedList

[Git Source](https://github.com/term-finance/term-finance-listing/blob/943d131dad4bf057496209b944354a91fe8dd755/src/RepoTokenLinkedList.sol)

**Inherits:** Initializable, UUPSUpgradeable, AccessControlUpgradeable, ReentrancyGuardUpgradeable, PausableUpgradeable, [RepoTokenLinkedListStorageV1](https://github.com/term-finance/term-finance-developer-docs/blob/main/src/RepoTokenLinkedList.sol/contract.RepoTokenLinkedListStorageV1.md)

A marketplace for trading repo tokens

*Implements upgradeable patterns and various security features*

## State Variables

### TERM\_CONTROLLER

```solidity
ITermController public immutable TERM_CONTROLLER;
```

### REPO\_TOKEN\_LINKED\_LIST\_EVENT\_EMITTER

```solidity
RepoTokenLinkedListEventEmitter public immutable REPO_TOKEN_LINKED_LIST_EVENT_EMITTER;
```

### ADMIN\_ROLE

```solidity
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
```

### DEVOPS\_ROLE

```solidity
bytes32 public constant DEVOPS_ROLE = keccak256("DEVOPS_ROLE");
```

### RATE\_PRECISION

```solidity
uint256 public constant RATE_PRECISION = 1e18;
```

### MAX\_MARKUP

```solidity
uint256 public constant MAX_MARKUP = 2e16;
```

### REDEMPTION\_VALUE\_PRECISION

```solidity
uint256 public constant REDEMPTION_VALUE_PRECISION = 1e18;
```

### THREESIXTY\_DAYCOUNT\_SECONDS

```solidity
uint256 public constant THREESIXTY_DAYCOUNT_SECONDS = 360 days;
```

### USDC

```solidity
address public constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
```

### WETH

```solidity
address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
```

## Functions

### constructor

Contract constructor

```solidity
constructor(address termController, address repoTokenLinkedListEventEmitter);
```

**Parameters**

| Name                              | Type      | Description                             |
| --------------------------------- | --------- | --------------------------------------- |
| `termController`                  | `address` | Address of the term controller contract |
| `repoTokenLinkedListEventEmitter` | `address` | Address of the event emitter contract   |

### initialize

Initializes the contract with admin and devops roles

*Sets up roles and initializes the contract using OpenZeppelin's upgradeable pattern*

*See: <https://docs.openzeppelin.com/contracts/4.x/upgradeable>*

```solidity
function initialize(address discountRateAdapter_, address adminWallet_, address devopsWallet_) external initializer;
```

**Parameters**

| Name                   | Type      | Description                               |
| ---------------------- | --------- | ----------------------------------------- |
| `discountRateAdapter_` | `address` | The address of the discount rate oracle   |
| `adminWallet_`         | `address` | The address to be granted the admin role  |
| `devopsWallet_`        | `address` | The address to be granted the devops role |

### onlyValidatedRepoToken

Modifier to check if a repo token is valid

```solidity
modifier onlyValidatedRepoToken(address repoToken);
```

### setRepoTokenBlacklist

Sets the blacklist status for a repo token

```solidity
function setRepoTokenBlacklist(address repoToken, bool blacklisted) external onlyRole(ADMIN_ROLE);
```

**Parameters**

| Name          | Type      | Description                   |
| ------------- | --------- | ----------------------------- |
| `repoToken`   | `address` | The address of the repo token |
| `blacklisted` | `bool`    | The blacklist status to set   |

### setDiscountRateAdapter

Sets a new discount rate adapter

```solidity
function setDiscountRateAdapter(address newAdapter) external onlyRole(ADMIN_ROLE);
```

**Parameters**

| Name         | Type      | Description                                  |
| ------------ | --------- | -------------------------------------------- |
| `newAdapter` | `address` | The address of the new discount rate adapter |

### setDiscountRateMarkup

Sets a new value for the discount rate markup

*Only callable by accounts with the ADMIN\_ROLE*

```solidity
function setDiscountRateMarkup(uint256 newMarkup) external onlyRole(ADMIN_ROLE);
```

**Parameters**

| Name        | Type      | Description                 |
| ----------- | --------- | --------------------------- |
| `newMarkup` | `uint256` | The new markup value to set |

### manageMinimumListing

```solidity
function manageMinimumListing(address token, uint256 amount) external onlyRole(ADMIN_ROLE);
```

### pause

Pauses the contract

```solidity
function pause() external onlyRole(ADMIN_ROLE);
```

### unpause

Unpauses the contract

```solidity
function unpause() external onlyRole(ADMIN_ROLE);
```

### createListing

Creates a new listing for a repo token

```solidity
function createListing(address repoToken, uint256 amount)
    external
    nonReentrant
    whenNotPaused
    onlyValidatedRepoToken(repoToken);
```

**Parameters**

| Name        | Type      | Description                           |
| ----------- | --------- | ------------------------------------- |
| `repoToken` | `address` | The address of the repo token to list |
| `amount`    | `uint256` | The amount of tokens to list          |

### purchase

Allows a user to purchase repo tokens

```solidity
function purchase(uint256 desiredAmount, address repoToken)
    external
    nonReentrant
    whenNotPaused
    onlyValidatedRepoToken(repoToken);
```

**Parameters**

| Name            | Type      | Description                               |
| --------------- | --------- | ----------------------------------------- |
| `desiredAmount` | `uint256` | The amount of tokens to purchase          |
| `repoToken`     | `address` | The address of the repo token to purchase |

### swapExactPurchaseForRepo

Allows a user to purchase repo tokens

```solidity
function swapExactPurchaseForRepo(uint256 purchaseTokenAmount, address repoToken)
    external
    nonReentrant
    whenNotPaused
    onlyValidatedRepoToken(repoToken);
```

**Parameters**

| Name                  | Type      | Description                                        |
| --------------------- | --------- | -------------------------------------------------- |
| `purchaseTokenAmount` | `uint256` | The amount of purchase tokens to spend in purchase |
| `repoToken`           | `address` | The address of the repo token to purchase          |

### \_purchase

Internal function to handle the purchase logic

```solidity
function _purchase(address repoToken, address purchaseToken, uint256 desiredAmount, uint256 pricePerToken) private;
```

**Parameters**

| Name            | Type      | Description                                |
| --------------- | --------- | ------------------------------------------ |
| `repoToken`     | `address` | The address of the repo token to purchase  |
| `purchaseToken` | `address` | The address of the token used for purchase |
| `desiredAmount` | `uint256` | The amount of tokens to purchase           |
| `pricePerToken` | `uint256` | The price per token                        |

### \_emitAndTransfer

```solidity
function _emitAndTransfer(
    uint256 currentListing,
    address purchaseToken,
    address seller,
    address token,
    uint256 purchaseAmount,
    uint256 cost
) private;
```

### cancelListing

Allows a seller to cancel their listing

```solidity
function cancelListing(uint256 listingId, bool skipRedeem) public nonReentrant whenNotPaused;
```

**Parameters**

| Name         | Type      | Description                        |
| ------------ | --------- | ---------------------------------- |
| `listingId`  | `uint256` | The ID of the listing to cancel    |
| `skipRedeem` | `bool`    | Whether to skip the redeem process |

### batchCancelListings

Repotokens can be redeemed by anyone on behalf of any third-party

```solidity
function batchCancelListings(uint256[] calldata listingIds, bool skipRedeem) external;
```

### removeListing

Internal function to remove a listing

```solidity
function removeListing(address repoToken, uint256 listingId) internal;
```

**Parameters**

| Name        | Type      | Description                     |
| ----------- | --------- | ------------------------------- |
| `repoToken` | `address` | The address of the repo token   |
| `listingId` | `uint256` | The ID of the listing to remove |

### getListing

Retrieves the details of a listing

```solidity
function getListing(uint256 listingId) external view returns (address seller, address token, uint256 amount);
```

**Parameters**

| Name        | Type      | Description                       |
| ----------- | --------- | --------------------------------- |
| `listingId` | `uint256` | The ID of the listing to retrieve |

**Returns**

| Name     | Type      | Description                         |
| -------- | --------- | ----------------------------------- |
| `seller` | `address` | The address of the seller           |
| `token`  | `address` | The address of the token being sold |
| `amount` | `uint256` | The amount of tokens being sold     |

### getTotalListings

Gets the total number of active listings

```solidity
function getTotalListings(address repoToken) external view returns (uint256);
```

**Parameters**

| Name        | Type      | Description                   |
| ----------- | --------- | ----------------------------- |
| `repoToken` | `address` | The address of the repo token |

**Returns**

| Name     | Type      | Description                  |
| -------- | --------- | ---------------------------- |
| `<none>` | `uint256` | The total number of listings |

### isRepoTokenValid

Checks if a repo token is valid

```solidity
function isRepoTokenValid(address repoToken) public view returns (bool);
```

**Parameters**

| Name        | Type      | Description                            |
| ----------- | --------- | -------------------------------------- |
| `repoToken` | `address` | The address of the repo token to check |

**Returns**

| Name     | Type   | Description                                |
| -------- | ------ | ------------------------------------------ |
| `<none>` | `bool` | bool indicating if the repo token is valid |

### \_authorizeUpgrade

Ensures only authorized addresses can upgrade the contract

*Overrides the UUPSUpgradeable \_authorizeUpgrade function to include role checks.*

*required override by the OpenZeppelin UUPS module*

```solidity
function _authorizeUpgrade(address) internal view override onlyRole(DEVOPS_ROLE);
```

**Parameters**

| Name     | Type      | Description |
| -------- | --------- | ----------- |
| `<none>` | `address` |             |
