Human Protocol SDK
v1.1.18
v1.1.18
  • Typescript SDK​
    • Encryption
      • Encryption
      • EncryptionUtils
    • Escrow
      • EscrowClient
      • EscrowUtils
    • KVStore
      • KVStoreClient
    • Staking
      • StakingClient
    • Storage
      • StorageClient
    • Statistics
      • StatisticsClient
  • Python SDK
    • agreement
      • bootstrap
      • measures
      • utils
    • encryption
      • encryption
      • legacy_encryption
      • encryption_utils
    • escrow
      • escrow_client
      • escrow_utils
    • kvstore
      • kvstore_client
    • staking
      • staking_client
      • staking_utils
    • statistics
      • statistics_client
    • storage
      • storage_client
      • storage_utils
    • constants
    • filter
    • utils
Powered by GitBook
On this page
  • Class: StakingClient
  • Introduction
  • Installation
  • Code example
  • Hierarchy
  • Table of contents
  • Constructors
  • Properties
  • Methods
  1. Typescript SDK​
  2. Staking

StakingClient

Last updated 1 year ago

/ / / StakingClient

Class: StakingClient

.StakingClient

Introduction

This client enables to perform actions on staking contracts and obtain staking information from both the contracts and subgraph.

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

static async build(signerOrProvider: Signer | Provider);

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 in order to send transactions caling the contract functions.

  • Provider: when the user wants to use this model in order 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);

Hierarchy

  • ↳ StakingClient

Table of contents

Constructors

Properties

Methods

Constructors

constructor

• new StakingClient(signerOrProvider, networkData, gasPriceMultiplier?)

StakingClient constructor

Parameters

Name
Type
Description

signerOrProvider

Signer | Provider

The Signer or Provider object to interact with the Ethereum network

networkData

NetworkData

-

gasPriceMultiplier?

number

The multiplier to apply to the gas price

Overrides

Defined in

Properties

escrowFactoryContract

• escrowFactoryContract: EscrowFactory

Defined in


gasPriceMultiplier

• Protected Optional gasPriceMultiplier: number

Inherited from

Defined in


networkData

• networkData: NetworkData

Inherited from

Defined in


rewardPoolContract

• rewardPoolContract: RewardPool

Defined in


signerOrProvider

• Protected signerOrProvider: Signer | Provider

Inherited from

Defined in


stakingContract

• stakingContract: Staking

Defined in


tokenContract

• tokenContract: HMToken

Defined in

Methods

allocate

▸ allocate(escrowAddress, amount): Promise<void>

This function allocates a portion of the staked tokens to a specific escrow.

Must have tokens staked

Parameters

Name
Type
Description

escrowAddress

string

Address of the escrow contract to allocate in.

amount

BigNumber

Amount in WEI of tokens to allocate.

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.utils.parseUnits(5, 'ether'); //convert from ETH to WEI
await stakingClient.allocate('0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);

Defined in


approveStake

▸ approveStake(amount): 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

Name
Type
Description

amount

BigNumber

Amount in WEI of tokens to approve for stake.

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.utils.parseUnits(5, 'ether'); //convert from ETH to WEI
await stakingClient.approveStake(amount);

Defined in


checkValidEscrow

▸ Private checkValidEscrow(escrowAddress): Promise<void>

Check if escrow exists

Parameters

Name
Type
Description

escrowAddress

string

Escrow address to check against

Returns

Promise<void>

Defined in


closeAllocation

▸ closeAllocation(escrowAddress): Promise<void>

This function drops the allocation from a specific escrow.

The escrow must have allocation The escrow must be cancelled or completed.

Parameters

Name
Type
Description

escrowAddress

string

Address of the escrow contract to close allocation from.

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.closeAllocation('0x62dD51230A30401C455c8398d06F85e4EaB6309f');

Defined in


distributeReward

▸ distributeReward(escrowAddress): Promise<void>

This function drops the allocation from a specific escrow.

The escrow must have rewards added

Parameters

Name
Type
Description

escrowAddress

string

Escrow address from which rewards are distributed.

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.distributeReward('0x62dD51230A30401C455c8398d06F85e4EaB6309f');

Defined in


gasPriceOptions

▸ Protected gasPriceOptions(): Promise<Partial<Overrides>>

Adjust the gas price, and return as an option to be passed to a transaction

Returns

Promise<Partial<Overrides>>

Returns the gas price options

Inherited from

Defined in


getAllocation

▸ getAllocation(escrowAddress): Promise<IAllocation>

This function returns information about the allocation of the specified escrow.

Parameters

Name
Type
Description

escrowAddress

string

Escrow address from which we want to get allocation information.

Returns

Promise<IAllocation>

Returns allocation info if exists.

Code example

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);

const allocationInfo = await stakingClient.getAllocation('0x62dD51230A30401C455c8398d06F85e4EaB6309f');

Defined in


getLeader

▸ getLeader(address): Promise<ILeader>

This function returns all the leader details of the protocol.

Parameters

Name
Type

address

string

Returns

Promise<ILeader>

Returns an array with all the leader details.

Code example

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);

const leaders = await stakingClient.getLeaders();

Defined in


getLeaders

▸ getLeaders(filter?): Promise<ILeader[]>

This function returns the leader data for the given address.

Parameters

Name
Type

filter

ILeadersFilter

Returns

Promise<ILeader[]>

Returns the leader details.

Code example

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);

const leader = await stakingClient.getLeader('0x62dD51230A30401C455c8398d06F85e4EaB6309f');

Defined in


getRewards

▸ getRewards(slasherAddress): Promise<IReward[]>

This function returns information about the rewards for a given slasher address.

Parameters

Name
Type
Description

slasherAddress

string

Slasher address.

Returns

Promise<IReward[]>

Returns an array of Reward objects that contain the rewards earned by the user through slashing other users.

Code example

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);

const rewards = await stakingClient.getRewards('0x62dD51230A30401C455c8398d06F85e4EaB6309f');

Defined in


slash

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

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

Parameters

Name
Type
Description

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 which allocation will be slashed

amount

BigNumber

Amount in WEI of tokens to unstake.

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.utils.parseUnits(5, 'ether'); //convert from ETH to WEI
await stakingClient.slash('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);

Defined in


stake

▸ stake(amount): Promise<void>

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

approveStake must be called before

Parameters

Name
Type
Description

amount

BigNumber

Amount in WEI of tokens to stake.

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.utils.parseUnits(5, 'ether'); //convert from ETH to WEI
await stakingClient.approveStake(amount); // if it was already approved before, this is not necessary
await stakingClient.approveStake(amount);

Defined in


unstake

▸ unstake(amount): 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

Name
Type
Description

amount

BigNumber

Amount in WEI of tokens to unstake.

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.utils.parseUnits(5, 'ether'); //convert from ETH to WEI
await stakingClient.unstake(amount);

Defined in


withdraw

▸ withdraw(): Promise<void>

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

Must have tokens available to withdraw

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 Signer or Provider.

Parameters

Name
Type
Description

signerOrProvider

Signer | Provider

The Signer or Provider object to interact with the Ethereum network

gasPriceMultiplier?

number

The multiplier to apply to the gas price

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

.

.

.

.

.

▸ Static build(signerOrProvider, gasPriceMultiplier?): Promise<>

Promise<>

@human-protocol/sdk
Modules
staking
staking
BaseEthersClient
BaseEthersClient
constructor
staking.ts:119
staking.ts:109
BaseEthersClient
gasPriceMultiplier
base.ts:14
BaseEthersClient
networkData
base.ts:15
staking.ts:110
BaseEthersClient
signerOrProvider
base.ts:13
staking.ts:108
staking.ts:107
staking.ts:461
staking.ts:222
staking.ts:187
staking.ts:512
staking.ts:551
BaseEthersClient
gasPriceOptions
base.ts:39
staking.ts:659
staking.ts:585
staking.ts:624
staking.ts:691
staking.ts:398
staking.ts:269
staking.ts:315
staking.ts:359
StakingClient
StakingClient
staking.ts:157
constructor
escrowFactoryContract
gasPriceMultiplier
networkData
rewardPoolContract
signerOrProvider
stakingContract
tokenContract
allocate
approveStake
checkValidEscrow
closeAllocation
distributeReward
gasPriceOptions
getAllocation
getLeader
getLeaders
getRewards
slash
stake
unstake
withdraw
build