Skip to content

Commit

Permalink
backend: server endpoint; check start/end date
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Sep 4, 2024
1 parent 055af5f commit 1bfdff8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"typescript": "^4.6.3"
},
"dependencies": {
"@hop-protocol/fee-refund": "^1.1.86",
"@hop-protocol/fee-refund": "^1.1.87",
"@slack/web-api": "^6.7.2",
"@types/express": "^4.17.13",
"@types/memory-cache": "^0.2.2",
Expand Down
6 changes: 3 additions & 3 deletions backend/src/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { ShardedMerkleTree } from './merkle.js'
import simpleGit_, { SimpleGit } from 'simple-git'
import { Signer, constants, Wallet, BigNumber, Contract, providers } from 'ethers'
import { formatUnits, parseUnits } from 'ethers/lib/utils.js'
import merkleRewardsAbi from './abi/MerkleRewards.json' assert { type: "json" }
import tokenAbi from './abi/ERC20.json' assert { type: "json" }
import merkleRewardsAbi from './abi/MerkleRewards.json' assert { type: 'json' }
import tokenAbi from './abi/ERC20.json' assert { type: 'json' }
import { forumPost } from './forumPost.js'
import { DateTime } from 'luxon'
import { config } from './config.js'
Expand Down Expand Up @@ -857,7 +857,7 @@ Parameters:
const oneDay = oneHour * 24
const defaultTimeMs = oneDay
if (!this.checkpointIntervalMs) {
console.log('this.checkpointIntervalMs not set')
console.log('"this.checkpointIntervalMs" not set, returning defaultTimeMs (OneDay) for getRemainingTimeTilCheckpoint')
return defaultTimeMs
}

Expand Down
8 changes: 4 additions & 4 deletions backend/src/cli/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function main (options: any) {

if (options.server) {
console.log('starting server')
startServer()
startServer(options)
}

const { controller } = await import('../instance.js')
Expand All @@ -64,10 +64,10 @@ async function main (options: any) {
}

const lastRepoCheckpointDate = DateTime.fromMillis(lastRepoCheckpointMs)
const lastDate = DateTime.fromSeconds(Number(options.doLastCheckpoint))
const endDateForLastCheckpoint = DateTime.fromSeconds(Number(options.doLastCheckpoint))

const diffInDays = lastDate.diff(lastRepoCheckpointDate, 'days').days
console.log(diffInDays, 'days difference')
const diffInDays = endDateForLastCheckpoint.diff(lastRepoCheckpointDate, 'days').days
console.log(diffInDays, 'days difference between lastRepoCheckpointDate and endDateForLastCheckpoint')

if (Math.abs(diffInDays) <= 2) {
console.log('last checkpoint is within 2 days, exiting worker')
Expand Down
17 changes: 16 additions & 1 deletion backend/src/instances/feeRefund/server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { responseCache } from '../../responseCache.js'
import { feeRefund } from './instance.js'

export function setAdditionalRoutes (app: any, middlewares: any) {
export function setAdditionalRoutes (app: any, middlewares: any, options: any = {}) {
const { ipRateLimitMiddleware } = middlewares
app.get('/v1/refund-amount', ipRateLimitMiddleware, responseCache, async (req: any, res: any) => {
if (options.startTimestamp) {
const now = Math.floor(Date.now() / 1000)
if (now < Number(options.startTimestamp)) {
res.status(400).json({ error: 'Rewards cycle has not started.' })
return
}
}
if (options.endTimestamp) {
const now = Math.floor(Date.now() / 1000)
if (now > Number(options.endTimestamp)) {
res.status(400).json({ error: 'Rewards cycle has ended.' })
return
}
}

try {
const {
gasCost,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getAddress } from 'ethers/lib/utils.js'
import { responseCache } from './responseCache.js'
import { DateTime } from 'luxon'

export async function startServer () {
export async function startServer (options: any = {}) {
const { controller, setAdditionalRoutes } = await import('./instance.js')
const app = express()

Expand Down Expand Up @@ -130,7 +130,7 @@ export async function startServer () {
}
})

setAdditionalRoutes(app, { ipRateLimitMiddleware })
setAdditionalRoutes(app, { ipRateLimitMiddleware }, options)

const host = '0.0.0.0'
app.listen(port, host, () => {
Expand Down

0 comments on commit 1bfdff8

Please sign in to comment.