@zyfai/sdk 0.1.11 → 0.1.13
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/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +48 -13
- package/dist/index.mjs +48 -13
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -416,6 +416,7 @@ declare class ZyfaiSDK {
|
|
|
416
416
|
private hasActiveSessionKey;
|
|
417
417
|
private environment;
|
|
418
418
|
private currentProvider;
|
|
419
|
+
private currentChainId;
|
|
419
420
|
constructor(config: SDKConfig | string);
|
|
420
421
|
/**
|
|
421
422
|
* Authenticate user with SIWE (Sign-In with Ethereum) & JWT token
|
package/dist/index.d.ts
CHANGED
|
@@ -416,6 +416,7 @@ declare class ZyfaiSDK {
|
|
|
416
416
|
private hasActiveSessionKey;
|
|
417
417
|
private environment;
|
|
418
418
|
private currentProvider;
|
|
419
|
+
private currentChainId;
|
|
419
420
|
constructor(config: SDKConfig | string);
|
|
420
421
|
/**
|
|
421
422
|
* Authenticate user with SIWE (Sign-In with Ethereum) & JWT token
|
package/dist/index.js
CHANGED
|
@@ -76,7 +76,7 @@ var ENDPOINTS = {
|
|
|
76
76
|
};
|
|
77
77
|
var DATA_ENDPOINTS = {
|
|
78
78
|
// User Initialization
|
|
79
|
-
USER_INITIALIZE: "/earnings/initialize",
|
|
79
|
+
USER_INITIALIZE: "/api/earnings/initialize",
|
|
80
80
|
// Earnings
|
|
81
81
|
ONCHAIN_EARNINGS: (walletAddress) => `/usercheck/onchain-earnings?walletAddress=${walletAddress}`,
|
|
82
82
|
CALCULATE_ONCHAIN_EARNINGS: "/usercheck/calculate-onchain-earnings",
|
|
@@ -110,6 +110,7 @@ var HttpClient = class {
|
|
|
110
110
|
this.authToken = null;
|
|
111
111
|
this.apiKey = apiKey;
|
|
112
112
|
this.dataApiKey = dataApiKey || apiKey;
|
|
113
|
+
this.environment = environment;
|
|
113
114
|
const endpoint = API_ENDPOINTS[environment];
|
|
114
115
|
const parsedUrl = new URL(endpoint);
|
|
115
116
|
this.origin = parsedUrl.origin;
|
|
@@ -214,6 +215,31 @@ var HttpClient = class {
|
|
|
214
215
|
const response = await this.dataClient.post(url, data, config);
|
|
215
216
|
return response.data;
|
|
216
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Make a POST request to Data API with a custom path (bypasses /api/v2 baseURL)
|
|
220
|
+
* Useful for endpoints that don't follow the /api/v2 pattern
|
|
221
|
+
*
|
|
222
|
+
* @param path - API path (e.g., "/api/earnings/initialize")
|
|
223
|
+
* @param data - Request body
|
|
224
|
+
* @param config - Additional axios config
|
|
225
|
+
*/
|
|
226
|
+
async dataPostCustom(path, data, config) {
|
|
227
|
+
const fullUrl = `${DATA_API_ENDPOINTS[this.environment]}${path}`;
|
|
228
|
+
const headers = {
|
|
229
|
+
"Content-Type": "application/json",
|
|
230
|
+
"X-API-Key": this.dataApiKey,
|
|
231
|
+
...config?.headers
|
|
232
|
+
};
|
|
233
|
+
if (this.authToken) {
|
|
234
|
+
headers["Authorization"] = `Bearer ${this.authToken}`;
|
|
235
|
+
}
|
|
236
|
+
const response = await import_axios.default.post(fullUrl, data, {
|
|
237
|
+
...config,
|
|
238
|
+
headers,
|
|
239
|
+
timeout: config?.timeout || 3e4
|
|
240
|
+
});
|
|
241
|
+
return response.data;
|
|
242
|
+
}
|
|
217
243
|
setupDataInterceptors() {
|
|
218
244
|
this.dataClient.interceptors.request.use(
|
|
219
245
|
(config) => {
|
|
@@ -348,7 +374,7 @@ var plasma = (0, import_viem2.defineChain)({
|
|
|
348
374
|
},
|
|
349
375
|
rpcUrls: {
|
|
350
376
|
default: {
|
|
351
|
-
http: ["https://rpc.plasma.
|
|
377
|
+
http: ["https://rpc.plasma.to"]
|
|
352
378
|
}
|
|
353
379
|
},
|
|
354
380
|
blockExplorers: {
|
|
@@ -361,7 +387,7 @@ var plasma = (0, import_viem2.defineChain)({
|
|
|
361
387
|
var DEFAULT_RPC_URLS = {
|
|
362
388
|
8453: "https://mainnet.base.org",
|
|
363
389
|
42161: "https://arb1.arbitrum.io/rpc",
|
|
364
|
-
9745: "https://rpc.plasma.
|
|
390
|
+
9745: "https://rpc.plasma.to"
|
|
365
391
|
};
|
|
366
392
|
var CHAINS = {
|
|
367
393
|
8453: import_chains.base,
|
|
@@ -632,7 +658,7 @@ var signSessionKey = async (config, sessions, allPublicClients) => {
|
|
|
632
658
|
// src/core/ZyfaiSDK.ts
|
|
633
659
|
var import_siwe = require("siwe");
|
|
634
660
|
var ZyfaiSDK = class {
|
|
635
|
-
// Store
|
|
661
|
+
// Store current chain ID for private key connections
|
|
636
662
|
constructor(config) {
|
|
637
663
|
this.signer = null;
|
|
638
664
|
this.walletClient = null;
|
|
@@ -641,6 +667,8 @@ var ZyfaiSDK = class {
|
|
|
641
667
|
this.hasActiveSessionKey = false;
|
|
642
668
|
// TODO: The environment should be removed. Having the same key for staging and production is not ideal, but for now it's fine.
|
|
643
669
|
this.currentProvider = null;
|
|
670
|
+
// Store reference to current provider for event handling
|
|
671
|
+
this.currentChainId = null;
|
|
644
672
|
const sdkConfig = typeof config === "string" ? { apiKey: config } : config;
|
|
645
673
|
const { apiKey, dataApiKey, environment, bundlerApiKey } = sdkConfig;
|
|
646
674
|
if (!apiKey) {
|
|
@@ -664,7 +692,7 @@ var ZyfaiSDK = class {
|
|
|
664
692
|
}
|
|
665
693
|
const walletClient = this.getWalletClient();
|
|
666
694
|
const userAddress = (0, import_viem4.getAddress)(walletClient.account.address);
|
|
667
|
-
const chainId = walletClient.chain?.id || 8453;
|
|
695
|
+
const chainId = this.currentChainId || walletClient.chain?.id || 8453;
|
|
668
696
|
const challengeResponse = await this.httpClient.post(ENDPOINTS.AUTH_CHALLENGE, {});
|
|
669
697
|
let uri;
|
|
670
698
|
let domain;
|
|
@@ -762,18 +790,18 @@ var ZyfaiSDK = class {
|
|
|
762
790
|
async initializeUser(smartWallet, chainId) {
|
|
763
791
|
try {
|
|
764
792
|
await this.authenticateUser();
|
|
765
|
-
const
|
|
793
|
+
const responseData = await this.httpClient.dataPostCustom(
|
|
766
794
|
DATA_ENDPOINTS.USER_INITIALIZE,
|
|
767
795
|
{
|
|
768
796
|
walletAddress: smartWallet
|
|
769
797
|
}
|
|
770
798
|
);
|
|
771
799
|
return {
|
|
772
|
-
success:
|
|
773
|
-
userId:
|
|
774
|
-
smartWallet:
|
|
775
|
-
chainId:
|
|
776
|
-
message:
|
|
800
|
+
success: responseData.status === "success" || true,
|
|
801
|
+
userId: responseData.userId || responseData.id,
|
|
802
|
+
smartWallet: responseData.smartWallet || smartWallet,
|
|
803
|
+
chainId: responseData.chainId || chainId,
|
|
804
|
+
message: responseData.message || responseData.status || "User initialized successfully"
|
|
777
805
|
};
|
|
778
806
|
} catch (error) {
|
|
779
807
|
throw new Error(`Failed to initialize user: ${error.message}`);
|
|
@@ -806,6 +834,7 @@ var ZyfaiSDK = class {
|
|
|
806
834
|
chain: chainConfig.chain,
|
|
807
835
|
transport: (0, import_viem4.custom)(this.currentProvider)
|
|
808
836
|
});
|
|
837
|
+
this.currentChainId = this.walletClient.chain?.id || null;
|
|
809
838
|
try {
|
|
810
839
|
await this.authenticateUser();
|
|
811
840
|
} catch (error) {
|
|
@@ -838,6 +867,7 @@ var ZyfaiSDK = class {
|
|
|
838
867
|
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
839
868
|
}
|
|
840
869
|
this.authenticatedUserId = null;
|
|
870
|
+
this.currentChainId = null;
|
|
841
871
|
this.httpClient.clearAuthToken();
|
|
842
872
|
if (this.currentProvider?.removeAllListeners) {
|
|
843
873
|
try {
|
|
@@ -853,6 +883,7 @@ var ZyfaiSDK = class {
|
|
|
853
883
|
privateKey = `0x${privateKey}`;
|
|
854
884
|
}
|
|
855
885
|
this.signer = (0, import_accounts2.privateKeyToAccount)(privateKey);
|
|
886
|
+
this.currentChainId = chainId;
|
|
856
887
|
this.walletClient = (0, import_viem4.createWalletClient)({
|
|
857
888
|
account: this.signer,
|
|
858
889
|
chain: chainConfig.chain,
|
|
@@ -881,6 +912,7 @@ var ZyfaiSDK = class {
|
|
|
881
912
|
});
|
|
882
913
|
connectedAddress = accounts[0];
|
|
883
914
|
this.currentProvider = provider;
|
|
915
|
+
this.currentChainId = chainId;
|
|
884
916
|
if (provider.on) {
|
|
885
917
|
provider.on("accountsChanged", this.handleAccountsChanged.bind(this));
|
|
886
918
|
}
|
|
@@ -892,6 +924,7 @@ var ZyfaiSDK = class {
|
|
|
892
924
|
});
|
|
893
925
|
connectedAddress = provider.account.address;
|
|
894
926
|
this.currentProvider = null;
|
|
927
|
+
this.currentChainId = chainId;
|
|
895
928
|
} else {
|
|
896
929
|
throw new Error(
|
|
897
930
|
"Invalid wallet provider. Expected EIP-1193 provider or viem WalletClient."
|
|
@@ -921,6 +954,7 @@ var ZyfaiSDK = class {
|
|
|
921
954
|
this.signer = null;
|
|
922
955
|
this.walletClient = null;
|
|
923
956
|
this.currentProvider = null;
|
|
957
|
+
this.currentChainId = null;
|
|
924
958
|
this.authenticatedUserId = null;
|
|
925
959
|
this.hasActiveSessionKey = false;
|
|
926
960
|
this.httpClient.clearAuthToken();
|
|
@@ -931,10 +965,11 @@ var ZyfaiSDK = class {
|
|
|
931
965
|
*/
|
|
932
966
|
getWalletClient(chainId) {
|
|
933
967
|
if (this.signer) {
|
|
968
|
+
const targetChainId = chainId || this.currentChainId || 8453;
|
|
934
969
|
return (0, import_viem4.createWalletClient)({
|
|
935
970
|
account: this.signer,
|
|
936
|
-
chain: getChainConfig(
|
|
937
|
-
transport: (0, import_viem4.http)(getChainConfig(
|
|
971
|
+
chain: getChainConfig(targetChainId).chain,
|
|
972
|
+
transport: (0, import_viem4.http)(getChainConfig(targetChainId).rpcUrl)
|
|
938
973
|
});
|
|
939
974
|
} else {
|
|
940
975
|
if (!this.walletClient) {
|
package/dist/index.mjs
CHANGED
|
@@ -37,7 +37,7 @@ var ENDPOINTS = {
|
|
|
37
37
|
};
|
|
38
38
|
var DATA_ENDPOINTS = {
|
|
39
39
|
// User Initialization
|
|
40
|
-
USER_INITIALIZE: "/earnings/initialize",
|
|
40
|
+
USER_INITIALIZE: "/api/earnings/initialize",
|
|
41
41
|
// Earnings
|
|
42
42
|
ONCHAIN_EARNINGS: (walletAddress) => `/usercheck/onchain-earnings?walletAddress=${walletAddress}`,
|
|
43
43
|
CALCULATE_ONCHAIN_EARNINGS: "/usercheck/calculate-onchain-earnings",
|
|
@@ -71,6 +71,7 @@ var HttpClient = class {
|
|
|
71
71
|
this.authToken = null;
|
|
72
72
|
this.apiKey = apiKey;
|
|
73
73
|
this.dataApiKey = dataApiKey || apiKey;
|
|
74
|
+
this.environment = environment;
|
|
74
75
|
const endpoint = API_ENDPOINTS[environment];
|
|
75
76
|
const parsedUrl = new URL(endpoint);
|
|
76
77
|
this.origin = parsedUrl.origin;
|
|
@@ -175,6 +176,31 @@ var HttpClient = class {
|
|
|
175
176
|
const response = await this.dataClient.post(url, data, config);
|
|
176
177
|
return response.data;
|
|
177
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Make a POST request to Data API with a custom path (bypasses /api/v2 baseURL)
|
|
181
|
+
* Useful for endpoints that don't follow the /api/v2 pattern
|
|
182
|
+
*
|
|
183
|
+
* @param path - API path (e.g., "/api/earnings/initialize")
|
|
184
|
+
* @param data - Request body
|
|
185
|
+
* @param config - Additional axios config
|
|
186
|
+
*/
|
|
187
|
+
async dataPostCustom(path, data, config) {
|
|
188
|
+
const fullUrl = `${DATA_API_ENDPOINTS[this.environment]}${path}`;
|
|
189
|
+
const headers = {
|
|
190
|
+
"Content-Type": "application/json",
|
|
191
|
+
"X-API-Key": this.dataApiKey,
|
|
192
|
+
...config?.headers
|
|
193
|
+
};
|
|
194
|
+
if (this.authToken) {
|
|
195
|
+
headers["Authorization"] = `Bearer ${this.authToken}`;
|
|
196
|
+
}
|
|
197
|
+
const response = await axios.post(fullUrl, data, {
|
|
198
|
+
...config,
|
|
199
|
+
headers,
|
|
200
|
+
timeout: config?.timeout || 3e4
|
|
201
|
+
});
|
|
202
|
+
return response.data;
|
|
203
|
+
}
|
|
178
204
|
setupDataInterceptors() {
|
|
179
205
|
this.dataClient.interceptors.request.use(
|
|
180
206
|
(config) => {
|
|
@@ -314,7 +340,7 @@ var plasma = defineChain({
|
|
|
314
340
|
},
|
|
315
341
|
rpcUrls: {
|
|
316
342
|
default: {
|
|
317
|
-
http: ["https://rpc.plasma.
|
|
343
|
+
http: ["https://rpc.plasma.to"]
|
|
318
344
|
}
|
|
319
345
|
},
|
|
320
346
|
blockExplorers: {
|
|
@@ -327,7 +353,7 @@ var plasma = defineChain({
|
|
|
327
353
|
var DEFAULT_RPC_URLS = {
|
|
328
354
|
8453: "https://mainnet.base.org",
|
|
329
355
|
42161: "https://arb1.arbitrum.io/rpc",
|
|
330
|
-
9745: "https://rpc.plasma.
|
|
356
|
+
9745: "https://rpc.plasma.to"
|
|
331
357
|
};
|
|
332
358
|
var CHAINS = {
|
|
333
359
|
8453: base,
|
|
@@ -612,7 +638,7 @@ var signSessionKey = async (config, sessions, allPublicClients) => {
|
|
|
612
638
|
// src/core/ZyfaiSDK.ts
|
|
613
639
|
import { SiweMessage } from "siwe";
|
|
614
640
|
var ZyfaiSDK = class {
|
|
615
|
-
// Store
|
|
641
|
+
// Store current chain ID for private key connections
|
|
616
642
|
constructor(config) {
|
|
617
643
|
this.signer = null;
|
|
618
644
|
this.walletClient = null;
|
|
@@ -621,6 +647,8 @@ var ZyfaiSDK = class {
|
|
|
621
647
|
this.hasActiveSessionKey = false;
|
|
622
648
|
// TODO: The environment should be removed. Having the same key for staging and production is not ideal, but for now it's fine.
|
|
623
649
|
this.currentProvider = null;
|
|
650
|
+
// Store reference to current provider for event handling
|
|
651
|
+
this.currentChainId = null;
|
|
624
652
|
const sdkConfig = typeof config === "string" ? { apiKey: config } : config;
|
|
625
653
|
const { apiKey, dataApiKey, environment, bundlerApiKey } = sdkConfig;
|
|
626
654
|
if (!apiKey) {
|
|
@@ -644,7 +672,7 @@ var ZyfaiSDK = class {
|
|
|
644
672
|
}
|
|
645
673
|
const walletClient = this.getWalletClient();
|
|
646
674
|
const userAddress = getAddress2(walletClient.account.address);
|
|
647
|
-
const chainId = walletClient.chain?.id || 8453;
|
|
675
|
+
const chainId = this.currentChainId || walletClient.chain?.id || 8453;
|
|
648
676
|
const challengeResponse = await this.httpClient.post(ENDPOINTS.AUTH_CHALLENGE, {});
|
|
649
677
|
let uri;
|
|
650
678
|
let domain;
|
|
@@ -742,18 +770,18 @@ var ZyfaiSDK = class {
|
|
|
742
770
|
async initializeUser(smartWallet, chainId) {
|
|
743
771
|
try {
|
|
744
772
|
await this.authenticateUser();
|
|
745
|
-
const
|
|
773
|
+
const responseData = await this.httpClient.dataPostCustom(
|
|
746
774
|
DATA_ENDPOINTS.USER_INITIALIZE,
|
|
747
775
|
{
|
|
748
776
|
walletAddress: smartWallet
|
|
749
777
|
}
|
|
750
778
|
);
|
|
751
779
|
return {
|
|
752
|
-
success:
|
|
753
|
-
userId:
|
|
754
|
-
smartWallet:
|
|
755
|
-
chainId:
|
|
756
|
-
message:
|
|
780
|
+
success: responseData.status === "success" || true,
|
|
781
|
+
userId: responseData.userId || responseData.id,
|
|
782
|
+
smartWallet: responseData.smartWallet || smartWallet,
|
|
783
|
+
chainId: responseData.chainId || chainId,
|
|
784
|
+
message: responseData.message || responseData.status || "User initialized successfully"
|
|
757
785
|
};
|
|
758
786
|
} catch (error) {
|
|
759
787
|
throw new Error(`Failed to initialize user: ${error.message}`);
|
|
@@ -786,6 +814,7 @@ var ZyfaiSDK = class {
|
|
|
786
814
|
chain: chainConfig.chain,
|
|
787
815
|
transport: custom(this.currentProvider)
|
|
788
816
|
});
|
|
817
|
+
this.currentChainId = this.walletClient.chain?.id || null;
|
|
789
818
|
try {
|
|
790
819
|
await this.authenticateUser();
|
|
791
820
|
} catch (error) {
|
|
@@ -818,6 +847,7 @@ var ZyfaiSDK = class {
|
|
|
818
847
|
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
819
848
|
}
|
|
820
849
|
this.authenticatedUserId = null;
|
|
850
|
+
this.currentChainId = null;
|
|
821
851
|
this.httpClient.clearAuthToken();
|
|
822
852
|
if (this.currentProvider?.removeAllListeners) {
|
|
823
853
|
try {
|
|
@@ -833,6 +863,7 @@ var ZyfaiSDK = class {
|
|
|
833
863
|
privateKey = `0x${privateKey}`;
|
|
834
864
|
}
|
|
835
865
|
this.signer = privateKeyToAccount(privateKey);
|
|
866
|
+
this.currentChainId = chainId;
|
|
836
867
|
this.walletClient = createWalletClient({
|
|
837
868
|
account: this.signer,
|
|
838
869
|
chain: chainConfig.chain,
|
|
@@ -861,6 +892,7 @@ var ZyfaiSDK = class {
|
|
|
861
892
|
});
|
|
862
893
|
connectedAddress = accounts[0];
|
|
863
894
|
this.currentProvider = provider;
|
|
895
|
+
this.currentChainId = chainId;
|
|
864
896
|
if (provider.on) {
|
|
865
897
|
provider.on("accountsChanged", this.handleAccountsChanged.bind(this));
|
|
866
898
|
}
|
|
@@ -872,6 +904,7 @@ var ZyfaiSDK = class {
|
|
|
872
904
|
});
|
|
873
905
|
connectedAddress = provider.account.address;
|
|
874
906
|
this.currentProvider = null;
|
|
907
|
+
this.currentChainId = chainId;
|
|
875
908
|
} else {
|
|
876
909
|
throw new Error(
|
|
877
910
|
"Invalid wallet provider. Expected EIP-1193 provider or viem WalletClient."
|
|
@@ -901,6 +934,7 @@ var ZyfaiSDK = class {
|
|
|
901
934
|
this.signer = null;
|
|
902
935
|
this.walletClient = null;
|
|
903
936
|
this.currentProvider = null;
|
|
937
|
+
this.currentChainId = null;
|
|
904
938
|
this.authenticatedUserId = null;
|
|
905
939
|
this.hasActiveSessionKey = false;
|
|
906
940
|
this.httpClient.clearAuthToken();
|
|
@@ -911,10 +945,11 @@ var ZyfaiSDK = class {
|
|
|
911
945
|
*/
|
|
912
946
|
getWalletClient(chainId) {
|
|
913
947
|
if (this.signer) {
|
|
948
|
+
const targetChainId = chainId || this.currentChainId || 8453;
|
|
914
949
|
return createWalletClient({
|
|
915
950
|
account: this.signer,
|
|
916
|
-
chain: getChainConfig(
|
|
917
|
-
transport: http3(getChainConfig(
|
|
951
|
+
chain: getChainConfig(targetChainId).chain,
|
|
952
|
+
transport: http3(getChainConfig(targetChainId).rpcUrl)
|
|
918
953
|
});
|
|
919
954
|
} else {
|
|
920
955
|
if (!this.walletClient) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zyfai/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.13",
|
|
4
4
|
"description": "TypeScript SDK for ZyFAI Yield Optimization Engine - Deploy Safe smart wallets, manage session keys, and interact with DeFi protocols",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|