Skip to content

Commit

Permalink
add POST/ syncPool
Browse files Browse the repository at this point in the history
  • Loading branch information
thelostone-mc committed Jan 9, 2025
1 parent 294e757 commit 515e103
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 8 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,8 @@
```
Needed API calls
----------------
POST/ pool (create and sync pool)
POST/ pool/distribution/predict ( prediction distribution )
Admin calls
-----------
POST/ application-metric-scores
Nice to have
-------------
POST/ pool/distribution/calculate (this will be internally called when vote is called)
Expand Down
58 changes: 56 additions & 2 deletions src/controllers/poolController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import poolService from '@/service/PoolService';
import applicationService from '@/service/ApplicationService';
import { catchError, validateRequest } from '@/utils';
import { createLogger } from '@/logger';
import { type RoundWithApplications as IndexerRoundWithApplications } from '@/ext/indexer';
import {
indexerClient,
type RoundWithApplications as IndexerRoundWithApplications,
} from '@/ext/indexer';

import { BadRequestError, IsNullError } from '@/errors';
import { BadRequestError, IsNullError, NotFoundError } from '@/errors';
import { EligibilityType } from '@/entity/EligibilityCriteria';

const logger = createLogger();
Expand All @@ -18,6 +21,11 @@ interface CreatePoolRequest {
eligibilityData: object;
}

interface SyncPoolRequest {
chainId: number;
alloPoolId: string;
}

/**
* Creates a pool
*
Expand Down Expand Up @@ -72,6 +80,52 @@ export const createPool = async (
res.status(200).json({ message: 'pool created successfully' });
};

/**
* Synchronizes a pool by fetching data from the indexer, updating the pool and it's applications
*
* @param req - Express request object
* @param res - Express response object
*/
export const syncPool = async (req: Request, res: Response): Promise<void> => {
// Validate the incoming request
validateRequest(req, res);

// Extract chainId and alloPoolId from the request body
const { chainId, alloPoolId } = req.body as SyncPoolRequest;

// Log the receipt of the update request
logger.info(
`Received update request for chainId: ${chainId}, alloPoolId: ${alloPoolId}`
);

// ---- Fetch pool data from the indexer ----
const [errorFetching, indexerPoolData] = await catchError(
indexerClient.getRoundWithApplications({
chainId,
roundId: alloPoolId,
})
);

// Handle errors or missing data from the indexer
if (errorFetching != null || indexerPoolData == null) {
logger.warn(
`No pool found for chainId: ${chainId}, alloPoolId: ${alloPoolId}`
);
res.status(404).json({ message: 'Pool not found on indexer' });
throw new NotFoundError(`Pool not found on indexer`);
}

// ---- Update Applications ----
// Update the pool with the applications from the indexer
await updateApplications(chainId, alloPoolId, indexerPoolData);

// Log success and respond to the request
logger.info(
`successfully synced pool, alloPoolId: ${alloPoolId} chainId: ${chainId}`
);
res.status(200).json({ message: 'pool synced successfully' });
};

const updateApplications = async (
chainId: number,
alloPoolId: string,
Expand Down
35 changes: 34 additions & 1 deletion src/routes/poolRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPool } from '@/controllers/poolController';
import { createPool, syncPool } from '@/controllers/poolController';
import { Router } from 'express';

const router = Router();
Expand Down Expand Up @@ -41,4 +41,37 @@ const router = Router();
*/
router.post('/', createPool);

/**
* @swagger
* /sync:
* post:
* summary: Syncs a pool with the given alloPoolId and chainId
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* alloPoolId:
* type: string
* description: The ID of the pool to sync
* example: "609" # Example of poolId
* chainId:
* type: number
* description: The chain ID associated with the pool
* example: 42161 # Example of chainId (Arbitrum)
* required:
* - alloPoolId
* - chainId
* responses:
* 200:
* description: Pool synced successfully
* 400:
* description: Invalid poolId or chainId format
* 500:
* description: Internal server error
*/
router.post('/sync', syncPool);

export default router;

0 comments on commit 515e103

Please sign in to comment.