Socket
Socket
Sign inDemoInstall

@dfinity/sns

Package Overview
Dependencies
23
Maintainers
10
Versions
832
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @dfinity/sns

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


Version published
Weekly downloads
2.1K
decreased by-41.05%
Maintainers
10
Created
Weekly downloads
 

Readme

Source

sns-js

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

npm version GitHub license

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 @dfinity/agent @dfinity/candid @dfinity/principal @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 snsWrapper = 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);

Features

sns-js implements following features:

:toolbox: Functions

:gear: initSnsWrapper

Lookup for the canister ids of a Sns and initialize the wrapper to access its features.

FunctionType
initSnsWrapperInitSnsWrapper

:link: Source

:factory: SnsGovernanceCanister

:link: Source

Methods
:gear: create

Instantiate a canister to interact with the governance of a Sns project.

MethodType
create(options: SnsCanisterOptions<_SERVICE>) => SnsGovernanceCanister

Parameters:

  • options: Miscellaneous options to initialize the canister. Its ID being the only mandatory parammeter.

:link: Source

:gear: listNeurons

List the neurons of the Sns

MethodType
listNeurons(params: SnsListNeuronsParams) => Promise<Neuron[]>

:link: Source

:gear: listProposals

List the proposals of the Sns

MethodType
listProposals(params: SnsListProposalsParams) => Promise<ListProposalsResponse>

:link: Source

:gear: getProposal

Get the proposal of the Sns

MethodType
getProposal(params: SnsGetProposalParams) => Promise<ProposalData>

:link: Source

:gear: listNervousSystemFunctions

List Nervous System Functions Neurons can follow other neurons in specific Nervous System Functions.

MethodType
listNervousSystemFunctions(params: QueryParams) => Promise<ListNervousSystemFunctionsResponse>

:link: Source

:gear: metadata

Get the Sns metadata (title, description, etc.)

MethodType
metadata(params: QueryParams) => Promise<GetMetadataResponse>

:link: Source

:gear: nervousSystemParameters

Get the Sns nervous system parameters (default followees, max dissolve delay, max number of neurons, etc.)

MethodType
nervousSystemParameters(params: QueryParams) => Promise<NervousSystemParameters>

:link: Source

:gear: getNeuron

Get the neuron of the Sns

MethodType
getNeuron(params: SnsGetNeuronParams) => Promise<Neuron>

:link: Source

:gear: queryNeuron

Same as getNeuron but returns undefined instead of raising error when not found.

MethodType
queryNeuron(params: SnsGetNeuronParams) => Promise<Neuron or undefined>

:link: Source

:gear: manageNeuron

Manage neuron. For advanced users.

MethodType
manageNeuron(request: ManageNeuron) => Promise<ManageNeuronResponse>

:link: Source

:gear: addNeuronPermissions

Add permissions to a neuron for a specific principal

MethodType
addNeuronPermissions(params: SnsNeuronPermissionsParams) => Promise<void>

:link: Source

:gear: removeNeuronPermissions

Remove permissions to a neuron for a specific principal

MethodType
removeNeuronPermissions(params: SnsNeuronPermissionsParams) => Promise<void>

:link: Source

:gear: splitNeuron

Split neuron

MethodType
splitNeuron(params: SnsSplitNeuronParams) => Promise<NeuronId or undefined>

:link: Source

:gear: disburse

Disburse neuron on Account

MethodType
disburse(params: SnsDisburseNeuronParams) => Promise<void>

:link: Source

:gear: startDissolving

Start dissolving process of a neuron

MethodType
startDissolving(neuronId: NeuronId) => Promise<void>

:link: Source

:gear: stopDissolving

Stop dissolving process of a neuron

MethodType
stopDissolving(neuronId: NeuronId) => Promise<void>

:link: Source

:gear: stakeMaturity

Stake the maturity of a neuron.

MethodType
stakeMaturity({ neuronId, percentageToStake, }: SnsNeuronStakeMaturityParams) => Promise<void>

Parameters:

  • neuronId: The id of the neuron for which to stake the maturity
  • percentageToStake: Optional. Percentage of the current maturity to stake. If not provided, all of the neuron's current maturity will be staked.

:link: Source

:gear: disburseMaturity

Disburse the maturity of a neuron.

MethodType
disburseMaturity(params: SnsNeuronDisburseMaturityParams) => Promise<void>

Parameters:

  • toAccount. Account: to disburse maturity.
  • neuronId: The id of the neuron for which to disburse the maturity
  • percentageToDisburse: What percentage of the available maturity to disburse.

:link: Source

:gear: autoStakeMaturity

Changes auto-stake maturity for a Neuron.

MethodType
autoStakeMaturity(params: SnsNeuronAutoStakeMaturityParams) => Promise<void>

Parameters:

  • neuronId: The id of the neuron for which to request a change of the auto stake feature
  • autoStake: true to enable the auto-stake maturity for this neuron, false to turn it off

:link: Source

:gear: setDissolveTimestamp

Increase dissolve delay of a neuron

MethodType
setDissolveTimestamp(params: SnsSetDissolveTimestampParams) => Promise<void>

:link: Source

:gear: increaseDissolveDelay

Increase dissolve delay of a neuron

MethodType
increaseDissolveDelay(params: SnsIncreaseDissolveDelayParams) => Promise<void>

:link: Source

:gear: setTopicFollowees

Sets followees of a neuron for a specific Nervous System Function (topic)

MethodType
setTopicFollowees(params: SnsSetTopicFollowees) => Promise<void>

:link: Source

:gear: registerVote

Registers vote for a proposal from the neuron passed.

MethodType
registerVote(params: SnsRegisterVoteParams) => Promise<void>

:link: Source

:gear: refreshNeuron

Refresh neuron

MethodType
refreshNeuron(neuronId: NeuronId) => Promise<void>

:link: Source

:gear: claimNeuron

Claim neuron

MethodType
claimNeuron({ memo, controller, subaccount, }: SnsClaimNeuronParams) => Promise<NeuronId>

:link: Source

:factory: SnsRootCanister

:link: Source

Methods
:gear: create
MethodType
create(options: SnsCanisterOptions<_SERVICE>) => SnsRootCanister

:link: Source

:gear: listSnsCanisters

List the canisters that are part of the Sns.

Source code: https://github.com/dfinity/ic/blob/master/rs/sns/root/src/lib.rs

MethodType
listSnsCanisters({ certified, }: { certified?: boolean or undefined; }) => Promise<ListSnsCanistersResponse>

Parameters:

  • params.certified: - Query or update calls

:link: Source

:factory: SnsSwapCanister

:link: Source

Methods
:gear: create
MethodType
create(options: SnsCanisterOptions<_SERVICE>) => SnsSwapCanister

:link: Source

:gear: state

Get the state of the swap

MethodType
state(params: QueryParams) => Promise<GetStateResponse>

:link: Source

:gear: notifyPaymentFailure

Notify of the payment failure to remove the ticket

MethodType
notifyPaymentFailure() => Promise<Ticket or undefined>

:link: Source

:gear: notifyParticipation

Notify of the user participating in the swap

MethodType
notifyParticipation(params: RefreshBuyerTokensRequest) => Promise<RefreshBuyerTokensResponse>

:link: Source

:gear: getUserCommitment

Get user commitment

MethodType
getUserCommitment(params: GetBuyerStateRequest and QueryParams) => Promise<BuyerState or undefined>

:link: Source

:gear: getDerivedState

Get sale buyers state

MethodType
getDerivedState({ certified, }: QueryParams) => Promise<GetDerivedStateResponse>

:link: Source

:gear: getSaleParameters

Get sale parameters

MethodType
getSaleParameters({ certified, }: QueryParams) => Promise<GetSaleParametersResponse>

:link: Source

:gear: getOpenTicket

Return a sale ticket if created and not yet removed (payment flow)

MethodType
getOpenTicket(params: QueryParams) => Promise<Ticket or undefined>

:link: Source

:gear: newSaleTicket

Create a sale ticket (payment flow)

MethodType
newSaleTicket(params: NewSaleTicketParams) => Promise<Ticket>

:link: Source

:gear: getLifecycle

Get sale lifecycle state

MethodType
getLifecycle(params: QueryParams) => Promise<GetLifecycleResponse>

:link: Source

:gear: getFinalizationStatus

Get sale lifecycle state

MethodType
getFinalizationStatus(params: QueryParams) => Promise<GetAutoFinalizationStatusResponse>

:link: Source

:factory: SnsWrapper

Sns wrapper - notably used by NNS-dapp - ease the access to a particular Sns. It knows all the Sns' canisters, wrap and enhance their available features. A wrapper either performs query or update calls.

:link: Source

Constructors

public: Constructor to instantiate a Sns

Parameters:

  • __0
Methods
:gear: listNeurons
MethodType
listNeurons(params: Omit<SnsListNeuronsParams, "certified">) => Promise<Neuron[]>

:link: Source

:gear: listProposals
MethodType
listProposals(params: Omit<SnsListProposalsParams, "certified">) => Promise<ListProposalsResponse>

:link: Source

:gear: getProposal
MethodType
getProposal(params: Omit<SnsGetProposalParams, "certified">) => Promise<ProposalData>

:link: Source

:gear: listNervousSystemFunctions
MethodType
listNervousSystemFunctions(params: Omit<QueryParams, "certified">) => Promise<ListNervousSystemFunctionsResponse>

:link: Source

:gear: metadata
MethodType
metadata(params: Omit<QueryParams, "certified">) => Promise<[GetMetadataResponse, IcrcTokenMetadataResponse]>

:link: Source

:gear: nervousSystemParameters
MethodType
nervousSystemParameters(params: Omit<QueryParams, "certified">) => Promise<NervousSystemParameters>

:link: Source

:gear: ledgerMetadata
MethodType
ledgerMetadata(params: Omit<QueryParams, "certified">) => Promise<IcrcTokenMetadataResponse>

:link: Source

:gear: transactionFee
MethodType
transactionFee(params: Omit<QueryParams, "certified">) => Promise<bigint>

:link: Source

:gear: totalTokensSupply
MethodType
totalTokensSupply(params: Omit<QueryParams, "certified">) => Promise<bigint>

:link: Source

:gear: balance
MethodType
balance(params: Omit<BalanceParams, "certified">) => Promise<bigint>

:link: Source

:gear: transfer
MethodType
transfer(params: TransferParams) => Promise<bigint>

:link: Source

:gear: getNeuron
MethodType
getNeuron(params: Omit<SnsGetNeuronParams, "certified">) => Promise<Neuron>

:link: Source

:gear: queryNeuron
MethodType
queryNeuron(params: Omit<SnsGetNeuronParams, "certified">) => Promise<Neuron or undefined>

:link: Source

:gear: nextNeuronAccount

Returns the subaccount of the next neuron to be created.

The neuron account is a subaccount of the governance canister. The subaccount is derived from the controller and an ascending index.

‼️ The id of the neuron is the subaccount (neuron ID = subaccount) ‼️.

If the neuron does not exist for that subaccount, then we use it for the next neuron.

The index is used in the memo of the transfer and when claiming the neuron. This is how the backend can identify which neuron is being claimed.

MethodType
nextNeuronAccount(controller: Principal) => Promise<{ account: IcrcAccount; index: bigint; }>

:link: Source

:gear: stakeNeuron

Stakes a neuron.

This is a convenient method that transfers the stake to the neuron subaccount and then claims the neuron.

⚠️ This feature is provided as it without warranty. It does not implement any additional checks of the validity of the payment flow - e.g. it does not handle refund nor retries claiming the neuron in case of errors.

MethodType
stakeNeuron({ stakeE8s, source, controller, createdAt, fee, }: SnsStakeNeuronParams) => Promise<NeuronId>

:link: Source

:gear: increaseStakeNeuron

Increase the stake of a neuron.

This is a convenient method that transfers the stake to the neuron subaccount and then refresh the neuron.

⚠️ This feature is provided as it without warranty. It does not implement any additional checks of the validity of the payment flow - e.g. it does not handle refund nor calls refresh again in case of errors.

MethodType
increaseStakeNeuron({ stakeE8s, source, neuronId, }: SnsIncreaseStakeNeuronParams) => Promise<void>

:link: Source

:gear: getNeuronBalance
MethodType
getNeuronBalance(neuronId: NeuronId) => Promise<bigint>

:link: Source

:gear: addNeuronPermissions
MethodType
addNeuronPermissions(params: SnsNeuronPermissionsParams) => Promise<void>

:link: Source

:gear: refreshNeuron
MethodType
refreshNeuron(neuronId: NeuronId) => Promise<void>

:link: Source

:gear: claimNeuron
MethodType
claimNeuron(params: SnsClaimNeuronParams) => Promise<NeuronId>

:link: Source

:gear: removeNeuronPermissions
MethodType
removeNeuronPermissions(params: SnsNeuronPermissionsParams) => Promise<void>

:link: Source

:gear: splitNeuron
MethodType
splitNeuron(params: SnsSplitNeuronParams) => Promise<NeuronId or undefined>

:link: Source

:gear: disburse
MethodType
disburse(params: SnsDisburseNeuronParams) => Promise<void>

:link: Source

:gear: startDissolving
MethodType
startDissolving(neuronId: NeuronId) => Promise<void>

:link: Source

:gear: stopDissolving
MethodType
stopDissolving(neuronId: NeuronId) => Promise<void>

:link: Source

:gear: setDissolveTimestamp
MethodType
setDissolveTimestamp(params: SnsSetDissolveTimestampParams) => Promise<void>

:link: Source

:gear: increaseDissolveDelay
MethodType
increaseDissolveDelay(params: SnsIncreaseDissolveDelayParams) => Promise<void>

:link: Source

:gear: setTopicFollowees
MethodType
setTopicFollowees(params: SnsSetTopicFollowees) => Promise<void>

:link: Source

:gear: registerVote
MethodType
registerVote(params: SnsRegisterVoteParams) => Promise<void>

:link: Source

:gear: swapState
MethodType
swapState(params: Omit<QueryParams, "certified">) => Promise<GetStateResponse>

:link: Source

:gear: notifyPaymentFailure

Returns the ticket if a ticket was found for the caller and the ticket was removed successfully. Returns None if no ticket was found for the caller. Only the owner of a ticket can remove it.

Always certified

MethodType
notifyPaymentFailure() => Promise<Ticket or undefined>

:link: Source

:gear: notifyParticipation
MethodType
notifyParticipation(params: RefreshBuyerTokensRequest) => Promise<RefreshBuyerTokensResponse>

:link: Source

:gear: getUserCommitment
MethodType
getUserCommitment(params: GetBuyerStateRequest) => Promise<BuyerState or undefined>

:link: Source

:gear: getOpenTicket
MethodType
getOpenTicket(params: Omit<QueryParams, "certified">) => Promise<Ticket or undefined>

:link: Source

:gear: newSaleTicket
MethodType
newSaleTicket(params: NewSaleTicketParams) => Promise<Ticket>

:link: Source

:gear: getLifecycle
MethodType
getLifecycle(params: Omit<QueryParams, "certified">) => Promise<GetLifecycleResponse or undefined>

:link: Source

:gear: getFinalizationStatus
MethodType
getFinalizationStatus(params: Omit<QueryParams, "certified">) => Promise<GetAutoFinalizationStatusResponse or undefined>

:link: Source

:gear: getSaleParameters
MethodType
getSaleParameters(params: Omit<QueryParams, "certified">) => Promise<GetSaleParametersResponse or undefined>

:link: Source

:gear: getDerivedState
MethodType
getDerivedState(params: Omit<QueryParams, "certified">) => Promise<GetDerivedStateResponse or undefined>

:link: Source

:gear: getTransactions
MethodType
getTransactions(params: GetAccountTransactionsParams) => Promise<GetTransactions>

:link: Source

:gear: stakeMaturity
MethodType
stakeMaturity(params: SnsNeuronStakeMaturityParams) => Promise<void>

:link: Source

:gear: disburseMaturity
MethodType
disburseMaturity(params: SnsNeuronDisburseMaturityParams) => Promise<void>

:link: Source

:gear: autoStakeMaturity
MethodType
autoStakeMaturity(params: SnsNeuronAutoStakeMaturityParams) => Promise<void>

:link: Source

Keywords

FAQs

Last updated on 29 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc