软件 版本号
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"));
    // solidity code code
    var source = "" +
    "pragma solidity ^0.4.6;" +
    "contract test {\n" +
    "   function multiply(uint a) constant returns(uint d) {\n" +
    "       return a * 7;\n" +
    "   }\n" +
    "}\n";
    var compiled = web3.eth.compile.solidity(source);
    var code = compiled.code;
    // contract json abi, this is autogenerated using solc CLI
    var abi = compiled.info.abiDefinition;
    var myContract;
    function createExampleContract() {
        // hide create button
        document.getElementById('create').style.visibility = 'hidden'; 
        document.getElementById('code').innerText = code;
        // let's assume that coinbase is our account
        web3.eth.defaultAccount = web3.eth.coinbase;
        // create contract
        document.getElementById('status').innerText = "transaction sent, waiting for confirmation";
        web3.eth.contract(abi).new({data: code}, function (err, contract) {
            if(err) {
                console.error(err);
                return;
            // callback fires twice, we only want the second call when the contract is deployed
            } else if(contract.address){
                myContract = contract;
                console.log('address: ' + myContract.address);
                document.getElementById('status').innerText = 'Mined!';
                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 = myContract.multiply(param);
        document.getElementById('result').innerText = res.toString(10);
    }
</script>
</head>
<body>
    <h1>contract</h1>
    <div id="code"></div> 
    <div id="status"></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>

  • 以太坊

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

    26 引用 • 1 回帖 • 1 关注
  • 区块链

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

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

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

    20 引用 • 9 回帖
  • Web3
    7 引用 • 4 回帖
感谢    赞同    分享    收藏    关注    反对    举报    ...