StakingClient

@human-protocol/sdkDocs


@human-protocol/sdk / staking / StakingClient

Class: 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 runner. To use this client, it is recommended to initialize it using the static build method.

static async build(runner: ContractRunner);

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

Extends

Constructors

new StakingClient()

new StakingClient(runner, networkData): StakingClient

StakingClient constructor

Parameters

runner: ContractRunner

The Runner object to interact with the Ethereum network

networkData: NetworkData

Returns

StakingClient

Overrides

BaseEthersClient.constructor

Source

staking.ts:111

Properties

escrowFactoryContract

escrowFactoryContract: EscrowFactory

Source

staking.ts:102


networkData

networkData: NetworkData

Inherited from

BaseEthersClient.networkData

Source

base.ts:12


rewardPoolContract

rewardPoolContract: RewardPool

Source

staking.ts:103


runner

protected runner: ContractRunner

Inherited from

BaseEthersClient.runner

Source

base.ts:11


stakingContract

stakingContract: Staking

Source

staking.ts:101


tokenContract

tokenContract: HMToken

Source

staking.ts:100

Methods

allocate()

allocate(escrowAddress, amount, txOptions?): Promise<void>

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

Must have tokens staked

Parameters

escrowAddress: string

Address of the escrow contract to allocate in.

amount: bigint

Amount in WEI of tokens to allocate.

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.allocate('0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);

Source

staking.ts:458


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

Source

staking.ts:203


checkValidEscrow()

private checkValidEscrow(escrowAddress): Promise<void>

Check if escrow exists

Parameters

escrowAddress: string

Escrow address to check against

Returns

Promise<void>

Source

staking.ts:167


closeAllocation()

closeAllocation(escrowAddress, txOptions?): Promise<void>

This function drops the allocation from a specific escrow.

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

Parameters

escrowAddress: string

Address of the escrow contract to close allocation from.

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

Source

staking.ts:511


distributeReward()

distributeReward(escrowAddress, txOptions?): Promise<void>

This function drops the allocation from a specific escrow.

The escrow must have rewards added

Parameters

escrowAddress: string

Escrow address from which rewards are distributed.

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

Source

staking.ts:554


getAllocation()

getAllocation(escrowAddress): Promise<IAllocation>

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

Parameters

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

Source

staking.ts:591


slash()

slash(slasher, staker, escrowAddress, amount, txOptions?): 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

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: 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.slash('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);

Source

staking.ts:387


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.approveStake(amount);

Source

staking.ts:258


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

Source

staking.ts:303


withdraw()

withdraw(txOptions?): Promise<void>

This function withdraws unstaked and non locked tokens form 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();

Source

staking.ts:349


build()

static build(runner): Promise<StakingClient>

Creates an instance of StakingClient from a Runner.

Parameters

runner: ContractRunner

The Runner object to interact with the Ethereum network

Returns

Promise<StakingClient>

  • 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

Source

staking.ts:145

Last updated