You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a prediction market platform (Polymarket-style) with three layers: Frontend (Vue 3), Backend (Spring Boot), and Blockchain (Polygon + Solidity).
# Deploy initial version
npx hardhat run scripts/deployBetMarket.js --network polygon
# Upgrade to new version
npx hardhat run scripts/upgradeBetMarket.js --network polygon
Order Matching
Flow
User places a limit order (YES or NO, at a specific price, for N shares)
Backend checks for opposing orders at compatible prices
If match found:
Both users' USDT transferred from their wallets → contract
placeBet() called on contract for both sides
Trade record created in database
If no match: order stays in order book as "pending"
Price Model
Prices are between 0.01 and 0.99 (representing probability)
YES price + NO price = 1.00 (always)
A YES order at 0.60 matches a NO order at 0.40
Winner gets $1.00 per share, so profit = $1.00 - price paid
Example
Event: "Will BTC reach $150K?"
Alice buys 10 shares YES @ $0.60 → pays $6.00
Bob buys 10 shares NO @ $0.40 → pays $4.00
Total in contract: $10.00
Result: YES wins
Alice receives 10 × $1.00 = $10.00 (profit: +$4.00)
Bob receives nothing (loss: -$4.00)
The PolymarketChainService handles all blockchain interactions:
Wallet Management — Auto-create Polygon wallet for each user, store encrypted private key
USDT Transfer — Transfer USDT from user wallet to contract (platform signs on behalf of user)
Contract Calls — placeBet, settleEvent, resetEvent via Web3j
Gas Sponsorship — Platform funds gas for all user transactions (EIP-1559)
Nonce Management — Atomic nonce counter to prevent collision under concurrency
Balance Query — On-chain USDT balance for users and contract
Gas Flow
Platform POL wallet (holds MATIC/POL for gas)
│
│ Funds gas for every user transaction
▼
User's Polygon wallet ──── USDT transfer ───▶ BetMarketV1 contract
Deployment
Prerequisites
Java 8+ & Maven
Node.js 16+ & npm
MySQL 5.7+
Polygon RPC endpoint (Alchemy/Infura)
Platform wallet with POL (for gas) and USDT (for initial liquidity)
Steps
# 1. Deploy smart contractcd contracts
npm install
npx hardhat run scripts/deployBetMarket.js --network polygon
# 2. Configure backend# Set contract address, RPC URL, platform wallet in application.yml# 3. Build & run backendcd Javabase
mvn package -DskipTests
java -jar ruoyi-admin/target/ruoyi-admin.jar # Admin API :8080
java -jar ruoyi-api/target/ruoyi-api.jar # User API :8221# 4. Build frontendcd h5base
npm install && npm run build
# 5. Build Android APK (optional)
npx cap sync android
cd android && gradlew.bat assembleDebug
# 6. Build admin panelcd admin
npm install && npm run build
Security Considerations
User private keys are encrypted at rest in database
Platform private key should be in environment variable, not in code
Contract is owner-only for critical operations
UUPS proxy allows patching vulnerabilities without redeployment
Rate limiting on order placement API
All chain operations logged to t_pm_chain_log
Future Roadmap
Batch settlement for events with many winners (gas optimization)
AMM liquidity pool for instant trading without order matching