Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api fetch projectById #3

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 147 additions & 1 deletion packages/nextjs/contracts/deployedContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,152 @@
*/
import { GenericContractsDeclaration } from "~~/utils/scaffold-eth/contract";

const deployedContracts = {} as const;
const deployedContracts = {
31337: {
YourContract: {
address: "0x5FbDB2315678afecb367f032d93F642f64180aa3",
abi: [
{
inputs: [
{
internalType: "address",
name: "_owner",
type: "address",
},
],
stateMutability: "nonpayable",
type: "constructor",
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "greetingSetter",
type: "address",
},
{
indexed: false,
internalType: "string",
name: "newGreeting",
type: "string",
},
{
indexed: false,
internalType: "bool",
name: "premium",
type: "bool",
},
{
indexed: false,
internalType: "uint256",
name: "value",
type: "uint256",
},
],
name: "GreetingChange",
type: "event",
},
{
inputs: [],
name: "greeting",
outputs: [
{
internalType: "string",
name: "",
type: "string",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "owner",
outputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "premium",
outputs: [
{
internalType: "bool",
name: "",
type: "bool",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "string",
name: "_newGreeting",
type: "string",
},
],
name: "setGreeting",
outputs: [],
stateMutability: "payable",
type: "function",
},
{
inputs: [],
name: "totalCounter",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
name: "userGreetingCounter",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "withdraw",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
{
stateMutability: "payable",
type: "receive",
},
],
inheritedFunctions: {},
},
},
} as const;

export default deployedContracts satisfies GenericContractsDeclaration;
1 change: 1 addition & 0 deletions packages/nextjs/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
2 changes: 2 additions & 0 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"@uniswap/sdk-core": "^4.0.1",
"@uniswap/v2-sdk": "^3.0.1",
"blo": "^1.0.1",
"csv-parser": "^3.0.0",
"daisyui": "^4.4.19",
"fs": "^0.0.1-security",
"next": "^14.0.4",
"nprogress": "^0.2.0",
"qrcode.react": "^3.1.0",
Expand Down
54 changes: 54 additions & 0 deletions packages/nextjs/pages/api/projects/[id].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import csv from "csv-parser";
import fs from "fs";
import type { NextApiRequest, NextApiResponse } from "next";
import path from "path";

const projectsFilePath = path.join(process.cwd(), "public", "csv/RetroPGF3 Results - ConsolidatedData.csv");

interface Project {
Project_ID: string;
}

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== "GET") {
return res.status(405).json({ message: "Method not allowed." });
}

const { id } = req.query;

try {
const project = await getProjectById(String(id));

if (project) {
res.status(200).json(project);
} else {
res.status(404).json({ message: "Project not found" });
}
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal Server Error" });
}
}

async function getProjectById(id: string) {
return new Promise((resolve, reject) => {
const projects: Project[] = [];

fs.createReadStream(projectsFilePath)
.pipe(csv())
.on("data", row => {
projects.push(row);
})
.on("end", () => {
const project = projects.find(p => p.Project_ID === id);
if (project) {
resolve(project);
} else {
resolve(null);
}
})
.on("error", error => {
reject(error);
});
});
}
Loading
Loading