-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
113 lines (100 loc) · 2.96 KB
/
index.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
// Initialize Firebase
var config = {
apiKey: "AIzaSyCXak386757EoosAb2vII2svD9hnUyasUs",
authDomain: "charkity-2db46.firebaseapp.com",
databaseURL: "https://charkity-2db46.firebaseio.com",
projectId: "charkity-2db46",
storageBucket: "charkity-2db46.appspot.com",
messagingSenderId: "88663340604"
};
firebase.initializeApp(config);
var database = firebase.database();
var myAddress = "";
var mySeed = "";
var myPublicKey = "";
function setBalance() {
arkAPI.getBalance(
myAddress,
function(error, success, response) {
document.getElementById("myBalance").innerHTML = response.balance/100000000;
}
);
}
function signOut(){
firebase.auth().signOut().then(function() {
}, function(error) {
alert('Sign Out Error', error);
});
}
function sendTransaction() {
if (document.getElementById("charity").value == "choose") {
alert("You need to choose a charity!");
return;
}
var seed = mySeed;
var amount = document.getElementById("amount").value;
var address = myAddress;
var precision = Math.pow(10, 1);
var final_amount = Math.ceil(amount * precision) / precision;
var difference = (final_amount - amount).toFixed(8);
var charity = difference * Math.pow(10, 8);
amount = parseInt(amount * 100000000);
// send to actual recipient
arkAPI.sendTransaction(
seed,
"ignore",
myPublicKey,
amount,
document.getElementById("recieve").value,
function(error, success, response) {
console.log(response);
});
// send remainder to charity
arkAPI.sendTransaction(
seed,
"ignore",
myPublicKey,
charity,
document.getElementById("charity").value,
function(error, success, response) {
if (!error) {
// reset values
document.getElementById("charity").selectedIndex = 0;
document.getElementById("recieve").value = '';
document.getElementById("amount").value = '';
} else {
console.log(error);
}
});
}
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
var displayName = user.displayName;
var email = user.email;
var uid = user.uid;
var userId = firebase.auth().currentUser.uid;
var leadsRef = database.ref('/users/' + userId);
leadsRef.on('value', function(snapshot) {
myAddress = snapshot.child("address").val();
mySeed = snapshot.child("seed").val();
myPublicKey = snapshot.child("publicKey").val();
document.getElementById("myAddress").innerHTML = myAddress;
new QRCode(document.getElementById("qrcode"), myAddress);
setBalance();
});
} else {
// User is signed out.
window.location = "login.html";
}
});
function loginUser(){
firebase.auth().signInWithEmailAndPassword(document.getElementById('loginEmail').value, document.getElementById('loginPassword')).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
});
}
setInterval(() => {
setBalance();
}, 3000);