案例业务:土豪发红包,屌丝抢红包
需要实现的核心功能:
- 发红包
- 抢红包
- 金额退还
- 合约销毁
角色分析:
- 土豪
- 屌丝
土豪操作:
- 发红包
- 红包退回
- 销毁合约
屌丝操作:
- 抢红包
具体代码实现如下:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
/* 1.sendpacket:随机或平均;
2.stakepacket;
*/
contract redpacket{
bool public rType; //记录红包类型
uint8 public rCount; //红包数量
uint256 public rTotalAmount; //总金额
address public tuhao; //记录发红包人的地址
mapping (address=>bool) panduan; //判断是否已经抢过红包
constructor(bool isAvg,uint8 _count,uint _amount)payable //是平均还是随机;红包数量
{
rType = isAvg;
rCount = _count;
rTotalAmount=_amount;
require(_amount == msg.value,"redpacket's balance is ok");
tuhao = msg.sender;
}