> For the complete documentation index, see [llms.txt](https://docs.untitledbank.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.untitledbank.co/developer-docs/how-to-create-a-market.md).

# How to create a market

To create a new market in the Bank contract, you need to call the `createMarket` function. This function requires specific parameters and a transaction fee. Follow these steps to create a market:

### Function Signature

{% code overflow="wrap" fullWidth="false" %}

```solidity
function createMarket(MarketConfigs memory marketConfigs) external payable returns (uint256)
```

{% endcode %}

### Parameters

The `createMarket` function takes a single parameter of type `MarketConfigs`, which is a struct containing the following fields:

<table><thead><tr><th width="165">Field</th><th width="96">Type</th><th>Description</th></tr></thead><tbody><tr><td>loanToken</td><td>address</td><td>The address of the ERC20 token used for loans in this market</td></tr><tr><td>collateralToken</td><td>address</td><td>The address of the ERC20 token used as collateral in this market</td></tr><tr><td>oracle</td><td>address</td><td>The address of the price oracle contract for this market</td></tr><tr><td>irm</td><td>address</td><td>The address of the Interest Rate Model contract for this market</td></tr><tr><td>lltv</td><td>uint256</td><td>The Liquidation Loan-to-Value ratio for this market (in WAD, e.g., 0.8 * 1e18 for 80%)</td></tr></tbody></table>

### Market Creation Fee

The `createMarket` function is `payable`, which means you need to send ETH along with the transaction. The required amount is stored in the `marketCreationFee` variable of the Bank contract. Make sure to include this fee when calling the function.

### Steps to Create a Market

1. Prepare the `MarketConfigs` struct with the necessary parameters.
2. Determine the current `marketCreationFee` by calling the `marketCreationFee()` function on the Bank contract.
3. Call the `createMarket` function with the prepared `MarketConfigs` and include the `marketCreationFee` as the transaction value.

### Example (using ethers.js)

```typescript
const Bank = // ... initialize Bank contract instance

// Prepare MarketConfigs
const marketConfigs = {
  loanToken: "0x...",       // Address of the loan token
  collateralToken: "0x...", // Address of the collateral token
  oracle: "0x...",          // Address of the price oracle
  irm: "0x...",             // Address of the Interest Rate Model
  lltv: ethers.utils.parseUnits("0.8", 18) // 80% LLTV
}

// Get the market creation fee
const marketCreationFee = await Bank.marketCreationFee()

// Create the market
const tx = await Bank.createMarket(marketConfigs, { value: marketCreationFee })
const receipt = await tx.wait()

// Get the new market ID from the event
const event = receipt.events.find(e => e.event === 'CreateMarket')
const newMarketId = event.args.id
console.log(`New market created with ID: ${newMarketId}`)
```

### Important Notes

* Ensure that the oracle and IRM addresses are valid and compatible with the Bank contract.
* The IRM (Interest Rate Model) must be registered with the Bank contract before creating a market. Use the `registerIrm` function to register a new IRM if needed.
* The LLTV (Liquidation Loan-to-Value) ratio should be set carefully as it affects the risk parameters of the market.
* Make sure you have sufficient ETH to cover the `marketCreationFee`.

After successfully creating a market, you can use the returned market ID for other operations such as supplying assets, borrowing, or managing collateral within that specific market.
