Easy-Go-Web3
知识图谱Go 教程React Web3智能合约
需求分析系统设计设计模式Go 微服务
项目实战DevOps
Go 生态React 生态智能合约生态Web3 生态AI × Web3工具箱Web3 公司远程Web3求职
🎯 AA 工程师面试手册博客
GitHub
项目实战NFT 元数据服务
初级区块链开发2周

NFT 元数据服务

解析 NFT 元数据,支持 IPFS/Arweave 存储,图片缓存与压缩

技术栈

GoFiberIPFSArweaveRedisMinIO

核心功能

ERC721/ERC1155 元数据解析
IPFS/Arweave 网关代理
图片缓存与压缩
元数据标准化输出
批量获取 NFT 信息
稀有度分析

系统架构


┌──────────────┐     ┌─────────────┐     ┌──────────────┐
│   Clients    │────▶│  Fiber API  │────▶│  Blockchain  │
│  (REST/HTTP) │     │   Service   │     │ (go-ethereum)│
└──────────────┘     └─────────────┘     └──────────────┘
                            │
               ┌────────────┼────────────┐
               ▼            ▼            ▼
         ┌──────────┐ ┌──────────┐ ┌──────────┐
         │  Redis   │ │  MinIO   │ │  IPFS    │
         │ (Cache)  │ │ (Images) │ │ (Gateway)│
         └──────────┘ └──────────┘ └──────────┘
  

课程章节

第一章:NFT 元数据标准解析

ERC721/ERC1155 tokenURI 标准1小时
元数据 JSON 结构45分钟
属性与稀有度概念45分钟

第二章:IPFS 与 Arweave 网关

去中心化存储原理1小时
多网关代理实现1.5小时
内容寻址与验证1小时

第三章:图片缓存与处理

MinIO 对象存储配置45分钟
图片下载与缓存1小时
缩略图生成1小时

第四章:API 服务与稀有度分析

Fiber 路由设计1小时
稀有度计算算法1.5小时
批量查询优化1小时

核心代码实现

NFT 元数据结构

go
1type NFTMetadata struct {
2 Name string `json:"name"`
3 Description string `json:"description"`
4 Image string `json:"image"`
5 ExternalURL string `json:"external_url,omitempty"`
6 AnimationURL string `json:"animation_url,omitempty"`
7 Attributes []NFTAttribute `json:"attributes,omitempty"`
8}
9
10type NFTAttribute struct {
11 TraitType string `json:"trait_type"`
12 Value interface{} `json:"value"`
13 DisplayType string `json:"display_type,omitempty"`
14}
15
16func (s *NFTService) GetTokenURI(ctx context.Context, chain, contract string, tokenID *big.Int) (string, error) {
17 client := s.clients[chain]
18
19 // 尝试 ERC721
20 uri, err := s.callTokenURI(ctx, client, contract, tokenID)
21 if err != nil {
22 // 尝试 ERC1155
23 uri, err = s.callURI(ctx, client, contract, tokenID)
24 }
25
26 return uri, err
27}

IPFS 网关代理

go
1type StorageGateway struct {
2 ipfsGateways []string
3 arweaveGateways []string
4 httpClient *http.Client
5}
6
7func (g *StorageGateway) ResolveURI(ctx context.Context, uri string) ([]byte, error) {
8 switch {
9 case strings.HasPrefix(uri, "ipfs://"):
10 return g.fetchFromIPFS(ctx, strings.TrimPrefix(uri, "ipfs://"))
11 case strings.HasPrefix(uri, "ar://"):
12 return g.fetchFromArweave(ctx, strings.TrimPrefix(uri, "ar://"))
13 case strings.HasPrefix(uri, "data:"):
14 return g.decodeDataURI(uri)
15 default:
16 return g.fetchHTTP(ctx, uri)
17 }
18}
19
20func (g *StorageGateway) fetchFromIPFS(ctx context.Context, cid string) ([]byte, error) {
21 for _, gateway := range g.ipfsGateways {
22 url := fmt.Sprintf("%s/ipfs/%s", gateway, cid)
23 req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
24 resp, err := g.httpClient.Do(req)
25 if err != nil || resp.StatusCode != 200 {
26 continue
27 }
28 defer resp.Body.Close()
29 return io.ReadAll(resp.Body)
30 }
31 return nil, fmt.Errorf("failed to fetch from IPFS: %s", cid)
32}

稀有度计算

go
1func (s *NFTService) calculateRarity(ctx context.Context, chain, contract string, attrs []NFTAttribute) (*RarityInfo, error) {
2 distribution, err := s.getTraitDistribution(ctx, chain, contract)
3 if err != nil {
4 return nil, err
5 }
6
7 totalSupply := distribution.TotalSupply
8 score := 0.0
9 traitRarity := make(map[string]float64)
10
11 for _, attr := range attrs {
12 key := fmt.Sprintf("%s:%v", attr.TraitType, attr.Value)
13 count := distribution.Traits[key]
14
15 // 稀有度 = 1 / (trait_count / total_supply)
16 rarity := float64(totalSupply) / float64(count)
17 score += rarity
18 traitRarity[attr.TraitType] = rarity
19 }
20
21 return &RarityInfo{
22 Score: score,
23 TraitRarity: traitRarity,
24 }, nil
25}
钱包余额查询 APIGas 价格追踪器
Easy-Go-Web3

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

学习路径

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

资源中心

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

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

Created withbyhardybao