-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: deposit status endpoint and api refactor (#46)
- Loading branch information
1 parent
b9cb3c5
commit 6351fd9
Showing
21 changed files
with
348 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import * as s from "superstruct"; | ||
import { DepositsService } from "../services/deposits"; | ||
import { | ||
HubPoolBalanceParams, | ||
SpokePoolBalanceParams, | ||
} from "../dtos/balances.dto"; | ||
|
||
export class BalancesController { | ||
constructor(private service: DepositsService) {} | ||
|
||
public getHubPoolBalance = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
s.assert(req.query, HubPoolBalanceParams); | ||
return 0; | ||
}; | ||
|
||
public getSpokePoolBalance = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
s.assert(req.query, SpokePoolBalanceParams); | ||
|
||
return 0; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import * as s from "superstruct"; | ||
import { DepositsService } from "../services/deposits"; | ||
import { DepositsParams, DepositParams } from "../dtos/deposits.dto"; | ||
|
||
export class DepositsController { | ||
constructor(private service: DepositsService) {} | ||
|
||
public getDeposits = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
try { | ||
const params = s.create(req.query, DepositsParams); | ||
const result = await this.service.getDeposits(params); | ||
return res.json(result); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
public getDepositStatus = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
try { | ||
const params = s.create(req.query, DepositParams); | ||
const result = await this.service.getDepositStatus(params); | ||
return res.json(result); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./balances"; | ||
export * from "./deposits"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import winston from "winston"; | ||
import { createDataSource, DatabaseConfig } from "@repo/indexer-database"; | ||
|
||
export async function connectToDatabase(databaseConfig: DatabaseConfig) { | ||
export async function connectToDatabase( | ||
databaseConfig: DatabaseConfig, | ||
logger: winston.Logger, | ||
) { | ||
try { | ||
const database = await createDataSource(databaseConfig).initialize(); | ||
logger.info("Postgres connection established"); | ||
return database; | ||
} catch (error) { | ||
console.log("Unable to connect to database", error); | ||
logger.error("Unable to connect to database", error); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as s from "superstruct"; | ||
|
||
// query hub pools by chainId? default to 1 if not specified. will leave option in case of testnets? | ||
export const HubPoolBalanceParams = s.object({ | ||
chainId: s.defaulted(s.number(), 1), | ||
l1Token: s.string(), | ||
}); | ||
|
||
// query spokepools by chainId, must specify | ||
export const SpokePoolBalanceParams = s.object({ | ||
chainId: s.number(), | ||
// unsure why we have timestamp, implies we are storign history of balances? this is in the spec. | ||
timestamp: s.number(), | ||
// unsure why specified as l2Token in spec, don't we have spoke pool on L1? | ||
l2Token: s.optional(s.number()), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as s from "superstruct"; | ||
|
||
const stringToInt = s.coerce(s.number(), s.string(), (value) => | ||
parseInt(value), | ||
); | ||
|
||
export const DepositsParams = s.object({ | ||
depositor: s.optional(s.string()), | ||
recipient: s.optional(s.string()), | ||
inputToken: s.optional(s.string()), | ||
outputToken: s.optional(s.string()), | ||
integrator: s.optional(s.string()), | ||
status: s.optional(s.string()), | ||
// some kind of pagination options, skip could be the start point | ||
skip: s.optional(stringToInt), | ||
// pagination limit, how many to return after the start, note we convert string to number | ||
limit: s.optional(stringToInt), | ||
}); | ||
|
||
export type DepositsParams = s.Infer<typeof DepositsParams>; | ||
|
||
export const DepositParams = s.object({ | ||
depositId: s.optional(stringToInt), | ||
originChainId: s.optional(stringToInt), | ||
depositTxHash: s.optional(s.string()), | ||
relayDataHash: s.optional(s.string()), | ||
index: s.refine( | ||
s.defaulted(stringToInt, 0), | ||
"positiveIndex", | ||
(value) => value >= 0, | ||
), | ||
}); | ||
|
||
export type DepositParams = s.Infer<typeof DepositParams>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { expect } from "chai"; | ||
|
||
describe("main", () => { | ||
it("should return true", async () => { | ||
expect(true).to.be.true; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum HttpStatus { | ||
OK = 200, | ||
BAD_REQUEST = 400, | ||
NOT_FOUND = 404, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Router } from "express"; | ||
import { DataSource } from "@repo/indexer-database"; | ||
import { DepositsController } from "../controllers/deposits"; | ||
import { DepositsService } from "../services/deposits"; | ||
|
||
export function getRouter(db: DataSource): Router { | ||
const router = Router(); | ||
const service = new DepositsService(db); | ||
const controller = new DepositsController(service); | ||
router.get("/deposits", controller.getDeposits); | ||
router.get("/deposit/status", controller.getDepositStatus); | ||
return router; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as deposits from "./deposits"; |
Oops, something went wrong.