@dfinity/sns

中文文档
8.0.0发布于 2026/09/22Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2258

A library for interfacing with a Service Nervous System (SNS) project.

原始使用文档
文档目录6 节
  1. Table of contents
  2. Installation
  3. Usage
  4. Explorative way
  5. Descriptive way
  6. Documentation

sns-js

A library for interfacing with a Service Nervous System (SNS) project.

npm version GitHub license

[!TIP] Still using @dfinity/sns? Upgrade to @icp-sdk/canisters/sns!

Table of contents

Installation

You can use sns-js by installing it in your project.

npm i @dfinity/sns

The bundle needs peer dependencies, be sure that following resources are available in your project as well.

npm i @icp-sdk/core @dfinity/utils @dfinity/ledger

Usage

sns-js can be utilized with two distinct approaches. The first approach is explorative, where you only need to provide the SNS root canister ID of your project to initialize a wrapper that handles routing the calls to the appropriate canister. This means having a single entry point for all functions. The second approach, which is more common, involves instantiating the specific canisters you require.

Explorative way

The explorative approach has the advantage to simplify the code but, implies more costs as it queries the root canister for the list of canister IDs of the Sns project upon initialization.

import { createAgent } from "@dfinity/utils";
import { initSnsWrapper } from "@dfinity/sns";

const agent = await createAgent({
  identity,
  host: HOST,
});

const wrapper = await initSnsWrapper({
  rootOptions: {
    canisterId: rootCanisterId,
  },
  agent,
  certified,
});

const { metadata, swapState } = wrapper;
const [data, token] = await metadata({});

console.log("Sns:", data, token, swapState);

Descriptive way

The descriptive approach limits the scope of the features but, is more verbose.

import { createAgent } from "@dfinity/utils";
import { SnsGovernanceCanister } from "@dfinity/sns";

const agent = await createAgent({
  identity,
  host: HOST,
});

const { metadata } = SnsGovernanceCanister.create({
  agent,
  canisterId: rootCanisterId,
});

const data = await metadata({ certified: true });

console.log("Summary data:", data);

Documentation

You can find the API docs here.

版本与发布记录

@dfinity/sns 共发布 1,079 个版本,首次发布于 2022/07/13;过去 12 个月发布了 32 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.0.0latest2026/09/221 个月前
7.1.0-next-2026-04-21.1next2026/04/216 个月前
7.1.0-next-2026-04-212026/04/216 个月前
7.1.0-next-2026-04-202026/04/216 个月前
7.1.0-next-2026-02-182026/02/188 个月前
7.1.0-next-2026-02-112026/02/119 个月前

其他发布通道:nightly → 0.0.3-nightly-2022-08-08.1,beta → 7.0.0-beta-2025-12-18,next → 7.1.0-next-2026-04-21.1。

关于 @dfinity/sns 的常见问题

@dfinity/sns 是什么?

A library for interfacing with a Service Nervous System (SNS) project.

如何安装 @dfinity/sns?

在项目目录运行 npm install @dfinity/sns,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@icp-sdk/canisters(^4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dfinity/sns 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dfinity/sns 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dfinity/sns 可以商用吗?

@dfinity/sns 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@dfinity/sns 的下载量有多少?

根据 npm 官方下载统计,@dfinity/sns 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,258 次,日均约 75 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dfinity/sns 还在维护吗?

最新版本 8.0.0 发布于 2026/09/22(1 个月前);至今共发布 1079 个版本,其中过去 12 个月发布了 32 个;首次发布于 2022/07/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。