Skip to content

Commit afaa41f

Browse files
authored
Merge pull request #79 from VoiceDeck/dev
hot fix 1.0.5.2
2 parents db091d8 + a167b91 commit afaa41f

File tree

4 files changed

+68
-35
lines changed

4 files changed

+68
-35
lines changed

app/api/contributions/route.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { type NextRequest, NextResponse } from "next/server";
33

44
export async function POST(req: NextRequest) {
55
try {
6-
const { txId, hypercertId, amount, comment } = await req.json();
7-
if (!txId || !hypercertId || !amount) {
6+
const { sender, txId, hypercertId, amount, comment } = await req.json();
7+
if (!sender || !txId || !hypercertId || !amount) {
88
return NextResponse.json(
99
{ error: "Missing required fields" },
1010
{ status: 400 },
1111
);
1212
}
1313
const result = await processNewContribution(
14+
sender,
1415
txId,
1516
hypercertId,
1617
amount,
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { type NextRequest, NextResponse } from "next/server";
2+
3+
import { getViemClient, removeContribution } from "@/lib/directus";
4+
5+
export async function POST(req: NextRequest) {
6+
try {
7+
const { txId } = await req.json();
8+
if (!txId) {
9+
return NextResponse.json(
10+
{ error: "Missing required fields" },
11+
{ status: 400 },
12+
);
13+
}
14+
15+
const viemClient = getViemClient();
16+
17+
// wait for the transaction to be included in a block
18+
console.log(
19+
`[Viem] waiting for tx ${txId} to be included in a block . . .`,
20+
);
21+
const txReceipt = await viemClient.waitForTransactionReceipt({
22+
hash: txId,
23+
});
24+
console.log(`[Viem] tx ${txId} included in block ${txReceipt.blockNumber}`);
25+
26+
if (txReceipt.status === "reverted") {
27+
console.log(`[Viem] tx ${txId} reverted, remove from CMS . . .`);
28+
29+
await removeContribution(txId);
30+
return NextResponse.json({ txStatus: "deleted" }, { status: 200 });
31+
}
32+
return NextResponse.json({ txStatus: "ok" }, { status: 200 });
33+
} catch (error) {
34+
let errorMessage = "An unknown error occurred";
35+
if (typeof error === "object" && error !== null) {
36+
errorMessage = (error as { message?: string }).message ?? errorMessage;
37+
} else if (typeof error === "string") {
38+
errorMessage = error;
39+
}
40+
return NextResponse.json({ error: errorMessage }, { status: 500 });
41+
}
42+
}

hooks/use-buy-fraction.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ const useHandleBuyFraction = (
3939
throw new Error("No order found");
4040
}
4141

42-
// if I enter 1 USD to buy in the UI, the amount will be 1000000000000000n
43-
// amount: 1000000000000000n (10^15)
44-
// pricePerUnit: 1
4542
console.log({ order, amount, address, hypercertId, comment });
46-
// print order.price
4743
console.log(order.price);
4844
const takerOrder = hypercertExhangeClient.createFractionalSaleTakerBid(
4945
order,
@@ -72,6 +68,7 @@ const useHandleBuyFraction = (
7268
"Content-Type": "application/json",
7369
},
7470
body: JSON.stringify({
71+
sender: address,
7572
txId: tx.hash as `0x${string}`,
7673
hypercertId: hypercertId,
7774
amount: amountInDollars,

lib/directus.ts

+22-29
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type RestClient,
77
createDirectus,
88
createItem,
9+
deleteItem,
910
readItem,
1011
readItems,
1112
rest,
@@ -47,6 +48,7 @@ const contributionsMutex = new Mutex();
4748
* @param comment The comment of the contributor to the contribution.
4849
*/
4950
export async function processNewContribution(
51+
sender: Address,
5052
txId: Hash,
5153
hypercertId: string,
5254
amount: number,
@@ -56,7 +58,6 @@ export async function processNewContribution(
5658
const client = getDirectusClient();
5759

5860
// check if the transaction is already processed
59-
// TODO: This is failing build @baumstern
6061
const response = await client.request(
6162
readItems("contributions", {
6263
fields: ["txid"],
@@ -68,33 +69,8 @@ export async function processNewContribution(
6869
})
6970
);
7071

71-
// if the transaction is already processed, do not create a contribution
72-
if (response.length > 0) {
73-
console.log(
74-
`[Directus] tx ${txId} already processed, skipping contribution creation in Directus`
75-
);
76-
return;
77-
}
78-
79-
// wait for the transaction to be included in a block
80-
console.log(
81-
`[Viem] waiting for tx ${txId} to be included in a block . . .`
82-
);
83-
const txReceipt = await getViemClient().waitForTransactionReceipt({
84-
hash: txId,
85-
});
86-
console.log(`[Viem] tx ${txId} included in block ${txReceipt.blockNumber}`);
87-
88-
// if the transaction is reverted, do not create a contribution
89-
if (txReceipt.status === "reverted") {
90-
console.log(
91-
`[Viem] tx ${txId} reverted, skipping contribution creation in Directus`
92-
);
93-
return;
94-
}
95-
9672
const contribution = {
97-
sender: getAddress(txReceipt.from),
73+
sender: getAddress(sender),
9874
hypercert_id: hypercertId,
9975
amount: amount,
10076
txid: txId,
@@ -153,6 +129,23 @@ export async function createContribution(contribution: Contribution) {
153129
}
154130
}
155131

132+
export async function removeContribution(txid: Hash) {
133+
134+
const client = getDirectusClient();
135+
136+
try {
137+
console.log(`[Directus] remove contribution of tx ${txid} . . .`);
138+
139+
await client.request(deleteItem("contributions", txid));
140+
console.log(
141+
`[Directus] contribution of tx ${txid} removed successfully`
142+
);
143+
} catch (error) {
144+
console.error("[Directus] failed to remove contribution: ", error);
145+
throw new Error(`[Directus] failed to remove contribution: ${error}`);
146+
}
147+
}
148+
156149
/**
157150
* Fetches the contents of the CMS `reports` collection.
158151
* @returns A promise that resolves to an array of CMS contents.
@@ -436,10 +429,10 @@ export const getViemClient = (): PublicClient => {
436429
if (viemClient) {
437430
return viemClient;
438431
}
439-
432+
440433
viemClient = createPublicClient({
441434
chain: sepolia,
442-
transport: http(),
435+
transport: http(process.env.JSON_RPC_ENDPOINT ? process.env.JSON_RPC_ENDPOINT : undefined),
443436
});
444437

445438
return viemClient;

0 commit comments

Comments
 (0)