@zyfai/sdk 1.9.2 → 1.10.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 +48 -36
- package/dist/index.d.mts +79 -2
- package/dist/index.d.ts +79 -2
- package/dist/index.js +316 -118
- package/dist/index.mjs +316 -119
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1632,6 +1632,15 @@ var getAccountType = async (address, publicClient) => {
|
|
|
1632
1632
|
return "Unknown";
|
|
1633
1633
|
}
|
|
1634
1634
|
};
|
|
1635
|
+
function isSafeDeployPermissionsReady(prepare) {
|
|
1636
|
+
return prepare.success && !prepare.userOpHashToSign && prepare.signingMode !== "typed-data";
|
|
1637
|
+
}
|
|
1638
|
+
async function postSafeDeployPrepare(httpClient, chainId, strategyBody = {}) {
|
|
1639
|
+
return await httpClient.post(
|
|
1640
|
+
`${ENDPOINTS.SAFE_DEPLOY}?chainId=${chainId}`,
|
|
1641
|
+
strategyBody
|
|
1642
|
+
);
|
|
1643
|
+
}
|
|
1635
1644
|
var signNestedSafeDeployTypedData = async (owner, parentSafe, typedData) => {
|
|
1636
1645
|
if (!owner.account) {
|
|
1637
1646
|
throw new Error("Wallet not connected. Please connect your wallet first.");
|
|
@@ -1679,12 +1688,13 @@ var deploySafeAccount = async (config) => {
|
|
|
1679
1688
|
}
|
|
1680
1689
|
console.log("strategy", strategy);
|
|
1681
1690
|
const strategyBody = strategy ? { strategy } : {};
|
|
1682
|
-
const prepareResponse = await
|
|
1683
|
-
|
|
1691
|
+
const prepareResponse = await postSafeDeployPrepare(
|
|
1692
|
+
httpClient,
|
|
1693
|
+
chainId,
|
|
1684
1694
|
strategyBody
|
|
1685
1695
|
);
|
|
1686
1696
|
console.log("prepareResponse", prepareResponse);
|
|
1687
|
-
if (prepareResponse
|
|
1697
|
+
if (isSafeDeployPermissionsReady(prepareResponse)) {
|
|
1688
1698
|
return {
|
|
1689
1699
|
safeAddress: prepareResponse.safeAddress || "0x",
|
|
1690
1700
|
txHash: "0x0",
|
|
@@ -1883,7 +1893,9 @@ async function signNestedOwnerSessionKey(config, sessions, allPublicClients, sig
|
|
|
1883
1893
|
|
|
1884
1894
|
// src/utils/nested-safe-deposit.ts
|
|
1885
1895
|
import {
|
|
1886
|
-
getAddress as getAddress3
|
|
1896
|
+
getAddress as getAddress3,
|
|
1897
|
+
decodeEventLog,
|
|
1898
|
+
parseAbiItem
|
|
1887
1899
|
} from "viem";
|
|
1888
1900
|
var NESTED_SAFE_DEPOSIT_TIMEOUT_MS = 20 * 60 * 1e3;
|
|
1889
1901
|
var NESTED_SAFE_DEPOSIT_POLL_MS = 1e4;
|
|
@@ -1925,35 +1937,6 @@ function sameAddress(left, right) {
|
|
|
1925
1937
|
return false;
|
|
1926
1938
|
}
|
|
1927
1939
|
}
|
|
1928
|
-
function transferMatches(transaction, params) {
|
|
1929
|
-
if (!sameAddress(transaction.to, params.tokenAddress)) return false;
|
|
1930
|
-
if (transaction.isSuccessful === false) return false;
|
|
1931
|
-
const decoded = transaction.dataDecoded;
|
|
1932
|
-
if (decoded?.method?.toLowerCase() !== "transfer") return false;
|
|
1933
|
-
const parameters = decoded.parameters;
|
|
1934
|
-
if (!parameters) return false;
|
|
1935
|
-
const destination = parameters.find(
|
|
1936
|
-
(p) => (p.name === "to" || p.name === "dst" || p.name === "_to") && p.type === "address"
|
|
1937
|
-
);
|
|
1938
|
-
if (!sameAddress(destination?.value, params.childSafe)) return false;
|
|
1939
|
-
if (params.amount === void 0) return true;
|
|
1940
|
-
const valueParam = parameters.find(
|
|
1941
|
-
(p) => (p.name === "value" || p.name === "amount" || p.name === "_value" || p.name === "wad") && p.type.startsWith("uint")
|
|
1942
|
-
);
|
|
1943
|
-
if (!valueParam) return false;
|
|
1944
|
-
try {
|
|
1945
|
-
return BigInt(valueParam.value) === params.amount;
|
|
1946
|
-
} catch {
|
|
1947
|
-
return false;
|
|
1948
|
-
}
|
|
1949
|
-
}
|
|
1950
|
-
function isWithinWindow(transaction, since) {
|
|
1951
|
-
if (!since) return true;
|
|
1952
|
-
const stamp = transaction.executionDate ?? transaction.submissionDate;
|
|
1953
|
-
if (!stamp) return false;
|
|
1954
|
-
const at = Date.parse(stamp);
|
|
1955
|
-
return Number.isFinite(at) && at >= since.getTime();
|
|
1956
|
-
}
|
|
1957
1940
|
async function findSafeTransferByHash(params) {
|
|
1958
1941
|
const networkName = safeTxServiceNetworkNameOrThrow(params.chainId);
|
|
1959
1942
|
const url = `https://api.safe.global/tx-service/${networkName}/api/v2/multisig-transactions/${params.safeTxHash}/`;
|
|
@@ -1972,54 +1955,100 @@ async function findSafeTransferByHash(params) {
|
|
|
1972
1955
|
isExecuted: transaction.isExecuted === true && !!transaction.transactionHash
|
|
1973
1956
|
};
|
|
1974
1957
|
}
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
const
|
|
1978
|
-
params.parentSafe
|
|
1979
|
-
)}/multisig-transactions/?ordering=-nonce`;
|
|
1980
|
-
const response = await fetch(url);
|
|
1981
|
-
if (!response.ok) return null;
|
|
1982
|
-
const body = await response.json();
|
|
1983
|
-
const match = (body.results ?? []).find(
|
|
1984
|
-
(transaction) => !!transaction.safeTxHash && isWithinWindow(transaction, params.since) && transferMatches(transaction, {
|
|
1985
|
-
childSafe: params.childSafe,
|
|
1986
|
-
tokenAddress: params.tokenAddress,
|
|
1987
|
-
amount: params.amount
|
|
1988
|
-
})
|
|
1989
|
-
);
|
|
1990
|
-
if (!match) return null;
|
|
1991
|
-
return {
|
|
1992
|
-
safeTxHash: match.safeTxHash,
|
|
1993
|
-
transactionHash: match.transactionHash ?? null,
|
|
1994
|
-
isExecuted: match.isExecuted === true && !!match.transactionHash
|
|
1995
|
-
};
|
|
1996
|
-
}
|
|
1997
|
-
async function waitForSafeTransferExecution(params) {
|
|
1998
|
-
safeTxServiceNetworkNameOrThrow(params.chainId);
|
|
1958
|
+
var TRANSFER_EVENT = parseAbiItem("event Transfer(address indexed from, address indexed to, uint256 value)");
|
|
1959
|
+
async function waitForExactSafeDepositExecution(params) {
|
|
1960
|
+
const started = Date.now();
|
|
1999
1961
|
const timeoutMs = params.timeoutMs ?? NESTED_SAFE_DEPOSIT_TIMEOUT_MS;
|
|
2000
1962
|
const pollMs = params.pollMs ?? NESTED_SAFE_DEPOSIT_POLL_MS;
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
1963
|
+
let hash = params.proposalHash;
|
|
1964
|
+
try {
|
|
1965
|
+
do {
|
|
1966
|
+
const receipt = await params.publicClient.getTransactionReceipt({ hash }).catch(() => void 0);
|
|
1967
|
+
if (receipt) {
|
|
1968
|
+
if (receipt.status !== "success") throw new Error("Safe transfer reverted");
|
|
1969
|
+
const matches = receipt.logs.some((log) => {
|
|
1970
|
+
if (!sameAddress(log.address, params.tokenAddress)) return false;
|
|
1971
|
+
try {
|
|
1972
|
+
const decoded = decodeEventLog({ abi: [TRANSFER_EVENT], data: log.data, topics: log.topics });
|
|
1973
|
+
return sameAddress(decoded.args.from, params.parentSafe) && sameAddress(decoded.args.to, params.childSafe) && decoded.args.value === params.amount;
|
|
1974
|
+
} catch {
|
|
1975
|
+
return false;
|
|
1976
|
+
}
|
|
1977
|
+
});
|
|
1978
|
+
if (!matches) throw new Error("Executed transaction does not contain the expected parent-to-child transfer");
|
|
1979
|
+
return hash;
|
|
1980
|
+
}
|
|
1981
|
+
if (safeTxServiceNetworkName(params.chainId) && hash === params.proposalHash) {
|
|
1982
|
+
const proposal = await findSafeTransferByHash({ chainId: params.chainId, safeTxHash: params.proposalHash });
|
|
1983
|
+
if (proposal?.isExecuted && proposal.transactionHash) {
|
|
1984
|
+
hash = proposal.transactionHash;
|
|
1985
|
+
continue;
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
await new Promise((resolve) => setTimeout(resolve, pollMs));
|
|
1989
|
+
} while (Date.now() - started < timeoutMs);
|
|
1990
|
+
throw new Error("Timed out waiting for Safe transfer execution");
|
|
1991
|
+
} catch (error) {
|
|
1992
|
+
throw new Error(`Safe deposit proposal ${params.proposalHash}: ${error.message}. Resume this exact proposal with resumeNestedSafeDeposit; do not send another transfer.`);
|
|
1993
|
+
}
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1996
|
+
// src/utils/nested-safe-onboarding.ts
|
|
1997
|
+
function positiveAmount(value) {
|
|
1998
|
+
if (!value) return false;
|
|
1999
|
+
const trimmed = value.trim();
|
|
2000
|
+
if (!trimmed || trimmed === "0" || trimmed === "0x" || trimmed === "0x0") {
|
|
2001
|
+
return false;
|
|
2002
|
+
}
|
|
2003
|
+
try {
|
|
2004
|
+
if (trimmed.startsWith("0x") || trimmed.startsWith("0X")) {
|
|
2005
|
+
return BigInt(trimmed) > 0n;
|
|
2016
2006
|
}
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2007
|
+
if (/^\d+$/.test(trimmed)) return BigInt(trimmed) > 0n;
|
|
2008
|
+
const numeric = Number(trimmed);
|
|
2009
|
+
return Number.isFinite(numeric) && numeric > 0;
|
|
2010
|
+
} catch {
|
|
2011
|
+
return false;
|
|
2012
|
+
}
|
|
2013
|
+
}
|
|
2014
|
+
function portfolioHasWithdrawableBalance(portfolio) {
|
|
2015
|
+
if (!portfolio) return false;
|
|
2016
|
+
if (portfolio.hasBalance) return true;
|
|
2017
|
+
const buckets = [
|
|
2018
|
+
...Object.values(portfolio.portfolioByAssetType ?? {}),
|
|
2019
|
+
...Object.values(portfolio.portfolioByChain ?? {}).flatMap(
|
|
2020
|
+
(byAsset) => Object.values(byAsset)
|
|
2021
|
+
)
|
|
2022
|
+
];
|
|
2023
|
+
if (buckets.some(
|
|
2024
|
+
(entry) => positiveAmount(entry.balance) || positiveAmount(entry.balanceWithFee)
|
|
2025
|
+
)) {
|
|
2026
|
+
return true;
|
|
2027
|
+
}
|
|
2028
|
+
if ((portfolio.positions ?? []).some(
|
|
2029
|
+
(slot) => positiveAmount(slot.underlyingAmountWithFee) || positiveAmount(slot.underlyingAmount) || positiveAmount(slot.amount)
|
|
2030
|
+
)) {
|
|
2031
|
+
return true;
|
|
2032
|
+
}
|
|
2033
|
+
return (portfolio.staleBalances ?? []).some(
|
|
2034
|
+
(stale) => positiveAmount(stale.balance)
|
|
2021
2035
|
);
|
|
2022
2036
|
}
|
|
2037
|
+
function deriveNestedSafeOnboardingStep(flags) {
|
|
2038
|
+
if (!flags.isNestedOwner) return "deploy";
|
|
2039
|
+
if (!flags.childRegistered || !flags.childDeployed) return "deploy";
|
|
2040
|
+
if (!flags.sessionActive) return "register";
|
|
2041
|
+
if (!flags.hasPortfolioBalance) return "deposit";
|
|
2042
|
+
if (flags.hasPortfolioBalance) return "withdraw";
|
|
2043
|
+
return "ready";
|
|
2044
|
+
}
|
|
2045
|
+
function buildNestedSafeOnboardingStatus(flags, child) {
|
|
2046
|
+
return {
|
|
2047
|
+
...flags,
|
|
2048
|
+
child,
|
|
2049
|
+
suggestedStep: deriveNestedSafeOnboardingStep(flags)
|
|
2050
|
+
};
|
|
2051
|
+
}
|
|
2023
2052
|
|
|
2024
2053
|
// src/utils/strategy.ts
|
|
2025
2054
|
var PUBLIC_STRATEGIES = `"conservative", "aggressive" or "yieldmaxxing"`;
|
|
@@ -3277,15 +3306,17 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3277
3306
|
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
3278
3307
|
}
|
|
3279
3308
|
const resolvedAddress = await this.resolveConnectAddress(account);
|
|
3280
|
-
if (this.sessionEoa && this.httpClient.hasAuthToken() && this.authenticatedUserId !== null && isAddressEqual2(resolvedAddress, this.sessionEoa)) {
|
|
3309
|
+
if (this.sessionEoa && this.httpClient.hasAuthToken() && this.authenticatedUserId !== null && isAddressEqual2(resolvedAddress, this.sessionEoa) && this.currentChainId === chainId && (account === this.currentProvider || account === this.walletClient)) {
|
|
3281
3310
|
this.currentChainId = chainId;
|
|
3282
3311
|
return resolvedAddress;
|
|
3283
3312
|
}
|
|
3284
3313
|
if (this.connectAccountInFlight) {
|
|
3285
|
-
|
|
3314
|
+
throw new Error("Wallet connection already in progress; retry after it completes");
|
|
3286
3315
|
}
|
|
3287
3316
|
const connect = (async () => {
|
|
3288
3317
|
this.authenticatedUserId = null;
|
|
3318
|
+
this.hasActiveSessionKey = false;
|
|
3319
|
+
this.signer = null;
|
|
3289
3320
|
this.isPredeployed = false;
|
|
3290
3321
|
this.predeployedResolved = false;
|
|
3291
3322
|
this.connectedSmartWallet = null;
|
|
@@ -3335,6 +3366,9 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3335
3366
|
transport: provider.transport
|
|
3336
3367
|
});
|
|
3337
3368
|
connectedAddress = getAddress4(resolvedAddress2);
|
|
3369
|
+
if (provider.chain?.id !== void 0 && provider.chain.id !== chainId) {
|
|
3370
|
+
throw new Error(`Wallet client chain ${provider.chain.id} does not match requested chain ${chainId}`);
|
|
3371
|
+
}
|
|
3338
3372
|
this.currentProvider = null;
|
|
3339
3373
|
this.currentChainId = chainId;
|
|
3340
3374
|
} else if (provider.request) {
|
|
@@ -3358,6 +3392,13 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3358
3392
|
);
|
|
3359
3393
|
}
|
|
3360
3394
|
connectedAddress = getAddress4(accountAddress);
|
|
3395
|
+
if (!isAddressEqual2(connectedAddress, resolvedAddress)) {
|
|
3396
|
+
throw new Error("Wallet account changed during connection; reconnect");
|
|
3397
|
+
}
|
|
3398
|
+
const providerChain = await eip1193.request({ method: "eth_chainId" });
|
|
3399
|
+
if (Number(providerChain) !== chainId) {
|
|
3400
|
+
throw new Error(`Wallet is on chain ${providerChain}; switch to chain ${chainId} before connecting`);
|
|
3401
|
+
}
|
|
3361
3402
|
this.walletClient = createWalletClient({
|
|
3362
3403
|
account: connectedAddress,
|
|
3363
3404
|
chain: chainConfig.chain,
|
|
@@ -3446,6 +3487,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3446
3487
|
* True when userAddress is the signer, wallet client, or agent session EOA.
|
|
3447
3488
|
* @private
|
|
3448
3489
|
*/
|
|
3490
|
+
assertConnectedUser(userAddress) {
|
|
3491
|
+
if (!this.isConnectedUser(userAddress) || this.sessionEoa && !isAddressEqual2(getAddress4(userAddress), this.sessionEoa)) {
|
|
3492
|
+
throw new Error("userAddress does not match the authenticated wallet");
|
|
3493
|
+
}
|
|
3494
|
+
}
|
|
3449
3495
|
isConnectedUser(userAddress) {
|
|
3450
3496
|
const connected = this.signer?.address ?? this.walletClient?.account?.address ?? this.sessionEoa;
|
|
3451
3497
|
return !!connected && connected.toLowerCase() === userAddress.toLowerCase();
|
|
@@ -3455,6 +3501,15 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3455
3501
|
* (install or emissary enable), then register with chainId only.
|
|
3456
3502
|
* @private
|
|
3457
3503
|
*/
|
|
3504
|
+
/**
|
|
3505
|
+
* Read-only probe for nested owners: on-chain session permissions on `chainId`.
|
|
3506
|
+
* Mirrors the early-return branch in {@link deploySafeAccount} without signing.
|
|
3507
|
+
* @private
|
|
3508
|
+
*/
|
|
3509
|
+
async isNestedChainSessionPermissionsReady(chainId) {
|
|
3510
|
+
const prepare = await postSafeDeployPrepare(this.httpClient, chainId);
|
|
3511
|
+
return isSafeDeployPermissionsReady(prepare);
|
|
3512
|
+
}
|
|
3458
3513
|
async registerNestedIntentSessionKey(userAddress, chainId, strategy) {
|
|
3459
3514
|
if (!this.authenticatedUserId) {
|
|
3460
3515
|
throw new Error(
|
|
@@ -3471,10 +3526,21 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3471
3526
|
httpClient: this.httpClient,
|
|
3472
3527
|
strategy
|
|
3473
3528
|
});
|
|
3529
|
+
if (this.hasActiveSessionKey) {
|
|
3530
|
+
return {
|
|
3531
|
+
success: true,
|
|
3532
|
+
userId: this.authenticatedUserId,
|
|
3533
|
+
message: "Intent session permissions ready on requested chain",
|
|
3534
|
+
alreadyActive: true
|
|
3535
|
+
};
|
|
3536
|
+
}
|
|
3474
3537
|
const activation = await this.httpClient.post(
|
|
3475
3538
|
ENDPOINTS.SESSION_KEYS_ADD,
|
|
3476
3539
|
{ chainId }
|
|
3477
3540
|
);
|
|
3541
|
+
if (!activation?.isActive || !activation?.isEnabled) {
|
|
3542
|
+
throw new Error("Intent session is not active and enabled on the requested chain");
|
|
3543
|
+
}
|
|
3478
3544
|
this.hasActiveSessionKey = true;
|
|
3479
3545
|
return {
|
|
3480
3546
|
success: true,
|
|
@@ -3520,7 +3586,9 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3520
3586
|
const childAddress = await this.getSafeAddressFor(userAddress, chainId);
|
|
3521
3587
|
const childDeployed = childAddress ? await isSafeDeployed(childAddress, chainConfig.publicClient) : false;
|
|
3522
3588
|
if (!childDeployed) {
|
|
3523
|
-
await this.deploySafe(userAddress, chainId, strategy,
|
|
3589
|
+
const deployment = await this.deploySafe(userAddress, chainId, strategy, false);
|
|
3590
|
+
if (!deployment.success) throw new Error("Nested Safe deployment failed");
|
|
3591
|
+
await this.createSessionKey(userAddress, chainId);
|
|
3524
3592
|
return;
|
|
3525
3593
|
}
|
|
3526
3594
|
console.info(
|
|
@@ -3645,6 +3713,84 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3645
3713
|
isOwner
|
|
3646
3714
|
};
|
|
3647
3715
|
}
|
|
3716
|
+
/**
|
|
3717
|
+
* Nested-parent onboarding snapshot for a connected parent Safe address.
|
|
3718
|
+
*
|
|
3719
|
+
* @group Deposits and withdrawals
|
|
3720
|
+
*
|
|
3721
|
+
* @remarks Combines child resolution ({@link ZyfaiSDK.getSmartWalletAddress}),
|
|
3722
|
+
* session state (account registration plus per-chain safe-deploy readiness),
|
|
3723
|
+
* and portfolio balances
|
|
3724
|
+
* ({@link ZyfaiSDK.getPortfolio}). Wallet connect remains integrator-side.
|
|
3725
|
+
*
|
|
3726
|
+
* @param userAddress - Parent Safe address from {@link ZyfaiSDK.connectAccount}
|
|
3727
|
+
* @param chainId - Target chain (deployment and session are per chain)
|
|
3728
|
+
*/
|
|
3729
|
+
async getNestedSafeOnboardingStatus(userAddress, chainId) {
|
|
3730
|
+
if (!userAddress) {
|
|
3731
|
+
throw new Error("User address is required");
|
|
3732
|
+
}
|
|
3733
|
+
if (!isSupportedChain(chainId)) {
|
|
3734
|
+
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
3735
|
+
}
|
|
3736
|
+
await this.authenticateUser();
|
|
3737
|
+
this.assertConnectedUser(userAddress);
|
|
3738
|
+
const chainConfig = getChainConfig(chainId, this.rpcUrls);
|
|
3739
|
+
const isNestedOwner = await getAccountType(userAddress, chainConfig.publicClient) === "Safe";
|
|
3740
|
+
let child = null;
|
|
3741
|
+
let childRegistered = false;
|
|
3742
|
+
let childDeployed = false;
|
|
3743
|
+
try {
|
|
3744
|
+
child = await this.getSmartWalletAddress(userAddress, chainId);
|
|
3745
|
+
childRegistered = true;
|
|
3746
|
+
childDeployed = child.isDeployed;
|
|
3747
|
+
} catch (error) {
|
|
3748
|
+
const message = error.message;
|
|
3749
|
+
if (message !== "NESTED_CHILD_UNREGISTERED") {
|
|
3750
|
+
throw error;
|
|
3751
|
+
}
|
|
3752
|
+
}
|
|
3753
|
+
let accountSessionRegistered = false;
|
|
3754
|
+
try {
|
|
3755
|
+
const details = await this.getUserDetails("USDC");
|
|
3756
|
+
accountSessionRegistered = Boolean(details.hasActiveSessionKey);
|
|
3757
|
+
} catch {
|
|
3758
|
+
}
|
|
3759
|
+
let chainSessionReady = false;
|
|
3760
|
+
if (isNestedOwner && childDeployed) {
|
|
3761
|
+
try {
|
|
3762
|
+
chainSessionReady = await this.isNestedChainSessionPermissionsReady(chainId);
|
|
3763
|
+
} catch {
|
|
3764
|
+
}
|
|
3765
|
+
}
|
|
3766
|
+
const sessionActive = chainSessionReady && accountSessionRegistered;
|
|
3767
|
+
let hasPortfolioBalance = false;
|
|
3768
|
+
if (childRegistered) {
|
|
3769
|
+
try {
|
|
3770
|
+
const portfolio = await this.getPortfolio(userAddress);
|
|
3771
|
+
hasPortfolioBalance = portfolioHasWithdrawableBalance(
|
|
3772
|
+
portfolio.portfolio
|
|
3773
|
+
);
|
|
3774
|
+
} catch {
|
|
3775
|
+
}
|
|
3776
|
+
}
|
|
3777
|
+
const status = buildNestedSafeOnboardingStatus(
|
|
3778
|
+
{
|
|
3779
|
+
isNestedOwner,
|
|
3780
|
+
childRegistered,
|
|
3781
|
+
childDeployed,
|
|
3782
|
+
sessionActive: sessionActive && childDeployed && childRegistered,
|
|
3783
|
+
hasPortfolioBalance
|
|
3784
|
+
},
|
|
3785
|
+
child
|
|
3786
|
+
);
|
|
3787
|
+
return {
|
|
3788
|
+
success: true,
|
|
3789
|
+
userAddress,
|
|
3790
|
+
chainId,
|
|
3791
|
+
status
|
|
3792
|
+
};
|
|
3793
|
+
}
|
|
3648
3794
|
/**
|
|
3649
3795
|
* Deploy Safe Smart Wallet for a user
|
|
3650
3796
|
*
|
|
@@ -3682,6 +3828,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3682
3828
|
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
3683
3829
|
}
|
|
3684
3830
|
await this.authenticateUser();
|
|
3831
|
+
this.assertConnectedUser(userAddress);
|
|
3685
3832
|
this.warnPredeployedOnboardingMethod("deploySafe");
|
|
3686
3833
|
if (this.isPredeployed && this.isConnectedUser(userAddress)) {
|
|
3687
3834
|
const predeployedAddress = await this.getSafeAddressFor(
|
|
@@ -3854,6 +4001,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3854
4001
|
async createSessionKey(userAddress, chainId) {
|
|
3855
4002
|
try {
|
|
3856
4003
|
await this.authenticateUser();
|
|
4004
|
+
this.assertConnectedUser(userAddress);
|
|
3857
4005
|
this.warnPredeployedOnboardingMethod("createSessionKey");
|
|
3858
4006
|
if (!this.authenticatedUserId) {
|
|
3859
4007
|
throw new Error(
|
|
@@ -3868,6 +4016,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3868
4016
|
alreadyActive: true
|
|
3869
4017
|
};
|
|
3870
4018
|
}
|
|
4019
|
+
const nestedChain = getChainConfig(chainId, this.rpcUrls);
|
|
4020
|
+
if (await getAccountType(userAddress, nestedChain.publicClient) === "Safe") {
|
|
4021
|
+
await assertParentSafeDeployedOnChain(userAddress, nestedChain.publicClient, chainId);
|
|
4022
|
+
return this.registerNestedIntentSessionKey(userAddress, chainId);
|
|
4023
|
+
}
|
|
3871
4024
|
if (this.hasActiveSessionKey) {
|
|
3872
4025
|
return {
|
|
3873
4026
|
success: true,
|
|
@@ -3899,12 +4052,8 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3899
4052
|
userAddress,
|
|
3900
4053
|
chainConfig.publicClient
|
|
3901
4054
|
);
|
|
3902
|
-
if (accountType
|
|
3903
|
-
|
|
3904
|
-
userAddress,
|
|
3905
|
-
chainId,
|
|
3906
|
-
void 0
|
|
3907
|
-
);
|
|
4055
|
+
if (accountType !== "EOA") {
|
|
4056
|
+
throw new Error("Unable to establish EOA session account type");
|
|
3908
4057
|
}
|
|
3909
4058
|
const signingParams = {
|
|
3910
4059
|
permitGenericPolicy,
|
|
@@ -4402,10 +4551,10 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4402
4551
|
* await sdk.waitForDepositPosition(sent.registration.id, 8453);
|
|
4403
4552
|
* ```
|
|
4404
4553
|
*/
|
|
4405
|
-
async sendDirectDeposit(userAddress, chainId, amount, asset, strategy) {
|
|
4406
|
-
return this.sendCollateralDeposit(userAddress, chainId, amount, asset, strategy, true);
|
|
4554
|
+
async sendDirectDeposit(userAddress, chainId, amount, asset, strategy, onSafeProposal) {
|
|
4555
|
+
return this.sendCollateralDeposit(userAddress, chainId, amount, asset, strategy, true, onSafeProposal);
|
|
4407
4556
|
}
|
|
4408
|
-
async sendCollateralDeposit(userAddress, chainId, amount, asset, strategy, allowPermit) {
|
|
4557
|
+
async sendCollateralDeposit(userAddress, chainId, amount, asset, strategy, allowPermit, onSafeProposal) {
|
|
4409
4558
|
try {
|
|
4410
4559
|
if (!userAddress) {
|
|
4411
4560
|
throw new Error("User address is required");
|
|
@@ -4457,11 +4606,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4457
4606
|
} else {
|
|
4458
4607
|
await this.authenticateUser();
|
|
4459
4608
|
}
|
|
4460
|
-
|
|
4461
|
-
|
|
4462
|
-
|
|
4463
|
-
throw new Error("Smart wallet address is not available");
|
|
4609
|
+
this.assertConnectedUser(userAddress);
|
|
4610
|
+
if (!this.signer && walletClient.chain?.id !== chainId) {
|
|
4611
|
+
throw new Error("Switch the connected wallet to the deposit chain and reconnect");
|
|
4464
4612
|
}
|
|
4613
|
+
await this.hydrateSessionWallet();
|
|
4614
|
+
let safeAddress = await this.getSafeAddressFor(userAddress, chainId);
|
|
4465
4615
|
const amountBigInt = BigInt(amount);
|
|
4466
4616
|
const ownerType = await getAccountType(
|
|
4467
4617
|
userAddress,
|
|
@@ -4472,6 +4622,15 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4472
4622
|
`Could not resolve the account type of ${userAddress} on chain ${chainId}: it is neither an EOA nor a Safe, or the RPC read failed. No deposit was sent.`
|
|
4473
4623
|
);
|
|
4474
4624
|
}
|
|
4625
|
+
let nestedOnboarded = false;
|
|
4626
|
+
if (!safeAddress && ownerType === "Safe" && !this.isPredeployed) {
|
|
4627
|
+
await this.ensureNestedOwnerReadyForDeposit(userAddress, chainId, strategy);
|
|
4628
|
+
safeAddress = await this.getSafeAddressFor(userAddress, chainId);
|
|
4629
|
+
nestedOnboarded = true;
|
|
4630
|
+
}
|
|
4631
|
+
if (!safeAddress) {
|
|
4632
|
+
throw new Error("Smart wallet address is not available");
|
|
4633
|
+
}
|
|
4475
4634
|
const [isDeployed, currentSafeBalance] = await Promise.all([
|
|
4476
4635
|
isSafeDeployed(safeAddress, chainConfig.publicClient),
|
|
4477
4636
|
minRequired !== void 0 ? chainConfig.publicClient.readContract({
|
|
@@ -4483,7 +4642,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4483
4642
|
this.prepareFirstDeposit(strategy)
|
|
4484
4643
|
]).then(([deployed, balance]) => [deployed, balance]);
|
|
4485
4644
|
if (!isDeployed) {
|
|
4486
|
-
if (ownerType === "Safe" && !this.isPredeployed) {
|
|
4645
|
+
if (ownerType === "Safe" && !this.isPredeployed && !nestedOnboarded) {
|
|
4487
4646
|
await assertParentSafeDeployedOnChain(
|
|
4488
4647
|
userAddress,
|
|
4489
4648
|
chainConfig.publicClient,
|
|
@@ -4517,7 +4676,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4517
4676
|
);
|
|
4518
4677
|
}
|
|
4519
4678
|
}
|
|
4520
|
-
if (ownerType === "Safe" && isDeployed) {
|
|
4679
|
+
if (ownerType === "Safe" && isDeployed && !nestedOnboarded) {
|
|
4521
4680
|
await assertParentSafeDeployedOnChain(
|
|
4522
4681
|
userAddress,
|
|
4523
4682
|
chainConfig.publicClient,
|
|
@@ -4540,35 +4699,35 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4540
4699
|
if (sponsored) return sponsored;
|
|
4541
4700
|
}
|
|
4542
4701
|
if (ownerType === "Safe") {
|
|
4543
|
-
const
|
|
4544
|
-
|
|
4545
|
-
|
|
4546
|
-
|
|
4547
|
-
|
|
4548
|
-
|
|
4549
|
-
|
|
4550
|
-
since
|
|
4702
|
+
const proposed = await walletClient.writeContract({
|
|
4703
|
+
address: token,
|
|
4704
|
+
abi: erc20TransferWriteAbi(assetSymbol),
|
|
4705
|
+
functionName: "transfer",
|
|
4706
|
+
args: [safeAddress, amountBigInt],
|
|
4707
|
+
chain: chainConfig.chain,
|
|
4708
|
+
account: walletClient.account
|
|
4551
4709
|
});
|
|
4552
|
-
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
|
|
4710
|
+
const reference = {
|
|
4711
|
+
userAddress: getAddress4(userAddress),
|
|
4712
|
+
chainId,
|
|
4713
|
+
amount,
|
|
4714
|
+
asset: assetSymbol,
|
|
4715
|
+
childSafe: safeAddress,
|
|
4716
|
+
proposalHash: proposed
|
|
4717
|
+
};
|
|
4718
|
+
try {
|
|
4719
|
+
await onSafeProposal?.(reference);
|
|
4720
|
+
} catch (error) {
|
|
4721
|
+
throw new Error(`Safe proposal ${proposed} was submitted; persist its reference and resumeNestedSafeDeposit instead of sending again: ${thrownMessage(error)}`);
|
|
4563
4722
|
}
|
|
4564
|
-
const executedHash =
|
|
4565
|
-
parentSafe: userAddress,
|
|
4723
|
+
const executedHash = await waitForExactSafeDepositExecution({
|
|
4724
|
+
parentSafe: reference.userAddress,
|
|
4566
4725
|
childSafe: safeAddress,
|
|
4567
4726
|
chainId,
|
|
4568
4727
|
tokenAddress: token,
|
|
4569
4728
|
amount: amountBigInt,
|
|
4570
|
-
|
|
4571
|
-
|
|
4729
|
+
proposalHash: proposed,
|
|
4730
|
+
publicClient: chainConfig.publicClient
|
|
4572
4731
|
});
|
|
4573
4732
|
const registration2 = await this.registerDeposit(
|
|
4574
4733
|
chainId,
|
|
@@ -4743,6 +4902,39 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4743
4902
|
strategy
|
|
4744
4903
|
);
|
|
4745
4904
|
}
|
|
4905
|
+
/**
|
|
4906
|
+
* Resume one specific parent Safe deposit without proposing another transfer.
|
|
4907
|
+
* Persist the reference supplied by sendDeposit's onSafeProposal callback.
|
|
4908
|
+
*
|
|
4909
|
+
* @group Deposits and withdrawals
|
|
4910
|
+
*/
|
|
4911
|
+
async resumeNestedSafeDeposit(reference) {
|
|
4912
|
+
const { userAddress, chainId, childSafe, proposalHash, asset, amount } = reference;
|
|
4913
|
+
if (!isSupportedChain(chainId)) throw new Error("Unsupported deposit chain");
|
|
4914
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(proposalHash)) throw new Error("Invalid proposal hash");
|
|
4915
|
+
await this.authenticateUser();
|
|
4916
|
+
if (!this.isConnectedUser(userAddress) || this.sessionEoa && !isAddressEqual2(this.sessionEoa, userAddress)) {
|
|
4917
|
+
throw new Error("Deposit reference does not match the authenticated parent");
|
|
4918
|
+
}
|
|
4919
|
+
await this.hydrateSessionWallet();
|
|
4920
|
+
const resolved = await this.getSafeAddressFor(userAddress, chainId);
|
|
4921
|
+
if (!resolved || !isAddressEqual2(resolved, childSafe)) throw new Error("Deposit reference does not match the registered child");
|
|
4922
|
+
const symbol = resolveAssetSymbol(asset);
|
|
4923
|
+
if (!getAssetChainIds(symbol).includes(chainId) || BigInt(amount) <= 0n) throw new Error("Invalid deposit asset or amount");
|
|
4924
|
+
const config = getChainConfig(chainId, this.rpcUrls);
|
|
4925
|
+
const token = getDefaultTokenAddress(chainId, symbol);
|
|
4926
|
+
const txHash = await waitForExactSafeDepositExecution({
|
|
4927
|
+
parentSafe: userAddress,
|
|
4928
|
+
childSafe,
|
|
4929
|
+
chainId,
|
|
4930
|
+
tokenAddress: token,
|
|
4931
|
+
amount: BigInt(amount),
|
|
4932
|
+
proposalHash,
|
|
4933
|
+
publicClient: config.publicClient
|
|
4934
|
+
});
|
|
4935
|
+
const registration = await this.registerDeposit(chainId, txHash, amount, token);
|
|
4936
|
+
return { success: true, txHash, smartWallet: childSafe, amount, route: "direct", registration };
|
|
4937
|
+
}
|
|
4746
4938
|
/**
|
|
4747
4939
|
* Resolve a deposit without signing.
|
|
4748
4940
|
*
|
|
@@ -5003,7 +5195,8 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5003
5195
|
params.chainId,
|
|
5004
5196
|
params.amount,
|
|
5005
5197
|
route.asset,
|
|
5006
|
-
params.strategy
|
|
5198
|
+
params.strategy,
|
|
5199
|
+
params.onSafeProposal
|
|
5007
5200
|
);
|
|
5008
5201
|
}
|
|
5009
5202
|
if (route.kind === "wrap") {
|
|
@@ -5621,7 +5814,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5621
5814
|
);
|
|
5622
5815
|
} catch (logError) {
|
|
5623
5816
|
throw new Error(
|
|
5624
|
-
`On-chain deposit succeeded (tx=${txHash}) but backend registration failed: ${logError.message}. Call connectAccount() then logDeposit(${chainId}, "${txHash}", "${amount}") to retry.`
|
|
5817
|
+
`On-chain deposit succeeded (tx=${txHash}) but backend registration failed: ${logError.message}. Call connectAccount() then logDeposit(${chainId}, "${txHash}", "${amount}", "${tokenAddress}") to retry.`
|
|
5625
5818
|
);
|
|
5626
5819
|
}
|
|
5627
5820
|
return registration.deposit;
|
|
@@ -5988,6 +6181,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5988
6181
|
);
|
|
5989
6182
|
}
|
|
5990
6183
|
await this.authenticateUser();
|
|
6184
|
+
this.assertConnectedUser(userAddress);
|
|
5991
6185
|
await this.assertAsyncRedemptionSlotFree(
|
|
5992
6186
|
userAddress,
|
|
5993
6187
|
chainId,
|
|
@@ -8513,9 +8707,11 @@ export {
|
|
|
8513
8707
|
WS_ENDPOINT,
|
|
8514
8708
|
WS_URLS,
|
|
8515
8709
|
ZyfaiSDK,
|
|
8710
|
+
buildNestedSafeOnboardingStatus,
|
|
8516
8711
|
buildV1ChainSessions,
|
|
8517
8712
|
createBankrProvider,
|
|
8518
8713
|
decodeUserOperationCalls,
|
|
8714
|
+
deriveNestedSafeOnboardingStep,
|
|
8519
8715
|
getChainConfig,
|
|
8520
8716
|
getDataApiBaseUrl,
|
|
8521
8717
|
getDefaultTokenAddress,
|
|
@@ -8528,6 +8724,7 @@ export {
|
|
|
8528
8724
|
isSupportedChain,
|
|
8529
8725
|
parseUserOperationJson,
|
|
8530
8726
|
parseV1ChainSessions,
|
|
8727
|
+
portfolioHasWithdrawableBalance,
|
|
8531
8728
|
resolveDepositRoute,
|
|
8532
8729
|
resolveRelaySlippageBps,
|
|
8533
8730
|
serializeV1ChainSessions
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zyfai/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.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",
|