@zyfai/sdk 1.0.0 → 1.2.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/README.md +53 -39
- package/dist/index.d.mts +89 -27
- package/dist/index.d.ts +89 -27
- package/dist/index.js +296 -80
- package/dist/index.mjs +267 -50
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -54,6 +54,9 @@ var ENDPOINTS = {
|
|
|
54
54
|
USER_WITHDRAW: "/users/withdraw/v2",
|
|
55
55
|
PARTIAL_WITHDRAW: "/users/partial-withdraw/v2",
|
|
56
56
|
LOG_DEPOSIT: "/users/log_deposit/v2",
|
|
57
|
+
FIRST_PERMIT_DEPOSIT: "/users/me/permit-deposit",
|
|
58
|
+
FIRST_PERMIT_DEPOSIT_PREPARE: "/users/me/permit-deposit/prepare",
|
|
59
|
+
FIRST_PERMIT_DEPOSIT_SUBMIT: (id) => `/users/me/permit-deposit/${encodeURIComponent(id)}/submit`,
|
|
57
60
|
FUNDING_ORDERS: "/users/me/funding-orders",
|
|
58
61
|
FUNDING_ORDER: (orderId) => `/users/me/funding-orders/${encodeURIComponent(orderId)}`,
|
|
59
62
|
FUNDING_ORDER_QUOTE: (orderId) => `/users/me/funding-orders/${encodeURIComponent(orderId)}/quote`,
|
|
@@ -127,27 +130,28 @@ var DATA_ENDPOINTS = {
|
|
|
127
130
|
if (endDate) url += `&endDate=${endDate}`;
|
|
128
131
|
return url;
|
|
129
132
|
},
|
|
130
|
-
// Opportunities
|
|
133
|
+
// Opportunities. Omitted status means live pools. Pass not_live to
|
|
134
|
+
// read retired rows. The data API returns both when status is absent.
|
|
131
135
|
OPPORTUNITIES_SAFE: (chainId, asset, status) => {
|
|
132
136
|
const params = [];
|
|
133
137
|
if (chainId !== void 0) params.push(`chainId=${chainId}`);
|
|
134
138
|
if (asset) params.push(`asset=${asset}`);
|
|
135
|
-
|
|
136
|
-
return
|
|
139
|
+
params.push(`status=${status ?? "live"}`);
|
|
140
|
+
return `/opportunities/safe?${params.join("&")}`;
|
|
137
141
|
},
|
|
138
142
|
OPPORTUNITIES_DEGEN: (chainId, asset, status) => {
|
|
139
143
|
const params = [];
|
|
140
144
|
if (chainId !== void 0) params.push(`chainId=${chainId}`);
|
|
141
145
|
if (asset) params.push(`asset=${asset}`);
|
|
142
|
-
|
|
143
|
-
return
|
|
146
|
+
params.push(`status=${status ?? "live"}`);
|
|
147
|
+
return `/opportunities/degen-strategies?${params.join("&")}`;
|
|
144
148
|
},
|
|
145
149
|
OPPORTUNITIES_ASYNC: (chainId, asset, status) => {
|
|
146
150
|
const params = [];
|
|
147
151
|
if (chainId !== void 0) params.push(`chainId=${chainId}`);
|
|
148
152
|
if (asset) params.push(`asset=${asset}`);
|
|
149
|
-
|
|
150
|
-
return
|
|
153
|
+
params.push(`status=${status ?? "live"}`);
|
|
154
|
+
return `/opportunities/async?${params.join("&")}`;
|
|
151
155
|
},
|
|
152
156
|
// APY History
|
|
153
157
|
DAILY_APY_HISTORY_WEIGHTED: (walletAddress, days) => `/daily-apy-history/weighted-multi-asset/${walletAddress}${days ? `?days=${days}` : ""}`,
|
|
@@ -261,8 +265,9 @@ var HttpClient = class {
|
|
|
261
265
|
case 403:
|
|
262
266
|
throw new Error("Forbidden: Access denied");
|
|
263
267
|
case 404:
|
|
264
|
-
throw
|
|
265
|
-
`Not found: ${data.message || "Resource not found"}`
|
|
268
|
+
throw Object.assign(
|
|
269
|
+
new Error(`Not found: ${data.message || "Resource not found"}`),
|
|
270
|
+
{ status }
|
|
266
271
|
);
|
|
267
272
|
case 429:
|
|
268
273
|
throw new Error("Rate limit exceeded. Please try again later.");
|
|
@@ -385,6 +390,20 @@ var HttpClient = class {
|
|
|
385
390
|
};
|
|
386
391
|
|
|
387
392
|
// src/utils/poll.ts
|
|
393
|
+
async function pollUntil(fn, predicate, options) {
|
|
394
|
+
const intervalMs = options?.intervalMs ?? 5e3;
|
|
395
|
+
const timeoutMs = options?.timeoutMs ?? 12e4;
|
|
396
|
+
const label = options?.label ?? "condition";
|
|
397
|
+
const started = Date.now();
|
|
398
|
+
while (Date.now() - started < timeoutMs) {
|
|
399
|
+
const value = await fn();
|
|
400
|
+
if (predicate(value)) {
|
|
401
|
+
return { value, elapsedMs: Date.now() - started };
|
|
402
|
+
}
|
|
403
|
+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
404
|
+
}
|
|
405
|
+
throw new Error(`Timed out waiting for ${label} after ${timeoutMs}ms`);
|
|
406
|
+
}
|
|
388
407
|
function sleep(ms) {
|
|
389
408
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
390
409
|
}
|
|
@@ -818,6 +837,7 @@ import {
|
|
|
818
837
|
custom,
|
|
819
838
|
http as http2,
|
|
820
839
|
getAddress as getAddress2,
|
|
840
|
+
isAddressEqual as isAddressEqual2,
|
|
821
841
|
encodeFunctionData as encodeFunctionData2,
|
|
822
842
|
defineChain
|
|
823
843
|
} from "viem";
|
|
@@ -943,7 +963,7 @@ var ASSET_CONFIGS = {
|
|
|
943
963
|
enabled: true
|
|
944
964
|
},
|
|
945
965
|
// Coinbase B20 tokenized NVIDIA equity. Base only, and only reachable through
|
|
946
|
-
// protocols with delayed withdrawals — see the `
|
|
966
|
+
// protocols with delayed withdrawals — see the `yieldmaxxing` strategy.
|
|
947
967
|
NVDAc: {
|
|
948
968
|
symbol: "NVDAc",
|
|
949
969
|
assetType: "nvdac",
|
|
@@ -1136,6 +1156,29 @@ var positiveUnits = (value) => {
|
|
|
1136
1156
|
};
|
|
1137
1157
|
var resolveCollateralMinimum = (storedMinimum, quote) => positiveUnits(storedMinimum) ?? positiveUnits(quote.details?.currencyOut?.minimumAmount) ?? positiveUnits(quote.details?.currencyOut?.amount) ?? "0";
|
|
1138
1158
|
|
|
1159
|
+
// src/deposits/first-permit-deposit.ts
|
|
1160
|
+
import { isAddressEqual } from "viem";
|
|
1161
|
+
var firstPermitTypes = {
|
|
1162
|
+
Permit: [
|
|
1163
|
+
{ name: "owner", type: "address" },
|
|
1164
|
+
{ name: "spender", type: "address" },
|
|
1165
|
+
{ name: "value", type: "uint256" },
|
|
1166
|
+
{ name: "nonce", type: "uint256" },
|
|
1167
|
+
{ name: "deadline", type: "uint256" }
|
|
1168
|
+
]
|
|
1169
|
+
};
|
|
1170
|
+
function matchesFirstPermitDeposit(intent, request) {
|
|
1171
|
+
return intent.chainId === request.chainId && isAddressEqual(intent.token, request.token) && intent.amount === request.amount.toString();
|
|
1172
|
+
}
|
|
1173
|
+
function validateFirstPermit({ domain, message }, request) {
|
|
1174
|
+
const value = BigInt(message.value);
|
|
1175
|
+
const nonce = BigInt(message.nonce);
|
|
1176
|
+
if (domain.chainId !== request.chainId || !isAddressEqual(domain.verifyingContract, request.token) || !isAddressEqual(message.owner, request.owner) || !isAddressEqual(message.spender, request.safe) || value !== request.amount || nonce < 0n || !Number.isSafeInteger(message.deadline) || message.deadline <= Math.floor(Date.now() / 1e3)) {
|
|
1177
|
+
throw new Error("Prepared permit does not match this deposit");
|
|
1178
|
+
}
|
|
1179
|
+
return { ...message, value, nonce, deadline: BigInt(message.deadline) };
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1139
1182
|
// src/deposits/relay-slippage.ts
|
|
1140
1183
|
var DEFAULT_RELAY_SLIPPAGE_BPS = 200;
|
|
1141
1184
|
var MAX_RELAY_SLIPPAGE_BPS = 500;
|
|
@@ -1507,15 +1550,15 @@ var signSessionKey = async (config, sessions, allPublicClients, signingParams) =
|
|
|
1507
1550
|
};
|
|
1508
1551
|
|
|
1509
1552
|
// src/utils/strategy.ts
|
|
1510
|
-
var PUBLIC_STRATEGIES = `"conservative", "
|
|
1553
|
+
var PUBLIC_STRATEGIES = `"conservative", "aggressive" or "yieldmaxxing"`;
|
|
1511
1554
|
var SUPPORTED_ASSETS = Object.entries(ASSET_CONFIGS).filter(([, config]) => config?.enabled === true).map(([symbol]) => `"${symbol}"`).join(", ");
|
|
1512
1555
|
function toInternalStrategy(publicStrategy) {
|
|
1513
1556
|
switch (publicStrategy) {
|
|
1514
1557
|
case "conservative":
|
|
1515
1558
|
return "safe_strategy";
|
|
1516
|
-
case "
|
|
1559
|
+
case "aggressive":
|
|
1517
1560
|
return "degen_strategy";
|
|
1518
|
-
case "
|
|
1561
|
+
case "yieldmaxxing":
|
|
1519
1562
|
return "async_strategy";
|
|
1520
1563
|
default:
|
|
1521
1564
|
throw new Error(
|
|
@@ -1528,10 +1571,10 @@ function toPublicStrategy(internalStrategy) {
|
|
|
1528
1571
|
return "conservative";
|
|
1529
1572
|
}
|
|
1530
1573
|
if (internalStrategy === "degen_strategy" || internalStrategy === "degen") {
|
|
1531
|
-
return "
|
|
1574
|
+
return "aggressive";
|
|
1532
1575
|
}
|
|
1533
1576
|
if (internalStrategy === "async_strategy" || internalStrategy === "async") {
|
|
1534
|
-
return "
|
|
1577
|
+
return "yieldmaxxing";
|
|
1535
1578
|
}
|
|
1536
1579
|
throw new Error(
|
|
1537
1580
|
`Invalid internal strategy: ${internalStrategy}. Must be "safe_strategy", "degen_strategy" or "async_strategy".`
|
|
@@ -1548,7 +1591,11 @@ function toPublicStrategyOrUndefined(internalStrategy) {
|
|
|
1548
1591
|
}
|
|
1549
1592
|
}
|
|
1550
1593
|
function isValidPublicStrategy(strategy) {
|
|
1551
|
-
return strategy === "conservative" || strategy === "
|
|
1594
|
+
return strategy === "conservative" || strategy === "aggressive" || strategy === "yieldmaxxing";
|
|
1595
|
+
}
|
|
1596
|
+
function shouldDefaultAutoSelectNewVaults(input) {
|
|
1597
|
+
if (input.requestedStrategy !== void 0) return true;
|
|
1598
|
+
return !input.hasChains && !input.hasProtocols;
|
|
1552
1599
|
}
|
|
1553
1600
|
function convertStrategyToPublic(obj) {
|
|
1554
1601
|
if (!obj.strategy) {
|
|
@@ -2320,7 +2367,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2320
2367
|
if (request.strategy) {
|
|
2321
2368
|
if (!isValidPublicStrategy(request.strategy)) {
|
|
2322
2369
|
throw new Error(
|
|
2323
|
-
`Invalid strategy: ${request.strategy}. Must be "conservative", "
|
|
2370
|
+
`Invalid strategy: ${request.strategy}. Must be "conservative", "aggressive" or "yieldmaxxing".`
|
|
2324
2371
|
);
|
|
2325
2372
|
}
|
|
2326
2373
|
rebalanceStrategy = toInternalStrategy(request.strategy);
|
|
@@ -2891,7 +2938,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2891
2938
|
*
|
|
2892
2939
|
* @param userAddress - User's EOA address (the connected EOA, not the smart wallet address)
|
|
2893
2940
|
* @param chainId - Target chain ID
|
|
2894
|
-
* @param strategy - Optional strategy selection: "conservative" (default), "
|
|
2941
|
+
* @param strategy - Optional strategy selection: "conservative" (default), "aggressive" or "yieldmaxxing"
|
|
2895
2942
|
* @param createSessionKey - If true, automatically creates a session key after deployment (default: false)
|
|
2896
2943
|
* @returns Deployment response with Safe address and transaction hash
|
|
2897
2944
|
*
|
|
@@ -3259,20 +3306,26 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3259
3306
|
internalStrategy
|
|
3260
3307
|
);
|
|
3261
3308
|
const mandateChainId = chains.find((c) => !existingChains.includes(c)) ?? chains[0];
|
|
3309
|
+
const autoSelectNewVaults = shouldDefaultAutoSelectNewVaults({
|
|
3310
|
+
requestedStrategy: strategy,
|
|
3311
|
+
hasChains: existingChains.length > 0,
|
|
3312
|
+
hasProtocols: (userDetails.protocols?.length ?? 0) > 0
|
|
3313
|
+
});
|
|
3262
3314
|
return await this.persistDiscoveredAssetProfile(
|
|
3263
3315
|
asset,
|
|
3264
3316
|
effectiveChains,
|
|
3265
3317
|
withPools,
|
|
3266
3318
|
strategy,
|
|
3267
3319
|
mandateChainId,
|
|
3268
|
-
existingChains
|
|
3320
|
+
existingChains,
|
|
3321
|
+
autoSelectNewVaults
|
|
3269
3322
|
);
|
|
3270
3323
|
}
|
|
3271
3324
|
/**
|
|
3272
3325
|
* Persist protocol discovery for one asset (user PATCH or agent deposit-setup).
|
|
3273
3326
|
* @internal
|
|
3274
3327
|
*/
|
|
3275
|
-
async persistDiscoveredAssetProfile(asset, effectiveChains, withPools, strategy, agentDepositChainId, existingChains = []) {
|
|
3328
|
+
async persistDiscoveredAssetProfile(asset, effectiveChains, withPools, strategy, agentDepositChainId, existingChains = [], autoSelectNewVaults = false) {
|
|
3276
3329
|
const internalAsset = convertAssetInternally(asset);
|
|
3277
3330
|
const assetSettings = {
|
|
3278
3331
|
chains: effectiveChains,
|
|
@@ -3281,11 +3334,15 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3281
3334
|
if (strategy !== void 0) {
|
|
3282
3335
|
assetSettings.rebalanceStrategy = toInternalStrategy(strategy);
|
|
3283
3336
|
}
|
|
3337
|
+
if (autoSelectNewVaults) {
|
|
3338
|
+
assetSettings.autoSelectProtocols = true;
|
|
3339
|
+
}
|
|
3284
3340
|
const configureProfile = () => this.updateUserProfile({
|
|
3285
3341
|
asset,
|
|
3286
3342
|
protocols: withPools,
|
|
3287
3343
|
chains: effectiveChains,
|
|
3288
|
-
...strategy !== void 0 && { strategy }
|
|
3344
|
+
...strategy !== void 0 && { strategy },
|
|
3345
|
+
...autoSelectNewVaults && { autoSelectProtocols: true }
|
|
3289
3346
|
});
|
|
3290
3347
|
if (this.sessionChannel === "agent") {
|
|
3291
3348
|
const chainId = agentDepositChainId ?? effectiveChains[effectiveChains.length - 1] ?? effectiveChains[0];
|
|
@@ -3329,7 +3386,9 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3329
3386
|
* @param params.asset - Asset to configure
|
|
3330
3387
|
* @param params.strategy - Strategy to apply; defaults to the one already
|
|
3331
3388
|
* stored for this asset (`"conservative"` on a fresh account), which lets
|
|
3332
|
-
* you recompute a protocol list without changing the strategy
|
|
3389
|
+
* you recompute a protocol list without changing the strategy. Passing a
|
|
3390
|
+
* strategy also opts the asset into vaults added later. Omitting it does
|
|
3391
|
+
* not change that opt-in.
|
|
3333
3392
|
* @param params.chains - Chains to enable. Defaults to every chain the asset
|
|
3334
3393
|
* exists on. **Additive**: chains already enabled are kept, so this cannot
|
|
3335
3394
|
* be used to disable one.
|
|
@@ -3339,11 +3398,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3339
3398
|
*
|
|
3340
3399
|
* @example
|
|
3341
3400
|
* ```typescript
|
|
3342
|
-
* // NVDAc only lives in async protocols, so it needs "
|
|
3401
|
+
* // NVDAc only lives in async protocols, so it needs "yieldmaxxing".
|
|
3343
3402
|
* // Chains default to Base, the only chain it exists on.
|
|
3344
3403
|
* const profile = await sdk.setAssetStrategy({
|
|
3345
3404
|
* asset: "NVDAc",
|
|
3346
|
-
* strategy: "
|
|
3405
|
+
* strategy: "yieldmaxxing",
|
|
3347
3406
|
* });
|
|
3348
3407
|
* console.log(profile.protocols); // Ipor + Superform
|
|
3349
3408
|
* ```
|
|
@@ -3353,7 +3412,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3353
3412
|
try {
|
|
3354
3413
|
if (strategy !== void 0 && !isValidPublicStrategy(strategy)) {
|
|
3355
3414
|
throw new Error(
|
|
3356
|
-
`Invalid strategy: ${strategy}. Must be "conservative", "
|
|
3415
|
+
`Invalid strategy: ${strategy}. Must be "conservative", "aggressive" or "yieldmaxxing".`
|
|
3357
3416
|
);
|
|
3358
3417
|
}
|
|
3359
3418
|
const supported = getAssetChainIds(asset);
|
|
@@ -3388,7 +3447,8 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3388
3447
|
* Apply a strategy and auto-select matching protocols for one or all managed
|
|
3389
3448
|
* assets ({@link getManagedAssets}). Prefer this over
|
|
3390
3449
|
* {@link updateUserProfile} when changing strategy so protocols stay aligned
|
|
3391
|
-
* with the tier (including `
|
|
3450
|
+
* with the tier (including `yieldmaxxing` async venues). The chosen strategy
|
|
3451
|
+
* also opts those assets into vaults added later.
|
|
3392
3452
|
*
|
|
3393
3453
|
* @group Portfolio and execution
|
|
3394
3454
|
*/
|
|
@@ -3396,7 +3456,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3396
3456
|
const { strategy, asset, chains } = params;
|
|
3397
3457
|
if (!isValidPublicStrategy(strategy)) {
|
|
3398
3458
|
throw new Error(
|
|
3399
|
-
`Invalid strategy: ${strategy}. Must be "conservative", "
|
|
3459
|
+
`Invalid strategy: ${strategy}. Must be "conservative", "aggressive" or "yieldmaxxing".`
|
|
3400
3460
|
);
|
|
3401
3461
|
}
|
|
3402
3462
|
const assets = asset ? [asset] : getManagedAssets();
|
|
@@ -3545,14 +3605,16 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3545
3605
|
return this.httpClient.post(ENDPOINTS.DEPLOY_CHAINS, { chainIds });
|
|
3546
3606
|
}
|
|
3547
3607
|
/**
|
|
3548
|
-
*
|
|
3608
|
+
* Deposit a collateral token the wallet already holds into the Safe.
|
|
3549
3609
|
*
|
|
3550
3610
|
* @remarks
|
|
3551
3611
|
* Use this when the connected wallet holds the strategy collateral on the
|
|
3552
3612
|
* deposit chain. {@link ZyfaiSDK.sendDeposit} calls this automatically in
|
|
3553
3613
|
* that case. A different token uses {@link ZyfaiSDK.sendSwapDeposit}.
|
|
3554
3614
|
*
|
|
3555
|
-
*
|
|
3615
|
+
* Eligible first deposits sign an ERC-2612 permit and the backend sponsors and
|
|
3616
|
+
* registers the pull. Other direct deposits confirm an ERC-20 transfer and
|
|
3617
|
+
* call {@link ZyfaiSDK.logDeposit}.
|
|
3556
3618
|
* Requires {@link ZyfaiSDK.connectAccount}. Pass the **EOA** as `userAddress`.
|
|
3557
3619
|
*
|
|
3558
3620
|
* @group Deposits and withdrawals
|
|
@@ -3576,6 +3638,9 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3576
3638
|
* ```
|
|
3577
3639
|
*/
|
|
3578
3640
|
async sendDirectDeposit(userAddress, chainId, amount, asset, strategy) {
|
|
3641
|
+
return this.sendCollateralDeposit(userAddress, chainId, amount, asset, strategy, true);
|
|
3642
|
+
}
|
|
3643
|
+
async sendCollateralDeposit(userAddress, chainId, amount, asset, strategy, allowPermit) {
|
|
3579
3644
|
try {
|
|
3580
3645
|
if (!userAddress) {
|
|
3581
3646
|
throw new Error("User address is required");
|
|
@@ -3593,7 +3658,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3593
3658
|
}
|
|
3594
3659
|
if (strategy !== void 0 && !isValidPublicStrategy(strategy)) {
|
|
3595
3660
|
throw new Error(
|
|
3596
|
-
`Invalid strategy: ${strategy}. Must be "conservative", "
|
|
3661
|
+
`Invalid strategy: ${strategy}. Must be "conservative", "aggressive" or "yieldmaxxing".`
|
|
3597
3662
|
);
|
|
3598
3663
|
}
|
|
3599
3664
|
const assetSymbol = resolveAssetSymbol(asset);
|
|
@@ -3667,6 +3732,16 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3667
3732
|
);
|
|
3668
3733
|
}
|
|
3669
3734
|
}
|
|
3735
|
+
if (allowPermit && this.isPredeployed && this.isConnectedUser(userAddress)) {
|
|
3736
|
+
const sponsored = await this.sendFirstPermitDeposit({
|
|
3737
|
+
owner: getAddress2(userAddress),
|
|
3738
|
+
safe: safeAddress,
|
|
3739
|
+
chainId,
|
|
3740
|
+
token: getAddress2(token),
|
|
3741
|
+
amount: amountBigInt
|
|
3742
|
+
});
|
|
3743
|
+
if (sponsored) return sponsored;
|
|
3744
|
+
}
|
|
3670
3745
|
const txHash = await walletClient.writeContract({
|
|
3671
3746
|
address: token,
|
|
3672
3747
|
abi: erc20TransferWriteAbi(assetSymbol),
|
|
@@ -3699,6 +3774,115 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3699
3774
|
throw new Error(`Deposit failed: ${error.message}`);
|
|
3700
3775
|
}
|
|
3701
3776
|
}
|
|
3777
|
+
/**
|
|
3778
|
+
* Read the current first-deposit permit intent after a pending or interrupted deposit.
|
|
3779
|
+
*
|
|
3780
|
+
* @remarks
|
|
3781
|
+
* Uses the execution API and the SIWE session from {@link ZyfaiSDK.connectAccount}.
|
|
3782
|
+
* A registered intent provides the deposit id for {@link ZyfaiSDK.getDepositStatus}.
|
|
3783
|
+
*
|
|
3784
|
+
* @group Deposits and withdrawals
|
|
3785
|
+
*
|
|
3786
|
+
* @param userAddress - Authenticated EOA
|
|
3787
|
+
* @returns The latest permit intent, or null when no intent exists
|
|
3788
|
+
* @throws If the address differs from the authenticated EOA or the request fails
|
|
3789
|
+
*
|
|
3790
|
+
* @example
|
|
3791
|
+
* ```typescript
|
|
3792
|
+
* const intent = await sdk.getFirstPermitDepositStatus(eoa);
|
|
3793
|
+
* if (intent?.depositId) await sdk.waitForDepositCredit(intent.depositId, intent.chainId);
|
|
3794
|
+
* ```
|
|
3795
|
+
*/
|
|
3796
|
+
async getFirstPermitDepositStatus(userAddress) {
|
|
3797
|
+
if (!this.sessionEoa || !isAddressEqual2(getAddress2(userAddress), this.sessionEoa)) {
|
|
3798
|
+
throw new Error("userAddress does not match authenticated session EOA");
|
|
3799
|
+
}
|
|
3800
|
+
return (await this.getFirstPermitDepositAvailability()).intent;
|
|
3801
|
+
}
|
|
3802
|
+
async getFirstPermitDepositAvailability(chainId) {
|
|
3803
|
+
try {
|
|
3804
|
+
return await this.httpClient.get(
|
|
3805
|
+
ENDPOINTS.FIRST_PERMIT_DEPOSIT,
|
|
3806
|
+
chainId != null ? { params: { chainId } } : void 0
|
|
3807
|
+
);
|
|
3808
|
+
} catch (error) {
|
|
3809
|
+
if (error instanceof Error && "status" in error && error.status === 404) {
|
|
3810
|
+
return { enabledTokens: [], intent: null };
|
|
3811
|
+
}
|
|
3812
|
+
throw error;
|
|
3813
|
+
}
|
|
3814
|
+
}
|
|
3815
|
+
async sendFirstPermitDeposit(request) {
|
|
3816
|
+
const { enabledTokens, intent, permitPreferred } = await this.getFirstPermitDepositAvailability(request.chainId);
|
|
3817
|
+
let id;
|
|
3818
|
+
let accepted;
|
|
3819
|
+
if (intent?.status === "queued" || intent?.status === "submitted") {
|
|
3820
|
+
if (!matchesFirstPermitDeposit(intent, request)) {
|
|
3821
|
+
throw new Error(`First permit deposit ${intent.id} is already pending for a different deposit`);
|
|
3822
|
+
}
|
|
3823
|
+
id = intent.id;
|
|
3824
|
+
accepted = intent;
|
|
3825
|
+
} else {
|
|
3826
|
+
const { owner, chainId, token, amount } = request;
|
|
3827
|
+
if (permitPreferred === false) return null;
|
|
3828
|
+
if (!enabledTokens.includes(`${chainId}:${token.toLowerCase()}`)) return null;
|
|
3829
|
+
const prepared = await this.httpClient.post(
|
|
3830
|
+
ENDPOINTS.FIRST_PERMIT_DEPOSIT_PREPARE,
|
|
3831
|
+
{ owner, chainId, token, amount: amount.toString() }
|
|
3832
|
+
);
|
|
3833
|
+
if (!prepared) return null;
|
|
3834
|
+
const message = validateFirstPermit(prepared, request);
|
|
3835
|
+
const currentWallet = this.getWalletClient();
|
|
3836
|
+
const wallet = currentWallet.chain?.id === chainId ? currentWallet : await this.walletClientForOrigin(chainId);
|
|
3837
|
+
const signature = await wallet.signTypedData({
|
|
3838
|
+
account: wallet.account,
|
|
3839
|
+
domain: prepared.domain,
|
|
3840
|
+
types: firstPermitTypes,
|
|
3841
|
+
primaryType: "Permit",
|
|
3842
|
+
message
|
|
3843
|
+
});
|
|
3844
|
+
id = prepared.id;
|
|
3845
|
+
try {
|
|
3846
|
+
accepted = await this.httpClient.post(
|
|
3847
|
+
ENDPOINTS.FIRST_PERMIT_DEPOSIT_SUBMIT(id),
|
|
3848
|
+
{ signature }
|
|
3849
|
+
);
|
|
3850
|
+
} catch (error) {
|
|
3851
|
+
accepted = await this.getFirstPermitDepositStatus(owner).catch(() => null);
|
|
3852
|
+
if (accepted?.id !== id || accepted.status === "prepared") {
|
|
3853
|
+
throw new Error(
|
|
3854
|
+
`Could not confirm first permit deposit ${id}. Check getFirstPermitDepositStatus before retrying: ${thrownMessage(error)}`
|
|
3855
|
+
);
|
|
3856
|
+
}
|
|
3857
|
+
}
|
|
3858
|
+
}
|
|
3859
|
+
let status = accepted;
|
|
3860
|
+
const { value: registered } = await pollUntil(async () => {
|
|
3861
|
+
const current = status ?? await this.getFirstPermitDepositStatus(request.owner);
|
|
3862
|
+
status = null;
|
|
3863
|
+
if (current?.id !== id || !matchesFirstPermitDeposit(current, request)) {
|
|
3864
|
+
throw new Error(`Unable to resolve first permit deposit ${id}. Check getFirstPermitDepositStatus before retrying.`);
|
|
3865
|
+
}
|
|
3866
|
+
if (current.status === "failed") {
|
|
3867
|
+
throw new Error(`First permit deposit ${id} failed`);
|
|
3868
|
+
}
|
|
3869
|
+
return current;
|
|
3870
|
+
}, (current) => current.status === "registered", {
|
|
3871
|
+
intervalMs: 3e3,
|
|
3872
|
+
label: `first permit deposit ${id} registration. Check getFirstPermitDepositStatus before retrying`
|
|
3873
|
+
});
|
|
3874
|
+
if (!registered.txHash || !registered.depositId) {
|
|
3875
|
+
throw new Error(`First permit deposit ${id} is missing its registration. Check getFirstPermitDepositStatus before retrying.`);
|
|
3876
|
+
}
|
|
3877
|
+
return {
|
|
3878
|
+
success: true,
|
|
3879
|
+
txHash: registered.txHash,
|
|
3880
|
+
smartWallet: request.safe,
|
|
3881
|
+
amount: registered.amount,
|
|
3882
|
+
route: "direct",
|
|
3883
|
+
registration: await this.getDepositStatus(registered.depositId)
|
|
3884
|
+
};
|
|
3885
|
+
}
|
|
3702
3886
|
async sendDeposit(userAddressOrParams, chainId, amount, asset, strategy) {
|
|
3703
3887
|
if (typeof userAddressOrParams !== "string") {
|
|
3704
3888
|
return this.sendRoutedDeposit(userAddressOrParams);
|
|
@@ -3987,12 +4171,13 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3987
4171
|
BigInt(params.amount)
|
|
3988
4172
|
);
|
|
3989
4173
|
await this.wrapNativeEth(params.chainId, params.amount);
|
|
3990
|
-
const sent = await this.
|
|
4174
|
+
const sent = await this.sendCollateralDeposit(
|
|
3991
4175
|
params.userAddress,
|
|
3992
4176
|
params.chainId,
|
|
3993
4177
|
params.amount,
|
|
3994
4178
|
"WETH",
|
|
3995
|
-
params.strategy
|
|
4179
|
+
params.strategy,
|
|
4180
|
+
false
|
|
3996
4181
|
);
|
|
3997
4182
|
return { ...sent, route: "wrap" };
|
|
3998
4183
|
}
|
|
@@ -4471,7 +4656,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4471
4656
|
async ensureFirstDepositSetup(strategy) {
|
|
4472
4657
|
if (strategy !== void 0 && !isValidPublicStrategy(strategy)) {
|
|
4473
4658
|
throw new Error(
|
|
4474
|
-
`Invalid strategy: ${strategy}. Must be "conservative", "
|
|
4659
|
+
`Invalid strategy: ${strategy}. Must be "conservative", "aggressive" or "yieldmaxxing".`
|
|
4475
4660
|
);
|
|
4476
4661
|
}
|
|
4477
4662
|
await this.authenticateUser();
|
|
@@ -4504,7 +4689,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4504
4689
|
const { strategy, asset, chainId } = params;
|
|
4505
4690
|
if (strategy !== void 0 && !isValidPublicStrategy(strategy)) {
|
|
4506
4691
|
throw new Error(
|
|
4507
|
-
`Invalid strategy: ${strategy}. Must be "conservative", "
|
|
4692
|
+
`Invalid strategy: ${strategy}. Must be "conservative", "aggressive" or "yieldmaxxing".`
|
|
4508
4693
|
);
|
|
4509
4694
|
}
|
|
4510
4695
|
await this.authenticateUser();
|
|
@@ -4887,7 +5072,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4887
5072
|
* Note: The withdrawal is processed asynchronously, so txHash may not be immediately available
|
|
4888
5073
|
* Funds are always withdrawn to the Safe owner's address (userAddress)
|
|
4889
5074
|
*
|
|
4890
|
-
* **Delayed withdrawals.** Positions held under the `"
|
|
5075
|
+
* **Delayed withdrawals.** Positions held under the `"yieldmaxxing"`
|
|
4891
5076
|
* strategy sit in protocols that cannot be exited on demand. For those the
|
|
4892
5077
|
* backend transfers the immediately available portion, then queues a
|
|
4893
5078
|
* redemption for the rest and forwards the funds to the EOA once the
|
|
@@ -5134,7 +5319,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5134
5319
|
* a snapshot, so net values can differ slightly from a fully live calc.
|
|
5135
5320
|
*
|
|
5136
5321
|
* **Total balance.** `portfolioByAssetType` sums deployed positions and idle
|
|
5137
|
-
* Safe balances, and nothing else. Under the `"
|
|
5322
|
+
* Safe balances, and nothing else. Under the `"yieldmaxxing"` strategy a
|
|
5138
5323
|
* redemption in flight has already left its position and has not landed in
|
|
5139
5324
|
* the Safe yet, so its funds appear in no balance field at all. That makes
|
|
5140
5325
|
* two distinct numbers:
|
|
@@ -5159,7 +5344,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5159
5344
|
* @group Portfolio and execution
|
|
5160
5345
|
*
|
|
5161
5346
|
* @remarks Data API portfolio endpoint. See {@link PortfolioDetailed.pendingAsyncWithdrawals}
|
|
5162
|
-
* when using {@link Strategy |
|
|
5347
|
+
* when using {@link Strategy | yieldmaxxing}.
|
|
5163
5348
|
*
|
|
5164
5349
|
* @param userAddress - User's EOA address
|
|
5165
5350
|
* @returns Portfolio with optional fee-adjusted balance fields
|
|
@@ -5357,7 +5542,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5357
5542
|
*
|
|
5358
5543
|
* @param crossChain - Whether to get cross-chain APY (true = omni account, false = simple account)
|
|
5359
5544
|
* @param days - Time period: 7, 14, or 30
|
|
5360
|
-
* @param strategy - Strategy type: "conservative" (default), "
|
|
5545
|
+
* @param strategy - Strategy type: "conservative" (default), "aggressive" or "yieldmaxxing"
|
|
5361
5546
|
* @param chainId - Optional chain ID filter
|
|
5362
5547
|
* @param tokenSymbol - Optional token symbol filter (e.g. "USDC", "WETH", "WBTC")
|
|
5363
5548
|
* @returns APY per strategy for a specific chain
|
|
@@ -5767,6 +5952,8 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5767
5952
|
/**
|
|
5768
5953
|
* Get conservative (low-risk) yield opportunities.
|
|
5769
5954
|
*
|
|
5955
|
+
* Omitting `status` requests live pools only. Pass `"not_live"` for retired pools.
|
|
5956
|
+
*
|
|
5770
5957
|
* @group Analytics and opportunities
|
|
5771
5958
|
*
|
|
5772
5959
|
* @remarks Data API. Partner API key only.
|
|
@@ -5812,19 +5999,21 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5812
5999
|
}
|
|
5813
6000
|
}
|
|
5814
6001
|
/**
|
|
5815
|
-
* Get
|
|
6002
|
+
* Get balanced (high-risk, high-reward) yield opportunities.
|
|
6003
|
+
*
|
|
6004
|
+
* Omitting `status` requests live pools only. Pass `"not_live"` for retired pools.
|
|
5816
6005
|
*
|
|
5817
6006
|
* @param chainId - Optional chain ID filter
|
|
5818
6007
|
* @param asset - Optional asset filter (e.g. "USDC", "WETH", "WBTC")
|
|
5819
|
-
* @returns List of
|
|
6008
|
+
* @returns List of balanced opportunities
|
|
5820
6009
|
*
|
|
5821
6010
|
* @example
|
|
5822
6011
|
* ```typescript
|
|
5823
|
-
* const opportunities = await sdk.
|
|
6012
|
+
* const opportunities = await sdk.getBalancedOpportunities(8453, "WETH");
|
|
5824
6013
|
* opportunities.data.forEach(o => console.log(o.protocolName, o.apy));
|
|
5825
6014
|
* ```
|
|
5826
6015
|
*/
|
|
5827
|
-
async
|
|
6016
|
+
async getBalancedOpportunities(chainId, asset, status) {
|
|
5828
6017
|
try {
|
|
5829
6018
|
const response = await this.httpClient.dataGet(
|
|
5830
6019
|
DATA_ENDPOINTS.OPPORTUNITIES_DEGEN(chainId, asset, status)
|
|
@@ -5850,28 +6039,43 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5850
6039
|
};
|
|
5851
6040
|
} catch (error) {
|
|
5852
6041
|
throw new Error(
|
|
5853
|
-
`Failed to get
|
|
6042
|
+
`Failed to get balanced opportunities: ${error.message}`
|
|
5854
6043
|
);
|
|
5855
6044
|
}
|
|
5856
6045
|
}
|
|
5857
6046
|
/**
|
|
5858
|
-
*
|
|
6047
|
+
* @deprecated Use {@link getBalancedOpportunities} instead. Retains the
|
|
6048
|
+
* pre-rename `getAggressiveOpportunities` entry point; same HTTP request as
|
|
6049
|
+
* balanced, with legacy `strategyType: "aggressive"` on the response and rows.
|
|
6050
|
+
*/
|
|
6051
|
+
async getAggressiveOpportunities(chainId, asset, status) {
|
|
6052
|
+
const result = await this.getBalancedOpportunities(chainId, asset, status);
|
|
6053
|
+
return {
|
|
6054
|
+
...result,
|
|
6055
|
+
strategyType: "aggressive",
|
|
6056
|
+
data: result.data.map((o) => ({ ...o, strategyType: "aggressive" }))
|
|
6057
|
+
};
|
|
6058
|
+
}
|
|
6059
|
+
/**
|
|
6060
|
+
* Get advanced yield opportunities — pools reachable only with the
|
|
5859
6061
|
* `"advanced"` strategy, including protocols whose withdrawals are
|
|
5860
6062
|
* asynchronous (Ipor, Superform). Exiting these pools takes about a day
|
|
5861
6063
|
* on Ipor and three on Superform; track the redemption through
|
|
5862
6064
|
* `getPortfolio().portfolio.pendingAsyncWithdrawals`.
|
|
5863
6065
|
*
|
|
6066
|
+
* Omitting `status` requests live pools only. Pass `"not_live"` for retired pools.
|
|
6067
|
+
*
|
|
5864
6068
|
* @param chainId - Optional chain ID filter
|
|
5865
6069
|
* @param asset - Optional asset filter (e.g. "USDC", "WETH")
|
|
5866
|
-
* @returns List of
|
|
6070
|
+
* @returns List of advanced opportunities
|
|
5867
6071
|
*
|
|
5868
6072
|
* @example
|
|
5869
6073
|
* ```typescript
|
|
5870
|
-
* const opportunities = await sdk.
|
|
6074
|
+
* const opportunities = await sdk.getAdvancedOpportunities(8453);
|
|
5871
6075
|
* opportunities.data.forEach(o => console.log(o.protocolName, o.apy));
|
|
5872
6076
|
* ```
|
|
5873
6077
|
*/
|
|
5874
|
-
async
|
|
6078
|
+
async getAdvancedOpportunities(chainId, asset, status) {
|
|
5875
6079
|
try {
|
|
5876
6080
|
const response = await this.httpClient.dataGet(
|
|
5877
6081
|
DATA_ENDPOINTS.OPPORTUNITIES_ASYNC(chainId, asset, status)
|
|
@@ -5897,10 +6101,23 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5897
6101
|
};
|
|
5898
6102
|
} catch (error) {
|
|
5899
6103
|
throw new Error(
|
|
5900
|
-
`Failed to get
|
|
6104
|
+
`Failed to get advanced opportunities: ${error.message}`
|
|
5901
6105
|
);
|
|
5902
6106
|
}
|
|
5903
6107
|
}
|
|
6108
|
+
/**
|
|
6109
|
+
* @deprecated Use {@link getAdvancedOpportunities} instead. Retains the
|
|
6110
|
+
* pre-rename `getAsyncOpportunities` entry point; same HTTP request as
|
|
6111
|
+
* advanced, with legacy `strategyType: "yieldmaxxing"` on the response and rows.
|
|
6112
|
+
*/
|
|
6113
|
+
async getAsyncOpportunities(chainId, asset, status) {
|
|
6114
|
+
const result = await this.getAdvancedOpportunities(chainId, asset, status);
|
|
6115
|
+
return {
|
|
6116
|
+
...result,
|
|
6117
|
+
strategyType: "yieldmaxxing",
|
|
6118
|
+
data: result.data.map((o) => ({ ...o, strategyType: "yieldmaxxing" }))
|
|
6119
|
+
};
|
|
6120
|
+
}
|
|
5904
6121
|
/**
|
|
5905
6122
|
* Get active conservative opportunities (status = "live") with risk and utilization data
|
|
5906
6123
|
* Returns pool info, liquidity depth (true if > 1M), utilization rate, stability metrics, avg APY, and collateral
|
|
@@ -6422,7 +6639,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
6422
6639
|
if (strategy) {
|
|
6423
6640
|
if (!isValidPublicStrategy(strategy)) {
|
|
6424
6641
|
throw new Error(
|
|
6425
|
-
`Invalid strategy: ${strategy}. Must be "conservative", "
|
|
6642
|
+
`Invalid strategy: ${strategy}. Must be "conservative", "aggressive" or "yieldmaxxing".`
|
|
6426
6643
|
);
|
|
6427
6644
|
}
|
|
6428
6645
|
internalStrategy = toInternalStrategy(strategy);
|
|
@@ -6472,7 +6689,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
6472
6689
|
try {
|
|
6473
6690
|
if (!isValidPublicStrategy(params.strategy)) {
|
|
6474
6691
|
throw new Error(
|
|
6475
|
-
`Invalid strategy: ${params.strategy}. Must be "conservative", "
|
|
6692
|
+
`Invalid strategy: ${params.strategy}. Must be "conservative", "aggressive" or "yieldmaxxing".`
|
|
6476
6693
|
);
|
|
6477
6694
|
}
|
|
6478
6695
|
const internalStrategy = toInternalStrategy(params.strategy);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zyfai/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "TypeScript SDK for self-custodial Zyfai yield agents: discover DeFi opportunities and manage deposits, withdrawals, positions, and strategies across Ethereum, Base, and Arbitrum",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|