-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
352 lines (290 loc) · 18 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Split Donations front-end</title>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Teko" rel="stylesheet">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<!-- web3.js from: https://github.com/ethereum/web3.js/blob/1.0/dist/web3.min.js -->
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
<style>
input {
margin: 0 auto;
}
* {
font-family: 'Teko', cursive;
}
.ethSection {
padding: 100px 0;
}
.ethLogo {
width: 400px;
max-width: 80%;
}
</style>
</head>
<body>
<div class="container">
<!-- create new splitDonation contract section -->
<div class="jumbotron pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
<div id="notUsingMetamask" rol="alert"></div>
<h1 class="display-4">Split Donations between the organizer and the house.</h1>
<!-- lazy spacing -->
<br>
<form>
<div class="form-group">
<input type="text" class="form-control col-md-6 col-md-offset-3 text-center" id="organizerAddress" placeholder="Enter Organizer's public address" required>
</div>
<div class="form-group">
<input type="text" class="form-control col-md-6 col-md-offset-3 text-center" id="venueAddress" placeholder="Enter Venue's public address" required>
</div>
<button type="submit" class="btn btn-lg btn-primary" onClick="startApp().createSplitContract();">
<img height="31" src="ethereumLogo.svg">
<strong>Deploy new SplitDonations contract</strong>
</button>
</form>
</div>
<!-- donate section -->
<div id="donate" class="jumbotron">
<form class="text-center">
<h2>Donate</h2>
<div class="form-group">
<input type="text" class="form-control col-md-6 col-md-offset-3 text-center" id="contractAddress" placeholder="Enter contract's address" required>
</div>
<br>
<!-- Alert the user of the organizer and venue owner -->
<!-- <div class="alert alert-warning" role="alert"></div> -->
<!-- A simple warning alert—check it out! -->
<div id="fetchOrgAddr" rol="alert"></div>
<div id="fetchVenAddr" rol="alert"></div>
<div>
<div class="card-deck mb-3 text-center">
<div class="card mb-4 box-shadow">
<div class="card-header">
<h4 class="my-0 font-weight-normal">Gold</h4>
</div>
<div class="card-body">
<h1 class="card-title pricing-card-title">Ξ 0.1 <small class="text-muted">ether</small></h1>
<button type="button" class="btn btn-lg btn-block btn-outline-primary" onClick="startApp().submitDeposit(0.1);">
<img height="31" src="ethereumLogo.svg">
<strong>Donate</strong>
</button>
</div>
</div>
<div class="card mb-4 box-shadow">
<div class="card-header">
<h4 class="my-0 font-weight-normal">Bronze</h4>
</div>
<div class="card-body">
<h1 class="card-title pricing-card-title">Ξ 0.01 <small class="text-muted">ether</small></h1>
<button type="button" class="btn btn-lg btn-block btn-primary" onClick="startApp().submitDeposit(0.01);">
<img height="31" src="ethereumLogo.svg">
<strong>Donate</strong>
</button>
</div>
</div>
<div class="card mb-4 box-shadow">
<div class="card-header">
<h4 class="my-0 font-weight-normal">Custom</h4>
</div>
<div class="card-body">
<input type="number" class="form-control card-title pricing-card-title" id="donationAmount" placeholder="Enter ether amount" required>
<button type="button" class="btn btn-lg btn-block btn-dark" onClick="startApp().submitDeposit();">
<img height="31" src="ethereumLogo.svg">
<strong>Donate</strong>
</button>
</div>
</div>
</div>
<!-- lazy spacing -->
<br>
<!-- Withdraw section -->
<h2>Withdraw</h2>
<button type="submit" class="btn btn-lg btn-primary" onClick="startApp().withdraw();">
<img height="31" src="ethereumLogo.svg">
<strong>Withdraw donations</strong>
</button>
</form>
</div>
</form>
</div>
<!-- end of main container -->
<!-- Dapp Features -->
<section class="dapp_features">
<div class="container">
<div class="row text-center">
<div class="col-sm-4 col-md-4">
<i class="fas fa-lock fa-4x"></i>
<br>
<h1 class="text-uppercase">Utmost Security</h1>
<p class="lead" style="color:#888;">The only way someone could steal the eth in the smart contract is by stealing the organizer's or venue onwer's private key</p>
</div>
<div class="col-sm-4 col-md-4">
<i class="fas fa-pen-nib fa-4x"></i>
<h1 class="text-uppercase">Ease</h1>
<p class="lead" style="color:#888;">Get donations from anybody as a simple Eth transfer transaction</p>
</div>
<div class="col-sm-4 col-md-4">
<i class="fas fa-piggy-bank fa-4x"></i>
<h1 class="text-uppercase">It's own bank</h1>
<p class="lead" style="color:#888;">Each contract retains received Eth as a vault.</p>
</div>
</div>
</div>
</section>
<!-- powered by Eth -->
<section class="ethSection container text-center"><a href="https://ethereum.org" target="_blank" rel="noopener noreferrer"><img src="powered-by-ethereum.png" class="ethLogo" alt=""></a></section>
<!-- contact -->
<div class="mastfoot text-center">
<div class="inner">
<p>Built with love by <a target="_blank" href="https://twitter.com/mohamedhayibor">Mo</a></p>
</div>
</div>
<!-- Dimissible transaction confirmation footer alert -->
<footer id="txStatus" class="fixed-bottom container">
</footer>
<script>
window.addEventListener('load', function() {
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
// Use Mist/MetaMask's provider
web3js = new Web3(web3.currentProvider);
} else {
$("#notUsingMetamask").addClass("alert alert-warning")
.text("Install Metamask in order to use this site :(");
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
web3js = new Web3(new Web3.providers.HttpProvider("https://infura.io/"));
}
// Now you can start your app & access web3 freely:
startApp();
});
function startApp() {
// "0xE50a24D1Bfb94D11df0DF6d226d382ddEb451dEf";
var splitContract = web3js.eth.contract([{"constant":false,"inputs":[],"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"venueAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"currentContractBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"organizerAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"deposit","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"inputs":[{"name":"_organizerAddress","type":"address"},{"name":"_venueAddress","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
// Extra layer of security for user to double check organizer and venue address
$("#contractAddress").on('keyup', function() {
var contractAdr = $("#contractAddress").val().trim();
if (contractAdr < 40) {
console.log(">> Invalid address, Stop everything.");
return; // return everything
}
if (contractAdr.length == 40) contractAdr = "0x" + contractAdr;
var contractInstance = splitContract.at(contractAdr);
var orgAdr = contractInstance.organizerAddress.call(function (err, res) {
if (err) {
return error(err);
} else {
console.log("successfully fetched organizerAddress");
}
$("#fetchOrgAddr").addClass("alert alert-warning")
.text("Organizer address: " + res);
});
var venueAdr = contractInstance.venueAddress.call(function (err, res) {
if (err) {
return error(err);
} else {
console.log("successfully fetched venueAddress");
}
$("#fetchVenAddr").addClass("alert alert-warning")
.text("Venue address: " + res);
});
});
// address from metamask > startApp() runs again at time of execution
// so it will pick up the right current userAddress
var userAddress = web3js.eth.accounts[0];
function submitDeposit(donationAmount) {
var contractAddress = $("#contractAddress").val().trim();
if (contractAddress < 40) {
alert("Invalid contract Address");
return; // return everything
}
if (contractAddress.length == 40) contractAddress = "0x" + contractAddress;
var splitDepositInstance = splitContract.at(contractAddress);
var userDonationChoice = donationAmount || $("#donationAmount").val();
var donationInEther = (10 ** 18) * userDonationChoice;
splitDepositInstance.deposit({from: userAddress, value: donationInEther}, function (err, txHash) {
if (err) {
return error(err);
}
document.querySelector("#txStatus").innerHTML = '<div class="alert alert-success alert-dismissible fade show" role="alert"><strong>Your deposit transaction has been broadcasted!</strong>\nTransaction hash: <a target="_blank" href="https://etherscan.io/tx/' + txHash +'">' + txHash + '</a> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
waitConfirmation(txHash, function() {
document.querySelector("#txStatus").innerHTML = '<div class="alert alert-success alert-dismissible fade show" role="alert"><strong>Successfully donated ' + donationAmount + ' Ether!</strong><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
});
})
}
// see pattern at https://programtheblockchain.com/posts/2017/12/13/building-decentralized-apps-with-ethereum-and-javascript/
function waitConfirmation(hash, cb) {
web3js.eth.getTransactionReceipt(hash, function(err, receipt) {
if (err) {
console.log(err);
}
if (receipt !== null) {
// transaction went through
if (cb) {
cb(receipt);
}
} else {
// try again in 1s
window.setTimeout(function () {
waitConfirmation(hash, cb);
}, 1000);
}
})
}
function withdraw() {
var contractAddress = $("#contractAddress").val().trim();
if (contractAddress.length < 40) {
alert("Invalid contract address!");
return; // stop everything
}
if (contractAddress.length == 40) contractAddress = "0x" + contractAddress;
var splitWithdrawInstance = splitContract.at(contractAddress);
splitWithdrawInstance.withdraw({from: userAddress}, function (err, txHash) {
if (err) {
return error(err);
}
document.querySelector("#txStatus").innerHTML = '<div class="alert alert-success alert-dismissible fade show" role="alert"><strong>Your withdraw transaction has been broadcasted!</strong>\nTransaction hash: <a target="_blank" href="https://etherscan.io/tx/' + txHash +'">' + txHash + '</a> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
waitConfirmation(txHash, function() {
console.log("Withdraw Transaction was successfully mined.");
document.querySelector("#txStatus").innerHTML = '<div class="alert alert-success alert-dismissible fade show" role="alert"><strong>Successfully withdrew funds!</strong><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
});
});
}
function createSplitContract() {
var organizerAddress = $("#organizerAddress").val().trim();
var venueAddress = $("#venueAddress").val().trim();
if (organizerAddress.length < 40 || venueAddress.length < 40 ) {
alert("Invalid ether addresses!");
return; // stop everything
}
// add 0x if not included
if (organizerAddress.length == 40) organizerAddress = "0x" + organizerAddress;
if (venueAddress.length == 40) venueAddress = "0x" + venueAddress;
var newContract = splitContract.new(organizerAddress, venueAddress, {
from: userAddress,
data: "0x608060405234801561001057600080fd5b5060405160408061061b83398101806040528101908080519060200190929190805190602001909291905050508073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614151515610107576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602d8152602001807f646f6e6174696f6e20616363742063616e2774206265207468652073616d652081526020017f617320686f75736520616363740000000000000000000000000000000000000081525060400191505060405180910390fd5b816000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505050610482806101996000396000f300608060405260043610610078576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633ccfd60b1461007d57806341c0e1b51461009457806369ba4f68146100ab578063888f26b814610102578063c373ec8d1461012d578063d0e30db014610184575b600080fd5b34801561008957600080fd5b5061009261018e565b005b3480156100a057600080fd5b506100a96103d1565b005b3480156100b757600080fd5b506100c06103ea565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561010e57600080fd5b50610117610410565b6040518082815260200191505060405180910390f35b34801561013957600080fd5b5061014261042f565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b61018c610454565b005b6000803373ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16148061023957503373ffffffffffffffffffffffffffffffffffffffff16600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16145b15156102d3576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260218152602001807f596f75277265206e6f7420746865206f776e6572206f72206465706c6f79657281526020017f2e0000000000000000000000000000000000000000000000000000000000000081525060400191505060405180910390fd5b3073ffffffffffffffffffffffffffffffffffffffff163191506002828115156102f957fe5b0490506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050158015610363573d6000803e3d6000fd5b50600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f193505050501580156103cc573d6000803e3d6000fd5b505050565b3073ffffffffffffffffffffffffffffffffffffffff16ff5b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60003073ffffffffffffffffffffffffffffffffffffffff1631905090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b5600a165627a7a72305820ad1b5d0aabaeaba873d1c6b566800d94ae69fd77424adc22190933ef8831dfe00029"
}, function(err, txHash) {
if (err) {
return error(err);
}
document.querySelector("#txStatus").innerHTML = '<div class="alert alert-success alert-dismissible fade show" role="alert"><strong>Your transaction has been broadcasted!</strong>\nTransaction hash: <a target="_blank" href="https://etherscan.io/tx/' + txHash +'">' + txHash + '</a> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
waitConfirmation(txHash, function() {
document.querySelector("#txStatus").innerHTML = '<div class="alert alert-success alert-dismissible fade show" role="alert"><strong>Successfully deployed new splitDonations contract!</strong><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
});
});
}
// shitty getters
return {
splitContract: splitContract,
userAddress: userAddress,
submitDeposit: submitDeposit,
createSplitContract: createSplitContract,
withdraw: withdraw
}
}
</script>
</body>
</html>