软件 版本号
Web3.js V1.0
<!DOCTYPE html>
<html>
    <head>
    <script type="text/javascript" src="../dist/web3.js"></script>
    <script type="text/javascript">
        var Web3 = require('web3');
        var web3 = new Web3();
        web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
        var source = "" +
        "pragma solidity ^0.4.6;" +
        "contract Contract { " +
        "   event Incremented(bool indexed odd, uint x); " +
        "   function Contract() { " +
        "        x = 70; " +
        "    } " +
        "    function inc() { " +
        "        ++x; " +
        "        Incremented(x % 2 == 1, x); " +
        "    } " +
        "    uint x; " +
        "}";
        var compiled = web3.eth.compile.solidity(source);
        var code = compiled.code;
        var abi = compiled.info.abiDefinition;
        var address;
        var contract;
        var inc;
        var update = function (err, x) {
            document.getElementById('result').textContent = JSON.stringify(x, null, 2);
        };
    
        var createContract = function () {
            // let's assume that we have a private key to coinbase ;)
            web3.eth.defaultAccount = web3.eth.coinbase;
            
            document.getElementById('create').style.visibility = 'hidden';
            document.getElementById('status').innerText = "transaction sent, waiting for confirmation";
            web3.eth.contract(abi).new({data: code}, function (err, c) {
                if (err) {
                    console.error(err);
                    return;
                // callback fires twice, we only want the second call when the contract is deployed
                } else if(c.address){
                    contract = c;
                    console.log('address: ' + contract.address);
                    document.getElementById('status').innerText = 'Mined!';
                    document.getElementById('call').style.visibility = 'visible';
                    inc = contract.Incremented({odd: true}, update);
                }
            });
        };
        var counter = 0;
        var callContract = function () {
            counter++;
            var all = 70 + counter;
            document.getElementById('count').innerText = 'Transaction sent ' + counter + ' times. ' + 
                'Expected x value is: ' + (all - (all % 2 ? 0 : 1)) + ' ' +
                'Waiting for the blocks to be mined...';
                 
            contract.inc();
        };
    </script>
    </head>

    <body>
        <div id="status"></div>
        <div>
            <button id="create" type="button" onClick="createContract();">create contract</button>
        </div>
        <div>
            <button id="call" style="visibility: hidden;" type="button" onClick="callContract();">test1</button>
        </div>
        <div id='count'></div>
        <div id="result">
        </div>
    </body>
</html>

  • 区块链

    区块链就是一条信息多方确认并存储于多个地方。 学习区块链,拥抱未来吧!

    63 引用 • 8 回帖 • 1 关注
  • 智能合约

    智能合约(英语:Smart contract )是一种旨在以信息化方式传播、验证或执行合同的计算机协议。

    20 引用 • 9 回帖
  • Web3
    7 引用 • 4 回帖
  • 以太坊

    以太坊的概念首次在 2013 至 2014 年间由程序员 Vitalik Buterin 受比特币启发后提出,大意为“下一代去中心化应用平台”

    26 引用 • 1 回帖 • 1 关注
感谢    赞同    分享    收藏    关注    反对    举报    ...