Human Protocol SDK
v4.0.0
v4.0.0
  • Typescript SDK
    • Encryption
      • Encryption
      • EncryptionUtils
    • Escrow
      • EscrowClient
      • EscrowUtils
    • KVStore
      • KVStoreClient
      • KVStoreUtils
    • Staking
      • StakingClient
    • Operator
      • OperatorUtils
    • Storage
      • StorageClient
    • Statistics
      • StatisticsClient
    • Transaction
      • TransactionUtils
  • Python SDK
    • agreement
      • bootstrap
      • measures
      • utils
    • encryption
      • encryption
      • legacy_encryption
      • encryption_utils
    • escrow
      • escrow_client
      • escrow_utils
    • kvstore
      • kvstore_client
      • kvstore_utils
    • staking
      • staking_client
      • staking_utils
    • operator
      • operator_utils
    • statistics
      • statistics_client
    • storage
      • storage_client
      • storage_utils
    • transaction
      • transaction_utils
    • constants
    • filter
    • utils
  • CHANGELOG
Powered by GitBook
On this page
  • Class: StakingClient
  • Introduction
  • Installation
  • Code example
  • Extends
  • Constructors
  • Properties
  • Methods
  1. Typescript SDK
  2. Staking

StakingClient

Last updated 3 months ago


/ / StakingClient

Class: StakingClient

Introduction

This client enables performing actions on staking contracts and obtaining staking information from both the contracts and subgraph.

Internally, the SDK will use one network or another according to the network ID of the runner. To use this client, it is recommended to initialize it using the static build method.

static async build(runner: ContractRunner): Promise<StakingClient>;

A Signer or a Provider should be passed depending on the use case of this module:

  • Signer: when the user wants to use this model to send transactions calling the contract functions.

  • Provider: when the user wants to use this model to get information from the contracts or subgraph.

Installation

npm

npm install @human-protocol/sdk

yarn

yarn install @human-protocol/sdk

Code example

Signer

Using private key (backend)

import { StakingClient } from '@human-protocol/sdk';
import { Wallet, providers } from 'ethers';

const rpcUrl = 'YOUR_RPC_URL';
const privateKey = 'YOUR_PRIVATE_KEY';

const provider = new providers.JsonRpcProvider(rpcUrl);
const signer = new Wallet(privateKey, provider);
const stakingClient = await StakingClient.build(signer);

Using Wagmi (frontend)

import { useSigner, useChainId } from 'wagmi';
import { StakingClient } from '@human-protocol/sdk';

const { data: signer } = useSigner();
const stakingClient = await StakingClient.build(signer);

Provider

import { StakingClient } from '@human-protocol/sdk';
import { providers } from 'ethers';

const rpcUrl = 'YOUR_RPC_URL';

const provider = new providers.JsonRpcProvider(rpcUrl);
const stakingClient = await StakingClient.build(provider);

Extends

Constructors

new StakingClient()

StakingClient constructor

Parameters

runner

ContractRunner

The Runner object to interact with the Ethereum network

networkData

The network information required to connect to the Staking contract

Returns

Overrides

Defined in

Properties

escrowFactoryContract

escrowFactoryContract: EscrowFactory

Defined in


networkData

Inherited from

Defined in


runner

protected runner: ContractRunner

Inherited from

Defined in


stakingContract

stakingContract: Staking

Defined in


tokenContract

tokenContract: HMToken

Defined in

Methods

approveStake()

approveStake(amount, txOptions?): Promise<void>

This function approves the staking contract to transfer a specified amount of tokens when the user stakes. It increases the allowance for the staking contract.

Parameters

amount

bigint

Amount in WEI of tokens to approve for stake.

txOptions?

Overrides = {}

Additional transaction parameters (optional, defaults to an empty object).

Returns

Promise<void>

Returns void if successful. Throws error if any.

Code example

import { ethers, Wallet, providers } from 'ethers';
import { StakingClient } from '@human-protocol/sdk';

const rpcUrl = 'YOUR_RPC_URL';
const privateKey = 'YOUR_PRIVATE_KEY';

const provider = new providers.JsonRpcProvider(rpcUrl);
const signer = new Wallet(privateKey, provider);
const stakingClient = await StakingClient.build(signer);

const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
await stakingClient.approveStake(amount);

Defined in


getStakerInfo()

Retrieves comprehensive staking information for a staker.

Parameters

stakerAddress

string

The address of the staker.

Returns

Code example

import { StakingClient } from '@human-protocol/sdk';

const rpcUrl = 'YOUR_RPC_URL';

const provider = new providers.JsonRpcProvider(rpcUrl);
const stakingClient = await StakingClient.build(provider);

const stakingInfo = await stakingClient.getStakerInfo('0xYourStakerAddress');
console.log(stakingInfo.tokensStaked);

Defined in


slash()

slash(slasher, staker, escrowAddress, amount, txOptions?): Promise<void>

This function reduces the allocated amount by a staker in an escrow and transfers those tokens to the reward pool. This allows the slasher to claim them later.

Parameters

slasher

string

Wallet address from who requested the slash

staker

string

Wallet address from who is going to be slashed

escrowAddress

string

Address of the escrow that the slash is made

amount

bigint

Amount in WEI of tokens to slash.

txOptions?

Overrides = {}

Additional transaction parameters (optional, defaults to an empty object).

Returns

Promise<void>

Returns void if successful. Throws error if any.

Code example

import { ethers, Wallet, providers } from 'ethers';
import { StakingClient } from '@human-protocol/sdk';

const rpcUrl = 'YOUR_RPC_URL';
const privateKey = 'YOUR_PRIVATE_KEY';

const provider = new providers.JsonRpcProvider(rpcUrl);
const signer = new Wallet(privateKey, provider);
const stakingClient = await StakingClient.build(signer);

const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
await stakingClient.slash('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);

Defined in


stake()

stake(amount, txOptions?): Promise<void>

This function stakes a specified amount of tokens on a specific network.

approveStake must be called before

Parameters

amount

bigint

Amount in WEI of tokens to stake.

txOptions?

Overrides = {}

Additional transaction parameters (optional, defaults to an empty object).

Returns

Promise<void>

Returns void if successful. Throws error if any.

Code example

import { ethers, Wallet, providers } from 'ethers';
import { StakingClient } from '@human-protocol/sdk';

const rpcUrl = 'YOUR_RPC_URL';
const privateKey = 'YOUR_PRIVATE_KEY';

const provider = new providers.JsonRpcProvider(rpcUrl);
const signer = new Wallet(privateKey, provider);
const stakingClient = await StakingClient.build(signer);

const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
await stakingClient.approveStake(amount); // if it was already approved before, this is not necessary
await stakingClient.stake(amount);

Defined in


unstake()

unstake(amount, txOptions?): Promise<void>

This function unstakes tokens from staking contract. The unstaked tokens stay locked for a period of time.

Must have tokens available to unstake

Parameters

amount

bigint

Amount in WEI of tokens to unstake.

txOptions?

Overrides = {}

Additional transaction parameters (optional, defaults to an empty object).

Returns

Promise<void>

Returns void if successful. Throws error if any.

Code example

import { ethers, Wallet, providers } from 'ethers';
import { StakingClient } from '@human-protocol/sdk';

const rpcUrl = 'YOUR_RPC_URL';
const privateKey = 'YOUR_PRIVATE_KEY';

const provider = new providers.JsonRpcProvider(rpcUrl);
const signer = new Wallet(privateKey, provider);
const stakingClient = await StakingClient.build(signer);

const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
await stakingClient.unstake(amount);

Defined in


withdraw()

withdraw(txOptions?): Promise<void>

This function withdraws unstaked and non-locked tokens from staking contract to the user wallet.

Must have tokens available to withdraw

Parameters

txOptions?

Overrides = {}

Additional transaction parameters (optional, defaults to an empty object).

Returns

Promise<void>

Returns void if successful. Throws error if any.

Code example

import { Wallet, providers } from 'ethers';
import { StakingClient } from '@human-protocol/sdk';

const rpcUrl = 'YOUR_RPC_URL';
const privateKey = 'YOUR_PRIVATE_KEY';

const provider = new providers.JsonRpcProvider(rpcUrl);
const signer = new Wallet(privateKey, provider);
const stakingClient = await StakingClient.build(signer);

await stakingClient.withdraw();

Defined in


build()

Creates an instance of StakingClient from a Runner.

Parameters

runner

ContractRunner

The Runner object to interact with the Ethereum network

Returns

  • An instance of StakingClient

Throws

  • Thrown if the provider does not exist for the provided Signer

Throws

  • Thrown if the network's chainId is not supported

Defined in

new StakingClient(runner, networkData):

.

networkData:

.

.

getStakerInfo(stakerAddress): Promise<>

Promise<>

static build(runner): Promise<>

Promise<>

@human-protocol/sdk
@human-protocol/sdk
staking
BaseEthersClient
StakingClient
NetworkData
StakingClient
BaseEthersClient
constructor
staking.ts:108
staking.ts:100
NetworkData
BaseEthersClient
networkData
base.ts:12
BaseEthersClient
runner
base.ts:11
staking.ts:99
staking.ts:98
staking.ts:193
StakerInfo
StakerInfo
staking.ts:435
staking.ts:373
staking.ts:247
staking.ts:291
staking.ts:336
StakingClient
StakingClient
staking.ts:136