> ## Documentation Index
> Fetch the complete documentation index at: https://quantumfinance.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# TradingVault: On-Chain Contract Functions and Events

> TradingVault contract reference for BNB Smart Chain — deployed address, callable functions, emitted events, and key parameters like fees and slippage limits.

Every deposit, trade, and withdrawal you make through Quantum Finance is handled by the TradingVault smart contract deployed on BNB Smart Chain. The contract is written in Solidity, audited against OpenZeppelin's security primitives, and enforces all fund custody rules on-chain — no centralized server can move your funds. This page describes what each function does, what events the contract emits, and where to find the deployed contract.

## Deployed contract

The TradingVault is deployed at the following address on BNB Smart Chain Testnet:

```
0x1f81d955D1348721553F8948Fb032973e9Db6Eff
```

<Tip>
  You can inspect every transaction, balance, and event on the block explorer: [View on BscScan Testnet](https://testnet.bscscan.com/address/0x1f81d955D1348721553F8948Fb032973e9Db6Eff)
</Tip>

<Note>
  The Quantum Finance interface calls all contract functions automatically on your behalf. You do not need to interact with the contract directly — this reference is provided for transparency and for developers who want to verify behavior.
</Note>

## Contract overview

The TradingVault is built on top of OpenZeppelin's `Ownable` and `ReentrancyGuard` contracts, which protect against ownership exploits and reentrancy attacks. Key properties enforced by the contract:

* **Minimum deposit:** 0.01 BNB
* **Maximum trade size:** 10 BNB (or 5,000 tokens for token-denominated trades)
* **Max slippage:** 5% per swap on PancakeSwap
* **Cooldown:** 30 seconds between trades per user
* **Fee:** 50% of trade output goes to the protocol treasury; 50% is credited to your profit balance

## Functions

<AccordionGroup>
  <Accordion title="deposit() — fund your vault">
    Called when you enter an amount and confirm a deposit in the Trading tab. The function is `payable`, meaning it accepts BNB directly. Your deposited amount is recorded against your wallet address in the contract's internal ledger.

    ```solidity theme={null}
    function deposit() external payable
    ```

    **Requirements:**

    * Amount must be greater than zero
    * Amount must be at least 0.01 BNB (`MIN_DEPOSIT`)

    **Emits:** `Deposited(address indexed user, uint256 amount)`
  </Accordion>

  <Accordion title="withdraw(uint256 amount) — pull funds from the vault">
    Called when you enter an amount in the Withdraw field and confirm. The contract checks that your recorded balance covers the requested amount before transferring BNB back to your wallet.

    ```solidity theme={null}
    function withdraw(uint256 amount) external
    ```

    **Requirements:**

    * `amount` must be greater than zero
    * Your vault balance must cover `amount`
    * The contract must hold sufficient BNB

    **Emits:** `Withdrawn(address indexed user, uint256 amount)`
  </Accordion>

  <Accordion title="executeBuyTrade() — engine opens a position">
    Called automatically by the engine when it enters a trade. The engine swaps BNB from your vault balance into the quote token (USDT or USDC) via PancakeSwap. Your vault balance is debited, and the resulting tokens are held in the contract. After fees, your profit balance is credited.

    ```solidity theme={null}
    function executeBuyTrade(
      string memory pair,
      address tokenOut,
      uint256 amountIn,
      uint256 minAmountOut
    ) external
    ```

    **Subject to:** 30-second cooldown, MAX\_TRADE\_SIZE limit, slippage check

    **Emits:** `TradeExecuted(...)`, `ProfitDistributed(...)`
  </Accordion>

  <Accordion title="executeSellTrade() — engine closes a position">
    Called automatically by the engine when it exits a trade. The engine swaps tokens from your profit balance back into BNB via PancakeSwap. After fees, your BNB vault balance is increased.

    ```solidity theme={null}
    function executeSellTrade(
      string memory pair,
      address tokenIn,
      uint256 amountIn,
      uint256 minAmountOut
    ) external
    ```

    **Subject to:** 30-second cooldown, MAX\_TRADE\_SIZE\_TOKENS limit, slippage check

    **Emits:** `TradeExecuted(...)`, `ProfitDistributed(...)`
  </Accordion>

  <Accordion title="withdrawProfits(address token, uint256 amount) — claim token profits">
    Called when you withdraw accumulated USDT or USDC profits. The function transfers the requested token amount from the contract to your wallet.

    ```solidity theme={null}
    function withdrawProfits(address token, uint256 amount) external
    ```

    **Requirements:**

    * `token` must be USDT or USDC
    * Your profit balance must cover `amount`
    * The contract must hold sufficient token balance
  </Accordion>

  <Accordion title="getUserInfo(address user) — read vault balances">
    Returns a snapshot of your vault state. The Quantum Finance interface calls this function to display your live balance and profit figures in the dashboard.

    ```solidity theme={null}
    function getUserInfo(address user)
      external
      view
      returns (
        uint256 balance,
        uint256 profit,
        uint256 lastTradeTime,
        uint256 tradeCount
      )
    ```

    This is a read-only (`view`) call — it costs no gas and does not modify any state.
  </Accordion>
</AccordionGroup>

## Events

The contract emits the following events. You can query them directly on BscScan or via any EVM-compatible indexer.

| Event | When it fires |
| - | - |
| `Deposited(address indexed user, uint256 amount)` | You successfully deposit BNB into the vault |
| `Withdrawn(address indexed user, uint256 amount)` | You successfully withdraw BNB from the vault |
| `TradeExecuted(address indexed user, string pair, uint256 amountIn, uint256 amountOut, bool isBuy, uint256 fee, uint256 timestamp)` | The engine completes a buy or sell swap on PancakeSwap |
| `ProfitDistributed(address indexed user, uint256 userShare, uint256 treasuryShare)` | After a trade, profit is split between your account and the protocol treasury |
| `EmergencyWithdraw(address indexed user, uint256 amount)` | The contract owner invokes emergency recovery (owner-only) |

## DEX integration

The TradingVault routes all swaps through PancakeSwap. The router addresses used are:

| Network | Router address |
| - | - |
| BNB Mainnet (`0x38`) | `0x10ED43C718714eb63d5aA57B78B54704E256024E` |
| BNB Testnet (`0x61`) | `0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3` |

<Warning>
  PancakeSwap Testnet has limited liquidity. Real on-chain swaps may fail if a liquidity pool does not exist for the selected pair. Use Demo mode to test trading logic without requiring on-chain liquidity.
</Warning>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.