# Dealer Registry

## Overview

The Dealer Registry (<mark style="color:red;">`DealerRegistry.sol`</mark>) is used to store dealer information. It's also extended to include some basic functionality, like creating dealers and adding them to instruments. Like most registries, it makes use of the eternal storage pattern to store information, and since it is a user registry, inherits the base user registry. In this case the Dealer Registry uses the dealer's Ids to store their associated data.

## Dealer

Dealers have the main purpose of managing investors, granting them access to instruments, and keeping their information up to date. They can be onboarded by registering them in the KAIO system.

### Dealer Creation

* **Function**: `addDealer(bytes32 _senderRole, bytes32 _dealerId, address _wallet)`
* **Purpose**: Creates a new Dealer in the KAIO system.
* **Parameters**:
  * `bytes32 _senderRole`: Role of the sender.
  * `bytes32 _dealerId`: Id of the dealer to create.
  * `address _wallet`: Wallet associated to be associated with the dealer.
* **Validation Checks**:
  * Checks the dealer Id is not 0.
  * Ensures the caller has the proper role.
  * Makes sure the dealer does not already exist.
* **Behavior**:
  * Creates the new dealer, with the associated wallet.
  * Emits a `NewDealerAdded` event, signaling the dealer has been created.
* **Example**:

  ```solidity
  addDealer(0xAdminRole, 0xDealerId, 0xDealerAddress);
  ```

## Instrument Access

Fund admins have the ability to decide which dealers can access their instrument. When a dealer is allowed into an instrument, it also allows all the dealer's investors in the instrument, with the condition they pass all required rules for it.

### Allowing Dealer

* **Function**: `allowDealer(bytes32 _senderRole, bytes32 _dealerId, bytes32 _instrumentId, bool _allowed)`
* **Purpose**: Allows or un-allows the dealer from accessing an instrument.
* **Parameters**:
  * `bytes32 _senderRole`: Role of the sender.
  * `bytes32 _dealerId`: Id of the dealer.
  * `bytes32 _instrumentId`: Id of instrument.
  * `bool _allowed`: Indicates if the dealer should be allowed or not.
* **Validation Checks**:
  * Ensures the caller has the proper role.
* **Behavior**:
  * Allows or un-allows the dealer for the instrument.
  * Emits a `dealerAllowedUpdated` event, signaling access to the instrument for the dealer has changed.
* **Example**:

  ```solidity
  allowDealer(0xAdminRole, 0xDealerId, 0xInstrumentId, true);
  ```

### Allowing Dealer (Batch)

* **Function**: `allowDealerBatch( bytes32 _senderRole, bytes32[] _dealerIds, bytes32[] _instrumentIds, bool[] _allowed )`
* **Purpose**: Batch function for allowing or un-allowing dealers to access instruments. Can be used with one dealer for multiple instruments, with multiple dealers for a single instrument, or with multiple dealers for multiple instruments.
* **Parameters**:
  * `bytes32 _senderRole`: Role of the sender.
  * `bytes32[] _dealerIds`: Ids of the dealers.
  * `bytes32[] _instrumentIds`: Ids of instruments.
  * `bool[] _allowed`: Indicates if the dealers should be allowed or not.
* **Validation Checks**:
  * Ensures the caller has the proper role.
  * Verifies the arrays all have the same length.
* **Behavior**:
  * Allows or un-allows the dealers for the instruments.
  * Emits `dealerAllowedUpdated` events, signaling access to the instruments for the dealers has changed.
* **Example**:

  ```solidity
  allowDealerBatch(0xAdminRole, [0xDealer1Id, 0xDealer1Id, 0xDealer2Id], [0xInstrument1Id, 0xInstrument2Id, 0xInstrument1Id], [true, true, true]);
  ```

### Getting Dealer Access

* **Function**: `isDealerAllowed(bytes32 _dealerId, bytes32 _instrumentId)`
* **Purpose**: Returns if a dealer is allowed access into an instrument.
* **Parameters**:
  * `bytes32 _dealerId`: Id of the dealer.
  * `bytes32 _instrumentId`: Id of the instrument.
* **Return Values:**
  * `bool`: Signals if the dealer is allowed.
* **Example**:

  ```solidity
  isDealerAllowed(0xDealerId, 0xInstrumentId);
  ```


---

# 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.kaio.xyz/how-kaio-works/smart-contracts/registries/dealer-registry.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.
