Skip to content

Commit d799fe2

Browse files
committed
Token rates & values
Signed-off-by: Omkar Phansopkar <omkarphansopkar@gmail.com>
1 parent ace93de commit d799fe2

File tree

6 files changed

+108
-35
lines changed

6 files changed

+108
-35
lines changed

src/apis/balances.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { hasWindow } from "../util/next-utils";
2+
import { web3 } from "./Contract";
3+
import TokenABI from '../contracts/UsdtABI.json';
4+
5+
const UsdtContract = new web3.eth.Contract(TokenABI, '0X466DD1E48570FAA2E7F69B75139813E4F8EF75C2');
6+
export const getUsdtBalance = async (address) => {
7+
const balance = await UsdtContract.methods.balanceOf(address).call();
8+
return balance ? balance / 1000000 : 0.00;
9+
}
10+
11+
export const getMaticBalance = async (address) => {
12+
const balance = await web3.eth.getBalance(address);
13+
return balance ? balance / 1000000000000000000 : 0.00;
14+
}
15+
16+
if(hasWindow()){
17+
window.getUsdtBalance = getUsdtBalance;
18+
window.getMaticBalance = getMaticBalance;
19+
}

src/apis/factory.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ export function CallerFn(method, debug, ...params){
1010
}
1111
resolve(res);
1212
}).catch((err) => {
13-
console.log(`Some error calling ${method} with params \n`, params, err);
13+
if(debug)
14+
console.log(`Some error calling ${method} with params \n`, params, err);
1415
reject(new Error(`Couldn't fetch results for ${method}`));
1516
});
1617
});
1718
}
1819

1920
export function SenderFn(method, senderAddress, debug, ...params){
2021
return new Promise((resolve, reject) => {
21-
const tx = Contract.methods.addPost(...params);
22+
const tx = Contract.methods[method](...params);
2223
if(debug)
2324
console.log("Prepared transaction: ", tx);
2425

src/apis/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CallerFn, SenderFn } from "./factory";
44
export const getProfile = (address) => {
55
if(!address)
66
console.log("Address invalid");
7-
return CallerFn('getUser', true, address);
7+
return CallerFn('getUser', false, address);
88
}
99

1010
export const createProfile = (address, name) => {

src/components/DashLeft/DashLeft.jsx

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@ import Image from "next/image";
66
import { useState } from "react";
77
import { useEffect } from "react";
88
import { Container } from "@mui/material";
9+
import { useMetamaskAuth } from "../../auth/authConfig";
10+
import Loader from "../Loader/Loader";
11+
import { getUsdtBalance, getMaticBalance } from "../../apis/balances";
912
const https = require("https");
1013

14+
15+
const randPriceFluctuations = range => Number((Math.random() * (range*2 + 1)-range).toFixed(2));
16+
1117
const DashLeft = () => {
12-
const [maticVal, setMaticVal] = useState({});
13-
const [usdVal, setUsdVal] = useState({});
18+
const [maticVal, setMaticVal] = useState({ rate: 1500 + randPriceFluctuations(3)});
19+
const [usdVal, setUsdVal] = useState({ rate: 1 + Number((randPriceFluctuations(1)/100).toFixed(2)) });
20+
const [maticBalance, setMaticBalance] = useState(0);
21+
const [usdtBalance, setUsdtBalance] = useState(0);
22+
const { profile, isLoggedIn, isProcessingLogin } = useMetamaskAuth();
23+
1424
const dummyMatic = "3260.351432121505620812986766";
1525
var myHeaders = new Headers();
1626
myHeaders.append("X-CoinAPI-Key", "272E2537-42DC-4DE3-BB31-89072ACE051D");
@@ -22,17 +32,31 @@ const DashLeft = () => {
2232
};
2333

2434
useEffect(() => {
25-
fetch("https://rest.coinapi.io/v1/exchangerate/BTC/MATIC/", requestOptions)
26-
.then((response) => response.text())
27-
.then((result) => setMaticVal(JSON.parse(result)))
28-
.catch((error) => console.log("error", error));
35+
// fetch("https://rest.coinapi.io/v1/exchangerate/ETH/USD/", requestOptions)
36+
// .then((response) => response.text())
37+
// .then((result) => setMaticVal(JSON.parse(result)))
38+
// .catch((error) => console.log("error", error))
2939

30-
fetch("https://rest.coinapi.io/v1/exchangerate/BTC/USD/", requestOptions)
31-
.then((response) => response.text())
32-
.then((result) => setUsdVal(JSON.parse(result)))
33-
.catch((error) => console.log("error", error));
40+
// fetch("https://rest.coinapi.io/v1/exchangerate/USDT/USD/", requestOptions)
41+
// .then((response) => response.text())
42+
// .then((result) => setUsdVal(JSON.parse(result)))
43+
// .catch((error) => console.log("error", error))
3444
}, []);
3545

46+
console.log(maticVal);
47+
console.log(usdVal);
48+
49+
useEffect(() => {
50+
if(!profile) return;
51+
getMaticBalance(profile.address)
52+
.then(setMaticBalance);
53+
getUsdtBalance(profile.address)
54+
.then(setUsdtBalance);
55+
}, [profile]);
56+
57+
if(!isLoggedIn || isProcessingLogin || !profile)
58+
return <Loader size="80px" />
59+
3660
return (
3761
<div className="w-[100%] h-[100%] font-inter overflow-auto" style={{backgroundColor : '#12131A'}}>
3862
{/* PROFILE */}
@@ -45,9 +69,13 @@ const DashLeft = () => {
4569
height="50"
4670
style={{ borderRadius: "50px" }}
4771
/>
48-
<div className="text-3xl ml-3">Vansh</div>
72+
<div className="text-3xl ml-3">
73+
{ profile.name }
74+
</div>
75+
</div>
76+
<div>
77+
{ profile.address }
4978
</div>
50-
<div>wallet address</div>
5179
</Container>
5280

5381
{/* CARDS */}
@@ -75,13 +103,11 @@ const DashLeft = () => {
75103
<div>
76104
<div style={{ color: "#696c80" }}>Price</div>
77105
<div>
78-
{maticVal.rate !== undefined
79-
? maticVal.rate
80-
: Math.round(dummyMatic).toFixed(2)}
106+
{ maticVal.rate.toFixed(2) }
81107
</div>
82108
</div>
83109
</div>
84-
<div className="m-2">code</div>
110+
<div className="m-2 h-1"></div>
85111
</div>
86112
<hr style={{ background: "#C77DFF", height: "0.05px" }} />
87113

@@ -95,18 +121,18 @@ const DashLeft = () => {
95121
height="50"
96122
style={{ borderRadius: "50px" }}
97123
/>
98-
<span className="m-2">MATIC</span>
124+
<span className="m-2">USDT</span>
99125
</div>
100126
<div>
101127
<div>Price</div>
102128
<div>
103-
{usdVal.rate !== undefined
104-
? usdVal.rate
105-
: Math.round(dummyMatic).toFixed(2)}
129+
{ usdVal.rate.toFixed(2) }
106130
</div>
107131
</div>
108132
</div>
109-
<div className="mt-2">code</div>
133+
<div className="mt-2">
134+
0x466DD1e48570FAA2E7f69B75139813e4F8EF75c2
135+
</div>
110136
</div>
111137
</Container>
112138

@@ -138,14 +164,12 @@ const DashLeft = () => {
138164
</div>
139165
<div>
140166
<div></div>
141-
<div>
142-
{maticVal.rate !== undefined
143-
? maticVal.rate
144-
: Math.round(dummyMatic).toFixed(2)}
167+
<div className="mt-1">
168+
{ maticBalance.toFixed(4) } (${ (maticVal.rate * maticBalance).toFixed(2) })
145169
</div>
146170
</div>
147171
</div>
148-
<div className="m-2">code</div>
172+
<div className="m-2 h-1"></div>
149173
</div>
150174
<hr style={{ background: "#C77DFF", height: "0.05px" }} />
151175
<div>
@@ -158,17 +182,15 @@ const DashLeft = () => {
158182
height="50"
159183
style={{ borderRadius: "50px" }}
160184
/>
161-
<span className="m-2">MATIC</span>
185+
<span className="m-2">USDT</span>
162186
</div>
163187
<div>
164-
<div>
165-
{usdVal.rate !== undefined
166-
? usdVal.rate
167-
: Math.round(dummyMatic).toFixed(2)}
188+
<div className="mt-2">
189+
{ usdtBalance.toFixed(2) } (${ (usdVal.rate * usdtBalance).toFixed(2) })
168190
</div>
169191
</div>
170192
</div>
171-
<div className="mt-2">code</div>
193+
<div className="mt-2"></div>
172194
</div>
173195
</Container>
174196
</div>

src/contracts/UsdtABI.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[{
2+
"constant": true,
3+
"inputs": [
4+
{
5+
"name": "_owner",
6+
"type": "address"
7+
}
8+
],
9+
"name": "balanceOf",
10+
"outputs": [
11+
{
12+
"name": "balance",
13+
"type": "uint256"
14+
}
15+
],
16+
"payable": false,
17+
"type": "function"
18+
}]

src/util/units.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const usdtToUnits = (usdt) => {
2+
if(!usdt)
3+
return 0;
4+
return Number(usdt) * 1000000;
5+
}
6+
7+
export const dateToUnits = (date) => {
8+
if(!date)
9+
date = new Date();
10+
return date / 1000;
11+
}
12+
13+
export const currDateToUnits = () => dateToUnits(new Date());

0 commit comments

Comments
 (0)