This repository was archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
235 lines (208 loc) · 7.65 KB
/
Copy pathapp.js
File metadata and controls
235 lines (208 loc) · 7.65 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
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
// Modern dapp browsers...
let web3Provider =
"wss://ropsten.infura.io/ws/v3/aae71ad4abb14859a9a7b91c34f8839e"
let web3 = null
let DglDonationAddress = "0xFbaaffc28904ADd40dc695432BB035B8Ca1464ed"
let DglDonationContract = null
let numOfDonations = 0
let numOfWithdraws = 0
let donationTableBody = document.querySelector("#donation-table tbody")
let withdrawTableBody = document.querySelector("#withdraw-table tbody")
// Elements
const totalValueEle = document.getElementById("total-value")
const donateButton = document.getElementById("donate-button")
function startApp() {
// Init web3
web3 = new Web3(web3Provider)
// Create contract instance
fetch("./build/DGLDonation.json").then((response) => {
// Parse data to json
response.json().then((data) => {
// Create instance with ABI and address
DglDonationContract = new web3.eth.Contract(
data.abi,
DglDonationAddress
)
// Get the total value
DglDonationContract.methods
.totalDonation()
.call()
.then((_totalValue) => {
totalValueEle.textContent = web3.utils.fromWei(_totalValue)
})
// Get the number of donations
DglDonationContract.methods
.donationsCounter()
.call()
.then((_counter) => {
// If existed
if (_counter > 0) {
numOfDonations = _counter
document.getElementById(
"donation-total-tx"
).textContent = _counter
// Update content of donation table
displayTables("donation")
}
})
// Get the number of withdraws
DglDonationContract.methods
.withdrawCounter()
.call()
.then((_counter) => {
// If existed
if (_counter > 0) {
numOfWithdraws = _counter
document.getElementById(
"withdraw-total-tx"
).textContent = _counter
// Update content of withdraw table
displayTables("withdraw")
}
})
})
})
// Donate handler
donateButton.addEventListener("click", async () => {
const amountElement = document.getElementById("donate-amount")
const donateAmount = parseFloat(amountElement.value)
const messageElement = document.getElementById("donate-message")
const donateMsg = messageElement.value
// Check amount is greater than 0
if (donateAmount <= 0) {
// Display error
displayError("Amount is invalid!")
return
}
// Donate
await donateHandler(donateAmount, donateMsg)
})
}
function displayTables(table = "donation") {
if (table === "donation") {
donationTableBody.innerHTML = ""
// Display donation table
for (let i = 0; i < numOfDonations; i++) {
DglDonationContract.methods
.donations(i)
.call()
.then(({ amount, message, owner, timestamp }) => {
const tblRow = createTableRow(
owner,
amount,
message,
timestamp
)
donationTableBody.appendChild(tblRow)
})
}
}
if (table === "withdraw") {
withdrawTableBody.innerHTML = ""
let ownerAddress = ""
DglDonationContract.methods
.owner()
.call().then(address => ownerAddress = address)
// Display donation table
for (let i = 0; i < numOfWithdraws; i++) {
DglDonationContract.methods
.withdrawRecords(i)
.call()
.then(({amount, message, timestamp}) => {
const tblRow = createTableRow(
ownerAddress,
amount,
message,
timestamp
)
withdrawTableBody.appendChild(tblRow)
}).catch(e => console.error(e))
}
}
}
async function donateHandler(_donateAmount, _donateMsg) {
// Estimate total gas
const estimateGas = await DglDonationContract.methods
.donate(_donateMsg)
.estimateGas({
gas: 5000000,
from: ethereum.selectedAddress,
value: web3.utils.toWei(_donateAmount.toString()),
})
const transactionParameters = {
// gasPrice: await web3.eth.getGasPrice(), // customizable by user during MetaMask confirmation.
gas: web3.utils.toHex(estimateGas), // customizable by user during MetaMask confirmation.
to: DglDonationAddress, // Required except during contract publications.
from: ethereum.selectedAddress, // must match user's active address.
value: web3.utils.toHex(
web3.utils.toWei(_donateAmount.toString()).toString()
), // Only required to send ether to the recipient from the initiating external account.
data: DglDonationContract.methods.donate(_donateMsg).encodeABI(), // Optional, but used for defining smart contract creation and interaction.
}
ethereum
.request({
method: "eth_sendTransaction",
params: [transactionParameters],
})
.then((txHash) => console.log(txHash))
.catch((error) => console.error)
}
function displayError(_message) {
const errorElement = document.getElementById("donate-error")
errorElement.textContent = _message
errorElement.classList.remove("d-none")
}
function createTableRow(_fromAddress, _amount, _message, _datetime) {
const row = document.createElement("tr")
// From column
const fromCol = document.createElement("th")
fromCol.setAttribute("scope", "row")
fromCol.textContent = _fromAddress
// Amount column
const amountCol = document.createElement("td")
amountCol.textContent = web3.utils.fromWei(_amount)
// Message column
const messageCol = document.createElement("td")
messageCol.textContent = _message
// Datetime column
const datetimeCol = document.createElement("td")
datetimeCol.textContent = new Date(_datetime * 1000).toLocaleDateString(
"en-US",
{
day: "2-digit",
month: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
}
)
row.appendChild(fromCol)
row.appendChild(amountCol)
row.appendChild(messageCol)
row.appendChild(datetimeCol)
return row
}
window.addEventListener("load", async function () {
// Modern dapp browsers...
if (window.ethereum) {
try {
// Request account access
await window.ethereum.request({ method: "eth_requestAccounts" })
} catch (error) {
// User denied account access...
console.error("User denied account access")
}
}
// Legacy dapp browsers...
else if (window.web3) {
web3Provider = window.web3.currentProvider
}
// If no injected web3 instance is detected, fall back to Ganache
else {
web3Provider = new Web3.providers.HttpProvider("http://localhost:7545")
}
console.log(web3Provider)
// Now you can start your app & access web3 freely:
startApp()
})