@zyfai/sdk 0.1.5 → 0.1.6
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 +76 -16
- package/dist/index.d.mts +17 -3
- package/dist/index.d.ts +17 -3
- package/dist/index.js +61 -34
- package/dist/index.mjs +61 -34
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -65,7 +65,7 @@ const sdk = new ZyfaiSDK({
|
|
|
65
65
|
|
|
66
66
|
### Connect Account
|
|
67
67
|
|
|
68
|
-
The SDK accepts either a private key or a modern wallet provider
|
|
68
|
+
The SDK accepts either a private key or a modern wallet provider. **The SDK automatically authenticates the user via SIWE (Sign-In with Ethereum) when connecting.**
|
|
69
69
|
|
|
70
70
|
```typescript
|
|
71
71
|
// Option 1: With private key (chainId required)
|
|
@@ -84,7 +84,25 @@ const userAddress = "0xUser...";
|
|
|
84
84
|
await sdk.deploySafe(userAddress, 42161);
|
|
85
85
|
```
|
|
86
86
|
|
|
87
|
-
**Note:**
|
|
87
|
+
**Note:**
|
|
88
|
+
- When using a wallet provider, the SDK automatically detects the chain from the provider. You can optionally specify `chainId` to override.
|
|
89
|
+
- The SDK automatically performs SIWE authentication when connecting, so you don't need to call any additional authentication methods.
|
|
90
|
+
|
|
91
|
+
### Disconnect Account
|
|
92
|
+
|
|
93
|
+
Disconnect the current account and clear all authentication state:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
// Disconnect account and clear JWT token
|
|
97
|
+
await sdk.disconnectAccount();
|
|
98
|
+
console.log("Account disconnected and authentication cleared");
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
This method:
|
|
102
|
+
- Clears the wallet connection
|
|
103
|
+
- Resets authentication state
|
|
104
|
+
- Clears the JWT token
|
|
105
|
+
- Resets session key tracking
|
|
88
106
|
|
|
89
107
|
## Core Features
|
|
90
108
|
|
|
@@ -164,7 +182,7 @@ new ZyfaiSDK(config: SDKConfig | string)
|
|
|
164
182
|
|
|
165
183
|
##### `connectAccount(account: string | any, chainId?: SupportedChainId): Promise<Address>`
|
|
166
184
|
|
|
167
|
-
Connect account for signing transactions. Accepts either a private key string or a modern wallet provider.
|
|
185
|
+
Connect account for signing transactions and authenticate via SIWE. Accepts either a private key string or a modern wallet provider.
|
|
168
186
|
|
|
169
187
|
**Parameters:**
|
|
170
188
|
|
|
@@ -176,6 +194,11 @@ Connect account for signing transactions. Accepts either a private key string or
|
|
|
176
194
|
|
|
177
195
|
**Returns:** Connected wallet address
|
|
178
196
|
|
|
197
|
+
**Automatic Actions:**
|
|
198
|
+
- Connects the wallet
|
|
199
|
+
- Authenticates via SIWE (Sign-In with Ethereum)
|
|
200
|
+
- Stores JWT token for authenticated endpoints
|
|
201
|
+
|
|
179
202
|
**Examples:**
|
|
180
203
|
|
|
181
204
|
```typescript
|
|
@@ -187,6 +210,25 @@ const provider = await connector.getProvider();
|
|
|
187
210
|
await sdk.connectAccount(provider); // Uses provider's current chain
|
|
188
211
|
```
|
|
189
212
|
|
|
213
|
+
##### `disconnectAccount(): Promise<void>`
|
|
214
|
+
|
|
215
|
+
Disconnect account and clear all authentication state.
|
|
216
|
+
|
|
217
|
+
**Returns:** Promise that resolves when disconnection is complete
|
|
218
|
+
|
|
219
|
+
**Actions:**
|
|
220
|
+
- Clears wallet connection
|
|
221
|
+
- Resets authentication state
|
|
222
|
+
- Clears JWT token
|
|
223
|
+
- Resets session key tracking
|
|
224
|
+
|
|
225
|
+
**Example:**
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
await sdk.disconnectAccount();
|
|
229
|
+
console.log("Disconnected and cleared");
|
|
230
|
+
```
|
|
231
|
+
|
|
190
232
|
##### `getSmartWalletAddress(userAddress: string, chainId: SupportedChainId): Promise<SmartWalletResponse>`
|
|
191
233
|
|
|
192
234
|
Get the Smart Wallet (Safe) address for a user.
|
|
@@ -235,7 +277,7 @@ The SDK automatically fetches optimal session configuration from ZyFAI API:
|
|
|
235
277
|
|
|
236
278
|
```typescript
|
|
237
279
|
// SDK automatically:
|
|
238
|
-
// 1.
|
|
280
|
+
// 1. Uses existing SIWE authentication (from connectAccount)
|
|
239
281
|
// 2. Checks if user already has an active session key (returns early if so)
|
|
240
282
|
// 3. Calculates the deterministic Safe address
|
|
241
283
|
// 4. Retrieves personalized config via /session-keys/config
|
|
@@ -257,9 +299,9 @@ console.log("User ID:", result.userId);
|
|
|
257
299
|
|
|
258
300
|
**Important**:
|
|
259
301
|
|
|
260
|
-
-
|
|
261
|
-
- The SDK proactively checks if the user already has an active session key
|
|
262
|
-
- The user record must have `smartWallet` and `chainId` set (automatically handled after calling `deploySafe`
|
|
302
|
+
- User must be authenticated (automatically done via `connectAccount()`)
|
|
303
|
+
- The SDK proactively checks if the user already has an active session key and returns early without requiring any signature if one exists
|
|
304
|
+
- The user record must have `smartWallet` and `chainId` set (automatically handled after calling `deploySafe`)
|
|
263
305
|
- When `alreadyActive` is `true`, `sessionKeyAddress` and `signature` are not available in the response
|
|
264
306
|
|
|
265
307
|
### 4. Deposit Funds
|
|
@@ -286,7 +328,7 @@ The SDK automatically authenticates via SIWE before logging the deposit with ZyF
|
|
|
286
328
|
|
|
287
329
|
### 5. Withdraw Funds
|
|
288
330
|
|
|
289
|
-
|
|
331
|
+
Initiate a withdrawal from your Safe. **Note: Withdrawals are processed asynchronously by the backend.**
|
|
290
332
|
|
|
291
333
|
```typescript
|
|
292
334
|
// Full withdrawal
|
|
@@ -301,13 +343,22 @@ const result = await sdk.withdrawFunds(
|
|
|
301
343
|
);
|
|
302
344
|
|
|
303
345
|
if (result.success) {
|
|
304
|
-
console.log("Withdrawal
|
|
305
|
-
console.log("
|
|
346
|
+
console.log("Withdrawal initiated!");
|
|
347
|
+
console.log("Message:", result.message); // e.g., "Withdrawal request sent"
|
|
348
|
+
if (result.txHash) {
|
|
349
|
+
console.log("Transaction Hash:", result.txHash);
|
|
350
|
+
} else {
|
|
351
|
+
console.log("Transaction will be processed asynchronously");
|
|
352
|
+
}
|
|
306
353
|
}
|
|
307
354
|
```
|
|
308
355
|
|
|
309
|
-
**
|
|
310
|
-
|
|
356
|
+
**Important Notes:**
|
|
357
|
+
- Amount must be in least decimal units. For USDC (6 decimals): 1 USDC = 1000000
|
|
358
|
+
- The SDK authenticates via SIWE before calling the withdrawal endpoints
|
|
359
|
+
- Withdrawals are processed asynchronously - the `txHash` may not be immediately available
|
|
360
|
+
- Check the `message` field for the withdrawal status
|
|
361
|
+
- Use `getHistory()` to track the withdrawal transaction once it's processed
|
|
311
362
|
|
|
312
363
|
### 6. Get Available Protocols
|
|
313
364
|
|
|
@@ -549,7 +600,7 @@ async function main() {
|
|
|
549
600
|
bundlerApiKey: process.env.BUNDLER_API_KEY!,
|
|
550
601
|
});
|
|
551
602
|
|
|
552
|
-
// Connect account (
|
|
603
|
+
// Connect account (automatically authenticates via SIWE)
|
|
553
604
|
await sdk.connectAccount(process.env.PRIVATE_KEY!, 42161);
|
|
554
605
|
|
|
555
606
|
const userAddress = "0xUser..."; // User's EOA address
|
|
@@ -598,9 +649,10 @@ function SafeDeployment() {
|
|
|
598
649
|
try {
|
|
599
650
|
// Client passes the wallet provider from their frontend
|
|
600
651
|
// e.g., from wagmi: const provider = await connector.getProvider();
|
|
652
|
+
// connectAccount automatically authenticates via SIWE
|
|
601
653
|
const address = await sdk.connectAccount(walletProvider); // chainId auto-detected
|
|
602
654
|
setUserAddress(address);
|
|
603
|
-
console.log("Connected:", address);
|
|
655
|
+
console.log("Connected and authenticated:", address);
|
|
604
656
|
|
|
605
657
|
// Get Safe address for this user
|
|
606
658
|
const walletInfo = await sdk.getSmartWalletAddress(address, 42161);
|
|
@@ -720,7 +772,7 @@ CHAIN_ID=8453
|
|
|
720
772
|
|
|
721
773
|
### "No account connected" Error
|
|
722
774
|
|
|
723
|
-
Make sure to call `connectAccount()` before calling other methods that require signing.
|
|
775
|
+
Make sure to call `connectAccount()` before calling other methods that require signing. Note that `connectAccount()` automatically authenticates the user via SIWE.
|
|
724
776
|
|
|
725
777
|
### "Unsupported chain" Error
|
|
726
778
|
|
|
@@ -728,18 +780,26 @@ Check that the chain ID is in the supported chains list: Arbitrum (42161), Base
|
|
|
728
780
|
|
|
729
781
|
### SIWE Authentication Issues in Browser
|
|
730
782
|
|
|
731
|
-
The SDK automatically detects browser vs Node.js environments
|
|
783
|
+
The SDK automatically performs SIWE authentication when you call `connectAccount()`. The SDK automatically detects browser vs Node.js environments:
|
|
732
784
|
- **Browser**: Uses `window.location.origin` for the SIWE message domain/uri to match the browser's automatic `Origin` header
|
|
733
785
|
- **Node.js**: Uses the API endpoint URL
|
|
734
786
|
|
|
735
787
|
If you encounter SIWE authentication failures in a browser, ensure:
|
|
736
788
|
1. Your frontend origin is allowed by the API's CORS configuration
|
|
737
789
|
2. You're using the correct `environment` setting (`staging` or `production`)
|
|
790
|
+
3. The user approves the SIWE signature request in their wallet
|
|
738
791
|
|
|
739
792
|
### Session Key Already Exists
|
|
740
793
|
|
|
741
794
|
If `createSessionKey` returns `{ alreadyActive: true }`, the user already has an active session key. This is not an error - the SDK proactively checks before attempting to create a new one.
|
|
742
795
|
|
|
796
|
+
### Withdrawal Transaction Hash Not Available
|
|
797
|
+
|
|
798
|
+
If `withdrawFunds` returns without a `txHash`, the withdrawal is being processed asynchronously by the backend. You can:
|
|
799
|
+
1. Check the `message` field for status information
|
|
800
|
+
2. Use `getHistory()` to track when the withdrawal transaction is processed
|
|
801
|
+
3. The transaction will appear in the history once it's been executed
|
|
802
|
+
|
|
743
803
|
### Data API CORS Errors
|
|
744
804
|
|
|
745
805
|
Some Data API endpoints may require server-side CORS configuration. If you see CORS errors for endpoints like `onchain-earnings`, `calculate-onchain-earnings`, or `opportunities`, contact ZyFAI support to ensure your origin is whitelisted.
|
package/dist/index.d.mts
CHANGED
|
@@ -347,7 +347,8 @@ interface DepositResponse {
|
|
|
347
347
|
}
|
|
348
348
|
interface WithdrawResponse {
|
|
349
349
|
success: boolean;
|
|
350
|
-
|
|
350
|
+
message: string;
|
|
351
|
+
txHash?: string;
|
|
351
352
|
type: "full" | "partial";
|
|
352
353
|
amount: string;
|
|
353
354
|
receiver: string;
|
|
@@ -456,6 +457,17 @@ declare class ZyfaiSDK {
|
|
|
456
457
|
* await sdk.connectAccount(provider);
|
|
457
458
|
*/
|
|
458
459
|
connectAccount(account: string | any, chainId?: SupportedChainId): Promise<Address>;
|
|
460
|
+
/**
|
|
461
|
+
* Disconnect account and clear authentication state
|
|
462
|
+
* Resets wallet connection, JWT token, and all authentication-related state
|
|
463
|
+
*
|
|
464
|
+
* @example
|
|
465
|
+
* ```typescript
|
|
466
|
+
* await sdk.disconnectAccount();
|
|
467
|
+
* console.log("Account disconnected");
|
|
468
|
+
* ```
|
|
469
|
+
*/
|
|
470
|
+
disconnectAccount(): Promise<void>;
|
|
459
471
|
/**
|
|
460
472
|
* Get wallet client (throws if not connected)
|
|
461
473
|
* @private
|
|
@@ -531,18 +543,20 @@ declare class ZyfaiSDK {
|
|
|
531
543
|
depositFunds(userAddress: string, chainId: SupportedChainId, tokenAddress: string, amount: string): Promise<DepositResponse>;
|
|
532
544
|
/**
|
|
533
545
|
* Withdraw funds from Safe smart wallet
|
|
534
|
-
*
|
|
546
|
+
* Initiates a withdrawal request to the ZyFAI API
|
|
547
|
+
* Note: The withdrawal is processed asynchronously, so txHash may not be immediately available
|
|
535
548
|
*
|
|
536
549
|
* @param userAddress - User's address (owner of the Safe)
|
|
537
550
|
* @param chainId - Target chain ID
|
|
538
551
|
* @param amount - Optional: Amount in least decimal units to withdraw (partial withdrawal). If not specified, withdraws all funds
|
|
539
552
|
* @param receiver - Optional: Receiver address. If not specified, sends to Safe owner
|
|
540
|
-
* @returns Withdraw response with transaction hash
|
|
553
|
+
* @returns Withdraw response with message and optional transaction hash (available once processed)
|
|
541
554
|
*
|
|
542
555
|
* @example
|
|
543
556
|
* ```typescript
|
|
544
557
|
* // Full withdrawal
|
|
545
558
|
* const result = await sdk.withdrawFunds("0xUser...", 42161);
|
|
559
|
+
* console.log(result.message); // "Withdrawal request sent"
|
|
546
560
|
*
|
|
547
561
|
* // Partial withdrawal of 50 USDC (6 decimals)
|
|
548
562
|
* const result = await sdk.withdrawFunds(
|
package/dist/index.d.ts
CHANGED
|
@@ -347,7 +347,8 @@ interface DepositResponse {
|
|
|
347
347
|
}
|
|
348
348
|
interface WithdrawResponse {
|
|
349
349
|
success: boolean;
|
|
350
|
-
|
|
350
|
+
message: string;
|
|
351
|
+
txHash?: string;
|
|
351
352
|
type: "full" | "partial";
|
|
352
353
|
amount: string;
|
|
353
354
|
receiver: string;
|
|
@@ -456,6 +457,17 @@ declare class ZyfaiSDK {
|
|
|
456
457
|
* await sdk.connectAccount(provider);
|
|
457
458
|
*/
|
|
458
459
|
connectAccount(account: string | any, chainId?: SupportedChainId): Promise<Address>;
|
|
460
|
+
/**
|
|
461
|
+
* Disconnect account and clear authentication state
|
|
462
|
+
* Resets wallet connection, JWT token, and all authentication-related state
|
|
463
|
+
*
|
|
464
|
+
* @example
|
|
465
|
+
* ```typescript
|
|
466
|
+
* await sdk.disconnectAccount();
|
|
467
|
+
* console.log("Account disconnected");
|
|
468
|
+
* ```
|
|
469
|
+
*/
|
|
470
|
+
disconnectAccount(): Promise<void>;
|
|
459
471
|
/**
|
|
460
472
|
* Get wallet client (throws if not connected)
|
|
461
473
|
* @private
|
|
@@ -531,18 +543,20 @@ declare class ZyfaiSDK {
|
|
|
531
543
|
depositFunds(userAddress: string, chainId: SupportedChainId, tokenAddress: string, amount: string): Promise<DepositResponse>;
|
|
532
544
|
/**
|
|
533
545
|
* Withdraw funds from Safe smart wallet
|
|
534
|
-
*
|
|
546
|
+
* Initiates a withdrawal request to the ZyFAI API
|
|
547
|
+
* Note: The withdrawal is processed asynchronously, so txHash may not be immediately available
|
|
535
548
|
*
|
|
536
549
|
* @param userAddress - User's address (owner of the Safe)
|
|
537
550
|
* @param chainId - Target chain ID
|
|
538
551
|
* @param amount - Optional: Amount in least decimal units to withdraw (partial withdrawal). If not specified, withdraws all funds
|
|
539
552
|
* @param receiver - Optional: Receiver address. If not specified, sends to Safe owner
|
|
540
|
-
* @returns Withdraw response with transaction hash
|
|
553
|
+
* @returns Withdraw response with message and optional transaction hash (available once processed)
|
|
541
554
|
*
|
|
542
555
|
* @example
|
|
543
556
|
* ```typescript
|
|
544
557
|
* // Full withdrawal
|
|
545
558
|
* const result = await sdk.withdrawFunds("0xUser...", 42161);
|
|
559
|
+
* console.log(result.message); // "Withdrawal request sent"
|
|
546
560
|
*
|
|
547
561
|
* // Partial withdrawal of 50 USDC (6 decimals)
|
|
548
562
|
* const result = await sdk.withdrawFunds(
|
package/dist/index.js
CHANGED
|
@@ -757,6 +757,7 @@ var ZyfaiSDK = class {
|
|
|
757
757
|
this.isAuthenticated = false;
|
|
758
758
|
this.httpClient.clearAuthToken();
|
|
759
759
|
const chainConfig = getChainConfig(chainId);
|
|
760
|
+
let connectedAddress;
|
|
760
761
|
if (typeof account === "string") {
|
|
761
762
|
let privateKey = account;
|
|
762
763
|
if (!privateKey.startsWith("0x")) {
|
|
@@ -768,39 +769,60 @@ var ZyfaiSDK = class {
|
|
|
768
769
|
chain: chainConfig.chain,
|
|
769
770
|
transport: (0, import_viem4.http)(chainConfig.rpcUrl)
|
|
770
771
|
});
|
|
771
|
-
|
|
772
|
-
}
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
772
|
+
connectedAddress = this.signer.address;
|
|
773
|
+
} else {
|
|
774
|
+
const provider = account;
|
|
775
|
+
if (!provider) {
|
|
776
|
+
throw new Error(
|
|
777
|
+
"Invalid account parameter. Expected private key string or wallet provider."
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
if (provider.request) {
|
|
781
|
+
const accounts = await provider.request({
|
|
782
|
+
method: "eth_requestAccounts"
|
|
783
|
+
});
|
|
784
|
+
if (!accounts || accounts.length === 0) {
|
|
785
|
+
throw new Error("No accounts found in wallet provider");
|
|
786
|
+
}
|
|
787
|
+
this.walletClient = (0, import_viem4.createWalletClient)({
|
|
788
|
+
account: accounts[0],
|
|
789
|
+
chain: chainConfig.chain,
|
|
790
|
+
transport: (0, import_viem4.custom)(provider)
|
|
791
|
+
});
|
|
792
|
+
connectedAddress = accounts[0];
|
|
793
|
+
} else if (provider.account && provider.transport) {
|
|
794
|
+
this.walletClient = (0, import_viem4.createWalletClient)({
|
|
795
|
+
account: provider.account,
|
|
796
|
+
chain: chainConfig.chain,
|
|
797
|
+
transport: provider.transport
|
|
798
|
+
});
|
|
799
|
+
connectedAddress = provider.account.address;
|
|
800
|
+
} else {
|
|
801
|
+
throw new Error(
|
|
802
|
+
"Invalid wallet provider. Expected EIP-1193 provider or viem WalletClient."
|
|
803
|
+
);
|
|
785
804
|
}
|
|
786
|
-
this.walletClient = (0, import_viem4.createWalletClient)({
|
|
787
|
-
account: accounts[0],
|
|
788
|
-
chain: chainConfig.chain,
|
|
789
|
-
transport: (0, import_viem4.custom)(provider)
|
|
790
|
-
});
|
|
791
|
-
return accounts[0];
|
|
792
|
-
}
|
|
793
|
-
if (provider.account && provider.transport) {
|
|
794
|
-
this.walletClient = (0, import_viem4.createWalletClient)({
|
|
795
|
-
account: provider.account,
|
|
796
|
-
chain: chainConfig.chain,
|
|
797
|
-
transport: provider.transport
|
|
798
|
-
});
|
|
799
|
-
return provider.account.address;
|
|
800
805
|
}
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
806
|
+
await this.authenticateUser();
|
|
807
|
+
return connectedAddress;
|
|
808
|
+
}
|
|
809
|
+
/**
|
|
810
|
+
* Disconnect account and clear authentication state
|
|
811
|
+
* Resets wallet connection, JWT token, and all authentication-related state
|
|
812
|
+
*
|
|
813
|
+
* @example
|
|
814
|
+
* ```typescript
|
|
815
|
+
* await sdk.disconnectAccount();
|
|
816
|
+
* console.log("Account disconnected");
|
|
817
|
+
* ```
|
|
818
|
+
*/
|
|
819
|
+
async disconnectAccount() {
|
|
820
|
+
this.signer = null;
|
|
821
|
+
this.walletClient = null;
|
|
822
|
+
this.isAuthenticated = false;
|
|
823
|
+
this.authenticatedUserId = null;
|
|
824
|
+
this.hasActiveSessionKey = false;
|
|
825
|
+
this.httpClient.clearAuthToken();
|
|
804
826
|
}
|
|
805
827
|
/**
|
|
806
828
|
* Get wallet client (throws if not connected)
|
|
@@ -1172,18 +1194,20 @@ var ZyfaiSDK = class {
|
|
|
1172
1194
|
}
|
|
1173
1195
|
/**
|
|
1174
1196
|
* Withdraw funds from Safe smart wallet
|
|
1175
|
-
*
|
|
1197
|
+
* Initiates a withdrawal request to the ZyFAI API
|
|
1198
|
+
* Note: The withdrawal is processed asynchronously, so txHash may not be immediately available
|
|
1176
1199
|
*
|
|
1177
1200
|
* @param userAddress - User's address (owner of the Safe)
|
|
1178
1201
|
* @param chainId - Target chain ID
|
|
1179
1202
|
* @param amount - Optional: Amount in least decimal units to withdraw (partial withdrawal). If not specified, withdraws all funds
|
|
1180
1203
|
* @param receiver - Optional: Receiver address. If not specified, sends to Safe owner
|
|
1181
|
-
* @returns Withdraw response with transaction hash
|
|
1204
|
+
* @returns Withdraw response with message and optional transaction hash (available once processed)
|
|
1182
1205
|
*
|
|
1183
1206
|
* @example
|
|
1184
1207
|
* ```typescript
|
|
1185
1208
|
* // Full withdrawal
|
|
1186
1209
|
* const result = await sdk.withdrawFunds("0xUser...", 42161);
|
|
1210
|
+
* console.log(result.message); // "Withdrawal request sent"
|
|
1187
1211
|
*
|
|
1188
1212
|
* // Partial withdrawal of 50 USDC (6 decimals)
|
|
1189
1213
|
* const result = await sdk.withdrawFunds(
|
|
@@ -1234,9 +1258,12 @@ var ZyfaiSDK = class {
|
|
|
1234
1258
|
});
|
|
1235
1259
|
}
|
|
1236
1260
|
const success = response?.success ?? true;
|
|
1261
|
+
const message = response?.message || "Withdrawal request sent";
|
|
1262
|
+
const txHash = response?.txHash || response?.transactionHash;
|
|
1237
1263
|
return {
|
|
1238
1264
|
success,
|
|
1239
|
-
|
|
1265
|
+
message,
|
|
1266
|
+
txHash,
|
|
1240
1267
|
type: amount ? "partial" : "full",
|
|
1241
1268
|
amount: amount || "all",
|
|
1242
1269
|
receiver: receiver || userAddress,
|
package/dist/index.mjs
CHANGED
|
@@ -736,6 +736,7 @@ var ZyfaiSDK = class {
|
|
|
736
736
|
this.isAuthenticated = false;
|
|
737
737
|
this.httpClient.clearAuthToken();
|
|
738
738
|
const chainConfig = getChainConfig(chainId);
|
|
739
|
+
let connectedAddress;
|
|
739
740
|
if (typeof account === "string") {
|
|
740
741
|
let privateKey = account;
|
|
741
742
|
if (!privateKey.startsWith("0x")) {
|
|
@@ -747,39 +748,60 @@ var ZyfaiSDK = class {
|
|
|
747
748
|
chain: chainConfig.chain,
|
|
748
749
|
transport: http3(chainConfig.rpcUrl)
|
|
749
750
|
});
|
|
750
|
-
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
751
|
+
connectedAddress = this.signer.address;
|
|
752
|
+
} else {
|
|
753
|
+
const provider = account;
|
|
754
|
+
if (!provider) {
|
|
755
|
+
throw new Error(
|
|
756
|
+
"Invalid account parameter. Expected private key string or wallet provider."
|
|
757
|
+
);
|
|
758
|
+
}
|
|
759
|
+
if (provider.request) {
|
|
760
|
+
const accounts = await provider.request({
|
|
761
|
+
method: "eth_requestAccounts"
|
|
762
|
+
});
|
|
763
|
+
if (!accounts || accounts.length === 0) {
|
|
764
|
+
throw new Error("No accounts found in wallet provider");
|
|
765
|
+
}
|
|
766
|
+
this.walletClient = createWalletClient({
|
|
767
|
+
account: accounts[0],
|
|
768
|
+
chain: chainConfig.chain,
|
|
769
|
+
transport: custom(provider)
|
|
770
|
+
});
|
|
771
|
+
connectedAddress = accounts[0];
|
|
772
|
+
} else if (provider.account && provider.transport) {
|
|
773
|
+
this.walletClient = createWalletClient({
|
|
774
|
+
account: provider.account,
|
|
775
|
+
chain: chainConfig.chain,
|
|
776
|
+
transport: provider.transport
|
|
777
|
+
});
|
|
778
|
+
connectedAddress = provider.account.address;
|
|
779
|
+
} else {
|
|
780
|
+
throw new Error(
|
|
781
|
+
"Invalid wallet provider. Expected EIP-1193 provider or viem WalletClient."
|
|
782
|
+
);
|
|
764
783
|
}
|
|
765
|
-
this.walletClient = createWalletClient({
|
|
766
|
-
account: accounts[0],
|
|
767
|
-
chain: chainConfig.chain,
|
|
768
|
-
transport: custom(provider)
|
|
769
|
-
});
|
|
770
|
-
return accounts[0];
|
|
771
|
-
}
|
|
772
|
-
if (provider.account && provider.transport) {
|
|
773
|
-
this.walletClient = createWalletClient({
|
|
774
|
-
account: provider.account,
|
|
775
|
-
chain: chainConfig.chain,
|
|
776
|
-
transport: provider.transport
|
|
777
|
-
});
|
|
778
|
-
return provider.account.address;
|
|
779
784
|
}
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
785
|
+
await this.authenticateUser();
|
|
786
|
+
return connectedAddress;
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Disconnect account and clear authentication state
|
|
790
|
+
* Resets wallet connection, JWT token, and all authentication-related state
|
|
791
|
+
*
|
|
792
|
+
* @example
|
|
793
|
+
* ```typescript
|
|
794
|
+
* await sdk.disconnectAccount();
|
|
795
|
+
* console.log("Account disconnected");
|
|
796
|
+
* ```
|
|
797
|
+
*/
|
|
798
|
+
async disconnectAccount() {
|
|
799
|
+
this.signer = null;
|
|
800
|
+
this.walletClient = null;
|
|
801
|
+
this.isAuthenticated = false;
|
|
802
|
+
this.authenticatedUserId = null;
|
|
803
|
+
this.hasActiveSessionKey = false;
|
|
804
|
+
this.httpClient.clearAuthToken();
|
|
783
805
|
}
|
|
784
806
|
/**
|
|
785
807
|
* Get wallet client (throws if not connected)
|
|
@@ -1151,18 +1173,20 @@ var ZyfaiSDK = class {
|
|
|
1151
1173
|
}
|
|
1152
1174
|
/**
|
|
1153
1175
|
* Withdraw funds from Safe smart wallet
|
|
1154
|
-
*
|
|
1176
|
+
* Initiates a withdrawal request to the ZyFAI API
|
|
1177
|
+
* Note: The withdrawal is processed asynchronously, so txHash may not be immediately available
|
|
1155
1178
|
*
|
|
1156
1179
|
* @param userAddress - User's address (owner of the Safe)
|
|
1157
1180
|
* @param chainId - Target chain ID
|
|
1158
1181
|
* @param amount - Optional: Amount in least decimal units to withdraw (partial withdrawal). If not specified, withdraws all funds
|
|
1159
1182
|
* @param receiver - Optional: Receiver address. If not specified, sends to Safe owner
|
|
1160
|
-
* @returns Withdraw response with transaction hash
|
|
1183
|
+
* @returns Withdraw response with message and optional transaction hash (available once processed)
|
|
1161
1184
|
*
|
|
1162
1185
|
* @example
|
|
1163
1186
|
* ```typescript
|
|
1164
1187
|
* // Full withdrawal
|
|
1165
1188
|
* const result = await sdk.withdrawFunds("0xUser...", 42161);
|
|
1189
|
+
* console.log(result.message); // "Withdrawal request sent"
|
|
1166
1190
|
*
|
|
1167
1191
|
* // Partial withdrawal of 50 USDC (6 decimals)
|
|
1168
1192
|
* const result = await sdk.withdrawFunds(
|
|
@@ -1213,9 +1237,12 @@ var ZyfaiSDK = class {
|
|
|
1213
1237
|
});
|
|
1214
1238
|
}
|
|
1215
1239
|
const success = response?.success ?? true;
|
|
1240
|
+
const message = response?.message || "Withdrawal request sent";
|
|
1241
|
+
const txHash = response?.txHash || response?.transactionHash;
|
|
1216
1242
|
return {
|
|
1217
1243
|
success,
|
|
1218
|
-
|
|
1244
|
+
message,
|
|
1245
|
+
txHash,
|
|
1219
1246
|
type: amount ? "partial" : "full",
|
|
1220
1247
|
amount: amount || "all",
|
|
1221
1248
|
receiver: receiver || userAddress,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zyfai/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
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",
|