web3.0社交平臺DAO投票dapp系統開發智慧合約部署程式碼詳情

nice1022發表於2023-03-01

“Web3”指的是“基於區塊鏈的去中心化線上生態系統”。 2021 年,Web3 的想法開始流行。到 2021 年底,特別興趣激增,這主要是由於加密貨幣愛好者的興趣以及知名技術專家和公司的投資。


Web3 的核心顯著特徵是商業模式的去中心化。從這個意義上說,它標誌著網際網路的第三階段(因此稱為“Web3”)和使用者當前現狀的逆轉。


Web3 是下一次迭代,它開發I34-合約I633-部署53I9可能會顛覆這種權力結構,將其轉移回使用者。開放標準和協議可以迴歸。其目的是控制不再集中在大型平臺和聚合器中,而是透過“無需許可”的去中心化區塊鏈和智慧合約廣泛分佈。


這在實踐中意味著什麼?從本質上講,它可以透過將去中介化作為核心要素,標誌著數字應用商業模式的正規化轉變。在資料、功能和價值方面可能不再需要中介。使用者和創作者可以佔據上風,並且透過開源而不是專有應用程式,將有動力進行創新、測試、構建和擴充套件。


HTML的程式碼如下:

<html>
<head>
    <title>Hello World DApp</title>
    <link href='
    <link href='
</head>
<body class="container">
<h1>A Simple Voting Application</h1>
<div class="table-responsive">
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>Candidate</th>
            <th>Votes</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>Alice</td>
            <td id="candidate-1"></td>
        </tr>
        <tr>
            <td>Bob</td>
            <td id="candidate-2"></td>
        </tr>
        <tr>
            <td>Cary</td>
            <td id="candidate-3"></td>
        </tr>
        </tbody>
    </table>
</div>
<input type="text" id="candidate" />
<a href="#" onclick="voteForCandidate()" class="btn btn-primary">Vote</a>
</body>
<script src="
<script src="
<script src="./app.js"></script>
</html>


再新建一個js檔案命名為app.js,js程式碼如下:

abi = JSON.parse('[{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"totalVotesFor","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"validCandidate","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"votesReceived","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"candidateList","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"voteForCandidate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"candidateNames","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]')
VotingContract = web3.eth.contract(abi);
//部署的合約地址
contractInstance = VotingContract.at('0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
candidates = {"Alice": "candidate-1","Bob": "candidate-2","Cary":"candidate-3"}
function voteForCandidate() {
   console.log(candidate);
   candidateName = $("#candidate").val();
   console.log(candidateName);
   contractInstance.voteForCandidate(candidateName, {from: web3.eth.accounts[0]}, function() {
       let div_id = candidates[candidateName];
       console.log(contractInstance.totalVotesFor.call(candidateName).toString());
       $("#" + div_id).html(contractInstance.totalVotesFor.call(candidateName).toString());
   });
   console.log(contractInstance.totalVotesFor.call(candidateName).toString());
}
$(document).ready(function() {
   candidateNames = Object.keys(candidates);
   for (var i = 0; i < candidateNames.length; i++) {
       let name = candidateNames[i];
       let val = contractInstance.totalVotesFor.call(name).toString()
       $("#" + candidates[name]).html(val);
   }
});


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70011332/viewspace-2937533/,如需轉載,請註明出處,否則將追究法律責任。

相關文章