Skip to content

Commit

Permalink
feat: add repayment estimation rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
Another-DevX committed Mar 30, 2024
1 parent 8ab7fcc commit e6822af
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@ethersproject/providers": "5.7.2",
"@ethersproject/strings": "5.7.0",
"@impact-market/ui": "1.6.3",
"@impact-market/utils": "4.2.0-beta.8",
"@impact-market/utils": "4.2.0-beta.12.2",
"@prismicio/client": "7.3.1",
"@prismicio/helpers": "2.3.9",
"@prismicio/next": "1.5.0",
Expand Down
54 changes: 52 additions & 2 deletions src/views/MicroCredit/LoanRepayment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ import {
colors,
toast
} from '@impact-market/ui';
import { GetNetworkResult } from '@wagmi/core';
import {
getPACTTradingMetrics,
useBorrower,
useCUSDBalance,
useRewards
} from '@impact-market/utils';
import { localeFormat } from '../../utils/currencies';
import { mq } from 'styled-gen';
import { useBorrower, useCUSDBalance } from '@impact-market/utils';
import { useNetwork } from 'wagmi';
import { useEffect, useState } from 'react';
import { useMicrocreditBorrower } from 'src/hooks/useMicrocredit';
import { useState } from 'react';

import Image from '../../libs/Prismic/components/Image';
import LoanOverview from './LoanOverview';
import Message from '../../libs/Prismic/components/Message';
Expand All @@ -23,6 +31,7 @@ import config from '../../../config';
import processTransactionError from '../../utils/processTransactionError';
import styled, { css } from 'styled-components';
import useTranslations from '../../libs/Prismic/hooks/useTranslations';
import BigNumber from 'bignumber.js';

const PerformanceWarning = styled(Card)<{ performance: number }>`
box-shadow: none;
Expand Down Expand Up @@ -86,6 +95,21 @@ const ActionWrapper = styled(Box)`
)}
`;

function useTokenPriceUSD(network: GetNetworkResult) {
const [priceUSD, setPriceUSD] = useState<number | null>(null);

useEffect(() => {
async function fetchPrice() {
const response = await getPACTTradingMetrics(network?.chain.id);
setPriceUSD(parseFloat(response.priceUSD));
}

fetchPrice();
}, [network]);

return priceUSD;
}

const LoanRepayment = (props: any) => {
const {
data,
Expand All @@ -103,6 +127,7 @@ const LoanRepayment = (props: any) => {
maximumFractionDigits: 6,
maximumSignificantDigits: 6
});
const { estimateDonationRewards, rewards } = useRewards();
const {
repayLoanTitle,
repayLoanDescription,
Expand All @@ -123,6 +148,27 @@ const LoanRepayment = (props: any) => {
const [approved, setApproved] = useState(false);
const [isLoadingApprove, setIsLoadingApprove] = useState(false);
const [isLoadingRepay, setIsLoadingRepay] = useState(false);
const network = useNetwork();

const pactPriceUSD = useTokenPriceUSD(network);

useEffect(() => {
console.debug(loan);
const result = new BigNumber(
(
(loan.amountRepayed + loan.currentDebt - loan.amountBorrowed) *
10 *
30 *
3
).toFixed(0)
).toString();

estimateDonationRewards(result);
}, [loan]);

useEffect(() => {
console.debug(rewards, pactPriceUSD);
}, [rewards.estimated, pactPriceUSD]);

const { borrower, loadingBorrower } = useMicrocreditBorrower([
`address=${userAddress}`
Expand Down Expand Up @@ -186,6 +232,10 @@ const LoanRepayment = (props: any) => {
small
mt={0.5}
variables={{
estimatedRewards: rewards.estimated.toFixed(3),
estimatedFiatRewards: (
rewards.estimated * pactPriceUSD
).toFixed(3),
currentDebt: loan.currentDebt.toFixed(3),
debtAccrues: (
loan.currentDebt *
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3433,10 +3433,10 @@
styled-gen "1.4.8"
styled-reset "4.4.7"

"@impact-market/[email protected].8":
version "4.2.0-beta.8"
resolved "https://registry.yarnpkg.com/@impact-market/utils/-/utils-4.2.0-beta.8.tgz#2acd14f042668fcc18f40cb0526860f89eb2337f"
integrity sha512-N7Rh8M4YGN3Dki9ruKLbT62jRr0kS+Zpn0QN4gHXGw8I+HdpgMWqTWiGF7YBHUPRsfeUSogxh6aJhOxgOfr5Dw==
"@impact-market/[email protected].12.2":
version "4.2.0-beta.12.2"
resolved "https://registry.yarnpkg.com/@impact-market/utils/-/utils-4.2.0-beta.12.2.tgz#47183157fe52eeac4a7756667d83f5900946d675"
integrity sha512-8eB9HkkXXtu1KNylCBUP38UvAXRSJReAActN5gyE8AH65tZ+51SVlq+3EfEhmCXcC9Z12FTwaDu2VIwBfLGZTg==

"@jridgewell/gen-mapping@^0.1.0":
version "0.1.1"
Expand Down

0 comments on commit e6822af

Please sign in to comment.