-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteract.js
168 lines (157 loc) · 4.32 KB
/
interact.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
//const {ethers} = require('ethers');
//import { ethers } from "ethers";
// const { ethers } = require("ethers");
// const express = require('express');
// const bodyParser = require('body-parser');
// const https = require('https');
// const app = express();
// const provider = new ethers.providers.JsonRpcProvider("https://goerli.infura.io/v3/3257f723cdc840198733d629c17c7cd2");
const provider = new ethers.providers.Web3Provider(window.ethereum);
const ContractAddress ="0x82cB090B231C55aF7b4fc462AED822eDA3267996";
const abi = [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Received",
"type": "event"
},
{
"inputs": [],
"name": "GetBalance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "Amount",
"type": "uint256"
},
{
"internalType": "address",
"name": "sendTo",
"type": "address"
}
],
"name": "Send",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "Withdraw",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"stateMutability": "payable",
"type": "receive"
}
];
const contract = new ethers.Contract(ContractAddress,abi,provider);
let amount,from,flag;
// Copy to clipboard
document.getElementById('CopyButton').onclick = function () {
var text = document.getElementById('myaddress');
navigator.clipboard.writeText(text.value).then(function () {
let button = document.getElementById('CopyButton')
button.classList.add('cpybtn');
button.innerHTML = "copied!";
setTimeout(function () { button.classList.remove('cpybtn'); button.innerHTML = "copy"; }, 1000);
document.getElementById('CBR').click();
})
}
// Update Balance function
const UpdateBalance = async() =>{
var bal = ethers.utils.formatEther(await contract.GetBalance());
document.getElementById('balance').innerHTML = (bal + " ETH");
document.getElementById('myaddress').value = ContractAddress ;
}
UpdateBalance();
// On send transaction and balance update
document.getElementById('SendBtn').onclick = async function(){
var sendAdd = document.getElementById('inputAddress').value ;
var Amt = document.getElementById('amount').value;
await provider.send("eth_requestAccounts",[]);
const signer = provider.getSigner();
const SignContract = new ethers.Contract(ContractAddress,abi,signer);
const txnResponse = await SignContract.Send(ethers.utils.parseEther(Amt),sendAdd);
document.getElementById('CBS').click();
setTimeout(function() {alert("Transaction initiated !")},1500);
const txnHash = txnResponse.hash;
await provider.waitForTransaction(txnHash);
flag = 1; amount = Amt ; from = sendAdd;
UpdateBalance();
UpdateLastTansaction(from,amount,flag);
}
// On receive balance update
contract.on('Received', (fromContract, amtContract, event) => {
UpdateBalance();
flag = 0; amount = ethers.utils.formatEther(amtContract); from = fromContract;
UpdateLastTansaction(from,amount,flag);
})
// update last transaction
function UpdateLastTansaction(from ,amount,flag){
localStorage.setItem('updateFrom',from);
if(localStorage.getItem('updateFrom')){
document.getElementById('LastTxnAdd').innerHTML = localStorage.getItem('updateFrom');
}
if(flag == 1){
localStorage.setItem('updateAmt',"-"+amount+" ETH");
if(localStorage.getItem('updateAmt')){
document.getElementById('LastTxnBal').innerHTML = localStorage.getItem('updateAmt');
}
}
else{
localStorage.setItem('updateAmt',"+"+amount+" ETH");
if(localStorage.getItem('updateAmt')){
document.getElementById('LastTxnBal').innerHTML = localStorage.getItem('updateAmt');
}
}
}
UpdateLastTansaction();
async function Text(){
console.log(ethers.utils.formatEther(await contract.GetBalance()));
}
Text();