- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个基于以太坊的投票应用程序 app dapp。
它使用松露并通过元掩码使用虚拟帐户。
投票的时候想把记录放到数据库里不知道怎么办。
我想使用mysql作为数据库。
操作系统:Ubuntu 18.07
松露:5.0
虚拟机:0.33.1
nodejs:12.14.1
npm : 6.13.4
契约(Contract)/选举.sol:
*pragma solidity ^0.5.0;
合约选举{
// 후보자 모델
struct Candidate {
uint id;
string name;
uint votefirst; //1순위투표수
uint voteSeond; //2순위투표수
uint votethird; //3순위투표수
uint voteCount; //전체 유권자들이 투표한 총 투표수
}
// 후보자 기호 변수
uint public candidatesCount; //후보자수
uint public voteCount; //총 투표수
string public votename;
uint public voterCount; //투표자수
uint public test = 0;
// 후보자 반환하기
mapping(uint => Candidate) public candidates;
// 투표에 참여한 ID 기록
mapping(address => bool) public voters;
// 후보자 등록하기
function addCandidate (string memory _name) public {
candidatesCount++;
candidates[candidatesCount] = Candidate(candidatesCount, _name, 0 , 0, 0, 0);
}
// constructor
constructor() public {
addCandidate('A');
addCandidate('B');
addCandidate('C');
//voterCount++;
}
// 투표하기
function vote(uint _candidateId) public {
// 중복투표를 하면 오류를 발생 시킨다.
//require(!voters[msg.sender]);
// 목록에 없는 후보자에게 투표하면 오류를 발생시킨다.
require(_candidateId > 0 && _candidateId <= candidatesCount);
// 투표에 참여한 ID를 기록해서 두번 투표하지 못하도록 한다.
voters[msg.sender] = true;
// 득표수를 +1 한다.
candidates[_candidateId].votefirst = candidates[_candidateId].votefirst+1;
candidates[_candidateId].votefirst = candidates[_candidateId].votefirst+test;
candidates[_candidateId].voteCount = candidates[_candidateId].voteCount+3;
}
function vote1(uint _candidateId) public {
// 중복투표를 하면 오류를 발생 시킨다.
//require(!voters[msg.sender]);
// 목록에 없는 후보자에게 투표하면 오류를 발생시킨다.
require(_candidateId > 0 && _candidateId <= candidatesCount);
// 투표에 참여한 ID를 기록해서 두번 투표하지 못하도록 한다.
voters[msg.sender] = true;
// 득표수를 +1 한다.
candidates[_candidateId].voteSeond = candidates[_candidateId].voteSeond +1 ;
candidates[_candidateId].voteCount = candidates[_candidateId].voteCount+3;
//votelist[voterCount].vote_s=candidates[_candidateId].voteSeond;
}
function vote2(uint _candidateId) public {
// 중복투표를 하면 오류를 발생 시킨다.
//require(!voters[msg.sender]);
// 목록에 없는 후보자에게 투표하면 오류를 발생시킨다.
require(_candidateId > 0 && _candidateId <= candidatesCount);
// 투표에 참여한 ID를 기록해서 두번 투표하지 못하도록 한다.
voters[msg.sender] = true;
// 득표수를 +1 한다.
candidates[_candidateId].votethird = candidates[_candidateId].votethird +1 ;
candidates[_candidateId].voteCount = candidates[_candidateId].voteCount+3;
//votelist[voterCount].vote_t=candidates[_candidateId].votethird;
}
}*
var App = {
web3Provider: null,
contracts: {}
}
$(window).load(function () {
// web3Provider 생성하기
if (typeof web3 !== 'undefined') {
// MetaMask가 설치되어 있어서 web3 인스턴스가 이미 생성되어 있음
App.web3Provider = web3.currentProvider;
web3 = new Web3(web3.currentProvider);
} else {
// MetaMask가 설치되지 않았을 경우 기본 인스턴스를 지정함
App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
web3 = new Web3(App.web3Provider);
}
// Election.json을 가지고 온다.
$.getJSON('Election.json', function (election) {
// Truffle 계약을 초기화 함
App.contracts.Election = TruffleContract(election);
// 인스턴스에 접속함
App.contracts.Election.setProvider(App.web3Provider);
render();
});
var voter =0;
var vote_winner;
var account_mine;
var vote_num;
// 화면구현
function render() {
// 계정 정보 읽어오기
web3.eth.getCoinbase(function (err, account) {
if (err === null) {
App.account = account;
$('#accountAddress').html('나의 계정: ' + account);
}
});
// 계약 정보를 읽어온다.
App.contracts.Election.deployed().then(function (instance) {
electionInstance = instance;
return electionInstance.candidatesCount();
}).then(function (candidatesCount) {
for (var i = 1; i <= candidatesCount; i++) {
electionInstance.candidates(i).then(function (candidate) {
var id = candidate[0];
var name = candidate[1];
var voteCount1 = candidate[2];
var voteCount2 = candidate[3];
var voteCount3 = candidate[4];
var voteCountall = candidate[5];
var name1 = candidate[6] + account_mine;
var voteAccount = account_mine;
var voteSum= (parseInt(voteCount1) + (parseInt(voteCount2)*2) + (parseInt(voteCount3)*3));
voter = voteCountall;
// 투표결과 html 파싱
var candidateTemplate = '<tr><th>' + id + '</th><td>' + name + '</td><td>' + voteCount1 + '</td><td>' + voteCount2 + '</td><td>' + voteCount3 + '</td></tr>'
$('#candidatesResults').append(candidateTemplate);
if( (voter / 3) / 2 < voteCount1)
{
vote_winner = name;
var candidateTemplate1 = '<tr><th>' + id + '</th><td>' + name + '</td></tr>'
$('#candidatesResults1').append(candidateTemplate1);
}
var candidateOption_first = '<option value="' + id + '">' + name + '</option>'
$('#candidateSelect1').append(candidateOption_first);
var candidateOption_second = '<option value="' + id + '">' + name + '</option>'
$('#candidateSelect2').append(candidateOption_second);
var candidateOption_third = '<option value="' + id + '">' + name + '</option>'
$('#candidateSelect3').append(candidateOption_third);
});
}
// 후보자 화면 표시
$('#loader').hide();
$('#content').show();
}).catch(function (error) {
console.warn(error);
});
}
$('#btnVote').on('click', function() {
var candidateId_first = $('#candidateSelect1').val()
var candidateId_second = $('#candidateSelect2').val()
var candidateId_third = $('#candidateSelect3').val()
vote_1 = candidateId_first;
vote_2 = candidateId_second;
vote_3 = candidateId_third;
if ((!candidateId_first) || ( !candidateId_second ) || ( !candidateId_third) ) {
return alert('모든 후보자의 순위를 선택해주세요.')
}
if ((candidateId_first == candidateId_second) || (candidateId_second == candidateId_third) || (candidateId_third == candidateId_first)){
return alert('순위별 후보자가 중복되었습니다.')
}
App.contracts.Election.deployed()
.then(function(instance) {
return instance.vote(candidateId_first, {from: App.account}),
instance.vote1(candidateId_second, {from: App.account}),
instance.vote2(candidateId_third, {from: App.account})
})
.then(function(result) {
if (result.receipt) {
vote_num++;
var j= parseInt(voter);
voter =j+3;
alert('성공적으로 투표했습니다.')
location.reload();
}
})
.catch(function(error) {
alert(error.message)
alert('투표하실 수 없습니다.')
})
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>투표 결과</title>
</head>
<body>
<h1>투표 결과</h1>
<!-- 로딩바 -->
<div id="loader">
<p>로딩중...</p>
</div>
<div id="content" style="display: none;">
<!-- 투표결과 표시 -->
<table class="table" style="width: 50%">
<colgroup>
<col width="200">
<col width="*">
<col width="100">
</colgroup>
<thead>
<tr>
<th scope="col">기호</th>
<th scope="col">이름</th>
<th scope="col">1순위 득표수</th>
<th scope="col">2순위 득표수</th>
<th scope="col">3순위 득표수</th>
</tr>
</thead>
<tbody id="candidatesResults">
</tbody>
</table>
<table>
<thead>
<tr>
<!-- <th >득표수</th> -->
</tr>
</thead>
<tbody id="voteCount">
</tbody>
</table>
<h2>투표 참여</h2>
<table class="table" style="width: 100%">
<colgroup>
<col width="60">
<col width="*">
<col width="100">
</colgroup>
<thead>
<tr>
<th scope="col">순위</th>
<th scope="col">이름</th>
</tr>
</thead>
<thead>
<tr>
<th scope="col">1</th>
<th scope="col">
<select id="candidateSelect1">
<option value="">후보자를 선택하세요.</option>
</select>
</th>
</tr>
</thead>
<thead>
<tr>
<th scope="col">2</th>
<th scope="col">
<select id="candidateSelect2">
<option value="">후보자를 선택하세요.</option>
</select>
</th>
</tr>
</thead>
<thead>
<tr>
<th scope="col">3</th>
<th scope="col">
<select id="candidateSelect3">
<option value="">후보자를 선택하세요.</option>
</select>
</tr>
</thead>
</table>
<button id="btnVote">투표하기</button>
<table>
<thead>
<colgroup>
<col width="200">
<col width="*">
<col width="100">
</colgroup>
<tr>
<th scope="col">투표자 기록</th>
<th scope="col"> 이름</th>
</tr>
</thead>
<tbody id="candidatesResults1">
<p id="accountAddress"></p>
</tbody>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="js/web3.min.js"></script>
<script src="js/truffle-contract.js"></script>
<script src="js/app.js"></script>
最佳答案
根据定义,dApp 不连接数据库,但它们连接到区块链。所以你的问题没有意义。
将数据从区块链获取到数据库的最佳方法是让服务器端进程连接到以太坊 Node ,监听 Solidity 事件,然后将这些事件复制并写入您的内部 SQL 数据库。
关于mysql - 我如何将 dapp 连接到数据库(mysql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65069383/
我想了解使用 Aurora (EVM) 或使用 Rust 部署 Dapp 在用户体验方面的区别?用户钱包交易的工作方式是否相同?或者用户与 Aurora 应用程序的交互方式必须不同于使用 Rust 构
除了 mist 浏览器之外,它们是访问以太坊 Dapps 的一种方式。我在考虑像 chrome 这样的普通浏览器。另外,作为一个子问题,一些 Android 和 IOS 应用程序如何连接到区 bloc
我正在创建一个以太坊 DApp。 DApp 由拥有电子邮件、姓名和个人资料图片等相关数据的用户组成。我想将用户的内容作为 JSON 对象存储在 IPFS 中,并使用 IPFS 哈希在链上引用它。我如何
在以太坊网络中与 DApps 通信是否需要 metamask?如果不是,那么与智能合约交互的另一种方式是什么? 最佳答案 开始使用 Ethereum 不是必需的,但非常少使用 MetaMask 的网络
在以太坊网络中与 DApps 通信是否需要 metamask?如果不是,那么与智能合约交互的另一种方式是什么? 最佳答案 开始使用 Ethereum 不是必需的,但非常少使用 MetaMask 的网络
使用 Elrond Network 创建 dApp 时,我需要在不实际发送交易的情况下对用户进行身份验证。 对于像以太坊这样的其他区 block 链,这是使用 MetaMask 实现的,它可以签署一条
我正在制作一个基于以太坊的投票应用程序 app dapp。 它使用松露并通过元掩码使用虚拟帐户。 投票的时候想把记录放到数据库里不知道怎么办。 我想使用mysql作为数据库。 操作系统:Ubuntu
有各种教程教如何激活和停用 Metamask 钱包,或者一般的任何加密钱包,但在所有这些教程中我都遇到了同样的问题,如下所示: 当我停用我的帐户时,它仍然显示在 Metamask GUI 中您已连接,
我正在学习区 block 链技术。我阅读了许多教程,并且对区 block 链是什么有了基本概念,但我对以下事实感到困惑以太坊区 block 链中的区 block 真的包含吗? 如果有人可以清除我的新手
我刚开始使用 IPFS 和以太坊。我们正在构建一个 dApp(移动)并计划用 IPFS 和以太坊替换标准的 API 和数据库层。所以,经过大量阅读后,我有以下问题, 使 dApp 直接与 IPFS(通
我使用 MERN 堆栈应用程序通过前端将我的 java 合约部署到 Mastery 网络,并在编译时遇到以下错误: Module not found: Error: Can't resolve 'fs
我一直在尝试整合WalletConnect通过关注 documentation的 web3-react . 我用于连接器的配置如下: import { WalletConnectConnector }
当我使用 Maven 和嵌入式 AVM 调用合约时,我收到以下消息: [ERROR] Failed to execute goal org.aion4j:aion4j-maven-plugin:0.5
我的 Ganche-GUI 和元掩码正在运行。但我收到以下错误在控制台中 类型错误:无法读取未定义的属性“getId” 在 App.componentDidMount (App.js:17) 以下是我
我在Third Web上部署了一份智能合同,但我没有加入其中。在资源管理器页面上,智能合同的功能在第三个网站的仪表板上看不到。我是否仍然可以使用相同的部署智能合同来构建我的DApp?或者我应该从头开始
我是一名优秀的程序员,十分优秀!