-
Notifications
You must be signed in to change notification settings - Fork 29
/
kvstore.ts
253 lines (241 loc) · 7.7 KB
/
kvstore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { Provider } from '@ethersproject/abstract-provider';
import { Network } from '@ethersproject/networks';
import {
KVStore,
KVStore__factory,
} from '@human-protocol/core/typechain-types';
import { Signer, ethers } from 'ethers';
import { NETWORKS } from './constants';
import { requiresSigner } from './decorators';
import { ChainId } from './enums';
import {
ErrorInvalidAddress,
ErrorKVStoreArrayLength,
ErrorKVStoreEmptyKey,
ErrorProviderDoesNotExist,
ErrorSigner,
ErrorUnsupportedChainID,
} from './error';
import { NetworkData } from './types';
/**
* ## Introduction
*
* This client enables to perform actions on KVStore contract and obtain 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.
*
* ```ts
* 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
* ```bash
* npm install @human-protocol/sdk
* ```
*
* ### yarn
* ```bash
* yarn install @human-protocol/sdk
* ```
*
* ## Code example
*
* ### Signer
*
* **Using private key(backend)**
*
* ```ts
* import { KVStoreClient } 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 kvstoreClient = await KVStoreClient.build(signer);
* ```
*
* **Using Wagmi(frontend)**
*
* ```ts
* import { useSigner, useChainId } from 'wagmi';
* import { KVStoreClient } from '@human-protocol/sdk';
*
* const { data: signer } = useSigner();
* const kvstoreClient = await KVStoreClient.build(signer);
* ```
*
* ### Provider
*
* ```ts
* import { KVStoreClient } from '@human-protocol/sdk';
* import { providers } from 'ethers';
*
* const rpcUrl = 'YOUR_RPC_URL';
*
* const provider = new providers.JsonRpcProvider(rpcUrl);
* const kvstoreClient = await KVStoreClient.build(signer);
* ```
*/
export class KVStoreClient {
private contract: KVStore;
private signerOrProvider: Signer | Provider;
/**
* **KVStoreClient constructor**
*
* @param {Signer | Provider} signerOrProvider - The Signer or Provider object to interact with the Ethereum network
* @param {NetworkData} network - The network information required to connect to the KVStore contract
*/
constructor(signerOrProvider: Signer | Provider, network: NetworkData) {
this.contract = KVStore__factory.connect(
network.kvstoreAddress,
signerOrProvider
);
this.signerOrProvider = signerOrProvider;
}
/**
* Creates an instance of KVStoreClient from a Signer or Provider.
*
* @param {Signer | Provider} signerOrProvider - The Signer or Provider object to interact with the Ethereum network
* @returns {Promise<KVStoreClient>} - An instance of KVStoreClient
* @throws {ErrorProviderDoesNotExist} - Thrown if the provider does not exist for the provided Signer
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
*/
public static async build(signerOrProvider: Signer | Provider) {
let network: Network;
if (Signer.isSigner(signerOrProvider)) {
if (!signerOrProvider.provider) {
throw ErrorProviderDoesNotExist;
}
network = await signerOrProvider.provider.getNetwork();
} else {
network = await signerOrProvider.getNetwork();
}
const chainId: ChainId = network.chainId;
const networkData = NETWORKS[chainId];
if (!networkData) {
throw ErrorUnsupportedChainID;
}
return new KVStoreClient(signerOrProvider, networkData);
}
/**
* This function sets a key-value pair associated with the address that submits the transaction.
*
* @param {string} key Key of the key-value pair
* @param {string} value Value of the key-value pair
* @returns Returns void if successful. Throws error if any.
*
*
* **Code example**
*
* > Need to have available stake.
*
* ```ts
* import { Wallet, providers } from 'ethers';
* import { KVStoreClient } 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 kvstoreClient = await KVStoreClient.build(signer);
*
* await kvstoreClient.set('Role', 'RecordingOracle');
* ```
*/
@requiresSigner
public async set(key: string, value: string): Promise<void> {
if (!Signer.isSigner(this.signerOrProvider)) throw ErrorSigner;
if (key === '') throw ErrorKVStoreEmptyKey;
try {
await this.contract?.set(key, value);
} catch (e) {
if (e instanceof Error) throw Error(`Failed to set value: ${e.message}`);
}
}
/**
* This function sets key-value pairs in bulk associated with the address that submits the transaction.
*
* @param {string[]} keys Array of keys (keys and value must have the same order)
* @param {string[]} values Array of values
* @returns Returns void if successful. Throws error if any.
*
*
* **Code example**
*
* > Need to have available stake.
*
* ```ts
* import { Wallet, providers } from 'ethers';
* import { KVStoreClient } 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 kvstoreClient = await KVStoreClient.build(signer);
*
* const keys = ['role', 'webhookUrl'];
* const values = ['RecordingOracle', 'http://localhost'];
* await kvstoreClient.set(keys, values);
* ```
*/
@requiresSigner
public async setBulk(keys: string[], values: string[]): Promise<void> {
if (!Signer.isSigner(this.signerOrProvider)) throw ErrorSigner;
if (keys.length !== values.length) throw ErrorKVStoreArrayLength;
if (keys.includes('')) throw ErrorKVStoreEmptyKey;
try {
await this.contract?.setBulk(keys, values);
} catch (e) {
if (e instanceof Error)
throw Error(`Failed to set bulk values: ${e.message}`);
}
}
/**
* This function returns the value for a specified key and address.
*
* @param {string} address Address from which to get the key value.
* @param {string} key Key to obtain the value.
* @returns {string} Value of the key.
*
*
* **Code example**
*
* > Need to have available stake.
*
* ```ts
* import { providers } from 'ethers';
* import { KVStoreClient } from '@human-protocol/sdk';
*
* const rpcUrl = 'YOUR_RPC_URL';
*
* const provider = new providers.JsonRpcProvider(rpcUrl);
* const kvstoreClient = await KVStoreClient.build(provider);
*
* const value = await kvstoreClient.get('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', 'Role');
* ```
*/
public async get(address: string, key: string): Promise<string> {
if (key === '') throw ErrorKVStoreEmptyKey;
if (!ethers.utils.isAddress(address)) throw ErrorInvalidAddress;
try {
const result = await this.contract?.get(address, key);
return result;
} catch (e) {
if (e instanceof Error) throw Error(`Failed to get value: ${e.message}`);
return e;
}
}
}