aboutsummaryrefslogtreecommitdiffstats
path: root/example/contract.html
blob: dccd1a64ff2e2ec3fbb2a31fabb6f6c9312b41d3 (plain) (blame)
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
<!doctype>
<html>

<head>
<script type="text/javascript" src="js/bignumber.js/bignumber.min.js"></script>
<script type="text/javascript" src="../dist/ethereum.js"></script>
<script type="text/javascript">

    var web3 = require('web3');
    web3.setProvider(new web3.providers.HttpSyncProvider());

    // solidity source code
    var source = "" + 
    "contract test {\n" +
    "   function multiply(uint a) returns(uint d) {\n" +
    "       return a * 7;\n" +
    "   }\n" +
    "}\n";

    // contract description, this will be autogenerated somehow
    var desc =  [{
        "name": "multiply(uint256)",
        "inputs": [
        {
            "name": "a",
            "type": "uint256"
        }
        ],
        "outputs": [
        {
            "name": "d",
            "type": "uint256"
        }
        ]
    }];

    var contract;

    function createExampleContract() {
        // hide create button
        document.getElementById('create').style.visibility = 'hidden'; 
        document.getElementById('source').innerText = source;

        // create contract
        var address = web3.eth.transact({code: web3.eth.solidity(source)});
        contract = web3.eth.contract(address, desc);
        document.getElementById('call').style.visibility = 'visible';
    }

    function callExampleContract() {
        // this should be generated by ethereum
        var param = parseInt(document.getElementById('value').value);

        // call the contract
        var res = contract.call().multiply(param);
        document.getElementById('result').innerText = res.toString(10);
    }

</script>
</head>
<body>
    <h1>contract</h1>
    <div id="source"></div> 
    <div id='create'>
        <button type="button" onClick="createExampleContract();">create example contract</button>
    </div>
    <div id='call' style='visibility: hidden;'>
        <input type="number" id="value" onkeyup='callExampleContract()'></input>
    </div>
    <div id="result"></div>
</body>
</html>