forked from chain4travel/showcase_bonuscard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
215 lines (183 loc) · 6.21 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bonuscard</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="js/vendor/web3/dist/web3.min.js"></script>
<script src="js/vendor/qr/qrcode.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<style type="text/tailwindcss">
#qrcode[title] {
@apply p-4
}
#qrcode[title] + #message {
@apply p-4
}
</style>
<script>
let web3
let providerAddress
let subscription
let qrCode
const contractAddress = "0x9df887C314FA61D38E9a1fb0dEC8b60A7e57cb40";
// TransferSingle event signature for tracking incoming NFT's
const TSS = "0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62"
const ABI = [
{
"inputs": [
{
"internalType": "address",
"name": "_owner",
"type": "address"
},
{
"internalType": "uint256",
"name": "_id",
"type": "uint256"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
]
/*********** WEB3 ***********/
const subscribeProvider = (provider) => {
if (!provider.on) {
return;
}
provider.on('accountsChanged', async (accounts) => {
setAddress(web3.utils.toChecksumAddress(accounts[0]));
});
};
function setAddress(address) {
let shortAddress
if (address) {
address = web3.utils.toChecksumAddress(address);
shortAddress = address.substring(0, 6) + '...' + address.substring(38);
} else {
shortAddress = 'CONNECT WALLET';
}
providerAddress = address;
document.getElementById('connection').innerText = shortAddress;
document.getElementById("bonus").src = 'loading.gif';
document.getElementById("qrcode").innerHTML = "";
document.getElementById("message").innerHTML = "";
if (address) checkForTokenId()
}
async function launchWeb3() {
await unLaunchWeb3();
if (window.ethereum) {
web3 = new Web3(window.ethereum);
try {
// ask user permission to access his accounts
await window.ethereum.request({ method: "eth_requestAccounts" });
} catch (error) {
console.log(error);
return;
}
} else {
alert("MetaMask required");
return;
}
const accounts = await web3.eth.getAccounts();
setAddress(web3.utils.toChecksumAddress(accounts[0]));
subscribeProvider(window.ethereum);
}
async function unLaunchWeb3() {
if (web3) {
window.ethereum.removeAllListeners();
web3 = undefined;
setAddress();
unsubscribe();
}
}
async function toggleLaunch() {
if (web3)
await unLaunchWeb3();
else
await launchWeb3(true);
}
async function checkForTokenId() {
const contract = new web3.eth.Contract(
ABI,
contractAddress
);
const balance = parseInt(await contract.methods.balanceOf(providerAddress, 2).call());
if (balance > 0) {
unsubscribe();
document.getElementById("bonus").src = './eurowings-bonuscard.png';
return;
}
if (!subscription) {
console.log('Waiting for TransferSingle ...');
subscription = web3.eth.subscribe('logs', {
address: contractAddress,
topics: [TSS, null, null, '0x000000000000000000000000' + providerAddress.substring(2)]
}, function(error, result){
if (error)
console.log(error);
})
.on("data", function(log){
console.log('Event catched');
checkForTokenId();
})
}
}
function unsubscribe() {
if (subscription) {
subscription.unsubscribe(function(error, success){
if(success) {
console.log('Successfully unsubscribed!');
subscription = undefined;
}
});
}
}
async function signMessage() {
const message = 'Burn my voucher and give me a drink'
const sig = web3.utils.toHex(await web3.eth.personal.sign(message, providerAddress));
const result = {
address: providerAddress,
signature: sig
}
qrCode = new QRCode(document.getElementById("qrcode"), JSON.stringify(result).padEnd(220));
const pretty = JSON.stringify(result, (k,v) => {
return k=='address' || k=='signature' ? v.substring(0,6) + '...' + v.substring(v.length - 4) : v
}, 2);
document.getElementById("message").innerHTML = '<pre style="white-space:pre-wrap">' + pretty + '</pre>';
}
</script>
</head>
<body onload="launchWeb3()">
<div class="container p-4 mx-auto text-center flex flex-col h-screen items-center justify-center">
<h1 class="font-bold text-3xl mb-4">
Passenger Bonuscard
</h1>
<a id="connection" onclick="toggleLaunch()" class="js-btn font-mono bg-gray-100 text-gray-700 py-2 px-4 rounded">
Connect wallet
</a>
<section class="border border-gray-200 max-w-xl mx-auto block my-8 rounded-xl shadow-xl overflow-hidden">
<img id="bonus" class="w-full max-w-lg" src="loading.gif" alt="Bonus" />
</section>
<section id="sign">
<a onclick="signMessage()" class="js-btn flex items-center bg-[#af1e65] text-white py-3 px-4 rounded cursor-pointer">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 5v2m0 4v2m0 4v2M5 5a2 2 0 00-2 2v3a2 2 0 110 4v3a2 2 0 002 2h14a2 2 0 002-2v-3a2 2 0 110-4V7a2 2 0 00-2-2H5z"></path></svg>
<span class="pl-2">Redeem Voucher</span>
</a>
</section>
<div id="overlay" class="fixed bg-white rounded-xl shadow-xl w-96 left-1/2 -translate-x-1/2">
<div id="qrcode" class="flex justify-center"></div>
<p id="message" class="text-sm font-mono text-gray-700 rounded"></p>
</div>
</div>
</body>
</html>