forked from tusharnagarop/knowyourglobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavbar.js
255 lines (230 loc) · 7.61 KB
/
navbar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import React from 'react';
import './stake.css';
import Progress from 'react-progressbar';
import { ethers, providers } from "ethers";
import values from "../../values.json"
import stakingAbi from '../../abi/staking.json';
import tokenAbi from '../../abi/token.json';
import {provider, setProvider, signer, setSigner} from '../../App';
const Stake = () => {
let [poolId, setPoolId] = React.useState(0);
let [poolInfo, setPoolInfo] = React.useState([]);
let [userInfo, setUserInfo] = React.useState([]);
let [whitelistedAddresses, setWalletAddresses] = React.useState([]);
let [amount, setAmount] = React.useState(0);
let [balance, setBalance] = React.useState(0);
let [stakingBalance, setStackingBalance] = React.useState(0);
let [currentPoolSize, setCurrentPoolSize] = React.useState(0);
let [maxPoolSize, setMaxPoolSize] = React.useState(0);
let [timeLock, setTimeLock] = React.useState(0);
let [myerror, setmyerror] = React.useState()
let _provider = React.useContext (provider);
let _setProvider = React.useContext (setProvider);
let _signer = React.useContext (signer);
let _setSigner = React.useContext (setSigner);
React.useEffect(()=>{
getPoolInfo();
getUserInfo();
getWhiteListAddresses();
async function fetch (){
try{
let _balance = await _getBalance(values.token);
console.log("BAlance", _balance);
setBalance(_balance);
}catch (err){
console.log("Error", err);
}
}
fetch();
}, [_provider, _signer, poolId]);
//get info function
async function getPoolInfo (){
try{
let rpcUrl = values.rpcUrl;
let provider_ = new ethers.providers.JsonRpcProvider(rpcUrl);
let staking = new ethers.Contract(
values.stakingAddress,
stakingAbi,
provider_
);
var _poolInfo = await staking.poolInfo(poolId);
console.log ("Pool Info: ", _poolInfo);
setPoolInfo(_poolInfo);
let temp = ethers.utils.formatUnits(_poolInfo[2].toString(), decimals).toString()
console.log ("temp: ", temp, " value: ", _poolInfo[2].toString());
setCurrentPoolSize(temp);
temp = ethers.utils.formatUnits(_poolInfo[1].toString(), decimals).toString()
setMaxPoolSize(temp)
}catch(err){
console.log(err);
}
}
async function _getBalance (tokenAddress, accountAddress){
try {
let rpcUrl = values.rpcUrl;
let provider_ = new ethers.providers.JsonRpcProvider(rpcUrl);
let token = new ethers.Contract(
tokenAddress,
tokenAbi,
provider_
);
if (!accountAddress){
accountAddress = await _signer.getAddress();
}
let balance = await token.balanceOf (accountAddress);
console.log ("Balalala", balance)
let decimals = await token.decimals();
decimals = parseInt(decimals.toString());
balance = ethers.utils.formatUnits(balance, decimals);
return parseFloat(balance.toString()).toFixed(2);
} catch (err){
console.log (err, tokenAddress);
return 0;
}
}
// removed bugs
//get stake tokens function
async function stakeTokens () {
try{
let staking = new ethers.Contract(
values.stakingAddress,
stakingAbi,
_signer
);
let _amount = ethers.utils.parseUnits(amount.toString(), decimals);
// console.log (_amount)
let tx = await staking.stakeTokens(poolId, _amount);
await tx.wait();
getPoolInfo();
getUserInfo();
}catch (error) {
if (error.data)
alert(error.data.message);
console.log (error)
}
}
async function unstakeTokens () {
try{
let staking = new ethers.Contract(
values.stakingAddress,
stakingAbi,
_signer
);
let tx = await staking.unstakeTokens(poolId);
await tx.wait();
getPoolInfo();
getUserInfo();
}catch (error) {
if (error.data)
alert(error.data.message);
console.log (error);
}
}
async function claimTokens () {
try{
let staking = new ethers.Contract(
values.stakingAddress,
stakingAbi,
_signer
);
let tx = await staking.claimTokens(poolId);
await tx.wait();
getPoolInfo();
getUserInfo();
}catch (error) {
if (error.data)
alert(error.data.message);
console.log (error);
}
}
async function emergencyWithdraw () {
try{
let staking = new ethers.Contract(
values.stakingAddress,
stakingAbi,
_signer
);
let tx = await staking.emergencyWithdraw(poolId);
await tx.wait();
getPoolInfo();
getUserInfo();
}catch (error) {
if (error.data)
alert (error.data.message);
console.log (error);
}
}
async function approve () {
try{
let token = new ethers.Contract(
values.token,
tokenAbi,
_signer
);
let _amount = ethers.utils.parseUnits("10000000000000000000", decimals);
let tx = await token.approve(values.stakingAddress, _amount);
await tx.wait();
stakeTokens()
}catch (error) {
// alert(error.data.message);
}
}
function handleChange(event) {
const { name, value } = event.target;
setAmount(value);
// console.log (amount);
}
const decimals = 18;
return (
<div>
<div className='landing'>
<div className='stak_box'>
<div className='stak_heading'>
<h2>STAKE YOUR TOKEN</h2>
</div>
<div className='stak_bar'>
<Progress color="#339CEE" completed={(parseFloat(currentPoolSize)* 100)/parseFloat(maxPoolSize)} height={20} data-label={`${(parseFloat(currentPoolSize)* 100)/parseFloat(maxPoolSize)}% Pool Filled`} />
</div>
{/* <Timer /> */}
<div className='stak_info'>
<p>Estimated APY : <span className='text-blue'>{`10.36%`}</span></p>
<p>My Balance : <span className='text-blue'>{balance}</span> </p>
<p>My Stakable Balance : <span className='text-blue'>{stakingBalance}</span></p>
</div>
<div className='inputs'>
<div className='inputbox'>
<div>
<label>Stake Your Token</label>
</div>
<div className="input1">
<input placeholder='Enter Token Amount' onChange= {(e)=> handleChange(e)} value= {amount} type="number" />
<div className='maxToken'>
<p onClick= {()=> setAmount(balance)} >MAX</p>
</div>
</div>
<div className='inputbox'>
{/* <div>
<label>Staked Token</label>
</div>
<input placeholder={`Show Staked Amount`} readOnly/> */}
</div>
</div>
</div>
<div className='stak_info'>
<p>Current Pool Size : <span className='text-blue'>{currentPoolSize}</span></p>
<p className='text-red'> {myerror}</p>
{/* <p>My Total Claimed Token : <span className='text-blue'>{`632123`}</span></p>
<p>Unstake Fee : <span className='text-blue'>{`0%`}</span></p> */}
</div>
<div className='all_buttons'>
<button className='greenButton' onClick={approve} >STAKE</button>
<button className='greenButton'onClick={claimTokens} >CLAIM</button>
<button className='greenButton' onClick={unstakeTokens}>UNSTAKE</button>
<button className='redbutton' onClick={emergencyWithdraw}>EMERGENCY UNSTAKE</button>
</div>
</div>
</div>
</div>
)
}
export default Stake