|
| 1 | +# ChainBridge Frontend |
| 2 | + |
| 3 | +Next.js 14 frontend for the ChainBridge cross-chain atomic swap protocol. |
| 4 | + |
| 5 | +## Tech Stack |
| 6 | + |
| 7 | +- **Framework**: Next.js 14 with App Router |
| 8 | +- **Language**: TypeScript |
| 9 | +- **Styling**: Tailwind CSS |
| 10 | +- **Blockchain Libraries**: |
| 11 | + - `ethers` - Ethereum interaction |
| 12 | + - `bitcoinjs-lib` - Bitcoin interaction |
| 13 | + - `@stellar/stellar-sdk` - Stellar/Soroban interaction |
| 14 | + - `@stellar/freighter-api` - Freighter wallet integration |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- Node.js 18.17 or later |
| 19 | +- npm, yarn, or pnpm |
| 20 | + |
| 21 | +## Getting Started |
| 22 | + |
| 23 | +### 1. Install Dependencies |
| 24 | + |
| 25 | +```bash |
| 26 | +npm install |
| 27 | +# or |
| 28 | +yarn install |
| 29 | +# or |
| 30 | +pnpm install |
| 31 | +``` |
| 32 | + |
| 33 | +### 2. Environment Setup |
| 34 | + |
| 35 | +Create a `.env.local` file: |
| 36 | + |
| 37 | +```bash |
| 38 | +cp .env.example .env.local |
| 39 | +``` |
| 40 | + |
| 41 | +Environment variables: |
| 42 | + |
| 43 | +| Variable | Description | Default | |
| 44 | +|----------|-------------|---------| |
| 45 | +| `NEXT_PUBLIC_API_URL` | Backend API URL | `http://localhost:8000` | |
| 46 | +| `NEXT_PUBLIC_STELLAR_NETWORK` | Stellar network (testnet/mainnet) | `testnet` | |
| 47 | +| `NEXT_PUBLIC_BITCOIN_NETWORK` | Bitcoin network | `testnet` | |
| 48 | + |
| 49 | +### 3. Development Server |
| 50 | + |
| 51 | +```bash |
| 52 | +npm run dev |
| 53 | +``` |
| 54 | + |
| 55 | +Open [http://localhost:3000](http://localhost:3000) in your browser. |
| 56 | + |
| 57 | +### 4. Production Build |
| 58 | + |
| 59 | +```bash |
| 60 | +npm run build |
| 61 | +npm run start |
| 62 | +``` |
| 63 | + |
| 64 | +## Project Structure |
| 65 | + |
| 66 | +``` |
| 67 | +frontend/ |
| 68 | +├── public/ # Static assets |
| 69 | +├── src/ |
| 70 | +│ ├── app/ # Next.js App Router pages |
| 71 | +│ │ ├── layout.tsx # Root layout |
| 72 | +│ │ └── page.tsx # Home page |
| 73 | +│ ├── components/ # React components |
| 74 | +│ ├── hooks/ # Custom React hooks |
| 75 | +│ │ ├── useLocalStorage.ts |
| 76 | +│ │ └── useAsync.ts |
| 77 | +│ ├── lib/ # Utility libraries |
| 78 | +│ │ ├── ethereum.ts # Ethereum wallet functions |
| 79 | +│ │ ├── bitcoin.ts # Bitcoin utilities |
| 80 | +│ │ └── stellar.ts # Stellar/Soroban functions |
| 81 | +│ ├── styles/ # Global styles |
| 82 | +│ │ └── globals.css |
| 83 | +│ └── types/ # TypeScript type definitions |
| 84 | +│ └── index.ts |
| 85 | +├── next.config.js |
| 86 | +├── tailwind.config.ts |
| 87 | +├── tsconfig.json |
| 88 | +└── package.json |
| 89 | +``` |
| 90 | + |
| 91 | +## Scripts |
| 92 | + |
| 93 | +| Script | Description | |
| 94 | +|--------|-------------| |
| 95 | +| `npm run dev` | Start development server | |
| 96 | +| `npm run build` | Build for production | |
| 97 | +| `npm run start` | Start production server | |
| 98 | +| `npm run lint` | Run ESLint | |
| 99 | +| `npm run format` | Format with Prettier | |
| 100 | + |
| 101 | +## Code Quality |
| 102 | + |
| 103 | +### ESLint |
| 104 | + |
| 105 | +```bash |
| 106 | +npm run lint |
| 107 | +``` |
| 108 | + |
| 109 | +### Prettier |
| 110 | + |
| 111 | +```bash |
| 112 | +# Format all files |
| 113 | +npm run format |
| 114 | + |
| 115 | +# Check formatting |
| 116 | +npx prettier --check . |
| 117 | +``` |
| 118 | + |
| 119 | +### TypeScript |
| 120 | + |
| 121 | +```bash |
| 122 | +# Type check |
| 123 | +npx tsc --noEmit |
| 124 | +``` |
| 125 | + |
| 126 | +## Features |
| 127 | + |
| 128 | +### Wallet Integration |
| 129 | + |
| 130 | +The frontend supports multiple blockchain wallets: |
| 131 | + |
| 132 | +- **Stellar**: Freighter wallet |
| 133 | +- **Ethereum**: MetaMask and other Web3 wallets |
| 134 | +- **Bitcoin**: Bitcoin wallets (manual address input) |
| 135 | + |
| 136 | +### Key Components |
| 137 | + |
| 138 | +- Swap creation and management |
| 139 | +- Order book browsing |
| 140 | +- Transaction history |
| 141 | +- Multi-chain wallet connection |
| 142 | + |
| 143 | +## Development Guidelines |
| 144 | + |
| 145 | +### Component Creation |
| 146 | + |
| 147 | +```tsx |
| 148 | +// Use functional components with TypeScript |
| 149 | +import { FC } from "react"; |
| 150 | + |
| 151 | +interface MyComponentProps { |
| 152 | + title: string; |
| 153 | +} |
| 154 | + |
| 155 | +export const MyComponent: FC<MyComponentProps> = ({ title }) => { |
| 156 | + return <div>{title}</div>; |
| 157 | +}; |
| 158 | +``` |
| 159 | + |
| 160 | +### Styling |
| 161 | + |
| 162 | +Use Tailwind CSS classes: |
| 163 | + |
| 164 | +```tsx |
| 165 | +<div className="flex items-center justify-center p-4 bg-stellar-primary text-white"> |
| 166 | + Content |
| 167 | +</div> |
| 168 | +``` |
| 169 | + |
| 170 | +### State Management |
| 171 | + |
| 172 | +- Use React hooks for local state |
| 173 | +- Use `useLocalStorage` for persistent state |
| 174 | +- Context API for global state (wallet, swap data) |
| 175 | + |
| 176 | +## Testing |
| 177 | + |
| 178 | +```bash |
| 179 | +# Unit tests (when implemented) |
| 180 | +npm run test |
| 181 | + |
| 182 | +# E2E tests (when implemented) |
| 183 | +npm run test:e2e |
| 184 | +``` |
| 185 | + |
| 186 | +## Deployment |
| 187 | + |
| 188 | +### Vercel (Recommended) |
| 189 | + |
| 190 | +1. Connect your repository to Vercel |
| 191 | +2. Configure environment variables |
| 192 | +3. Deploy |
| 193 | + |
| 194 | +### Docker |
| 195 | + |
| 196 | +```dockerfile |
| 197 | +# Build stage |
| 198 | +FROM node:18-alpine AS builder |
| 199 | +WORKDIR /app |
| 200 | +COPY package*.json ./ |
| 201 | +RUN npm ci |
| 202 | +COPY . . |
| 203 | +RUN npm run build |
| 204 | + |
| 205 | +# Run stage |
| 206 | +FROM node:18-alpine |
| 207 | +WORKDIR /app |
| 208 | +COPY --from=builder /app/.next/standalone ./ |
| 209 | +COPY --from=builder /app/.next/static ./.next/static |
| 210 | +COPY --from=builder /app/public ./public |
| 211 | + |
| 212 | +EXPOSE 3000 |
| 213 | +CMD ["node", "server.js"] |
| 214 | +``` |
| 215 | + |
| 216 | +## Troubleshooting |
| 217 | + |
| 218 | +### Module Not Found |
| 219 | + |
| 220 | +Clear Next.js cache: |
| 221 | +```bash |
| 222 | +rm -rf .next node_modules |
| 223 | +npm install |
| 224 | +``` |
| 225 | + |
| 226 | +### TypeScript Errors |
| 227 | + |
| 228 | +Regenerate type declarations: |
| 229 | +```bash |
| 230 | +npm run build |
| 231 | +``` |
| 232 | + |
| 233 | +### Wallet Connection Issues |
| 234 | + |
| 235 | +- Ensure wallet extension is installed |
| 236 | +- Check network configuration |
| 237 | +- Verify wallet is unlocked |
| 238 | + |
| 239 | +## Contributing |
| 240 | + |
| 241 | +See [CONTRIBUTING.md](../CONTRIBUTING.md) for guidelines. |
| 242 | + |
| 243 | +## License |
| 244 | + |
| 245 | +MIT License - see [LICENSE](../LICENSE) for details. |
0 commit comments