-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.js
76 lines (64 loc) · 3.41 KB
/
client.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
var abi = "[{\"constant\":true,\"inputs\":[{\"name\":\"addr\",\"type\":\"address\"}],\"name\":\"getAgreement\",\"outputs\":[{\"name\":\"stringToAgreeOn\",\"type\":\"string\"},{\"name\":\"signed\",\"type\":\"bool\"},{\"name\":\"initialized\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_stringToAgreeOn\",\"type\":\"string\"},{\"name\":\"customer\",\"type\":\"address\"}],\"name\":\"createAgreement\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"signAgreement\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"}]";
var host = "http://localhost:8080";
(function() {
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545"));
var accounts = web3.eth.accounts;
var currentAccount = accounts[1];
var balance = web3.eth.getBalance(currentAccount);
var Signer = web3.eth.contract(JSON.parse(abi));
var instance = Signer.at("0x34c349e99ccc2f258cbc663ca508c805194cdf10")
var agreement = document.getElementById("agreement");
agreement.innerText = "";
var signed = document.getElementById("signed");
signed.innerText = "";
var refresh = document.getElementById("refresh");
refresh.addEventListener("click", function(event) {
instance.getAgreement(currentAccount, function(err, results) {
if (err || !results[2]) {
agreement.innerText = ""
signed.innerText = "";
} else {
agreement.innerText = results[0];
signed.innerText = results[1];
}
});
});
var refreshbalance = document.getElementById("refreshbalance");
refreshbalance.addEventListener("click", function(event) {
var newBalance = web3.eth.getBalance(currentAccount);
var balanceElement = document.getElementById("balance");
balanceElement.innerText = newBalance.toNumber();
});
var address = document.getElementById("address");
address.innerText = currentAccount;
var balanceElement = document.getElementById("balance");
balanceElement.innerText = balance.toNumber();
var signbutton = document.getElementById("signbutton");
signbutton.addEventListener("click", function(event) {
var tx = {
from: currentAccount,
value: 0,
gas: 100000
}
instance.signAgreement.sendTransaction(tx, function(err, res) {
if (err) {
return alert(err)
}
})
});
var agreementButton = document.getElementById("agreementButton");
agreementButton.addEventListener("click", function(event) {
var agreementInput = document.getElementById("agreementInput");
agreementValue = agreementInput.value
superagent.post(host + "/agreement").send({
account: currentAccount,
agreement: agreementValue,
}).end(function(err, res) {
if (err) {
return alert(res.text)
}
console.log(res);
});
});
})();