-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
197 lines (196 loc) · 6.15 KB
/
Copy pathscript.js
File metadata and controls
197 lines (196 loc) · 6.15 KB
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
let provider;
let signer;
let contract;
let contractAddress;
let abi;
let userAddress;
async function connect(){
if(window.ethereum){
provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts",[]);
signer = provider.getSigner();
userAddress = await signer.getAddress();
contractAddress = "0x468A022767926F84288283b15584d115baF8B060";
abi = [
"function stake() payable",
"function myStake() view returns(uint256)",
"function calculateMyReward() view returns(uint256)",
"function unstake()",
"function totalStakes() view returns(uint256)",
"function APY() view returns(uint256)",
"function balance(address) view returns(uint256)",
"function startTime(address) view returns(uint256)",
"function allWithdraw()",
"function getBalance() view returns(uint256)"
];
contract = new ethers.Contract(contractAddress,abi,signer);
console.log(`Wallet Connected To : ${userAddress}`)
alert("Wallet Connected To : "+userAddress);
switchToSepolia();
await poolbalance();
}else{
alert("Please Install Metamask");
}
}
// Force switch to Sepolia network
async function switchToSepolia(){
try {
await window.ethereum.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: "0xaa36a7" }], // Sepolia chainId in hex
});
alert("Switched To Sepolia");
} catch (switchError) {
// This error code means the chain has not been added to MetaMask
if (switchError.code === 4902) {
try {
await window.ethereum.request({
method: "wallet_addEthereumChain",
params: [{
chainId: "0xaa36a7",
chainName: "Sepolia Testnet",
nativeCurrency: {
name: "SepoliaETH",
symbol: "ETH",
decimals: 18
},
rpcUrls: ["https://rpc.sepolia.org/"],
blockExplorerUrls: ["https://sepolia.etherscan.io"]
}],
});
alert("Seitched To Sepolia");
} catch (addError) {
alert("Failed to add Sepolia: " + addError.message);
}
} else {
alert("Switch to Sepolia failed: " + switchError.message);
}
}
}
async function checkWalletIsConnected(){
if(!contract){
throw new Error("Please Connect Wallet First");
return;
}
}
async function unstake(){
try{
await checkWalletIsConnected();
let poolbalance = await contract.getBalance();
userAddress = await signer.getAddress();
let userBalance = await contract.balance(userAddress);
if(poolbalance.lt(userBalance)){
alert("Pool Balance Is Not Enough To Unstake");
return;
}
try{
let tx = await contract.unstake();
await tx.wait();
alert("Unstaking Successful");
poolbalance();
}catch(err){
alert(`Error Unstaking : ${err.message}`);
}
}catch(err){
alert(`Error : ${err.message}`);
}
}
async function stake(){
try{
let amount;
let stakedBalance;
let valueEth;
let balanceUser;
let tx;
await checkWalletIsConnected();
amount = document.getElementById("amount").value;
if(!amount||isNaN(amount)||amount<0){
alert("Please Enter Valid Amount");
return;
}
console.log(`amount Entered : ${amount}`);
stakedBalance = await contract.myStake();
if(stakedBalance.gt(0)){
alert(`Already Staked\nYour Staked Balance : ${ethers.utils.formatEther(stakedBalance)}ETH`);
return;
}
try{
valueEth = ethers.utils.parseEther(amount);
console.log(`Ethers Parsed : ${valueEth}`);
balanceUser = await signer.getBalance();
console.log(`Use Balance : ${balanceUser}`);
}catch(err){
console.log(`Error getting values : ${err.message||err.reason}`);
}
if(balanceUser.lt(valueEth)){
alert(`Not Enough Balance\nYour Balance : ${ethers.utils.formatEther(balanceUser)}ETH`);
return;
}
try{
tx = await contract.stake({value : valueEth});
await tx.wait();
alert("Staking Successful");
console.log("Staking Successful");
await poolbalance();
}catch(err){
alert("Error In Staking : "+(err.reason||err.message));
}
}catch(err){
alert(`Error : ${err.message}`);
}
}
async function myStake(){
try{
await checkWalletIsConnected();
let stakedBalance = await contract.balance(userAddress);
if(stakedBalance.lte(0)){
alert("You Have Not Staked");
return;
}
userAddress = await signer.getAddress();
const time = await contract.startTime(userAddress);
const date = new Date(time*1000);
alert(`Your Staked Balance : ${ethers.utils.formatEther(stakedBalance)}ETH\nStarted on : ${date.toString()}`);
}catch(err){
alert(`Error : ${err.message}`);
}
}
async function poolbalance(){
try{
let balance = await provider.getBalance(contractAddress);
let stakes = await contract.totalStakes();
userAddress = await signer.getAddress();
document.getElementById("top-right").style.display = "block";
document.getElementById("userAddress").textContent = `User Address : ${userAddress}`;
document.getElementById("totalstakes").textContent = `Total User Stakes : ${stakes.toString()}`;
document.getElementById("balance").textContent = `Pool Balance : ${ethers.utils.formatEther(balance)}ETH`;
}catch(err){
console.log("Error Displaying : ",err.message);
}
}
async function calculateReward(){
try{
await checkWalletIsConnected();
let stakedBalance = await contract.myStake();
if(stakedBalance.lte(0)){
alert("You Have Not Staked");
return;
}
let reward = await contract.calculateMyReward();
const staked = parseFloat(ethers.utils.formatEther(stakedBalance));
let earnedReward = parseFloat(ethers.utils.formatEther(reward));
let total = staked+earnedReward;
alert(`Your Staked Balance : ${staked}ETH\nYour Reward : ${ethers.utils.formatEther(reward)}ETH\nTotal Balance : ${total}ETH`)
}catch(err){
alert(`Error : ${err.message}`);
}
}
async function checkAPY(){
try{
await checkWalletIsConnected();
let Apy = await contract.APY();
alert(`Current APY : ${Apy}%`);
}catch(err){
alert(`Error : ${err.message}`);
}
}