Easy-Go-Web3
知识图谱Go 教程React Web3智能合约
需求分析系统设计设计模式Go 微服务
项目实战DevOps
Go 生态React 生态智能合约生态Web3 生态AI × Web3工具箱Web3 公司远程Web3求职
🎯 AA 工程师面试手册博客
GitHub
项目实战NFT 交易市场后端
中级NFT 开发5周

NFT 交易市场后端

完整的 NFT 市场服务,支持挂单、竞拍、版税分成

技术栈

GoGinPostgreSQLRedisIPFSElasticsearch

核心功能

固定价格与拍卖模式
EIP-712 链下签名订单
版税与平台费分成
集合与稀有度分析
实时活动推送
防刷单检测

系统架构


┌──────────────┐     ┌─────────────┐     ┌──────────────┐
│   Clients    │────▶│   Gin API   │────▶│  Blockchain  │
│  (REST/WS)   │     │   Service   │     │  (Settlement)│
└──────────────┘     └─────────────┘     └──────────────┘
                            │
        ┌───────────────────┼───────────────────┐
        ▼                   ▼                   ▼
  ┌──────────┐        ┌──────────┐        ┌──────────┐
  │PostgreSQL│        │  Redis   │        │Elasticsearch│
  │(Orders)  │        │(Cache/WS)│        │ (Search)  │
  └──────────┘        └──────────┘        └──────────┘
  

课程章节

第一章:EIP-712 签名订单系统

EIP-712 结构化签名2小时
订单数据结构设计1.5小时
签名验证实现2小时

第二章:订单簿与匹配引擎

订单簿数据结构2小时
固定价格订单匹配1.5小时
拍卖机制实现2.5小时

第三章:版税与费用系统

EIP-2981 版税标准1.5小时
多级费用分成2小时
结算合约交互2小时

第四章:搜索与推荐

Elasticsearch 索引2小时
稀有度排名1.5小时
推荐算法2小时

核心代码实现

EIP-712 订单签名

go
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 construction
20}
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 false
27 }
28 return crypto.PubkeyToAddress(*pubKey) == o.Maker
29}

订单匹配引擎

go
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, err
5 }
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 }, nil
26}
DEX 聚合器后端DeFi 投资组合追踪器
Easy-Go-Web3

构建 Go 后端与 Web3 的学习之路。从基础到进阶,从理论到实践,助你成为全栈区块链开发者。

学习路径

  • 知识图谱
  • Go 教程
  • Go 微服务
  • 面试手册

资源中心

  • 工具箱
  • DevOps 工具
  • Web3 生态
  • 博客

© 2025 Easy-Go-Web3. All rights reserved.

Created withbyhardybao