完整的 NFT 市场服务,支持挂单、竞拍、版税分成
┌──────────────┐ ┌─────────────┐ ┌──────────────┐
│ Clients │────▶│ Gin API │────▶│ Blockchain │
│ (REST/WS) │ │ Service │ │ (Settlement)│
└──────────────┘ └─────────────┘ └──────────────┘
│
┌───────────────────┼───────────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│PostgreSQL│ │ Redis │ │Elasticsearch│
│(Orders) │ │(Cache/WS)│ │ (Search) │
└──────────┘ └──────────┘ └──────────┘
1type Order struct {2 Maker common.Address `json:"maker"`3 Taker common.Address `json:"taker"`4 Collection common.Address `json:"collection"`5 TokenID *big.Int `json:"token_id"`6 Price *big.Int `json:"price"`7 Currency common.Address `json:"currency"`8 OrderType uint8 `json:"order_type"`9 StartTime uint64 `json:"start_time"`10 EndTime uint64 `json:"end_time"`11 Salt *big.Int `json:"salt"`12 Signature []byte `json:"signature"`13}14 15func (o *Order) Hash() common.Hash {16 typeHash := crypto.Keccak256Hash([]byte(17 "Order(address maker,address taker,address collection,uint256 tokenId,uint256 price,address currency,uint8 orderType,uint64 startTime,uint64 endTime,uint256 salt)",18 ))19 // ... EIP-712 hash construction20}21 22func (o *Order) VerifySignature() bool {23 hash := o.Hash()24 pubKey, err := crypto.SigToPub(hash.Bytes(), o.Signature)25 if err != nil {26 return false27 }28 return crypto.PubkeyToAddress(*pubKey) == o.Maker29}1func (e *MatchingEngine) MatchOrder(ctx context.Context, buyOrder, sellOrder *Order) (*MatchResult, error) {2 // 验证订单3 if err := e.validateOrders(buyOrder, sellOrder); err != nil {4 return nil, err5 }6 7 // 计算费用8 platformFee := new(big.Int).Div(9 new(big.Int).Mul(sellOrder.Price, big.NewInt(int64(e.platformFeeBps))),10 big.NewInt(10000),11 )12 13 royalty, _ := e.getRoyalty(ctx, sellOrder.Collection, sellOrder.TokenID, sellOrder.Price)14 15 sellerProceeds := new(big.Int).Sub(sellOrder.Price, platformFee)16 sellerProceeds.Sub(sellerProceeds, royalty)17 18 return &MatchResult{19 BuyOrder: buyOrder,20 SellOrder: sellOrder,21 Price: sellOrder.Price,22 PlatformFee: platformFee,23 Royalty: royalty,24 SellerProceeds: sellerProceeds,25 }, nil26}