@zyfai/sdk 0.3.0 → 0.4.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 +102 -34
- package/dist/index.d.mts +611 -78
- package/dist/index.d.ts +611 -78
- package/dist/index.js +609 -68
- package/dist/index.mjs +609 -68
- package/package.json +8 -3
package/dist/index.mjs
CHANGED
|
@@ -44,6 +44,13 @@ var ENDPOINTS = {
|
|
|
44
44
|
AUTH_CHALLENGE: "/auth/challenge",
|
|
45
45
|
// User
|
|
46
46
|
USER_ME: "/users/me",
|
|
47
|
+
USER_ASSET_TYPE_SETTINGS: "/users/asset-type-settings",
|
|
48
|
+
AGENT_DEPOSIT_SETUP: "/users/me/agent-deposit-setup",
|
|
49
|
+
USER_AGENT_DEPOSIT_INTENTS: "/users/me/agent-deposit-intents",
|
|
50
|
+
USER_AGENT_DEPOSIT_INTENT: (actionId) => `/users/me/agent-deposit-intents/${encodeURIComponent(actionId)}`,
|
|
51
|
+
USER_AGENT_DEPOSIT_INTENT_CONSUME: (actionId) => `/users/me/agent-deposit-intents/${encodeURIComponent(actionId)}/consume`,
|
|
52
|
+
USER_AGENT_DEPOSIT_INTENT_COMPLETE: (actionId) => `/users/me/agent-deposit-intents/${encodeURIComponent(actionId)}/complete`,
|
|
53
|
+
AGENT_DEPOSIT_INTENT_RESOLVE: "/agent-deposit-intents/resolve",
|
|
47
54
|
USER_WITHDRAW: "/users/withdraw",
|
|
48
55
|
PARTIAL_WITHDRAW: "/users/partial-withdraw",
|
|
49
56
|
LOG_DEPOSIT: "/users/log_deposit/v2",
|
|
@@ -169,6 +176,8 @@ var HttpClient = class {
|
|
|
169
176
|
*/
|
|
170
177
|
constructor(apiKey, config = {}) {
|
|
171
178
|
this.authToken = null;
|
|
179
|
+
/** @internal Extra headers on execution API requests (e.g. MCP server channel key). */
|
|
180
|
+
this.extraExecutionHeaders = {};
|
|
172
181
|
this.apiKey = apiKey;
|
|
173
182
|
const executionApiUrl = config.executionApiUrl ?? API_ENDPOINT;
|
|
174
183
|
this.dataApiBaseUrl = config.dataApiUrl ?? DATA_API_ENDPOINT;
|
|
@@ -197,9 +206,17 @@ var HttpClient = class {
|
|
|
197
206
|
setAuthToken(token) {
|
|
198
207
|
this.authToken = token;
|
|
199
208
|
}
|
|
209
|
+
/** @internal */
|
|
210
|
+
setExtraExecutionHeaders(headers) {
|
|
211
|
+
this.extraExecutionHeaders = headers;
|
|
212
|
+
}
|
|
200
213
|
clearAuthToken() {
|
|
201
214
|
this.authToken = null;
|
|
202
215
|
}
|
|
216
|
+
/** Whether a user Bearer token is set on this client instance. */
|
|
217
|
+
hasAuthToken() {
|
|
218
|
+
return this.authToken !== null;
|
|
219
|
+
}
|
|
203
220
|
getOrigin() {
|
|
204
221
|
return this.origin;
|
|
205
222
|
}
|
|
@@ -213,6 +230,9 @@ var HttpClient = class {
|
|
|
213
230
|
if (this.authToken) {
|
|
214
231
|
config.headers["Authorization"] = `Bearer ${this.authToken}`;
|
|
215
232
|
}
|
|
233
|
+
for (const [key, value] of Object.entries(this.extraExecutionHeaders)) {
|
|
234
|
+
config.headers[key] = value;
|
|
235
|
+
}
|
|
216
236
|
return config;
|
|
217
237
|
},
|
|
218
238
|
(error) => {
|
|
@@ -265,6 +285,10 @@ var HttpClient = class {
|
|
|
265
285
|
const response = await this.client.patch(url, data, config);
|
|
266
286
|
return response.data;
|
|
267
287
|
}
|
|
288
|
+
async put(url, data, config) {
|
|
289
|
+
const response = await this.client.put(url, data, config);
|
|
290
|
+
return response.data;
|
|
291
|
+
}
|
|
268
292
|
async delete(url, config) {
|
|
269
293
|
const response = await this.client.delete(url, config);
|
|
270
294
|
return response.data;
|
|
@@ -611,11 +635,22 @@ function isLocalExecutionApiUrl(executionApiUrl) {
|
|
|
611
635
|
return false;
|
|
612
636
|
}
|
|
613
637
|
}
|
|
638
|
+
function isStagingExecutionApiUrl(executionApiUrl) {
|
|
639
|
+
if (executionApiUrl === EXECUTION_API_BASE_URLS.staging) {
|
|
640
|
+
return true;
|
|
641
|
+
}
|
|
642
|
+
try {
|
|
643
|
+
const { hostname } = new URL(executionApiUrl);
|
|
644
|
+
return hostname === "staging-api.zyf.ai";
|
|
645
|
+
} catch {
|
|
646
|
+
return false;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
614
649
|
function shouldBypassMinPortfolioCheck(executionApiUrl) {
|
|
615
650
|
if (process.env.NODE_ENV === "production") {
|
|
616
651
|
return false;
|
|
617
652
|
}
|
|
618
|
-
return isLocalExecutionApiUrl(executionApiUrl);
|
|
653
|
+
return isLocalExecutionApiUrl(executionApiUrl) || isStagingExecutionApiUrl(executionApiUrl);
|
|
619
654
|
}
|
|
620
655
|
|
|
621
656
|
// src/core/ZyfaiSDK.ts
|
|
@@ -1519,7 +1554,7 @@ function enrichOnchainEarningsTotals(apiData) {
|
|
|
1519
1554
|
const unrealized = unrealizedByToken[symbol] ?? 0;
|
|
1520
1555
|
const current = currentByToken[symbol] ?? 0;
|
|
1521
1556
|
totalEarningsByTokenWithFee[symbol] = formatHumanAmount(
|
|
1522
|
-
lifetime + unrealized + current * keepRate
|
|
1557
|
+
(lifetime + unrealized + current) * keepRate
|
|
1523
1558
|
);
|
|
1524
1559
|
});
|
|
1525
1560
|
const chainKeys = /* @__PURE__ */ new Set([
|
|
@@ -1543,7 +1578,7 @@ function enrichOnchainEarningsTotals(apiData) {
|
|
|
1543
1578
|
const unrealized = parseHumanAmount(unrealizedTokens[symbol]);
|
|
1544
1579
|
const current = parseHumanAmount(currentTokens[symbol]);
|
|
1545
1580
|
chainMap[symbol] = formatHumanAmount(
|
|
1546
|
-
lifetime + unrealized + current * keepRate
|
|
1581
|
+
(lifetime + unrealized + current) * keepRate
|
|
1547
1582
|
);
|
|
1548
1583
|
});
|
|
1549
1584
|
totalEarningsByChainWithFee[chainId] = chainMap;
|
|
@@ -1583,15 +1618,35 @@ var DepositCreditTimeoutError = class extends Error {
|
|
|
1583
1618
|
};
|
|
1584
1619
|
var MANAGED_ASSETS = ["USDC", "WETH", "EURC", "NVDAc"];
|
|
1585
1620
|
var _ZyfaiSDK = class _ZyfaiSDK {
|
|
1621
|
+
/**
|
|
1622
|
+
* Create an SDK instance bound to a partner API key.
|
|
1623
|
+
*
|
|
1624
|
+
* @param config - API key string or {@link SDKConfig} with optional `rpcUrls` and `referralSource`
|
|
1625
|
+
*
|
|
1626
|
+
* @remarks
|
|
1627
|
+
* `@internal` fields on {@link SDKConfig} (`executionApiUrl`, `dataApiUrl`,
|
|
1628
|
+
* `bypassMinPortfolio`) are for local integration tests only.
|
|
1629
|
+
*
|
|
1630
|
+
* @throws If `apiKey` is missing
|
|
1631
|
+
*
|
|
1632
|
+
* @example
|
|
1633
|
+
* ```typescript
|
|
1634
|
+
* const sdk = new ZyfaiSDK({ apiKey: process.env.ZYFAI_API_KEY! });
|
|
1635
|
+
* ```
|
|
1636
|
+
*/
|
|
1586
1637
|
constructor(config) {
|
|
1587
1638
|
this.signer = null;
|
|
1588
1639
|
this.walletClient = null;
|
|
1589
1640
|
this.authenticatedUserId = null;
|
|
1590
1641
|
this.hasActiveSessionKey = false;
|
|
1591
1642
|
this.isPredeployed = false;
|
|
1643
|
+
/** False until SIWE login or GET /users/me has set `isPredeployed`. */
|
|
1644
|
+
this.predeployedResolved = false;
|
|
1592
1645
|
this.connectedSmartWallet = null;
|
|
1593
1646
|
this.currentProvider = null;
|
|
1594
1647
|
this.currentChainId = null;
|
|
1648
|
+
/** EOA of the active user session (bearer or SIWE). Cleared on disconnect. */
|
|
1649
|
+
this.sessionEoa = null;
|
|
1595
1650
|
const sdkConfig = typeof config === "string" ? { apiKey: config } : config;
|
|
1596
1651
|
const {
|
|
1597
1652
|
apiKey,
|
|
@@ -1613,6 +1668,84 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
1613
1668
|
this.rpcUrls = rpcUrls;
|
|
1614
1669
|
this.referralSource = referralSource;
|
|
1615
1670
|
}
|
|
1671
|
+
/**
|
|
1672
|
+
* Create an SDK instance with an existing Zyfai user JWT (no wallet connected).
|
|
1673
|
+
*
|
|
1674
|
+
* @group Authentication and wallet
|
|
1675
|
+
*/
|
|
1676
|
+
static forUser(config, session) {
|
|
1677
|
+
const sdk = new _ZyfaiSDK(config);
|
|
1678
|
+
sdk.applyUserSession(session);
|
|
1679
|
+
return sdk;
|
|
1680
|
+
}
|
|
1681
|
+
/** Alias for MCP delegated grants (scoped agent JWT). */
|
|
1682
|
+
static forDelegatedGrant(config, session) {
|
|
1683
|
+
return _ZyfaiSDK.forUser(config, session);
|
|
1684
|
+
}
|
|
1685
|
+
/**
|
|
1686
|
+
* Attach a Zyfai user JWT for API calls without `connectAccount`.
|
|
1687
|
+
*
|
|
1688
|
+
* @group Authentication and wallet
|
|
1689
|
+
*/
|
|
1690
|
+
applyUserSession(session) {
|
|
1691
|
+
if (!session.accessToken) {
|
|
1692
|
+
throw new Error("accessToken is required");
|
|
1693
|
+
}
|
|
1694
|
+
this.httpClient.setAuthToken(session.accessToken);
|
|
1695
|
+
this.authenticatedUserId = session.userId ?? "bearer-session";
|
|
1696
|
+
this.sessionEoa = session.eoa ? getAddress2(session.eoa) : null;
|
|
1697
|
+
this.sessionChannel = session.channel;
|
|
1698
|
+
this.isPredeployed = false;
|
|
1699
|
+
this.predeployedResolved = false;
|
|
1700
|
+
this.connectedSmartWallet = null;
|
|
1701
|
+
}
|
|
1702
|
+
/**
|
|
1703
|
+
* @internal Attach headers to execution API requests (MCP server agent channel).
|
|
1704
|
+
*/
|
|
1705
|
+
setExecutionRequestHeaders(headers) {
|
|
1706
|
+
this.httpClient.setExtraExecutionHeaders(headers);
|
|
1707
|
+
}
|
|
1708
|
+
/** EOA of the active user session, if one is bound. */
|
|
1709
|
+
getSessionEoa() {
|
|
1710
|
+
return this.sessionEoa;
|
|
1711
|
+
}
|
|
1712
|
+
/**
|
|
1713
|
+
* Complete SIWE login with a prepared message and signature (OAuth consent, MCP).
|
|
1714
|
+
*
|
|
1715
|
+
* @group Authentication and wallet
|
|
1716
|
+
*
|
|
1717
|
+
* @param params.message - SIWE message fields (same shape as login API body)
|
|
1718
|
+
* @param params.signature - Wallet signature over the prepared SIWE string
|
|
1719
|
+
* @param params.origin - Origin header value; must match `message.uri`
|
|
1720
|
+
*/
|
|
1721
|
+
async authenticateWithSignature(params) {
|
|
1722
|
+
const origin = params.origin ?? params.message.uri;
|
|
1723
|
+
const loginResponse = await this.httpClient.post(
|
|
1724
|
+
ENDPOINTS.AUTH_LOGIN,
|
|
1725
|
+
{
|
|
1726
|
+
message: params.message,
|
|
1727
|
+
signature: params.signature,
|
|
1728
|
+
referralSource: this.referralSource
|
|
1729
|
+
},
|
|
1730
|
+
{
|
|
1731
|
+
headers: {
|
|
1732
|
+
Origin: origin
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
);
|
|
1736
|
+
if (!loginResponse.accessToken) {
|
|
1737
|
+
throw new Error("Authentication response missing access token");
|
|
1738
|
+
}
|
|
1739
|
+
this.httpClient.setAuthToken(loginResponse.accessToken);
|
|
1740
|
+
this.authenticatedUserId = loginResponse.userId || null;
|
|
1741
|
+
this.hasActiveSessionKey = loginResponse.hasActiveSessionKey || false;
|
|
1742
|
+
this.isPredeployed = loginResponse.predeployed || false;
|
|
1743
|
+
this.predeployedResolved = true;
|
|
1744
|
+
this.connectedSmartWallet = loginResponse.smartWallet || null;
|
|
1745
|
+
this.sessionEoa = getAddress2(params.message.address);
|
|
1746
|
+
this.sessionChannel = void 0;
|
|
1747
|
+
return loginResponse;
|
|
1748
|
+
}
|
|
1616
1749
|
/**
|
|
1617
1750
|
* Warn that a legacy method is deprecated in favor of sendDeposit.
|
|
1618
1751
|
* Predeployed wallets no longer require deploySafe / createSessionKey.
|
|
@@ -1634,6 +1767,9 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
1634
1767
|
if (this.authenticatedUserId !== null) {
|
|
1635
1768
|
return;
|
|
1636
1769
|
}
|
|
1770
|
+
if (this.httpClient.hasAuthToken()) {
|
|
1771
|
+
return;
|
|
1772
|
+
}
|
|
1637
1773
|
const walletClient = this.getWalletClient();
|
|
1638
1774
|
const userAddress = getAddress2(walletClient.account.address);
|
|
1639
1775
|
const chainId = this.currentChainId || walletClient.chain?.id || 8453;
|
|
@@ -1686,7 +1822,9 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
1686
1822
|
this.authenticatedUserId = loginResponse.userId || null;
|
|
1687
1823
|
this.hasActiveSessionKey = loginResponse.hasActiveSessionKey || false;
|
|
1688
1824
|
this.isPredeployed = loginResponse.predeployed || false;
|
|
1825
|
+
this.predeployedResolved = true;
|
|
1689
1826
|
this.connectedSmartWallet = loginResponse.smartWallet || null;
|
|
1827
|
+
this.sessionEoa = userAddress;
|
|
1690
1828
|
} catch (error) {
|
|
1691
1829
|
throw new Error(
|
|
1692
1830
|
`Failed to authenticate user: ${error.message}`
|
|
@@ -1697,8 +1835,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
1697
1835
|
* Update user profile with Smart Wallet address and chain configuration
|
|
1698
1836
|
* This method requires SIWE authentication and is automatically called after deploySafe
|
|
1699
1837
|
*
|
|
1700
|
-
* @
|
|
1701
|
-
*
|
|
1838
|
+
* @group Portfolio and execution
|
|
1839
|
+
*
|
|
1840
|
+
* @param request - Profile fields to update (per-asset settings supported)
|
|
1841
|
+
* @returns Updated user profile from the execution API
|
|
1842
|
+
*
|
|
1843
|
+
* @throws If strategy is invalid or authentication fails
|
|
1702
1844
|
*
|
|
1703
1845
|
* @example
|
|
1704
1846
|
* ```typescript
|
|
@@ -1759,8 +1901,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
1759
1901
|
* Pause the agent by clearing all protocols
|
|
1760
1902
|
* Sets the user's protocols to an empty array, effectively pausing automated operations
|
|
1761
1903
|
*
|
|
1904
|
+
* @group Portfolio and execution
|
|
1905
|
+
*
|
|
1762
1906
|
* @returns Response indicating success and updated user details
|
|
1763
1907
|
*
|
|
1908
|
+
* @remarks Requires {@link ZyfaiSDK.connectAccount} (SIWE).
|
|
1909
|
+
*
|
|
1764
1910
|
* @example
|
|
1765
1911
|
* ```typescript
|
|
1766
1912
|
* const sdk = new ZyfaiSDK({ apiKey: 'your-api-key' });
|
|
@@ -1791,8 +1937,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
1791
1937
|
* Resume the agent by restoring protocols based on user's strategy for each asset
|
|
1792
1938
|
* Fetches available protocols and assigns them based on each asset's strategy
|
|
1793
1939
|
*
|
|
1940
|
+
* @group Portfolio and execution
|
|
1941
|
+
*
|
|
1794
1942
|
* @returns Response indicating success and updated user details
|
|
1795
1943
|
*
|
|
1944
|
+
* @remarks Requires {@link ZyfaiSDK.connectAccount} (SIWE).
|
|
1945
|
+
*
|
|
1796
1946
|
* @example
|
|
1797
1947
|
* ```typescript
|
|
1798
1948
|
* const sdk = new ZyfaiSDK({ apiKey: 'your-api-key' });
|
|
@@ -1831,9 +1981,16 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
1831
1981
|
* Enable splitting for the user's account
|
|
1832
1982
|
* When enabled, deposits are split across multiple protocols based on minSplits setting
|
|
1833
1983
|
*
|
|
1834
|
-
* @
|
|
1984
|
+
* @group Portfolio and execution
|
|
1985
|
+
*
|
|
1986
|
+
* @param minSplits - Minimum protocols to split across (max 4)
|
|
1987
|
+
* @defaultValue 1
|
|
1835
1988
|
* @returns Response indicating success and updated user details
|
|
1836
1989
|
*
|
|
1990
|
+
* @remarks Requires {@link ZyfaiSDK.connectAccount} (SIWE).
|
|
1991
|
+
*
|
|
1992
|
+
* @throws If `minSplits` exceeds 4
|
|
1993
|
+
*
|
|
1837
1994
|
* @example
|
|
1838
1995
|
* ```typescript
|
|
1839
1996
|
* const sdk = new ZyfaiSDK({ apiKey: 'your-api-key' });
|
|
@@ -1866,8 +2023,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
1866
2023
|
* Disable splitting for the user's account
|
|
1867
2024
|
* When disabled, deposits will not be split across multiple protocols
|
|
1868
2025
|
*
|
|
2026
|
+
* @group Portfolio and execution
|
|
2027
|
+
*
|
|
1869
2028
|
* @returns Response indicating success and updated user details
|
|
1870
2029
|
*
|
|
2030
|
+
* @remarks Requires {@link ZyfaiSDK.connectAccount} (SIWE).
|
|
2031
|
+
*
|
|
1871
2032
|
* @example
|
|
1872
2033
|
* ```typescript
|
|
1873
2034
|
* const sdk = new ZyfaiSDK({ apiKey: 'your-api-key' });
|
|
@@ -1896,9 +2057,13 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
1896
2057
|
* Update the minimum number of splits for the user's account
|
|
1897
2058
|
* This controls across how many protocols deposits should be distributed
|
|
1898
2059
|
*
|
|
1899
|
-
* @
|
|
2060
|
+
* @group Portfolio and execution
|
|
2061
|
+
*
|
|
2062
|
+
* @param minSplits - Minimum number of protocols to split across (at least 1)
|
|
1900
2063
|
* @returns Response indicating success and updated user details
|
|
1901
2064
|
*
|
|
2065
|
+
* @throws If `minSplits` is less than 1
|
|
2066
|
+
*
|
|
1902
2067
|
* @example
|
|
1903
2068
|
* ```typescript
|
|
1904
2069
|
* const sdk = new ZyfaiSDK({ apiKey: 'your-api-key' });
|
|
@@ -1977,6 +2142,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
1977
2142
|
}
|
|
1978
2143
|
this.authenticatedUserId = null;
|
|
1979
2144
|
this.hasActiveSessionKey = false;
|
|
2145
|
+
this.sessionEoa = null;
|
|
1980
2146
|
this.httpClient.clearAuthToken();
|
|
1981
2147
|
if (this.walletClient && this.currentProvider) {
|
|
1982
2148
|
const chainConfig = getChainConfig(
|
|
@@ -2000,12 +2166,20 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2000
2166
|
}
|
|
2001
2167
|
}
|
|
2002
2168
|
/**
|
|
2003
|
-
* Connect account for signing
|
|
2004
|
-
*
|
|
2169
|
+
* Connect an account for signing (SIWE and on-chain transfers).
|
|
2170
|
+
*
|
|
2171
|
+
* @remarks
|
|
2172
|
+
* Required before user-scoped execution calls (deposits, withdrawals, profile).
|
|
2173
|
+
* Data API reads typically need only the API key.
|
|
2005
2174
|
*
|
|
2006
|
-
* @
|
|
2007
|
-
*
|
|
2008
|
-
* @
|
|
2175
|
+
* @group Authentication and wallet
|
|
2176
|
+
*
|
|
2177
|
+
* @param account - Private key hex string or EIP-1193 wallet provider
|
|
2178
|
+
* @param chainId - Initial chain for RPC and SIWE
|
|
2179
|
+
* @defaultValue 8453
|
|
2180
|
+
* @returns The connected EOA address (checksum)
|
|
2181
|
+
*
|
|
2182
|
+
* @throws If the chain is unsupported or the provider returns no accounts
|
|
2009
2183
|
*
|
|
2010
2184
|
* @example
|
|
2011
2185
|
* // With private key
|
|
@@ -2022,8 +2196,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2022
2196
|
}
|
|
2023
2197
|
this.authenticatedUserId = null;
|
|
2024
2198
|
this.isPredeployed = false;
|
|
2199
|
+
this.predeployedResolved = false;
|
|
2025
2200
|
this.connectedSmartWallet = null;
|
|
2026
2201
|
this.currentChainId = null;
|
|
2202
|
+
this.sessionEoa = null;
|
|
2203
|
+
this.sessionChannel = void 0;
|
|
2027
2204
|
this.httpClient.clearAuthToken();
|
|
2028
2205
|
if (this.currentProvider?.removeAllListeners) {
|
|
2029
2206
|
try {
|
|
@@ -2091,8 +2268,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2091
2268
|
return connectedAddress;
|
|
2092
2269
|
}
|
|
2093
2270
|
/**
|
|
2094
|
-
*
|
|
2095
|
-
*
|
|
2271
|
+
* Clear the connected wallet, SIWE session, and cached smart-wallet state.
|
|
2272
|
+
*
|
|
2273
|
+
* @group Authentication and wallet
|
|
2274
|
+
*
|
|
2275
|
+
* @returns Resolves when local SDK state is cleared
|
|
2096
2276
|
*
|
|
2097
2277
|
* @example
|
|
2098
2278
|
* ```typescript
|
|
@@ -2114,7 +2294,10 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2114
2294
|
this.authenticatedUserId = null;
|
|
2115
2295
|
this.hasActiveSessionKey = false;
|
|
2116
2296
|
this.isPredeployed = false;
|
|
2297
|
+
this.predeployedResolved = false;
|
|
2117
2298
|
this.connectedSmartWallet = null;
|
|
2299
|
+
this.sessionEoa = null;
|
|
2300
|
+
this.sessionChannel = void 0;
|
|
2118
2301
|
this.httpClient.clearAuthToken();
|
|
2119
2302
|
}
|
|
2120
2303
|
/**
|
|
@@ -2141,13 +2324,25 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2141
2324
|
}
|
|
2142
2325
|
}
|
|
2143
2326
|
/**
|
|
2144
|
-
* True when userAddress is the
|
|
2327
|
+
* True when userAddress is the signer, wallet client, or agent session EOA.
|
|
2145
2328
|
* @private
|
|
2146
2329
|
*/
|
|
2147
2330
|
isConnectedUser(userAddress) {
|
|
2148
|
-
const connected = this.signer?.address ?? this.walletClient?.account?.address;
|
|
2331
|
+
const connected = this.signer?.address ?? this.walletClient?.account?.address ?? this.sessionEoa;
|
|
2149
2332
|
return !!connected && connected.toLowerCase() === userAddress.toLowerCase();
|
|
2150
2333
|
}
|
|
2334
|
+
/**
|
|
2335
|
+
* SIWE login sets predeployed. Agent sessions (`forUser`) do not, so read it
|
|
2336
|
+
* from the user record before allowing a transfer to an undeployed pool Safe.
|
|
2337
|
+
* @private
|
|
2338
|
+
*/
|
|
2339
|
+
async hydrateSessionWallet() {
|
|
2340
|
+
if (this.predeployedResolved || !this.sessionEoa) return;
|
|
2341
|
+
const me = await this.httpClient.get(ENDPOINTS.USER_ME);
|
|
2342
|
+
this.isPredeployed = !!me?.predeployed;
|
|
2343
|
+
this.connectedSmartWallet = me?.smartWallet || null;
|
|
2344
|
+
this.predeployedResolved = true;
|
|
2345
|
+
}
|
|
2151
2346
|
/**
|
|
2152
2347
|
* Resolve the Safe address: backend-assigned for the connected predeployed
|
|
2153
2348
|
* user, registered agent for everyone else. If none, CREATE2-derive when
|
|
@@ -2181,15 +2376,19 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2181
2376
|
});
|
|
2182
2377
|
}
|
|
2183
2378
|
/**
|
|
2184
|
-
*
|
|
2185
|
-
*
|
|
2186
|
-
*
|
|
2379
|
+
* Resolve the Safe smart wallet for an EOA on a chain.
|
|
2380
|
+
*
|
|
2381
|
+
* @remarks
|
|
2382
|
+
* Predeployed pool wallets use a backend-assigned address; legacy wallets use
|
|
2383
|
+
* a deterministic CREATE2 address.
|
|
2384
|
+
*
|
|
2385
|
+
* @group Authentication and wallet
|
|
2187
2386
|
*
|
|
2188
2387
|
* @param userAddress - User's EOA address
|
|
2189
2388
|
* @param chainId - Target chain ID
|
|
2190
|
-
* @returns Smart wallet address, deployment status, and
|
|
2191
|
-
*
|
|
2192
|
-
*
|
|
2389
|
+
* @returns Smart wallet address, deployment status, and on-chain ownership flag
|
|
2390
|
+
*
|
|
2391
|
+
* @throws If `userAddress` is missing or `chainId` is unsupported
|
|
2193
2392
|
*/
|
|
2194
2393
|
async getSmartWalletAddress(userAddress, chainId) {
|
|
2195
2394
|
if (!userAddress) {
|
|
@@ -2221,10 +2420,14 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2221
2420
|
/**
|
|
2222
2421
|
* Deploy Safe Smart Wallet for a user
|
|
2223
2422
|
*
|
|
2224
|
-
* @deprecated Prefer
|
|
2423
|
+
* @deprecated Prefer {@link ZyfaiSDK.sendDeposit}. Predeployed Safes and session keys are
|
|
2225
2424
|
* managed automatically on first deposit. This method remains available for
|
|
2226
2425
|
* legacy flows.
|
|
2227
2426
|
*
|
|
2427
|
+
* @see {@link ZyfaiSDK.sendDeposit}
|
|
2428
|
+
*
|
|
2429
|
+
* @group Deposits and withdrawals
|
|
2430
|
+
*
|
|
2228
2431
|
* @param userAddress - User's EOA address (the connected EOA, not the smart wallet address)
|
|
2229
2432
|
* @param chainId - Target chain ID
|
|
2230
2433
|
* @param strategy - Optional strategy selection: "conservative" (default), "aggressive" or "yieldmaxxing"
|
|
@@ -2361,10 +2564,14 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2361
2564
|
* Create session key with auto-fetched configuration from Zyfai API
|
|
2362
2565
|
* This is the simplified method that automatically fetches session configuration
|
|
2363
2566
|
*
|
|
2364
|
-
* @deprecated Prefer
|
|
2567
|
+
* @deprecated Prefer {@link ZyfaiSDK.sendDeposit}. Predeployed wallets already have the
|
|
2365
2568
|
* agent session enabled; session activation is handled after first deposit.
|
|
2366
2569
|
* This method remains available for legacy flows.
|
|
2367
2570
|
*
|
|
2571
|
+
* @see {@link ZyfaiSDK.sendDeposit}
|
|
2572
|
+
*
|
|
2573
|
+
* @group Deposits and withdrawals
|
|
2574
|
+
*
|
|
2368
2575
|
* @param userAddress - User's EOA or Safe address
|
|
2369
2576
|
* @param chainId - Target chain ID
|
|
2370
2577
|
* @returns Session key response with signature and nonces
|
|
@@ -2590,6 +2797,43 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2590
2797
|
asset,
|
|
2591
2798
|
internalStrategy
|
|
2592
2799
|
);
|
|
2800
|
+
const mandateChainId = chains.find((c) => !existingChains.includes(c)) ?? chains[0];
|
|
2801
|
+
return await this.persistDiscoveredAssetProfile(
|
|
2802
|
+
asset,
|
|
2803
|
+
effectiveChains,
|
|
2804
|
+
withPools,
|
|
2805
|
+
strategy,
|
|
2806
|
+
mandateChainId
|
|
2807
|
+
);
|
|
2808
|
+
}
|
|
2809
|
+
/**
|
|
2810
|
+
* Persist protocol discovery for one asset (user PATCH or agent deposit-setup).
|
|
2811
|
+
* @internal
|
|
2812
|
+
*/
|
|
2813
|
+
async persistDiscoveredAssetProfile(asset, effectiveChains, withPools, strategy, agentDepositChainId) {
|
|
2814
|
+
const internalAsset = convertAssetInternally(asset);
|
|
2815
|
+
const assetSettings = {
|
|
2816
|
+
chains: effectiveChains,
|
|
2817
|
+
protocols: withPools
|
|
2818
|
+
};
|
|
2819
|
+
if (strategy !== void 0) {
|
|
2820
|
+
assetSettings.rebalanceStrategy = toInternalStrategy(strategy);
|
|
2821
|
+
}
|
|
2822
|
+
if (this.sessionChannel === "agent") {
|
|
2823
|
+
const chainId = agentDepositChainId ?? effectiveChains[effectiveChains.length - 1] ?? effectiveChains[0];
|
|
2824
|
+
await this.httpClient.post(
|
|
2825
|
+
ENDPOINTS.AGENT_DEPOSIT_SETUP,
|
|
2826
|
+
{
|
|
2827
|
+
asset,
|
|
2828
|
+
chainId,
|
|
2829
|
+
strategy,
|
|
2830
|
+
assetTypeSettings: {
|
|
2831
|
+
[internalAsset]: assetSettings
|
|
2832
|
+
}
|
|
2833
|
+
}
|
|
2834
|
+
);
|
|
2835
|
+
return await this.getUserDetails(asset);
|
|
2836
|
+
}
|
|
2593
2837
|
return await this.updateUserProfile({
|
|
2594
2838
|
asset,
|
|
2595
2839
|
protocols: withPools,
|
|
@@ -2618,6 +2862,8 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2618
2862
|
* @param params.chains - Chains to enable. Defaults to every chain the asset
|
|
2619
2863
|
* exists on. **Additive**: chains already enabled are kept, so this cannot
|
|
2620
2864
|
* be used to disable one.
|
|
2865
|
+
* @group Portfolio and execution
|
|
2866
|
+
*
|
|
2621
2867
|
* @returns The updated profile for that asset
|
|
2622
2868
|
*
|
|
2623
2869
|
* @example
|
|
@@ -2735,7 +2981,16 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2735
2981
|
* transfer lands on the counterfactual address and the backend deploys + hands over on first
|
|
2736
2982
|
* deposit (report-deposit). Use this only to pre-provision chains ahead of time.
|
|
2737
2983
|
*
|
|
2738
|
-
* @
|
|
2984
|
+
* @group Deposits and withdrawals
|
|
2985
|
+
*
|
|
2986
|
+
* @param chainIds - Chain IDs to deploy the wallet on (e.g. `[1, 42161]`)
|
|
2987
|
+
* @returns Deployment summary from the execution API
|
|
2988
|
+
*
|
|
2989
|
+
* @throws If `chainIds` is empty or contains an unsupported chain
|
|
2990
|
+
*
|
|
2991
|
+
* @remarks
|
|
2992
|
+
* Requires {@link ZyfaiSDK.connectAccount} (SIWE). Optional ahead of deposits;
|
|
2993
|
+
* {@link ZyfaiSDK.sendDeposit} can deploy on first deposit for predeployed wallets.
|
|
2739
2994
|
*/
|
|
2740
2995
|
async deployOnChains(chainIds) {
|
|
2741
2996
|
if (!chainIds?.length) {
|
|
@@ -2748,10 +3003,33 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2748
3003
|
return this.httpClient.post(ENDPOINTS.DEPLOY_CHAINS, { chainIds });
|
|
2749
3004
|
}
|
|
2750
3005
|
/**
|
|
2751
|
-
*
|
|
3006
|
+
* Transfer funds from the connected EOA to the user's Safe and register the deposit.
|
|
3007
|
+
*
|
|
3008
|
+
* @remarks
|
|
3009
|
+
* Execution API + on-chain ERC-20 `transfer`. Confirms the transaction, then calls
|
|
3010
|
+
* {@link ZyfaiSDK.logDeposit}. Pair with {@link ZyfaiSDK.waitForDepositCredit} for
|
|
3011
|
+
* investable balance. Custom-wallet flows: {@link ZyfaiSDK.buildDepositTransfer},
|
|
3012
|
+
* {@link ZyfaiSDK.logDeposit}, {@link ZyfaiSDK.getDepositStatus}.
|
|
3013
|
+
*
|
|
3014
|
+
* Requires {@link ZyfaiSDK.connectAccount}. Pass the **EOA** as `userAddress`.
|
|
3015
|
+
*
|
|
3016
|
+
* @group Deposits and withdrawals
|
|
3017
|
+
*
|
|
3018
|
+
* @param userAddress - Owner EOA (not the Safe)
|
|
3019
|
+
* @param chainId - Chain for the transfer
|
|
3020
|
+
* @param amount - Amount in least units (string integer)
|
|
3021
|
+
* @param asset - Asset symbol (`USDC`, `WETH`, `EURC`, `NVDAc`)
|
|
3022
|
+
* @param strategy - Optional first-deposit {@link Strategy}
|
|
3023
|
+
* @returns Transfer receipt and deposit registration (not yet credited until polled)
|
|
3024
|
+
*
|
|
3025
|
+
* @throws If validation fails, minimum portfolio is not met, Safe is unavailable, or transfer/register fails
|
|
2752
3026
|
*
|
|
2753
|
-
*
|
|
2754
|
-
*
|
|
3027
|
+
* @example
|
|
3028
|
+
* ```typescript
|
|
3029
|
+
* await sdk.connectAccount(provider);
|
|
3030
|
+
* const sent = await sdk.sendDeposit(eoa, 8453, "10000000", "USDC", "conservative");
|
|
3031
|
+
* await sdk.waitForDepositCredit(sent.registration.id, 8453);
|
|
3032
|
+
* ```
|
|
2755
3033
|
*/
|
|
2756
3034
|
async sendDeposit(userAddress, chainId, amount, asset, strategy) {
|
|
2757
3035
|
try {
|
|
@@ -2802,6 +3080,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2802
3080
|
} else {
|
|
2803
3081
|
await this.authenticateUser();
|
|
2804
3082
|
}
|
|
3083
|
+
await this.hydrateSessionWallet();
|
|
2805
3084
|
const safeAddress = await this.getSafeAddressFor(userAddress, chainId);
|
|
2806
3085
|
if (!safeAddress) {
|
|
2807
3086
|
throw new Error("Smart wallet address is not available");
|
|
@@ -2870,8 +3149,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2870
3149
|
}
|
|
2871
3150
|
}
|
|
2872
3151
|
/**
|
|
2873
|
-
* @deprecated Compatibility convenience method. Prefer
|
|
2874
|
-
*
|
|
3152
|
+
* @deprecated Compatibility convenience method. Prefer {@link ZyfaiSDK.sendDeposit} followed by
|
|
3153
|
+
* {@link ZyfaiSDK.waitForDepositCredit} when the caller owns the completion UX.
|
|
3154
|
+
*
|
|
3155
|
+
* @see {@link ZyfaiSDK.sendDeposit}
|
|
3156
|
+
*
|
|
3157
|
+
* @group Deposits and withdrawals
|
|
2875
3158
|
*/
|
|
2876
3159
|
async depositFunds(userAddress, chainId, amount, asset, strategy) {
|
|
2877
3160
|
this.warnDeprecatedOnboardingMethod("depositFunds");
|
|
@@ -2891,10 +3174,21 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2891
3174
|
};
|
|
2892
3175
|
}
|
|
2893
3176
|
/**
|
|
2894
|
-
*
|
|
2895
|
-
*
|
|
2896
|
-
*
|
|
2897
|
-
*
|
|
3177
|
+
* Build ERC-20 transfer calldata for a custom signer or sponsored tx flow.
|
|
3178
|
+
*
|
|
3179
|
+
* @remarks
|
|
3180
|
+
* Compose with {@link ZyfaiSDK.ensureFirstDepositSetup}, submit the transfer, then
|
|
3181
|
+
* {@link ZyfaiSDK.logDeposit} and {@link ZyfaiSDK.waitForDepositCredit}.
|
|
3182
|
+
*
|
|
3183
|
+
* @group Deposits and withdrawals
|
|
3184
|
+
*
|
|
3185
|
+
* @param params.userAddress - Owner EOA
|
|
3186
|
+
* @param params.chainId - Target chain
|
|
3187
|
+
* @param params.amount - Least units (string integer)
|
|
3188
|
+
* @param params.asset - {@link SupportedAsset}
|
|
3189
|
+
* @returns Safe address, token, and encoded `transfer` calldata
|
|
3190
|
+
*
|
|
3191
|
+
* @throws If inputs are invalid or the Safe address cannot be resolved
|
|
2898
3192
|
*/
|
|
2899
3193
|
async buildDepositTransfer(params) {
|
|
2900
3194
|
const { userAddress, chainId, amount, asset } = params;
|
|
@@ -2913,6 +3207,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2913
3207
|
)}.`
|
|
2914
3208
|
);
|
|
2915
3209
|
}
|
|
3210
|
+
await this.hydrateSessionWallet();
|
|
2916
3211
|
const safeAddress = await this.getSafeAddressFor(userAddress, chainId);
|
|
2917
3212
|
if (!safeAddress) {
|
|
2918
3213
|
throw new Error("Smart wallet address is not available");
|
|
@@ -3001,6 +3296,13 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3001
3296
|
* When setup runs (`applied: true`), protocol configuration is persisted
|
|
3002
3297
|
* before returning. Failures throw so callers can abort before submitting a
|
|
3003
3298
|
* transfer. High-level deposit methods still treat setup as best-effort.
|
|
3299
|
+
*
|
|
3300
|
+
* @group Deposits and withdrawals
|
|
3301
|
+
*
|
|
3302
|
+
* @param strategy - Optional first-deposit {@link Strategy}
|
|
3303
|
+
* @returns `{ applied: true }` when chain/protocol config was written; `false` if already configured
|
|
3304
|
+
*
|
|
3305
|
+
* @throws If strategy is invalid or setup did not persist
|
|
3004
3306
|
*/
|
|
3005
3307
|
async ensureFirstDepositSetup(strategy) {
|
|
3006
3308
|
if (strategy !== void 0 && !isValidPublicStrategy(strategy)) {
|
|
@@ -3022,6 +3324,67 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3022
3324
|
}
|
|
3023
3325
|
return { applied: true };
|
|
3024
3326
|
}
|
|
3327
|
+
/**
|
|
3328
|
+
* Idempotently configure the deposit asset for a single chain before MCP prepare.
|
|
3329
|
+
*
|
|
3330
|
+
* @internal
|
|
3331
|
+
*/
|
|
3332
|
+
async ensureDepositProfileForPrepare(params) {
|
|
3333
|
+
const { strategy, asset, chainId } = params;
|
|
3334
|
+
if (strategy !== void 0 && !isValidPublicStrategy(strategy)) {
|
|
3335
|
+
throw new Error(
|
|
3336
|
+
`Invalid strategy: ${strategy}. Must be "conservative", "aggressive" or "yieldmaxxing".`
|
|
3337
|
+
);
|
|
3338
|
+
}
|
|
3339
|
+
await this.authenticateUser();
|
|
3340
|
+
const details = await this.getUserDetails(asset);
|
|
3341
|
+
if (details.chains?.includes(chainId)) {
|
|
3342
|
+
return { applied: false };
|
|
3343
|
+
}
|
|
3344
|
+
const allProtocols = await this.httpClient.get(
|
|
3345
|
+
ENDPOINTS.PROTOCOLS()
|
|
3346
|
+
);
|
|
3347
|
+
await this.updateUserProtocolsForAsset(
|
|
3348
|
+
asset,
|
|
3349
|
+
[chainId],
|
|
3350
|
+
strategy,
|
|
3351
|
+
allProtocols
|
|
3352
|
+
);
|
|
3353
|
+
const afterSetup = await this.getUserDetails(asset);
|
|
3354
|
+
if (!afterSetup.chains?.includes(chainId)) {
|
|
3355
|
+
throw new Error(
|
|
3356
|
+
`First-deposit setup did not persist ${asset} chain ${chainId} configuration.`
|
|
3357
|
+
);
|
|
3358
|
+
}
|
|
3359
|
+
return { applied: true };
|
|
3360
|
+
}
|
|
3361
|
+
/**
|
|
3362
|
+
* Prepare a deposit: persist first-deposit profile when needed and return
|
|
3363
|
+
* ERC-20 transfer calldata for the user to sign (does not broadcast).
|
|
3364
|
+
*
|
|
3365
|
+
* @group Deposits and withdrawals
|
|
3366
|
+
*/
|
|
3367
|
+
async prepareDeposit(params) {
|
|
3368
|
+
const { userAddress, chainId, amount, asset } = params;
|
|
3369
|
+
if (this.sessionEoa && getAddress2(userAddress) !== this.sessionEoa) {
|
|
3370
|
+
throw new Error("userAddress does not match authenticated session EOA");
|
|
3371
|
+
}
|
|
3372
|
+
const setup = await this.ensureDepositProfileForPrepare({
|
|
3373
|
+
asset,
|
|
3374
|
+
chainId
|
|
3375
|
+
});
|
|
3376
|
+
const transfer = await this.buildDepositTransfer({
|
|
3377
|
+
userAddress,
|
|
3378
|
+
chainId,
|
|
3379
|
+
amount,
|
|
3380
|
+
asset
|
|
3381
|
+
});
|
|
3382
|
+
return {
|
|
3383
|
+
phase: "prepare_transfer",
|
|
3384
|
+
setup,
|
|
3385
|
+
transfer
|
|
3386
|
+
};
|
|
3387
|
+
}
|
|
3025
3388
|
/** Best-effort first-deposit setup shared by both high-level deposit flows. */
|
|
3026
3389
|
async prepareFirstDeposit(strategy) {
|
|
3027
3390
|
try {
|
|
@@ -3077,8 +3440,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3077
3440
|
* @param chainId - Chain ID where the deposit was made
|
|
3078
3441
|
* @param txHash - Transaction hash of the deposit
|
|
3079
3442
|
* @param amount - Amount in least decimal units (e.g., "100000000" for 100 USDC with 6 decimals)
|
|
3080
|
-
* @param tokenAddress - Optional
|
|
3081
|
-
* @returns Log deposit response with
|
|
3443
|
+
* @param tokenAddress - Optional token contract (defaults per chain)
|
|
3444
|
+
* @returns Log deposit response with lifecycle record
|
|
3445
|
+
*
|
|
3446
|
+
* @group Deposits and withdrawals
|
|
3447
|
+
*
|
|
3448
|
+
* @throws If inputs are invalid or the execution API rejects registration
|
|
3082
3449
|
*
|
|
3083
3450
|
* @example
|
|
3084
3451
|
* ```typescript
|
|
@@ -3129,8 +3496,18 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3129
3496
|
}
|
|
3130
3497
|
}
|
|
3131
3498
|
/**
|
|
3132
|
-
* Fetch
|
|
3133
|
-
*
|
|
3499
|
+
* Fetch custody handover and balance-credit status for a registered deposit.
|
|
3500
|
+
*
|
|
3501
|
+
* @remarks
|
|
3502
|
+
* `status === "credited"` with `balanceCredited === true` means funds are investable.
|
|
3503
|
+
* Requires {@link ZyfaiSDK.connectAccount} (SIWE).
|
|
3504
|
+
*
|
|
3505
|
+
* @group Deposits and withdrawals
|
|
3506
|
+
*
|
|
3507
|
+
* @param depositId - Id from {@link LogDepositResponse} / {@link DepositResponse.registration}
|
|
3508
|
+
* @returns {@link DepositLifecycleResponse}
|
|
3509
|
+
*
|
|
3510
|
+
* @throws If `depositId` is missing
|
|
3134
3511
|
*/
|
|
3135
3512
|
async getDepositStatus(depositId) {
|
|
3136
3513
|
if (!depositId) {
|
|
@@ -3152,7 +3529,17 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3152
3529
|
* ~250ms) with a ~20 second completion window. Override `timeoutMs` when a
|
|
3153
3530
|
* caller intentionally wants to wait longer than the normal UX window.
|
|
3154
3531
|
*
|
|
3155
|
-
* Requires
|
|
3532
|
+
* Requires {@link ZyfaiSDK.connectAccount} on the same SDK instance.
|
|
3533
|
+
*
|
|
3534
|
+
* @group Deposits and withdrawals
|
|
3535
|
+
*
|
|
3536
|
+
* @param depositId - Deposit lifecycle id
|
|
3537
|
+
* @param chainId - Chain (controls poll interval and default timeout)
|
|
3538
|
+
* @param options - Optional `intervalMs` and `timeoutMs` overrides
|
|
3539
|
+
* @returns Final lifecycle record when credited
|
|
3540
|
+
*
|
|
3541
|
+
* @throws `DepositCreditTimeoutError` when the poll window elapses without credit
|
|
3542
|
+
* @throws If deposit recovers to EOA or polling fails
|
|
3156
3543
|
*/
|
|
3157
3544
|
async waitForDepositCredit(depositId, chainId, options) {
|
|
3158
3545
|
if (!depositId) {
|
|
@@ -3215,8 +3602,13 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3215
3602
|
*
|
|
3216
3603
|
* @param userAddress - User's address (owner of the Safe)
|
|
3217
3604
|
* @param chainId - Target chain ID
|
|
3218
|
-
* @param amount - Optional
|
|
3219
|
-
* @
|
|
3605
|
+
* @param amount - Optional amount in least units (partial withdrawal); omit for full withdraw
|
|
3606
|
+
* @param tokenSymbol - Optional asset symbol when withdrawing a specific token
|
|
3607
|
+
* @returns Withdraw response with message and optional transaction hash
|
|
3608
|
+
*
|
|
3609
|
+
* @group Deposits and withdrawals
|
|
3610
|
+
*
|
|
3611
|
+
* @throws If `userAddress` or `chainId` is invalid, or the execution API rejects the request
|
|
3220
3612
|
*
|
|
3221
3613
|
* @example
|
|
3222
3614
|
* ```typescript
|
|
@@ -3313,11 +3705,17 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3313
3705
|
);
|
|
3314
3706
|
}
|
|
3315
3707
|
/**
|
|
3316
|
-
* Get available DeFi protocols and pools for a specific chain
|
|
3708
|
+
* Get available DeFi protocols and pools for a specific chain.
|
|
3709
|
+
*
|
|
3710
|
+
* @group Portfolio and execution
|
|
3711
|
+
*
|
|
3712
|
+
* @remarks Execution API. Partner API key only.
|
|
3317
3713
|
*
|
|
3318
3714
|
* @param chainId - Target chain ID
|
|
3319
3715
|
* @returns List of available protocols with their pools and APY data
|
|
3320
3716
|
*
|
|
3717
|
+
* @throws If `chainId` is unsupported
|
|
3718
|
+
*
|
|
3321
3719
|
* @example
|
|
3322
3720
|
* ```typescript
|
|
3323
3721
|
* const protocols = await sdk.getAvailableProtocols(8453);
|
|
@@ -3346,11 +3744,15 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3346
3744
|
}
|
|
3347
3745
|
}
|
|
3348
3746
|
/**
|
|
3349
|
-
* Get
|
|
3747
|
+
* Get active DeFi positions for a user.
|
|
3748
|
+
*
|
|
3749
|
+
* @group Portfolio and execution
|
|
3750
|
+
*
|
|
3751
|
+
* @remarks Execution API (`/data/position`). Pass the **EOA**; the SDK resolves the Safe.
|
|
3350
3752
|
*
|
|
3351
3753
|
* @param userAddress - User's EOA address
|
|
3352
|
-
* @param chainId -
|
|
3353
|
-
* @returns User's positions across
|
|
3754
|
+
* @param chainId - Chain used to resolve the Safe (defaults to Base `8453`)
|
|
3755
|
+
* @returns User's positions across protocols
|
|
3354
3756
|
*
|
|
3355
3757
|
* @example
|
|
3356
3758
|
* ```typescript
|
|
@@ -3430,9 +3832,16 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3430
3832
|
* Do not add `CLAIMED` withdrawals either (the array keeps them for 24
|
|
3431
3833
|
* hours); their funds are back in the Safe and already counted.
|
|
3432
3834
|
*
|
|
3835
|
+
* @group Portfolio and execution
|
|
3836
|
+
*
|
|
3837
|
+
* @remarks Data API portfolio endpoint. See {@link PortfolioDetailed.pendingAsyncWithdrawals}
|
|
3838
|
+
* when using {@link Strategy | yieldmaxxing}.
|
|
3839
|
+
*
|
|
3433
3840
|
* @param userAddress - User's EOA address
|
|
3434
3841
|
* @returns Portfolio with optional fee-adjusted balance fields
|
|
3435
3842
|
*
|
|
3843
|
+
* @throws If `userAddress` is missing
|
|
3844
|
+
*
|
|
3436
3845
|
* @example
|
|
3437
3846
|
* ```typescript
|
|
3438
3847
|
* const { portfolio } = await sdk.getPortfolio(userAddress);
|
|
@@ -3487,10 +3896,15 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3487
3896
|
// User Details Methods
|
|
3488
3897
|
// ============================================================================
|
|
3489
3898
|
/**
|
|
3490
|
-
* Get
|
|
3491
|
-
* Requires SIWE authentication
|
|
3899
|
+
* Get the authenticated user's profile for an asset.
|
|
3492
3900
|
*
|
|
3493
|
-
* @
|
|
3901
|
+
* @group Portfolio and execution
|
|
3902
|
+
*
|
|
3903
|
+
* @remarks Execution API. Requires {@link ZyfaiSDK.connectAccount} (SIWE).
|
|
3904
|
+
*
|
|
3905
|
+
* @param asset - Asset slice of the profile to return
|
|
3906
|
+
* @defaultValue `"USDC"`
|
|
3907
|
+
* @returns User details including smart wallet, chains, and protocols
|
|
3494
3908
|
*
|
|
3495
3909
|
* @example
|
|
3496
3910
|
* ```typescript
|
|
@@ -3537,9 +3951,13 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3537
3951
|
// TVL & Volume Methods
|
|
3538
3952
|
// ============================================================================
|
|
3539
3953
|
/**
|
|
3540
|
-
* Get total value locked (TVL) across all Zyfai accounts
|
|
3954
|
+
* Get total value locked (TVL) across all Zyfai accounts.
|
|
3955
|
+
*
|
|
3956
|
+
* @group Analytics and opportunities
|
|
3957
|
+
*
|
|
3958
|
+
* @remarks Data API. Partner API key only; SIWE not required.
|
|
3541
3959
|
*
|
|
3542
|
-
* @returns Total TVL in USD
|
|
3960
|
+
* @returns Total TVL in USD
|
|
3543
3961
|
*
|
|
3544
3962
|
* @example
|
|
3545
3963
|
* ```typescript
|
|
@@ -3562,7 +3980,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3562
3980
|
// APY Per Strategy Methods
|
|
3563
3981
|
// ============================================================================
|
|
3564
3982
|
/**
|
|
3565
|
-
* Get APY per strategy for a specific chain
|
|
3983
|
+
* Get APY per strategy for a specific chain.
|
|
3984
|
+
*
|
|
3985
|
+
* @group Analytics and opportunities
|
|
3986
|
+
*
|
|
3987
|
+
* @remarks Data API. Partner API key only.
|
|
3566
3988
|
*
|
|
3567
3989
|
* @param crossChain - Whether to get cross-chain APY (true = omni account, false = simple account)
|
|
3568
3990
|
* @param days - Time period: 7, 14, or 30
|
|
@@ -3605,7 +4027,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3605
4027
|
}
|
|
3606
4028
|
}
|
|
3607
4029
|
/**
|
|
3608
|
-
* Get total volume across all Zyfai accounts
|
|
4030
|
+
* Get total volume across all Zyfai accounts.
|
|
4031
|
+
*
|
|
4032
|
+
* @group Analytics and opportunities
|
|
4033
|
+
*
|
|
4034
|
+
* @remarks Data API. Partner API key only.
|
|
3609
4035
|
*
|
|
3610
4036
|
* @returns Total volume in USD
|
|
3611
4037
|
*
|
|
@@ -3632,7 +4058,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3632
4058
|
// Active Wallets Methods
|
|
3633
4059
|
// ============================================================================
|
|
3634
4060
|
/**
|
|
3635
|
-
* Get active wallets for a specific chain
|
|
4061
|
+
* Get active wallets for a specific chain.
|
|
4062
|
+
*
|
|
4063
|
+
* @group Analytics and opportunities
|
|
4064
|
+
*
|
|
4065
|
+
* @remarks Data API. Partner API key only.
|
|
3636
4066
|
*
|
|
3637
4067
|
* @param chainId - Chain ID to filter wallets
|
|
3638
4068
|
* @returns List of active wallets on the specified chain
|
|
@@ -3669,7 +4099,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3669
4099
|
}
|
|
3670
4100
|
}
|
|
3671
4101
|
/**
|
|
3672
|
-
* Get smart wallets associated with an EOA address
|
|
4102
|
+
* Get smart wallets associated with an EOA address.
|
|
4103
|
+
*
|
|
4104
|
+
* @group Analytics and opportunities
|
|
4105
|
+
*
|
|
4106
|
+
* @remarks Data API. Partner API key only.
|
|
3673
4107
|
*
|
|
3674
4108
|
* @param eoaAddress - EOA (externally owned account) address
|
|
3675
4109
|
* @returns List of smart wallets owned by the EOA
|
|
@@ -3706,7 +4140,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3706
4140
|
// First Topup & History Methods
|
|
3707
4141
|
// ============================================================================
|
|
3708
4142
|
/**
|
|
3709
|
-
* Get the first topup (deposit) information for a wallet
|
|
4143
|
+
* Get the first topup (deposit) information for a wallet.
|
|
4144
|
+
*
|
|
4145
|
+
* @group Analytics and opportunities
|
|
4146
|
+
*
|
|
4147
|
+
* @remarks Data API. Partner API key only.
|
|
3710
4148
|
*
|
|
3711
4149
|
* @param walletAddress - Smart wallet address
|
|
3712
4150
|
* @param chainId - Chain ID
|
|
@@ -3741,7 +4179,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3741
4179
|
}
|
|
3742
4180
|
}
|
|
3743
4181
|
/**
|
|
3744
|
-
* Get transaction history for a wallet
|
|
4182
|
+
* Get transaction history for a wallet.
|
|
4183
|
+
*
|
|
4184
|
+
* @group Analytics and opportunities
|
|
4185
|
+
*
|
|
4186
|
+
* @remarks Data API. Partner API key only.
|
|
3745
4187
|
*
|
|
3746
4188
|
* @param walletAddress - Smart wallet address
|
|
3747
4189
|
* @param chainId - Chain ID
|
|
@@ -3797,11 +4239,15 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3797
4239
|
// Onchain Earnings Methods (Data API v2)
|
|
3798
4240
|
// ============================================================================
|
|
3799
4241
|
/**
|
|
3800
|
-
* Get onchain earnings for a wallet
|
|
4242
|
+
* Get onchain earnings for a wallet.
|
|
4243
|
+
*
|
|
4244
|
+
* @group Analytics and opportunities
|
|
4245
|
+
*
|
|
4246
|
+
* @remarks Data API. Partner API key only.
|
|
3801
4247
|
*
|
|
3802
4248
|
* Includes gross totals plus net-of-fee totals:
|
|
3803
|
-
* `totalEarningsByTokenWithFee = lifetime + unrealized + current × (1 - feeRate)`.
|
|
3804
|
-
*
|
|
4249
|
+
* `totalEarningsByTokenWithFee = (lifetime + unrealized + current) × (1 - feeRate)`.
|
|
4250
|
+
* Ledger buckets are gross, so the keep-rate applies to all three.
|
|
3805
4251
|
*
|
|
3806
4252
|
* @param walletAddress - Smart wallet address
|
|
3807
4253
|
* @returns Onchain earnings data with per-token breakdowns
|
|
@@ -3950,7 +4396,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3950
4396
|
// Opportunities Methods (Data API v2)
|
|
3951
4397
|
// ============================================================================
|
|
3952
4398
|
/**
|
|
3953
|
-
* Get conservative (low-risk) yield opportunities
|
|
4399
|
+
* Get conservative (low-risk) yield opportunities.
|
|
4400
|
+
*
|
|
4401
|
+
* @group Analytics and opportunities
|
|
4402
|
+
*
|
|
4403
|
+
* @remarks Data API. Partner API key only.
|
|
3954
4404
|
*
|
|
3955
4405
|
* @param chainId - Optional chain ID filter
|
|
3956
4406
|
* @param asset - Optional asset filter (e.g. "USDC", "WETH", "WBTC")
|
|
@@ -4408,6 +4858,10 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4408
4858
|
* Allows granular control over which pools to use for each protocol on each chain.
|
|
4409
4859
|
* This is useful for advanced users who want to target specific pools with desired APY/risk profiles.
|
|
4410
4860
|
*
|
|
4861
|
+
* @group Customization and simulation
|
|
4862
|
+
*
|
|
4863
|
+
* @remarks Execution API. Requires {@link ZyfaiSDK.connectAccount} (SIWE).
|
|
4864
|
+
*
|
|
4411
4865
|
* @param customizations - Array of customization configurations
|
|
4412
4866
|
* @returns Response indicating success
|
|
4413
4867
|
*
|
|
@@ -4436,6 +4890,73 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4436
4890
|
* ]);
|
|
4437
4891
|
* ```
|
|
4438
4892
|
*/
|
|
4893
|
+
/**
|
|
4894
|
+
* Get persisted per-asset-type management settings (strategy, chains, protocols).
|
|
4895
|
+
*
|
|
4896
|
+
* @group Agent management
|
|
4897
|
+
*/
|
|
4898
|
+
async getAssetTypeSettings() {
|
|
4899
|
+
await this.authenticateUser();
|
|
4900
|
+
return this.httpClient.get(ENDPOINTS.USER_ASSET_TYPE_SETTINGS);
|
|
4901
|
+
}
|
|
4902
|
+
/**
|
|
4903
|
+
* Reserve a one-time action id before the user signs an ERC-20 transfer.
|
|
4904
|
+
*
|
|
4905
|
+
* @group Agent management
|
|
4906
|
+
*/
|
|
4907
|
+
async createAgentDepositIntent(request) {
|
|
4908
|
+
await this.authenticateUser();
|
|
4909
|
+
return this.httpClient.post(
|
|
4910
|
+
ENDPOINTS.USER_AGENT_DEPOSIT_INTENTS,
|
|
4911
|
+
request
|
|
4912
|
+
);
|
|
4913
|
+
}
|
|
4914
|
+
/**
|
|
4915
|
+
* Consume a one-time action id when registering the signed deposit.
|
|
4916
|
+
*
|
|
4917
|
+
* @group Agent management
|
|
4918
|
+
*/
|
|
4919
|
+
async consumeAgentDepositIntent(actionId, request) {
|
|
4920
|
+
await this.authenticateUser();
|
|
4921
|
+
return this.httpClient.post(
|
|
4922
|
+
ENDPOINTS.USER_AGENT_DEPOSIT_INTENT_CONSUME(actionId),
|
|
4923
|
+
request
|
|
4924
|
+
);
|
|
4925
|
+
}
|
|
4926
|
+
/**
|
|
4927
|
+
* Poll MCP deposit-intent status (agent delegated session).
|
|
4928
|
+
*
|
|
4929
|
+
* @group Agent management
|
|
4930
|
+
*/
|
|
4931
|
+
async getAgentDepositIntentStatus(actionId) {
|
|
4932
|
+
await this.authenticateUser();
|
|
4933
|
+
return this.httpClient.get(
|
|
4934
|
+
ENDPOINTS.USER_AGENT_DEPOSIT_INTENT(actionId)
|
|
4935
|
+
);
|
|
4936
|
+
}
|
|
4937
|
+
/**
|
|
4938
|
+
* Mark intent complete after the user signed on zyf.ai (SIWE session).
|
|
4939
|
+
*
|
|
4940
|
+
* @group Agent management
|
|
4941
|
+
*/
|
|
4942
|
+
async completeAgentDepositIntent(actionId, request) {
|
|
4943
|
+
await this.authenticateUser();
|
|
4944
|
+
return this.httpClient.post(
|
|
4945
|
+
ENDPOINTS.USER_AGENT_DEPOSIT_INTENT_COMPLETE(actionId),
|
|
4946
|
+
request
|
|
4947
|
+
);
|
|
4948
|
+
}
|
|
4949
|
+
/**
|
|
4950
|
+
* Resolve a signing ticket from prepare_deposit (no user auth).
|
|
4951
|
+
*
|
|
4952
|
+
* @group Agent management
|
|
4953
|
+
*/
|
|
4954
|
+
async resolveAgentDepositIntentTicket(ticket) {
|
|
4955
|
+
const query = `?ticket=${encodeURIComponent(ticket)}`;
|
|
4956
|
+
return this.httpClient.get(
|
|
4957
|
+
`${ENDPOINTS.AGENT_DEPOSIT_INTENT_RESOLVE}${query}`
|
|
4958
|
+
);
|
|
4959
|
+
}
|
|
4439
4960
|
async customizeBatch(customizations) {
|
|
4440
4961
|
try {
|
|
4441
4962
|
await this.authenticateUser();
|
|
@@ -4455,8 +4976,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4455
4976
|
*
|
|
4456
4977
|
* Returns the list of pools available for a given protocol, optionally filtered by strategy.
|
|
4457
4978
|
*
|
|
4979
|
+
* @group Customization and simulation
|
|
4980
|
+
*
|
|
4981
|
+
* @remarks Execution API. Partner API key only.
|
|
4982
|
+
*
|
|
4458
4983
|
* @param protocolId - The protocol UUID
|
|
4459
|
-
* @param strategy - Optional
|
|
4984
|
+
* @param strategy - Optional {@link Strategy} filter
|
|
4460
4985
|
* @returns List of available pool names
|
|
4461
4986
|
*
|
|
4462
4987
|
* @example
|
|
@@ -4504,7 +5029,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4504
5029
|
* calldata contains a "<RECEIVER>" placeholder that must be replaced with the
|
|
4505
5030
|
* actual receiving address (e.g. the user's Safe) before sending the transaction.
|
|
4506
5031
|
*
|
|
4507
|
-
* @
|
|
5032
|
+
* @group Customization and simulation
|
|
5033
|
+
*
|
|
5034
|
+
* @remarks Data API simulation endpoint. Partner API key only.
|
|
5035
|
+
*
|
|
5036
|
+
* @param params - {@link SimulateBestPositionsParams}
|
|
4508
5037
|
* @returns Chain-keyed map of simulated positions with calldata
|
|
4509
5038
|
*
|
|
4510
5039
|
* @example
|
|
@@ -4547,6 +5076,10 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4547
5076
|
* Returns the pools that are currently configured for the authenticated user
|
|
4548
5077
|
* for a given protocol and chain combination.
|
|
4549
5078
|
*
|
|
5079
|
+
* @group Customization and simulation
|
|
5080
|
+
*
|
|
5081
|
+
* @remarks Execution API. Requires {@link ZyfaiSDK.connectAccount} (SIWE).
|
|
5082
|
+
*
|
|
4550
5083
|
* @param protocolId - The protocol UUID
|
|
4551
5084
|
* @param chainId - The chain ID
|
|
4552
5085
|
* @returns Currently selected pools and autoselect status
|
|
@@ -4654,10 +5187,13 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4654
5187
|
// Vault Methods (Base only)
|
|
4655
5188
|
// ============================================
|
|
4656
5189
|
/**
|
|
4657
|
-
* Deposit assets into the Zyfai Vault
|
|
4658
|
-
*
|
|
5190
|
+
* Deposit assets into the Zyfai Vault (USDC on Base by default).
|
|
5191
|
+
*
|
|
5192
|
+
* @group Vault
|
|
4659
5193
|
*
|
|
4660
|
-
* @
|
|
5194
|
+
* @remarks On-chain vault flow via connected wallet. Requires {@link ZyfaiSDK.connectAccount}.
|
|
5195
|
+
*
|
|
5196
|
+
* @param amount - Human-readable amount (e.g. `"100"` USDC)
|
|
4661
5197
|
* @param asset - Asset to deposit (default: "USDC")
|
|
4662
5198
|
* @returns Deposit transaction result
|
|
4663
5199
|
*
|
|
@@ -5006,9 +5542,14 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5006
5542
|
* Events: depeg, liquidity_trap, liquidity_restored, pool_status_change,
|
|
5007
5543
|
* new_collateral_detected, liquidity_drop.
|
|
5008
5544
|
*
|
|
5009
|
-
* @
|
|
5010
|
-
*
|
|
5011
|
-
* @
|
|
5545
|
+
* @group Realtime events
|
|
5546
|
+
*
|
|
5547
|
+
* @remarks Connects to defi-api WebSocket (`WS_ENDPOINT`). Auto-reconnects every 3s until
|
|
5548
|
+
* the returned cleanup function is called.
|
|
5549
|
+
*
|
|
5550
|
+
* @param handlers - {@link ZyfaiEventHandlers}
|
|
5551
|
+
* @param filters - Optional {@link ZyfaiEventFilters} sent on subscribe
|
|
5552
|
+
* @returns Cleanup function — call it to close the connection and stop reconnecting
|
|
5012
5553
|
*
|
|
5013
5554
|
* @example
|
|
5014
5555
|
* ```typescript
|