# Smart Contract

The `UniswapFirstBuy` contract is a pivotal part of the 0xFair Lab ecosystem, designed to facilitate fair and secure token launches. This contract, being an `Ownable` contract, grants exclusive control to the owner (usually the deploying entity) over certain functions, enhancing security and governance.

Mainnet smart contract is [deployed here](https://etherscan.io/address/0x749d8582d569e62053f69588c998368465f37046).

### Key Features of the UniswapFirstBuy Contract

1. **Contribution and Token Tracking:**
   * `totalEthContributed`: Tracks the total ETH contributed by participants.
   * `totalTokensBought`: Records the total number of tokens bought through the contract.
   * `maxContribution`: Limits the maximum ETH contribution, ensuring fair participation and preventing large-scale whale manipulation.
2. **Allowlist Management:**
   * `firstBuyAllowlist`: Holds the address of the allowlist contract, ensuring that only pre-approved participants can contribute.
3. **Token Integration:**
   * `tokenAddress` and `token`: These represent the address and interface of the token being launched, integrating the token directly with the contract.
4. **Launch State Controls:**
   * `isOpen`: Indicates if the contract is open for contributions.
   * `isLiquidityAdded`: Ensures that the launch process can only be executed once.
5. **Uniswap Integration:**
   * `uniswapV2Router`: An immutable instance of the Uniswap V2 Router, enabling the contract to interact with Uniswap for token swaps.
6. **Contribution Tracking:**
   * `ethContributions`: A mapping to track the ETH contributed by each address.

#### Contract Workflow

1. **Initialization:**
   * The contract is deployed with the Uniswap V2 Router address, establishing the connection to Uniswap.
2. **Configuration:**
   * The contract owner sets the token address, maximum contribution limit, and the allowlist address.
3. **Token Launch:**
   * The `launchToken` function is called by the owner, adding liquidity to Uniswap and buying tokens with the accumulated ETH contributions.
   * This function turns off further contributions by setting `isOpen` to false and marks `isLiquidityAdded` as true.
4. **Contributions:**
   * Participants can contribute ETH by sending it directly to the contract.
   * The contract checks for allowlist inclusion, contribution limits, and open state before accepting ETH.
5. **Token Purchase:**
   * `buyTokensWithEth` is triggered internally, using ETH to buy tokens on Uniswap.
   * The contract calculates the new total tokens bought and updates the balance.
6. **Token Withdrawal:**
   * Participants can withdraw their tokens through `withdrawTokens`.
   * The contract calculates the token amount based on the participant’s ETH contribution and sends the tokens to them.
7. **Token Distribution Calculation:**
   * `calculateTokenAmount` determines the number of tokens a user can withdraw, based on their contribution and the total tokens bought.

### Conclusion

The `UniswapFirstBuy` contract is meticulously designed to ensure a fair, transparent, and secure token launch experience. By automating critical processes and enforcing strict guidelines, it plays a crucial role in upholding the integrity and fairness of every launch on 0xFair Lab.
