【区块链】以太坊truffle+web3+ganache简单实践
以太坊truffle+web3+ganache简单实践
- 安装
- 帮助文档
- 结构
- 代码
- solidity代码
- html
- 注意
安装
- 下载最新版nodejs
npm install -g truffle
- 下载
ganache
帮助文档
https://www.trufflesuite.com/docs/truffle/getting-started/installation
https://web3js.readthedocs.io/en/v1.2.9/web3-eth-abi.html
https://github.com/ethereum/web3.js
结构
代码
solidity代码
pragma solidity ^0.5.0;contract Auction {// 受益者address payable public benefitAddr;// 拍卖时间uint public startTime;uint public endTime;uint public remainTime;// 是否结束bool public hasBid = false;// 现在的最高价string public bidderId = "";uint public highestPrice;address public highestBidder;// 当前商品string public currentBid = "";// 存储历史出价记录mapping(address => uint) pendingResults;function startAuction(string memory bidName) public returns(bool) {// 检查现在是否正在拍卖require(hasBid != true);uint _bidTime = 300000;benefitAddr = msg.sender;// 设定时间startTime = now;startTime = now + _bidTime;remainTime = _bidTime;// 设定拍卖品currentBid = bidName;// 设定当前拍卖价格highestPrice = 0;highestBidder = address(0);hasBid = true;return true;}function bid(string memory _bidderId) public payable{// 如果出价最高就执行拍卖require(msg.value > highestPrice);highestBidder = msg.sender;highestPrice = msg.value;bidderId = _bidderId;if(highestPrice != 0){pendingResults[highestBidder] += highestPrice;}}function endAuction() public {// 合约是没有定时的// 验证拍卖时间,谁可以结束拍卖hasBid = false;currentBid = '';benefitAddr.transfer(highestPrice);}function getInfo() public view returns (string memory, uint){return (bidderId, highestPrice);}// 没拍到的退款function withdraw() public returns (bool){uint amount = pendingResults[msg.sender];require(amount > 0);if(amount > 0){pendingResults[msg.sender] = 0;if (!msg.sender.send(amount)){pendingResults[msg.sender] = amount;return false;}}return true;}
}
html
<html>
<head><title>第一个智能合约</title><meta charset="UTF-8"><script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
</head>
<body>
用户id: <input type="text" id="userId" value="2"/> <br/>
账户: <input type="text" id="account" value="0xCFE84e7D2262fedb2C5Dd30D2d9746a211CD5Abd"/> <br/>
出价:<input type="text" id="ethe" value="3"/> <br/>
<button id="setInfo">设定</button><br/>
<button id="getInfo">获取</button><br/>
当前值: <div id="curInfo"></div>
</body><script>$(function() {var currentProvider = new Web3.providers.HttpProvider('http://localhost:7545');var web3 = new Web3(currentProvider);// var fs = require('fs');// var squadJSON = JSON.parse(fs.readFileSync('./build/contracts/Auction.json', 'utf8'));// var abi = squadJSON.abi;// console.log(abi);var address = '0xCFE84e7D2262fedb2C5Dd30D2d9746a211CD5Abd';// console.log(Auction);var simpleAuction = new web3.eth.Contract(<你得编译后的合约>, '0x44B5E9f98d61eC4B7918fDAC8C9bC57e360d57EA');// 注意这个合约的地址不是什么账户地址,migrate后的地址simpleAuction.setProvider(currentProvider);simpleAuction.methods.startAuction("华为手机").send({from:address,gas:200000});$("#setInfo").click(function() {var userId = String($("#userId").val());var acc1 = String($("#account").val());var ethe = String($("#ethe").val());console.log(ethe);// simpleAuction.methods.bid(2).call({from:acc1});simpleAuction.methods.bid(userId).send({from:acc1, value:web3.utils.toWei(ethe,"ether"), gas:200000});// simpleAuction.methods.bid(userId).send({from:acc1, value:ethe, gas:200000});// simpleAuction.methods.bid().send({from:'0x90ba5323772524c7E423184545b9dE72B9Bd65D3', value:web3.utils.toWei(ethe,"ether")});// simpleAuction.methods.getInfo().call({gas:1000000}).then(function(result){// console.log(result);// $("#curInfo").text("highestPrice =>" + result);// });// simpleAuction.methods.getInfo().send({from:acc1, gas:1000000}).then(console.log());});$("#getInfo").click(function() {simpleAuction.methods.getInfo().call({gas:200000}).then(function(result){console.log(result);$("#curInfo").text("highestPrice =>" + web3.utils.fromWei(result[1],"ether"));})});});</script>
</html>
注意
【区块链】以太坊truffle+web3+ganache简单实践相关推荐
- 【区块链-以太坊】5 Ubuntu下truffle ganache安装及使用
[区块链-以太坊]5 Ubuntu下truffle & ganache安装及使用 1 truffle安装 1)输入sudo npm install -g truffle 2)将truffle复 ...
- 8支团队正在努力构建下一代区块链以太坊Ethereum 2.0
2019独角兽企业重金招聘Python工程师标准>>> "我们不想在构建 Ethereum 2.0时重新造轮子." 谈到开发人员为 Ethereum 区块链进行两 ...
- 可编程区块链以太坊的未来
区块链的火热就不用说了,回看一下Vinay Gupta 2015年发表的文章<内容可编程的区块链:以太坊的未来>,感受一下先行者的思想. 到本文结束时,你将了解一般的区块链(特别是下一代区 ...
- 回看2015年是如何描述区块链以太坊的未来
区块链的火热就不用说了,回看一下Vinay Gupta 2015年发表的文章<内容可编程的区块链:以太坊的未来>,感受一下先行者的思想. 到本文结束时,你将了解一般的区块链(特别是下一代区 ...
- 回望Vinay2015年的文章:内容可编程的区块链——以太坊的未来
2019独角兽企业重金招聘Python工程师标准>>> 区块链的火热就不用说了,回看一下Vinay Gupta 2015年发表的文章<内容可编程的区块链:以太坊的未来>, ...
- 上下文可编程区块链——以太坊的未来
区块链的火热就不用说了,回看一下Vinay Gupta 2015年发表的文章<内容可编程的区块链:以太坊的未来>,感受一下先行者的思想. 到本文结束时,你将了解一般的区块链(特别是下一代区 ...
- 区块链-以太坊学习资料汇总
最近一段时间以来,对区块链的底层研究和基于区块链的应用开发已经越来越火热了.机缘巧合,目前我也在进行区块链方面的开发,在之后的博客中,我会和大家一起分享区块链开发中的酸甜苦辣.今天我先来对收藏区块链- ...
- 区块链以太坊以及hyperledger总结
https://learnblockchain.cn/ 1.什么是智能合约?它有什么特点? 就是具有交互能力而且能够在区块链中传递的合约 一个由计算机代码控制的以太币账户 特点: 公开透明.能即时与区 ...
- 区块链以太坊学习笔记
以太坊物联网区块链(一) Web3js 与 java 连接区块链可以参考我github上的两个小demo Ethereum-java Ethereum-javascript 搭建私有链, 利用以太坊平 ...
最新文章
- php将数组最后一个单元弹出,php array_pop()数组函数将数组最后一个单元弹出(出栈)...
- Solaris10 for x86网卡替换配置
- java拆解_深入拆解Java虚拟机视频教程
- linux系统用xset命令,专 linux命令之set x详解
- nginx index.php 端口,nginx-如果index.php不在nginx文件夹中,则禁止使用php fpm
- 要运行python程序要安装什么_傲视天地
- AngularJS:表达式
- python 多条件 选择 算法_python部署python算法 - 快速寻找满足条件的两个数
- go json数据出现unicode_【Android】OkHttp、Retrofit拿到的json是Unicode,我要的是UTF-8呀...
- python字符串数字比较大小_Python 2如何比较string和int?为什么列表比数字大,元组比列表大?...
- ActiveMQ消息的持久化策略
- php open basedir配置,php下open_basedir的配置
- 配置kafka Server
- VS2015 更换exe的图标
- 【多目标优化求解】基于matlab遗传算法求解多目标配电网重构模型【含Matlab源码 970期】
- mac安装mysql后找不到_Mac 安装MySQL数据库,系统提示mysql: command not found怎么办
- Android中使用Iconfont图标制作自己的矢量图库
- 只有努力了,你才能成为想要的样子
- java编写连接数据库代码
- 解决PostgreSQL远程访问报错could not connect to server:Connection refused (0x0000274D/10061)
热门文章
- mysql clr_SQLCLR Tips: 配置数据库使其支持SQLCLR
- 平面直角坐标系中的旋转公式_初一下学期,平面直角坐标系中求图形面积,转化与化归思想的体现...
- python字节码执行函数_做一个字节码追踪器,从内部理解 Python 的执行过程
- MDC功能软件-感知融合算法介绍
- LightGCN: Simplifying and Powering Graph Convolution Network for Recommendation 论文笔记
- 实验5.1 编写并测试3×3矩阵转置函数
- 跑步(【CCF】NOI Online能力测试 入门组第二题)
- 自动驾驶——localization的学习笔记
- 敏捷开发中asp.net MVC的开发次序感受(先开发View?先开发Model?先开发Controller!)...
- extjs fieldset 和 radio