Skip to content

Commit

Permalink
feat: add a fiat contribution handler in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
baumstern committed Nov 6, 2024
1 parent d5146ae commit 2035dda
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/api/contributions/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { processNewContribution } from "@/lib/directus";
import { processNewCryptoContribution } from "@/lib/directus";
import { type NextRequest, NextResponse } from "next/server";

export async function POST(req: NextRequest) {
Expand All @@ -10,7 +10,7 @@ export async function POST(req: NextRequest) {
{ status: 400 },
);
}
const result = await processNewContribution(
const result = await processNewCryptoContribution(
sender,
txId,
hypercertId,
Expand Down
63 changes: 59 additions & 4 deletions lib/directus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,51 @@ const users: { [address: Address]: string } = {};
const contributionsByHCId: { [hypercertId: string]: Contribution[] } = {};
const contributionsMutex = new Mutex();


/**
* Processes a new contribution by waiting for a transaction to be included in a block,
* 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,
* checking the transaction status, and creating a contribution record if successful.
*
* @param txId The hash of the transaction to be processed.
* @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 processNewContribution(
export async function processNewCryptoContribution(
sender: Address,
txId: Hash,
hypercertId: string,
Expand Down Expand Up @@ -78,7 +113,7 @@ export async function processNewContribution(
comment: comment,
} as Contribution;
// create a contribution record in Directus
await createContribution(contribution);
await createCryptoContribution(contribution);

// update the funded amount of the hypercert in server memory
await updateFundedAmount(hypercertId, amount);
Expand All @@ -90,7 +125,27 @@ export async function processNewContribution(
}
}

export async function createContribution(contribution: Contribution) {
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) {
const user = {
address: contribution.sender,
};
Expand Down

0 comments on commit 2035dda

Please sign in to comment.