AI 辅助的 Web3 社交图谱分析与推荐:从链上行为到社交网络,去中心化身份的关系挖掘

cover

一、Web3 社交的图谱困境:链上行为的语义鸿沟

Web3 社交协议(如 Farcaster、Lens Protocol)将社交关系建立在链上,用户的关注、转发、点赞等行为以智能合约事件的形式永久记录。然而,链上行为数据存在显著的"语义鸿沟":一个地址之间的 ETH 转账可能是朋友间的日常交互,也可能是 NFT 交易的支付,还可能是 DeFi 协议的清算操作。仅从链上原始事件无法区分社交行为与金融行为。

AI 辅助的 Web3 社交图谱分析,通过多维度行为特征提取与语义推理,将原始链上事件转化为有意义的社交关系,并基于图谱结构实现个性化推荐。

二、链上行为到社交图谱的映射机制

flowchart TD
    A[链上事件流] --> B[行为分类引擎]
    B --> C[社交图谱构建]
    C --> D[图谱推理与推荐]

    subgraph 行为分类
        B1[转账: 社交/金融/DeFi]
        B2[NFT 交互: 收藏/交易/铸造]
        B3[DAO 参与: 投票/提案/贡献]
        B4[社交协议: 关注/转发/评论]
    end

    subgraph 图谱节点
        C1[地址节点]
        C2[协议节点]
        C3[内容节点]
    end

    subgraph 推理任务
        D1[相似用户发现]
        D2[社区检测]
        D3[影响力评估]
        D4[内容推荐]
    end

    A --> B1
    A --> B2
    A --> B3
    A --> B4
    C --> C1
    C --> C2
    C --> C3
    D --> D1
    D --> D2
    D --> D3
    D --> D4

行为分类引擎是关键组件:通过交易模式(频率、金额、时间分布)、合约交互类型(社交协议 vs DeFi 协议)、NFT 铸造与持有模式,将原始链上事件分类为社交行为与金融行为,仅将社交行为纳入图谱构建。

三、工程实现:Web3 社交图谱分析系统

// social-graph-analyzer.ts — Web3 社交图谱分析引擎
import { createPublicClient, http, parseAbi } from 'viam';
import { mainnet } from 'viam/chains';

interface SocialEdge {
  from: string;
  to: string;
  weight: number;     // 关系强度 0-1
  edgeType: 'follow' | 'interaction' | 'shared_interest';
  lastActivity: number;
}

interface GraphNode {
  address: string;
  ensName?: string;
  socialScore: number;  // 社交影响力评分
  interests: string[];  // 兴趣标签
  communities: string[]; // 所属社区
}

class SocialGraphAnalyzer {
  private client;

  constructor(rpcUrl: string) {
    this.client = createPublicClient({
      chain: mainnet,
      transport: http(rpcUrl),
    });
  }

  // 从链上事件构建社交图谱
  async buildSocialGraph(
    targetAddress: string,
    depth: number = 2
  ): Promise<{ nodes: GraphNode[]; edges: SocialEdge[] }> {
    const nodes: GraphNode[] = [];
    const edges: SocialEdge[] = [];
    const visited = new Set<string>();

    await this.bfsGraph(targetAddress, depth, visited, nodes, edges);

    return { nodes, edges };
  }

  private async bfsGraph(
    address: string,
    remainingDepth: number,
    visited: Set<string>,
    nodes: GraphNode[],
    edges: SocialEdge[]
  ): Promise<void> {
    if (remainingDepth <= 0 || visited.has(address)) return;
    visited.add(address);

    // 1. 提取 Farcaster 关注关系
    const follows = await this.extractFarcasterFollows(address);
    for (const follow of follows) {
      edges.push({
        from: address,
        to: follow,
        weight: 1.0,
        edgeType: 'follow',
        lastActivity: Date.now(),
      });
    }

    // 2. 提取链上交互关系(转账、NFT 交互)
    const interactions = await this.extractOnChainInteractions(address);
    for (const interaction of interactions) {
      // AI 分类:判断交互是否具有社交属性
      const isSocial = await this.classifyInteraction(interaction);
      if (isSocial) {
        edges.push({
          from: address,
          to: interaction.counterparty,
          weight: interaction.frequency / 10,  // 归一化
          edgeType: 'interaction',
          lastActivity: interaction.lastTimestamp,
        });
      }
    }

    // 3. 提取共同兴趣(持有相同 NFT 集合、参与相同 DAO)
    const sharedInterests = await this.extractSharedInterests(address);
    for (const interest of sharedInterests) {
      edges.push({
        from: address,
        to: interest.address,
        weight: interest.overlap / interest.total,
        edgeType: 'shared_interest',
        lastActivity: Date.now(),
      });
    }

    // 构建节点
    const node = await this.buildGraphNode(address);
    nodes.push(node);

    // 递归扩展
    for (const edge of edges.filter(e => e.from === address)) {
      await this.bfsGraph(edge.to, remainingDepth - 1, visited, nodes, edges);
    }
  }

  // AI 交互分类:区分社交行为与金融行为
  private async classifyInteraction(
    interaction: OnChainInteraction
  ): Promise<boolean> {
    const prompt = `判断以下链上交互是否具有社交属性:

交互类型: ${interaction.type}
交易金额: ${interaction.value} ETH
频率: ${interaction.frequency} 次/月
合约类型: ${interaction.contractType}
时间分布: ${interaction.timePattern}

社交属性的判断标准:
1. 小额频繁转账(< 0.01 ETH)更可能是社交
2. NFT 转赠(非交易)是社交行为
3. DeFi 协议交互通常是金融行为
4. 社交协议交互是社交行为

请输出 JSON: {"isSocial": true/false, "confidence": 0-1, "reason": "..."}`;

    const response = await callLLM(prompt, { temperature: 0.1 });
    const result = JSON.parse(response);
    return result.isSocial && result.confidence > 0.7;
  }

  // 基于图谱的个性化推荐
  async recommendAccounts(
    targetAddress: string,
    topK: number = 10
  ): Promise<Array<{ address: string; reason: string; score: number }>> {
    const { nodes, edges } = await this.buildSocialGraph(targetAddress, 2);

    // 计算二度关系中的推荐候选
    const directFollows = new Set(
      edges
        .filter(e => e.from === targetAddress && e.edgeType === 'follow')
        .map(e => e.to)
    );

    // 二度关系:朋友的朋友
    const candidates = new Map<string, { score: number; paths: string[] }>();

    for (const follow of directFollows) {
      const secondDegree = edges
        .filter(e => e.from === follow && e.edgeType === 'follow')
        .map(e => e.to);

      for (const candidate of secondDegree) {
        if (candidate === targetAddress || directFollows.has(candidate)) continue;

        const existing = candidates.get(candidate) || { score: 0, paths: [] };
        existing.score += 1; // 每个共同关注增加评分
        existing.paths.push(follow);
        candidates.set(candidate, existing);
      }
    }

    // 按评分排序,返回 Top-K
    return Array.from(candidates.entries())
      .sort((a, b) => b[1].score - a[1].score)
      .slice(0, topK)
      .map(([address, data]) => ({
        address,
        reason: `${data.paths.length} 个共同关注: ${data.paths.slice(0, 3).join(', ')}`,
        score: data.score,
      }));
  }
}

四、Web3 社交图谱分析的边界与权衡

链上数据的隐私边界:虽然区块链数据公开可查,但构建社交图谱涉及对用户行为的聚合分析,可能暴露用户不愿公开的社交关系。建议仅分析用户主动公开的社交协议数据(Farcaster 关注),对链上交互的社交推理需用户授权。

图谱构建的计算成本:BFS 遍历链上事件的 RPC 调用次数随深度指数增长,2 度关系的构建可能需要数千次 RPC 调用。建议使用 The Graph 等索引服务替代直接 RPC 调用,将查询延迟从分钟级降至秒级。

地址归并的不确定性:同一用户可能拥有多个地址,图谱中的"不同节点"实际是同一人。地址归并(通过 ENS、CEX 充提记录、链上行为模式)是图谱质量的关键前提,但完全准确的归并几乎不可能。

推荐的可解释性:Web3 用户对推荐算法的透明度有更高期待。推荐理由应明确展示(如"3 个共同关注"而非"算法推荐"),让用户理解推荐的依据。

五、总结

AI 辅助的 Web3 社交图谱分析,将链上行为数据转化为有意义的社交关系,并基于图谱结构实现个性化推荐。核心机制是行为分类引擎区分社交与金融行为、BFS 构建多度关系图谱、共同关注算法驱动推荐。工程落地的关键在于:使用索引服务降低链上查询成本、用户授权限定隐私边界、推荐理由可解释增强信任。Web3 社交图谱的价值不在于替代 Web2 社交网络,而在于提供用户自主控制的、可验证的社交关系基础设施。

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐