Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 13 additions & 49 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,50 +1,14 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/blockchain/node_modules
/.pnp
.pnp.js

# vscode
.vscode

# hardhat
artifacts
cache
deployments
cache/
coverage*
blockchain/typechain-types/@chainlink
blockchain/typechain-types/@openzeppelin
blockchain/typechain-types/factories/@chainlink
blockchain/typechain-types/factories/@openzeppelin

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
node_modules/
.next/
dist/
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel
__pycache__/
*.log
.DS_Store
*.swp
.vscode/
.idea/
*.test.js
*.test.ts
coverage/
.turbo/
64 changes: 64 additions & 0 deletions components/LotteryHistory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React, { useState, useEffect } from 'react';
import { useLotteryContext } from '../context/context';
import Table from './Table';
import styles from '../styles/Table.module.css';

export interface LotteryRound {
roundId: number;
timestamp: Date;
potSize: string;
winner: string;
participants: string[];
}

const LotteryHistory: React.FC = () => {
const { contract } = useLotteryContext();
const [history, setHistory] = useState<LotteryRound[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
const fetchLotteryHistory = async () => {
if (!contract) {
setError('Contract not initialized');
setIsLoading(false);
return;
}

try {
// Simulated history fetching - replace with actual contract method
const rounds: LotteryRound[] = await contract.getPastRounds();
setHistory(rounds);
setIsLoading(false);
} catch (err) {
console.error('Failed to fetch lottery history:', err);
setError('Failed to load lottery history');
setIsLoading(false);
}
};

fetchLotteryHistory();
}, [contract]);

if (isLoading) return <div>Loading lottery history...</div>;
if (error) return <div className={styles.error}>{error}</div>;
if (history.length === 0) return <div>No lottery history available</div>;

const historyHeaders = ['Round', 'Date', 'Pot Size', 'Winner', 'Participants'];
const historyRows = history.map(round => [
round.roundId.toString(),
round.timestamp.toLocaleDateString(),
round.potSize,
round.winner,
round.participants.length.toString()
]);

return (
<div className={styles.historyContainer}>
<h2>Lottery History</h2>
<Table headers={historyHeaders} rows={historyRows} />
</div>
);
};

export default LotteryHistory;
61 changes: 61 additions & 0 deletions components/__tests__/LotteryHistory.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import LotteryHistory from '../LotteryHistory';
import { useLotteryContext } from '../../context/context';

// Mock the context
vi.mock('../../context/context', () => ({
useLotteryContext: vi.fn()
}));

describe('LotteryHistory Component', () => {
it('shows loading state initially', () => {
vi.mocked(useLotteryContext).mockReturnValue({
contract: {
getPastRounds: vi.fn().mockResolvedValue([])
}
});

render(<LotteryHistory />);
expect(screen.getByText(/loading lottery history/i)).toBeInTheDocument();
});

it('displays error when contract is not initialized', async () => {
vi.mocked(useLotteryContext).mockReturnValue({
contract: null
});

render(<LotteryHistory />);

await waitFor(() => {
expect(screen.getByText(/contract not initialized/i)).toBeInTheDocument();
});
});

it('renders lottery history when data is available', async () => {
const mockHistory = [
{
roundId: 1,
timestamp: new Date('2023-01-01'),
potSize: '10 ETH',
winner: '0x123...',
participants: ['0x456...', '0x789...']
}
];

vi.mocked(useLotteryContext).mockReturnValue({
contract: {
getPastRounds: vi.fn().mockResolvedValue(mockHistory)
}
});

render(<LotteryHistory />);

await waitFor(() => {
expect(screen.getByText('Lottery History')).toBeInTheDocument();
expect(screen.getByText('1')).toBeInTheDocument(); // RoundId
expect(screen.getByText('10 ETH')).toBeInTheDocument(); // Pot Size
});
});
});
37 changes: 13 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
{
"name": "lottery",
"version": "0.1.0",
"private": true,
"scripts": {
"test": "vitest",
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"deploy:sepolia": "cd blockchain && npx hardhat run scripts/deploy.ts --network sepolia && cd .."
"start": "next start"
},
"dependencies": {
"@chainlink/contracts": "^0.6.1",
"@formkit/auto-animate": "^1.0.0-beta.6",
"@openzeppelin/contracts": "^4.8.2",
"dotenv": "^16.0.3",
"next": "12.2.5",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hot-toast": "^2.4.0",
"truncate-eth-address": "^1.0.2",
"web3": "^1.7.5"
"next": "^13.5.4",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^2.0.2",
"@types/node": "^18.15.3",
"@types/react": "^18.0.28",
"eslint": "8.21.0",
"eslint-config-next": "12.2.5",
"hardhat": "^2.13.0",
"typescript": "^5.0.2"
"@testing-library/react": "^14.0.0",
"@types/react": "^18.2.25",
"vitest": "^0.34.6",
"jsdom": "^22.1.0"
},
"jest": {
"testEnvironment": "jsdom"
}
}
}
34 changes: 25 additions & 9 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import Header from "../components/Header";
import LotteryCard from "../components/LotteryCard";
import Table from "../components/Table";
import style from "../styles/Home.module.css";
import React from 'react';
import Head from 'next/head';
import { ConnectWalletBtn } from '../components/ConnectWalletBtn';
import LotteryCard from '../components/LotteryCard';
import Header from '../components/Header';
import LotteryHistory from '../components/LotteryHistory';
import styles from '../styles/Home.module.css';

const Home = () => {
const Home: React.FC = () => {
return (
<div className={style.wrapper}>
<div className={styles.container}>
<Head>
<title>Blockchain Lottery</title>
<meta name="description" content="Decentralized Lottery Platform" />
<link rel="icon" href="/favicon.ico" />
</Head>

<Header />
<LotteryCard />
<Table />

<main className={styles.main}>
<ConnectWalletBtn />

<div className={styles.gridContainer}>
<LotteryCard />
<LotteryHistory />
</div>
</main>
</div>
);
};

export default Home;
export default Home;
11 changes: 11 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';

export default defineConfig({
plugins: [react()],
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['./vitest.setup.ts'],
},
});
3 changes: 3 additions & 0 deletions vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import '@testing-library/jest-dom';

// Add any global setup here if needed