Skip to content

Commit

Permalink
feat: sum all available chain tvl
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Apr 2, 2024
1 parent b35b508 commit be5731b
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import React from 'react';
import React, { useContext } from 'react';

import { Divider, HStack } from '@chakra-ui/react';
import { TokenStatsBoardTVL } from '@components/proof-of-reserve/components/token-stats-board/components/token-stats-board-tvl';
import { useBitcoinPrice } from '@hooks/use-bitcoin-price';
import { ProofOfReserveContext } from '@providers/proof-of-reserve-context-provider';

import { ProtocolSummaryStack } from '../protocol-summary-stack/protocol-summary-stack';
import { LandingPageLayout } from './components/landing-page.layout';
import { SetupInformation } from './components/setup-information/setup-informatio';
import { WelcomeStack } from './components/welcome-stack';

export function LandingPage(): React.JSX.Element {
const { bitcoinPrice } = useBitcoinPrice();
const { totalSupply } = useContext(ProofOfReserveContext);
return (
<LandingPageLayout>
<WelcomeStack />
<Divider orientation={'horizontal'} h={'35px'} variant={'thick'} />
<HStack w={'100%'} align={'start'} pt={'50px'}>
<SetupInformation />
<Divider orientation={'vertical'} w={'35px'} h={'205px'} variant={'thick'} />
<ProtocolSummaryStack />
<TokenStatsBoardTVL totalSupply={totalSupply} bitcoinPrice={bitcoinPrice} />
</HStack>
</LandingPageLayout>
);
Expand Down

This file was deleted.

This file was deleted.

5 changes: 2 additions & 3 deletions src/app/components/proof-of-reserve/proof-of-reserve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useContext } from 'react';

import { Divider, HStack, Text } from '@chakra-ui/react';
import { useBitcoinPrice } from '@hooks/use-bitcoin-price';
import { useEthereum } from '@hooks/use-ethereum';
import { bitcoin, dlcBTC } from '@models/token';
import { ProofOfReserveContext } from '@providers/proof-of-reserve-context-provider';

Expand All @@ -12,9 +11,9 @@ import { TokenStatsBoardTVL } from './components/token-stats-board/components/to
import { TokenStatsBoardLayout } from './components/token-stats-board/token-stats-board.layout';

export function ProofOfReserve(): React.JSX.Element {
const { totalSupply } = useEthereum();
const { bitcoinPrice } = useBitcoinPrice();
const { proofOfReserve } = useContext(ProofOfReserveContext);

const { proofOfReserve, totalSupply } = useContext(ProofOfReserveContext);

return (
<ProofOfReserveLayout>
Expand Down
66 changes: 49 additions & 17 deletions src/app/hooks/use-ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { useSelector } from 'react-redux';

import { customShiftValue } from '@common/utilities';
import { EthereumError } from '@models/error-types';
import {
EthereumNetwork,
EthereumNetworkID,
ethereumArbSepolia,
ethereumSepolia,
} from '@models/ethereum-network';
import { RawVault, Vault, VaultState } from '@models/vault';
import { VaultContext } from '@providers/vault-context-provider';
import { RootState, store } from '@store/index';
Expand All @@ -17,10 +23,10 @@ interface UseEthereumReturnType {
getDLCBTCBalance: () => Promise<number | undefined>;
getLockedBTCBalance: () => Promise<number | undefined>;
totalSupply: number | undefined;
getAttestorGroupPublicKey: () => Promise<string>;
getAttestorGroupPublicKey: (ethereumNetwork: EthereumNetwork) => Promise<string>;
getAllVaults: () => Promise<void>;
getVault: (vaultUUID: string, vaultState: VaultState) => Promise<void>;
getAllFundedVaults: () => Promise<RawVault[]>;
getAllFundedVaults: (thereumNetwork: EthereumNetwork) => Promise<RawVault[]>;
setupVault: (btcDepositAmount: number) => Promise<void>;
closeVault: (vaultUUID: string) => Promise<void>;
}
Expand All @@ -38,14 +44,18 @@ export function throwEthereumError(message: string, error: any): void {

export function useEthereum(): UseEthereumReturnType {
const { vaults } = useContext(VaultContext);
const { protocolContract, dlcBTCContract, dlcManagerContract } = useEthereumContext();
const { protocolContract, dlcBTCContract } = useEthereumContext();

const { address, network } = useSelector((state: RootState) => state.account);

const [totalSupply, setTotalSupply] = useState<number | undefined>(undefined);

const fetchTotalSupply = async () => {
await getTotalSupply();
const sepoliaTotalSupply = await getTotalSupply(ethereumSepolia);
console.log('sepoliaTotalSupply', sepoliaTotalSupply);

Check warning on line 55 in src/app/hooks/use-ethereum.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement
const arbSepoliaTotalSupply = await getTotalSupply(ethereumArbSepolia);
console.log('arbSepoliaTotalSupply', arbSepoliaTotalSupply);

Check warning on line 57 in src/app/hooks/use-ethereum.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement
setTotalSupply(customShiftValue(sepoliaTotalSupply + arbSepoliaTotalSupply, 8, true));
};

useEffect(() => {
Expand All @@ -69,24 +79,45 @@ export function useEthereum(): UseEthereumReturnType {
};
}

async function getTotalSupply() {
const provider = ethers.providers.getDefaultProvider(
'https://ethereum-sepolia.publicnode.com/'
);
const branchName = import.meta.env.VITE_ETHEREUM_DEPLOYMENT_BRANCH;
const contractVersion = import.meta.env.VITE_ETHEREUM_DEPLOYMENT_VERSION;
const deploymentPlanURL = `https://raw.githubusercontent.com/DLC-link/dlc-solidity/${branchName}/deploymentFiles/sepolia/v${contractVersion}/DLCBTC.json`;
async function getDefaultProvider(
ethereumNetwork: EthereumNetwork,
contractName: string
): Promise<ethers.Contract> {
let nodeURL: string;
switch (ethereumNetwork.id) {
case EthereumNetworkID.Sepolia:
nodeURL = 'https://ethereum-sepolia.publicnode.com/';
break;
case EthereumNetworkID.ArbSepolia:
nodeURL = 'https://sepolia-rollup.arbitrum.io/rpc';
break;
default:
throw new EthereumError(`Invalid Ethereum Network ID: ${ethereumNetwork.id}`);
}

try {
const provider = ethers.providers.getDefaultProvider(nodeURL);
const branchName = import.meta.env.VITE_ETHEREUM_DEPLOYMENT_BRANCH;
const contractVersion = import.meta.env.VITE_ETHEREUM_DEPLOYMENT_VERSION;
const deploymentPlanURL = `https://raw.githubusercontent.com/DLC-link/dlc-solidity/${branchName}/deploymentFiles/${ethereumNetwork.name.toLowerCase()}/v${contractVersion}/${contractName}.json`;
const response = await fetch(deploymentPlanURL);
const contractData = await response.json();
const protocolContract = new ethers.Contract(
contractData.contract.address,
contractData.contract.abi,
provider
);
return protocolContract;
} catch (error) {
throw new EthereumError(`Could not get Default Provider: ${error}}`);
}
}

async function getTotalSupply(ethereumNetwork: EthereumNetwork): Promise<number> {
try {
const protocolContract = await getDefaultProvider(ethereumNetwork, 'DLCBTC');
const totalSupply = await protocolContract.totalSupply();
setTotalSupply(customShiftValue(totalSupply, 8, true));
return totalSupply.toNumber();
} catch (error) {
throw new EthereumError(`Could not fetch Total Supply Info: ${error}}`);
}
Expand Down Expand Up @@ -115,10 +146,11 @@ export function useEthereum(): UseEthereumReturnType {
}
}

async function getAttestorGroupPublicKey(): Promise<string> {
async function getAttestorGroupPublicKey(ethereumNetwork: EthereumNetwork): Promise<string> {
try {
if (!dlcManagerContract) throw new Error('Protocol contract not initialized');
return await dlcManagerContract.attestorGroupPubKey();
const dlcManagerContract = await getDefaultProvider(ethereumNetwork, 'DLCManager');
const attestorGroupPubKey = await dlcManagerContract.attestorGroupPubKey();
return attestorGroupPubKey;
} catch (error) {
throw new EthereumError(`Could not fetch Attestor Public Key: ${error}`);
}
Expand Down Expand Up @@ -173,9 +205,9 @@ export function useEthereum(): UseEthereumReturnType {
throw new EthereumError(`Failed to fetch Vault ${vaultUUID} after ${maxRetries} retries`);
}

async function getAllFundedVaults(): Promise<RawVault[]> {
async function getAllFundedVaults(ethereumNetwork: EthereumNetwork): Promise<RawVault[]> {
try {
if (!dlcManagerContract) throw new Error('DLC Manager contract not initialized');
const dlcManagerContract = await getDefaultProvider(ethereumNetwork, 'DLCManager');
const vaults: RawVault[] = await dlcManagerContract.getFundedDLCs(0, 1000000);
return vaults;
} catch (error) {
Expand Down
10 changes: 7 additions & 3 deletions src/app/hooks/use-proof-of-reserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useQuery } from 'react-query';
import { customShiftValue } from '@common/utilities';
import { BitcoinNetwork } from '@models/bitcoin-network';
import { BitcoinTransaction, BitcoinTransactionVectorOutput } from '@models/bitcoin-transaction';
import { ethereumArbSepolia, ethereumSepolia } from '@models/ethereum-network';
import { RawVault } from '@models/vault';
import { hex } from '@scure/base';
import { p2tr, p2tr_ns, taprootTweakPubkey } from '@scure/btc-signer';
Expand Down Expand Up @@ -141,7 +142,7 @@ export function useProofOfReserve(): UseProofOfReserveReturnType {
);

// Get the Attestor Public Key from the Attestor Group
const attestorPublicKey = await getAttestorGroupPublicKey();
const attestorPublicKey = await getAttestorGroupPublicKey(ethereumSepolia);

// Create two MultiSig Transactions, because the User and Attestor can sign in any order
// Create the MultiSig Transaction A
Expand Down Expand Up @@ -177,10 +178,13 @@ export function useProofOfReserve(): UseProofOfReserveReturnType {
}

async function calculateProofOfReserve() {
const fundedVaults = await getAllFundedVaults();
const sepoliaFundedVaults = await getAllFundedVaults(ethereumSepolia);
const arbSepoliaFundedVaults = await getAllFundedVaults(ethereumArbSepolia);

const allVaults = [...sepoliaFundedVaults, ...arbSepoliaFundedVaults];

const results = await Promise.all(
fundedVaults.map(async vault => {
allVaults.map(async vault => {
try {
if (await verifyVaultDeposit(vault)) {
return vault.valueLocked.toNumber();
Expand Down
6 changes: 5 additions & 1 deletion src/app/providers/proof-of-reserve-context-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { createContext } from 'react';

import { useEthereum } from '@hooks/use-ethereum';
import { useProofOfReserve } from '@hooks/use-proof-of-reserve';
import { HasChildren } from '@models/has-children';

interface ProofOfReserveContextProviderType {
proofOfReserve: number | undefined;
totalSupply: number | undefined;
}

export const ProofOfReserveContext = createContext<ProofOfReserveContextProviderType>({
proofOfReserve: undefined,
totalSupply: undefined,
});

export function ProofOfReserveContextProvider({ children }: HasChildren): React.JSX.Element {
const { proofOfReserve } = useProofOfReserve();
const { totalSupply } = useEthereum();

return (
<ProofOfReserveContext.Provider value={{ proofOfReserve }}>
<ProofOfReserveContext.Provider value={{ proofOfReserve, totalSupply }}>
{children}
</ProofOfReserveContext.Provider>
);
Expand Down
4 changes: 2 additions & 2 deletions src/shared/models/ethereum-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ const ethereumGoerli: EthereumNetwork = {
id: EthereumNetworkID.Goerli,
};

const ethereumSepolia: EthereumNetwork = {
export const ethereumSepolia: EthereumNetwork = {
name: 'Sepolia',
displayName: 'Sepolia',
id: EthereumNetworkID.Sepolia,
};

const ethereumArbSepolia: EthereumNetwork = {
export const ethereumArbSepolia: EthereumNetwork = {
name: 'ArbSepolia',
displayName: 'Arbitrum Sepolia',
id: EthereumNetworkID.ArbSepolia,
Expand Down

0 comments on commit be5731b

Please sign in to comment.