安装
- 下载最新版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>
注意