> For the complete documentation index, see [llms.txt](https://bitsmiley.gitbook.io/bitsmiley-native-btc-backed-stablecoin/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bitsmiley.gitbook.io/bitsmiley-native-btc-backed-stablecoin/core-start-contracts/peg-stability-module.md).

# Peg Stability Module

### **Introduction**

PSM stands for **Peg Stability Module**. It is a modular tool designed to stabilize the value of BITUSD by facilitating direct exchanges between BITUSD and its pegged assets (e.g., USD-backed stablecoins like USDC or fiat-like assets).PSMs play a vital role in maintaining the effectiveness of BITUSDs by addressing the following issues:

1. **Price Stability:** PSMs ensure that BITUSD prices remain closely pegged to their target value, even during periods of market volatility.
2. **Liquidity Assurance:** By providing a direct and efficient exchange mechanism, PSMs increase confidence in the BITUSD and prevent liquidity crises.
3. **Reduced Arbitrage Opportunities:** PSMs limit extreme price deviations and reduce the profitability of speculative trading that destabilizes BITUSD prices.
4. **Flexibility in Asset Management:** PSMs allow protocols to diversify or switch between pegged assets, enhancing the scalability and resilience of the system.

***

### **How Does PSM Work?**

The PSM operates by enabling predictable, low-slippage conversions between BITUSD and its pegged assets. Its functionality can be broken down into three core components:

#### 1. **Fixed Exchange Ratio**

* **When the BITUSD price rises above $1 (e.g., $1.01):** Users can deposit pegged assets (like USDC) into the system to mint BITUSDs, increasing the supply and bringing the price back down.
* **When the BITUSD price falls below $1 (e.g., $0.99):** Users can redeem BITUSDs for pegged assets, reducing the supply and pushing the price back up.

#### 2. **Liquidity Management**

The PSM maintains a reserve of pegged assets and BITUSDs in its treasury to ensure smooth exchanges and adequate liquidity for all participants.

#### 3. **Fee Mechanism**

To control excessive arbitrage and incentivize stability, the PSM may apply small conversion fees:

* **Minting fees**: Charged when pegged assets are exchanged for BITUSDs.
* **Redemption fees**: Charged when BITUSDs are exchanged for pegged assets.

***

#### **Implementation of Sui Blockchain**

On the Sui blockchain, a Peg Stability Module for BitUSD and USDC/USDT can be deployed using smart contracts. Below is a detailed explanation of its use:

**Contract Address**

* **PSM ObjectId:**
  * testnet: `0x3db997c68d87b0405c20c04c56a887837d4115e261d265e83644faa988e59012`

**Key Interfaces**

1. **Deposit X and Y Tokens:**
   1. **Function:**
   2. ```java
      public fun deposit<X, Y>(
          self: &mut Psm,
          x_coin: &mut Coin<X>,
          x_amount: u64,
          y_coin: &mut Coin<Y>,
          y_amount: u64,
          clock: &Clock,
          ctx: &mut TxContext,
      )
      ```
   3. **Description:** This function enables users to deposit tokens of type X and Y into the Psm. It mints BitUSD at a 1:1 ratio based on the deposited amounts of X and Y. It takes mutable references to `Coin<X>` and `Coin<Y>` objects representing the tokens to be deposited, along with their respective amounts `x_amount` and `y_amount`. The `clock` and `ctx` parameters are used for time-related and transaction context information.
   4. **Key Inputs:**
      * `x_coin`: A mutable reference to the `Coin<X>` object that holds the tokens of type X to be deposited.
      * `x_amount`: The amount of X tokens to deposit, represented as a non-negative integer value (u64). It should not exceed the available balance of the `x_coin`.
      * `y_coin`: A mutable reference to the `Coin<Y>` object that holds the tokens of type Y to be deposited.
      * `y_amount`: The amount of Y tokens to deposit, represented as a non-negative integer value (u64). It should not exceed the available balance of the `y_coin`.
      * `clock`: A reference to the `Clock` object, which may be used for time-dependent operations or checks.
      * `ctx`: A mutable reference to the `TxContext` object, which contains transaction context information.
2. **Withdraw X and Y Tokens:**
   1. **Function:**
   2. ```java
      public fun withdraw<X, Y>(
          self: &mut Psm,
          lp_amount: u256,
          clock: &Clock,
          ctx: &mut TxContext
      ): (u64, u64)
      ```
   3. **Description:** This function allows users to withdraw LP tokens from the Psm. It takes the `lp_amount` of LP tokens as input and returns the withdrawn amounts of tokens of type X and Y. The function uses the `clock` and `ctx` parameters for time and transaction context.
   4. **Key Inputs:**
      * `lp_amount`: The amount of LP tokens to withdraw, represented as a non-negative integer value (u256). It should not exceed the user's available LP token balance.
      * `clock`: A reference to the `Clock` object, which may be used for time-dependent operations or checks.
      * `ctx`: A mutable reference to the `TxContext` object, which contains transaction context information.
   5. **Outputs:**
      * `x_amount`: The amount of X tokens withdrawn, represented as a non-negative integer value (u64). The value returned depends on the internal logic of the Psm and the `lp_amount` provided.
      * `y_amount`: The amount of Y tokens withdrawn, represented as a non-negative integer value (u64). The value returned depends on the internal logic of the Psm and the `lp_amount` provided.
3. **Swap X tokens for Y tokens:**
   1. **Function:**
   2. ```java
      public fun swap<X, Y>(
          self: &mut Psm,
          x: &mut Coin<X>,
          x_amount: u64,
          clock: &Clock,
          ctx: &mut TxContext
      ): u64
      ```
   3. **Description:** This function facilitates the swapping of tokens of type X for tokens of type Y. It takes a mutable reference to the `Coin<X>` object and the `x_amount` of X tokens to swap. The function uses the `clock` and `ctx` parameters for time and transaction context. It returns the amount of Y tokens obtained after the swap.
   4. **Key Inputs:**
      * `x`: A mutable reference to the `Coin<X>` object that holds the tokens of type X to be swapped.
      * `x_amount`: The amount of X tokens to swap, represented as a non-negative integer value (u64). It should not exceed the available balance of the `x` coin.
      * `clock`: A reference to the `Clock` object, which may be used for time-dependent operations or checks.
      * `ctx`: A mutable reference to the `TxContext` object, which contains transaction context information.
   5. **Outputs:**
      * `y_amount`: The amount of Y tokens obtained after the swap, represented as a non-negative integer value (u64). The value depends on the internal swap rate and logic of the Psm.

***

### **Advantages of PSM**

1. **Efficient Price Stabilization:** Rapidly stabilizes BITUSD prices during market turbulence.
2. **Low Transaction Costs:** Provides a low-slippage and cost-effective way for users to exchange BITUSDs and pegged assets.
3. **Increased Trust:** Enhances confidence in the BITUSD by ensuring its value remains consistent with the target peg.
4. **Adaptability:** Offers flexibility in selecting and switching between various pegged assets.

***

### **Challenges of PSM**

1. **Capital Inefficiency:** Maintaining large reserves of pegged assets can tie up capital and reduce the system's overall efficiency.

***

### **Conclusion**

The Peg Stability Module is a cornerstone of BITUSD ecosystems, enabling them to maintain price stability and liquidity in the face of market fluctuations. While it offers significant advantages, including enhanced user trust and system efficiency, it also poses challenges related to centralization and capital utilization. As blockchain technology evolves, PSMs will likely continue to play a crucial role in shaping the future of decentralized finance, ensuring stability and reliability across global financial systems.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://bitsmiley.gitbook.io/bitsmiley-native-btc-backed-stablecoin/core-start-contracts/peg-stability-module.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
