@sogni-ai/sogni-client 3.0.0-alpha.8 → 3.0.0
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 +335 -0
- package/README.md +4 -11
- 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 +42 -13
- package/dist/Account/index.js +132 -29
- 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 +9 -18
- 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 +48 -10
- 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/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 +163 -59
- 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 +11 -19
- package/src/Projects/createJobRequestMessage.ts +4 -2
- package/src/Projects/index.ts +53 -13
- 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/index.ts +23 -19
- package/src/lib/utils.ts +4 -0
- package/src/types/token.ts +1 -0
- package/src/version.ts +2 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { SupernetType } from '../../ApiClient/WebSocketClient/types';
|
|
2
2
|
import { ControlNetParams } from './ControlNetParams';
|
|
3
|
+
import { TokenType } from '../../types/token';
|
|
3
4
|
export interface SupportedModel {
|
|
4
5
|
id: string;
|
|
5
6
|
name: string;
|
|
@@ -90,12 +91,14 @@ export interface ProjectParams {
|
|
|
90
91
|
*/
|
|
91
92
|
numberOfImages: number;
|
|
92
93
|
/**
|
|
93
|
-
* Generate images based on starting image.
|
|
94
|
+
* Generate images based on the starting image.
|
|
95
|
+
* Supported types:
|
|
94
96
|
* `File` - file object from input[type=file]
|
|
95
|
-
* `Buffer` - buffer object with image data
|
|
97
|
+
* `Buffer` - Node.js buffer object with image data
|
|
96
98
|
* `Blob` - blob object with image data
|
|
99
|
+
* `true` - indicates that the image is already uploaded to the server
|
|
97
100
|
*/
|
|
98
|
-
startingImage?: File | Buffer | Blob;
|
|
101
|
+
startingImage?: File | Buffer | Blob | boolean;
|
|
99
102
|
/**
|
|
100
103
|
* How strong effect of starting image should be. From 0 to 1, default 0.5
|
|
101
104
|
*/
|
|
@@ -129,6 +132,11 @@ export interface ProjectParams {
|
|
|
129
132
|
* ControlNet model parameters
|
|
130
133
|
*/
|
|
131
134
|
controlNet?: ControlNetParams;
|
|
135
|
+
/**
|
|
136
|
+
* Select which tokens to use for the project.
|
|
137
|
+
* If not specified, the Sogni token will be used.
|
|
138
|
+
*/
|
|
139
|
+
tokenType?: TokenType;
|
|
132
140
|
}
|
|
133
141
|
export type ImageUrlParams = {
|
|
134
142
|
imageId: string;
|
|
@@ -141,6 +149,10 @@ export interface EstimateRequest {
|
|
|
141
149
|
* Network to use. Can be 'fast' or 'relaxed'
|
|
142
150
|
*/
|
|
143
151
|
network: SupernetType;
|
|
152
|
+
/**
|
|
153
|
+
* Token type
|
|
154
|
+
*/
|
|
155
|
+
tokenType?: TokenType;
|
|
144
156
|
/**
|
|
145
157
|
* Model ID
|
|
146
158
|
*/
|
|
@@ -180,3 +192,4 @@ export interface EstimateRequest {
|
|
|
180
192
|
*/
|
|
181
193
|
height?: number;
|
|
182
194
|
}
|
|
195
|
+
export type EnhancementStrength = 'light' | 'medium' | 'heavy';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEnhacementStrength = getEnhacementStrength;
|
|
4
|
+
function getEnhacementStrength(strength) {
|
|
5
|
+
switch (strength) {
|
|
6
|
+
case 'light':
|
|
7
|
+
return 0.15;
|
|
8
|
+
case 'heavy':
|
|
9
|
+
return 0.49;
|
|
10
|
+
default:
|
|
11
|
+
return 0.35;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/Projects/utils.ts"],"names":[],"mappings":";;AAEA,sDASC;AATD,SAAgB,qBAAqB,CAAC,QAA6B;IACjE,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,OAAO;YACV,OAAO,IAAI,CAAC;QACd,KAAK,OAAO;YACV,OAAO,IAAI,CAAC;QACd;YACE,OAAO,IAAI,CAAC;IAChB,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,7 +9,8 @@ import Project, { ProjectStatus } from './Projects/Project';
|
|
|
9
9
|
import { AvailableModel, ProjectParams, Scheduler, TimeStepSpacing } from './Projects/types';
|
|
10
10
|
import StatsApi from './Stats';
|
|
11
11
|
import ErrorData from './types/ErrorData';
|
|
12
|
-
|
|
12
|
+
import { TokenType } from './types/token';
|
|
13
|
+
export type { AvailableModel, ErrorData, JobStatus, Logger, LogLevel, ProjectParams, ProjectStatus, Scheduler, SupernetType, TimeStepSpacing, TokenType };
|
|
13
14
|
export { ApiError, CurrentAccount, Job, Project };
|
|
14
15
|
export interface SogniClientConfig {
|
|
15
16
|
/**
|
|
@@ -26,6 +27,13 @@ export interface SogniClientConfig {
|
|
|
26
27
|
* @internal
|
|
27
28
|
*/
|
|
28
29
|
socketEndpoint?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Disable WebSocket connection. Useful for testing or when WebSocket is not needed.
|
|
32
|
+
* Note that many may not work without WebSocket connection.
|
|
33
|
+
* @experimental
|
|
34
|
+
* @internal
|
|
35
|
+
*/
|
|
36
|
+
disableSocket?: boolean;
|
|
29
37
|
/**
|
|
30
38
|
* Which network to use after logging in. Can be 'fast' or 'relaxed'
|
|
31
39
|
*/
|
|
@@ -39,10 +47,6 @@ export interface SogniClientConfig {
|
|
|
39
47
|
* @default 'warn'
|
|
40
48
|
**/
|
|
41
49
|
logLevel?: LogLevel;
|
|
42
|
-
/**
|
|
43
|
-
* If provided, the client will connect to this JSON-RPC endpoint to interact with the blockchain
|
|
44
|
-
*/
|
|
45
|
-
jsonRpcUrl?: string;
|
|
46
50
|
/**
|
|
47
51
|
* If true, the client will connect to the testnet. Ignored if jsonRpcUrl is provided
|
|
48
52
|
*/
|
|
@@ -56,7 +60,7 @@ export declare class SogniClient {
|
|
|
56
60
|
private constructor();
|
|
57
61
|
get currentAccount(): CurrentAccount;
|
|
58
62
|
/**
|
|
59
|
-
*
|
|
63
|
+
* Create client instance, with default configuration
|
|
60
64
|
* @param config
|
|
61
65
|
*/
|
|
62
66
|
static createInstance(config: SogniClientConfig): Promise<SogniClient>;
|
package/dist/index.js
CHANGED
|
@@ -36,7 +36,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
};
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
38
|
exports.SogniClient = exports.Project = exports.Job = exports.CurrentAccount = exports.ApiError = void 0;
|
|
39
|
-
const ethers_1 = require("ethers");
|
|
40
39
|
// Account API
|
|
41
40
|
const Account_1 = __importDefault(require("./Account"));
|
|
42
41
|
const CurrentAccount_1 = __importDefault(require("./Account/CurrentAccount"));
|
|
@@ -66,7 +65,7 @@ class SogniClient {
|
|
|
66
65
|
return this.account.currentAccount;
|
|
67
66
|
}
|
|
68
67
|
/**
|
|
69
|
-
*
|
|
68
|
+
* Create client instance, with default configuration
|
|
70
69
|
* @param config
|
|
71
70
|
*/
|
|
72
71
|
static createInstance(config) {
|
|
@@ -75,22 +74,14 @@ class SogniClient {
|
|
|
75
74
|
const socketEndpoint = config.socketEndpoint || 'wss://socket.sogni.ai';
|
|
76
75
|
const network = config.network || 'fast';
|
|
77
76
|
const logger = config.logger || new DefaultLogger_1.DefaultLogger(config.logLevel || 'warn');
|
|
78
|
-
const isTestnet = config.testnet !== undefined ? config.testnet :
|
|
79
|
-
const client = new ApiClient_1.default(restEndpoint, socketEndpoint, config.appId, network, logger);
|
|
80
|
-
let provider;
|
|
81
|
-
if ('jsonRpcUrl' in config) {
|
|
82
|
-
provider = new ethers_1.JsonRpcProvider(config.jsonRpcUrl);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
provider = (0, ethers_1.getDefaultProvider)(isTestnet ? 84532 : 8453);
|
|
86
|
-
}
|
|
87
|
-
const chainId = yield provider.getNetwork().then((network) => network.chainId);
|
|
77
|
+
const isTestnet = config.testnet !== undefined ? config.testnet : false;
|
|
78
|
+
const client = new ApiClient_1.default(restEndpoint, socketEndpoint, config.appId, network, logger, config.disableSocket);
|
|
88
79
|
const eip712 = new EIP712Helper_1.default({
|
|
89
|
-
name: 'Sogni-testnet',
|
|
80
|
+
name: isTestnet ? 'Sogni-testnet' : 'Sogni AI',
|
|
90
81
|
version: '1',
|
|
91
|
-
chainId:
|
|
82
|
+
chainId: isTestnet ? '84532' : '8453'
|
|
92
83
|
});
|
|
93
|
-
return new SogniClient({ client,
|
|
84
|
+
return new SogniClient({ client, eip712 });
|
|
94
85
|
});
|
|
95
86
|
}
|
|
96
87
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,cAAc;AACd,wDAAmC;AACnC,8EAAsD;AAiCnC,yBAjCZ,wBAAc,CAiCY;AAhCjC,YAAY;AACZ,yDAAkD;AA+BzC,yFA/BW,oBAAQ,OA+BX;AA5BjB,QAAQ;AACR,uDAAsE;AACtE,sEAA8C;AAC9C,eAAe;AACf,0DAAqC;AACrC,yDAAgD;AAuBb,cAvB5B,aAAG,CAuB4B;AAtBtC,iEAA4D;AAsBpB,kBAtBjC,iBAAO,CAsBiC;AApB/C,YAAY;AACZ,oDAA+B;AA8D/B,MAAa,WAAW;IAOtB,YAAoB,MAAiB;QACnC,IAAI,CAAC,OAAO,GAAG,IAAI,iBAAU,CAAC,MAAM,CAAC,CAAC;QACtC,IAAI,CAAC,QAAQ,GAAG,IAAI,kBAAW,CAAC,MAAM,CAAC,CAAC;QACxC,IAAI,CAAC,KAAK,GAAG,IAAI,eAAQ,CAAC,MAAM,CAAC,CAAC;QAElC,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC;IACjC,CAAC;IAED,IAAI,cAAc;QAChB,OAAO,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC;IACrC,CAAC;IAED;;;OAGG;IACH,MAAM,CAAO,cAAc,CAAC,MAAyB;;YACnD,MAAM,YAAY,GAAG,MAAM,CAAC,YAAY,IAAI,sBAAsB,CAAC;YACnE,MAAM,cAAc,GAAG,MAAM,CAAC,cAAc,IAAI,uBAAuB,CAAC;YACxE,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC;YACzC,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,IAAI,6BAAa,CAAC,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,CAAC;YAC7E,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC;YAExE,MAAM,MAAM,GAAG,IAAI,mBAAS,CAC1B,YAAY,EACZ,cAAc,EACd,MAAM,CAAC,KAAK,EACZ,OAAO,EACP,MAAM,EACN,MAAM,CAAC,aAAa,CACrB,CAAC;YACF,MAAM,MAAM,GAAG,IAAI,sBAAY,CAAC;gBAC9B,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,UAAU;gBAC9C,OAAO,EAAE,GAAG;gBACZ,OAAO,EAAE,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM;aACtC,CAAC,CAAC;YACH,OAAO,IAAI,WAAW,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;QAC7C,CAAC;KAAA;CACF;AA7CD,kCA6CC"}
|
package/dist/lib/utils.d.ts
CHANGED
package/dist/lib/utils.js
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.decodeToken = decodeToken;
|
|
4
13
|
exports.decodeRefreshToken = decodeRefreshToken;
|
|
14
|
+
exports.delay = delay;
|
|
5
15
|
const jwt_decode_1 = require("jwt-decode");
|
|
6
16
|
function decodeToken(token) {
|
|
7
17
|
const data = (0, jwt_decode_1.jwtDecode)(token);
|
|
@@ -17,4 +27,9 @@ function decodeRefreshToken(token) {
|
|
|
17
27
|
expiresAt: new Date(data.exp * 1000)
|
|
18
28
|
};
|
|
19
29
|
}
|
|
30
|
+
function delay(ms) {
|
|
31
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
33
|
+
});
|
|
34
|
+
}
|
|
20
35
|
//# sourceMappingURL=utils.js.map
|
package/dist/lib/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/lib/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/lib/utils.ts"],"names":[],"mappings":";;;;;;;;;;;AAEA,kCAMC;AAED,gDAMC;AAED,sBAEC;AApBD,2CAAuC;AAEvC,SAAgB,WAAW,CAAC,KAAa;IACvC,MAAM,IAAI,GAAG,IAAA,sBAAS,EAA0D,KAAK,CAAC,CAAC;IACvF,OAAO;QACL,aAAa,EAAE,IAAI,CAAC,IAAI;QACxB,SAAS,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC;KACrC,CAAC;AACJ,CAAC;AAED,SAAgB,kBAAkB,CAAC,KAAa;IAC9C,MAAM,IAAI,GAAG,IAAA,sBAAS,EAA0D,KAAK,CAAC,CAAC;IACvF,OAAO;QACL,GAAG,EAAE,IAAI,CAAC,GAAG;QACb,SAAS,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC;KACrC,CAAC;AACJ,CAAC;AAED,SAAsB,KAAK,CAAC,EAAU;;QACpC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;IAC3D,CAAC;CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type TokenType = 'sogni' | 'spark';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../src/types/token.ts"],"names":[],"mappings":""}
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const LIB_VERSION
|
|
1
|
+
export declare const LIB_VERSION: string;
|
package/dist/version.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LIB_VERSION = void 0;
|
|
4
|
-
|
|
4
|
+
const package_json_1 = require("../package.json");
|
|
5
|
+
exports.LIB_VERSION = package_json_1.version;
|
|
5
6
|
//# sourceMappingURL=version.js.map
|
package/dist/version.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":";;;AAAA,kDAA0C;AAC7B,QAAA,WAAW,GAAG,sBAAO,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import DataEntity from '../lib/DataEntity';
|
|
2
|
-
import {
|
|
2
|
+
import { Balances } from './types';
|
|
3
3
|
import { SupernetType } from '../ApiClient/WebSocketClient/types';
|
|
4
4
|
/**
|
|
5
5
|
* @inline
|
|
@@ -16,7 +16,7 @@ export interface AccountData {
|
|
|
16
16
|
*/
|
|
17
17
|
networkStatus: 'connected' | 'disconnected' | 'connecting' | 'switching';
|
|
18
18
|
network: SupernetType | null;
|
|
19
|
-
balance:
|
|
19
|
+
balance: Balances;
|
|
20
20
|
walletAddress?: string;
|
|
21
21
|
username?: string;
|
|
22
22
|
token?: string;
|
|
@@ -28,10 +28,18 @@ function getDefaults(): AccountData {
|
|
|
28
28
|
networkStatus: 'disconnected',
|
|
29
29
|
network: null,
|
|
30
30
|
balance: {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
sogni: {
|
|
32
|
+
credit: '0',
|
|
33
|
+
debit: '0',
|
|
34
|
+
net: '0',
|
|
35
|
+
settled: '0'
|
|
36
|
+
},
|
|
37
|
+
spark: {
|
|
38
|
+
credit: '0',
|
|
39
|
+
debit: '0',
|
|
40
|
+
net: '0',
|
|
41
|
+
settled: '0'
|
|
42
|
+
}
|
|
35
43
|
},
|
|
36
44
|
walletAddress: undefined,
|
|
37
45
|
username: undefined,
|
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,13 +412,22 @@ class AccountApi extends ApiGroup {
|
|
|
369
412
|
* Claim rewards by reward IDs.
|
|
370
413
|
* @internal
|
|
371
414
|
* @param rewardIds
|
|
372
|
-
* @param
|
|
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
|
|
373
418
|
*/
|
|
374
|
-
async claimRewards(
|
|
375
|
-
|
|
419
|
+
async claimRewards(
|
|
420
|
+
rewardIds: string[],
|
|
421
|
+
{ turnstileToken, provider }: ClaimOptions = {}
|
|
422
|
+
): Promise<void> {
|
|
423
|
+
const payload: Record<string, any> = {
|
|
376
424
|
claims: rewardIds,
|
|
377
|
-
|
|
378
|
-
}
|
|
425
|
+
provider: provider || 'base'
|
|
426
|
+
};
|
|
427
|
+
if (turnstileToken) {
|
|
428
|
+
payload.turnstileToken = turnstileToken;
|
|
429
|
+
}
|
|
430
|
+
await this.client.rest.post('/v3/account/reward/claim', payload);
|
|
379
431
|
}
|
|
380
432
|
|
|
381
433
|
/**
|
|
@@ -388,7 +440,7 @@ class AccountApi extends ApiGroup {
|
|
|
388
440
|
* @param password - account password
|
|
389
441
|
* @param amount - amount of tokens to withdraw from account to wallet
|
|
390
442
|
*/
|
|
391
|
-
async withdraw(password: string, amount: number): Promise<void> {
|
|
443
|
+
async withdraw(password: string, amount: number | string): Promise<void> {
|
|
392
444
|
const wallet = this.getWallet(this.currentAccount.username!, password);
|
|
393
445
|
const walletAddress = wallet.address;
|
|
394
446
|
const nonce = await this.getNonce(walletAddress);
|
|
@@ -396,6 +448,13 @@ class AccountApi extends ApiGroup {
|
|
|
396
448
|
walletAddress: walletAddress,
|
|
397
449
|
amount: parseEther(amount.toString()).toString()
|
|
398
450
|
};
|
|
451
|
+
if (walletAddress !== this.currentAccount.walletAddress) {
|
|
452
|
+
throw new ApiError(400, {
|
|
453
|
+
status: 'error',
|
|
454
|
+
message: 'Incorrect password',
|
|
455
|
+
errorCode: 0
|
|
456
|
+
});
|
|
457
|
+
}
|
|
399
458
|
const signature = await this.eip712.signTypedData(wallet, 'withdraw', { ...payload, nonce });
|
|
400
459
|
await this.client.rest.post('/v1/account/token/withdraw', {
|
|
401
460
|
...payload,
|
|
@@ -413,35 +472,80 @@ class AccountApi extends ApiGroup {
|
|
|
413
472
|
* @param password - account password
|
|
414
473
|
* @param amount - amount to transfer
|
|
415
474
|
*/
|
|
416
|
-
async deposit(password: string, amount: number): Promise<void> {
|
|
475
|
+
async deposit(password: string, amount: number | string): Promise<void> {
|
|
476
|
+
return this._deposit(password, amount, 1);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
private async _deposit(
|
|
480
|
+
password: string,
|
|
481
|
+
amount: number | string,
|
|
482
|
+
attemptCount: number = 1
|
|
483
|
+
): Promise<void> {
|
|
417
484
|
const wallet = this.getWallet(this.currentAccount.username!, password);
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
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
|
+
}
|
|
427
540
|
);
|
|
428
|
-
const {
|
|
429
|
-
const
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
s: permitSignature.s
|
|
437
|
-
};
|
|
438
|
-
const depositSignature = await this.eip712.signTypedData(wallet, 'deposit', {
|
|
439
|
-
...depositPayload,
|
|
440
|
-
nonce
|
|
441
|
-
});
|
|
442
|
-
await this.client.rest.post('/v1/account/token/deposit', {
|
|
443
|
-
...depositPayload,
|
|
444
|
-
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
|
|
445
549
|
});
|
|
446
550
|
}
|
|
447
551
|
}
|