Skip to content

Commit

Permalink
account balances
Browse files Browse the repository at this point in the history
  • Loading branch information
limone-eth committed Mar 16, 2024
1 parent 7788ec7 commit 966f2cf
Show file tree
Hide file tree
Showing 8 changed files with 13,741 additions and 3 deletions.
21 changes: 20 additions & 1 deletion app/api/accounts/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { fetchUSDCTokenBalances } from "@/lib/airstack";
import { getUserAccounts, upsertAccount } from "@/lib/db/accounts";
import { Account } from "@/lib/db/interfaces";
import { upsertRecord } from "@/lib/db/records";
import { getWalletBalance } from "@/lib/lifi";
import { NextResponse, NextRequest } from "next/server";
import slugify from "slugify";
import { normalize } from "viem/ens";
Expand Down Expand Up @@ -41,5 +44,21 @@ export const POST = async (req: NextRequest, res: NextResponse) => {
export const GET = async (req: NextRequest, res: NextResponse) => {
const username = req.headers.get("x-username");
const accounts = await getUserAccounts(username!);
return NextResponse.json(accounts);
const enrichedAccounts = await enrichAccountsWithBalances(accounts);
return NextResponse.json(enrichedAccounts);
};

export const enrichAccountsWithBalances = async (accounts: Account[]) => {
const tokenBalances = await fetchUSDCTokenBalances(
accounts.map((a) => a.address)
);
return accounts.map((a) => ({
balance: tokenBalances
? tokenBalances?.find(
(b) =>
b.owner?.addresses![0].toLowerCase() === a.address.toLowerCase()
)?.formattedAmount
: "0",
...a,
}));
};
2 changes: 1 addition & 1 deletion app/home/accounts/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function AccountsPage() {
</div>
)}
<p className="text-[#8F8F91] leading-none font-medium">
$50,00
${parseFloat(account.balance).toFixed(2)}
</p>
</div>
</Button>
Expand Down
20 changes: 20 additions & 0 deletions codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
overwrite: true,
schema: "https://api.airstack.xyz/gql",
documents: "lib/airstack.ts",
generates: {
// Output type file
"lib/airstack-types.ts": {
// add to plugins: @graphql-codegen/typescript and @graphql-codegen/typescript-operations
plugins: ["typescript", "typescript-operations"],
config: {
avoidOptionals: true,
skipTypename: true,
},
},
},
};

export default config;
Loading

0 comments on commit 966f2cf

Please sign in to comment.