区块链应用--Web3彩票网站--Polygon 以太坊Layer2
01/19
本文最后更新于
2023年01月27日,已超过
796天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
网站链接点击上面 标题
今天的这个Web3项目是一个部署在Polygon主网的项目,Polygon是一个以太坊的Layer 2网络。
视频(Youtube,Bilibili)
实现功能
- 买彩票
- 显示池子中的钱数
- 显示上一轮获胜玩家
- 手机观看优化模式
- 手机和PC端均可流畅完成项目
- 前端云部署
- 使用Ploygon主网部署
智能合约代码
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
contract Lottery {
address public owner;
address payable[] public players;
address[] public winners;
uint public lotteryId;
constructor() {
owner = msg.sender;
lotteryId = 0;
}
function getWinners() public view returns (address[] memory){
return winners;
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
function getPlayers() public view returns (address payable[] memory) {
return players;
}
function enter() public payable {
require(msg.value >= .01 ether);
// address of player entering lottery
players.push(payable(msg.sender));
}
function getRandomNumber() public view returns (uint) {
return uint(keccak256(abi.encodePacked(owner, block.timestamp)));
}
function getLotteryId() public view returns(uint) {
return lotteryId;
}
function pickWinner() public onlyOwner {
uint randomIndex = getRandomNumber() % players.length;
players[randomIndex].transfer(address(this).balance);
winners.push(players[randomIndex]);
lotteryId++;
// Clear the players array. ['player1', 'player2'] 👉 []
players = new address payable[](0);
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
}
感谢Clever Programmer的分享,开源万岁,希望大家能帮助到大家
情感表达稍显含蓄,可适当强化渲染。
选材新颖独特,通过细节描写赋予主题鲜活生命力。