-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession4.js
126 lines (111 loc) · 3.21 KB
/
session4.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
const abi = [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "donor",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "DonationReceived",
"type": "event"
},
{
"inputs": [],
"name": "donate",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "donations",
"outputs": [
{
"internalType": "address",
"name": "donor",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "timestamp",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getBalance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address payable",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
];
async function makeDonation() {
const contractAddress = "0xFB5271cC736d130Eda93B9BEB7871fa939f40075";
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
// Get the donation amount from the input
const donationAmount = ethers.utils.parseEther("0.001");
// Call the donate function with the specified amount
const tx = await contract.donate({ value: donationAmount });
// Wait for the transaction to be mined
await tx.wait();
//console.log("Balance:", ethers.utils.formatUnits(balance, 18)); // Assuming 18 decimals
}
async function checkBalance() {
const contractAddress = "0xFB5271cC736d130Eda93B9BEB7871fa939f40075";
const provider = new ethers.providers.Web3Provider(window.ethereum);
const contract = new ethers.Contract(contractAddress, abi, provider);
console.log("calling balance");
const balance = await contract.getBalance();
console.log(balance);
console.log("Balance:", ethers.utils.formatUnits(balance, 18)); // Assuming 18 decimals
console.log("balance fetched");
}