Skip to content

Commit 87d65ac

Browse files
OmkarPhRupesh-2003
andauthored
Contract Integration (#5)
* Basic APIs Signed-off-by: Omkar Phansopkar <omkarphansopkar@gmail.com> * integrated borrow calcuation * Redeployed contract Signed-off-by: Omkar Phansopkar <omkarphansopkar@gmail.com> * Integrated my borrowings * partial integrated my lendings * Completed integration Signed-off-by: Omkar Phansopkar <omkarphansopkar@gmail.com> * Updated eth components Signed-off-by: Omkar Phansopkar <omkarphansopkar@gmail.com> --------- Signed-off-by: Omkar Phansopkar <omkarphansopkar@gmail.com> Co-authored-by: Rupesh Raut <rupeshraut99396@gmail.com>
1 parent c6d0c3e commit 87d65ac

File tree

16 files changed

+904
-268
lines changed

16 files changed

+904
-268
lines changed

public/assets/eth.png

25 KB
Loading

src/apis/Contract.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import Web3 from 'web3';
22
import ContractABI from '../contracts/ContractABI.json';
3+
import TokenABI from '../contracts/UsdtABI.json';
34
import {
45
contractAddress,
56
contractDeploymentTx,
6-
contractDeploymentTxLink
7+
contractDeploymentTxLink,
8+
usdtContractAddress
79
} from '../contracts/deploymentDetails'
810

911
if (typeof window != 'undefined' && typeof window.ethereum !== 'undefined') {
@@ -27,4 +29,6 @@ if(typeof window != 'undefined'){
2729
window.Contract = Contract;
2830
}
2931

32+
export const UsdtContract = new web3.eth.Contract(TokenABI, usdtContractAddress);
33+
3034
export default Contract;

src/apis/balances.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { hasWindow } from "../util/next-utils";
2-
import { web3 } from "./Contract";
3-
import TokenABI from '../contracts/UsdtABI.json';
2+
import { UsdtContract, web3 } from "./Contract";
43

5-
const UsdtContract = new web3.eth.Contract(TokenABI, '0X466DD1E48570FAA2E7F69B75139813E4F8EF75C2');
64
export const getUsdtBalance = async (address) => {
75
const balance = await UsdtContract.methods.balanceOf(address).call();
86
return balance ? balance / 1000000 : 0.00;

src/apis/factory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function SenderFn(method, senderAddress, debug, ...params){
2525

2626
tx
2727
.send({
28-
from: senderAddress
28+
from: senderAddress,
2929
})
3030
.then((receipt) => {
3131
console.log(`${method} - Tx Receipt`, receipt);

src/apis/lending.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { hasWindow } from "../util/next-utils";
2+
import { ethToWei, usdtToUnits } from "../util/units";
3+
import { linkFromTxHash } from "./Contract";
4+
import { CallerFn, SenderFn } from "./factory";
5+
6+
export function PayableSenderFn(method, senderAddress, debug, payableAmountWei, ...params){
7+
return new Promise((resolve, reject) => {
8+
const tx = Contract.methods[method](...params);
9+
if(debug)
10+
console.log("Prepared transaction: ", tx);
11+
12+
tx
13+
.send({
14+
from: senderAddress,
15+
value: payableAmountWei,
16+
})
17+
.then((receipt) => {
18+
console.log(`${method} - Tx Receipt`, receipt);
19+
console.log(`Transaction hash: ${receipt?.transactionHash}`);
20+
console.log(
21+
`View the transaction here: `,
22+
linkFromTxHash(receipt?.transactionHash)
23+
);
24+
return resolve(receipt);
25+
})
26+
.catch((err) => {
27+
console.log(`Some error sending ${method} with params \n`, params, err);
28+
reject(new Error(`Couldn't send tx for ${method}`));
29+
});
30+
});
31+
}
32+
33+
export const borrowRequest = (address, payableAmountEth, usdtLoanAmount, repayDays) => {
34+
if(!address)
35+
return console.log("Address invalid");
36+
if(!usdtLoanAmount)
37+
return console.log("Loan amount invalid");
38+
if(!repayDays)
39+
return console.log("Repay days invalid");
40+
return PayableSenderFn('createLoan', address, true, ethToWei(payableAmountEth), usdtToUnits(usdtLoanAmount), repayDays);
41+
}
42+
43+
export const approveRequest = (address, loanId) => {
44+
if(!address)
45+
return console.log("Address invalid");
46+
return SenderFn('approveLoan', address, true, loanId);
47+
}
48+
49+
export const payLoan = (address, loanId) => {
50+
if(!address)
51+
return console.log("Address invalid");
52+
return SenderFn('payLoan', address, true, loanId);
53+
}
54+
55+
export const squareOff = (address, loanId) => {
56+
if(!address)
57+
return console.log("Address invalid");
58+
return SenderFn('squareOff', address, true, loanId);
59+
}
60+
61+
export const getInterestTillDate = loanId => {
62+
return CallerFn('getInterestTillDate', true, loanId);
63+
}
64+
65+
if(hasWindow()){
66+
window.approveRequest = approveRequest;
67+
window.squareOff = squareOff;
68+
// window.squareOff = ();
69+
// window.borrowRequest = () => borrowRequest(
70+
// '0xabd8EeD5b630578F72eEB06c637dB7179576A811',
71+
// 0.021,
72+
// 25,
73+
// 3
74+
// );
75+
}

src/components/Borrow/Borrow.jsx

Lines changed: 90 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,139 @@
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

310
const 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

src/components/DashLeft/DashLeft.jsx

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import { Container } from "@mui/material";
99
import { useMetamaskAuth } from "../../auth/authConfig";
1010
import Loader from "../Loader/Loader";
1111
import { getUsdtBalance, getMaticBalance } from "../../apis/balances";
12-
const https = require("https");
13-
12+
import { borrowRequest } from "../../apis/lending";
1413

1514
const randPriceFluctuations = range => Number((Math.random() * (range*2 + 1)-range).toFixed(2));
1615

@@ -43,8 +42,8 @@ const DashLeft = () => {
4342
// .catch((error) => console.log("error", error))
4443
}, []);
4544

46-
console.log(maticVal);
47-
console.log(usdVal);
45+
// console.log(maticVal);
46+
// console.log(usdVal);
4847

4948
useEffect(() => {
5049
if(!profile) return;
@@ -100,16 +99,16 @@ const DashLeft = () => {
10099
/> */}
101100
<div className="flex flex-row items-center gap-x-[10px] text-[14px] text-white w-auto h-[50px] p-[5px] px-[8px] rounded-[20px] font-medium pr-[15px] ml-[10px] bg-[#404557]">
102101
<div className="flex justify-center w-[40px] h-[40px] rounded-full bg-white p-[4px] box-border">
103-
<img src="./assets/matic.svg" alt="maticLogo"/>
102+
<img src="./assets/eth.png" alt="ethLogo"/>
104103
</div>
105-
MATIC
104+
ETH
106105
</div>
107-
{/* <span className="m-2">MATIC</span> */}
106+
{/* <span className="m-2">ETH</span> */}
108107
</div>
109108
<div>
110-
<div style={{ color: "#696c80" }}>Price</div>
109+
<div className="text-right mr-[15px]" style={{ color: "#696c80" }}>Price</div>
111110
<div>
112-
{ maticVal.rate.toFixed(2) }
111+
${ maticVal.rate.toFixed(2) }
113112
</div>
114113
</div>
115114
</div>
@@ -131,16 +130,16 @@ const DashLeft = () => {
131130
<img className="w-[40px] h-[40px] rounded-full" src="./assets/usdc.svg" alt="usdcLogo"/>
132131
USDT
133132
</div>
134-
{/* <span className="m-2">MATIC</span> */}
133+
{/* <span className="m-2">ETH</span> */}
135134
</div>
136135
<div>
137136
<div>Price</div>
138137
<div>
139-
{ usdVal.rate.toFixed(2) }
138+
${ usdVal.rate.toFixed(2) }
140139
</div>
141140
</div>
142141
</div>
143-
<div className="m-2 text-[#4489ff] text-[12px] overflow-ellipsis overflow-hidden">0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270</div>
142+
<div className="m-2 text-[#4489ff] text-[12px] overflow-ellipsis overflow-hidden">0x466DD1e48570FAA2E7f69B75139813e4F8EF75c2</div>
144143
</div>
145144
</Container>
146145

@@ -169,18 +168,16 @@ const DashLeft = () => {
169168
/> */}
170169
<div className="flex flex-row items-center gap-x-[10px] text-[14px] text-white w-auto h-[50px] p-[5px] px-[8px] rounded-[20px] font-medium pr-[15px] ml-[10px] bg-[#404557]">
171170
<div className="flex justify-center w-[40px] h-[40px] rounded-full bg-white p-[4px] box-border">
172-
<img src="./assets/matic.svg" alt="maticLogo"/>
171+
<img src="./assets/eth.png" alt="ethLogo"/>
173172
</div>
174-
MATIC
173+
ETH
175174
</div>
176-
{/* <span className="m-2">MATIC</span> */}
175+
{/* <span className="m-2">ETH</span> */}
177176
</div>
178177
<div>
179178
<div style={{ color: "#696c80" }}>Price</div>
180179
<div>
181-
{maticVal.rate !== undefined
182-
? maticVal.rate
183-
: Math.round(dummyMatic).toFixed(2)}
180+
{ maticBalance.toFixed(4) } (${ (maticVal.rate * maticBalance).toFixed(2) })
184181
</div>
185182
</div>
186183
</div>
@@ -200,16 +197,14 @@ const DashLeft = () => {
200197
/> */}
201198
<div className="flex flex-row items-center gap-x-[10px] text-[14px] text-white w-auto h-[50px] p-[5px] px-[8px] rounded-[20px] font-medium pr-[15px] ml-[10px] bg-[#404557]">
202199
<img className="w-[40px] h-[40px] rounded-full" src="./assets/usdc.svg" alt="usdcLogo"/>
203-
USDC
200+
USDT
204201
</div>
205-
{/* <span className="m-2">MATIC</span> */}
202+
{/* <span className="m-2">ETH</span> */}
206203
</div>
207204
<div>
208205
<div>Price</div>
209206
<div>
210-
{usdVal.rate !== undefined
211-
? usdVal.rate
212-
: Math.round(dummyMatic).toFixed(2)}
207+
{ usdtBalance.toFixed(2) } (${ (usdVal.rate * usdtBalance).toFixed(2) })
213208
</div>
214209
</div>
215210
</div>
@@ -241,7 +236,7 @@ const DashLeft = () => {
241236
height="50"
242237
style={{ borderRadius: "50px" }}
243238
/>
244-
<span className="m-2">MATIC</span>
239+
<span className="m-2">ETH</span>
245240
</div>
246241
<div>
247242
<div></div>

0 commit comments

Comments
 (0)