# @harmony-js/staking

> staking transaction package for harmony

Latest version **0.1.56** (published 2020-11-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @harmony-js/staking
pnpm add @harmony-js/staking
yarn add @harmony-js/staking
bun add @harmony-js/staking
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.56 |
| Published | 2020-11-22 |
| First published | 2019-10-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 99.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | gupadhyaya, jhwon0820, ghostcorn |

## Links

- npm: https://www.npmjs.com/package/@harmony-js/staking
- npm.io page: https://npm.io/package/@harmony-js/staking

## Dependencies (5)

- [text-encoding](https://npm.io/package/text-encoding.md) ^0.7.0
- [@harmony-js/utils](https://npm.io/package/@harmony-js/utils.md) 0.1.56
- [@harmony-js/crypto](https://npm.io/package/@harmony-js/crypto.md) 0.1.56
- [@harmony-js/network](https://npm.io/package/@harmony-js/network.md) 0.1.56
- [@harmony-js/transaction](https://npm.io/package/@harmony-js/transaction.md) 0.1.56

## Recent versions

- 0.1.56 (latest) — 2020-11-22
- 0.1.58 (next) — 2022-07-06
- 0.1.41-alpha.1 (canary) — 2020-05-04
- 0.1.55 — 2020-09-29
- 0.1.54 — 2020-09-07
- 0.1.51 — 2020-08-06
- 0.1.48 — 2020-07-13
- 0.1.45 — 2020-06-06
- 0.1.43 — 2020-05-04
- 0.1.36 — 2019-11-27
- 0.1.35 — 2019-11-27
- 0.1.34 — 2019-11-18
- 0.1.32 — 2019-10-31
- 0.1.31 — 2019-10-25

## README

# @harmony-js/staking

This package provides a collection of apis to create, sign/send staking transaction, and receive confirm/receipt.

## Installation

```
npm install @harmony-js/staking
```

## Usage

Create a Harmony instance connecting to testnet

```javascript
const { Harmony } = require('@harmony-js/core');
const {
  ChainID,
  ChainType,
  hexToNumber,
  numberToHex,
  fromWei,
  Units,
  Unit,
} = require('@harmony-js/utils');

const hmy = new Harmony(
    'https://api.s0.b.hmny.io/',
    {
        chainType: ChainType.Harmony,
        chainId: ChainID.HmyTestnet,
    },
);
```
Below, examples show how to send delegate, undelegate, and collect rewards staking transactions. First, set the chainId, gasLimit, gasPrice for all subsequent staking transactions
```javascript
hmy.stakings.setTxParams({
  gasLimit: 25000,
  gasPrice: numberToHex(new hmy.utils.Unit('1').asGwei().toWei()),
  chainId: 2
});
```
<span style="color:red">Note: create and edit validator transactions are not fully supported in the sdk</span>

Create delegate staking transaction
```javascript
const delegate = hmy.stakings.delegate({
  delegatorAddress: 'one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7',
  validatorAddress: 'one1vfqqagdzz352mtvdl69v0hw953hm993n6v26yl',
  amount: numberToHex(new Unit(1000).asOne().toWei())
});
const delegateStakingTx = delegate.build();
```

Sign and send the delegate transaction and receive confirmation
```javascript
// key corresponds to one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7, only has testnet balance
hmy.wallet.addByPrivateKey('45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e');

hmy.wallet.signStaking(delegateStakingTx).then(signedTxn => {
  signedTxn.sendTransaction().then(([tx, hash]) => {
    console.log(hash);
    signedTxn.confirm(hash).then(response => {
      console.log(response.receipt);
    });
  });
});
```

Similarily, undelegate and collect reward transactions can be composed, signed and sent
Create undelegate staking transaction
```javascript
const undelegate = hmy.stakings.undelegate({
  delegatorAddress: 'one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7',
  validatorAddress: 'one1vfqqagdzz352mtvdl69v0hw953hm993n6v26yl',
  amount: numberToHex(new Unit(1000).asOne().toWei())
});
const undelegateStakingTx = undelegate.build();
```

Create collect rewards staking transaction
```javascript
const collectRewards = hmy.stakings.collectRewards({
  delegatorAddress: 'one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7'
});
const collectRewardsStakingTx = collectRewards.build();
```

Also, similar to normal transaction, signing and sending can be performed asynchronously.

---
_Source: https://npm.io/package/@harmony-js/staking · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
