# Smart Contract Architecture

ModePad comprises two key smart contracts:&#x20;

* A `LaunchPadFactory` contract; and&#x20;
* A `Launchpad` contract

The former acts as a factory, deploying new instances of the latter, which represents individual launchpads.&#x20;

{% hint style="info" %}
You can find the contracts on [**ModePad's Contract GitHub Repo**](https://github.com/Demeter-Lab/Mode-Launchpad-Contracts)
{% endhint %}

Let's delve into the architecture of these contracts.

### **LaunchPadFactory Contract:**

**`constructor`:**

* **Purpose:** Initializes the contract with the address of the contract owner and registers an NFT with a specific contract.
* **Technical Details:**
  * Uses the constructor to set the owner's address.
  * Calls the `Register` contract's `register` function, associating the NFT with the owner.

**`deploy`:**

* **Purpose:** Creates a new `Launchpad` contract and initializes it with specified parameters.
* **Technical Details:**
  * Computes the bytecode, salt, and constructor parameters to determine the new contract's address.
  * Uses assembly code to deploy the contract using `create2`.
  * Initializes the newly created contract with the provided parameters.
  * Updates the `createdLaunchPools` mapping and emits the `LaunchPoolCreated` event.

**View Functions:**

1. `getPadAddress`:
   * **Purpose:** Retrieves the address of a specific launchpad.
   * **Technical Details:** Accepts a `padNumber` parameter and returns the corresponding address from the `createdLaunchPools` mapping.
2. `getNoOfLaunchPads`:
   * **Purpose:** Retrieves the total number of launchpads created.
   * **Technical Details:** Simply returns the `poolCount` variable.
3. `getPadName`:
   * **Purpose:** Retrieves the name of a specific launchpad.
   * **Technical Details:** Accepts the `padAddress` parameter and calls the `poolName` function from the `Launchpad` contract at the specified address.
4. `getPadDuration`:
   * **Purpose:** Retrieves the duration of a specific launchpad's token sale.
   * **Technical Details:** Similar to `getPadName`, it calls the `saleDurationInSeconds` function from the targeted `Launchpad` contract.
5. `getPadMaxCap` and `getPadMinCap`:
   * **Purpose:** Retrieves the maximum and minimum investment caps of a specific launchpad.
   * **Technical Details:** Similar to previous functions, these retrieve values from the targeted `Launchpad` contract.
6. `getUnsoldTokensAmount`:
   * **Purpose:** Retrieves the amount of unsold tokens for a specific launchpad.
   * **Technical Details:** Calls the `getUnsoldTokens` function from the targeted `Launchpad` contract.
7. `getUserTokenPurchase`:
   * **Purpose:** Retrieves the number of tokens purchased by a specific user for a launchpad.
   * **Technical Details:** Calls the `getUserTokenPurchase` function from the targeted `Launchpad` contract.
8. `getPadPrice`:
   * **Purpose:** Retrieves the token price for a specific launchpad.
   * **Technical Details:** Calls the `tokenPrice` function from the targeted `Launchpad` contract.
9. `getPadContractBalance`:
   * **Purpose:** Retrieves the balance of Ether held by a specific launchpad contract.
   * **Technical Details:** Calls the `getContractBalance` function from the targeted `Launchpad` contract.
10. `getPadSaleStatus`:
    * **Purpose:** Retrieves the sale status of a specific launchpad.
    * **Technical Details:** Calls the `getIsSaleActive` function from the targeted `Launchpad` contract.

### LaunchPad Contract:

**`initializer`:**

* **Purpose:** Initializes the `Launchpad` contract with essential parameters.
* **Technical Details:**
  * Validates that the contract has not been initialized before.
  * Sets contract parameters and initializes the ERC-20 token contract.

**View Functions:**

1. `isSaleDurationElapsed`:
   * **Purpose:** Checks if the sale duration has elapsed.
   * **Technical Details:** Compares the current timestamp with the sum of the sale start time and duration.
2. `getUserTokenPurchase`:
   * **Purpose:** Retrieves the number of tokens purchased by a specific user.
   * **Technical Details:** Calls the `tokensPurchased` mapping.
3. `getUnsoldTokens`:
   * **Purpose:** Retrieves the amount of unsold tokens.
   * **Technical Details:** Computes the difference between the contract's total token balance and the total tokens sold.
4. `getContractBalance`:
   * **Purpose:** Retrieves the Ether balance of the contract.
   * **Technical Details:** Calls the `balance` property of the contract.
5. `getIsSaleActive`:
   * **Purpose:** Retrieves the sale status.
   * **Technical Details:** Returns the value of `isSaleActive`.

**Sale Control Functions:**

1. `startSale` and `stopSale`:
   * **Purpose:** Start and stop the token sale.
   * **Technical Details:** Modify the `isSaleActive` status and emit events accordingly.
2. `buyTokens`:
   * **Purpose:** Allows users to purchase tokens by sending Ether.
   * **Technical Details:**
     * Validates sale and investment conditions.
     * Transfers tokens to the buyer and updates investment records.
3. `participateInGame`:
   * **Purpose:** Specialized function for participants with a gas fee consideration.
   * **Technical Details:**
     * Calculates tokens to receive considering gas fees.
     * Transfers tokens to the participant and updates investment records.

**Token Management Functions:**

1. `withdrawFunds`:
   * **Purpose:** Allows the owner to withdraw Ether funds from the contract.
   * **Technical Details:** Transfers the contract's balance to the owner.
2. `withdrawUnsoldTokens`:
   * **Purpose:** Allows the owner to withdraw unsold tokens after the sale.
   * **Technical Details:** Transfers unsold tokens to the owner.
3. `claimTokens`:
   * **Purpose:** Allows users to claim their purchased tokens after the sale.
   * **Technical Details:** Transfers purchased tokens to the caller and resets their token purchase record.

### Overall Architecture:

* **LaunchPadFactory** acts as a deployer and manager of individual **Launchpad** contracts, providing functions for deployment, retrieval of launchpad details, and management of associated contracts.
* **Launchpad** represents an individual token sale, managing its own sale lifecycle, user interactions, and token-related operations. It ensures transparent and secure token sales.

This architecture enables secure, controlled, and transparent token sales on ModePad, catering to the diverse needs of both project owners and investors.

<br>


---

# 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://mide-sofek.gitbook.io/modepad/technical-docs/smart-contract-architecture.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.
