# PIDRegistry

### Overview

`PIDRegistry` is a core contract in the Payment ID system that manages the mapping between tokenIds and their respective owners and resolvers. It serves as the central registry for all Payment ID records, maintaining ownership information and resolver configurations for each tokenId.

### Key Concepts

| Concept               | Description                                                                       |
| --------------------- | --------------------------------------------------------------------------------- |
| Record Management     | Stores and manages owner and resolver information for each tokenId.               |
| Authorization Control | Ensures only authorized parties (token owner or PID contract) can modify records. |
| Ownership Tracking    | Maintains a record of who owns each tokenId.                                      |
| Resolver Assignment   | Associates each tokenId with a resolver contract that handles address resolution. |

### Data Structures

State Variables

| Name    | Type                       | Description                                                                  |
| ------- | -------------------------- | ---------------------------------------------------------------------------- |
| records | mapping(uint256 => Record) | Private mapping that stores owner and resolver information for each tokenId. |
| pid     | address                    | Address of the PID token contract that is authorized to modify records.      |

Custom Types

| Type   | Structure                          | Description                                                        |
| ------ | ---------------------------------- | ------------------------------------------------------------------ |
| Record | {address owner; address resolver;} | Structure containing owner and resolver information for a tokenId. |

### Functions

Constructor

```solidity
constructor(address _pid) public
```

| Input Parameter | Type    | Description                        |
| --------------- | ------- | ---------------------------------- |
| \_pid           | address | Address of the PID token contract. |

| Reverts                          | Description                                   |
| -------------------------------- | --------------------------------------------- |
| PIDRegistry\_\_InvalidPIDAddress | Thrown when the provided PID address is zero. |

Record Management

setRecord

```solidity
function setRecord(uint256 _tokenId, address _owner, address _resolver) external
```

| Input Parameter | Type    | Description                            |
| --------------- | ------- | -------------------------------------- |
| \_tokenId       | uint256 | The token ID to update the record for. |
| \_owner         | address | The new owner address.                 |
| \_resolver      | address | The new resolver address.              |

setResolver

```solidity
function setResolver(uint256 _tokenId, address _resolver) public authorised(_tokenId)
```

| Input Parameter | Type    | Description                              |
| --------------- | ------- | ---------------------------------------- |
| \_tokenId       | uint256 | The token ID to update the resolver for. |
| \_resolver      | address | The new resolver address.                |

| Reverts                      | Description                                                 |
| ---------------------------- | ----------------------------------------------------------- |
| PIDRegistry\_\_NotAuthorized | Thrown when caller is not the record owner or PID contract. |

setOwner

```solidity
function setOwner(uint256 _tokenId, address _owner) public authorised(_tokenId)
```

| Input Parameter | Type    | Description                           |
| --------------- | ------- | ------------------------------------- |
| \_tokenId       | uint256 | The token ID to update the owner for. |
| \_owner         | address | The new owner address.                |

| Reverts                      | Description                                                 |
| ---------------------------- | ----------------------------------------------------------- |
| PIDRegistry\_\_NotAuthorized | Thrown when caller is not the record owner or PID contract. |

Getter Functions

owner

```solidity
function owner(uint256 tokenId) external view returns (address)
```

| Input Parameter | Type    | Description                        |
| --------------- | ------- | ---------------------------------- |
| tokenId         | uint256 | The token ID to get the owner for. |

| Returns | Description                         |
| ------- | ----------------------------------- |
| address | The owner of the specified tokenId. |

resolver

```solidity
function resolver(uint256 tokenId) external view returns (address)
```

| Input Parameter | Type    | Description                           |
| --------------- | ------- | ------------------------------------- |
| tokenId         | uint256 | The token ID to get the resolver for. |

| Returns | Description                                     |
| ------- | ----------------------------------------------- |
| address | The resolver address for the specified tokenId. |

recordExists

```solidity
function recordExists(uint256 tokenId) external view returns (bool)
```

| Input Parameter | Type    | Description                          |
| --------------- | ------- | ------------------------------------ |
| tokenId         | uint256 | The token ID to check existence for. |

| Returns | Description                                                   |
| ------- | ------------------------------------------------------------- |
| bool    | `true` if a record exists for the tokenId, `false` otherwise. |

### Events

| Event       | Parameters                                  | Description                                         |
| ----------- | ------------------------------------------- | --------------------------------------------------- |
| NewResolver | (uint256 indexed tokenId, address resolver) | Emitted when a resolver is set for a tokenId.       |
| Transfer    | (uint256 indexed tokenId, address owner)    | Emitted when ownership of a tokenId is transferred. |

### Errors

| Error                            | Description                                                        |
| -------------------------------- | ------------------------------------------------------------------ |
| PIDRegistry\_\_NotAuthorized     | Thrown when a caller attempts to modify a record they don't own.   |
| PIDRegistry\_\_InvalidPIDAddress | Thrown when initializing with a zero address for the PID contract. |
