NFT交易合约核心功能的实现思路(二)

上一篇我们讲了一口价交易NFT的思路,这次我们理一下拍卖的实现思路。

拍卖的情况,我们除了要保存NFT基础信息之外,还要保存“起拍价”和“结束时间”两个参数。在函数的原型上体现如下:

```javascript
function onAuctionNft(address _token, uint256 _nftType, address _nftAddress, uint256 _id, uint256 _num, uint256 _value, uint256 _endTime) external
```

将NFT转移到合约的步骤不再重复,现在需要定义一个结构体来保存拍卖信息。这里比函数多了一个手续费,是可以单独配置的。

```javascript
struct AuctionShelves {
    address token;
    uint256 nftType;
    address nftAddress;
    address seller;
    uint256 id;
    uint256 num;
    uint256 value;
    uint256 endTime;
    uint256 fee;
}
```

 拍卖的第二个重点信息在于保存出价信息。我们定义如下结构体:

```javascript
struct Bid {
    uint256 id;
    address bidder;
    uint256 value;
}
```

我们假设tokenID就是订单号,首先从ass中把拍卖信息取回,再从bs中取回出价信息。

首先判断是否已经超过拍卖结束时间,然后判断当前出价即高于底价,又高于当前价格。随后保存出价信息。

如果是第一次出价,直接用bs保存出价信息。

如果是第二次出价,则在用bs保存出价信息后,再将第一次出价的token退还给上一个参与竞拍的人。

```javascript
function _bidAuctionNft(uint256 _id, address _seller, uint256 amount) internal virtual {
    AuctionShelves memory item = ass[_id][_seller];
    Bid memory bsItem = bs[_id][_seller];
    require(block.timestamp < item.endTime, "The auction is over");
    require(amount > item.value, "This quotation is less than the current quotation");
    require(amount > bsItem.value, "This quotation is less than the current quotation");
    IERC20(item.token).transferFrom(msg.sender, address(this), amount);
    if (bsItem.value == 0) {
        bs[_id][_seller] = Bid(_id, msg.sender, amount);
    } else {
        bs[_id][_seller] = Bid(_id, msg.sender, amount);
        IERC20(item.token).transfer(bsItem.bidder, bsItem.value);
    }
    emit BuyAuctionNft(_id, amount);
}
```

智能合约的被动属性,导致我们没有定时器来完成最后的成交步骤。因此等到拍卖结束,卖家或买家需要调用一个“settlement”方法,确认交易时间结束,交易达成,将token和NFT转移给买家和卖家。

需要注意的是,在本例中,我们要及时删除掉出价信息,以免商品第二次上架拍卖时,导致数据混乱。

代码就省略不提了。

猜你喜欢

转载自blog.csdn.net/2301_76642277/article/details/129836343
今日推荐