From 3dee3bf40a456cef991f58a0454e73d66c11d033 Mon Sep 17 00:00:00 2001 From: Daehyun Paik Date: Thu, 7 Nov 2024 14:27:20 +0700 Subject: [PATCH 1/2] Revert "feat: support fiat contribution in REST API" This reverts commit ec94ad2cbc73c4af5ae8faa64a1293df5d16e4cf. --- app/api/contributions/route.ts | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/app/api/contributions/route.ts b/app/api/contributions/route.ts index 0ac17ab..2a0ef44 100644 --- a/app/api/contributions/route.ts +++ b/app/api/contributions/route.ts @@ -1,37 +1,23 @@ -import { processNewCryptoContribution, processNewFiatContribution } from "@/lib/directus"; +import { processNewCryptoContribution } from "@/lib/directus"; import { type NextRequest, NextResponse } from "next/server"; export async function POST(req: NextRequest) { try { const { sender, txId, hypercertId, amount, comment } = await req.json(); - - if (!hypercertId || !amount) { + if (!sender || !txId || !hypercertId || !amount) { return NextResponse.json( { error: "Missing required fields" }, { status: 400 }, ); } - - if (sender && txId) { - const result = await processNewCryptoContribution( - sender, - txId, - hypercertId, - amount, - comment, - ); - return NextResponse.json(result); - } else { - // when sender and txId field is omitted - // then, assuming it's a fiat contribution - const result = await processNewFiatContribution( - hypercertId, - amount, - comment, - ); - return NextResponse.json(result); - } - + const result = await processNewCryptoContribution( + sender, + txId, + hypercertId, + amount, + comment, + ); + return NextResponse.json(result); } catch (error) { let errorMessage = "An unknown error occurred"; if (typeof error === "object" && error !== null) { From 93592278e08bed3411890f774d9f37144dd0f47d Mon Sep 17 00:00:00 2001 From: Daehyun Paik Date: Thu, 7 Nov 2024 14:27:46 +0700 Subject: [PATCH 2/2] Revert "feat: add a fiat contribution handler in backend" This reverts commit 2035dda2ecb495e32bef83aa3ddaf88a27420a96. --- app/api/contributions/route.ts | 4 +-- lib/directus.ts | 63 +++------------------------------- 2 files changed, 6 insertions(+), 61 deletions(-) diff --git a/app/api/contributions/route.ts b/app/api/contributions/route.ts index 2a0ef44..6a8ef18 100644 --- a/app/api/contributions/route.ts +++ b/app/api/contributions/route.ts @@ -1,4 +1,4 @@ -import { processNewCryptoContribution } from "@/lib/directus"; +import { processNewContribution } from "@/lib/directus"; import { type NextRequest, NextResponse } from "next/server"; export async function POST(req: NextRequest) { @@ -10,7 +10,7 @@ export async function POST(req: NextRequest) { { status: 400 }, ); } - const result = await processNewCryptoContribution( + const result = await processNewContribution( sender, txId, hypercertId, diff --git a/lib/directus.ts b/lib/directus.ts index 962b02c..dbcd84c 100644 --- a/lib/directus.ts +++ b/lib/directus.ts @@ -38,43 +38,8 @@ const users: { [address: Address]: string } = {}; const contributionsByHCId: { [hypercertId: string]: Contribution[] } = {}; const contributionsMutex = new Mutex(); - /** - * Processes a new fiat contribution - * - * @param hypercertId The identifier of the hypercert associated with the contribution. - * @param amount The amount of the contribution. - * @param comment The comment of the contributor to the contribution. - */ -export async function processNewFiatContribution( - hypercertId: string, - amount: number, - comment?: string -) { - try { - const client = getDirectusClient(); - - const contribution = { - hypercert_id: hypercertId.toLowerCase(), - amount: amount, - date_created: new Date().toISOString(), - comment: comment, - } as Contribution; - // create a contribution record in Directus - await createFiatContribution(contribution); - - // update the funded amount of the hypercert in server memory - await updateFundedAmount(hypercertId, amount); - // add the contribution to the cache - await updateContribution(hypercertId, contribution); - } catch (error) { - console.error(`[server] failed to process new contribution: ${error}`); - throw new Error(`[server] failed to process new contribution: ${error}`); - } -} - -/** - * Processes a new crypto contribution by waiting for a transaction to be included in a block, + * Processes a new contribution by waiting for a transaction to be included in a block, * checking the transaction status, and creating a contribution record if successful. * * @param txId The hash of the transaction to be processed. @@ -82,7 +47,7 @@ export async function processNewFiatContribution( * @param amount The amount of the contribution. * @param comment The comment of the contributor to the contribution. */ -export async function processNewCryptoContribution( +export async function processNewContribution( sender: Address, txId: Hash, hypercertId: string, @@ -113,7 +78,7 @@ export async function processNewCryptoContribution( comment: comment, } as Contribution; // create a contribution record in Directus - await createCryptoContribution(contribution); + await createContribution(contribution); // update the funded amount of the hypercert in server memory await updateFundedAmount(hypercertId, amount); @@ -125,27 +90,7 @@ export async function processNewCryptoContribution( } } -export async function createFiatContribution(contribution: Contribution) { - const client = getDirectusClient(); - - try { - console.log(`[Directus] creating fiat contribution . . .`); - console.log(` - hypercert_id: ${contribution.hypercert_id}`); - console.log(` - amount: ${contribution.amount}`); - console.log( - ` - comment exists: ${contribution.comment ? "true" : "false"}` - ); - await client.request(createItem("contributions", contribution)); - console.log( - `[Directus] fiat contribution created successfully` - ); - } catch (error) { - console.error("[Directus] failed to create contribution: ", error); - throw new Error(`[Directus] failed to create contribution: ${error}`); - } -} - -export async function createCryptoContribution(contribution: Contribution) { +export async function createContribution(contribution: Contribution) { const user = { address: contribution.sender, };