# DegenDuel Functions

**constructor()**

* Visibility: Public (called on deployment).
* Parameters: None.
* Description: Sets up the contract with fixed addresses:
  * Uniswap V2 router: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D (Ethereum mainnet; adjust for other networks).
  * Marketing wallet: `msg.sender`.
  * Oracle addresses: Assets 1-5 mapped to 0x... (will be updated for production).
  * USDC: 0x... (placeholder; replaced with actual USDC address).
  * $DUEL: Same placeholder address (update to deployed DuelToken address).
* Requirements: None.
* Effects: Ownership to `msg.sender`. Immutables set.

**createDuel(uint256 \_asset, uint256 \_duration, uint8 \_percentage, bool \_isUp, uint256 \_duelStake, uint256 \_usdcStake, bool sideFor) → uint256 duelId**

* Visibility: External.
* Parameters:
  * `_asset`: uint256 asset ID (must have a mapped oracle).
  * `_duration`: uint256 seconds (>0).
  * `_percentage`: uint8 (1-100).
  * `_isUp`: bool (true for up prediction).
  * `_duelStake`: uint256 optional initial $DUEL stake.
  * `_usdcStake`: uint256 optional initial USDC stake.
  * `sideFor`: bool (true for "for" side).
* Description: Creates a new duel, fetches start price from oracle, and optionally stakes initially. Increments `nextDuelId`.
* Requirements:
  * Valid oracle for asset.
  * Positive duration and valid percentage.
  * Oracle price >0.
  * If staking, meets MIN\_STAKE.
* Effects: Stores duel config. If staking, calls internal `_stake`. Emits `DuelCreated` with oracle address.
* Returns: The new `duelId`.

**stakeOnDuel(uint256 duelId, uint256 \_duelAmount, uint256 \_usdcAmount, bool sideFor)**

* Visibility: External.
* Parameters:
  * `duelId`: uint256 existing duel.
  * `_duelAmount`: uint256 $DUEL to stake (0 ok if USDC >0).
  * `_usdcAmount`: uint256 USDC to stake (0 ok if $DUEL >0).
  * `sideFor`: bool (true for "for").
* Description: Allows staking on an active duel.
* Requirements:
  * Duel exists and not resolved.
  * Total stake ≥ MIN\_STAKE.
  * Time not expired.
* Effects: Transfers tokens, updates pots and user stakes. Calls internal `_stake`. Emits `Staked`.

**resolveDuel(uint256 duelId)**

* Visibility: External.
* Parameters:
  * `duelId`: uint256.
* Description: Resolves the duel by checking end price, determining winner, and processing fees.
* Requirements:
  * Duel exists and not resolved.
  * Duration has ended.
  * Valid oracle price.
* Effects: Updates status. Calls internal `_determineWinner` and `_processFeesAndPots`. Emits `DuelResolved`.

**claimWinnings(uint256 duelId)**

* Visibility: External.
* Parameters:
  * `duelId`: uint256.
* Description: Allows winners to claim their payouts.
* Requirements:
  * Duel resolved.
  * Caller is a winner (has stakes on winning side).
* Effects: Calculates and transfers payout (original stake + share of effective losing pot). Zeroes user stakes. Calls internal `_calculatePayout` and `_checkWinnerEligibility`. Emits `Payout`.

**updateMarketingWallet(address newWallet)**

* Visibility: External.
* Modifiers: onlyOwner.
* Parameters:
  * `newWallet`: Address.
* Description: Updates the wallet receiving fees.
* Requirements: Caller is owner. `newWallet` != address(0) (not enforced; use caution).
* Effects: Changes `marketingWallet`.

**emergencyWithdraw()**

* Visibility: External.
* Modifiers: onlyOwner.
* Parameters: None.
* Description: Withdraws all $DUEL, USDC, and ETH from the contract to the owner. For emergencies only.
* Requirements: Caller is owner.
* Effects: Transfers balances to owner.

**Internal Functions (Reference)**

These are not directly callable but crucial to understanding:

* `_stake`: Handles token transfers and pot/user updates.
* `_determineWinner`: Compares oracle prices to target.
* `_processFeesAndPots`: Calculates fees, burns/swaps $DUEL, transfers USDC fees, updates effective pots. Calls swapFee.
* `swapFee`: Swaps $DUEL to ETH via Uniswap and sends to marketing.
* `_checkWinnerEligibility`: Verifies if user staked on winning side.
* `_calculatePayout`: Computes proportional winnings and zeroes stakes.

\\
