@human-protocol/sdk 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/constants.d.ts +46 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +203 -0
- package/dist/decorators.d.ts +2 -0
- package/dist/decorators.d.ts.map +1 -0
- package/dist/decorators.js +17 -0
- package/dist/enums.d.ts +17 -0
- package/dist/enums.d.ts.map +1 -0
- package/dist/enums.js +20 -0
- package/dist/error.d.ts +196 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +229 -0
- package/dist/escrow.d.ts +176 -0
- package/dist/escrow.d.ts.map +1 -0
- package/dist/escrow.js +590 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/init.d.ts +13 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +35 -0
- package/dist/interfaces.d.ts +44 -0
- package/dist/interfaces.d.ts.map +1 -0
- package/dist/interfaces.js +2 -0
- package/dist/kvstore.d.ts +40 -0
- package/dist/kvstore.d.ts.map +1 -0
- package/dist/kvstore.js +106 -0
- package/dist/queries.d.ts +4 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +22 -0
- package/dist/staking.d.ts +121 -0
- package/dist/staking.d.ts.map +1 -0
- package/dist/staking.js +381 -0
- package/dist/storage.d.ts +48 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +164 -0
- package/dist/types.d.ts +123 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +35 -0
- package/dist/utils.d.ts +32 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +99 -0
- package/package.json +4 -7
- package/src/constants.ts +221 -4
- package/src/decorators.ts +21 -0
- package/src/enums.ts +16 -0
- package/src/error.ts +295 -18
- package/src/escrow.ts +754 -0
- package/src/index.ts +14 -1
- package/src/init.ts +45 -0
- package/src/interfaces.ts +50 -0
- package/src/kvstore.ts +93 -0
- package/src/queries.ts +18 -0
- package/src/staking.ts +421 -0
- package/src/storage.ts +159 -131
- package/src/types.ts +36 -586
- package/src/utils.ts +80 -142
- package/test/escrow.test.ts +1339 -0
- package/test/init.test.ts +88 -0
- package/test/kvstore.test.ts +208 -0
- package/test/staking.test.ts +640 -0
- package/test/storage.test.ts +422 -0
- package/test/utils/constants.ts +38 -1
- package/example/simple-existing-job.ts +0 -86
- package/example/simple-new-job-public.ts +0 -74
- package/example/simple-new-job.ts +0 -72
- package/src/job.ts +0 -977
- package/src/logger.ts +0 -29
- package/test/job.test.ts +0 -716
- package/test/utils/manifest.ts +0 -33
package/dist/staking.js
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const typechain_types_1 = require("@human-protocol/core/typechain-types");
|
|
13
|
+
const ethers_1 = require("ethers");
|
|
14
|
+
const error_1 = require("./error");
|
|
15
|
+
const utils_1 = require("./utils");
|
|
16
|
+
const queries_1 = require("./queries");
|
|
17
|
+
const decorators_1 = require("./decorators");
|
|
18
|
+
class StakingClient {
|
|
19
|
+
/**
|
|
20
|
+
* **Staking constructor**
|
|
21
|
+
*
|
|
22
|
+
* @param {IClientParams} clientParams - Init client parameters
|
|
23
|
+
*/
|
|
24
|
+
constructor(clientParams) {
|
|
25
|
+
this.clientParams = clientParams;
|
|
26
|
+
this.signerOrProvider = clientParams.signerOrProvider;
|
|
27
|
+
this.network = clientParams.network;
|
|
28
|
+
this.stakingContract = typechain_types_1.Staking__factory.connect(this.network.stakingAddress, this.signerOrProvider);
|
|
29
|
+
this.escrowFactoryContract = typechain_types_1.EscrowFactory__factory.connect(this.network.factoryAddress, this.signerOrProvider);
|
|
30
|
+
this.tokenContract = typechain_types_1.HMToken__factory.connect(this.network.hmtAddress, this.signerOrProvider);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* **Approves the staking contract to transfer a specified amount of tokens when the user stakes.
|
|
34
|
+
* **It increases the allowance for the staking contract.*
|
|
35
|
+
*
|
|
36
|
+
* @param {BigNumber} amount - Amount of tokens to approve for stake
|
|
37
|
+
* @returns {Promise<void>}
|
|
38
|
+
* @throws {Error} - An error object if an error occurred, void otherwise
|
|
39
|
+
*/
|
|
40
|
+
async approveStake(amount) {
|
|
41
|
+
if (!ethers_1.BigNumber.isBigNumber(amount)) {
|
|
42
|
+
throw error_1.ErrorInvalidStakingValueType;
|
|
43
|
+
}
|
|
44
|
+
if (amount.isNegative()) {
|
|
45
|
+
throw error_1.ErrorInvalidStakingValueSign;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
await this.tokenContract.approve(this.stakingContract.address, amount);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
return (0, utils_1.throwError)(e);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* **Stakes a specified amount of tokens on a specific network.*
|
|
57
|
+
*
|
|
58
|
+
* @param {BigNumber} amount - Amount of tokens to stake
|
|
59
|
+
* @returns {Promise<void>}
|
|
60
|
+
* @throws {Error} - An error object if an error occurred, void otherwise
|
|
61
|
+
*/
|
|
62
|
+
async stake(amount) {
|
|
63
|
+
if (!ethers_1.BigNumber.isBigNumber(amount)) {
|
|
64
|
+
throw error_1.ErrorInvalidStakingValueType;
|
|
65
|
+
}
|
|
66
|
+
if (amount.isNegative()) {
|
|
67
|
+
throw error_1.ErrorInvalidStakingValueSign;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
await this.stakingContract.stake(amount);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
return (0, utils_1.throwError)(e);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* **Unstakes tokens from staking contract.
|
|
79
|
+
* **The unstaked tokens stay locked for a period of time.*
|
|
80
|
+
*
|
|
81
|
+
* @param {BigNumber} amount - Amount of tokens to unstake
|
|
82
|
+
* @returns {Promise<void>}
|
|
83
|
+
* @throws {Error} - An error object if an error occurred, void otherwise
|
|
84
|
+
*/
|
|
85
|
+
async unstake(amount) {
|
|
86
|
+
if (!ethers_1.BigNumber.isBigNumber(amount)) {
|
|
87
|
+
throw error_1.ErrorInvalidStakingValueType;
|
|
88
|
+
}
|
|
89
|
+
if (amount.isNegative()) {
|
|
90
|
+
throw error_1.ErrorInvalidStakingValueSign;
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
await this.stakingContract.unstake(amount);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
return (0, utils_1.throwError)(e);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* **Withdraws unstaked and non locked tokens form staking contract to the user wallet.*
|
|
102
|
+
*
|
|
103
|
+
* @returns {Promise<void>}
|
|
104
|
+
* @throws {Error} - An error object if an error occurred, void otherwise
|
|
105
|
+
*/
|
|
106
|
+
async withdraw() {
|
|
107
|
+
try {
|
|
108
|
+
await this.stakingContract.withdraw();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
return (0, utils_1.throwError)(e);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* **Slash the allocated amount by an staker in an escrow and transfers those tokens to the reward pool.
|
|
117
|
+
* **This allows the slasher to claim them later.*
|
|
118
|
+
*
|
|
119
|
+
* @param {string} slasher - Wallet address from who requested the slash
|
|
120
|
+
* @param {string} staker - Wallet address from who is going to be slashed
|
|
121
|
+
* @param {string} escrowAddress - Address of the escrow which allocation will be slashed
|
|
122
|
+
* @param {BigNumber} amount - Amount of tokens to slash
|
|
123
|
+
* @returns {Promise<void>}
|
|
124
|
+
* @throws {Error} - An error object if an error occurred, void otherwise
|
|
125
|
+
*/
|
|
126
|
+
async slash(slasher, staker, escrowAddress, amount) {
|
|
127
|
+
if (!ethers_1.BigNumber.isBigNumber(amount)) {
|
|
128
|
+
throw error_1.ErrorInvalidStakingValueType;
|
|
129
|
+
}
|
|
130
|
+
if (amount.isNegative()) {
|
|
131
|
+
throw error_1.ErrorInvalidStakingValueSign;
|
|
132
|
+
}
|
|
133
|
+
if (!ethers_1.ethers.utils.isAddress(slasher)) {
|
|
134
|
+
throw error_1.ErrorInvalidSlasherAddressProvided;
|
|
135
|
+
}
|
|
136
|
+
if (!ethers_1.ethers.utils.isAddress(staker)) {
|
|
137
|
+
throw error_1.ErrorInvalidStakerAddressProvided;
|
|
138
|
+
}
|
|
139
|
+
if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
|
|
140
|
+
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
141
|
+
}
|
|
142
|
+
if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
|
|
143
|
+
throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
await this.stakingContract.slash(slasher, staker, escrowAddress, amount);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
catch (e) {
|
|
150
|
+
return (0, utils_1.throwError)(e);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* **Allocates a portion of the staked tokens to a specific escrow.*
|
|
155
|
+
*
|
|
156
|
+
* @param {string} escrowAddress - Address of the escrow contract
|
|
157
|
+
* @param {BigNumber} amount - Amount of tokens to allocate
|
|
158
|
+
* @returns {Promise<void>}
|
|
159
|
+
* @throws {Error} - An error object if an error occurred, void otherwise
|
|
160
|
+
*/
|
|
161
|
+
async allocate(escrowAddress, amount) {
|
|
162
|
+
if (!ethers_1.BigNumber.isBigNumber(amount)) {
|
|
163
|
+
throw error_1.ErrorInvalidStakingValueType;
|
|
164
|
+
}
|
|
165
|
+
if (amount.isNegative()) {
|
|
166
|
+
throw error_1.ErrorInvalidStakingValueSign;
|
|
167
|
+
}
|
|
168
|
+
if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
|
|
169
|
+
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
170
|
+
}
|
|
171
|
+
if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
|
|
172
|
+
throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
await this.stakingContract.allocate(escrowAddress, amount);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
catch (e) {
|
|
179
|
+
return (0, utils_1.throwError)(e);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* **Drops the allocation from a specific escrow.*
|
|
184
|
+
*
|
|
185
|
+
* @param {string} escrowAddress - Address of the escrow contract.
|
|
186
|
+
* @returns {Promise<void>}
|
|
187
|
+
* @throws {Error} - An error object if an error occurred, void otherwise
|
|
188
|
+
*/
|
|
189
|
+
async closeAllocation(escrowAddress) {
|
|
190
|
+
if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
|
|
191
|
+
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
192
|
+
}
|
|
193
|
+
if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
|
|
194
|
+
throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
await this.stakingContract.closeAllocation(escrowAddress);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
catch (e) {
|
|
201
|
+
return (0, utils_1.throwError)(e);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* **Pays out rewards to the slashers for the specified escrow address.*
|
|
206
|
+
*
|
|
207
|
+
* @param {string} escrowAddress - Escrow address from which rewards are distributed.
|
|
208
|
+
* @returns {Promise<void>}
|
|
209
|
+
* @throws {Error} - An error object if an error occurred, void otherwise
|
|
210
|
+
*/
|
|
211
|
+
async distributeRewards(escrowAddress) {
|
|
212
|
+
if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
|
|
213
|
+
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
214
|
+
}
|
|
215
|
+
if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
|
|
216
|
+
throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
|
|
217
|
+
}
|
|
218
|
+
try {
|
|
219
|
+
const rewardPoolContract = typechain_types_1.RewardPool__factory.connect(await this.stakingContract.rewardPool(), this.signerOrProvider);
|
|
220
|
+
await rewardPoolContract.distributeReward(escrowAddress);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
catch (e) {
|
|
224
|
+
return (0, utils_1.throwError)(e);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* **Returns the staking information about an staker address.*
|
|
229
|
+
*
|
|
230
|
+
* @param {string} staker - Address of the staker
|
|
231
|
+
* @returns {Promise<IStaker>} - Return staking information of the specified address
|
|
232
|
+
* @throws {Error} - An error object if an error occurred, result otherwise
|
|
233
|
+
*/
|
|
234
|
+
async getStaker(staker) {
|
|
235
|
+
if (!ethers_1.ethers.utils.isAddress(staker)) {
|
|
236
|
+
throw error_1.ErrorInvalidStakerAddressProvided;
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
const result = await this.stakingContract.getStaker(staker);
|
|
240
|
+
const tokensStaked = ethers_1.BigNumber.from(result.tokensStaked), tokensAllocated = ethers_1.BigNumber.from(result.tokensAllocated), tokensLocked = ethers_1.BigNumber.from(result.tokensLocked), tokensLockedUntil = ethers_1.BigNumber.from(result.tokensLockedUntil);
|
|
241
|
+
const tokensAvailable = tokensStaked
|
|
242
|
+
.sub(tokensAllocated)
|
|
243
|
+
.sub(tokensLocked);
|
|
244
|
+
return {
|
|
245
|
+
tokensStaked,
|
|
246
|
+
tokensAllocated,
|
|
247
|
+
tokensLocked,
|
|
248
|
+
tokensLockedUntil,
|
|
249
|
+
tokensAvailable,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
catch (e) {
|
|
253
|
+
return (0, utils_1.throwError)(e);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* **Returns the staking information about all stakers of the protocol.*
|
|
258
|
+
*
|
|
259
|
+
* @returns {Promise<IStakerInfo>} - Return an array with all stakers information
|
|
260
|
+
* @throws {Error} - An error object if an error occurred, results otherwise
|
|
261
|
+
*/
|
|
262
|
+
async getAllStakers() {
|
|
263
|
+
try {
|
|
264
|
+
const result = await this.stakingContract.getListOfStakers();
|
|
265
|
+
if (result[1].length === 0) {
|
|
266
|
+
throw error_1.ErrorStakingGetStakers;
|
|
267
|
+
}
|
|
268
|
+
return result[1].map((staker) => {
|
|
269
|
+
const tokensStaked = ethers_1.BigNumber.from(staker.tokensStaked), tokensAllocated = ethers_1.BigNumber.from(staker.tokensAllocated), tokensLocked = ethers_1.BigNumber.from(staker.tokensLocked), tokensLockedUntil = ethers_1.BigNumber.from(staker.tokensLockedUntil);
|
|
270
|
+
const tokensAvailable = tokensStaked
|
|
271
|
+
.sub(tokensAllocated)
|
|
272
|
+
.sub(tokensLocked);
|
|
273
|
+
return {
|
|
274
|
+
tokensStaked,
|
|
275
|
+
tokensAllocated,
|
|
276
|
+
tokensLocked,
|
|
277
|
+
tokensLockedUntil,
|
|
278
|
+
tokensAvailable,
|
|
279
|
+
};
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
catch (e) {
|
|
283
|
+
return (0, utils_1.throwError)(e);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* **Returns information about the allocation of the specified escrow.*
|
|
288
|
+
*
|
|
289
|
+
* @param {string} escrowAddress - The escrow address for the received allocation data
|
|
290
|
+
* @returns {Promise<IAllocation>} - Returns allocation info if exists
|
|
291
|
+
* @throws {Error} - An error object if an error occurred, result otherwise
|
|
292
|
+
*/
|
|
293
|
+
async getAllocation(escrowAddress) {
|
|
294
|
+
if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
|
|
295
|
+
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
296
|
+
}
|
|
297
|
+
if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
|
|
298
|
+
throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
|
|
299
|
+
}
|
|
300
|
+
try {
|
|
301
|
+
const result = await this.stakingContract.getAllocation(escrowAddress);
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
catch (e) {
|
|
305
|
+
return (0, utils_1.throwError)(e);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* **Returns information about the rewards for a given escrow address.*
|
|
310
|
+
*
|
|
311
|
+
* @param {string} slasherAddress - Address of the slasher
|
|
312
|
+
* @returns {Promise<IReward[]>} - Returns rewards info if exists
|
|
313
|
+
* @throws {Error} - An error object if an error occurred, results otherwise
|
|
314
|
+
*/
|
|
315
|
+
async getRewards(slasherAddress) {
|
|
316
|
+
if (!ethers_1.ethers.utils.isAddress(slasherAddress)) {
|
|
317
|
+
throw error_1.ErrorInvalidSlasherAddressProvided;
|
|
318
|
+
}
|
|
319
|
+
try {
|
|
320
|
+
const { data } = await (0, utils_1.gqlFetch)(this.network.subgraphUrl, (0, queries_1.RAW_REWARDS_QUERY)(slasherAddress));
|
|
321
|
+
return data.rewardAddedEvents.map((reward) => {
|
|
322
|
+
return {
|
|
323
|
+
escrowAddress: reward.escrow,
|
|
324
|
+
amount: reward.amount,
|
|
325
|
+
};
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
catch (e) {
|
|
329
|
+
return (0, utils_1.throwError)(e);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
__decorate([
|
|
334
|
+
decorators_1.requiresSigner,
|
|
335
|
+
__metadata("design:type", Function),
|
|
336
|
+
__metadata("design:paramtypes", [ethers_1.BigNumber]),
|
|
337
|
+
__metadata("design:returntype", Promise)
|
|
338
|
+
], StakingClient.prototype, "approveStake", null);
|
|
339
|
+
__decorate([
|
|
340
|
+
decorators_1.requiresSigner,
|
|
341
|
+
__metadata("design:type", Function),
|
|
342
|
+
__metadata("design:paramtypes", [ethers_1.BigNumber]),
|
|
343
|
+
__metadata("design:returntype", Promise)
|
|
344
|
+
], StakingClient.prototype, "stake", null);
|
|
345
|
+
__decorate([
|
|
346
|
+
decorators_1.requiresSigner,
|
|
347
|
+
__metadata("design:type", Function),
|
|
348
|
+
__metadata("design:paramtypes", [ethers_1.BigNumber]),
|
|
349
|
+
__metadata("design:returntype", Promise)
|
|
350
|
+
], StakingClient.prototype, "unstake", null);
|
|
351
|
+
__decorate([
|
|
352
|
+
decorators_1.requiresSigner,
|
|
353
|
+
__metadata("design:type", Function),
|
|
354
|
+
__metadata("design:paramtypes", []),
|
|
355
|
+
__metadata("design:returntype", Promise)
|
|
356
|
+
], StakingClient.prototype, "withdraw", null);
|
|
357
|
+
__decorate([
|
|
358
|
+
decorators_1.requiresSigner,
|
|
359
|
+
__metadata("design:type", Function),
|
|
360
|
+
__metadata("design:paramtypes", [String, String, String, ethers_1.BigNumber]),
|
|
361
|
+
__metadata("design:returntype", Promise)
|
|
362
|
+
], StakingClient.prototype, "slash", null);
|
|
363
|
+
__decorate([
|
|
364
|
+
decorators_1.requiresSigner,
|
|
365
|
+
__metadata("design:type", Function),
|
|
366
|
+
__metadata("design:paramtypes", [String, ethers_1.BigNumber]),
|
|
367
|
+
__metadata("design:returntype", Promise)
|
|
368
|
+
], StakingClient.prototype, "allocate", null);
|
|
369
|
+
__decorate([
|
|
370
|
+
decorators_1.requiresSigner,
|
|
371
|
+
__metadata("design:type", Function),
|
|
372
|
+
__metadata("design:paramtypes", [String]),
|
|
373
|
+
__metadata("design:returntype", Promise)
|
|
374
|
+
], StakingClient.prototype, "closeAllocation", null);
|
|
375
|
+
__decorate([
|
|
376
|
+
decorators_1.requiresSigner,
|
|
377
|
+
__metadata("design:type", Function),
|
|
378
|
+
__metadata("design:paramtypes", [String]),
|
|
379
|
+
__metadata("design:returntype", Promise)
|
|
380
|
+
], StakingClient.prototype, "distributeRewards", null);
|
|
381
|
+
exports.default = StakingClient;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { UploadFile, StorageCredentials, StorageParams } from './types';
|
|
2
|
+
export default class StorageClient {
|
|
3
|
+
private client;
|
|
4
|
+
/**
|
|
5
|
+
* **Storage client constructor**
|
|
6
|
+
*
|
|
7
|
+
* @param {StorageCredentials} credentials - Cloud storage access data
|
|
8
|
+
* @param {StorageParams} params - Cloud storage params
|
|
9
|
+
*/
|
|
10
|
+
constructor(credentials: StorageCredentials, params: StorageParams);
|
|
11
|
+
/**
|
|
12
|
+
* **Download files from cloud storage**
|
|
13
|
+
*
|
|
14
|
+
* @param {string} keys - Keys of files
|
|
15
|
+
* @returns {Promise<File>} - Downloaded file
|
|
16
|
+
*/
|
|
17
|
+
downloadFiles(keys: string[], bucket: string): Promise<any[]>;
|
|
18
|
+
/**
|
|
19
|
+
* **Download files from cloud storage.*
|
|
20
|
+
*
|
|
21
|
+
* @param {string} url - URL to the file
|
|
22
|
+
* @returns {Promise<File>} - Downloaded file
|
|
23
|
+
*/
|
|
24
|
+
static downloadFileFromUrl(url: string): Promise<any>;
|
|
25
|
+
/**
|
|
26
|
+
* **Upload file to cloud storage**
|
|
27
|
+
*
|
|
28
|
+
* @param {File[]} files - Files to upload
|
|
29
|
+
* @param {string} bucket - Bucket name
|
|
30
|
+
* @returns {Promise<UploadFile>} - Uploaded file with key/hash
|
|
31
|
+
*/
|
|
32
|
+
uploadFiles(files: any[], bucket: string): Promise<UploadFile[]>;
|
|
33
|
+
/**
|
|
34
|
+
* **Checks if a bucket exists**
|
|
35
|
+
*
|
|
36
|
+
* @param {string} bucket - Name of the bucket
|
|
37
|
+
* @returns {Promise<boolean>} - True if bucket exists, false otherwise
|
|
38
|
+
*/
|
|
39
|
+
bucketExists(bucket: string): Promise<boolean>;
|
|
40
|
+
/**
|
|
41
|
+
* **Checks if a bucket exists**
|
|
42
|
+
*
|
|
43
|
+
* @param {string} bucket - Name of the bucket
|
|
44
|
+
* @returns {Promise<string[]>} - A list of filenames with their extensions in the bucket
|
|
45
|
+
*/
|
|
46
|
+
listObjects(bucket: string): Promise<string[]>;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAIxE,MAAM,CAAC,OAAO,OAAO,aAAa;IAChC,OAAO,CAAC,MAAM,CAAe;IAE7B;;;;;OAKG;gBACS,WAAW,EAAE,kBAAkB,EAAE,MAAM,EAAE,aAAa;IAYlE;;;;;OAKG;IACU,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;IAoB1E;;;;;OAKG;WACiB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAsBlE;;;;;;OAMG;IACU,WAAW,CACtB,KAAK,EAAE,GAAG,EAAE,EACZ,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,UAAU,EAAE,CAAC;IA0BxB;;;;;OAKG;IACU,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAI3D;;;;;OAKG;IACU,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAqB5D"}
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const axios_1 = __importDefault(require("axios"));
|
|
30
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
31
|
+
const Minio = __importStar(require("minio"));
|
|
32
|
+
const error_1 = require("./error");
|
|
33
|
+
const utils_1 = require("./utils");
|
|
34
|
+
const constants_1 = require("./constants");
|
|
35
|
+
class StorageClient {
|
|
36
|
+
/**
|
|
37
|
+
* **Storage client constructor**
|
|
38
|
+
*
|
|
39
|
+
* @param {StorageCredentials} credentials - Cloud storage access data
|
|
40
|
+
* @param {StorageParams} params - Cloud storage params
|
|
41
|
+
*/
|
|
42
|
+
constructor(credentials, params) {
|
|
43
|
+
try {
|
|
44
|
+
this.client = new Minio.Client({
|
|
45
|
+
...params,
|
|
46
|
+
accessKey: credentials.accessKey,
|
|
47
|
+
secretKey: credentials.secretKey,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
catch (e) {
|
|
51
|
+
throw error_1.ErrorStorageClientNotInitialized;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* **Download files from cloud storage**
|
|
56
|
+
*
|
|
57
|
+
* @param {string} keys - Keys of files
|
|
58
|
+
* @returns {Promise<File>} - Downloaded file
|
|
59
|
+
*/
|
|
60
|
+
async downloadFiles(keys, bucket) {
|
|
61
|
+
const isBucketExists = await this.client.bucketExists(bucket);
|
|
62
|
+
if (!isBucketExists) {
|
|
63
|
+
throw error_1.ErrorStorageBucketNotFound;
|
|
64
|
+
}
|
|
65
|
+
return Promise.all(keys.map(async (key) => {
|
|
66
|
+
try {
|
|
67
|
+
const response = await this.client.getObject(bucket, key);
|
|
68
|
+
const content = response?.read();
|
|
69
|
+
return { key, content: JSON.parse(content?.toString('utf-8') || '') };
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
throw error_1.ErrorStorageFileNotFound;
|
|
73
|
+
}
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* **Download files from cloud storage.*
|
|
78
|
+
*
|
|
79
|
+
* @param {string} url - URL to the file
|
|
80
|
+
* @returns {Promise<File>} - Downloaded file
|
|
81
|
+
*/
|
|
82
|
+
static async downloadFileFromUrl(url) {
|
|
83
|
+
if (!(0, utils_1.isValidUrl)(url)) {
|
|
84
|
+
throw error_1.ErrorInvalidUrl;
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
const { data, status } = await axios_1.default.get(url, {
|
|
88
|
+
headers: {
|
|
89
|
+
'Content-Type': 'application/json',
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
if (status !== constants_1.HttpStatus.OK) {
|
|
93
|
+
throw error_1.ErrorStorageFileNotFound;
|
|
94
|
+
}
|
|
95
|
+
return data;
|
|
96
|
+
}
|
|
97
|
+
catch (e) {
|
|
98
|
+
throw error_1.ErrorStorageFileNotFound;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* **Upload file to cloud storage**
|
|
103
|
+
*
|
|
104
|
+
* @param {File[]} files - Files to upload
|
|
105
|
+
* @param {string} bucket - Bucket name
|
|
106
|
+
* @returns {Promise<UploadFile>} - Uploaded file with key/hash
|
|
107
|
+
*/
|
|
108
|
+
async uploadFiles(files, bucket) {
|
|
109
|
+
const isBucketExists = await this.client.bucketExists(bucket);
|
|
110
|
+
if (!isBucketExists) {
|
|
111
|
+
throw error_1.ErrorStorageBucketNotFound;
|
|
112
|
+
}
|
|
113
|
+
return Promise.all(files.map(async (file) => {
|
|
114
|
+
const content = JSON.stringify(file);
|
|
115
|
+
const hash = crypto_1.default.createHash('sha1').update(content).digest('hex');
|
|
116
|
+
const key = hash;
|
|
117
|
+
try {
|
|
118
|
+
await this.client.putObject(bucket, key, content, {
|
|
119
|
+
'Content-Type': 'application/json',
|
|
120
|
+
});
|
|
121
|
+
return { key, hash };
|
|
122
|
+
}
|
|
123
|
+
catch (e) {
|
|
124
|
+
throw error_1.ErrorStorageFileNotUploaded;
|
|
125
|
+
}
|
|
126
|
+
}));
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* **Checks if a bucket exists**
|
|
130
|
+
*
|
|
131
|
+
* @param {string} bucket - Name of the bucket
|
|
132
|
+
* @returns {Promise<boolean>} - True if bucket exists, false otherwise
|
|
133
|
+
*/
|
|
134
|
+
async bucketExists(bucket) {
|
|
135
|
+
return this.client.bucketExists(bucket);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* **Checks if a bucket exists**
|
|
139
|
+
*
|
|
140
|
+
* @param {string} bucket - Name of the bucket
|
|
141
|
+
* @returns {Promise<string[]>} - A list of filenames with their extensions in the bucket
|
|
142
|
+
*/
|
|
143
|
+
async listObjects(bucket) {
|
|
144
|
+
const isBucketExists = await this.client.bucketExists(bucket);
|
|
145
|
+
if (!isBucketExists) {
|
|
146
|
+
throw error_1.ErrorStorageBucketNotFound;
|
|
147
|
+
}
|
|
148
|
+
try {
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
const keys = [];
|
|
151
|
+
const stream = this.client.listObjectsV2(bucket, '', true, '');
|
|
152
|
+
stream.on('data', (obj) => keys.push(obj.name));
|
|
153
|
+
stream.on('error', reject);
|
|
154
|
+
stream.on('end', () => {
|
|
155
|
+
resolve(keys);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
catch (e) {
|
|
160
|
+
throw new Error(String(e));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
exports.default = StorageClient;
|