From ea401cac0d955c108bc9669be393ac05f83dabb9 Mon Sep 17 00:00:00 2001 From: SumanthPal Date: Tue, 18 Nov 2025 23:31:11 -0800 Subject: [PATCH 1/2] added contract deployement code for base sepolia --- contracts/contracts/AgonusBetting.sol | 1 + contracts/hardhat.config.ts | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/contracts/contracts/AgonusBetting.sol b/contracts/contracts/AgonusBetting.sol index 9723ab9..f752322 100644 --- a/contracts/contracts/AgonusBetting.sol +++ b/contracts/contracts/AgonusBetting.sol @@ -107,6 +107,7 @@ contract AgonusBetting is Ownable, ReentrancyGuard { require(!t.isSettled, "Already settled"); require(t.totalPool > 0, "No bets placed"); require(winningAgentId > 0 && winningAgentId <= t.agentCount, "Invalid agent ID"); + require(agentPools[tournamentId][winningAgentId] > 0, "Winning agent has no bets"); t.isSettled = true; t.winningAgentId = winningAgentId; diff --git a/contracts/hardhat.config.ts b/contracts/hardhat.config.ts index d8b90b8..5428885 100644 --- a/contracts/hardhat.config.ts +++ b/contracts/hardhat.config.ts @@ -36,6 +36,12 @@ const config: HardhatUserConfig = { url: configVariable("SEPOLIA_RPC_URL"), accounts: [configVariable("SEPOLIA_PRIVATE_KEY")], }, + baseSepolia: { + type: "http", + chainType: "op", + url: configVariable("BASE_SEPOLIA_RPC_URL"), + accounts: [configVariable("PRIVATE_KEY")], + }, }, }; From f7a21651d3418f753a023c45aa91ef6c571ae98e Mon Sep 17 00:00:00 2001 From: SumanthPal Date: Wed, 19 Nov 2025 08:16:44 -0800 Subject: [PATCH 2/2] removing trade tool test issues in branch --- backend/tests/test_trade_tool.py | 179 ------------------- backend/tests/test_trade_tool_integration.py | 114 ------------ 2 files changed, 293 deletions(-) delete mode 100644 backend/tests/test_trade_tool.py delete mode 100644 backend/tests/test_trade_tool_integration.py diff --git a/backend/tests/test_trade_tool.py b/backend/tests/test_trade_tool.py deleted file mode 100644 index ae41476..0000000 --- a/backend/tests/test_trade_tool.py +++ /dev/null @@ -1,179 +0,0 @@ -""" -Unit tests for TradeTool calculation methods. - -Run: pytest backend/tests/test_trade_tool.py -v -""" - -import pytest -from datetime import datetime, timezone -from app.agents.datatools import TradeTool -from app.agents.data_classes import Trade - - -@pytest.fixture -def trade_tool(): - return TradeTool(agent_id="agent_1") - - -@pytest.fixture -def sample_history(): - """Sample trade history with multiple buys""" - return [ - Trade( - trade_id=1, token="WETH", agent_id="agent_1", action="BUY", - qty=1.0, price=3000.0, confidence=0.8, summary="First buy", - timestamp=datetime.now(timezone.utc), tx_hash="0xabc", - realized_pnl=None, roi=None - ), - Trade( - trade_id=2, token="WETH", agent_id="agent_1", action="BUY", - qty=2.0, price=3300.0, confidence=0.9, summary="Second buy", - timestamp=datetime.now(timezone.utc), tx_hash="0xdef", - realized_pnl=None, roi=None - ), - Trade( - trade_id=3, token="cbBTC", agent_id="agent_1", action="BUY", - qty=0.5, price=60000.0, confidence=0.85, summary="BTC buy", - timestamp=datetime.now(timezone.utc), tx_hash="0xghi", - realized_pnl=None, roi=None - ) - ] - - -class TestGetAvgBuyPrice: - """Test _get_avg_buy_price helper method""" - - def test_single_buy(self, trade_tool, sample_history): - avg = trade_tool._get_avg_buy_price("cbBTC", sample_history) - assert avg == 60000.0 - - def test_multiple_buys_weighted_average(self, trade_tool, sample_history): - # WETH: 1.0@3000 + 2.0@3300 = (3000 + 6600) / 3 = 3200 - avg = trade_tool._get_avg_buy_price("WETH", sample_history) - assert avg == 3200.0 - - def test_no_buys(self, trade_tool, sample_history): - avg = trade_tool._get_avg_buy_price("USDC", sample_history) - assert avg == 0.0 - - def test_empty_history(self, trade_tool): - avg = trade_tool._get_avg_buy_price("WETH", []) - assert avg == 0.0 - - -class TestCalculateRealizedPnL: - """Test PnL calculation for SELL trades""" - - def test_profitable_sell(self, trade_tool, sample_history): - sell = Trade( - trade_id=4, token="WETH", agent_id="agent_1", action="SELL", - qty=1.5, price=3500.0, confidence=0.75, summary="Profit", - timestamp=datetime.now(timezone.utc), tx_hash="0xjkl", - realized_pnl=None, roi=None - ) - - pnl = trade_tool.calculate_realized_pnl(sell, sample_history) - # (3500 - 3200) * 1.5 = 450 - assert pnl == 450.0 - - def test_losing_sell(self, trade_tool, sample_history): - sell = Trade( - trade_id=4, token="WETH", agent_id="agent_1", action="SELL", - qty=1.0, price=3000.0, confidence=0.7, summary="Loss", - timestamp=datetime.now(timezone.utc), tx_hash="0xmno", - realized_pnl=None, roi=None - ) - - pnl = trade_tool.calculate_realized_pnl(sell, sample_history) - # (3000 - 3200) * 1.0 = -200 - assert pnl == -200.0 - - def test_no_buy_history(self, trade_tool): - sell = Trade( - trade_id=1, token="WETH", agent_id="agent_1", action="SELL", - qty=1.0, price=3500.0, confidence=0.8, summary="Sell", - timestamp=datetime.now(timezone.utc), tx_hash="0xpqr", - realized_pnl=None, roi=None - ) - - pnl = trade_tool.calculate_realized_pnl(sell, []) - assert pnl == 0.0 - - -class TestCalculateROI: - """Test ROI calculation for SELL trades""" - - def test_positive_roi(self, trade_tool, sample_history): - sell = Trade( - trade_id=4, token="WETH", agent_id="agent_1", action="SELL", - qty=1.5, price=3500.0, confidence=0.75, summary="Profit", - timestamp=datetime.now(timezone.utc), tx_hash="0xstu", - realized_pnl=None, roi=None - ) - - roi = trade_tool.calculate_roi(sell, sample_history) - # Invested: 3200 * 1.5 = 4800 - # PnL: (3500 - 3200) * 1.5 = 450 - # ROI: 450 / 4800 = 0.09375 - assert roi == pytest.approx(0.09375) - - def test_negative_roi(self, trade_tool, sample_history): - sell = Trade( - trade_id=4, token="WETH", agent_id="agent_1", action="SELL", - qty=1.0, price=2880.0, confidence=0.7, summary="Loss", - timestamp=datetime.now(timezone.utc), tx_hash="0xvwx", - realized_pnl=None, roi=None - ) - - roi = trade_tool.calculate_roi(sell, sample_history) - # Invested: 3200 * 1.0 = 3200 - # PnL: (2880 - 3200) * 1.0 = -320 - # ROI: -320 / 3200 = -0.1 - assert roi == pytest.approx(-0.1) - - def test_no_buy_history(self, trade_tool): - sell = Trade( - trade_id=1, token="WETH", agent_id="agent_1", action="SELL", - qty=1.0, price=3500.0, confidence=0.8, summary="Sell", - timestamp=datetime.now(timezone.utc), tx_hash="0xyza", - realized_pnl=None, roi=None - ) - - roi = trade_tool.calculate_roi(sell, []) - assert roi == 0.0 - - -class TestExecuteTradeValidation: - """Test input validation for execute_trade""" - - def test_invalid_action(self, trade_tool): - with pytest.raises(ValueError, match="Invalid action"): - trade_tool.execute_trade("HOLD", "WETH", 1.0, 3000.0, 0.8, "Test") - - def test_invalid_token(self, trade_tool): - with pytest.raises(ValueError, match="Invalid token"): - trade_tool.execute_trade("BUY", "INVALID", 1.0, 0.0, 0.8, "Test") - - def test_case_insensitive_action(self, trade_tool): - # Lowercase action should be accepted and converted to uppercase - # Should NOT raise ValueError for invalid action - try: - trade_tool.execute_trade("buy", "WETH", 1.0, 0.0, 0.8, "Test") - except ValueError as e: - if "Invalid action" in str(e): - pytest.fail("Should accept lowercase action") - except Exception: - # Other exceptions (like swap failures) are acceptable - pass - - def test_case_insensitive_token(self, trade_tool): - # Lowercase token should be accepted and converted to uppercase - # Should NOT raise ValueError for invalid token - try: - trade_tool.execute_trade("BUY", "weth", 1.0, 0.0, 0.8, "Test") - except ValueError as e: - if "Invalid token" in str(e): - pytest.fail("Should accept lowercase token") - except Exception: - # Other exceptions (like swap failures) are acceptable - pass diff --git a/backend/tests/test_trade_tool_integration.py b/backend/tests/test_trade_tool_integration.py deleted file mode 100644 index f1bbbf0..0000000 --- a/backend/tests/test_trade_tool_integration.py +++ /dev/null @@ -1,114 +0,0 @@ -""" -Integration tests for TradeTool with real Uniswap swaps on Base blockchain. - -Prerequisites: -1. Fund agent wallets using trading-bot/fundWallet.ts -2. Ensure Anvil fork is running: anvil --fork-url --chain-id 8453 --auto-impersonate - -Tests execute REAL on-chain transactions and cost gas. - -Run: pytest backend/tests/test_trade_tool_integration.py -v -s -""" - -import pytest -from app.agents.datatools import TradeTool - - -@pytest.fixture(scope="module") -def trade_tool(): - """Create TradeTool instance for agent_1""" - return TradeTool(agent_id="agent_1") - - -@pytest.mark.integration -class TestRealTrades: - """Execute real trades on Base blockchain""" - - def test_buy_weth_with_usdc(self, trade_tool): - """Test BUY: USDC → WETH""" - - trade = trade_tool.execute_trade( - action="BUY", - token="WETH", - qty=10.0, - price=0.0, - confidence=0.85, - summary="Test buy WETH" - ) - - assert trade.action == "BUY" - assert trade.token == "WETH" - assert trade.agent_id == "agent_1" - assert trade.qty > 0 - assert trade.price > 0 - assert trade.tx_hash is not None - assert trade.tx_hash.startswith("0x") - assert len(trade.tx_hash) == 66 - - print(f"\n✅ BUY WETH: Spent 10 USDC, received {trade.qty:.6f} WETH at {trade.price:.2f} USDC/WETH") - print(f"TX: https://basescan.org/tx/{trade.tx_hash}") - - def test_buy_cbbtc_with_usdc(self, trade_tool): - """Test BUY: USDC → cbBTC""" - - trade = trade_tool.execute_trade( - action="BUY", - token="CBBTC", - qty=10.0, - price=0.0, - confidence=0.8, - summary="Test buy cbBTC" - ) - - assert trade.action == "BUY" - assert trade.token == "CBBTC" - assert trade.qty > 0 - assert trade.price > 0 - assert trade.tx_hash is not None - - print(f"\n✅ BUY CBBTC: Spent 10 USDC, received {trade.qty:.8f} CBBTC at {trade.price:.2f} USDC/CBBTC") - print(f"TX: https://basescan.org/tx/{trade.tx_hash}") - - def test_sell_weth_for_usdc(self, trade_tool): - """Test SELL: WETH → USDC""" - - trade = trade_tool.execute_trade( - action="SELL", - token="WETH", - qty=0.003, - price=0.0, - confidence=0.75, - summary="Test sell WETH" - ) - - assert trade.action == "SELL" - assert trade.token == "WETH" - assert trade.qty == 0.003 - assert trade.price > 0 - assert trade.tx_hash is not None - - usdc_received = trade.qty * trade.price - print(f"\n✅ SELL WETH: Sold {trade.qty:.6f} WETH, received ~{usdc_received:.2f} USDC") - print(f"TX: https://basescan.org/tx/{trade.tx_hash}") - - def test_sell_cbbtc_for_usdc(self, trade_tool): - """Test SELL: cbBTC → USDC""" - - trade = trade_tool.execute_trade( - action="SELL", - token="CBBTC", - qty=0.0001, - price=0.0, - confidence=0.7, - summary="Test sell cbBTC" - ) - - assert trade.action == "SELL" - assert trade.token == "CBBTC" - assert trade.qty == 0.0001 - assert trade.price > 0 - assert trade.tx_hash is not None - - usdc_received = trade.qty * trade.price - print(f"\n✅ SELL CBBTC: Sold {trade.qty:.8f} CBBTC, received ~{usdc_received:.2f} USDC") - print(f"TX: https://basescan.org/tx/{trade.tx_hash}")