Skip to content

Commit

Permalink
Merge pull request #521 from djeck1432/fix/dispay-user-extra-deposit
Browse files Browse the repository at this point in the history
[Backend] Display user extra deposit
  • Loading branch information
djeck1432 authored Jan 28, 2025
2 parents c2ed6c9 + 38c8fd9 commit e2d5718
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 28 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/cairo_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ on:
push:
branches:
- main
paths:
- '**/*.rs'
- '**/*.cairo'
- Scarb.toml
- Scarb.lock
- .tool-versions
pull_request:
branches:
- main
paths:
- '**/*.rs'
- '**/*.cairo'
- Scarb.toml
- Scarb.lock
- .tool-versions

jobs:
build-and-test:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ on:
push:
branches:
- main
paths:
- 'web_app/**'
- 'devops/**'
pull_request:
branches:
- main
paths:
- 'web_app/**'
- 'devops/**'

jobs:
shared:
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/frontend_tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
name: Run Frontend Tests

on: [push, pull_request]
on:
push:
paths:
- 'frontend/**'
- 'devops/**'
pull_request:
paths:
- 'frontend/**'
- 'devops/**'

jobs:
test:
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ on:
push:
branches:
- main
paths:
- 'web_app/**'
- 'devops/**'
pull_request:
branches:
- main

paths:
- 'web_app/**'
- 'devops/**'
jobs:
shared:
uses: ./.github/workflows/shared_workflow.yml
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ name: Pylint
on:
push:
branches: [main]
paths:
- '**/*.py'
pull_request:
branches: [main]
paths:
- '**/*.py'

jobs:
shared:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ This guide explains how to start the development environment for the project usi

If you get a "command not found" error, you might have the older version. Check with:
```sh
docker-compose version
docker compose version
```

### Installing/Updating Docker
Expand Down Expand Up @@ -231,7 +231,7 @@ docker compose up -d celery celery_beat
To stop the Celery worker and Beat services, run
```bash
docker-compose stop celery celery_beat
docker compose stop celery celery_beat
```
### Purging Celery Tasks
Expand Down Expand Up @@ -267,7 +267,7 @@ docker exec -ti backend_dev python -m web_app.db.seed_data
Run up docker containers
```bash
docker compose -f docker-compose.dev.yaml up --build
docker compose -f devops/docker-compose.dev.yaml up --build
```
Windows users:
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/dashboard/deposited/Deposited.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ function Deposited({ data }) {
<p className="currency-value">{data.usdc}</p>
</div>

<div className="info-divider" />
{/* <div className="info-divider" />
<div className="deposited-item">
<div className="currency-name">
<EthIcon className="icon" />
<p className="currency-name">USDT</p>
</div>
<p className="currency-value">{data.usdt}</p>
</div>
</div> */}
</div>
</div>
);
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/pages/dashboard/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default function DashboardPage({ telegramId }) {
const [currentSum, setCurrentSum] = useState(0);
const [loading, setLoading] = useState(true);
const [activeTab, setActiveTab] = useState(COLLATERAL);
const [depositedData, setDepositedData] = useState({ eth: 0, strk: 0, usdc: 0, usdt: 0 });

// ... (keep existing useEffect logic from the previous implementation)

Expand All @@ -88,7 +89,13 @@ export default function DashboardPage({ telegramId }) {
return;
}

const { health_ratio, current_sum, start_sum, borrowed, multipliers, balance } = data;
const { health_ratio, current_sum, start_sum, borrowed, multipliers, balance, deposit_data } = data;

// group extra deposits for each token
const updatedDepositedData = { eth: 0, strk: 0, usdc: 0, usdt: 0 };
deposit_data.forEach((deposit) => {
updatedDepositedData[deposit.token.toLowerCase()] += Number(deposit.amount);
});

let currencyName = 'Ethereum';
let currencyIcon = EthIcon;
Expand Down Expand Up @@ -126,6 +133,7 @@ export default function DashboardPage({ telegramId }) {

setCardData(updatedCardData);
setHealthFactor(health_ratio || '0.00');
setDepositedData(updatedDepositedData);
setCurrentSum(current_sum || 0);
setStartSum(start_sum || 0);
setLoading(false);
Expand All @@ -140,8 +148,6 @@ export default function DashboardPage({ telegramId }) {
return '';
};

const depositedData = { eth: 1, strk: 12, usdc: 4, usdt: 9 };

return (
<DashboardLayout>
{loading && <Spinner loading={loading} />}
Expand Down
2 changes: 1 addition & 1 deletion scripts/migrate.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

docker-compose -f docker-compose.back.yaml up --build
docker compose -f devops/docker-compose.back.yaml up --build

echo "Installing Poetry globally..."
curl -sSL https://install.python-poetry.org | python3 -
Expand Down
4 changes: 2 additions & 2 deletions web_app/api/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async def get_dashboard(wallet_id: str) -> DashboardResponse:
position_amount,
position_multiplier,
)
position_balance, extra_deposit_balance = await DashboardMixin.get_position_balance(
position_balance = await DashboardMixin.get_position_balance(
first_opened_position["id"]
)
total_position_balance = await DashboardMixin.calculate_position_balance(
Expand All @@ -106,7 +106,7 @@ async def get_dashboard(wallet_id: str) -> DashboardResponse:
current_sum=current_sum,
start_sum=start_sum,
borrowed=str(start_sum * Decimal(tvl)),
balance=str(total_position_balance + extra_deposit_balance),
balance=str(total_position_balance),
position_id=first_opened_position["id"],
deposit_data=deposit_data,
)
13 changes: 4 additions & 9 deletions web_app/contract_tools/mixins/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,13 @@ async def calculate_position_balance(cls, amount: str, multiplier: str) -> Decim
)

@classmethod
async def get_position_balance(cls, position_id: int) -> tuple:
async def get_position_balance(cls, position_id: int) -> str:
"""
Calculate the position balance.
:param position_id: Position ID
:return: Position balance
:return (str): Position balance
"""
main_position = position_db_connector.get_position_by_id(position_id)
extra_deposits = position_db_connector.get_extra_deposits_by_position_id(
position_id
)
main_position_balance = main_position and main_position.amount or "0"
total_extra_balance = Decimal("0")
for extra_deposit in extra_deposits:
total_extra_balance += Decimal(extra_deposit.amount)
return main_position_balance, total_extra_balance
return main_position_balance

21 changes: 15 additions & 6 deletions web_app/tests/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from web_app.api.dashboard import get_dashboard, router
from web_app.api.serializers.dashboard import DashboardResponse
from web_app.db.models import ExtraDeposit
from web_app.contract_tools.mixins import HealthRatioMixin
from web_app.contract_tools.mixins.dashboard import DashboardMixin

Expand Down Expand Up @@ -68,6 +69,14 @@ async def generic_exception_handler(request: Request, exc: Exception):

MOCK_WALLET_BALANCES = {"ETH": "10.5", "USDC": "1000.0"}

MOCK_EXTRA_DEPOSITS = [
ExtraDeposit(token_symbol="ETH", amount="100.0"),
ExtraDeposit(token_symbol="USDC", amount="200.0"),
]
RETURN_EXTRA_DEPOSIT = [
{"token": "ETH", "amount": "100.0"},
{"token": "USDC", "amount": "200.0"},
]

@pytest.mark.asyncio
async def test_get_dashboard_success():
Expand All @@ -76,7 +85,6 @@ async def test_get_dashboard_success():
wallet_id = "0x1234567890abcdef"
id = uuid.uuid4()
mock_position_balance = 500
mock_extra_deposit = 700
multiplier = 1

with patch(
Expand All @@ -86,6 +94,8 @@ async def test_get_dashboard_success():
) as mock_get_positions_by_wallet_id, patch(
"web_app.contract_tools.mixins.health_ratio.HealthRatioMixin.get_health_ratio_and_tvl"
) as mock_get_health_ratio_and_tvl, patch(
"web_app.db.crud.position.PositionDBConnector.get_extra_deposits_by_position_id",
) as mock_get_extra_deposits_by_position_id, patch(
"web_app.contract_tools.mixins.dashboard.DashboardMixin.get_wallet_balances",
new_callable=AsyncMock,
) as mock_get_wallet_balances, patch(
Expand All @@ -100,9 +110,9 @@ async def test_get_dashboard_success():
) as mock_get_position_balance:

mock_get_position_balance.return_value = (
mock_position_balance,
mock_extra_deposit,
mock_position_balance
)
mock_get_extra_deposits_by_position_id.return_value = MOCK_EXTRA_DEPOSITS
mock_get_contract_address_by_wallet_id.return_value = "0xabcdef1234567890"
mock_get_positions_by_wallet_id.return_value = [
{
Expand All @@ -128,7 +138,6 @@ async def test_get_dashboard_success():
* Decimal(multiplier)
* (Decimal(100) / Decimal(99))
)
balance = total_position_balance + mock_extra_deposit

async with AsyncClient(
transport=ASGITransport(app=app), base_url=BASE_URL
Expand All @@ -144,10 +153,10 @@ async def test_get_dashboard_success():
"current_sum": "200.0",
"start_sum": "200.0",
"borrowed": "200000.00",
"balance": str(balance),
"balance": str(total_position_balance),
"health_ratio": "1.2",
"position_id": str(id),
"deposit_data": [],
"deposit_data": RETURN_EXTRA_DEPOSIT,
}


Expand Down

0 comments on commit e2d5718

Please sign in to comment.