Skip to content

Commit

Permalink
add kind donors
Browse files Browse the repository at this point in the history
  • Loading branch information
kualta committed Oct 12, 2024
1 parent 9ecc452 commit c0be584
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 54 deletions.
Empty file removed src/components/Dontations.tsx
Empty file.
82 changes: 44 additions & 38 deletions src/components/TopDonors.tsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,65 @@
import axios from "axios";
import { ethers } from "ethers";
import type React from "react";
import { useEffect, useState } from "react";
import Link from "next/link";
import { env } from "~/env.mjs";

interface Donor {
address: string;
totalAmount: string;
}

const DONOR_WALLET = env.DONOR_WALLET
const ETHERSCAN_API_KEY = env.ETHERSCAN_API_KEY
const DONOR_WALLET = env.DONOR_WALLET;
const ETHERSCAN_API_KEY = env.ETHERSCAN_API_KEY;

export const TopDonors = () => {
const [topDonors, setTopDonors] = useState<Donor[]>([]);
export const revalidate = 3600;

useEffect(() => {
fetchTopDonors();
}, []);
async function fetchTopDonors(): Promise<Donor[]> {
try {
const response = await fetch(
`https://api.etherscan.io/api?module=account&action=txlist&address=${DONOR_WALLET}&startblock=0&endblock=99999999&sort=desc&apikey=${ETHERSCAN_API_KEY}`,
{ next: { revalidate: 3600 } },
);

const fetchTopDonors = async () => {
try {
const response = await axios.get(
`https://api.etherscan.io/api?module=account&action=txlist&address=${DONOR_WALLET}&startblock=0&endblock=99999999&sort=desc&apikey=${ETHERSCAN_API_KEY}`,
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const transactions = response.data.result;
const data = await response.json();
const transactions = data.result;

const donorMap = new Map<string, ethers.BigNumber>();
const donorMap = new Map<string, ethers.BigNumber>();

for (const tx of transactions) {
if (tx.to.toLowerCase() === DONOR_WALLET.toLowerCase()) {
const amount = ethers.BigNumber.from(tx.value);
const currentTotal = donorMap.get(tx.from) || ethers.BigNumber.from(0);
donorMap.set(tx.from, currentTotal.add(amount));
}
for (const tx of transactions) {
if (tx.to.toLowerCase() === DONOR_WALLET.toLowerCase()) {
const amount = ethers.BigNumber.from(tx.value);
const currentTotal = donorMap.get(tx.from) || ethers.BigNumber.from(0);
donorMap.set(tx.from, currentTotal.add(amount));
}
}

const sortedDonors = Array.from(donorMap.entries())
.sort((a, b) => (b[1].gt(a[1]) ? 1 : -1))
.slice(0, 5)
.map(([address, amount]) => ({
address,
totalAmount: ethers.utils.formatEther(amount),
}));
const sortedDonors = Array.from(donorMap.entries())
.sort((a, b) => (b[1].gt(a[1]) ? 1 : -1))
.slice(0, 5)
.map(([address, amount]) => ({
address,
totalAmount: ethers.utils.formatEther(amount),
}));

setTopDonors(sortedDonors);
} catch (error) {
console.error("Error fetching top donors:", error);
}
};
return sortedDonors;
} catch (error) {
console.error("Error fetching top donors:", error);
return [];
}
}

export async function TopDonors() {
const topDonors = await fetchTopDonors();

return (
<div className="bg-white shadow-md rounded-lg p-6">
<h2 className="text-2xl font-bold mb-4">Top 5 Donors</h2>
<div className="rounded-lg">
<p className="pb-2">
Pingpad is an <Link href="https://github.com/pingpad-io/ping">open source</Link> project, and we are being
supported by our kind donors:{" "}
</p>
<ul>
{topDonors.map((donor, index) => (
<li key={donor.address} className="mb-2">
Expand All @@ -64,4 +70,4 @@ export const TopDonors = () => {
</ul>
</div>
);
};
}
26 changes: 10 additions & 16 deletions src/components/menu/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Button } from "../ui/button";
import { lensProfileToUser } from "../user/User";
import { UserAvatar } from "../user/UserAvatar";
import { SearchBar } from "./Search";
import { TopDonors } from "../TopDonors";

const UserBar = async () => {
const { handle, profileId, client } = await getServerAuth();
Expand Down Expand Up @@ -63,22 +64,15 @@ export function Sidebar() {
</AccordionTrigger>
<AccordionContent>
<p>Welcome to pingpad!</p>
<p>This is a beta, some things might be broken</p>
<p>We would love to hear what you think!</p>
<p>
Leave your feedback here:{" "}
<Link className="underline underline-offset-2" href={"/c/pingpad"}>
/pingpad
</Link>
</p>
<br />
<p>
big love,{" "}
<Link className="underline underline-offset-2" href={"/u/pingpad"}>
@pingpad
</Link>{" "}
team
</p>
<p>This is a beta, we would love to hear what you think!</p>
</AccordionContent>
</AccordionItem>
<AccordionItem value="donos">
<AccordionTrigger className="py-2">
<span> Top Donors </span>
</AccordionTrigger>
<AccordionContent>
<TopDonors />
</AccordionContent>
</AccordionItem>
</Accordion>
Expand Down

0 comments on commit c0be584

Please sign in to comment.