@zyfai/sdk 0.1.12 → 0.1.14
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 +22 -22
- package/dist/index.d.mts +9 -8
- package/dist/index.d.ts +9 -8
- package/dist/index.js +33 -17
- package/dist/index.mjs +33 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -69,7 +69,7 @@ The SDK accepts either a private key or a modern wallet provider. **The SDK auto
|
|
|
69
69
|
|
|
70
70
|
```typescript
|
|
71
71
|
// Option 1: With private key (chainId required)
|
|
72
|
-
await sdk.connectAccount("0x...",
|
|
72
|
+
await sdk.connectAccount("0x...", 8453);
|
|
73
73
|
|
|
74
74
|
// Option 2: With wallet provider (chainId optional - uses provider's chain)
|
|
75
75
|
const provider = await connector.getProvider();
|
|
@@ -81,7 +81,7 @@ await sdk.connectAccount(provider); // Automatically uses provider's current cha
|
|
|
81
81
|
|
|
82
82
|
// Now call methods with explicit user addresses
|
|
83
83
|
const userAddress = "0xUser...";
|
|
84
|
-
await sdk.deploySafe(userAddress,
|
|
84
|
+
await sdk.deploySafe(userAddress, 8453);
|
|
85
85
|
```
|
|
86
86
|
|
|
87
87
|
**Note:**
|
|
@@ -116,12 +116,12 @@ Deploy a Safe smart wallet:
|
|
|
116
116
|
const userAddress = "0xUser..."; // User's EOA address
|
|
117
117
|
|
|
118
118
|
// Get the deterministic Safe address (before deployment)
|
|
119
|
-
const walletInfo = await sdk.getSmartWalletAddress(userAddress,
|
|
119
|
+
const walletInfo = await sdk.getSmartWalletAddress(userAddress, 8453);
|
|
120
120
|
console.log("Safe Address:", walletInfo.address);
|
|
121
121
|
console.log("Is Deployed:", walletInfo.isDeployed);
|
|
122
122
|
|
|
123
123
|
// Deploy the Safe (automatically checks if already deployed)
|
|
124
|
-
const result = await sdk.deploySafe(userAddress,
|
|
124
|
+
const result = await sdk.deploySafe(userAddress, 8453);
|
|
125
125
|
|
|
126
126
|
if (result.success) {
|
|
127
127
|
console.log("Safe Address:", result.safeAddress);
|
|
@@ -157,9 +157,9 @@ const chains = getSupportedChainIds();
|
|
|
157
157
|
console.log("Supported chains:", chains);
|
|
158
158
|
|
|
159
159
|
// Check if a chain is supported
|
|
160
|
-
if (isSupportedChain(
|
|
160
|
+
if (isSupportedChain(8453)) {
|
|
161
161
|
const userAddress = "0xUser...";
|
|
162
|
-
const result = await sdk.deploySafe(userAddress,
|
|
162
|
+
const result = await sdk.deploySafe(userAddress, 8453); // Base
|
|
163
163
|
}
|
|
164
164
|
```
|
|
165
165
|
|
|
@@ -192,7 +192,7 @@ Connect account for signing transactions and authenticate via SIWE. Accepts eith
|
|
|
192
192
|
- `chainId`: Target chain ID
|
|
193
193
|
- **Required** for private key
|
|
194
194
|
- **Optional** for wallet providers (auto-detects from provider)
|
|
195
|
-
- Default:
|
|
195
|
+
- Default: 8453 (Base)
|
|
196
196
|
|
|
197
197
|
**Returns:** Connected wallet address
|
|
198
198
|
|
|
@@ -206,7 +206,7 @@ Connect account for signing transactions and authenticate via SIWE. Accepts eith
|
|
|
206
206
|
|
|
207
207
|
```typescript
|
|
208
208
|
// With private key (chainId required)
|
|
209
|
-
await sdk.connectAccount("0x...",
|
|
209
|
+
await sdk.connectAccount("0x...", 8453);
|
|
210
210
|
|
|
211
211
|
// With wallet provider (chainId optional)
|
|
212
212
|
const provider = await connector.getProvider();
|
|
@@ -307,7 +307,7 @@ The SDK automatically fetches optimal session configuration from ZyFAI API:
|
|
|
307
307
|
// 5. Signs the session key
|
|
308
308
|
// 6. Calls /session-keys/add so the session becomes active immediately
|
|
309
309
|
|
|
310
|
-
const result = await sdk.createSessionKey(userAddress,
|
|
310
|
+
const result = await sdk.createSessionKey(userAddress, 8453);
|
|
311
311
|
|
|
312
312
|
// Check if session key already existed
|
|
313
313
|
if (result.alreadyActive) {
|
|
@@ -332,11 +332,11 @@ console.log("User ID:", result.userId);
|
|
|
332
332
|
Transfer tokens to your Safe smart wallet:
|
|
333
333
|
|
|
334
334
|
```typescript
|
|
335
|
-
// Deposit 100 USDC (6 decimals) to Safe on
|
|
335
|
+
// Deposit 100 USDC (6 decimals) to Safe on Base
|
|
336
336
|
const result = await sdk.depositFunds(
|
|
337
337
|
userAddress,
|
|
338
|
-
|
|
339
|
-
"
|
|
338
|
+
8453, // Chain ID
|
|
339
|
+
"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", // USDC on Base
|
|
340
340
|
"100000000" // Amount: 100 USDC = 100 * 10^6
|
|
341
341
|
);
|
|
342
342
|
|
|
@@ -355,12 +355,12 @@ Initiate a withdrawal from your Safe. **Note: Withdrawals are processed asynchro
|
|
|
355
355
|
|
|
356
356
|
```typescript
|
|
357
357
|
// Full withdrawal
|
|
358
|
-
const result = await sdk.withdrawFunds(userAddress,
|
|
358
|
+
const result = await sdk.withdrawFunds(userAddress, 8453);
|
|
359
359
|
|
|
360
360
|
// Partial withdrawal of 50 USDC (6 decimals)
|
|
361
361
|
const result = await sdk.withdrawFunds(
|
|
362
362
|
userAddress,
|
|
363
|
-
|
|
363
|
+
8453,
|
|
364
364
|
"50000000", // Amount: 50 USDC = 50 * 10^6
|
|
365
365
|
"0xReceiverAddress" // Optional: receiver address
|
|
366
366
|
);
|
|
@@ -389,7 +389,7 @@ if (result.success) {
|
|
|
389
389
|
Retrieve all available DeFi protocols and pools for a specific chain:
|
|
390
390
|
|
|
391
391
|
```typescript
|
|
392
|
-
const protocols = await sdk.getAvailableProtocols(
|
|
392
|
+
const protocols = await sdk.getAvailableProtocols(8453);
|
|
393
393
|
|
|
394
394
|
console.log(`Found ${protocols.protocols.length} protocols`);
|
|
395
395
|
protocols.protocols.forEach((protocol) => {
|
|
@@ -626,7 +626,7 @@ pnpm tsx examples/end-to-end.ts
|
|
|
626
626
|
|
|
627
627
|
## Complete Examples
|
|
628
628
|
|
|
629
|
-
### Example 1: Deploy Safe on
|
|
629
|
+
### Example 1: Deploy Safe on Base
|
|
630
630
|
|
|
631
631
|
```typescript
|
|
632
632
|
import { ZyfaiSDK } from "@zyfai/sdk";
|
|
@@ -638,12 +638,12 @@ async function main() {
|
|
|
638
638
|
});
|
|
639
639
|
|
|
640
640
|
// Connect account (automatically authenticates via SIWE)
|
|
641
|
-
await sdk.connectAccount(process.env.PRIVATE_KEY!,
|
|
641
|
+
await sdk.connectAccount(process.env.PRIVATE_KEY!, 8453);
|
|
642
642
|
|
|
643
643
|
const userAddress = "0xUser..."; // User's EOA address
|
|
644
644
|
|
|
645
645
|
// Check if Safe already exists
|
|
646
|
-
const walletInfo = await sdk.getSmartWalletAddress(userAddress,
|
|
646
|
+
const walletInfo = await sdk.getSmartWalletAddress(userAddress, 8453);
|
|
647
647
|
|
|
648
648
|
if (walletInfo.isDeployed) {
|
|
649
649
|
console.log("Safe already deployed at:", walletInfo.address);
|
|
@@ -651,7 +651,7 @@ async function main() {
|
|
|
651
651
|
}
|
|
652
652
|
|
|
653
653
|
// Deploy Safe
|
|
654
|
-
const result = await sdk.deploySafe(userAddress,
|
|
654
|
+
const result = await sdk.deploySafe(userAddress, 8453);
|
|
655
655
|
|
|
656
656
|
if (result.success) {
|
|
657
657
|
console.log("✅ Successfully deployed Safe");
|
|
@@ -692,7 +692,7 @@ function SafeDeployment() {
|
|
|
692
692
|
console.log("Connected and authenticated:", address);
|
|
693
693
|
|
|
694
694
|
// Get Safe address for this user
|
|
695
|
-
const walletInfo = await sdk.getSmartWalletAddress(address,
|
|
695
|
+
const walletInfo = await sdk.getSmartWalletAddress(address, 8453);
|
|
696
696
|
setSafeAddress(walletInfo.address);
|
|
697
697
|
} catch (error) {
|
|
698
698
|
console.error("Connection failed:", error);
|
|
@@ -704,7 +704,7 @@ function SafeDeployment() {
|
|
|
704
704
|
|
|
705
705
|
setIsDeploying(true);
|
|
706
706
|
try {
|
|
707
|
-
const result = await sdk.deploySafe(userAddress,
|
|
707
|
+
const result = await sdk.deploySafe(userAddress, 8453);
|
|
708
708
|
if (result.success) {
|
|
709
709
|
alert(`Safe deployed at ${result.safeAddress}`);
|
|
710
710
|
}
|
|
@@ -761,7 +761,7 @@ The SDK is built on top of:
|
|
|
761
761
|
```typescript
|
|
762
762
|
try {
|
|
763
763
|
const userAddress = "0xUser...";
|
|
764
|
-
const result = await sdk.deploySafe(userAddress,
|
|
764
|
+
const result = await sdk.deploySafe(userAddress, 8453);
|
|
765
765
|
if (!result.success) {
|
|
766
766
|
console.error("Deployment failed:", result.status);
|
|
767
767
|
}
|
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
|
|
@@ -436,7 +437,7 @@ declare class ZyfaiSDK {
|
|
|
436
437
|
* ```typescript
|
|
437
438
|
* await sdk.updateUserProfile({
|
|
438
439
|
* smartWallet: "0x1396730...",
|
|
439
|
-
* chains: [8453
|
|
440
|
+
* chains: [8453],
|
|
440
441
|
* });
|
|
441
442
|
* ```
|
|
442
443
|
*/
|
|
@@ -467,7 +468,7 @@ declare class ZyfaiSDK {
|
|
|
467
468
|
* Accepts either a private key string or a modern wallet provider
|
|
468
469
|
*
|
|
469
470
|
* @param account - Private key string or wallet provider object
|
|
470
|
-
* @param chainId - Target chain ID (default:
|
|
471
|
+
* @param chainId - Target chain ID (default: 8453 - Base)
|
|
471
472
|
* @returns The connected EOA address
|
|
472
473
|
*
|
|
473
474
|
* @example
|
|
@@ -562,10 +563,10 @@ declare class ZyfaiSDK {
|
|
|
562
563
|
*
|
|
563
564
|
* @example
|
|
564
565
|
* ```typescript
|
|
565
|
-
* // Deposit 100 USDC (6 decimals) to Safe on
|
|
566
|
+
* // Deposit 100 USDC (6 decimals) to Safe on Base
|
|
566
567
|
* const result = await sdk.depositFunds(
|
|
567
568
|
* "0xUser...",
|
|
568
|
-
*
|
|
569
|
+
* 8453,
|
|
569
570
|
* "0xaf88d065e77c8cc2239327c5edb3a432268e5831", // USDC
|
|
570
571
|
* "100000000" // 100 USDC = 100 * 10^6
|
|
571
572
|
* );
|
|
@@ -586,13 +587,13 @@ declare class ZyfaiSDK {
|
|
|
586
587
|
* @example
|
|
587
588
|
* ```typescript
|
|
588
589
|
* // Full withdrawal
|
|
589
|
-
* const result = await sdk.withdrawFunds("0xUser...",
|
|
590
|
+
* const result = await sdk.withdrawFunds("0xUser...", 8453);
|
|
590
591
|
* console.log(result.message); // "Withdrawal request sent"
|
|
591
592
|
*
|
|
592
593
|
* // Partial withdrawal of 50 USDC (6 decimals)
|
|
593
594
|
* const result = await sdk.withdrawFunds(
|
|
594
595
|
* "0xUser...",
|
|
595
|
-
*
|
|
596
|
+
* 8453,
|
|
596
597
|
* "50000000", // 50 USDC = 50 * 10^6
|
|
597
598
|
* "0xReceiver..."
|
|
598
599
|
* );
|
|
@@ -607,7 +608,7 @@ declare class ZyfaiSDK {
|
|
|
607
608
|
*
|
|
608
609
|
* @example
|
|
609
610
|
* ```typescript
|
|
610
|
-
* const protocols = await sdk.getAvailableProtocols(
|
|
611
|
+
* const protocols = await sdk.getAvailableProtocols(8453);
|
|
611
612
|
* protocols.forEach(protocol => {
|
|
612
613
|
* console.log(`${protocol.name}: ${protocol.minApy}% - ${protocol.maxApy}% APY`);
|
|
613
614
|
* });
|
|
@@ -627,7 +628,7 @@ declare class ZyfaiSDK {
|
|
|
627
628
|
* const positions = await sdk.getPositions(userAddress);
|
|
628
629
|
*
|
|
629
630
|
* // Get positions on a specific chain
|
|
630
|
-
* const
|
|
631
|
+
* const basePositions = await sdk.getPositions(userAddress, 8453);
|
|
631
632
|
* ```
|
|
632
633
|
*/
|
|
633
634
|
getPositions(userAddress: string, chainId?: SupportedChainId): Promise<PositionsResponse>;
|
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
|
|
@@ -436,7 +437,7 @@ declare class ZyfaiSDK {
|
|
|
436
437
|
* ```typescript
|
|
437
438
|
* await sdk.updateUserProfile({
|
|
438
439
|
* smartWallet: "0x1396730...",
|
|
439
|
-
* chains: [8453
|
|
440
|
+
* chains: [8453],
|
|
440
441
|
* });
|
|
441
442
|
* ```
|
|
442
443
|
*/
|
|
@@ -467,7 +468,7 @@ declare class ZyfaiSDK {
|
|
|
467
468
|
* Accepts either a private key string or a modern wallet provider
|
|
468
469
|
*
|
|
469
470
|
* @param account - Private key string or wallet provider object
|
|
470
|
-
* @param chainId - Target chain ID (default:
|
|
471
|
+
* @param chainId - Target chain ID (default: 8453 - Base)
|
|
471
472
|
* @returns The connected EOA address
|
|
472
473
|
*
|
|
473
474
|
* @example
|
|
@@ -562,10 +563,10 @@ declare class ZyfaiSDK {
|
|
|
562
563
|
*
|
|
563
564
|
* @example
|
|
564
565
|
* ```typescript
|
|
565
|
-
* // Deposit 100 USDC (6 decimals) to Safe on
|
|
566
|
+
* // Deposit 100 USDC (6 decimals) to Safe on Base
|
|
566
567
|
* const result = await sdk.depositFunds(
|
|
567
568
|
* "0xUser...",
|
|
568
|
-
*
|
|
569
|
+
* 8453,
|
|
569
570
|
* "0xaf88d065e77c8cc2239327c5edb3a432268e5831", // USDC
|
|
570
571
|
* "100000000" // 100 USDC = 100 * 10^6
|
|
571
572
|
* );
|
|
@@ -586,13 +587,13 @@ declare class ZyfaiSDK {
|
|
|
586
587
|
* @example
|
|
587
588
|
* ```typescript
|
|
588
589
|
* // Full withdrawal
|
|
589
|
-
* const result = await sdk.withdrawFunds("0xUser...",
|
|
590
|
+
* const result = await sdk.withdrawFunds("0xUser...", 8453);
|
|
590
591
|
* console.log(result.message); // "Withdrawal request sent"
|
|
591
592
|
*
|
|
592
593
|
* // Partial withdrawal of 50 USDC (6 decimals)
|
|
593
594
|
* const result = await sdk.withdrawFunds(
|
|
594
595
|
* "0xUser...",
|
|
595
|
-
*
|
|
596
|
+
* 8453,
|
|
596
597
|
* "50000000", // 50 USDC = 50 * 10^6
|
|
597
598
|
* "0xReceiver..."
|
|
598
599
|
* );
|
|
@@ -607,7 +608,7 @@ declare class ZyfaiSDK {
|
|
|
607
608
|
*
|
|
608
609
|
* @example
|
|
609
610
|
* ```typescript
|
|
610
|
-
* const protocols = await sdk.getAvailableProtocols(
|
|
611
|
+
* const protocols = await sdk.getAvailableProtocols(8453);
|
|
611
612
|
* protocols.forEach(protocol => {
|
|
612
613
|
* console.log(`${protocol.name}: ${protocol.minApy}% - ${protocol.maxApy}% APY`);
|
|
613
614
|
* });
|
|
@@ -627,7 +628,7 @@ declare class ZyfaiSDK {
|
|
|
627
628
|
* const positions = await sdk.getPositions(userAddress);
|
|
628
629
|
*
|
|
629
630
|
* // Get positions on a specific chain
|
|
630
|
-
* const
|
|
631
|
+
* const basePositions = await sdk.getPositions(userAddress, 8453);
|
|
631
632
|
* ```
|
|
632
633
|
*/
|
|
633
634
|
getPositions(userAddress: string, chainId?: SupportedChainId): Promise<PositionsResponse>;
|
package/dist/index.js
CHANGED
|
@@ -374,7 +374,7 @@ var plasma = (0, import_viem2.defineChain)({
|
|
|
374
374
|
},
|
|
375
375
|
rpcUrls: {
|
|
376
376
|
default: {
|
|
377
|
-
http: ["https://rpc.plasma.
|
|
377
|
+
http: ["https://rpc.plasma.to"]
|
|
378
378
|
}
|
|
379
379
|
},
|
|
380
380
|
blockExplorers: {
|
|
@@ -387,7 +387,7 @@ var plasma = (0, import_viem2.defineChain)({
|
|
|
387
387
|
var DEFAULT_RPC_URLS = {
|
|
388
388
|
8453: "https://mainnet.base.org",
|
|
389
389
|
42161: "https://arb1.arbitrum.io/rpc",
|
|
390
|
-
9745: "https://rpc.plasma.
|
|
390
|
+
9745: "https://rpc.plasma.to"
|
|
391
391
|
};
|
|
392
392
|
var CHAINS = {
|
|
393
393
|
8453: import_chains.base,
|
|
@@ -658,7 +658,7 @@ var signSessionKey = async (config, sessions, allPublicClients) => {
|
|
|
658
658
|
// src/core/ZyfaiSDK.ts
|
|
659
659
|
var import_siwe = require("siwe");
|
|
660
660
|
var ZyfaiSDK = class {
|
|
661
|
-
// Store
|
|
661
|
+
// Store current chain ID for private key connections
|
|
662
662
|
constructor(config) {
|
|
663
663
|
this.signer = null;
|
|
664
664
|
this.walletClient = null;
|
|
@@ -667,6 +667,8 @@ var ZyfaiSDK = class {
|
|
|
667
667
|
this.hasActiveSessionKey = false;
|
|
668
668
|
// TODO: The environment should be removed. Having the same key for staging and production is not ideal, but for now it's fine.
|
|
669
669
|
this.currentProvider = null;
|
|
670
|
+
// Store reference to current provider for event handling
|
|
671
|
+
this.currentChainId = null;
|
|
670
672
|
const sdkConfig = typeof config === "string" ? { apiKey: config } : config;
|
|
671
673
|
const { apiKey, dataApiKey, environment, bundlerApiKey } = sdkConfig;
|
|
672
674
|
if (!apiKey) {
|
|
@@ -690,11 +692,12 @@ var ZyfaiSDK = class {
|
|
|
690
692
|
}
|
|
691
693
|
const walletClient = this.getWalletClient();
|
|
692
694
|
const userAddress = (0, import_viem4.getAddress)(walletClient.account.address);
|
|
693
|
-
const chainId = walletClient.chain?.id || 8453;
|
|
695
|
+
const chainId = this.currentChainId || walletClient.chain?.id || 8453;
|
|
694
696
|
const challengeResponse = await this.httpClient.post(ENDPOINTS.AUTH_CHALLENGE, {});
|
|
695
697
|
let uri;
|
|
696
698
|
let domain;
|
|
697
699
|
const globalWindow = typeof globalThis !== "undefined" ? globalThis.window : void 0;
|
|
700
|
+
const isNodeJs = !globalWindow?.location?.origin;
|
|
698
701
|
if (globalWindow?.location?.origin) {
|
|
699
702
|
uri = globalWindow.location.origin;
|
|
700
703
|
domain = globalWindow.location.host;
|
|
@@ -722,7 +725,13 @@ var ZyfaiSDK = class {
|
|
|
722
725
|
{
|
|
723
726
|
message: messageObj,
|
|
724
727
|
signature
|
|
725
|
-
}
|
|
728
|
+
},
|
|
729
|
+
// Set Origin header in Node.js to match message.uri (required by backend validation)
|
|
730
|
+
isNodeJs ? {
|
|
731
|
+
headers: {
|
|
732
|
+
Origin: uri
|
|
733
|
+
}
|
|
734
|
+
} : void 0
|
|
726
735
|
);
|
|
727
736
|
const authToken = loginResponse.accessToken;
|
|
728
737
|
if (!authToken) {
|
|
@@ -748,7 +757,7 @@ var ZyfaiSDK = class {
|
|
|
748
757
|
* ```typescript
|
|
749
758
|
* await sdk.updateUserProfile({
|
|
750
759
|
* smartWallet: "0x1396730...",
|
|
751
|
-
* chains: [8453
|
|
760
|
+
* chains: [8453],
|
|
752
761
|
* });
|
|
753
762
|
* ```
|
|
754
763
|
*/
|
|
@@ -825,13 +834,14 @@ var ZyfaiSDK = class {
|
|
|
825
834
|
this.httpClient.clearAuthToken();
|
|
826
835
|
if (this.walletClient && this.currentProvider) {
|
|
827
836
|
const chainConfig = getChainConfig(
|
|
828
|
-
this.walletClient.chain?.id ||
|
|
837
|
+
this.walletClient.chain?.id || 8453
|
|
829
838
|
);
|
|
830
839
|
this.walletClient = (0, import_viem4.createWalletClient)({
|
|
831
840
|
account: newAddress,
|
|
832
841
|
chain: chainConfig.chain,
|
|
833
842
|
transport: (0, import_viem4.custom)(this.currentProvider)
|
|
834
843
|
});
|
|
844
|
+
this.currentChainId = this.walletClient.chain?.id || null;
|
|
835
845
|
try {
|
|
836
846
|
await this.authenticateUser();
|
|
837
847
|
} catch (error) {
|
|
@@ -847,7 +857,7 @@ var ZyfaiSDK = class {
|
|
|
847
857
|
* Accepts either a private key string or a modern wallet provider
|
|
848
858
|
*
|
|
849
859
|
* @param account - Private key string or wallet provider object
|
|
850
|
-
* @param chainId - Target chain ID (default:
|
|
860
|
+
* @param chainId - Target chain ID (default: 8453 - Base)
|
|
851
861
|
* @returns The connected EOA address
|
|
852
862
|
*
|
|
853
863
|
* @example
|
|
@@ -859,11 +869,12 @@ var ZyfaiSDK = class {
|
|
|
859
869
|
* const provider = await connector.getProvider();
|
|
860
870
|
* await sdk.connectAccount(provider);
|
|
861
871
|
*/
|
|
862
|
-
async connectAccount(account, chainId =
|
|
872
|
+
async connectAccount(account, chainId = 8453) {
|
|
863
873
|
if (!isSupportedChain(chainId)) {
|
|
864
874
|
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
865
875
|
}
|
|
866
876
|
this.authenticatedUserId = null;
|
|
877
|
+
this.currentChainId = null;
|
|
867
878
|
this.httpClient.clearAuthToken();
|
|
868
879
|
if (this.currentProvider?.removeAllListeners) {
|
|
869
880
|
try {
|
|
@@ -879,6 +890,7 @@ var ZyfaiSDK = class {
|
|
|
879
890
|
privateKey = `0x${privateKey}`;
|
|
880
891
|
}
|
|
881
892
|
this.signer = (0, import_accounts2.privateKeyToAccount)(privateKey);
|
|
893
|
+
this.currentChainId = chainId;
|
|
882
894
|
this.walletClient = (0, import_viem4.createWalletClient)({
|
|
883
895
|
account: this.signer,
|
|
884
896
|
chain: chainConfig.chain,
|
|
@@ -907,6 +919,7 @@ var ZyfaiSDK = class {
|
|
|
907
919
|
});
|
|
908
920
|
connectedAddress = accounts[0];
|
|
909
921
|
this.currentProvider = provider;
|
|
922
|
+
this.currentChainId = chainId;
|
|
910
923
|
if (provider.on) {
|
|
911
924
|
provider.on("accountsChanged", this.handleAccountsChanged.bind(this));
|
|
912
925
|
}
|
|
@@ -918,6 +931,7 @@ var ZyfaiSDK = class {
|
|
|
918
931
|
});
|
|
919
932
|
connectedAddress = provider.account.address;
|
|
920
933
|
this.currentProvider = null;
|
|
934
|
+
this.currentChainId = chainId;
|
|
921
935
|
} else {
|
|
922
936
|
throw new Error(
|
|
923
937
|
"Invalid wallet provider. Expected EIP-1193 provider or viem WalletClient."
|
|
@@ -947,6 +961,7 @@ var ZyfaiSDK = class {
|
|
|
947
961
|
this.signer = null;
|
|
948
962
|
this.walletClient = null;
|
|
949
963
|
this.currentProvider = null;
|
|
964
|
+
this.currentChainId = null;
|
|
950
965
|
this.authenticatedUserId = null;
|
|
951
966
|
this.hasActiveSessionKey = false;
|
|
952
967
|
this.httpClient.clearAuthToken();
|
|
@@ -957,10 +972,11 @@ var ZyfaiSDK = class {
|
|
|
957
972
|
*/
|
|
958
973
|
getWalletClient(chainId) {
|
|
959
974
|
if (this.signer) {
|
|
975
|
+
const targetChainId = chainId || this.currentChainId || 8453;
|
|
960
976
|
return (0, import_viem4.createWalletClient)({
|
|
961
977
|
account: this.signer,
|
|
962
|
-
chain: getChainConfig(
|
|
963
|
-
transport: (0, import_viem4.http)(getChainConfig(
|
|
978
|
+
chain: getChainConfig(targetChainId).chain,
|
|
979
|
+
transport: (0, import_viem4.http)(getChainConfig(targetChainId).rpcUrl)
|
|
964
980
|
});
|
|
965
981
|
} else {
|
|
966
982
|
if (!this.walletClient) {
|
|
@@ -1298,10 +1314,10 @@ var ZyfaiSDK = class {
|
|
|
1298
1314
|
*
|
|
1299
1315
|
* @example
|
|
1300
1316
|
* ```typescript
|
|
1301
|
-
* // Deposit 100 USDC (6 decimals) to Safe on
|
|
1317
|
+
* // Deposit 100 USDC (6 decimals) to Safe on Base
|
|
1302
1318
|
* const result = await sdk.depositFunds(
|
|
1303
1319
|
* "0xUser...",
|
|
1304
|
-
*
|
|
1320
|
+
* 8453,
|
|
1305
1321
|
* "0xaf88d065e77c8cc2239327c5edb3a432268e5831", // USDC
|
|
1306
1322
|
* "100000000" // 100 USDC = 100 * 10^6
|
|
1307
1323
|
* );
|
|
@@ -1379,13 +1395,13 @@ var ZyfaiSDK = class {
|
|
|
1379
1395
|
* @example
|
|
1380
1396
|
* ```typescript
|
|
1381
1397
|
* // Full withdrawal
|
|
1382
|
-
* const result = await sdk.withdrawFunds("0xUser...",
|
|
1398
|
+
* const result = await sdk.withdrawFunds("0xUser...", 8453);
|
|
1383
1399
|
* console.log(result.message); // "Withdrawal request sent"
|
|
1384
1400
|
*
|
|
1385
1401
|
* // Partial withdrawal of 50 USDC (6 decimals)
|
|
1386
1402
|
* const result = await sdk.withdrawFunds(
|
|
1387
1403
|
* "0xUser...",
|
|
1388
|
-
*
|
|
1404
|
+
* 8453,
|
|
1389
1405
|
* "50000000", // 50 USDC = 50 * 10^6
|
|
1390
1406
|
* "0xReceiver..."
|
|
1391
1407
|
* );
|
|
@@ -1471,7 +1487,7 @@ var ZyfaiSDK = class {
|
|
|
1471
1487
|
*
|
|
1472
1488
|
* @example
|
|
1473
1489
|
* ```typescript
|
|
1474
|
-
* const protocols = await sdk.getAvailableProtocols(
|
|
1490
|
+
* const protocols = await sdk.getAvailableProtocols(8453);
|
|
1475
1491
|
* protocols.forEach(protocol => {
|
|
1476
1492
|
* console.log(`${protocol.name}: ${protocol.minApy}% - ${protocol.maxApy}% APY`);
|
|
1477
1493
|
* });
|
|
@@ -1509,7 +1525,7 @@ var ZyfaiSDK = class {
|
|
|
1509
1525
|
* const positions = await sdk.getPositions(userAddress);
|
|
1510
1526
|
*
|
|
1511
1527
|
* // Get positions on a specific chain
|
|
1512
|
-
* const
|
|
1528
|
+
* const basePositions = await sdk.getPositions(userAddress, 8453);
|
|
1513
1529
|
* ```
|
|
1514
1530
|
*/
|
|
1515
1531
|
async getPositions(userAddress, chainId) {
|
package/dist/index.mjs
CHANGED
|
@@ -340,7 +340,7 @@ var plasma = defineChain({
|
|
|
340
340
|
},
|
|
341
341
|
rpcUrls: {
|
|
342
342
|
default: {
|
|
343
|
-
http: ["https://rpc.plasma.
|
|
343
|
+
http: ["https://rpc.plasma.to"]
|
|
344
344
|
}
|
|
345
345
|
},
|
|
346
346
|
blockExplorers: {
|
|
@@ -353,7 +353,7 @@ var plasma = defineChain({
|
|
|
353
353
|
var DEFAULT_RPC_URLS = {
|
|
354
354
|
8453: "https://mainnet.base.org",
|
|
355
355
|
42161: "https://arb1.arbitrum.io/rpc",
|
|
356
|
-
9745: "https://rpc.plasma.
|
|
356
|
+
9745: "https://rpc.plasma.to"
|
|
357
357
|
};
|
|
358
358
|
var CHAINS = {
|
|
359
359
|
8453: base,
|
|
@@ -638,7 +638,7 @@ var signSessionKey = async (config, sessions, allPublicClients) => {
|
|
|
638
638
|
// src/core/ZyfaiSDK.ts
|
|
639
639
|
import { SiweMessage } from "siwe";
|
|
640
640
|
var ZyfaiSDK = class {
|
|
641
|
-
// Store
|
|
641
|
+
// Store current chain ID for private key connections
|
|
642
642
|
constructor(config) {
|
|
643
643
|
this.signer = null;
|
|
644
644
|
this.walletClient = null;
|
|
@@ -647,6 +647,8 @@ var ZyfaiSDK = class {
|
|
|
647
647
|
this.hasActiveSessionKey = false;
|
|
648
648
|
// TODO: The environment should be removed. Having the same key for staging and production is not ideal, but for now it's fine.
|
|
649
649
|
this.currentProvider = null;
|
|
650
|
+
// Store reference to current provider for event handling
|
|
651
|
+
this.currentChainId = null;
|
|
650
652
|
const sdkConfig = typeof config === "string" ? { apiKey: config } : config;
|
|
651
653
|
const { apiKey, dataApiKey, environment, bundlerApiKey } = sdkConfig;
|
|
652
654
|
if (!apiKey) {
|
|
@@ -670,11 +672,12 @@ var ZyfaiSDK = class {
|
|
|
670
672
|
}
|
|
671
673
|
const walletClient = this.getWalletClient();
|
|
672
674
|
const userAddress = getAddress2(walletClient.account.address);
|
|
673
|
-
const chainId = walletClient.chain?.id || 8453;
|
|
675
|
+
const chainId = this.currentChainId || walletClient.chain?.id || 8453;
|
|
674
676
|
const challengeResponse = await this.httpClient.post(ENDPOINTS.AUTH_CHALLENGE, {});
|
|
675
677
|
let uri;
|
|
676
678
|
let domain;
|
|
677
679
|
const globalWindow = typeof globalThis !== "undefined" ? globalThis.window : void 0;
|
|
680
|
+
const isNodeJs = !globalWindow?.location?.origin;
|
|
678
681
|
if (globalWindow?.location?.origin) {
|
|
679
682
|
uri = globalWindow.location.origin;
|
|
680
683
|
domain = globalWindow.location.host;
|
|
@@ -702,7 +705,13 @@ var ZyfaiSDK = class {
|
|
|
702
705
|
{
|
|
703
706
|
message: messageObj,
|
|
704
707
|
signature
|
|
705
|
-
}
|
|
708
|
+
},
|
|
709
|
+
// Set Origin header in Node.js to match message.uri (required by backend validation)
|
|
710
|
+
isNodeJs ? {
|
|
711
|
+
headers: {
|
|
712
|
+
Origin: uri
|
|
713
|
+
}
|
|
714
|
+
} : void 0
|
|
706
715
|
);
|
|
707
716
|
const authToken = loginResponse.accessToken;
|
|
708
717
|
if (!authToken) {
|
|
@@ -728,7 +737,7 @@ var ZyfaiSDK = class {
|
|
|
728
737
|
* ```typescript
|
|
729
738
|
* await sdk.updateUserProfile({
|
|
730
739
|
* smartWallet: "0x1396730...",
|
|
731
|
-
* chains: [8453
|
|
740
|
+
* chains: [8453],
|
|
732
741
|
* });
|
|
733
742
|
* ```
|
|
734
743
|
*/
|
|
@@ -805,13 +814,14 @@ var ZyfaiSDK = class {
|
|
|
805
814
|
this.httpClient.clearAuthToken();
|
|
806
815
|
if (this.walletClient && this.currentProvider) {
|
|
807
816
|
const chainConfig = getChainConfig(
|
|
808
|
-
this.walletClient.chain?.id ||
|
|
817
|
+
this.walletClient.chain?.id || 8453
|
|
809
818
|
);
|
|
810
819
|
this.walletClient = createWalletClient({
|
|
811
820
|
account: newAddress,
|
|
812
821
|
chain: chainConfig.chain,
|
|
813
822
|
transport: custom(this.currentProvider)
|
|
814
823
|
});
|
|
824
|
+
this.currentChainId = this.walletClient.chain?.id || null;
|
|
815
825
|
try {
|
|
816
826
|
await this.authenticateUser();
|
|
817
827
|
} catch (error) {
|
|
@@ -827,7 +837,7 @@ var ZyfaiSDK = class {
|
|
|
827
837
|
* Accepts either a private key string or a modern wallet provider
|
|
828
838
|
*
|
|
829
839
|
* @param account - Private key string or wallet provider object
|
|
830
|
-
* @param chainId - Target chain ID (default:
|
|
840
|
+
* @param chainId - Target chain ID (default: 8453 - Base)
|
|
831
841
|
* @returns The connected EOA address
|
|
832
842
|
*
|
|
833
843
|
* @example
|
|
@@ -839,11 +849,12 @@ var ZyfaiSDK = class {
|
|
|
839
849
|
* const provider = await connector.getProvider();
|
|
840
850
|
* await sdk.connectAccount(provider);
|
|
841
851
|
*/
|
|
842
|
-
async connectAccount(account, chainId =
|
|
852
|
+
async connectAccount(account, chainId = 8453) {
|
|
843
853
|
if (!isSupportedChain(chainId)) {
|
|
844
854
|
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
845
855
|
}
|
|
846
856
|
this.authenticatedUserId = null;
|
|
857
|
+
this.currentChainId = null;
|
|
847
858
|
this.httpClient.clearAuthToken();
|
|
848
859
|
if (this.currentProvider?.removeAllListeners) {
|
|
849
860
|
try {
|
|
@@ -859,6 +870,7 @@ var ZyfaiSDK = class {
|
|
|
859
870
|
privateKey = `0x${privateKey}`;
|
|
860
871
|
}
|
|
861
872
|
this.signer = privateKeyToAccount(privateKey);
|
|
873
|
+
this.currentChainId = chainId;
|
|
862
874
|
this.walletClient = createWalletClient({
|
|
863
875
|
account: this.signer,
|
|
864
876
|
chain: chainConfig.chain,
|
|
@@ -887,6 +899,7 @@ var ZyfaiSDK = class {
|
|
|
887
899
|
});
|
|
888
900
|
connectedAddress = accounts[0];
|
|
889
901
|
this.currentProvider = provider;
|
|
902
|
+
this.currentChainId = chainId;
|
|
890
903
|
if (provider.on) {
|
|
891
904
|
provider.on("accountsChanged", this.handleAccountsChanged.bind(this));
|
|
892
905
|
}
|
|
@@ -898,6 +911,7 @@ var ZyfaiSDK = class {
|
|
|
898
911
|
});
|
|
899
912
|
connectedAddress = provider.account.address;
|
|
900
913
|
this.currentProvider = null;
|
|
914
|
+
this.currentChainId = chainId;
|
|
901
915
|
} else {
|
|
902
916
|
throw new Error(
|
|
903
917
|
"Invalid wallet provider. Expected EIP-1193 provider or viem WalletClient."
|
|
@@ -927,6 +941,7 @@ var ZyfaiSDK = class {
|
|
|
927
941
|
this.signer = null;
|
|
928
942
|
this.walletClient = null;
|
|
929
943
|
this.currentProvider = null;
|
|
944
|
+
this.currentChainId = null;
|
|
930
945
|
this.authenticatedUserId = null;
|
|
931
946
|
this.hasActiveSessionKey = false;
|
|
932
947
|
this.httpClient.clearAuthToken();
|
|
@@ -937,10 +952,11 @@ var ZyfaiSDK = class {
|
|
|
937
952
|
*/
|
|
938
953
|
getWalletClient(chainId) {
|
|
939
954
|
if (this.signer) {
|
|
955
|
+
const targetChainId = chainId || this.currentChainId || 8453;
|
|
940
956
|
return createWalletClient({
|
|
941
957
|
account: this.signer,
|
|
942
|
-
chain: getChainConfig(
|
|
943
|
-
transport: http3(getChainConfig(
|
|
958
|
+
chain: getChainConfig(targetChainId).chain,
|
|
959
|
+
transport: http3(getChainConfig(targetChainId).rpcUrl)
|
|
944
960
|
});
|
|
945
961
|
} else {
|
|
946
962
|
if (!this.walletClient) {
|
|
@@ -1278,10 +1294,10 @@ var ZyfaiSDK = class {
|
|
|
1278
1294
|
*
|
|
1279
1295
|
* @example
|
|
1280
1296
|
* ```typescript
|
|
1281
|
-
* // Deposit 100 USDC (6 decimals) to Safe on
|
|
1297
|
+
* // Deposit 100 USDC (6 decimals) to Safe on Base
|
|
1282
1298
|
* const result = await sdk.depositFunds(
|
|
1283
1299
|
* "0xUser...",
|
|
1284
|
-
*
|
|
1300
|
+
* 8453,
|
|
1285
1301
|
* "0xaf88d065e77c8cc2239327c5edb3a432268e5831", // USDC
|
|
1286
1302
|
* "100000000" // 100 USDC = 100 * 10^6
|
|
1287
1303
|
* );
|
|
@@ -1359,13 +1375,13 @@ var ZyfaiSDK = class {
|
|
|
1359
1375
|
* @example
|
|
1360
1376
|
* ```typescript
|
|
1361
1377
|
* // Full withdrawal
|
|
1362
|
-
* const result = await sdk.withdrawFunds("0xUser...",
|
|
1378
|
+
* const result = await sdk.withdrawFunds("0xUser...", 8453);
|
|
1363
1379
|
* console.log(result.message); // "Withdrawal request sent"
|
|
1364
1380
|
*
|
|
1365
1381
|
* // Partial withdrawal of 50 USDC (6 decimals)
|
|
1366
1382
|
* const result = await sdk.withdrawFunds(
|
|
1367
1383
|
* "0xUser...",
|
|
1368
|
-
*
|
|
1384
|
+
* 8453,
|
|
1369
1385
|
* "50000000", // 50 USDC = 50 * 10^6
|
|
1370
1386
|
* "0xReceiver..."
|
|
1371
1387
|
* );
|
|
@@ -1451,7 +1467,7 @@ var ZyfaiSDK = class {
|
|
|
1451
1467
|
*
|
|
1452
1468
|
* @example
|
|
1453
1469
|
* ```typescript
|
|
1454
|
-
* const protocols = await sdk.getAvailableProtocols(
|
|
1470
|
+
* const protocols = await sdk.getAvailableProtocols(8453);
|
|
1455
1471
|
* protocols.forEach(protocol => {
|
|
1456
1472
|
* console.log(`${protocol.name}: ${protocol.minApy}% - ${protocol.maxApy}% APY`);
|
|
1457
1473
|
* });
|
|
@@ -1489,7 +1505,7 @@ var ZyfaiSDK = class {
|
|
|
1489
1505
|
* const positions = await sdk.getPositions(userAddress);
|
|
1490
1506
|
*
|
|
1491
1507
|
* // Get positions on a specific chain
|
|
1492
|
-
* const
|
|
1508
|
+
* const basePositions = await sdk.getPositions(userAddress, 8453);
|
|
1493
1509
|
* ```
|
|
1494
1510
|
*/
|
|
1495
1511
|
async getPositions(userAddress, chainId) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zyfai/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
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",
|