Skip to content

Commit

Permalink
Merge pull request #44 from DLC-link/feat/protocol-fee-percentage-fet…
Browse files Browse the repository at this point in the history
…ching

feat: added protocol fee percentage fetching for lock screen
  • Loading branch information
Polybius93 authored Dec 19, 2023
2 parents 93e6b0c + 2cce2cd commit 2642614
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HStack, Text, VStack } from '@chakra-ui/react';
interface LockScreenProtocolFeeProps {
assetAmount?: number;
bitcoinPrice?: number;
protocolFeePercentage?: number;
}

function calculateProtocolFee(amount: number, protocolFeePercentage: number): string {
Expand All @@ -20,6 +21,7 @@ function calculateProtocolFeeInUSD(
export function LockScreenProtocolFee({
assetAmount,
bitcoinPrice,
protocolFeePercentage,
}: LockScreenProtocolFeeProps): React.JSX.Element {
return (
<VStack
Expand All @@ -35,14 +37,18 @@ export function LockScreenProtocolFee({
Protocol Fee
</Text>
<Text color={'white.01'} fontSize={'sm'} fontWeight={800}>
{assetAmount && calculateProtocolFee(assetAmount, 0.0001)} BTC
{assetAmount &&
protocolFeePercentage &&
calculateProtocolFee(assetAmount, protocolFeePercentage)}{' '}
BTC
</Text>{' '}
</HStack>
<Text color={'white.01'} fontSize={'sm'}>
={' '}
{assetAmount &&
bitcoinPrice &&
calculateProtocolFeeInUSD(assetAmount, bitcoinPrice, 0.0001)}{' '}
protocolFeePercentage &&
calculateProtocolFeeInUSD(assetAmount, bitcoinPrice, protocolFeePercentage)}{' '}
$
</Text>
</VStack>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useState } from 'react';
import { useContext, useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';

import { Button, VStack } from '@chakra-ui/react';
Expand All @@ -19,10 +19,21 @@ export function LockScreen({ currentStep }: LockScreenProps): React.JSX.Element
const { readyVaults } = useVaults();
const blockchainContext = useContext(BlockchainContext);
const bitcoin = blockchainContext?.bitcoin;
const ethereum = blockchainContext?.ethereum;

const [isSubmitting, setIsSubmitting] = useState(false);
const [protocolFeePercentage, setProtocolFeePercentage] = useState<number | undefined>(undefined);

const currentVault = readyVaults.find(vault => vault.uuid === currentStep[1]);

useEffect(() => {
const fetchProtocolFeePercentage = async () => {
const currentProtocolFeePercentage = await ethereum?.getProtocolFee();
setProtocolFeePercentage(currentProtocolFeePercentage);
};
fetchProtocolFeePercentage();

Check warning on line 34 in src/app/components/mint-unmint/components/lock-screen/lock-screen.tsx

View workflow job for this annotation

GitHub Actions / lint-eslint

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}, [ethereum]);

async function handleClick(currentVault?: Vault) {
if (!currentVault) return;

Expand All @@ -41,6 +52,7 @@ export function LockScreen({ currentStep }: LockScreenProps): React.JSX.Element
<LockScreenProtocolFee
assetAmount={currentVault?.collateral}
bitcoinPrice={bitcoin?.bitcoinPrice}
protocolFeePercentage={protocolFeePercentage}
/>
<Button
isLoading={isSubmitting}
Expand Down
12 changes: 12 additions & 0 deletions src/app/hooks/use-ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface UseEthereumReturnType {
dlcBTCContract: Contract | undefined;
getDLCBTCBalance: () => Promise<number | undefined>;
getLockedBTCBalance: () => Promise<number | undefined>;
getProtocolFee: () => Promise<number | undefined>;
totalSupply: number | undefined;
requestEthereumAccount: (network: Network, walletType: WalletType) => Promise<void>;
getAllVaults: () => Promise<void>;
Expand Down Expand Up @@ -189,6 +190,16 @@ export function useEthereum(): UseEthereumReturnType {
}
}

async function getProtocolFee(): Promise<number | undefined> {
if (!protocolContract) throw new Error('Protocol contract not initialized');
try {
const btcMintFeeRate = await protocolContract.btcMintFeeRate();
return customShiftValue(btcMintFeeRate.toNumber(), 4, true);
} catch (error) {
throwEthereumError(`Could not fetch protocol fee: `, error);
}
}

async function addEthereumNetwork(
newEthereumNetwork: EthereumNetwork,
walletType: WalletType
Expand Down Expand Up @@ -467,6 +478,7 @@ export function useEthereum(): UseEthereumReturnType {
dlcManagerContract,
dlcBTCContract,
getDLCBTCBalance,
getProtocolFee,
totalSupply,
getLockedBTCBalance,
requestEthereumAccount,
Expand Down

0 comments on commit 2642614

Please sign in to comment.