-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRainbow.html
More file actions
105 lines (105 loc) · 3.34 KB
/
Rainbow.html
File metadata and controls
105 lines (105 loc) · 3.34 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JSAPI_1.html</title>
<a href="index.html">Back to Contents</a>
<script type="text/javascript">
//Serpent source Code
var serpentSource = "def init():\n" +
" self.storage[msg.sender] = 10000\n" +
"def send(to, value):\n" +
" from = msg.sender\n" +
" if self.storage[from] >= value:\n" +
" self.storage[from] = self.storage[from] - value\n" +
" self.storage[to] = self.storage[to] + value\n";
var soliditySource = "contract Coin {\n"+
" mapping (address => uint) balances;\n" +
" function Coin() {\n" +
" balances[msg.sender] = 10000;\n" +
" }\n" +
" function send(address receiver, uint amount) {\n" +
" if (balances[msg.sender] < amount) return;\n" +
" balances[msg.sender] -= amount;\n" +
" balances[receiver] += amount;\n" +
" }\n" +
"}";
var contractDesc = [{
"name": "Send",
"inputs": [{
"name": "to",
"type": "uint256"
}, {
"name": "value",
"type": "uint256"
}]
}];
//var contract = web3.contract(contractAddress, contractDesc);
function sendEther () {
web3.eth.transact({to: document.querySelector('#recipient').value ,value: document.querySelector('#value').value}).then(function(){});
}
function createSerpentContract() {
web3.eth.transact({code: web3.eth.serpent(serpentSource)}).then(function(address){
contract = web3.contract(address, contractDesc);
contractAddress = address;
});
};
function createSolidityContract() {
web3.eth.transact({code: web3.eth.solidity(soliditySource)}).then(function(address){
contract = web3.contract(address, contractDesc);
contractAddress = address;
});
};
//function to send transaction using parameters from the HTML input boxes
function dataTransaction() {
receiverAddress = document.querySelector('#receiverAddress').value;
console.log(receiverAddress);
amount = document.querySelector('#amount').value;
console.log(amount);
contract.Send(receiverAddress, amount).transact()
};
web3.eth.watch({
altered: web3.eth.number
}).changed(function() {
web3.eth.storageAt(contractAddress).then(function(result) {
document.getElementById('balance').innerText = web3.toDecimal(result[web3.eth.coinbase._result]);
});
});
</script>
</head>
<body>
<div>
<div>
<h3>Transact Example</h3>
</div>
<div>
<input id="recipient" type="text" placeholder="To">
<input id="value" type="text" placeholder="Value in Wei">
</div>
<div>
<button onclick="sendEther();">Send Ether</button>
</div>
<div>
<h3>Create a contract</h3>
</div>
<div>
<button onclick="createSerpentContract();">Create Serpent Coin Contract</button>
<button onclick="createSolidityContract();">Create Solidity Coin Contract</button>
</div>
<div>
<h3>Send some coins</h3>
</div>
<div>
<h4>Coin Balance: <strong id="balance"></strong></h4>
</div>
<div>
<input id="receiverAddress" class="form-control" type="text" placeholder="Receiver address"></input><br>
<input id="amount" class="form-control" type="text" placeholder="Amount"></input><br>
</div>
<button onclick="dataTransaction();">Send transaction</button>
</div>
<div>
Typical transaction for a coin contract. Put in addresses prefixed with '0x' and hit 'Send transaction'
</div>
</body>
</html>