---
# System prepended metadata

title: Tradex

---

# Tradex

## Flow
- get a list of stocks  -> fetch from backend which will fetch from IB
- buy a stock   -> frontend will call blockchain: approve + create buy order
                -> backend will listen for CreateBuyEvent? from blockchain
                -> backend will trigger buying stock from IB for a given USDC at a current market price
                -> backend will call blockchain: mint token to the user
- sell a stock  -> frontend will call blockchain: approve + create sell order
                -> backend will listen for CreateSellEvent? from blockchain
                -> backend will trigger selling stock from IB for a current market price
                -> backend will call blockchain: burn user token and send

## Backend

### Database

OrderTask
- id: uint
- wallet: string
- amount_usd: uint
- order_id: uint
- stock_id: uint
- type: BUY, SELL
- created_at: timestamp
- updated_at: timestamp
- confirmation_id: string
- status: enum: PENDING, SUCCESSFUL, FAILED
- blockchain_task_id: uint
unique: (wallet, order_id)

BlockchainTask
- id: uint
- wallet: string
- chain_id: uint
- amount_usd: uint
- order_id: uint
- type: enum: BUY, SELL
- created_at: timestamp
- tx_hash: string
- status: enum: PENDING, SUCCESSFUL, FAILED
- order_task_id: uint
unique: (wallet, order_id)

## Blockchain

### Events


    event BuyOrderCreated(address indexed wallet, string stockId, uint256 orderId, uint256 amount, uint256 timestamp);
    event SellOrderCreated(address indexed wallet, string stockId, uint256 orderId, uint256 amount, uint256 timestamp);
    event OrderSettled();


### Function calls

    function settle(
        address wallet,
        uint256 orderId,
        uint256 usdcAmountSettled,
        uint256 tokenAmountSettled
    )
