Skip to content
Open
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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# dependencies
/node_modules
/.pnp
.pnp.js

# dependencies (bun install)
node_modules

Expand Down Expand Up @@ -47,4 +52,4 @@ lerna-debug.log*
*.ntvs*
*.njsproj
*.sln
*.sw?
*.sw?
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
# Chain Indexer Notification and Leaderboard Builder API

This API notifies a blockchain indexer about new submissions and triggers a leaderboard builder.

## Features

- Notify a chain indexer with submission data
- Trigger a leaderboard update
- Modular design with clear service responsibilities

## Prerequisites

- Node.js (v16+)
- npm or yarn

## Installation

1. Clone the repository:
```bash
git clone https://github.com/yourusername/backend.git
cd chain-notifier
```

2. Install dependencies:
npm install express
```

3. Create a `.env` file in the root directory for environment variables if required.

## Usage

1. Start the server:
node app.js
```

2. Use the `/api/notify` endpoint to notify the indexer and trigger the leaderboard.

### Example Request
**Endpoint**: `POST /api/notify`

**Body**:
```json
{
"submissionData": {
"userId": "05421",
"submissionId": "fghij",
"score": 40
}
}
=======
# DeCleanup Network Backend

Backend API for the DeCleanup Network application, providing wallet-based
Expand Down
19 changes: 19 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require('express');
const apiRoutes = require('./routes/apiRoutes');

const app = express();
app.use(express.json());

// Use API routes
app.use('/api', apiRoutes);

// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Something went wrong!' });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
27 changes: 27 additions & 0 deletions controllers/submissionController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { notifyIndexerService } = require('../services/indexerService');
const { updateLeaderboard } = require('../services/leaderboardService');

exports.notifyIndexer = async (req, res) => {
try {
const { submissionData } = req.body;

if (!submissionData) {
return res.status(400).json({ error: 'Submission data is required' });
}

// Notify the chain indexer
const indexerResponse = await notifyIndexerService(submissionData);

// Trigger the leaderboard builder
const leaderboardResponse = await updateLeaderboard();

res.status(200).json({
message: 'Indexer notified and leaderboard updated successfully.',
indexerResponse,
leaderboardResponse,
});
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to process the request.' });
}
};
75 changes: 74 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"dependencies": {
"express": "^4.21.2"
}
"name": "decleanup-network_backend",
"module": "index.ts",
"type": "module",
Expand Down
8 changes: 8 additions & 0 deletions routes/apiRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const { notifyIndexer } = require('../controllers/submissionController');
const router = express.Router();

// Endpoint to notify the indexer and trigger leaderboard update
router.post('/notify', notifyIndexer);

module.exports = router;
12 changes: 12 additions & 0 deletions services/indexerService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports.notifyIndexerService = async (submissionData) => {
try {
// Simulate notifying the blockchain indexer
console.log('Notifying chain indexer with submission:', submissionData);

// Simulate a successful response
return { status: 'success', message: 'Indexer notified.' };
} catch (error) {
console.error('Error notifying indexer:', error);
throw new Error('Failed to notify indexer.');
}
};
12 changes: 12 additions & 0 deletions services/leaderboardService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports.updateLeaderboard = async () => {
try {
// Simulate leaderboard rebuilding
console.log('Rebuilding leaderboard...');

// Simulate a successful response
return { status: 'success', message: 'Leaderboard updated.' };
} catch (error) {
console.error('Error updating leaderboard:', error);
throw new Error('Failed to update leaderboard.');
}
};