Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
214 lines (200 loc) · 4.71 KB
/
Copy pathapp.js
File metadata and controls
214 lines (200 loc) · 4.71 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
const contractAddress='0x62d92ea2573702f69d62cffe28395120b8bd2cd1';
//ABI: collection of functions and data structures that the smart contract exposes to the outside world
const contractABI=[
{
"inputs": [
{
"internalType": "uint256",
"name": "points",
"type": "uint256"
}
],
"name": "earnPoints",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "user",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "pointsEarned",
"type": "uint256"
}
],
"name": "PointsEarned",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "user",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "pointsRedeemed",
"type": "uint256"
}
],
"name": "PointsRedeemed",
"type": "event"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "pointsToRedeem",
"type": "uint256"
}
],
"name": "redeemPoints",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "transferOwnership",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "newName",
"type": "string"
}
],
"name": "updateProgramName",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "user",
"type": "address"
}
],
"name": "getUserPoints",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "loyaltyPoints",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "programName",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
}
];
async function main() {
// Checking if the Ethereum provider (MetaMask) is available
if (typeof window.ethereum !== 'undefined') {
// Requesting Metamask to enable accounts
await window.ethereum.enable();
const accounts = await window.ethereum.request({ method: 'eth_accounts' });
const userAccount = accounts[0];
// Creating an instance of the contract deployed on Sepolia TestNet
const web3 = new Web3(window.ethereum); // Create a web3 instance
const loyaltyContract = new web3.eth.Contract(contractABI, contractAddress);
// Displaying program name and user points
displayProgramName(loyaltyContract);
displayUserPoints(loyaltyContract, userAccount);
// Adding event listeners to buttons
document.getElementById('earnButton').addEventListener('click', () => earnPoints(loyaltyContract, userAccount));
document.getElementById('redeemButton').addEventListener('click', () => redeemPoints(loyaltyContract, userAccount));
} else {
console.log('MetaMask not detected. Please install the MetaMask extension.');
}
}
async function displayProgramName(contract) {
console.log(contract)
const programName = await contract.methods.programName().call();
document.getElementById('programName').textContent = programName;
}
async function displayUserPoints(contract, user) {
const points = await contract.methods.getUserPoints(user).call();
document.getElementById('userPoints').textContent = points;
}
async function earnPoints(contract, user) {
await contract.methods.earnPoints(100).send({ from: user });
displayUserPoints(contract, user);
}
async function redeemPoints(contract, user) {
await contract.methods.redeemPoints(50).send({ from: user });
displayUserPoints(contract, user);
}
async function updateProgramName(contract, user) {
await contract.methods.updateProgramName("LOYALTY REWARD PROGRAM").send({ from: user });
displayUserPoints(contract, user);
}
main();