Git Product home page Git Product logo

codechain-stakeholder-sdk-js's Introduction

CodeChain Stakeholder SDK

A JavaScript implementation for CodeChain stake token related custom actions and custom transactions

Features

It adds the following features to CodeChain SDK for JavaScript:

  • Query staking states
  • Call staking related RPC
  • Create staking transactions

API Examples

You first need to install the package.

# npm
npm install codechain-stakeholder-sdk

# yarn
yarn add codechain-stakeholder-sdk

Then prepare SDK instance as usual.

import { SDK } from "codechain-sdk";
const sdk = new SDK({
  server: "http://localhost:8080",
  networkId: "tc"
});

Now, you are prepared to use stakeholder-sdk-js

Query staking states

These functions can have an optional block number parameter at the end.

Get the list of stakeholders

import { getCCSHolders } from "codechain-stakeholder-sdk";

const holders = await getCCSHolders(sdk);
// holders: PlatformAddress[]

Get the quantity of undelegated stake token of a stakeholder

import { getUndelegatedCCS } from "codechain-stakeholder-sdk";

const balance = await getUndelegatedCCS(
  sdk,
  "tccq9h7vnl68frvqapzv3tujrxtxtwqdnxw6yamrrgd"
);
// balance: U64

Get the list of delegations that a stakeholder delegated to delegatees

import { getDelegations } from "codechain-stakeholder-sdk";

const delegations = await getDelegations(
  sdk,
  "tccq9h7vnl68frvqapzv3tujrxtxtwqdnxw6yamrrgd"
);
for (const { delegatee, quantity } of delegations) {
  // delegatee: PlatformAddress
  // quantity: U64
}

Get the list of validator candidates

import { getCandidates } from "codechain-stakeholder-sdk";

const candidates = await getCandidates(sdk);
for (const { pubkey, deposit, nominationEndsAt, metadata } of candidates) {
  // pubkey: H512
  // deposit: U64
  // nominationEndsAt: U64
  // metadata: Buffer
}

Get the list of jailed accounts

import { getJailed } from "codechain-stakeholder-sdk";

const prisoners = await getJailed(sdk);
for (const { address, deposit, custodyUntil, releasedAt } of prisoners) {
  // address: PlatformAddress
  // deposit: U64
  // custodyUntil: U64
  // releasedAt: U64
}

Get the list of banned accounts

import { getBanned } from "codechain-stakeholder-sdk";

const banned = await getBanned(sdk);
// banned: PlatformAddress[]

Get intermediate rewards

import { getIntermediateRewards } from "codechain-stakeholder-sdk";

const { previous, current } = await getIntermediateRewards(sdk);
// previous, current: { address: PlatformAddress, quantity: U64 }[]

Get the list of current validators

import { getValidators } from "codechain-stakeholder-sdk";

const validators = await getValidators(sdk);
for (const { weight, delegation, deposit, pubkey } of validators) {
  // weight: U64
  // delegation: U64
  // deposit: U64
  // pubkey: H512
}

RPCs to query staking status

TermMetadata

import { getTermMetadata } from "codechain-stakeholder-sdk";

const { lastTermFinishedBlockNumber, currentTermId } = await getTermMetadata(
  sdk
);

Create staking transactions

Transfer stake tokens

import { createTransferCCSTransaction } from "codechain-stakeholder-sdk";

// Transfer 100 tokens to tccq94guhkrfndnehnca06dlkxcfuq0gdlamvw9ga4f
const tx = createTransferCCSTransaction(
  sdk,
  "tccq94guhkrfndnehnca06dlkxcfuq0gdlamvw9ga4f",
  100
)
const signedTx = .sign({ secret: "...", seq: "...", fee: "..." });
const txhash = await sdk.rpc.chain.sendSignedTransaction(signedTx);

Delegate stake tokens

import { createDelegateCCSTransaction } from "codechain-stakeholder-sdk";

// Delegate 100 tokens to tccq94guhkrfndnehnca06dlkxcfuq0gdlamvw9ga4f
const tx = createDelegateCCSTransaction(
  sdk,
  "tccq94guhkrfndnehnca06dlkxcfuq0gdlamvw9ga4f",
  100
);
const signedTx = tx.sign({ secret: "...", seq: "...", fee: "..." });
const txhash = await sdk.rpc.chain.sendSignedTransaction(signedTx);

Revoke stake tokens

import { createRevokeTransaction } from "codechain-stakeholder-sdk";

// Revoke 100 tokens delegated to tccq94guhkrfndnehnca06dlkxcfuq0gdlamvw9ga4f
const tx = createRevokeTransaction(
  sdk,
  "tccq94guhkrfndnehnca06dlkxcfuq0gdlamvw9ga4f",
  100
);
const signedTx = tx.sign({ secret: "...", seq: "...", fee: "..." });
const txhash = await sdk.rpc.chain.sendSignedTransaction(signedTx);

Redelegate stake tokens

import { createRedelegateTransaction } from "codechain-stakeholder-sdk";

// Redelegate 100 tokens to "tccq9qvruafmf9vegjhkl0ruunkwp0d4lc8fgxknzh5"
// which was delegated to "tccq94guhkrfndnehnca06dlkxcfuq0gdlamvw9ga4f".
const tx = createRedelegateTransaction(
  sdk,
  "tccq94guhkrfndnehnca06dlkxcfuq0gdlamvw9ga4f",
  "tccq9qvruafmf9vegjhkl0ruunkwp0d4lc8fgxknzh5",
  100
);
const signedTx = tx.sign({ secret: "...", seq: "...", fee: "..." });
const txhash = await sdk.rpc.chain.sendSignedTransaction(signedTx);

Self-nominate

import { createSelfNominateTransaction } from "codechain-stakeholder-sdk";

// Self-nominate with 1000 CCC and metadata
const tx = createSelfNominateTransaction(sdk, 1000, "some-metadata");
const signedTx = tx.sign({ secret: "...", seq: "...", fee: "..." });
const txhash = await sdk.rpc.chain.sendSignedTransaction(signedTx);

Report double vote

import { createReportDoubleVoteTransaction } from "codechain-stakeholder-sdk";

// Report double vote message
const tx = createReportDoubleVoteTransaction(sdk, message1, message2);
const signedTx = tx.sign({ secret: "...", seq: "...", fee: "..." });
const txhash = await sdk.rpc.chain.sendSignedTransaction(signedTx);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.