@sogni-ai/sogni-client 3.0.0-alpha.4 → 3.0.0-alpha.40
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/CHANGELOG.md +274 -0
- package/README.md +3 -1
- package/dist/Account/CurrentAccount.d.ts +3 -3
- package/dist/Account/CurrentAccount.js +12 -4
- package/dist/Account/CurrentAccount.js.map +1 -1
- package/dist/Account/index.d.ts +40 -10
- package/dist/Account/index.js +133 -28
- package/dist/Account/index.js.map +1 -1
- package/dist/Account/types.d.ts +21 -0
- package/dist/ApiClient/WebSocketClient/events.d.ts +55 -7
- package/dist/ApiClient/WebSocketClient/index.js +1 -1
- package/dist/ApiClient/index.d.ts +4 -2
- package/dist/ApiClient/index.js +12 -3
- package/dist/ApiClient/index.js.map +1 -1
- package/dist/ApiGroup.d.ts +0 -3
- package/dist/ApiGroup.js +0 -1
- package/dist/ApiGroup.js.map +1 -1
- package/dist/Projects/Job.d.ts +43 -0
- package/dist/Projects/Job.js +76 -0
- package/dist/Projects/Job.js.map +1 -1
- package/dist/Projects/Project.d.ts +1 -1
- package/dist/Projects/Project.js +22 -19
- package/dist/Projects/Project.js.map +1 -1
- package/dist/Projects/createJobRequestMessage.js +2 -2
- package/dist/Projects/createJobRequestMessage.js.map +1 -1
- package/dist/Projects/index.d.ts +7 -2
- package/dist/Projects/index.js +50 -12
- package/dist/Projects/index.js.map +1 -1
- package/dist/Projects/types/ControlNetParams.d.ts +7 -2
- package/dist/Projects/types/events.d.ts +6 -0
- package/dist/Projects/types/index.d.ts +16 -3
- package/dist/Projects/utils.d.ts +2 -0
- package/dist/Projects/utils.js +14 -0
- package/dist/Projects/utils.js.map +1 -0
- package/dist/Stats/types.d.ts +1 -1
- package/dist/index.d.ts +10 -6
- package/dist/index.js +6 -15
- package/dist/index.js.map +1 -1
- package/dist/lib/utils.d.ts +1 -0
- package/dist/lib/utils.js +15 -0
- package/dist/lib/utils.js.map +1 -1
- package/dist/types/token.d.ts +1 -0
- package/dist/types/token.js +3 -0
- package/dist/types/token.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +2 -1
- package/dist/version.js.map +1 -1
- package/package.json +1 -1
- package/src/Account/CurrentAccount.ts +14 -6
- package/src/Account/index.ts +162 -56
- package/src/Account/types.ts +26 -0
- package/src/ApiClient/WebSocketClient/events.ts +59 -7
- package/src/ApiClient/WebSocketClient/index.ts +1 -1
- package/src/ApiClient/index.ts +15 -4
- package/src/ApiGroup.ts +0 -4
- package/src/Projects/Job.ts +98 -0
- package/src/Projects/Project.ts +24 -20
- package/src/Projects/createJobRequestMessage.ts +4 -2
- package/src/Projects/index.ts +55 -15
- package/src/Projects/types/ControlNetParams.ts +8 -2
- package/src/Projects/types/events.ts +6 -0
- package/src/Projects/types/index.ts +17 -3
- package/src/Projects/utils.ts +12 -0
- package/src/Stats/index.ts +2 -2
- package/src/Stats/types.ts +2 -0
- package/src/index.ts +23 -19
- package/src/lib/utils.ts +4 -0
- package/src/types/token.ts +1 -0
- package/src/version.ts +2 -1
package/src/Account/index.ts
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AccountCreateData,
|
|
3
3
|
AccountCreateParams,
|
|
4
|
-
|
|
4
|
+
Balances,
|
|
5
|
+
ClaimOptions,
|
|
6
|
+
FullBalances,
|
|
5
7
|
LoginData,
|
|
6
8
|
Nonce,
|
|
7
9
|
Reward,
|
|
8
10
|
RewardRaw,
|
|
11
|
+
RewardsQuery,
|
|
9
12
|
TxHistoryData,
|
|
10
13
|
TxHistoryEntry,
|
|
11
14
|
TxHistoryParams
|
|
12
15
|
} from './types';
|
|
13
16
|
import ApiGroup, { ApiConfig } from '../ApiGroup';
|
|
14
|
-
import {
|
|
15
|
-
import { ApiError,
|
|
17
|
+
import { parseEther, pbkdf2, toUtf8Bytes, Wallet } from 'ethers';
|
|
18
|
+
import { ApiError, ApiResponse } from '../ApiClient';
|
|
16
19
|
import CurrentAccount from './CurrentAccount';
|
|
17
20
|
import { SupernetType } from '../ApiClient/WebSocketClient/types';
|
|
18
21
|
import { AuthUpdatedEvent, Tokens } from '../lib/AuthManager';
|
|
22
|
+
import { delay } from '../lib/utils';
|
|
19
23
|
|
|
24
|
+
const MAX_DEPOSIT_ATTEMPTS = 4;
|
|
25
|
+
enum ErrorCode {
|
|
26
|
+
INSUFFICIENT_BALANCE = 123,
|
|
27
|
+
INSUFFICIENT_ALLOWANCE = 149
|
|
28
|
+
}
|
|
20
29
|
/**
|
|
21
30
|
* Account API methods that let you interact with the user's account.
|
|
22
31
|
* Can be accessed via `client.account`. Look for more samples below.
|
|
@@ -39,7 +48,7 @@ class AccountApi extends ApiGroup {
|
|
|
39
48
|
this.client.auth.on('updated', this.handleAuthUpdated.bind(this));
|
|
40
49
|
}
|
|
41
50
|
|
|
42
|
-
private handleBalanceUpdate(data:
|
|
51
|
+
private handleBalanceUpdate(data: Balances) {
|
|
43
52
|
this.currentAccount._update({ balance: data });
|
|
44
53
|
}
|
|
45
54
|
|
|
@@ -62,8 +71,13 @@ class AccountApi extends ApiGroup {
|
|
|
62
71
|
}
|
|
63
72
|
}
|
|
64
73
|
|
|
65
|
-
|
|
66
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Get the nonce for the given wallet address.
|
|
76
|
+
* @param walletAddress
|
|
77
|
+
* @internal
|
|
78
|
+
*/
|
|
79
|
+
async getNonce(walletAddress: string): Promise<string> {
|
|
80
|
+
const res = await this.client.rest.post<ApiResponse<Nonce>>('/v1/account/nonce', {
|
|
67
81
|
walletAddress
|
|
68
82
|
});
|
|
69
83
|
return res.data.nonce;
|
|
@@ -87,7 +101,7 @@ class AccountApi extends ApiGroup {
|
|
|
87
101
|
const pwd = toUtf8Bytes(username.toLowerCase() + password);
|
|
88
102
|
const salt = toUtf8Bytes('sogni-salt-value');
|
|
89
103
|
const pkey = pbkdf2(pwd, salt, 10000, 32, 'sha256');
|
|
90
|
-
return new Wallet(pkey
|
|
104
|
+
return new Wallet(pkey);
|
|
91
105
|
}
|
|
92
106
|
|
|
93
107
|
/**
|
|
@@ -113,7 +127,7 @@ class AccountApi extends ApiGroup {
|
|
|
113
127
|
turnstileToken
|
|
114
128
|
};
|
|
115
129
|
const signature = await this.eip712.signTypedData(wallet, 'signup', { ...payload, nonce });
|
|
116
|
-
const res = await this.client.rest.post<
|
|
130
|
+
const res = await this.client.rest.post<ApiResponse<AccountCreateData>>('/v1/account/create', {
|
|
117
131
|
...payload,
|
|
118
132
|
referralCode,
|
|
119
133
|
signature
|
|
@@ -176,7 +190,7 @@ class AccountApi extends ApiGroup {
|
|
|
176
190
|
walletAddress: wallet.address,
|
|
177
191
|
nonce
|
|
178
192
|
});
|
|
179
|
-
const res = await this.client.rest.post<
|
|
193
|
+
const res = await this.client.rest.post<ApiResponse<LoginData>>('/v1/account/login', {
|
|
180
194
|
walletAddress: wallet.address,
|
|
181
195
|
signature
|
|
182
196
|
});
|
|
@@ -205,18 +219,33 @@ class AccountApi extends ApiGroup {
|
|
|
205
219
|
* Refresh the balance of the current account.
|
|
206
220
|
*
|
|
207
221
|
* Usually, you don't need to call this method manually. Balance is updated automatically
|
|
208
|
-
* through WebSocket events. But you can call this method to force a balance refresh.
|
|
222
|
+
* through WebSocket events. But you can call this method to force a balance refresh. Note that
|
|
223
|
+
* will also trigger updated event on the current account.
|
|
209
224
|
*
|
|
210
225
|
* @example Refresh user account balance
|
|
211
226
|
* ```typescript
|
|
212
227
|
* const balance = await client.account.refreshBalance();
|
|
213
228
|
* console.log(balance);
|
|
214
|
-
* // { net: '100.000000', settled: '100.000000', credit: '0.000000', debit: '0.000000' }
|
|
215
229
|
* ```
|
|
216
230
|
*/
|
|
217
|
-
async refreshBalance(): Promise<
|
|
218
|
-
const
|
|
219
|
-
this.currentAccount._update({ balance:
|
|
231
|
+
async refreshBalance(): Promise<Balances> {
|
|
232
|
+
const balance = await this.accountBalance();
|
|
233
|
+
this.currentAccount._update({ balance: balance });
|
|
234
|
+
return balance;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Get the account balance of the current account.
|
|
239
|
+
* This method returns the account balance of the current user, including settled, credit, debit, and unclaimed earnings amounts.
|
|
240
|
+
*
|
|
241
|
+
* @example Get the account balance of the current user
|
|
242
|
+
* ```typescript
|
|
243
|
+
* const balance = await client.account.accountBalance();
|
|
244
|
+
* console.log(balance);
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
async accountBalance(): Promise<FullBalances> {
|
|
248
|
+
const res = await this.client.rest.get<ApiResponse<FullBalances>>('/v3/account/balance');
|
|
220
249
|
return res.data;
|
|
221
250
|
}
|
|
222
251
|
|
|
@@ -236,12 +265,11 @@ class AccountApi extends ApiGroup {
|
|
|
236
265
|
* @param walletAddress
|
|
237
266
|
*/
|
|
238
267
|
async walletBalance(walletAddress: string) {
|
|
239
|
-
const res = await this.client.rest.get<
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
);
|
|
268
|
+
const res = await this.client.rest.get<
|
|
269
|
+
ApiResponse<{ sogni: string; spark: string; ether: string }>
|
|
270
|
+
>('/v2/wallet/balance', {
|
|
271
|
+
walletAddress
|
|
272
|
+
});
|
|
245
273
|
return res.data;
|
|
246
274
|
}
|
|
247
275
|
|
|
@@ -252,7 +280,7 @@ class AccountApi extends ApiGroup {
|
|
|
252
280
|
*/
|
|
253
281
|
async validateUsername(username: string) {
|
|
254
282
|
try {
|
|
255
|
-
return await this.client.rest.post<
|
|
283
|
+
return await this.client.rest.post<ApiResponse<undefined>>('/v1/account/username/validate', {
|
|
256
284
|
username
|
|
257
285
|
});
|
|
258
286
|
} catch (e) {
|
|
@@ -310,11 +338,21 @@ class AccountApi extends ApiGroup {
|
|
|
310
338
|
async transactionHistory(
|
|
311
339
|
params: TxHistoryParams
|
|
312
340
|
): Promise<{ entries: TxHistoryEntry[]; next: TxHistoryParams }> {
|
|
313
|
-
const
|
|
341
|
+
const query: Record<string, string> = {
|
|
314
342
|
status: params.status,
|
|
315
343
|
address: params.address,
|
|
316
344
|
limit: params.limit.toString()
|
|
317
|
-
}
|
|
345
|
+
};
|
|
346
|
+
if (params.offset) {
|
|
347
|
+
query.offset = params.offset.toString();
|
|
348
|
+
}
|
|
349
|
+
if (params.provider) {
|
|
350
|
+
query.provider = params.provider;
|
|
351
|
+
}
|
|
352
|
+
const res = await this.client.rest.get<ApiResponse<TxHistoryData>>(
|
|
353
|
+
'/v1/transactions/list',
|
|
354
|
+
query
|
|
355
|
+
);
|
|
318
356
|
|
|
319
357
|
return {
|
|
320
358
|
entries: res.data.transactions.map(
|
|
@@ -326,6 +364,7 @@ class AccountApi extends ApiGroup {
|
|
|
326
364
|
status: tx.status,
|
|
327
365
|
role: tx.role,
|
|
328
366
|
amount: tx.amount,
|
|
367
|
+
tokenType: tx.tokenType,
|
|
329
368
|
description: tx.description,
|
|
330
369
|
source: tx.source,
|
|
331
370
|
endTime: new Date(tx.endTime),
|
|
@@ -343,9 +382,11 @@ class AccountApi extends ApiGroup {
|
|
|
343
382
|
* Get the rewards of the current account.
|
|
344
383
|
* @internal
|
|
345
384
|
*/
|
|
346
|
-
async rewards(): Promise<Reward[]> {
|
|
347
|
-
const r =
|
|
348
|
-
|
|
385
|
+
async rewards(query: RewardsQuery = {}): Promise<Reward[]> {
|
|
386
|
+
const r = await this.client.rest.get<ApiResponse<{ rewards: RewardRaw[] }>>(
|
|
387
|
+
'/v4/account/rewards',
|
|
388
|
+
query
|
|
389
|
+
);
|
|
349
390
|
|
|
350
391
|
return r.data.rewards.map(
|
|
351
392
|
(raw: RewardRaw): Reward => ({
|
|
@@ -354,9 +395,11 @@ class AccountApi extends ApiGroup {
|
|
|
354
395
|
title: raw.title,
|
|
355
396
|
description: raw.description,
|
|
356
397
|
amount: raw.amount,
|
|
398
|
+
tokenType: raw.tokenType,
|
|
357
399
|
claimed: !!raw.claimed,
|
|
358
400
|
canClaim: !!raw.canClaim,
|
|
359
401
|
lastClaim: new Date(raw.lastClaimTimestamp * 1000),
|
|
402
|
+
provider: query.provider || 'base',
|
|
360
403
|
nextClaim:
|
|
361
404
|
raw.lastClaimTimestamp && raw.claimResetFrequencySec > -1
|
|
362
405
|
? new Date(raw.lastClaimTimestamp * 1000 + raw.claimResetFrequencySec * 1000)
|
|
@@ -369,11 +412,22 @@ class AccountApi extends ApiGroup {
|
|
|
369
412
|
* Claim rewards by reward IDs.
|
|
370
413
|
* @internal
|
|
371
414
|
* @param rewardIds
|
|
415
|
+
* @param options - Options for claiming rewards
|
|
416
|
+
* @param options.turnstileToken - Turnstile token for anti-bot protection
|
|
417
|
+
* @param options.provider - Provider name for the rewards
|
|
372
418
|
*/
|
|
373
|
-
async claimRewards(
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
419
|
+
async claimRewards(
|
|
420
|
+
rewardIds: string[],
|
|
421
|
+
{ turnstileToken, provider }: ClaimOptions = {}
|
|
422
|
+
): Promise<void> {
|
|
423
|
+
const payload: Record<string, any> = {
|
|
424
|
+
claims: rewardIds,
|
|
425
|
+
provider: provider || 'base'
|
|
426
|
+
};
|
|
427
|
+
if (turnstileToken) {
|
|
428
|
+
payload.turnstileToken = turnstileToken;
|
|
429
|
+
}
|
|
430
|
+
await this.client.rest.post('/v3/account/reward/claim', payload);
|
|
377
431
|
}
|
|
378
432
|
|
|
379
433
|
/**
|
|
@@ -394,6 +448,13 @@ class AccountApi extends ApiGroup {
|
|
|
394
448
|
walletAddress: walletAddress,
|
|
395
449
|
amount: parseEther(amount.toString()).toString()
|
|
396
450
|
};
|
|
451
|
+
if (walletAddress !== this.currentAccount.walletAddress) {
|
|
452
|
+
throw new ApiError(400, {
|
|
453
|
+
status: 'error',
|
|
454
|
+
message: 'Incorrect password',
|
|
455
|
+
errorCode: 0
|
|
456
|
+
});
|
|
457
|
+
}
|
|
397
458
|
const signature = await this.eip712.signTypedData(wallet, 'withdraw', { ...payload, nonce });
|
|
398
459
|
await this.client.rest.post('/v1/account/token/withdraw', {
|
|
399
460
|
...payload,
|
|
@@ -412,34 +473,79 @@ class AccountApi extends ApiGroup {
|
|
|
412
473
|
* @param amount - amount to transfer
|
|
413
474
|
*/
|
|
414
475
|
async deposit(password: string, amount: number): Promise<void> {
|
|
476
|
+
return this._deposit(password, amount, 1);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
private async _deposit(
|
|
480
|
+
password: string,
|
|
481
|
+
amount: number,
|
|
482
|
+
attemptCount: number = 1
|
|
483
|
+
): Promise<void> {
|
|
415
484
|
const wallet = this.getWallet(this.currentAccount.username!, password);
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
485
|
+
if (wallet.address !== this.currentAccount.walletAddress) {
|
|
486
|
+
throw new ApiError(400, {
|
|
487
|
+
status: 'error',
|
|
488
|
+
message: 'Incorrect password',
|
|
489
|
+
errorCode: 0
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
try {
|
|
493
|
+
await this.client.rest.post('/v3/account/token/deposit', {
|
|
494
|
+
walletAddress: wallet.address,
|
|
495
|
+
amount: parseEther(amount.toString()).toString(),
|
|
496
|
+
provider: 'base'
|
|
497
|
+
});
|
|
498
|
+
} catch (error) {
|
|
499
|
+
if (error instanceof ApiError) {
|
|
500
|
+
if (error.payload.errorCode === ErrorCode.INSUFFICIENT_ALLOWANCE) {
|
|
501
|
+
// If this is the first attempt, we need to approve the token usage,
|
|
502
|
+
// otherwise we can retry the deposit directly.
|
|
503
|
+
if (attemptCount === 1) {
|
|
504
|
+
await this.approveTokenUsage(password, 'account');
|
|
505
|
+
}
|
|
506
|
+
if (attemptCount >= MAX_DEPOSIT_ATTEMPTS) {
|
|
507
|
+
throw error;
|
|
508
|
+
}
|
|
509
|
+
await delay(10000); // Wait for the approval transaction to be processed
|
|
510
|
+
await this._deposit(password, amount, attemptCount + 1);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
throw error;
|
|
514
|
+
}
|
|
515
|
+
throw error;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Approve SOGNI token usage for the specified spender.
|
|
521
|
+
* @internal
|
|
522
|
+
*
|
|
523
|
+
* @param password - user account password
|
|
524
|
+
* @param spender - Spender type, either 'account' for deposit or 'staker' for staking contract
|
|
525
|
+
* @param provider - Provider name, defaults to 'base', can be 'base', 'etherlink', etc.
|
|
526
|
+
*/
|
|
527
|
+
async approveTokenUsage(
|
|
528
|
+
password: string,
|
|
529
|
+
spender: 'account' | 'staker',
|
|
530
|
+
provider: string = 'base'
|
|
531
|
+
): Promise<void> {
|
|
532
|
+
const wallet = this.getWallet(this.currentAccount.username!, password);
|
|
533
|
+
const permitR = await this.client.rest.post<{ data: Record<string, any> }>(
|
|
534
|
+
'/v1/contract/token/approve/permit',
|
|
535
|
+
{
|
|
536
|
+
walletAddress: wallet.address,
|
|
537
|
+
spender: spender,
|
|
538
|
+
provider: provider
|
|
539
|
+
}
|
|
425
540
|
);
|
|
426
|
-
const {
|
|
427
|
-
const
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
s: permitSignature.s
|
|
435
|
-
};
|
|
436
|
-
const depositSignature = await this.eip712.signTypedData(wallet, 'deposit', {
|
|
437
|
-
...depositPayload,
|
|
438
|
-
nonce
|
|
439
|
-
});
|
|
440
|
-
await this.client.rest.post('/v1/account/token/deposit', {
|
|
441
|
-
...depositPayload,
|
|
442
|
-
signature: depositSignature
|
|
541
|
+
const { domain, types, message } = permitR.data;
|
|
542
|
+
const signature = await wallet.signTypedData(domain, types, message);
|
|
543
|
+
await this.client.rest.post('/v1/contract/token/approve', {
|
|
544
|
+
walletAddress: wallet.address,
|
|
545
|
+
spender: spender,
|
|
546
|
+
provider: provider,
|
|
547
|
+
deadline: message.deadline,
|
|
548
|
+
approveSignature: signature
|
|
443
549
|
});
|
|
444
550
|
}
|
|
445
551
|
}
|
package/src/Account/types.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { TokenType } from '../types/token';
|
|
2
|
+
|
|
1
3
|
export interface Nonce {
|
|
2
4
|
nonce: string;
|
|
3
5
|
}
|
|
@@ -27,12 +29,22 @@ export interface BalanceData {
|
|
|
27
29
|
credit: string;
|
|
28
30
|
debit: string;
|
|
29
31
|
net: string;
|
|
32
|
+
/**
|
|
33
|
+
* Unclaimed worker earnings amount
|
|
34
|
+
* @experimental Socket messages do not provide this field yet, so it may not be available in all cases.
|
|
35
|
+
*/
|
|
36
|
+
unclaimed?: string;
|
|
30
37
|
}
|
|
31
38
|
|
|
39
|
+
export type Balances = Record<TokenType, BalanceData>;
|
|
40
|
+
|
|
41
|
+
export type FullBalances = Record<TokenType, Required<BalanceData>>;
|
|
42
|
+
|
|
32
43
|
export interface TxHistoryParams {
|
|
33
44
|
status: 'completed';
|
|
34
45
|
address: string;
|
|
35
46
|
limit: number;
|
|
47
|
+
provider?: string;
|
|
36
48
|
offset?: number;
|
|
37
49
|
}
|
|
38
50
|
|
|
@@ -58,6 +70,7 @@ export interface TxRaw {
|
|
|
58
70
|
sourceSID: string;
|
|
59
71
|
endTime: number;
|
|
60
72
|
type: 'debit' | string;
|
|
73
|
+
tokenType: TokenType;
|
|
61
74
|
}
|
|
62
75
|
|
|
63
76
|
export interface TxHistoryEntry {
|
|
@@ -68,6 +81,7 @@ export interface TxHistoryEntry {
|
|
|
68
81
|
status: 'completed';
|
|
69
82
|
role: 'artist' | 'worker';
|
|
70
83
|
amount: number;
|
|
84
|
+
tokenType: TokenType;
|
|
71
85
|
description: string;
|
|
72
86
|
source: 'project' | string;
|
|
73
87
|
endTime: Date;
|
|
@@ -82,20 +96,32 @@ export interface RewardRaw {
|
|
|
82
96
|
title: string;
|
|
83
97
|
description: string;
|
|
84
98
|
amount: string;
|
|
99
|
+
tokenType: TokenType;
|
|
85
100
|
claimed: number;
|
|
86
101
|
canClaim: number;
|
|
87
102
|
lastClaimTimestamp: number;
|
|
88
103
|
claimResetFrequencySec: number;
|
|
89
104
|
}
|
|
90
105
|
|
|
106
|
+
export interface RewardsQuery {
|
|
107
|
+
provider?: string;
|
|
108
|
+
}
|
|
109
|
+
|
|
91
110
|
export interface Reward {
|
|
92
111
|
id: string;
|
|
93
112
|
type: RewardType;
|
|
94
113
|
title: string;
|
|
95
114
|
description: string;
|
|
96
115
|
amount: string;
|
|
116
|
+
tokenType: TokenType;
|
|
97
117
|
claimed: boolean;
|
|
98
118
|
canClaim: boolean;
|
|
99
119
|
lastClaim: Date;
|
|
100
120
|
nextClaim: Date | null;
|
|
121
|
+
provider?: string;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface ClaimOptions {
|
|
125
|
+
turnstileToken?: string;
|
|
126
|
+
provider?: string;
|
|
101
127
|
}
|
|
@@ -1,11 +1,36 @@
|
|
|
1
1
|
import { SupernetType } from './types';
|
|
2
|
+
import { Balances } from '../../Account/types';
|
|
2
3
|
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
export interface AuthenticatedData {
|
|
5
|
+
id: string;
|
|
6
|
+
clientType: 'artist' | 'worker';
|
|
7
|
+
username: string;
|
|
8
|
+
address: string;
|
|
9
|
+
SID: number;
|
|
10
|
+
clientSID: number;
|
|
11
|
+
addressSID: number;
|
|
12
|
+
balanceVersion: 2;
|
|
13
|
+
tokens: {
|
|
14
|
+
sogni: {
|
|
15
|
+
settled: string;
|
|
16
|
+
credit: string;
|
|
17
|
+
debit: string;
|
|
18
|
+
net: string;
|
|
19
|
+
};
|
|
20
|
+
spark: {
|
|
21
|
+
settled: string;
|
|
22
|
+
credit: string;
|
|
23
|
+
debit: string;
|
|
24
|
+
net: string;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
activeProjects: [];
|
|
28
|
+
unclaimedCompletedProjects: [];
|
|
29
|
+
isMainnet: boolean;
|
|
30
|
+
accountWasMigrated: boolean;
|
|
31
|
+
hasUnclaimedAirdrop: boolean;
|
|
32
|
+
firstLoginAfterMigration: boolean;
|
|
33
|
+
}
|
|
9
34
|
|
|
10
35
|
export type JobErrorData = {
|
|
11
36
|
jobID: string;
|
|
@@ -38,6 +63,9 @@ export type JobStateData =
|
|
|
38
63
|
jobID: string;
|
|
39
64
|
imgID: string;
|
|
40
65
|
workerName: string;
|
|
66
|
+
positivePrompt?: string;
|
|
67
|
+
negativePrompt?: string;
|
|
68
|
+
jobIndex?: number;
|
|
41
69
|
}
|
|
42
70
|
| {
|
|
43
71
|
jobID: string;
|
|
@@ -58,11 +86,29 @@ export type ServerDisconnectData = {
|
|
|
58
86
|
reason: string;
|
|
59
87
|
};
|
|
60
88
|
|
|
89
|
+
export type ToastMessage = {
|
|
90
|
+
type: 'info' | 'success' | 'warning' | 'error';
|
|
91
|
+
message: string;
|
|
92
|
+
// Number of milliseconds to show the toast
|
|
93
|
+
autoClose: number;
|
|
94
|
+
stickyID: string;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export type ArtistCancelConfirmation = {
|
|
98
|
+
didCancel: boolean;
|
|
99
|
+
error_message: string;
|
|
100
|
+
jobID: string;
|
|
101
|
+
};
|
|
102
|
+
|
|
61
103
|
export type SocketEventMap = {
|
|
104
|
+
/**
|
|
105
|
+
* @event WebSocketClient#authenticated - Received after successful connection to the WebSocket server
|
|
106
|
+
*/
|
|
107
|
+
authenticated: AuthenticatedData;
|
|
62
108
|
/**
|
|
63
109
|
* @event WebSocketClient#balanceUpdate - Received balance update
|
|
64
110
|
*/
|
|
65
|
-
balanceUpdate:
|
|
111
|
+
balanceUpdate: Balances;
|
|
66
112
|
/**
|
|
67
113
|
* @event WebSocketClient#changeNetwork - Default network changed
|
|
68
114
|
*/
|
|
@@ -95,4 +141,10 @@ export type SocketEventMap = {
|
|
|
95
141
|
* @event WebSocketClient#disconnected - WebSocket connection was closed
|
|
96
142
|
*/
|
|
97
143
|
disconnected: ServerDisconnectData;
|
|
144
|
+
/**
|
|
145
|
+
* @event WebSocketClient#toastMessage - Toast message received
|
|
146
|
+
*/
|
|
147
|
+
toastMessage: ToastMessage;
|
|
148
|
+
|
|
149
|
+
artistCancelConfirmation: ArtistCancelConfirmation;
|
|
98
150
|
};
|
|
@@ -9,7 +9,7 @@ import { LIB_VERSION } from '../../version';
|
|
|
9
9
|
import { Logger } from '../../lib/DefaultLogger';
|
|
10
10
|
import AuthManager from '../../lib/AuthManager';
|
|
11
11
|
|
|
12
|
-
const PROTOCOL_VERSION = '0.
|
|
12
|
+
const PROTOCOL_VERSION = '3.0.0';
|
|
13
13
|
|
|
14
14
|
const PING_INTERVAL = 15000;
|
|
15
15
|
|
package/src/ApiClient/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ import AuthManager, { Tokens } from '../lib/AuthManager';
|
|
|
11
11
|
|
|
12
12
|
const WS_RECONNECT_ATTEMPTS = 5;
|
|
13
13
|
|
|
14
|
-
export interface
|
|
14
|
+
export interface ApiResponse<D = JSONValue> {
|
|
15
15
|
status: 'success';
|
|
16
16
|
data: D;
|
|
17
17
|
}
|
|
@@ -40,13 +40,15 @@ class ApiClient extends TypedEventEmitter<ApiClientEvents> {
|
|
|
40
40
|
private _socket: WebSocketClient;
|
|
41
41
|
private _auth: AuthManager;
|
|
42
42
|
private _reconnectAttempts = WS_RECONNECT_ATTEMPTS;
|
|
43
|
+
private _disableSocket: boolean = false;
|
|
43
44
|
|
|
44
45
|
constructor(
|
|
45
46
|
baseUrl: string,
|
|
46
47
|
socketUrl: string,
|
|
47
48
|
appId: string,
|
|
48
49
|
networkType: SupernetType,
|
|
49
|
-
logger: Logger
|
|
50
|
+
logger: Logger,
|
|
51
|
+
disableSocket: boolean = false
|
|
50
52
|
) {
|
|
51
53
|
super();
|
|
52
54
|
this.appId = appId;
|
|
@@ -54,6 +56,7 @@ class ApiClient extends TypedEventEmitter<ApiClientEvents> {
|
|
|
54
56
|
this._auth = new AuthManager(baseUrl, logger);
|
|
55
57
|
this._rest = new RestClient(baseUrl, this._auth, logger);
|
|
56
58
|
this._socket = new WebSocketClient(socketUrl, this._auth, appId, networkType, logger);
|
|
59
|
+
this._disableSocket = disableSocket;
|
|
57
60
|
|
|
58
61
|
this._auth.on('refreshFailed', this.handleRefreshFailed.bind(this));
|
|
59
62
|
this._socket.on('connected', this.handleSocketConnect.bind(this));
|
|
@@ -76,14 +79,22 @@ class ApiClient extends TypedEventEmitter<ApiClientEvents> {
|
|
|
76
79
|
return this._rest;
|
|
77
80
|
}
|
|
78
81
|
|
|
82
|
+
get socketEnabled(): boolean {
|
|
83
|
+
return !this._disableSocket;
|
|
84
|
+
}
|
|
85
|
+
|
|
79
86
|
async authenticate(tokens: Tokens) {
|
|
80
87
|
await this.auth.setTokens(tokens);
|
|
81
|
-
|
|
88
|
+
if (!this._disableSocket) {
|
|
89
|
+
await this.socket.connect();
|
|
90
|
+
}
|
|
82
91
|
}
|
|
83
92
|
|
|
84
93
|
removeAuth() {
|
|
85
94
|
this.auth.clear();
|
|
86
|
-
this.socket.
|
|
95
|
+
if (this.socket.isConnected) {
|
|
96
|
+
this.socket.disconnect();
|
|
97
|
+
}
|
|
87
98
|
}
|
|
88
99
|
|
|
89
100
|
handleSocketConnect({ network }: ServerConnectData) {
|
package/src/ApiGroup.ts
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
|
-
import { AbstractProvider } from 'ethers';
|
|
2
1
|
import ApiClient from './ApiClient';
|
|
3
2
|
import EIP712Helper from './lib/EIP712Helper';
|
|
4
3
|
import TypedEventEmitter, { EventMap } from './lib/TypedEventEmitter';
|
|
5
4
|
|
|
6
5
|
export interface ApiConfig {
|
|
7
6
|
client: ApiClient;
|
|
8
|
-
provider: AbstractProvider;
|
|
9
7
|
eip712: EIP712Helper;
|
|
10
8
|
}
|
|
11
9
|
|
|
12
10
|
abstract class ApiGroup<E extends EventMap = {}> extends TypedEventEmitter<E> {
|
|
13
11
|
protected client: ApiClient;
|
|
14
|
-
protected provider: AbstractProvider;
|
|
15
12
|
protected eip712: EIP712Helper;
|
|
16
13
|
|
|
17
14
|
constructor(config: ApiConfig) {
|
|
18
15
|
super();
|
|
19
16
|
this.client = config.client;
|
|
20
|
-
this.provider = config.provider;
|
|
21
17
|
this.eip712 = config.eip712;
|
|
22
18
|
}
|
|
23
19
|
}
|