npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

harmony-staking-sdk

v1.0.6

Published

Harmony Staking SDK

Downloads

13

Readme

Harmony Staking SDK

Harmony Staking SDK is a library that allows you to interact with the Harmony blockchain and delegate tokens to validators, undelegate them, collect staking rewards, and send tokens to other users.

Installation

To install the Harmony Staking SDK, use the following command:

Copy code

npm install harmony-staking-sdk

Usage

Staking Contract

The StakingContract class allows you to delegate tokens to validators, undelegate them, collect staking rewards, and send tokens to other users. To use this class, you need to provide the following parameters:

  • rpcUrl: The RPC endpoint for the Harmony network you want to interact with.
  • privateKey: The private key of the account that you want to use to perform the operations. If you don't provide a private key, the SDK will try to use the Metamask wallet if it's installed in your browser.
  • contractAddress: The address of the Harmony staking contract you want to interact with.
  • gasPrice, gasPriceRewards, gasLimit: The gas price and gas limit to use when sending transactions to the Harmony network.

Example:

import { StakingContract } from 'harmony-staking-sdk';

const stakingContract = new StakingContract({
  // Set the RPC endpoint for the Harmony network
  rpcUrl: 'https://api.harmony.one',
  // Set your private key for signing transactions
  privateKey: 'YOUR_PRIVATE_KEY',
});

// Delegate some amount of ONE tokens to a validator
await stakingContract.delegate(
  'VALIDATOR_ADDRESS', 
  'AMOUNT_IN_ETHER', 
  tx => { 
    console.log(`Transaction hash: ${tx}`);
  }
);

// Undelegate some amount of ONE tokens from a validator
await stakingContract.unDelegate(
  'VALIDATOR_ADDRESS', 
  'AMOUNT_IN_ETHER', 
  tx => { 
    console.log(`Transaction hash: ${tx}`);
  }
);

// Collect rewards from all validators where the account has staked
await stakingContract.collectRewards(tx => { 
  console.log(`Transaction hash: ${tx}`);
});

// Send some amount of ONE tokens to a receiver address
await stakingContract.send(
  'RECEIVER_ADDRESS', 
  'AMOUNT_IN_ETHER', 
  tx => { 
    console.log(`Transaction hash: ${tx}`);
  }
);

Staking API

The StakingAPI class allows you to fetch information about validators, delegations, network info, and more. To use this class, you need to provide the following parameters:

  • apiUrl: The API endpoint for the Harmony staking API.

Example:

const { StakingAPI, NETWORK_TYPE } = require('harmony-staking-sdk');

const stakingApi = new StakingAPI({ apiUrl: 'https://api.stake.hmny.io' });

// Fetch list of available networks
stakingApi.fetchNetworks()
  .then((networks) => {
    console.log('Available networks:', networks);
  })
  .catch((err) => {
    console.error('Error fetching networks:', err);
  });

// Fetch list of validators for the mainnet
stakingApi.fetchValidators(NETWORK_TYPE.MAINNET)
  .then((validators) => {
    console.log('Validators:', validators);
  })
  .catch((err) => {
    console.error('Error fetching validators:', err);
  });

// Fetch information about a specific validator
const validatorAddress = 'one1w7nvheulzwprf9d9a3r8sqtv5q47qlqx7kured';

stakingApi.fetchValidatorByAddress(NETWORK_TYPE.MAINNET, validatorAddress)
  .then((validator) => {
    console.log('Validator information:', validator);
  })
  .catch((err) => {
    console.error(`Error fetching validator information for address ${validatorAddress}:`, err);
  });

// Fetch the avatar URL for a specific validator
const avatarUrl = stakingApi.getValidatorAvatarUrl(NETWORK_TYPE.MAINNET, validatorAddress);

console.log('Avatar URL:', avatarUrl);

// Fetch historical data for a specific validator
stakingApi.fetchValidatorHistory(NETWORK_TYPE.MAINNET, validatorAddress)
  .then((history) => {
    console.log('Validator history:', history);
  })
  .catch((err) => {
    console.error(`Error fetching validator history for address ${validatorAddress}:`, err);
  });

// Fetch the list of delegations for a specific address
const delegatorAddress = 'one13hnlv6vmyj6umjtf6kafwy6w3vq3ynzxs8tuzy';

stakingApi.fetchDelegationsByAddress(NETWORK_TYPE.MAINNET, delegatorAddress)
  .then((delegations) => {
    console.log('Delegations:', delegations);
  })
  .catch((err) => {
    console.error(`Error fetching delegations for address ${delegatorAddress}:`, err);
  });

// Fetch network information for the mainnet
stakingApi.fetchNetworkInfo(NETWORK_TYPE.MAINNET)
  .then((info) => {
    console.log('Network information:', info);
  })
  .catch((err) => {
    console.error('Error fetching network information:', err);
  });

Contributing

We welcome contributions to the Harmony Staking SDK! If you'd like to contribute, please submit a pull request to the develop branch. Make sure to include tests for your code, and to run the existing tests using npm test before submitting your pull request.

License

Harmony Staking SDK is MIT licensed.