Skip to content

feat(tide-chart): wallet connect + gTrade trading integration#23

Closed
RenzoMXD wants to merge 4 commits intoentrius:mainfrom
RenzoMXD:feat-connect-wallet
Closed

feat(tide-chart): wallet connect + gTrade trading integration#23
RenzoMXD wants to merge 4 commits intoentrius:mainfrom
RenzoMXD:feat-connect-wallet

Conversation

@RenzoMXD
Copy link
Copy Markdown

@RenzoMXD RenzoMXD commented Mar 9, 2026

Summary

Closes #14

Adds MetaMask wallet connection and direct equity trading via gTrade (Gains Network) on Arbitrum to the Tide Chart dashboard — completing the forecast-to-execution loop without leaving the app.

Demo

Connect wallet → view Synth forecasts → execute leveraged trade — all in one dashboard.

What Changed

tools/tide-chart/main.py

Frontend CDN

Wallet Connection (JS — WalletManager)

  • MetaMask integration via window.ethereum / EIP-1193 provider
  • Auto-switch to Arbitrum (wallet_switchEthereumChain) with auto-add fallback (wallet_addEthereumChain)
  • Event listeners for accountsChanged and chainChanged
  • Silent reconnect on page refresh via eth_accounts
  • Connect/disconnect toggle with truncated address display + Arbitrum network badge

Trading Panel (HTML + CSS + JS)

  • Collapsible panel between chart and table sections
  • Direction toggle: Long (green) / Short (red)
  • Collateral input with live USDC balance display
  • Leverage slider: 2x–150x with real-time position size calculation
  • Optional Take Profit / Stop Loss (%) inputs
  • Max Slippage control (default 1%)
  • "Connect wallet to trade" overlay when wallet not connected

Trade Execution (JS — GTradeManager)

  • USDC balance check via balanceOf()
  • Automatic USDC approval flow via approve() with MaxUint256
  • Trade struct building with correct precision:
    • USDC: 6 decimals (ethers.parseUnits)
    • Leverage: 1e3 (15x = 15000)
    • Prices (TP/SL): 1e10
    • openPrice: 0 for market orders
  • openTrade() call to GNSMultiCollatDiamond contract on Arbitrum
  • Success toast with clickable Arbiscan transaction link

Table Enhancement

  • New "Trade" column in asset rankings table
  • Gold "TRADE" buttons for 5 supported equities: SPY, NVDA, TSLA, AAPL, GOOGL
  • -- placeholder for non-tradeable assets: BTC, ETH, SOL, XAU
  • Delegated event listener on rank-tbody (survives table refreshes)

Toast Notification System

  • Fixed-position container (bottom-right)
  • Three types: success (green), error (red), info (gold)
  • Auto-dismiss with fade-out animation
  • Supports HTML content (clickable Arbiscan links)

All CSS matches existing dark theme: CSS variables, IBM Plex fonts, gold accent, responsive at 768px breakpoint.

tools/tide-chart/tests/test_tool.py

10 new tests:

Test Verifies
test_dashboard_has_wallet_button id="wallet-btn" and "Connect Wallet" text present
test_dashboard_has_trade_panel id="trade-panel", trade-form-grid, trade-connect-overlay present
test_dashboard_has_ethers_cdn ethers.js script tag from cdn.jsdelivr.net
test_dashboard_has_trade_buttons_for_equities data-asset="SPY/NVDA/TSLA/AAPL/GOOGL" present
test_dashboard_no_trade_buttons_for_crypto data-asset="BTC/ETH/SOL/XAU" NOT present
test_dashboard_has_toast_container id="toast-container" present
test_dashboard_has_gtrade_config GTRADE_CONFIG, contract addresses, chain ID in JS
test_dashboard_has_wallet_manager Wallet functions: handleWalletConnect, switchToArbitrum, event handlers
test_dashboard_has_trade_execution Trade functions: submitTrade, openTradePanel, ABI constants
test_table_rows_have_trade_column API response contains trade-cell-btn for equities, trade-cell-na for crypto

tools/tide-chart/README.md

  • New "Trading Integration" section with prerequisites, step-by-step trading flow, parameter table, and security notes
  • Updated technical details with trading stack info

gTrade Contract Details

Parameter Value
Network Arbitrum One (chain ID: 42161)
Trading Contract GNSMultiCollatDiamond0xFF162c694eAA571f685030649814282eA457f169
Collateral USDC — 0xaf88d065e77c8cC2239327C5EDb3A432268e5831
Collateral Index 3 (USDC in multi-collateral system)
Min Position Size $1,500 (collateral × leverage)
Leverage Range 2x – 150x

Edge Cases Handled (16 total)

Category Edge Cases
Wallet No extension installed (shows install link), connection rejected, disconnected mid-session, account changed (re-fetches balance), page refresh (silent reconnect)
Network Wrong chain (auto-switch to Arbitrum), Arbitrum not in wallet (auto-add via wallet_addEthereumChain), chain changed away from Arbitrum (warning + disable trading)
Trading Insufficient USDC (inline error), insufficient ETH for gas, USDC approval rejected, tx reverted (parsed error message), market closed (equity hours hint), slippage exceeded, position below $1,500 minimum (real-time validation), leverage out of 2-150x range (slider clamped), concurrent pending txs (button disabled with spinner)
UI 1h horizon hides trade buttons (no equities in 1h), trade panel closes on wallet disconnect, delegated events survive table refresh

Security

  • Non-custodial: Dashboard never touches private keys. All transactions signed in MetaMask.
  • Transparent: Every action (USDC approval, trade) requires explicit wallet confirmation popup.
  • On-chain: Trades execute on gTrade's audited smart contracts — no intermediary server.
  • No backend changes: Wallet + trading logic is 100% client-side JavaScript.

Test Plan

  • All 56 tide-chart tests pass (34 existing + 22 new)
  • All 73 synth_client tests pass (unchanged)
  • Dashboard loads with "Connect Wallet" button visible
  • Clicking "Connect Wallet" without MetaMask shows error toast with install link
  • MetaMask connection shows truncated address and Arbitrum badge
  • Clicking "Trade" on SPY opens trading panel with form
  • Switching to 1h horizon hides trade buttons (no equities)
  • Invalid inputs (0 collateral, >150x leverage) show inline validation errors
  • Full trade flow on Arbitrum (connect → approve USDC → open position → Arbiscan link)

Demo Video

Screencast.from.2026-03-09.13-45-03.webm

Not connected wallet:

Screencast.from.2026-03-09.13-52-13.webm

@MkDev11
Copy link
Copy Markdown
Contributor

MkDev11 commented Mar 9, 2026

@RenzoMXD unfortunately, you're not available to take part in issue competition since you're not in bronze+ tier

@RenzoMXD RenzoMXD changed the title eat(tide-chart): wallet connect + gTrade trading integration feat(tide-chart): wallet connect + gTrade trading integration Mar 9, 2026
@RenzoMXD
Copy link
Copy Markdown
Author

RenzoMXD commented Mar 9, 2026

@e35ventura Can you review my PR please? Thanks.

@RenzoMXD
Copy link
Copy Markdown
Author

RenzoMXD commented Mar 9, 2026

@MkDev11 Don't worry. I am already in Bronze Tier.

@ventura-oss
Copy link
Copy Markdown

I've updated the original issue (#14) to expand the scope to all assets supported by the Synth API (Crypto, Gold, and Equities).

A key requirement for the final integration is that the UI should never allow a user to submit a transaction that gTrade wouldn't accept. This means the interface needs to prevent inputs or submissions that would trigger protocol reverts—such as leverage mismatches, insufficient position sizes, or exceeding maximum leverage for specific asset classes. The goal is to catch these at the UI level before the transaction is ever sent.

@RenzoMXD
Copy link
Copy Markdown
Author

RenzoMXD commented Mar 9, 2026

@ventura-oss Thanks for your reply. I am going to fix right now.

@RenzoMXD
Copy link
Copy Markdown
Author

RenzoMXD commented Mar 9, 2026

@ventura-oss I have updated the code based on your updated issue. Here's what I've newly fixed.
Please let me know if anything else needs to be implemented.


1. Trade buttons on ALL assets

Every row in the table should have a Trade button — including BTC, ETH, SOL, and XAU (previously only equities had them).

2. Validation guard

Try entering collateral that makes position size < $1,500 — the submit button should stay disabled with an error message.

3. Wallet connect

Click Connect Wallet to test MetaMask integration (requires MetaMask in your browser).

@ventura-oss
Copy link
Copy Markdown

Screenshot from 2026-03-09 19-52-35

@e35ventura
Copy link
Copy Markdown
Collaborator

Please make sure you can place and monitor trades. Also, please upload an updated demo video that showcases the tool working end-to-end, opening and closing a trade

@RenzoMXD
Copy link
Copy Markdown
Author

@e35ventura @ventura-oss Thanks for your feedback. I will fix right now.

@RenzoMXD
Copy link
Copy Markdown
Author

@e35ventura Sorry for late. I just updated code. I will push my code in 30 minutes.

@RenzoMXD RenzoMXD force-pushed the feat-connect-wallet branch 2 times, most recently from 6b02f34 to 959b622 Compare March 10, 2026 18:28
@RenzoMXD
Copy link
Copy Markdown
Author

Screencast.from.2026-03-10.19-03-50.webm

@RenzoMXD RenzoMXD force-pushed the feat-connect-wallet branch from 959b622 to ff6a967 Compare March 10, 2026 19:10
@RenzoMXD
Copy link
Copy Markdown
Author

@e35ventura @ventura-oss I've addressed comments. Please let me know if anything else is needed. Thanks.

@ventura-oss
Copy link
Copy Markdown

I will be closing this PR in favor of #24. Local testing reveals that #24 handles P&L calculation much more accurately by tracking actual wallet balance changes, whereas this version relies on a theoretical formula that doesn't account for protocol fees and slippage.

@RenzoMXD RenzoMXD marked this pull request as draft March 10, 2026 19:26
@RenzoMXD
Copy link
Copy Markdown
Author

@ventura-oss @e35ventura Thanks for your review.
But I think PR 24 has some serious bugs.

Here is my opinion of bugs in 24.

  • TP/SL broken for shorts** — That PR sets TP above entry for shorts (should be below). This means short trades with TP set will either revert or trigger immediately at a loss.
  • Wrong leverage limits — hardcodes 2-150x for everything. A user trading XAU (allowed up to 250x) is artificially limited. A user trying 100x on stocks (max 50x) gets a revert and potentially loses gas.
  • No max SL validation — The exact bug you hit ("GOLD 6$ 200x + 1% SL fails") — no 75%/leverage max SL check.

@e35ventura
Copy link
Copy Markdown
Collaborator

thank you for bringing this to my attention.

@e35ventura e35ventura closed this Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tide Chart: Wallet Connect + Direct Trading via gTrade

4 participants