Skip to content

Commit

Permalink
Merge pull request #79 from VoiceDeck/dev
Browse files Browse the repository at this point in the history
hot fix 1.0.5.2
  • Loading branch information
thebeyondr authored Mar 28, 2024
2 parents db091d8 + a167b91 commit afaa41f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 35 deletions.
5 changes: 3 additions & 2 deletions app/api/contributions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { type NextRequest, NextResponse } from "next/server";

export async function POST(req: NextRequest) {
try {
const { txId, hypercertId, amount, comment } = await req.json();
if (!txId || !hypercertId || !amount) {
const { sender, txId, hypercertId, amount, comment } = await req.json();
if (!sender || !txId || !hypercertId || !amount) {
return NextResponse.json(
{ error: "Missing required fields" },
{ status: 400 },
);
}
const result = await processNewContribution(
sender,
txId,
hypercertId,
amount,
Expand Down
42 changes: 42 additions & 0 deletions app/api/contributions/validate/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { type NextRequest, NextResponse } from "next/server";

import { getViemClient, removeContribution } from "@/lib/directus";

export async function POST(req: NextRequest) {
try {
const { txId } = await req.json();
if (!txId) {
return NextResponse.json(
{ error: "Missing required fields" },
{ status: 400 },
);
}

const viemClient = getViemClient();

// wait for the transaction to be included in a block
console.log(
`[Viem] waiting for tx ${txId} to be included in a block . . .`,
);
const txReceipt = await viemClient.waitForTransactionReceipt({
hash: txId,
});
console.log(`[Viem] tx ${txId} included in block ${txReceipt.blockNumber}`);

if (txReceipt.status === "reverted") {
console.log(`[Viem] tx ${txId} reverted, remove from CMS . . .`);

await removeContribution(txId);
return NextResponse.json({ txStatus: "deleted" }, { status: 200 });
}
return NextResponse.json({ txStatus: "ok" }, { status: 200 });
} catch (error) {
let errorMessage = "An unknown error occurred";
if (typeof error === "object" && error !== null) {
errorMessage = (error as { message?: string }).message ?? errorMessage;
} else if (typeof error === "string") {
errorMessage = error;
}
return NextResponse.json({ error: errorMessage }, { status: 500 });
}
}
5 changes: 1 addition & 4 deletions hooks/use-buy-fraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ const useHandleBuyFraction = (
throw new Error("No order found");
}

// if I enter 1 USD to buy in the UI, the amount will be 1000000000000000n
// amount: 1000000000000000n (10^15)
// pricePerUnit: 1
console.log({ order, amount, address, hypercertId, comment });
// print order.price
console.log(order.price);
const takerOrder = hypercertExhangeClient.createFractionalSaleTakerBid(
order,
Expand Down Expand Up @@ -72,6 +68,7 @@ const useHandleBuyFraction = (
"Content-Type": "application/json",
},
body: JSON.stringify({
sender: address,
txId: tx.hash as `0x${string}`,
hypercertId: hypercertId,
amount: amountInDollars,
Expand Down
51 changes: 22 additions & 29 deletions lib/directus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type RestClient,
createDirectus,
createItem,
deleteItem,
readItem,
readItems,
rest,
Expand Down Expand Up @@ -47,6 +48,7 @@ const contributionsMutex = new Mutex();
* @param comment The comment of the contributor to the contribution.
*/
export async function processNewContribution(
sender: Address,
txId: Hash,
hypercertId: string,
amount: number,
Expand All @@ -56,7 +58,6 @@ export async function processNewContribution(
const client = getDirectusClient();

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

// if the transaction is already processed, do not create a contribution
if (response.length > 0) {
console.log(
`[Directus] tx ${txId} already processed, skipping contribution creation in Directus`
);
return;
}

// wait for the transaction to be included in a block
console.log(
`[Viem] waiting for tx ${txId} to be included in a block . . .`
);
const txReceipt = await getViemClient().waitForTransactionReceipt({
hash: txId,
});
console.log(`[Viem] tx ${txId} included in block ${txReceipt.blockNumber}`);

// if the transaction is reverted, do not create a contribution
if (txReceipt.status === "reverted") {
console.log(
`[Viem] tx ${txId} reverted, skipping contribution creation in Directus`
);
return;
}

const contribution = {
sender: getAddress(txReceipt.from),
sender: getAddress(sender),
hypercert_id: hypercertId,
amount: amount,
txid: txId,
Expand Down Expand Up @@ -153,6 +129,23 @@ export async function createContribution(contribution: Contribution) {
}
}

export async function removeContribution(txid: Hash) {

const client = getDirectusClient();

try {
console.log(`[Directus] remove contribution of tx ${txid} . . .`);

await client.request(deleteItem("contributions", txid));
console.log(
`[Directus] contribution of tx ${txid} removed successfully`
);
} catch (error) {
console.error("[Directus] failed to remove contribution: ", error);
throw new Error(`[Directus] failed to remove contribution: ${error}`);
}
}

/**
* Fetches the contents of the CMS `reports` collection.
* @returns A promise that resolves to an array of CMS contents.
Expand Down Expand Up @@ -436,10 +429,10 @@ export const getViemClient = (): PublicClient => {
if (viemClient) {
return viemClient;
}

viemClient = createPublicClient({
chain: sepolia,
transport: http(),
transport: http(process.env.JSON_RPC_ENDPOINT ? process.env.JSON_RPC_ENDPOINT : undefined),
});

return viemClient;
Expand Down

0 comments on commit afaa41f

Please sign in to comment.