Skip to content

Commit

Permalink
feat: show vaults data
Browse files Browse the repository at this point in the history
  • Loading branch information
0xChin committed Aug 10, 2024
1 parent 32bb208 commit a5be5de
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
3 changes: 0 additions & 3 deletions frontend/app/api/createVault/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { privateKeyToAccount } from "viem/accounts";
import { optimism } from "viem/chains";

export async function GET(request: NextRequest) {
console.log(request.nextUrl.searchParams);

const publicClient = createPublicClient({
chain: optimism,
transport: http(
Expand All @@ -39,7 +37,6 @@ export async function GET(request: NextRequest) {
// 1b. Or public and/or wallet clients
}); */

console.log(process.env.PRIVATE_KEY);
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);

/* const { result, request: contractRequest } =
Expand Down
8 changes: 3 additions & 5 deletions frontend/app/api/getVaults/route.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { NextRequest } from "next/server";

export async function GET(request: NextRequest) {
const chainId = request.nextUrl.searchParams.get("chainId") || "10";

export async function GET() {
const vaults: { [key: number]: string[] } = {
[10]: [
"0xd582ac93a270b8B1c2761D0aFde400Cb334A55c5",
"0x71836DE191ec5622e514A0B3a22D71abd5Bc5448",
],
[42161]: ["0x4d13b7cb87d44796aa409615706caf07b8c01aaa"],
[42161]: ["0x5f975746a539E2E9Fa3a0b4d4B85FE8E4220113E"],
[64122]: [],
};

return Response.json({ vaults: vaults[parseInt(chainId)] });
return Response.json({ vaults });
}
19 changes: 11 additions & 8 deletions frontend/app/hooks/useVault.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useReadContract, useReadContracts } from "wagmi";
import { VAULT_ABI } from "@/utils/vaultConfig";
import { erc20Abi } from "viem";
import { erc20Abi, maxUint256 } from "viem";

export const useVault = (address: `0x${string}`) => {
const assetAddress = useReadContract({
Expand Down Expand Up @@ -31,6 +31,12 @@ export const useVault = (address: `0x${string}`) => {
functionName: "borrowedAsset",
});

const targetHealthFactor = useReadContract({
abi: VAULT_ABI,
address,
functionName: "TARGET_HEALTH_FACTOR",
});

const borrowedAssetData = useReadContracts({
allowFailure: false,
contracts: [
Expand Down Expand Up @@ -58,9 +64,7 @@ export const useVault = (address: `0x${string}`) => {
],
});

if (aaveData.data) {
console.log(aaveData.data[0][0] - aaveData.data[0][1]);
}
console.log(aaveData.data);

return {
token: {
Expand All @@ -73,9 +77,8 @@ export const useVault = (address: `0x${string}`) => {
decimals: borrowedAssetData.data ? borrowedAssetData.data[0] : 0,
symbol: borrowedAssetData.data ? borrowedAssetData.data[1] : "",
},
tvl: aaveData.data
? aaveData.data[0][0] - aaveData.data[0][1]
: parseInt("0").toFixed(2),
healthFactor: 0,
tvl: aaveData.data ? aaveData.data[0][0] - aaveData.data[0][1] : BigInt(0),
targetHealthFactor: targetHealthFactor.data,
healthFactor: aaveData.data ? aaveData.data[0][5] : maxUint256,
};
};
39 changes: 21 additions & 18 deletions frontend/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import Image from "next/image";
import { ConnectButton } from "@rainbow-me/rainbowkit";
import { useChainId, useWriteContract } from "wagmi";
import { BORROW_ASSETS, FACTORY_ABI, FACTORY_CONTRACT_ADDRESS } from "@/utils/vaultConfig";
import {
BORROW_ASSETS,
FACTORY_ABI,
FACTORY_CONTRACT_ADDRESS,
} from "@/utils/vaultConfig";
import { parseUnits } from "viem";
import { pythPriceFeedIds } from "@/utils/vaultConfig";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
Expand All @@ -16,7 +20,11 @@ import {
TableRow,
} from "@/components/ui/table";
import Vault from "@/components/vault";
import { LENDING_PROTOCOLS, ASSETS, ADDRESS_TO_ORACLE_ID } from "@/utils/vaultConfig";
import {
LENDING_PROTOCOLS,
ASSETS,
ADDRESS_TO_ORACLE_ID,
} from "@/utils/vaultConfig";

import {
Select,
Expand All @@ -31,7 +39,7 @@ export default function Home() {
const { writeContractAsync } = useWriteContract();
const chainId = useChainId();
const [isCreatingVault, setIsCreatingVault] = useState(false);
const [vaults, setVaults] = useState<`0x${string}`[]>([]);
const [vaults, setVaults] = useState<{ [key: number]: `0x${string}`[] }>({});
const [asset, setAsset] = useState<`0x${string}`>("0x");
const [borrowToken, setBorrowToken] = useState<`0x${string}`>("0x");
const [minHealthFactor, setMinHealthFactor] = useState(125);
Expand All @@ -51,20 +59,15 @@ export default function Home() {
ADDRESS_TO_ORACLE_ID[chainId][asset!],
],
});

console.log(result);
} catch (error) {
console.error("Error creating vault:", error);
}
};

useEffect(() => {
console.log("Fetching vaults with chainId", chainId);

fetch(`/api/getVaults?chainId=${chainId}`)
.then((res) => res.json())
.then((data) => {
console.log(data);
setVaults(data.vaults);
});
}, [chainId]);
Expand Down Expand Up @@ -107,7 +110,7 @@ export default function Home() {
</ToggleGroupItem>
</ToggleGroup>
{!isCreatingVault &&
(vaults.length > 0 ? (
(vaults && vaults[chainId] && vaults[chainId].length > 0 ? (
<Table className="mt-4">
<TableHeader>
<TableRow>
Expand All @@ -122,7 +125,7 @@ export default function Home() {
</TableRow>
</TableHeader>
<TableBody>
{vaults.map((vault, index) => (
{vaults[chainId].map((vault, index) => (
<Vault address={vault} key={index} />
))}
</TableBody>
Expand All @@ -141,8 +144,8 @@ export default function Home() {
<SelectValue placeholder="Select Protocol" />
</SelectTrigger>
<SelectContent>
{LENDING_PROTOCOLS.map((protocol) => (
<SelectItem value={protocol.name}>
{LENDING_PROTOCOLS.map((protocol, index) => (
<SelectItem value={protocol.name} key={index}>
<div className="flex items-center">
<Image
src={protocol.image}
Expand Down Expand Up @@ -181,16 +184,16 @@ export default function Home() {
onValueChange={(value) =>
setBorrowToken(value as `0x${string}`)
}
>
>
<SelectTrigger className="w-full mt-1 p-2 border rounded">
<SelectValue placeholder="Select Borrow Token" />
</SelectTrigger>
<SelectContent>
{BORROW_ASSETS[chainId].map((token: `0x${string}`, index) => (
<SelectItem value={token} key={index}>
<Token address={token} />
</SelectItem>
))}
{BORROW_ASSETS[chainId].map((token: `0x${string}`, index) => (
<SelectItem value={token} key={index}>
<Token address={token} />
</SelectItem>
))}
</SelectContent>
</Select>
</label>
Expand Down
7 changes: 4 additions & 3 deletions frontend/components/vault.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect } from "react";
import { TableCell, TableRow } from "./ui/table";
import { useVault } from "@/app/hooks/useVault";
import Image from "next/image";
import { formatUnits, maxUint256 } from "viem";

export default function Vault({ address }: { address: `0x${string}` }) {
const vaultData = useVault(address);
Expand Down Expand Up @@ -38,9 +39,9 @@ export default function Vault({ address }: { address: `0x${string}` }) {
{vaultData.debtToken.symbol}
</div>
</TableCell>
<TableCell className="text-center">$10.000</TableCell>
<TableCell className="text-center">1.25</TableCell>
<TableCell className="text-center">1.25</TableCell>
<TableCell className="text-center">{vaultData.tvl > BigInt(0) ? `$${formatUnits(vaultData.tvl as bigint, 18)}` : "-"}</TableCell>
<TableCell className="text-center">{vaultData.targetHealthFactor ? formatUnits(vaultData.targetHealthFactor, 18) : "-"}</TableCell>
<TableCell className="text-center">{vaultData.healthFactor && vaultData.healthFactor !== maxUint256 ? formatUnits(vaultData.healthFactor, 18) : "-"}</TableCell>
<TableCell className="text-center">
<button className="py-2 px-4 bg-blue-500 text-white rounded">
Deposit
Expand Down
2 changes: 1 addition & 1 deletion frontend/utils/vaultConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const FACTORY_CONTRACT_ADDRESS: { [chainId: number]: `0x${string}` } = {
10: "0x6032Aa03C26dc0eec909af1613D81e829aB2A36A",
42161: "0x4d13b7cb87d44796aa409615706caf07b8c01aaa",
64122: "0x6032aa03c26dc0eec909af1613d81e829ab2a36a",
64122: "0x09C786C3fDA647B53d791633dFc565e5259ADB6F",
};

export const LENDING_PROTOCOLS = [
Expand Down

0 comments on commit a5be5de

Please sign in to comment.