solidity实战练习3——荷兰拍卖

复制代码
//SPDX-License-Identifier:MIT
pragma solidity ^0.8.24;
interface IERC721{
    function transFrom(address _from,address _to,uint nftid) external ;
}

contract DutchAuction
{   
    address payable immutable seller;//卖方
    uint immutable startTime;//拍卖开始时间
    uint immutable endTime;//拍卖结束时间
    uint immutable lastTime;//拍卖持续时间
    uint immutable startPrice;//启始价格
    uint immutable discount;//每秒减少多少钱

    IERC721 public nft;//nft 变量能够存储一个已经部署的符合 IERC721 接口的合约地址
    uint public nftId;

    constructor(//对合约中的不可变变量进行赋值
    address payable _seller,
    uint  _startTime,

    uint  _lastTime,
    uint  _startPrice,
    uint  _discount,
    IERC721  _nft,
    uint _nftId
    ){
        seller=_seller;
        startTime=_startTime;

        lastTime=_lastTime;
        endTime=_lastTime+_startTime;
        startPrice=_startPrice;
        discount=_discount;
        require(startPrice>discount * lastTime,"it may become nagetive number");//检查一下防止拍卖价成为负数
        nft=_nft;
        nftId=_nftId;
    }
    function getPrice() public view returns(uint ){//获取当前(调用本函数的时刻)的价格
        require(startTime+block.timestamp<=endTime,"Sorry,you comes too late");//检查是否流派
       return(startPrice-(block.timestamp-startTime)*discount);
    }
    function buy()external payable returns(bool)
    {
        uint nowPrice=getPrice();
        require(msg.value>= nowPrice,"You have no enough money");
        require(startTime+block.timestamp<=endTime,"Sorry,you comes too late");
        nft.transFrom(seller,msg.sender,nftId);
        uint  refund = msg.value- nowPrice;
        payable(msg.sender).transfer(refund);
        seller.transfer(nowPrice);
        destroy();
        return true;
    }
    function destroy()public payable{
        seller.transfer(address(this).balance);
    }
}

荷兰合约(Dutch Auction Contract)是一种智能合约,通常运行在区块链上,用于实现荷兰式拍卖(Dutch Auction)的拍卖逻辑。荷兰合约的设计旨在确保拍卖过程的透明性、安全性和自动化执行。

荷兰拍卖的基本组成和功能:

  1. 起始价格设定

    • 合约会设定一个初始的高价作为起始拍卖价格。
  2. 价格递减设定

    • 根据预定的规则或时间间隔,合约会以固定速度递减拍卖价格。这个递减的速度通常是事先确定好的,可以根据拍卖的需求调整。
  3. 买家接受价格

    • 参与者可以在拍卖过程中选择接受当前价格。一旦有人接受了当前价格,拍卖即结束。
  4. 自动执行

    • 荷兰合约通常具备自动执行功能,一旦有人接受了当前价格,合约会立即执行资金的转移和商品或服务的交割。
  5. 资金管理

    • 合约会确保安全地处理所有资金的转移,确保卖方和买方的权益。
  6. 拍卖结束条件

    • 拍卖可以在以下条件下结束:
      • 有人接受了当前价格;
      • 拍卖价格降至预设的最低价;
      • 达到预定的拍卖结束时间。
相关推荐
梦帮科技3 小时前
GRAVIS v4.0:基于Web的极速套利架构设计与实时数据流实现
前端·人工智能·rust·自动化·区块链·智能合约·数字货币
梦帮科技14 小时前
GRAVIS v5.5:向硬核桌面端进化与云端多节点容灾部署实践
windows·架构·rust·自动化·区块链·智能合约·数字货币
北冥you鱼1 天前
abigen 最佳实践:从入门到精通,高效生成 Go 语言合约绑定
开发语言·golang·区块链
闲研随记1 天前
【文献阅读】BunnyFinder:探寻以太坊共识机制中的激励机制漏洞
区块链·agent·漏洞·以太坊·ppo·incentive flaws
双缝观察者2 天前
USDT中电子数据会计鉴定和链上资金认定
区块链
梦帮科技2 天前
基于EVM架构的Web3音频确权与RNS Token智能合约设计深度解析
人工智能·python·架构·web3·区块链·音视频·智能合约
北冥you鱼2 天前
智能合约分类详解:逻辑合约、部署合约与业务合约
区块链·智能合约
梦帮科技3 天前
从零到一构建音乐版权公链:RNS Token 区块链基础设施与智能合约架构全解析
架构·区块链·智能合约
Web3李李3 天前
如何利用真实故事做 Web3 项目营销 —— 结合\(LABUBU、\)ASTEROID 两大万倍代币拆解
web3·区块链·软件开发·dapp开发·太空狗·拉布布labubu
区块链小八歌3 天前
探索 Aqua,Hyperliquid 如何打通衍生品流动性向零售渗透的最终圣杯
区块链