Skip to content

Commit

Permalink
Add a POST /deposits route to persist reveal info
Browse files Browse the repository at this point in the history
  • Loading branch information
beaurancourt committed Mar 20, 2024
1 parent 7fa63df commit 6e5d521
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
34 changes: 33 additions & 1 deletion infrastructure/tbtc-deposit-reveals/src/controllers/deposits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function getDepositsForAddress(
`
SELECT
address,
reveal_info as reveal_info,
reveal_info,
metadata,
application,
CAST(strftime('%s', inserted_at) as INT) as inserted_at
Expand All @@ -56,3 +56,35 @@ export async function getDepositsForAddress(
}
})
}

export type SaveDepositRequest = {
address: string
revealInfo: DepositReceipt
metadata: Object
application: string
}

export async function saveDeposit(
request: SaveDepositRequest & IRequest,
env: Env,
): Promise<{ success: boolean }> {
const saveDepositInfo: SaveDepositRequest = request.content
const { address, revealInfo, metadata, application } = saveDepositInfo
const result = await env.DB.prepare(
`
INSERT INTO reveals
(address, reveal_info, metadata, application)
VALUES
(?1, ?2, ?3, ?4);
`,
)
.bind(
address,
JSON.stringify(revealInfo),
JSON.stringify(metadata),
application,
)
.run()

return { success: result.success }
}
7 changes: 6 additions & 1 deletion infrastructure/tbtc-deposit-reveals/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
RouteHandler,
} from "itty-router"
import { Env } from "#/types"
import { getDepositsForAddress } from "./controllers/deposits"
import {
getDepositsForAddress,
saveDeposit,
SaveDepositRequest,
} from "./controllers/deposits"

export const { preflight, corsify } = createCors({
origins: (_: string) => true,
Expand Down Expand Up @@ -56,6 +60,7 @@ router
withContent,
)
.get("/deposits/:address", getDepositsForAddress)
.post<SaveDepositRequest & IRequest>("/deposits", saveDeposit)
.all("*", () => error(404, "No home route."))

export default router

0 comments on commit 6e5d521

Please sign in to comment.