78 large

会议预定智能合约

msino · 于 发布 · 最后由 yaoy26回复 · 614 次阅读

最近在研究solidity,写了个demo练练手,请大家多多指导

这个合约可以解决参会人员与组织者之间的信任问题:
1. 参会人付钱预定了一个会议但是最后会议没开成
2. 组织者组织了一个会议但是最后参会者不付钱

会议角色

组织者可执行的动作
* 创建一个会议
发布会议主题,内容,地点,票价,人数,起止时间等
* 检票
可检查某个地址是否购买了当前会议的门票
* 调整会议人数
新的会议人数必须大于已经报名成功的人数
* 结束会议
只有当当前时间大于会议结束时间,组织者才可以结束会议,会议结束合约自动将所有的票款发送到组织者的账户

参会者可执行的动作
* 买票
购买一张门票,如果购买时间大于会议开始时间,用户余额不足,参会人数超过限制都不可以购买
* 退票
只要在会议开始前,参会者都可以发起退票

合约代码如下:

contract Conference { 

    bytes32 public topic;
    bytes32 public content;
    bytes32 public conferenceAddress;
    uint public amount;
    uint public maxNum;
    uint public beginTime;
    uint public endTime;        
    bytes32 public webSite;

    address public organizer;
    uint public joinNum;

    address private x = 0x0;

    mapping(address => User) private joinUsers;

    modifier onlyOwner()  {
            if (msg.sender != organizer) throw;
            _
     }

     modifier noMoney() {
            if (msg.value < amount) throw;
            _
     }

    modifier noOverTime() {
            if (now >= beginTime) throw;
            _
     }

    struct User {
        address userAddress;
        uint num ;
    }

    function Conference(bytes32 _topic, bytes32 _content, bytes32 _conferenceAddress, uint _amount, uint _maxNum, uint _beginTime, uint _endTime, bytes32 _webSite) {
        if (_maxNum <= 0 || _amount < 0)  throw;
        if (_beginTime < now || _endTime <= _beginTime) throw;
        topic = _topic;
        content = _content;
        conferenceAddress = _conferenceAddress;
        amount = _amount;
        maxNum = _maxNum;
        beginTime = _beginTime;
        endTime = _endTime;
        webSite = _webSite;
        organizer = msg.sender;
        joinNum = 0;
    }

    function buyTicket() noMoney  noOverTime public returns(bool success) {
        if (joinNum >= maxNum) {
            return false;
        }
        joinNum++;
        User u = joinUsers[msg.sender];
        if (u.userAddress == x) {  //not exist, add in mapping
            joinUsers[msg.sender] = User(msg.sender, 1);
        } else {
            u.num ++;
        }
        return true;
    }

    function refundTicket() noOverTime public returns(bool success) {
        address recipient = msg.sender;
        User u = joinUsers[recipient];
        if (u.userAddress != x) {
            address myAddress = this;
            uint refundAmount = amount * u.num;
            if (myAddress.balance >= refundAmount) {
                recipient.send(refundAmount);
                joinUsers[recipient] = User(x, 0);
                joinNum--;
                return true;
            }
        }
        return false;
    }


    function checkTicket(address recipient) onlyOwner public returns(bool success) {
            User u = joinUsers[recipient];
        if (u.userAddress != x) {
            joinUsers[recipient] = User(x, 0);
            return true;
        }
        return false;
    }

    function changeMaxNum(uint newMaxNum) onlyOwner public  returns(bool success) {     
        if (newMaxNum <= joinNum) {
            return false;
        }
        maxNum = newMaxNum;
        return true;
    }

    function finish()  onlyOwner {
        if (now < endTime) throw;
        if (this.balance > 0) {
            //send all balance to organizer 
            organizer.send(this.balance);
        }
    }

    function destroy()  onlyOwner {
        if (now < endTime) throw;
        suicide(organizer);
    }

}
  • 3 large
    tomlion

    有点像上次DAPP培训第二天实战的题目哦,赞!

  • 1672 large
    yaoy26

    这个信任问题在代码上是怎么解决的“1. 参会人付钱预定了一个会议但是最后会议没开成”,现在合约的逻辑,如果买票后,会议发起人没开始会议到时间也是可以提取钱的吧?