-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
94 lines (80 loc) · 2.91 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Simple Interest Calculator</title>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
</head>
<body>
<input type="text" id="principal" />
<br />
<input type="text" id="rate" />
<br />
<input type="text" id="time" />
<br />
<button id="calculate" onclick="calculateInterest()">Calculate</button>
<br />
<label id="output"></label>
<script>
//Connecting with testrpc
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
//Setting a default account
web3.eth.defaultAccount = web3.eth.accounts[0];
//Contract representation with ABI
var intrestContract = web3.eth.contract([
{
"constant": false,
"inputs": [
{
"name": "principal",
"type": "int256"
},
{
"name": "time",
"type": "int256"
},
{
"name": "rate",
"type": "int256"
}
],
"name": "calculateSimpleIntrest",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getSimpleIntrest",
"outputs": [
{
"name": "",
"type": "int256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]);
//Instance of a contract (Contract address is from remix)
var contractInstance = intrestContract.at('0x45149a5b922b0f384be16241dd55524b6566987e');
var principal = document.getElementById("principal");
var rate = document.getElementById("rate");
var time = document.getElementById("time");
var output = document.getElementById("output");
function calculateInterest() {
//Calculate simple intrest
contractInstance.calculateSimpleIntrest(parseInt(principal.value), parseInt(rate.value), parseInt(time.value));
//Get simple intrest
var outputValue = contractInstance.getSimpleIntrest();
//Set simple intrest to a label
output.innerHTML = outputValue;
}
</script>
</body>
</html>