0.0.2 • Published 3 years ago

@garizonchain/staking v0.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

@garizonchain/staking

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

Installation

npm install @garizonchain/staking

Usage

Create a Garizon instance connecting to testnet

const { Garizon } = require('@garizonchain/core');
const {
  ChainID,
  ChainType,
  hexToNumber,
  numberToHex,
  fromWei,
  Units,
  Unit,
} = require('@garizonchain/utils');

const gar = new Garizon(
    'https://api.s0.b.hmny.io/',
    {
        chainType: ChainType.Garizon,
        chainId: ChainID.GarTestnet,
    },
);

Below, examples show how to send delegate, undelegate, and collect rewards staking transactions. First, set the chainId, gasLimit, gasPrice for all subsequent staking transactions

gar.stakings.setTxParams({
  gasLimit: 25000,
  gasPrice: numberToHex(new gar.utils.Unit('1').asGwei().toWei()),
  chainId: 2
});

Note: create and edit validator transactions are not fully supported in the sdk

Create delegate staking transaction

const delegate = gar.stakings.delegate({
  delegatorAddress: 'gar103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7',
  validatorAddress: 'gar1vfqqagdzz352mtvdl69v0hw953hm993n6v26yl',
  amount: numberToHex(new Unit(1000).asGar().toWei())
});
const delegateStakingTx = delegate.build();

Sign and send the delegate transaction and receive confirmation

// key corresponds to gar103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7, only has testnet balance
gar.wallet.addByPrivateKey('45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e');

gar.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

const undelegate = gar.stakings.undelegate({
  delegatorAddress: 'gar103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7',
  validatorAddress: 'gar1vfqqagdzz352mtvdl69v0hw953hm993n6v26yl',
  amount: numberToHex(new Unit(1000).asGar().toWei())
});
const undelegateStakingTx = undelegate.build();

Create collect rewards staking transaction

const collectRewards = gar.stakings.collectRewards({
  delegatorAddress: 'gar103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7'
});
const collectRewardsStakingTx = collectRewards.build();

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