1- import { useState } from "react"
1+ import { useEffect , useState } from "react"
2+ import { toast } from "react-toastify" ;
3+ import { borrowRequest } from "../../apis/lending"
4+ import { useMetamaskAuth } from "../../auth/authConfig"
5+
6+ function collateralCalculator ( loanAmount , repayDays ) {
7+ return ( ( parseInt ( loanAmount ) + ( parseInt ( loanAmount ) * ( 0.1 * repayDays ) / 100 ) ) * 0.000639 + 0.005 ) * 1.3 ;
8+ }
29
310const Borrow = ( ) => {
4- const [ eligible , setEligible ] = useState ( false )
11+ // const [eligible, setEligible] = useState(true)
12+ const [ loanAmount , setLoanAmount ] = useState ( null )
13+ const [ repayDays , setRepayDays ] = useState ( 1 )
14+ const [ maticBalance , setMaticBalance ] = useState ( 0 ) ;
15+ const [ usdtBalance , setUsdtBalance ] = useState ( 0 ) ;
16+ const { profile, isLoggedIn, isProcessingLogin } = useMetamaskAuth ( ) ;
17+
18+ console . log ( maticBalance )
19+
20+
21+ const onConfirmBorrowClickHandler = ( e ) => {
22+ if ( ! profile )
23+ return ;
24+ e . preventDefault ( )
25+
26+ const totalRepayInUSDC = e . target . loanAmount . value + ( e . target . loanAmount . value * ( 0.0356 * e . target . repayDays . value ) / 100 )
27+ const requiredCollateralInUSDT = totalRepayInUSDC + totalRepayInUSDC * 150 / 100
528
6- const onConfirmBorrowClickHandler = ( ) => {
29+ console . log ( totalRepayInUSDC , requiredCollateralInUSDT )
730
31+ console . log ( "Borrow with fields" , {
32+ address : profile . address ,
33+ eth : collateralCalculator ( loanAmount , repayDays ) ,
34+ loanAmount,
35+ repayDays
36+ } ) ;
37+ borrowRequest (
38+ profile . address ,
39+ collateralCalculator ( loanAmount , repayDays ) ,
40+ loanAmount ,
41+ repayDays
42+ ) . then ( ( ) => {
43+ toast . success ( "Borrow request is successfuly registered" , { autoClose : 900 } ) ;
44+ setTimeout ( ( ) => window . location . reload ( ) , 1000 ) ;
45+ } ) . catch ( err => {
46+ toast . error ( "Metamask request rejected" ) ;
47+ } )
48+ // loans.push(Loan({
49+ // // loanId: loanId,
50+ // // borrower: msg.sender,
51+ // // lender: address(0),
52+ // loanAmount: Number(e.target.loanAmount.value),
53+ // repayDays: Number(e.target.repayDays.value),
54+ // // loanGrantedTime: 0,
55+ // // payableDeadline: 0,
56+ // // collateralAmount: msg.value,
57+ // // loanApproved: false,
58+ // // loanRepayed: false
59+ // }));
860 }
9-
61+
62+ useEffect ( ( ) => {
63+ if ( ! profile ) return ;
64+ getMaticBalance ( profile . address )
65+ . then ( setMaticBalance ) ;
66+ getUsdtBalance ( profile . address )
67+ . then ( setUsdtBalance ) ;
68+ } , [ profile ] ) ;
69+
70+ // console.log(loanAmount, loanAmount, repayDays)
71+ // console.log("interest"+( (parseInt(loanAmount) + (parseInt(loanAmount)*(0.1*repayDays)/100))*0.000639))
72+
73+ const eligible = maticBalance > collateralCalculator ( loanAmount , repayDays ) ;
74+
1075 return (
11- < div className = "flex flex-col gap-y-[30px] w-[100%] h-[100%] text-white p-[20px] px-[30px] text-inter" >
76+ < form className = "flex flex-col gap-y-[30px] w-[100%] h-[100%] text-white p-[20px] px-[30px] text-inter" onSubmit = { onConfirmBorrowClickHandler } >
1277 < div className = "flex flex-row items-center" >
1378 < div className = "font-inter text-[16px] font-medium" >
1479 Loan amount:
1580 </ div >
1681
17- < input className = "ml-auto bg-transparent text-right text-[#696c80] font-medium outline-none" placeholder = "0.00" />
82+ < input value = { loanAmount } onChange = { ( e ) => setLoanAmount ( e ?. target . value ) } name = "loanAmount" className = "ml-auto bg-transparent text-right text-[#696c80] font-medium outline-none" placeholder = "0.00" />
1883 < div className = "flex flex-row items-center gap-x-[10px] text-[14px] text-white w-auto h-[40px] p-[5px] px-[8px] rounded-[20px] font-medium pr-[15px] ml-[25px] bg-[#404557]" >
19- < img className = "w-[30px] h-[30px] rounded-full" src = "./assets/usdc.svg" alt = "usdcLogo" />
20- USDC
84+ < img className = "w-[30px] h-[30px] rounded-full" src = "./assets/usdc.svg" alt = "usdcLogo" />
85+ USDT
2186 </ div >
2287 </ div >
2388
2489 < div className = "flex flex-row items-center" >
2590
2691 < div className = "font-inter text-[16px] font-medium" > Expected repay duration:</ div >
27- < input className = "ml-auto bg-transparent text-right text-[#696c80] font-medium mr-[10px] outline-none" placeholder = "0" />
92+ < input value = { repayDays } onChange = { ( e ) => setRepayDays ( e ?. target . value ) } name = "repayDays" className = "ml-auto bg-transparent text-right text-[#696c80] font-medium mr-[10px] outline-none" placeholder = "0" />
2893 < div > days</ div >
2994
3095 </ div >
3196
3297 < div className = "flex flex-row items-center" >
3398
3499 < div className = "font-inter text-[16px] font-medium" > Interest %:</ div >
35- < div className = "ml-auto bg-transparent text-right text-[#696c80] font-medium mr-[10px] outline-none" > 13 %</ div >
100+ < div className = "ml-auto bg-transparent text-right text-[#696c80] font-medium mr-[10px] outline-none" > 36 %</ div >
36101 < div > APR</ div >
37102
38103 </ div >
39104
40105 < div className = "flex flex-row items-center" >
41106
42107 < div className = "font-inter text-[16px] font-medium" > Interest Amount:</ div >
43- < div className = "ml-auto text-right text-[#696c80] font-medium mr-[10px]" > 9999 </ div >
108+ < div className = "ml-auto text-right text-[#696c80] font-medium mr-[10px]" > { ( loanAmount * ( 0.1 * repayDays ) / 100 ) . toFixed ( 2 ) } </ div >
44109 < div className = "flex flex-row items-center gap-x-[10px] text-[14px] text-white w-auto h-[40px] p-[5px] px-[8px] rounded-[20px] font-medium pr-[15px] ml-[10px] bg-[#404557]" >
45- < img className = "w-[30px] h-[30px] rounded-full" src = "./assets/usdc.svg" alt = "usdcLogo" />
46- USDC
110+ < img className = "w-[30px] h-[30px] rounded-full" src = "./assets/usdc.svg" alt = "usdcLogo" />
111+ USDT
47112 </ div >
48113
49114 </ div >
50115
51116 < div className = "flex flex-row items-center" >
52117
53118 < div className = "font-inter text-[16px] font-medium" > Required collateral:</ div >
54- < div className = "ml-auto text-right text-[#696c80] font-medium mr-[10px]" > 9999</ div >
119+ < div className = "ml-auto text-right text-[#696c80] font-medium mr-[10px]" >
120+ {
121+ loanAmount ? collateralCalculator ( loanAmount , repayDays ) . toFixed ( 4 ) : 0
122+ }
123+ </ div >
55124 < div className = "flex flex-row items-center gap-x-[10px] text-[14px] text-white w-auto h-[40px] p-[5px] px-[8px] rounded-[20px] font-medium pr-[15px] ml-[10px] bg-[#404557]" >
56125 < div className = "flex justify-center w-[30px] h-[30px] rounded-full bg-white p-[4px] box-border" >
57- < img src = "./assets/matic.svg " alt = "maticLogo " />
126+ < img src = "./assets/eth.png " alt = "ethLogo " />
58127 </ div >
59- MATIC
128+ ETH
60129 </ div >
61130 </ div >
62131
63- { eligible && < div className = "text-slate-300 text-center" >
132+ { eligible &&
133+ < div className = "text-slate-300 text-center" >
64134 Congratulations 🎉 you are eligible for borrowing
65- </ div > }
135+ </ div >
136+ }
66137
67138 < button
68139 type = "submit"
@@ -78,7 +149,7 @@ const Borrow = () => {
78149 </ svg >
79150 on conforming collateral amount will be deducted from your account
80151 </ div >
81- </ div >
152+ </ form >
82153 )
83154}
84155
0 commit comments