Skip to content

Commit

Permalink
Merge branch 'djeck1432:main' into fix/dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunusabdul38 authored Dec 17, 2024
2 parents 6cc75ad + 2b3ad6b commit 8dde0f9
Show file tree
Hide file tree
Showing 25 changed files with 766 additions and 133 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ env:
DB_USER: postgres
DB_PASSWORD: password
DB_HOST: db
STARKNET_NODE_URL: http://178.32.172.148:6060
STARKNET_NODE_URL: http://51.195.57.196:6060/v0_7
REDIS_HOST: redis
REDIS_PORT: 6379
ENV_VERSION: DEV
Expand All @@ -37,7 +37,7 @@ jobs:

- name: Create .env file
run: |
cat << EOF > .env.dev
cat << EOF > /home/runner/work/spotnet/spotnet/.env
ENV_VERSION=DEV
STARKNET_NODE_URL=${{ env.STARKNET_NODE_URL }}
DB_USER=${{ env.DB_USER }}
Expand Down Expand Up @@ -65,30 +65,30 @@ jobs:
run: |
while ! curl -s http://localhost:8000/health > /dev/null; do
echo "Waiting for backend service..."
sleep 10
sleep 30
# Check if the container is still running before logging
if ! docker ps | grep -q backend_dev; then
if ! docker ps | grep -q backend; then
echo "Backend container is not running!"
docker compose -f docker-compose.dev.yaml logs backend_dev || true
docker compose -f docker-compose.dev.yaml logs backend || true
exit 1
fi
# Log the backend service status for debugging purposes.
docker compose -f docker-compose.dev.yaml logs backend_dev || true
docker compose -f docker-compose.dev.yaml logs backend || true
done
- name: Apply Migrations
run: |
docker exec backend_dev alembic -c web_app/alembic.ini upgrade head || {
echo "Migration failed. Showing backend logs:"
docker compose -f docker-compose.dev.yaml logs backend_dev || true
docker compose -f docker-compose.dev.yaml logs backend || true
exit 1
}
- name: Run Integration Tests with Coverage
run: |
docker exec backend_dev bash -c "cd /app && python -m pytest web_app/test_integration/ -v"
docker compose exec backend bash -c "cd /app && python -m pytest web_app/test_integration/ -v"
- name: Clean Up
Expand Down
44 changes: 44 additions & 0 deletions Dockerfile.windows
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

FROM python:3.12-slim

# Environment settings
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

# Set PATH for Poetry
ENV PATH="/root/.local/bin:$PATH"

# Add system-level dependencies (including gcc and npm)
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
libpq-dev gcc g++ make libffi-dev build-essential \
curl nodejs npm \
dos2unix \
&& rm -rf /var/lib/apt/lists/*

# Install Poetry
RUN curl -sSL https://install.python-poetry.org | python3 -

# Create app directory
RUN mkdir /app
WORKDIR /app

# Copy the pyproject.toml and poetry.lock files into container's /app/ directory
COPY pyproject.toml poetry.lock /app/

# Install dependencies from the poetry.lock file
RUN poetry config virtualenvs.create false \
&& poetry install --no-dev --no-interaction --no-root

# Copy the rest of the application code
ADD . /app

# Install StarknetKit via npm with legacy-peer-deps flag
RUN npm install @argent/get-starknet --legacy-peer-deps --save

# Set the entrypoint script as executable
# Copy the rest of the application code and set the entrypoint
COPY . /app
RUN dos2unix /app/entrypoint.sh

EXPOSE 8000
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ This guide explains how to start the development environment for the project usi
docker-compose -f docker-compose.dev.yaml up --build
```

For Windows users, use this command to build and start the development environment:

```sh
docker-compose -f docker-compose.dev-windows.yaml up --build
```

This command will:
- Build the backend and frontend Docker images.
- Start the backend, frontend, and PostgreSQL database containers.
Expand All @@ -60,6 +66,10 @@ This guide explains how to start the development environment for the project usi
```sh
docker-compose -f docker-compose.dev.yaml build --no-cache
```
Windows users:
```sh
docker-compose -f docker-compose.dev-windows.yaml build --no-cache
```

## How to run test cases
In root folder run next commands:
Expand All @@ -82,6 +92,11 @@ To stop the environment and remove containers, use:
```sh
docker-compose -f docker-compose.dev.yaml down
```
windows users:

```sh
docker-compose -f docker-compose.dev-windows.yaml down
```

This command stops all running containers and removes them, but the data volumes will persist.

Expand All @@ -92,6 +107,10 @@ If you have made changes to the code or Docker configuration, rebuild the contai
```sh
docker-compose -f docker-compose.dev.yaml up --build
```
windows users:
```sh
docker-compose -f docker-compose.dev-windows.yaml up --build
```

## About Celery

Expand Down Expand Up @@ -129,6 +148,10 @@ docker-compose run --rm celery celery -A spotnet_tracker.celery_config purge
```
docker-compose -f docker-compose.dev.yaml up --build
```
windows only:
```
docker-compose -f docker-compose.dev-windows.yaml up --build
```
2. In new terminal window run command to populate db
```
docker exec -ti backend_dev python -m web_app.db.seed_data
Expand All @@ -140,6 +163,10 @@ Run up docker containers
```bash
docker-compose -f docker-compose.dev.yaml up --build
```
Windows users:
```bash
docker-compose -f docker-compose.dev-windows.yaml up --build
```
Go to backend container in new terminal window
```bash
docker exec -ti backend_dev bash
Expand Down
67 changes: 67 additions & 0 deletions docker-compose.dev-windows.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

version: '3.8'

networks:
app_network:
driver: bridge

services:
backend:
build:
context: .
dockerfile: Dockerfile.windows
command: ["/bin/bash", "-c", "chmod +x /app/entrypoint.sh && /app/entrypoint.sh"]
container_name: backend_dev
volumes:
- .:/app
env_file:
- .env.dev
ports:
- "8000:8000"
networks:
- app_network
depends_on:
- db
environment:
- DB_HOST=db
- DB_PORT=5432
- DB_NAME=spotnet
- DB_USER=postgres
- DB_PASSWORD=password

db:
image: postgres:16
container_name: postgres_dev
environment:
POSTGRES_DB: spotnet
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
volumes:
- postgres_data_dev:/var/lib/postgresql/data
- ./init-db:/docker-entrypoint-initdb.d
networks:
- app_network
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5

frontend:
build:
context: ./frontend
dockerfile: Dockerfile.dev
container_name: frontend_dev
volumes:
- ./frontend:/app
ports:
- "3000:80"
networks:
- app_network
depends_on:
- backend

volumes:
postgres_data_dev:
71 changes: 71 additions & 0 deletions frontend/src/hooks/useHealthRatio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { useQuery } from '@tanstack/react-query';
import { axiosInstance } from 'utils/axios';

const TOKEN_CONFIG = {
ETH: {
id: 'ethereum',
collateralFactor: 0.8,
borrowFactor: 0.9,
decimals: 18
},
USDC: {
id: 'usd-coin',
collateralFactor: 0.85,
borrowFactor: 0.9,
decimals: 6
},
STRK: {
id: 'starknet',
collateralFactor: 0.75,
borrowFactor: 0.85,
decimals: 18
}
};

const fetchTokenPrice = async (tokenId) => {
const { data } = await axiosInstance.get(
`https://api.coingecko.com/api/v3/simple/price?ids=${tokenId}&vs_currencies=usd`
);
return data[tokenId].usd;
};

export const useHealthFactor = (selectedToken, tokenAmount, selectedMultiplier) => {
const tokenId = selectedToken ? TOKEN_CONFIG[selectedToken]?.id : null;

const { data: tokenPrice = 0, error } = useQuery({
queryKey: ['tokenPrice', tokenId],
queryFn: () => fetchTokenPrice(tokenId),
enabled: !!tokenId,
staleTime: 30000,
cacheTime: 60000,
});

const calculateHealthFactor = () => {
if (!tokenAmount || !selectedMultiplier || !tokenPrice) {
return 0;
}

try {
const amount = parseFloat(tokenAmount);
const multiplier = parseFloat(selectedMultiplier);
const tokenConfig = TOKEN_CONFIG[selectedToken];

const collateralValue = amount * tokenPrice * tokenConfig.collateralFactor;
const borrowedAmount = amount * tokenPrice * (multiplier - 1);
const adjustedDebtValue = borrowedAmount / tokenConfig.borrowFactor;
const healthFactorValue = collateralValue / adjustedDebtValue;

return Number(healthFactorValue.toFixed(6));
} catch (error) {
console.error('Error calculating health factor:', error);
return 0;
}
};

return {
healthFactor: calculateHealthFactor(),
tokenPrice,
isLoading: !error && !tokenPrice,
isError: !!error
};
};
20 changes: 18 additions & 2 deletions frontend/src/pages/forms/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useConnectWallet } from 'hooks/useConnectWallet';
import { useCheckPosition } from 'hooks/useClosePosition';
import { useNavigate } from 'react-router-dom';
import { ActionModal } from 'components/ui/ActionModal';
import { useHealthFactor } from 'hooks/useHealthRatio';

const Form = () => {
const navigate = useNavigate();
Expand All @@ -29,11 +30,18 @@ const Form = () => {
const [loading, setLoading] = useState(false);
const [alertMessage, setAlertMessage] = useState('');
const [successful, setSuccessful] = useState(false);

useLockBodyScroll(successful);
const [isClosePositionOpen, setClosePositionOpen] = useState(false);
const connectWalletMutation = useConnectWallet(setWalletId);
const { data: positionData, refetch: refetchPosition } = useCheckPosition();

const { healthFactor, isLoading: isHealthFactorLoading } = useHealthFactor(
selectedToken,
tokenAmount,
selectedMultiplier
);

const connectWalletHandler = () => {
if (!walletId) {
connectWalletMutation.mutate();
Expand Down Expand Up @@ -97,7 +105,7 @@ const Form = () => {
content={[
'You have already opened a position.',
'Please close active position to open a new one.',
'Click the Close Active Position button to continue.',
"Click the 'Close Active Position' button to continue.",
]}
cancelLabel="Cancel"
submitLabel="Close Active Position"
Expand Down Expand Up @@ -134,6 +142,14 @@ const Form = () => {
/>
</div>
<div>
<div className="form-health-factor">
<p>
Estimated Health Factor Level:
</p>
<p>
{isHealthFactorLoading ? 'Loading...' : healthFactor}
</p>
</div>
<div className="form-button-container">
<Button variant="secondary" size="lg" type="submit">
Submit
Expand All @@ -146,4 +162,4 @@ const Form = () => {
);
};

export default Form;
export default Form;
12 changes: 12 additions & 0 deletions frontend/src/pages/forms/form.css
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,15 @@ input[type='number'].error {
font-size: 15px;
}
}

.form-health-factor{
display: flex;
flex-direction: row;
align-items: end;
justify-self: flex-end;
width: fit-content;
margin: 2px;
color: gray;
align-self: flex-end;
gap: 5px;
}
Loading

0 comments on commit 8dde0f9

Please sign in to comment.