Skip to content

The new styling applies starting with version 5.2.0. For earlier versions, visit legacy-sdk.humanprotocol.org.

KVStoreClient

Client for interacting with the KVStore contract.

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

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.

Example

Using Signer

Using private key (backend)

import { KVStoreClient } from '@human-protocol/sdk';
import { Wallet, JsonRpcProvider } from 'ethers';

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

const provider = new JsonRpcProvider(rpcUrl);
const signer = new Wallet(privateKey, provider);
const kvstoreClient = await KVStoreClient.build(signer);

Using Wagmi (frontend)

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

const { data: signer } = useSigner();
const kvstoreClient = await KVStoreClient.build(signer);

Using Provider

import { KVStoreClient } from '@human-protocol/sdk';
import { JsonRpcProvider } from 'ethers';

const rpcUrl = 'YOUR_RPC_URL';

const provider = new JsonRpcProvider(rpcUrl);
const kvstoreClient = await KVStoreClient.build(provider);

Extends

  • BaseEthersClient

Constructors

Constructor

new KVStoreClient(runner: ContractRunner, networkData: NetworkData): KVStoreClient;

KVStoreClient constructor

Parameters

Parameter Type Description
runner ContractRunner The Runner object to interact with the Ethereum network
networkData NetworkData The network information required to connect to the KVStore contract

Returns

Type Description
KVStoreClient -

Overrides

BaseEthersClient.constructor

Methods

build()

static build(runner: ContractRunner): Promise<KVStoreClient>;

Creates an instance of KVStoreClient from a runner.

Parameters

Parameter Type Description
runner ContractRunner The Runner object to interact with the Ethereum network

Returns

Type Description
KVStoreClient An instance of KVStoreClient

Throws

Type Description
ErrorProviderDoesNotExist If the provider does not exist for the provided Signer
ErrorUnsupportedChainID If the network's chainId is not supported
Example
import { KVStoreClient } from '@human-protocol/sdk';
import { Wallet, JsonRpcProvider } from 'ethers';

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

const provider = new JsonRpcProvider(rpcUrl);
const signer = new Wallet(privateKey, provider);
const kvstoreClient = await KVStoreClient.build(signer);

set()

set(
   key: string, 
   value: string, 
txOptions?: TransactionOverrides): Promise<void>;

This function sets a key-value pair associated with the address that submits the transaction.

Parameters

Parameter Type Description
key string Key of the key-value pair
value string Value of the key-value pair
txOptions TransactionOverrides Additional transaction parameters (optional, defaults to an empty object).

Returns

Type Description
void -

Throws

Type Description
ErrorKVStoreEmptyKey If the key is empty
Error If the transaction fails
Example
await kvstoreClient.set('Role', 'RecordingOracle');

setBulk()

setBulk(
   keys: string[], 
   values: string[], 
txOptions?: TransactionOverrides): Promise<void>;

This function sets key-value pairs in bulk associated with the address that submits the transaction.

Parameters

Parameter Type Description
keys string[] Array of keys (keys and value must have the same order)
values string[] Array of values
txOptions TransactionOverrides Additional transaction parameters (optional, defaults to an empty object).

Returns

Type Description
void -

Throws

Type Description
ErrorKVStoreArrayLength If keys and values arrays have different lengths
ErrorKVStoreEmptyKey If any key is empty
Error If the transaction fails
Example
const keys = ['role', 'webhook_url'];
const values = ['RecordingOracle', 'http://localhost'];
await kvstoreClient.setBulk(keys, values);

setFileUrlAndHash()

setFileUrlAndHash(
   url: string, 
   urlKey?: string, 
txOptions?: TransactionOverrides): Promise<void>;

Sets a URL value for the address that submits the transaction, and its hash.

Parameters

Parameter Type Default value Description
url string undefined URL to set
urlKey string 'url' Configurable URL key. url by default.
txOptions TransactionOverrides {} Additional transaction parameters (optional, defaults to an empty object).

Returns

Type Description
void -

Throws

Type Description
ErrorInvalidUrl If the URL is invalid
Error If the transaction fails
Example
await kvstoreClient.setFileUrlAndHash('example.com');
await kvstoreClient.setFileUrlAndHash('linkedin.com/example', 'linkedin_url');

get()

get(address: string, key: string): Promise<string>;

Gets the value of a key-value pair in the contract.

Parameters

Parameter Type Description
address string Address from which to get the key value.
key string Key to obtain the value.

Returns

Type Description
string Value of the key.

Throws

Type Description
ErrorKVStoreEmptyKey If the key is empty
ErrorInvalidAddress If the address is invalid
Error If the contract call fails
Example
const value = await kvstoreClient.get('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', 'Role');
console.log('Value:', value);