@zyfai/sdk 0.10.0 → 0.12.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 +51 -1
- package/dist/index.d.mts +424 -25
- package/dist/index.d.ts +424 -25
- package/dist/index.js +889 -27
- package/dist/index.mjs +890 -27
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -34,6 +34,7 @@ __export(index_exports, {
|
|
|
34
34
|
API_ENDPOINT: () => API_ENDPOINT,
|
|
35
35
|
DATA_API_BASE_URLS: () => DATA_API_BASE_URLS,
|
|
36
36
|
DATA_API_ENDPOINT: () => DATA_API_ENDPOINT,
|
|
37
|
+
DEFAULT_RELAY_SLIPPAGE_BPS: () => DEFAULT_RELAY_SLIPPAGE_BPS,
|
|
37
38
|
DEFAULT_TOKEN_ADDRESSES: () => DEFAULT_TOKEN_ADDRESSES,
|
|
38
39
|
ERC7579_LAUNCHPAD_ADDRESS: () => ERC7579_LAUNCHPAD_ADDRESS,
|
|
39
40
|
EXECUTION_API_BASE_URLS: () => EXECUTION_API_BASE_URLS,
|
|
@@ -42,9 +43,11 @@ __export(index_exports, {
|
|
|
42
43
|
LOCAL_DATA_API_BASE_URL: () => LOCAL_DATA_API_BASE_URL,
|
|
43
44
|
LOCAL_EXECUTION_API_BASE_URL: () => LOCAL_EXECUTION_API_BASE_URL,
|
|
44
45
|
LOCAL_WS_URL: () => LOCAL_WS_URL,
|
|
46
|
+
MAX_RELAY_SLIPPAGE_BPS: () => MAX_RELAY_SLIPPAGE_BPS,
|
|
45
47
|
OWNABLE_VALIDATOR: () => OWNABLE_VALIDATOR,
|
|
46
48
|
POOL_MODULE_ADDRESSES: () => POOL_MODULE_ADDRESSES,
|
|
47
49
|
PROXY_EXECUTOR: () => PROXY_EXECUTOR,
|
|
50
|
+
RELAY_SLIPPAGE_PRESETS_BPS: () => RELAY_SLIPPAGE_PRESETS_BPS,
|
|
48
51
|
SAFE_7579_ADDRESS: () => SAFE_7579_ADDRESS,
|
|
49
52
|
SMART_SESSIONS_FALLBACK: () => SMART_SESSIONS_FALLBACK,
|
|
50
53
|
SMART_SESSIONS_VALIDATOR: () => SMART_SESSIONS_VALIDATOR,
|
|
@@ -63,7 +66,9 @@ __export(index_exports, {
|
|
|
63
66
|
getManagedAssets: () => getManagedAssets,
|
|
64
67
|
getSupportedChainIds: () => getSupportedChainIds,
|
|
65
68
|
getWsUrl: () => getWsUrl,
|
|
66
|
-
isSupportedChain: () => isSupportedChain
|
|
69
|
+
isSupportedChain: () => isSupportedChain,
|
|
70
|
+
resolveDepositRoute: () => resolveDepositRoute,
|
|
71
|
+
resolveRelaySlippageBps: () => resolveRelaySlippageBps
|
|
67
72
|
});
|
|
68
73
|
module.exports = __toCommonJS(index_exports);
|
|
69
74
|
|
|
@@ -123,10 +128,15 @@ var ENDPOINTS = {
|
|
|
123
128
|
USER_WITHDRAW: "/users/withdraw/v2",
|
|
124
129
|
PARTIAL_WITHDRAW: "/users/partial-withdraw/v2",
|
|
125
130
|
LOG_DEPOSIT: "/users/log_deposit/v2",
|
|
131
|
+
FUNDING_ORDERS: "/users/me/funding-orders",
|
|
132
|
+
FUNDING_ORDER: (orderId) => `/users/me/funding-orders/${encodeURIComponent(orderId)}`,
|
|
133
|
+
FUNDING_ORDER_QUOTE: (orderId) => `/users/me/funding-orders/${encodeURIComponent(orderId)}/quote`,
|
|
134
|
+
FUNDING_ORDER_EXECUTION: (orderId) => `/users/me/funding-orders/${encodeURIComponent(orderId)}/execution`,
|
|
126
135
|
DEPOSIT_STATUS: (depositId) => `/users/deposits/${depositId}/v2`,
|
|
127
136
|
WITHDRAW_STATUS: (withdrawalId) => `/users/withdrawals/${withdrawalId}`,
|
|
128
137
|
// Safe Deployment (single endpoint)
|
|
129
138
|
SAFE_DEPLOY: "/users/safe-deploy",
|
|
139
|
+
RZFI_CLAIM: "/users/rzfi-claim",
|
|
130
140
|
// Multichain deploy for predeployed (pool) wallets — deploys + rotates the
|
|
131
141
|
// same address on the requested chains (sponsored, no user signature).
|
|
132
142
|
DEPLOY_CHAINS: "/users/deploy-chains",
|
|
@@ -178,6 +188,8 @@ var ENDPOINTS = {
|
|
|
178
188
|
CUSTOMIZATION_SELECTED_POOLS: (protocolId, chainId) => `/customization/selected-pools?protocolId=${protocolId}&chainId=${chainId}`
|
|
179
189
|
};
|
|
180
190
|
var DATA_ENDPOINTS = {
|
|
191
|
+
// Merkl incentive campaigns
|
|
192
|
+
MERKL_CAMPAIGN_INFO: (campaign) => `/merkl/${campaign}/campaign-info`,
|
|
181
193
|
// User Initialization
|
|
182
194
|
USER_INITIALIZE: "/api/earnings/initialize",
|
|
183
195
|
// Earnings
|
|
@@ -554,6 +566,15 @@ var ERC20_ABI = [
|
|
|
554
566
|
outputs: [{ name: "", type: "uint256" }]
|
|
555
567
|
}
|
|
556
568
|
];
|
|
569
|
+
var WETH_DEPOSIT_ABI = [
|
|
570
|
+
{
|
|
571
|
+
name: "deposit",
|
|
572
|
+
type: "function",
|
|
573
|
+
stateMutability: "payable",
|
|
574
|
+
inputs: [],
|
|
575
|
+
outputs: []
|
|
576
|
+
}
|
|
577
|
+
];
|
|
557
578
|
var ERC20_TRANSFER_LEGACY_ABI = [
|
|
558
579
|
{
|
|
559
580
|
name: "transfer",
|
|
@@ -748,9 +769,9 @@ function nextWithdrawCompleteDeadline(startedAt, previousDeadline, estimatedClai
|
|
|
748
769
|
var AGENT_DEPOSIT_INTENT_INTERVAL_MS = 1500;
|
|
749
770
|
var AGENT_DEPOSIT_INTENT_TIMEOUT_MS = 30 * 60 * 1e3;
|
|
750
771
|
var formatMinPortfolioLabel = (raw, decimals, symbol) => {
|
|
751
|
-
const
|
|
752
|
-
const whole = raw /
|
|
753
|
-
const fraction = raw %
|
|
772
|
+
const base3 = 10n ** BigInt(decimals);
|
|
773
|
+
const whole = raw / base3;
|
|
774
|
+
const fraction = raw % base3;
|
|
754
775
|
if (fraction === 0n) {
|
|
755
776
|
return `${whole.toString()} ${symbol}`;
|
|
756
777
|
}
|
|
@@ -805,6 +826,7 @@ function isMainnetMinPortfolioExempt(chainId, ...addresses) {
|
|
|
805
826
|
// src/core/ZyfaiSDK.ts
|
|
806
827
|
var import_accounts2 = require("viem/accounts");
|
|
807
828
|
var import_viem4 = require("viem");
|
|
829
|
+
var import_relay_sdk2 = require("@relayprotocol/relay-sdk");
|
|
808
830
|
|
|
809
831
|
// src/config/chains.ts
|
|
810
832
|
var import_viem2 = require("viem");
|
|
@@ -999,6 +1021,228 @@ var getSupportedChainIds = () => {
|
|
|
999
1021
|
return Object.keys(CHAINS).map(Number);
|
|
1000
1022
|
};
|
|
1001
1023
|
|
|
1024
|
+
// src/deposits/deposit-route.ts
|
|
1025
|
+
var NATIVE_CURRENCY = "0x0000000000000000000000000000000000000000";
|
|
1026
|
+
var RELAY_DESTINATION_ASSETS = [
|
|
1027
|
+
"USDC",
|
|
1028
|
+
"WETH",
|
|
1029
|
+
"CBBTC",
|
|
1030
|
+
"WBTC",
|
|
1031
|
+
"EURC",
|
|
1032
|
+
"USDT",
|
|
1033
|
+
"PYUSD",
|
|
1034
|
+
"NVDAC",
|
|
1035
|
+
"GOOGLC",
|
|
1036
|
+
"AAPLC",
|
|
1037
|
+
"METAC"
|
|
1038
|
+
];
|
|
1039
|
+
var lookupCollateral = (asset) => Object.keys(ASSET_CONFIGS).find(
|
|
1040
|
+
(key) => key.toLowerCase() === asset.toLowerCase()
|
|
1041
|
+
) ?? null;
|
|
1042
|
+
var toRelayDestinationAsset = (asset) => asset.trim().toUpperCase();
|
|
1043
|
+
var isNativeSpendToken = (token) => {
|
|
1044
|
+
const value = token.trim().toLowerCase();
|
|
1045
|
+
return value === "eth" || value === "native" || value === NATIVE_CURRENCY;
|
|
1046
|
+
};
|
|
1047
|
+
var isSameCollateralToken = (spend, collateral, chainId) => {
|
|
1048
|
+
if (spend.toLowerCase() === collateral.toLowerCase()) return true;
|
|
1049
|
+
const address = ASSET_CONFIGS[collateral]?.addresses?.[chainId];
|
|
1050
|
+
return !!address && spend.toLowerCase() === address.toLowerCase();
|
|
1051
|
+
};
|
|
1052
|
+
var originCurrencyForSpend = (spend, chainId) => {
|
|
1053
|
+
if (isNativeSpendToken(spend)) return NATIVE_CURRENCY;
|
|
1054
|
+
if (/^0x[0-9a-fA-F]{40}$/.test(spend)) return spend;
|
|
1055
|
+
const collateral = lookupCollateral(spend);
|
|
1056
|
+
if (!collateral) {
|
|
1057
|
+
throw new Error(
|
|
1058
|
+
`spendToken must be a collateral symbol or a token contract address. Received "${spend}".`
|
|
1059
|
+
);
|
|
1060
|
+
}
|
|
1061
|
+
const address = ASSET_CONFIGS[collateral]?.addresses?.[chainId];
|
|
1062
|
+
if (!address) {
|
|
1063
|
+
throw new Error(`${collateral} is not available on chain ${chainId}.`);
|
|
1064
|
+
}
|
|
1065
|
+
return address;
|
|
1066
|
+
};
|
|
1067
|
+
var resolveDepositRoute = (input) => {
|
|
1068
|
+
const asset = input.asset?.trim();
|
|
1069
|
+
if (!asset) throw new Error("Asset is required");
|
|
1070
|
+
const spendChainId = input.spendChainId ?? input.chainId;
|
|
1071
|
+
const collateral = lookupCollateral(asset);
|
|
1072
|
+
const spend = input.spendToken?.trim();
|
|
1073
|
+
const holdsDestinationCollateral = !spend || spendChainId === input.chainId && !!collateral && isSameCollateralToken(spend, collateral, input.chainId);
|
|
1074
|
+
if (holdsDestinationCollateral) {
|
|
1075
|
+
if (!collateral) {
|
|
1076
|
+
throw new Error(
|
|
1077
|
+
`${asset} cannot be transferred directly. Pass spendToken to swap another token into it.`
|
|
1078
|
+
);
|
|
1079
|
+
}
|
|
1080
|
+
const chains = getAssetChainIds(collateral);
|
|
1081
|
+
if (!chains.includes(input.chainId)) {
|
|
1082
|
+
throw new Error(
|
|
1083
|
+
`${collateral} is not available on chain ${input.chainId}. Supported chains: ${chains.join(", ")}.`
|
|
1084
|
+
);
|
|
1085
|
+
}
|
|
1086
|
+
return { kind: "direct", asset: collateral };
|
|
1087
|
+
}
|
|
1088
|
+
if (collateral === "WETH" && spendChainId === input.chainId && spend && isNativeSpendToken(spend)) {
|
|
1089
|
+
return { kind: "wrap", asset: "WETH" };
|
|
1090
|
+
}
|
|
1091
|
+
const destinationAsset = toRelayDestinationAsset(asset);
|
|
1092
|
+
if (!RELAY_DESTINATION_ASSETS.includes(
|
|
1093
|
+
destinationAsset
|
|
1094
|
+
)) {
|
|
1095
|
+
throw new Error(
|
|
1096
|
+
`${asset} is not a collateral Relay can deliver. Supported: ${RELAY_DESTINATION_ASSETS.join(", ")}.`
|
|
1097
|
+
);
|
|
1098
|
+
}
|
|
1099
|
+
return {
|
|
1100
|
+
kind: "swap",
|
|
1101
|
+
originChainId: spendChainId,
|
|
1102
|
+
originCurrency: originCurrencyForSpend(spend, spendChainId),
|
|
1103
|
+
destinationAsset
|
|
1104
|
+
};
|
|
1105
|
+
};
|
|
1106
|
+
|
|
1107
|
+
// src/deposits/funding-order-route.ts
|
|
1108
|
+
var sameText = (left, right) => typeof left === "string" && left.trim().toLowerCase() === right.trim().toLowerCase();
|
|
1109
|
+
var assertStoredFundingOrderRoute = (order, requested) => {
|
|
1110
|
+
const mismatches = [];
|
|
1111
|
+
if (order.originChainId !== requested.originChainId) {
|
|
1112
|
+
mismatches.push(
|
|
1113
|
+
`originChainId caller ${requested.originChainId}, stored ${order.originChainId}`
|
|
1114
|
+
);
|
|
1115
|
+
}
|
|
1116
|
+
if (!sameText(order.originCurrency, requested.originCurrency)) {
|
|
1117
|
+
mismatches.push(
|
|
1118
|
+
`originCurrency caller ${requested.originCurrency}, stored ${order.originCurrency}`
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
if (order.destinationChainId !== requested.destinationChainId) {
|
|
1122
|
+
mismatches.push(
|
|
1123
|
+
`destinationChainId caller ${requested.destinationChainId}, stored ${order.destinationChainId}`
|
|
1124
|
+
);
|
|
1125
|
+
}
|
|
1126
|
+
if (!sameText(order.destinationAsset, requested.destinationAsset)) {
|
|
1127
|
+
mismatches.push(
|
|
1128
|
+
`destinationAsset caller ${requested.destinationAsset}, stored ${order.destinationAsset}`
|
|
1129
|
+
);
|
|
1130
|
+
}
|
|
1131
|
+
if (mismatches.length === 0) return;
|
|
1132
|
+
throw new Error(
|
|
1133
|
+
`Funding order ${order.id} does not match this swap: ${mismatches.join("; ")}`
|
|
1134
|
+
);
|
|
1135
|
+
};
|
|
1136
|
+
|
|
1137
|
+
// src/deposits/collateral-minimum.ts
|
|
1138
|
+
var positiveUnits = (value) => {
|
|
1139
|
+
if (!value || value === "0") return null;
|
|
1140
|
+
return value;
|
|
1141
|
+
};
|
|
1142
|
+
var resolveCollateralMinimum = (storedMinimum, quote) => positiveUnits(storedMinimum) ?? positiveUnits(quote.details?.currencyOut?.minimumAmount) ?? positiveUnits(quote.details?.currencyOut?.amount) ?? "0";
|
|
1143
|
+
|
|
1144
|
+
// src/deposits/relay-slippage.ts
|
|
1145
|
+
var DEFAULT_RELAY_SLIPPAGE_BPS = 200;
|
|
1146
|
+
var MAX_RELAY_SLIPPAGE_BPS = 500;
|
|
1147
|
+
var RELAY_SLIPPAGE_PRESETS_BPS = [50, 200, 500];
|
|
1148
|
+
var resolveRelaySlippageBps = (clientBps) => {
|
|
1149
|
+
if (clientBps == null || !Number.isFinite(clientBps)) {
|
|
1150
|
+
return DEFAULT_RELAY_SLIPPAGE_BPS;
|
|
1151
|
+
}
|
|
1152
|
+
return Math.min(
|
|
1153
|
+
Math.max(1, Math.trunc(clientBps)),
|
|
1154
|
+
MAX_RELAY_SLIPPAGE_BPS
|
|
1155
|
+
);
|
|
1156
|
+
};
|
|
1157
|
+
|
|
1158
|
+
// src/deposits/relay-client.ts
|
|
1159
|
+
var import_relay_sdk = require("@relayprotocol/relay-sdk");
|
|
1160
|
+
var import_chains3 = require("viem/chains");
|
|
1161
|
+
var RELAY_FUNDING_CHAIN_IDS = [import_chains3.mainnet.id, import_chains3.base.id, import_chains3.arbitrum.id];
|
|
1162
|
+
var clientHasFundingChains = () => {
|
|
1163
|
+
const client = (0, import_relay_sdk.getClient)();
|
|
1164
|
+
if (!client?.baseApiUrl) return false;
|
|
1165
|
+
return RELAY_FUNDING_CHAIN_IDS.every(
|
|
1166
|
+
(chainId) => client.chains.some((chain) => chain.id === chainId)
|
|
1167
|
+
);
|
|
1168
|
+
};
|
|
1169
|
+
var ensureRelaySdkClient = () => {
|
|
1170
|
+
if (clientHasFundingChains()) return;
|
|
1171
|
+
(0, import_relay_sdk.createClient)({
|
|
1172
|
+
baseApiUrl: import_relay_sdk.MAINNET_RELAY_API,
|
|
1173
|
+
source: "zyf.ai",
|
|
1174
|
+
chains: [import_chains3.mainnet, import_chains3.base, import_chains3.arbitrum].map(import_relay_sdk.convertViemChainToRelayChain)
|
|
1175
|
+
});
|
|
1176
|
+
};
|
|
1177
|
+
|
|
1178
|
+
// src/deposits/relay-fill.ts
|
|
1179
|
+
var TRANSFER_TOPIC = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
|
|
1180
|
+
var collectTxHashes = (execution) => {
|
|
1181
|
+
const out = [];
|
|
1182
|
+
for (const step of execution.steps ?? []) {
|
|
1183
|
+
for (const item of step.items ?? []) {
|
|
1184
|
+
for (const row of item.txHashes ?? []) {
|
|
1185
|
+
if (row.txHash) out.push(row);
|
|
1186
|
+
}
|
|
1187
|
+
for (const row of item.internalTxHashes ?? []) {
|
|
1188
|
+
if (row.txHash) out.push(row);
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
return out;
|
|
1193
|
+
};
|
|
1194
|
+
var collectRelaySourceTxHashes = (execution, originChainId) => {
|
|
1195
|
+
const hashes = /* @__PURE__ */ new Set();
|
|
1196
|
+
for (const row of collectTxHashes(execution)) {
|
|
1197
|
+
if (row.chainId === originChainId) hashes.add(row.txHash);
|
|
1198
|
+
}
|
|
1199
|
+
return [...hashes];
|
|
1200
|
+
};
|
|
1201
|
+
var collectRelayDestinationTxHashes = (execution, destinationChainId) => {
|
|
1202
|
+
const hashes = /* @__PURE__ */ new Set();
|
|
1203
|
+
for (const row of collectTxHashes(execution)) {
|
|
1204
|
+
if (row.chainId === destinationChainId && row.txHash) {
|
|
1205
|
+
hashes.add(row.txHash);
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
return [...hashes];
|
|
1209
|
+
};
|
|
1210
|
+
var transferAmountTo = (logs, token, recipient) => {
|
|
1211
|
+
const tokenAddr = token.toLowerCase();
|
|
1212
|
+
const recipientAddr = recipient.toLowerCase();
|
|
1213
|
+
let total = 0n;
|
|
1214
|
+
for (const log of logs) {
|
|
1215
|
+
if (log.address.toLowerCase() !== tokenAddr) continue;
|
|
1216
|
+
if (log.topics[0]?.toLowerCase() !== TRANSFER_TOPIC) continue;
|
|
1217
|
+
const toTopic = log.topics[2];
|
|
1218
|
+
if (!toTopic) continue;
|
|
1219
|
+
const to = `0x${toTopic.slice(-40)}`.toLowerCase();
|
|
1220
|
+
if (to !== recipientAddr) continue;
|
|
1221
|
+
try {
|
|
1222
|
+
const value = BigInt(log.data);
|
|
1223
|
+
if (value > 0n) total += value;
|
|
1224
|
+
} catch {
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
return total;
|
|
1228
|
+
};
|
|
1229
|
+
var receiptTransfersTokenTo = (logs, token, recipient) => transferAmountTo(logs, token, recipient) > 0n;
|
|
1230
|
+
var selectRelayCreditTxHash = (params) => {
|
|
1231
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1232
|
+
for (const hash of params.candidates) {
|
|
1233
|
+
if (!hash) continue;
|
|
1234
|
+
const key = hash.toLowerCase();
|
|
1235
|
+
if (seen.has(key)) continue;
|
|
1236
|
+
seen.add(key);
|
|
1237
|
+
const receipt = params.receipts.get(key);
|
|
1238
|
+
if (!receipt || receipt.status !== "success") continue;
|
|
1239
|
+
if (receiptTransfersTokenTo(receipt.logs, params.token, params.recipient)) {
|
|
1240
|
+
return hash;
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
return null;
|
|
1244
|
+
};
|
|
1245
|
+
|
|
1002
1246
|
// src/config/managed-assets.ts
|
|
1003
1247
|
var MANAGED_ASSET_ORDER = [
|
|
1004
1248
|
"USDC",
|
|
@@ -1813,6 +2057,13 @@ var AgentDepositIntentTimeoutError = class extends Error {
|
|
|
1813
2057
|
this.name = "AgentDepositIntentTimeoutError";
|
|
1814
2058
|
}
|
|
1815
2059
|
};
|
|
2060
|
+
var thrownMessage = (error) => {
|
|
2061
|
+
if (error instanceof Error) return error.message;
|
|
2062
|
+
if (error && typeof error === "object" && "message" in error && typeof error.message === "string") {
|
|
2063
|
+
return error.message;
|
|
2064
|
+
}
|
|
2065
|
+
return String(error);
|
|
2066
|
+
};
|
|
1816
2067
|
var _ZyfaiSDK = class _ZyfaiSDK {
|
|
1817
2068
|
/**
|
|
1818
2069
|
* Create an SDK instance bound to a partner API key.
|
|
@@ -2999,14 +3250,15 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2999
3250
|
effectiveChains,
|
|
3000
3251
|
withPools,
|
|
3001
3252
|
strategy,
|
|
3002
|
-
mandateChainId
|
|
3253
|
+
mandateChainId,
|
|
3254
|
+
existingChains
|
|
3003
3255
|
);
|
|
3004
3256
|
}
|
|
3005
3257
|
/**
|
|
3006
3258
|
* Persist protocol discovery for one asset (user PATCH or agent deposit-setup).
|
|
3007
3259
|
* @internal
|
|
3008
3260
|
*/
|
|
3009
|
-
async persistDiscoveredAssetProfile(asset, effectiveChains, withPools, strategy, agentDepositChainId) {
|
|
3261
|
+
async persistDiscoveredAssetProfile(asset, effectiveChains, withPools, strategy, agentDepositChainId, existingChains = []) {
|
|
3010
3262
|
const internalAsset = convertAssetInternally(asset);
|
|
3011
3263
|
const assetSettings = {
|
|
3012
3264
|
chains: effectiveChains,
|
|
@@ -3015,9 +3267,18 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3015
3267
|
if (strategy !== void 0) {
|
|
3016
3268
|
assetSettings.rebalanceStrategy = toInternalStrategy(strategy);
|
|
3017
3269
|
}
|
|
3270
|
+
const configureProfile = () => this.updateUserProfile({
|
|
3271
|
+
asset,
|
|
3272
|
+
protocols: withPools,
|
|
3273
|
+
chains: effectiveChains,
|
|
3274
|
+
...strategy !== void 0 && { strategy }
|
|
3275
|
+
});
|
|
3018
3276
|
if (this.sessionChannel === "agent") {
|
|
3019
3277
|
const chainId = agentDepositChainId ?? effectiveChains[effectiveChains.length - 1] ?? effectiveChains[0];
|
|
3020
|
-
|
|
3278
|
+
if (existingChains.includes(chainId)) {
|
|
3279
|
+
return await configureProfile();
|
|
3280
|
+
}
|
|
3281
|
+
const setupResult = await this.httpClient.post(
|
|
3021
3282
|
ENDPOINTS.AGENT_DEPOSIT_SETUP,
|
|
3022
3283
|
{
|
|
3023
3284
|
asset,
|
|
@@ -3028,14 +3289,14 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3028
3289
|
}
|
|
3029
3290
|
}
|
|
3030
3291
|
);
|
|
3292
|
+
if (!setupResult.applied) {
|
|
3293
|
+
throw new Error(
|
|
3294
|
+
`Agent deposit setup did not apply for ${asset} on chain ${chainId}.`
|
|
3295
|
+
);
|
|
3296
|
+
}
|
|
3031
3297
|
return await this.getUserDetails(asset);
|
|
3032
3298
|
}
|
|
3033
|
-
return await
|
|
3034
|
-
asset,
|
|
3035
|
-
protocols: withPools,
|
|
3036
|
-
chains: effectiveChains,
|
|
3037
|
-
...strategy !== void 0 && { strategy }
|
|
3038
|
-
});
|
|
3299
|
+
return await configureProfile();
|
|
3039
3300
|
}
|
|
3040
3301
|
/**
|
|
3041
3302
|
* Switch one asset to a strategy and select the protocols that go with it.
|
|
@@ -3270,14 +3531,14 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3270
3531
|
return this.httpClient.post(ENDPOINTS.DEPLOY_CHAINS, { chainIds });
|
|
3271
3532
|
}
|
|
3272
3533
|
/**
|
|
3273
|
-
* Transfer
|
|
3534
|
+
* Transfer a collateral token the wallet already holds into the Safe.
|
|
3274
3535
|
*
|
|
3275
3536
|
* @remarks
|
|
3276
|
-
*
|
|
3277
|
-
* {@link ZyfaiSDK.
|
|
3278
|
-
*
|
|
3279
|
-
* {@link ZyfaiSDK.logDeposit}, {@link ZyfaiSDK.getDepositStatus}.
|
|
3537
|
+
* Use this when the connected wallet holds the strategy collateral on the
|
|
3538
|
+
* deposit chain. {@link ZyfaiSDK.sendDeposit} calls this automatically in
|
|
3539
|
+
* that case. A different token uses {@link ZyfaiSDK.sendSwapDeposit}.
|
|
3280
3540
|
*
|
|
3541
|
+
* Confirms the ERC-20 `transfer`, then calls {@link ZyfaiSDK.logDeposit}.
|
|
3281
3542
|
* Requires {@link ZyfaiSDK.connectAccount}. Pass the **EOA** as `userAddress`.
|
|
3282
3543
|
*
|
|
3283
3544
|
* @group Deposits and withdrawals
|
|
@@ -3285,20 +3546,22 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3285
3546
|
* @param userAddress - Owner EOA (not the Safe)
|
|
3286
3547
|
* @param chainId - Chain for the transfer
|
|
3287
3548
|
* @param amount - Amount in least units (string integer)
|
|
3288
|
-
* @param asset -
|
|
3549
|
+
* @param asset - Collateral symbol (`USDC`, `WETH`, `EURC`, `NVDAc`)
|
|
3289
3550
|
* @param strategy - Optional first-deposit {@link Strategy}
|
|
3290
|
-
* @returns Transfer receipt and deposit registration
|
|
3551
|
+
* @returns Transfer receipt and deposit registration
|
|
3552
|
+
*
|
|
3553
|
+
* @throws If validation fails, the minimum portfolio is not met, the Safe is unavailable, or transfer/register fails
|
|
3291
3554
|
*
|
|
3292
|
-
* @
|
|
3555
|
+
* @see {@link ZyfaiSDK.sendDeposit}
|
|
3293
3556
|
*
|
|
3294
3557
|
* @example
|
|
3295
3558
|
* ```typescript
|
|
3296
3559
|
* await sdk.connectAccount(provider);
|
|
3297
|
-
* const sent = await sdk.
|
|
3298
|
-
* await sdk.
|
|
3560
|
+
* const sent = await sdk.sendDirectDeposit(eoa, 8453, "10000000", "USDC", "conservative");
|
|
3561
|
+
* await sdk.waitForDepositPosition(sent.registration.id, 8453);
|
|
3299
3562
|
* ```
|
|
3300
3563
|
*/
|
|
3301
|
-
async
|
|
3564
|
+
async sendDirectDeposit(userAddress, chainId, amount, asset, strategy) {
|
|
3302
3565
|
try {
|
|
3303
3566
|
if (!userAddress) {
|
|
3304
3567
|
throw new Error("User address is required");
|
|
@@ -3415,12 +3678,489 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3415
3678
|
txHash,
|
|
3416
3679
|
smartWallet: safeAddress,
|
|
3417
3680
|
amount: amountBigInt.toString(),
|
|
3681
|
+
route: "direct",
|
|
3418
3682
|
registration
|
|
3419
3683
|
};
|
|
3420
3684
|
} catch (error) {
|
|
3421
3685
|
throw new Error(`Deposit failed: ${error.message}`);
|
|
3422
3686
|
}
|
|
3423
3687
|
}
|
|
3688
|
+
async sendDeposit(userAddressOrParams, chainId, amount, asset, strategy) {
|
|
3689
|
+
if (typeof userAddressOrParams !== "string") {
|
|
3690
|
+
return this.sendRoutedDeposit(userAddressOrParams);
|
|
3691
|
+
}
|
|
3692
|
+
if (chainId === void 0 || amount === void 0 || asset === void 0) {
|
|
3693
|
+
throw new Error(
|
|
3694
|
+
"Deposit failed: chainId, amount, and asset are required"
|
|
3695
|
+
);
|
|
3696
|
+
}
|
|
3697
|
+
return this.sendDirectDeposit(
|
|
3698
|
+
userAddressOrParams,
|
|
3699
|
+
chainId,
|
|
3700
|
+
amount,
|
|
3701
|
+
asset,
|
|
3702
|
+
strategy
|
|
3703
|
+
);
|
|
3704
|
+
}
|
|
3705
|
+
/**
|
|
3706
|
+
* Resolve a deposit without signing.
|
|
3707
|
+
*
|
|
3708
|
+
* @remarks
|
|
3709
|
+
* A collateral the wallet already holds on `chainId` returns `route: "direct"`.
|
|
3710
|
+
* Same-chain native ETH into WETH returns `route: "wrap"`. Any other spend
|
|
3711
|
+
* token returns a Relay review quote (`route: "swap"`). Nothing is signed.
|
|
3712
|
+
* Optional `slippageBps` (default 200, max 500) is stored on that funding
|
|
3713
|
+
* order and reused when the quote is refreshed for signing.
|
|
3714
|
+
*
|
|
3715
|
+
* @group Deposits and withdrawals
|
|
3716
|
+
*
|
|
3717
|
+
* @param params - Destination collateral and optional spend token
|
|
3718
|
+
* @returns The route, and a review quote when a swap is required
|
|
3719
|
+
*
|
|
3720
|
+
* @throws If the spend token or destination collateral is not supported
|
|
3721
|
+
*/
|
|
3722
|
+
async quoteDeposit(params) {
|
|
3723
|
+
if (!params.userAddress) throw new Error("User address is required");
|
|
3724
|
+
if (!isSupportedChain(params.chainId)) {
|
|
3725
|
+
throw new Error(`Unsupported chain ID: ${params.chainId}`);
|
|
3726
|
+
}
|
|
3727
|
+
if (!params.amount || BigInt(params.amount) <= 0n) {
|
|
3728
|
+
throw new Error("Valid amount is required");
|
|
3729
|
+
}
|
|
3730
|
+
const route = resolveDepositRoute(params);
|
|
3731
|
+
if (route.kind === "direct" || route.kind === "wrap") {
|
|
3732
|
+
return {
|
|
3733
|
+
route: route.kind,
|
|
3734
|
+
asset: route.asset,
|
|
3735
|
+
chainId: params.chainId,
|
|
3736
|
+
amount: params.amount
|
|
3737
|
+
};
|
|
3738
|
+
}
|
|
3739
|
+
const quote = await this.quoteSwapDeposit({
|
|
3740
|
+
userAddress: params.userAddress,
|
|
3741
|
+
originChainId: route.originChainId,
|
|
3742
|
+
originCurrency: route.originCurrency,
|
|
3743
|
+
originAmount: params.amount,
|
|
3744
|
+
destinationChainId: params.chainId,
|
|
3745
|
+
destinationAsset: route.destinationAsset,
|
|
3746
|
+
strategy: params.strategy,
|
|
3747
|
+
slippageBps: params.slippageBps
|
|
3748
|
+
});
|
|
3749
|
+
return { route: "swap", ...quote };
|
|
3750
|
+
}
|
|
3751
|
+
/**
|
|
3752
|
+
* Quote a Relay swap that delivers a collateral token to the Safe.
|
|
3753
|
+
*
|
|
3754
|
+
* @remarks
|
|
3755
|
+
* Creates a funding order on the execution API. The connected wallet does
|
|
3756
|
+
* not sign. Pass `orderId` to {@link ZyfaiSDK.sendSwapDeposit} to sign the
|
|
3757
|
+
* refreshed quote, or call {@link ZyfaiSDK.sendDeposit} to quote and sign
|
|
3758
|
+
* in one step.
|
|
3759
|
+
*
|
|
3760
|
+
* Same-chain native ETH into WETH is rejected here. Use {@link ZyfaiSDK.sendDeposit}
|
|
3761
|
+
* with `spendToken: "ETH"`, which wraps in the wallet.
|
|
3762
|
+
*
|
|
3763
|
+
* Optional `slippageBps` defaults to 200 (max 500) and is fixed when this
|
|
3764
|
+
* funding order is created. Sign-time refresh reuses the stored value.
|
|
3765
|
+
*
|
|
3766
|
+
* @group Deposits and withdrawals
|
|
3767
|
+
*
|
|
3768
|
+
* @param params - Origin token, destination collateral, and funding wallet
|
|
3769
|
+
* @returns Review quote. Not yet signed.
|
|
3770
|
+
*
|
|
3771
|
+
* @throws If the server rejects the pair, the amount, or the quote
|
|
3772
|
+
*/
|
|
3773
|
+
async quoteSwapDeposit(params) {
|
|
3774
|
+
this.assertSwapDepositParams(params);
|
|
3775
|
+
await this.authenticateUser();
|
|
3776
|
+
const created = await this.httpClient.post(
|
|
3777
|
+
ENDPOINTS.FUNDING_ORDERS,
|
|
3778
|
+
{
|
|
3779
|
+
originChainId: params.originChainId,
|
|
3780
|
+
destinationChainId: params.destinationChainId,
|
|
3781
|
+
originCurrency: params.originCurrency,
|
|
3782
|
+
originAmount: params.originAmount,
|
|
3783
|
+
destinationAsset: params.destinationAsset.toUpperCase(),
|
|
3784
|
+
fundingWallet: params.userAddress,
|
|
3785
|
+
...params.slippageBps != null ? { slippageBps: resolveRelaySlippageBps(params.slippageBps) } : {}
|
|
3786
|
+
}
|
|
3787
|
+
);
|
|
3788
|
+
return {
|
|
3789
|
+
orderId: created.orderId,
|
|
3790
|
+
status: created.status,
|
|
3791
|
+
spend: created.spend,
|
|
3792
|
+
expectedOutput: created.expectedOutput,
|
|
3793
|
+
minimumOutput: created.minimumOutput,
|
|
3794
|
+
destinationChainId: created.destinationChainId,
|
|
3795
|
+
destinationCurrency: created.destinationCurrency,
|
|
3796
|
+
destinationAsset: params.destinationAsset.toUpperCase(),
|
|
3797
|
+
fees: created.fees ?? [],
|
|
3798
|
+
investEligible: created.investEligible,
|
|
3799
|
+
expiresAt: created.expiresAt ?? null,
|
|
3800
|
+
timeEstimate: created.timeEstimate ?? null
|
|
3801
|
+
};
|
|
3802
|
+
}
|
|
3803
|
+
/**
|
|
3804
|
+
* Read a Relay funding order, including a linked deposit once credit starts.
|
|
3805
|
+
*
|
|
3806
|
+
* @group Deposits and withdrawals
|
|
3807
|
+
*
|
|
3808
|
+
* @param orderId - Id from {@link ZyfaiSDK.quoteSwapDeposit}
|
|
3809
|
+
* @returns Stored order
|
|
3810
|
+
*
|
|
3811
|
+
* @throws If `orderId` is missing
|
|
3812
|
+
*/
|
|
3813
|
+
async getSwapDeposit(orderId) {
|
|
3814
|
+
if (!orderId) throw new Error("Funding order id is required");
|
|
3815
|
+
await this.authenticateUser();
|
|
3816
|
+
return this.httpClient.get(ENDPOINTS.FUNDING_ORDER(orderId));
|
|
3817
|
+
}
|
|
3818
|
+
/**
|
|
3819
|
+
* Swap or bridge a token into a Zyfai collateral, then register the deposit.
|
|
3820
|
+
*
|
|
3821
|
+
* @remarks
|
|
3822
|
+
* The execution API quotes Relay with the Safe as recipient. This method
|
|
3823
|
+
* refreshes that quote, signs the steps with the connected wallet, waits
|
|
3824
|
+
* for the collateral transfer into the Safe, and calls {@link ZyfaiSDK.logDeposit}.
|
|
3825
|
+
* Credit still requires that verified transfer. A refund or failed fill does
|
|
3826
|
+
* not register a deposit.
|
|
3827
|
+
*
|
|
3828
|
+
* Requires {@link ZyfaiSDK.connectAccount}. Origin chains other than
|
|
3829
|
+
* Ethereum, Base, and Arbitrum need an EIP-1193 wallet that can sign there.
|
|
3830
|
+
*
|
|
3831
|
+
* Optional `slippageBps` defaults to 200 (max 500) and is fixed when the
|
|
3832
|
+
* funding order is created. A later call that passes `orderId` ignores
|
|
3833
|
+
* `slippageBps` and signs the stored order.
|
|
3834
|
+
*
|
|
3835
|
+
* @group Deposits and withdrawals
|
|
3836
|
+
*
|
|
3837
|
+
* @param params - Origin token and destination collateral
|
|
3838
|
+
* @returns Destination transfer and deposit registration
|
|
3839
|
+
*
|
|
3840
|
+
* @throws If the quote, signature, fill, or registration fails, or if
|
|
3841
|
+
* `orderId` names a funding order whose origin or destination differs from
|
|
3842
|
+
* this call
|
|
3843
|
+
*
|
|
3844
|
+
* @see {@link ZyfaiSDK.sendDeposit}
|
|
3845
|
+
*
|
|
3846
|
+
* @example
|
|
3847
|
+
* ```typescript
|
|
3848
|
+
* await sdk.connectAccount(provider);
|
|
3849
|
+
* const sent = await sdk.sendSwapDeposit({
|
|
3850
|
+
* userAddress: eoa,
|
|
3851
|
+
* originChainId: 42161,
|
|
3852
|
+
* originCurrency: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
3853
|
+
* originAmount: "10000000",
|
|
3854
|
+
* destinationChainId: 8453,
|
|
3855
|
+
* destinationAsset: "WETH",
|
|
3856
|
+
* });
|
|
3857
|
+
* ```
|
|
3858
|
+
*/
|
|
3859
|
+
async sendSwapDeposit(params) {
|
|
3860
|
+
try {
|
|
3861
|
+
this.assertSwapDepositParams(params);
|
|
3862
|
+
await this.authenticateUser();
|
|
3863
|
+
await this.prepareFirstDeposit(params.strategy);
|
|
3864
|
+
let orderId;
|
|
3865
|
+
let destinationCurrency;
|
|
3866
|
+
let storedMinimum;
|
|
3867
|
+
if (params.orderId) {
|
|
3868
|
+
const existing = await this.getSwapDeposit(params.orderId);
|
|
3869
|
+
assertStoredFundingOrderRoute(existing, params);
|
|
3870
|
+
orderId = existing.id;
|
|
3871
|
+
destinationCurrency = existing.destinationCurrency;
|
|
3872
|
+
storedMinimum = existing.minimumOutput;
|
|
3873
|
+
} else {
|
|
3874
|
+
const created = await this.quoteSwapDeposit(params);
|
|
3875
|
+
orderId = created.orderId;
|
|
3876
|
+
destinationCurrency = created.destinationCurrency;
|
|
3877
|
+
storedMinimum = created.minimumOutput;
|
|
3878
|
+
}
|
|
3879
|
+
const signQuote = await this.httpClient.post(ENDPOINTS.FUNDING_ORDER_QUOTE(orderId));
|
|
3880
|
+
if (!signQuote.quote) {
|
|
3881
|
+
throw new Error("Relay funding quote is missing from the server response");
|
|
3882
|
+
}
|
|
3883
|
+
const minimumOutput = resolveCollateralMinimum(
|
|
3884
|
+
storedMinimum,
|
|
3885
|
+
signQuote.quote
|
|
3886
|
+
);
|
|
3887
|
+
await this.assertCollateralMinimum(
|
|
3888
|
+
params.userAddress,
|
|
3889
|
+
params.destinationChainId,
|
|
3890
|
+
params.destinationAsset,
|
|
3891
|
+
BigInt(minimumOutput)
|
|
3892
|
+
);
|
|
3893
|
+
ensureRelaySdkClient();
|
|
3894
|
+
const walletClient = await this.walletClientForOrigin(params.originChainId);
|
|
3895
|
+
const { data: execution } = await (0, import_relay_sdk2.execute)({
|
|
3896
|
+
quote: signQuote.quote,
|
|
3897
|
+
wallet: walletClient
|
|
3898
|
+
});
|
|
3899
|
+
const sourceTxHashes = collectRelaySourceTxHashes(
|
|
3900
|
+
execution,
|
|
3901
|
+
params.originChainId
|
|
3902
|
+
);
|
|
3903
|
+
const destinationFromExecution = collectRelayDestinationTxHashes(
|
|
3904
|
+
execution,
|
|
3905
|
+
params.destinationChainId
|
|
3906
|
+
);
|
|
3907
|
+
await this.httpClient.post(ENDPOINTS.FUNDING_ORDER_EXECUTION(orderId), {
|
|
3908
|
+
sourceTxHashes: sourceTxHashes.length ? sourceTxHashes : void 0,
|
|
3909
|
+
stepStatus: "submitted"
|
|
3910
|
+
});
|
|
3911
|
+
await this.hydrateSessionWallet();
|
|
3912
|
+
const safeAddress = await this.getSafeAddressFor(
|
|
3913
|
+
params.userAddress,
|
|
3914
|
+
params.destinationChainId
|
|
3915
|
+
);
|
|
3916
|
+
if (!safeAddress) {
|
|
3917
|
+
throw new Error("Smart wallet address is not available");
|
|
3918
|
+
}
|
|
3919
|
+
const filled = await this.waitForRelayCreditTx({
|
|
3920
|
+
orderId,
|
|
3921
|
+
destinationChainId: params.destinationChainId,
|
|
3922
|
+
destinationToken: destinationCurrency,
|
|
3923
|
+
recipientSafe: safeAddress,
|
|
3924
|
+
initialCandidates: destinationFromExecution
|
|
3925
|
+
});
|
|
3926
|
+
const creditAmount = filled.amount.toString();
|
|
3927
|
+
const registration = await this.registerDeposit(
|
|
3928
|
+
params.destinationChainId,
|
|
3929
|
+
filled.hash,
|
|
3930
|
+
creditAmount,
|
|
3931
|
+
destinationCurrency
|
|
3932
|
+
);
|
|
3933
|
+
return {
|
|
3934
|
+
success: true,
|
|
3935
|
+
txHash: filled.hash,
|
|
3936
|
+
smartWallet: safeAddress,
|
|
3937
|
+
amount: creditAmount,
|
|
3938
|
+
route: "swap",
|
|
3939
|
+
orderId,
|
|
3940
|
+
registration
|
|
3941
|
+
};
|
|
3942
|
+
} catch (error) {
|
|
3943
|
+
const message = thrownMessage(error);
|
|
3944
|
+
throw new Error(
|
|
3945
|
+
message.startsWith("Deposit failed:") ? message : `Deposit failed: ${message}`
|
|
3946
|
+
);
|
|
3947
|
+
}
|
|
3948
|
+
}
|
|
3949
|
+
async sendRoutedDeposit(params) {
|
|
3950
|
+
try {
|
|
3951
|
+
if (!params.userAddress) throw new Error("User address is required");
|
|
3952
|
+
if (!isSupportedChain(params.chainId)) {
|
|
3953
|
+
throw new Error(`Unsupported chain ID: ${params.chainId}`);
|
|
3954
|
+
}
|
|
3955
|
+
if (!params.amount || BigInt(params.amount) <= 0n) {
|
|
3956
|
+
throw new Error("Valid amount is required");
|
|
3957
|
+
}
|
|
3958
|
+
const route = resolveDepositRoute(params);
|
|
3959
|
+
if (route.kind === "direct") {
|
|
3960
|
+
return this.sendDirectDeposit(
|
|
3961
|
+
params.userAddress,
|
|
3962
|
+
params.chainId,
|
|
3963
|
+
params.amount,
|
|
3964
|
+
route.asset,
|
|
3965
|
+
params.strategy
|
|
3966
|
+
);
|
|
3967
|
+
}
|
|
3968
|
+
if (route.kind === "wrap") {
|
|
3969
|
+
await this.assertCollateralMinimum(
|
|
3970
|
+
params.userAddress,
|
|
3971
|
+
params.chainId,
|
|
3972
|
+
"WETH",
|
|
3973
|
+
BigInt(params.amount)
|
|
3974
|
+
);
|
|
3975
|
+
await this.wrapNativeEth(params.chainId, params.amount);
|
|
3976
|
+
const sent = await this.sendDirectDeposit(
|
|
3977
|
+
params.userAddress,
|
|
3978
|
+
params.chainId,
|
|
3979
|
+
params.amount,
|
|
3980
|
+
"WETH",
|
|
3981
|
+
params.strategy
|
|
3982
|
+
);
|
|
3983
|
+
return { ...sent, route: "wrap" };
|
|
3984
|
+
}
|
|
3985
|
+
return this.sendSwapDeposit({
|
|
3986
|
+
userAddress: params.userAddress,
|
|
3987
|
+
originChainId: route.originChainId,
|
|
3988
|
+
originCurrency: route.originCurrency,
|
|
3989
|
+
originAmount: params.amount,
|
|
3990
|
+
destinationChainId: params.chainId,
|
|
3991
|
+
destinationAsset: route.destinationAsset,
|
|
3992
|
+
strategy: params.strategy,
|
|
3993
|
+
slippageBps: params.slippageBps
|
|
3994
|
+
});
|
|
3995
|
+
} catch (error) {
|
|
3996
|
+
const message = thrownMessage(error);
|
|
3997
|
+
throw new Error(
|
|
3998
|
+
message.startsWith("Deposit failed:") ? message : `Deposit failed: ${message}`
|
|
3999
|
+
);
|
|
4000
|
+
}
|
|
4001
|
+
}
|
|
4002
|
+
/** Portfolio floor used by a direct deposit, applied before a wrap or swap spends. */
|
|
4003
|
+
async assertCollateralMinimum(userAddress, chainId, asset, amount) {
|
|
4004
|
+
const symbol = Object.keys(ASSET_CONFIGS).find(
|
|
4005
|
+
(key) => key.toLowerCase() === asset.toLowerCase()
|
|
4006
|
+
);
|
|
4007
|
+
if (!symbol || !getAssetChainIds(symbol).includes(chainId)) return;
|
|
4008
|
+
await this.hydrateSessionWallet();
|
|
4009
|
+
const safeAddress = await this.getSafeAddressFor(userAddress, chainId);
|
|
4010
|
+
if (!safeAddress) {
|
|
4011
|
+
throw new Error("Smart wallet address is not available");
|
|
4012
|
+
}
|
|
4013
|
+
await this.enforceMinimumPortfolioBalance(
|
|
4014
|
+
chainId,
|
|
4015
|
+
symbol,
|
|
4016
|
+
amount,
|
|
4017
|
+
safeAddress,
|
|
4018
|
+
userAddress
|
|
4019
|
+
);
|
|
4020
|
+
}
|
|
4021
|
+
assertSwapDepositParams(params) {
|
|
4022
|
+
if (!params.userAddress) throw new Error("User address is required");
|
|
4023
|
+
if (!params.originAmount || BigInt(params.originAmount) <= 0n) {
|
|
4024
|
+
throw new Error("Valid origin amount is required");
|
|
4025
|
+
}
|
|
4026
|
+
if (!Number.isInteger(params.originChainId) || params.originChainId <= 0) {
|
|
4027
|
+
throw new Error("Valid origin chain is required");
|
|
4028
|
+
}
|
|
4029
|
+
if (!isSupportedChain(params.destinationChainId)) {
|
|
4030
|
+
throw new Error(`Unsupported chain ID: ${params.destinationChainId}`);
|
|
4031
|
+
}
|
|
4032
|
+
if (!params.originCurrency) throw new Error("Origin token is required");
|
|
4033
|
+
if (!params.destinationAsset) throw new Error("Destination asset is required");
|
|
4034
|
+
if (params.slippageBps != null) {
|
|
4035
|
+
if (!Number.isInteger(params.slippageBps) || params.slippageBps < 1 || params.slippageBps > MAX_RELAY_SLIPPAGE_BPS) {
|
|
4036
|
+
throw new Error(
|
|
4037
|
+
`slippageBps must be an integer from 1 to ${MAX_RELAY_SLIPPAGE_BPS}. Received ${params.slippageBps}.`
|
|
4038
|
+
);
|
|
4039
|
+
}
|
|
4040
|
+
}
|
|
4041
|
+
if (params.destinationAsset.toUpperCase() === "WETH" && params.originChainId === params.destinationChainId && isNativeSpendToken(params.originCurrency)) {
|
|
4042
|
+
throw new Error(
|
|
4043
|
+
'Same-chain ETH to WETH is a wrap, not a Relay swap. Call sendDeposit with spendToken "ETH".'
|
|
4044
|
+
);
|
|
4045
|
+
}
|
|
4046
|
+
}
|
|
4047
|
+
async walletClientForOrigin(originChainId) {
|
|
4048
|
+
if (this.signer) {
|
|
4049
|
+
if (!isSupportedChain(originChainId)) {
|
|
4050
|
+
throw new Error(
|
|
4051
|
+
`Cannot sign a swap from chain ${originChainId} with a private key. Connect an EIP-1193 wallet, or spend from Ethereum, Base, or Arbitrum.`
|
|
4052
|
+
);
|
|
4053
|
+
}
|
|
4054
|
+
return this.getWalletClient(originChainId);
|
|
4055
|
+
}
|
|
4056
|
+
if (!this.walletClient?.account) {
|
|
4057
|
+
throw new Error("No account connected. Call connectAccount() first");
|
|
4058
|
+
}
|
|
4059
|
+
if (!this.currentProvider?.request) {
|
|
4060
|
+
if (this.walletClient.chain?.id === originChainId) return this.walletClient;
|
|
4061
|
+
throw new Error(
|
|
4062
|
+
`Wallet is on chain ${this.walletClient.chain?.id ?? "unknown"}. Switch it to chain ${originChainId} before this swap.`
|
|
4063
|
+
);
|
|
4064
|
+
}
|
|
4065
|
+
await this.currentProvider.request({
|
|
4066
|
+
method: "wallet_switchEthereumChain",
|
|
4067
|
+
params: [{ chainId: `0x${originChainId.toString(16)}` }]
|
|
4068
|
+
});
|
|
4069
|
+
const chain = isSupportedChain(originChainId) ? getChainConfig(originChainId, this.rpcUrls).chain : (0, import_viem4.defineChain)({
|
|
4070
|
+
id: originChainId,
|
|
4071
|
+
name: `Chain ${originChainId}`,
|
|
4072
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
4073
|
+
rpcUrls: { default: { http: ["http://127.0.0.1"] } }
|
|
4074
|
+
});
|
|
4075
|
+
this.walletClient = (0, import_viem4.createWalletClient)({
|
|
4076
|
+
account: this.walletClient.account,
|
|
4077
|
+
chain,
|
|
4078
|
+
transport: (0, import_viem4.custom)(this.currentProvider)
|
|
4079
|
+
});
|
|
4080
|
+
if (isSupportedChain(originChainId)) {
|
|
4081
|
+
this.currentChainId = originChainId;
|
|
4082
|
+
}
|
|
4083
|
+
return this.walletClient;
|
|
4084
|
+
}
|
|
4085
|
+
async wrapNativeEth(chainId, amount) {
|
|
4086
|
+
const token = getDefaultTokenAddress(chainId, "WETH");
|
|
4087
|
+
const walletClient = this.getWalletClient(chainId);
|
|
4088
|
+
const chainConfig = getChainConfig(chainId, this.rpcUrls);
|
|
4089
|
+
const hash = await walletClient.writeContract({
|
|
4090
|
+
address: token,
|
|
4091
|
+
abi: WETH_DEPOSIT_ABI,
|
|
4092
|
+
functionName: "deposit",
|
|
4093
|
+
value: BigInt(amount),
|
|
4094
|
+
chain: chainConfig.chain,
|
|
4095
|
+
account: walletClient.account
|
|
4096
|
+
});
|
|
4097
|
+
const receipt = await chainConfig.publicClient.waitForTransactionReceipt({
|
|
4098
|
+
hash
|
|
4099
|
+
});
|
|
4100
|
+
if (receipt.status !== "success") {
|
|
4101
|
+
throw new Error("ETH wrap failed");
|
|
4102
|
+
}
|
|
4103
|
+
}
|
|
4104
|
+
async waitForRelayCreditTx(params) {
|
|
4105
|
+
const chainConfig = getChainConfig(params.destinationChainId, this.rpcUrls);
|
|
4106
|
+
const receipts = /* @__PURE__ */ new Map();
|
|
4107
|
+
const maxAttempts = 120;
|
|
4108
|
+
const intervalMs = 5e3;
|
|
4109
|
+
const loadReceipt = async (hash) => {
|
|
4110
|
+
const key = hash.toLowerCase();
|
|
4111
|
+
if (receipts.has(key)) return;
|
|
4112
|
+
try {
|
|
4113
|
+
const receipt = await chainConfig.publicClient.getTransactionReceipt({
|
|
4114
|
+
hash
|
|
4115
|
+
});
|
|
4116
|
+
receipts.set(key, {
|
|
4117
|
+
status: receipt.status,
|
|
4118
|
+
logs: receipt.logs.map((log) => ({
|
|
4119
|
+
address: log.address,
|
|
4120
|
+
topics: log.topics,
|
|
4121
|
+
data: log.data
|
|
4122
|
+
}))
|
|
4123
|
+
});
|
|
4124
|
+
} catch {
|
|
4125
|
+
}
|
|
4126
|
+
};
|
|
4127
|
+
const pick = async (candidates) => {
|
|
4128
|
+
for (const hash2 of candidates) {
|
|
4129
|
+
if (hash2) await loadReceipt(hash2);
|
|
4130
|
+
}
|
|
4131
|
+
const hash = selectRelayCreditTxHash({
|
|
4132
|
+
candidates,
|
|
4133
|
+
receipts,
|
|
4134
|
+
token: params.destinationToken,
|
|
4135
|
+
recipient: params.recipientSafe
|
|
4136
|
+
});
|
|
4137
|
+
if (!hash) return null;
|
|
4138
|
+
const receipt = receipts.get(hash.toLowerCase());
|
|
4139
|
+
return {
|
|
4140
|
+
hash,
|
|
4141
|
+
amount: receipt ? transferAmountTo(
|
|
4142
|
+
receipt.logs,
|
|
4143
|
+
params.destinationToken,
|
|
4144
|
+
params.recipientSafe
|
|
4145
|
+
) : 0n
|
|
4146
|
+
};
|
|
4147
|
+
};
|
|
4148
|
+
const immediate = await pick(params.initialCandidates);
|
|
4149
|
+
if (immediate) return immediate;
|
|
4150
|
+
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
4151
|
+
const order = await this.getSwapDeposit(params.orderId);
|
|
4152
|
+
if (order.status === "refunded" || order.status === "failed") {
|
|
4153
|
+
throw new Error(`Relay funding ${order.status}`);
|
|
4154
|
+
}
|
|
4155
|
+
const match = await pick([
|
|
4156
|
+
...params.initialCandidates,
|
|
4157
|
+
...order.destinationTxHashes ?? []
|
|
4158
|
+
]);
|
|
4159
|
+
if (match) return match;
|
|
4160
|
+
await sleep(intervalMs);
|
|
4161
|
+
}
|
|
4162
|
+
throw new Error("Relay funding is still converting");
|
|
4163
|
+
}
|
|
3424
4164
|
/**
|
|
3425
4165
|
* @deprecated Compatibility convenience method. Prefer {@link ZyfaiSDK.sendDeposit} followed by
|
|
3426
4166
|
* {@link ZyfaiSDK.waitForDepositCredit} when the caller owns the completion UX.
|
|
@@ -3519,6 +4259,57 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3519
4259
|
value: "0"
|
|
3520
4260
|
};
|
|
3521
4261
|
}
|
|
4262
|
+
/**
|
|
4263
|
+
* Prepare a gas-sponsored rZFI claim into the user's Safe on Base.
|
|
4264
|
+
*
|
|
4265
|
+
* @remarks
|
|
4266
|
+
* The SDK never signs. Sign `userOpHashToSign` with the owner EOA as a
|
|
4267
|
+
* personal_sign of the raw 32-byte hash (viem:
|
|
4268
|
+
* `signMessage({ message: { raw: userOpHashToSign } })`), then pass the
|
|
4269
|
+
* signature to {@link ZyfaiSDK.submitRzfiClaim} within 5 minutes.
|
|
4270
|
+
* Requires an authenticated user session.
|
|
4271
|
+
*
|
|
4272
|
+
* @group Rewards
|
|
4273
|
+
*
|
|
4274
|
+
* @param userAddress - Owner EOA
|
|
4275
|
+
* @returns `status: "pending"` with the hash to sign and the amount
|
|
4276
|
+
*
|
|
4277
|
+
* @throws If there is no rZFI to claim, or the reward tree is updating (retry later)
|
|
4278
|
+
*/
|
|
4279
|
+
async prepareRzfiClaim(userAddress) {
|
|
4280
|
+
this.assertRzfiClaimUser(userAddress);
|
|
4281
|
+
await this.authenticateUser();
|
|
4282
|
+
return this.httpClient.post(ENDPOINTS.RZFI_CLAIM, {});
|
|
4283
|
+
}
|
|
4284
|
+
/**
|
|
4285
|
+
* Submit the owner-signed rZFI claim prepared by {@link ZyfaiSDK.prepareRzfiClaim}.
|
|
4286
|
+
*
|
|
4287
|
+
* @group Rewards
|
|
4288
|
+
*
|
|
4289
|
+
* @param userAddress - Owner EOA
|
|
4290
|
+
* @param userOpSignature - Owner EOA signature over `userOpHashToSign`
|
|
4291
|
+
* @returns `status: "claimed"` with the on-chain `txHash`
|
|
4292
|
+
*
|
|
4293
|
+
* @throws If the signature is not the owner's, the claim expired, or it reverted
|
|
4294
|
+
*/
|
|
4295
|
+
async submitRzfiClaim(userAddress, userOpSignature) {
|
|
4296
|
+
this.assertRzfiClaimUser(userAddress);
|
|
4297
|
+
if (!userOpSignature?.startsWith("0x")) {
|
|
4298
|
+
throw new Error("A 0x-prefixed userOpSignature is required");
|
|
4299
|
+
}
|
|
4300
|
+
await this.authenticateUser();
|
|
4301
|
+
return this.httpClient.post(ENDPOINTS.RZFI_CLAIM, {
|
|
4302
|
+
userOpSignature
|
|
4303
|
+
});
|
|
4304
|
+
}
|
|
4305
|
+
assertRzfiClaimUser(userAddress) {
|
|
4306
|
+
if (!userAddress) {
|
|
4307
|
+
throw new Error("A user address is required");
|
|
4308
|
+
}
|
|
4309
|
+
if (this.sessionEoa && (0, import_viem4.getAddress)(userAddress) !== this.sessionEoa) {
|
|
4310
|
+
throw new Error("userAddress does not match authenticated session EOA");
|
|
4311
|
+
}
|
|
4312
|
+
}
|
|
3522
4313
|
/** Apply the same per-asset minimum portfolio rule as sendDeposit. */
|
|
3523
4314
|
async enforceMinimumPortfolioBalance(chainId, asset, amount, safeAddress, userAddress) {
|
|
3524
4315
|
if (this.bypassMinPortfolio || isMainnetMinPortfolioExempt(chainId, userAddress, safeAddress)) {
|
|
@@ -3586,7 +4377,9 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3586
4377
|
}
|
|
3587
4378
|
await this.authenticateUser();
|
|
3588
4379
|
const usdcDetails = await this.getUserDetails("USDC");
|
|
3589
|
-
|
|
4380
|
+
const usdcChainsConfigured = (usdcDetails.chains?.length ?? 0) > 0;
|
|
4381
|
+
const usdcProtocolsConfigured = (usdcDetails.protocols?.length ?? 0) > 0;
|
|
4382
|
+
if (usdcChainsConfigured && usdcProtocolsConfigured) {
|
|
3590
4383
|
return { applied: false };
|
|
3591
4384
|
}
|
|
3592
4385
|
await this.updateUserProtocols(strategy, { strict: true });
|
|
@@ -3596,6 +4389,11 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3596
4389
|
"First-deposit setup did not persist USDC chain configuration."
|
|
3597
4390
|
);
|
|
3598
4391
|
}
|
|
4392
|
+
if ((usdcAfterSetup.protocols?.length ?? 0) === 0) {
|
|
4393
|
+
throw new Error(
|
|
4394
|
+
"First-deposit setup did not persist USDC protocol configuration."
|
|
4395
|
+
);
|
|
4396
|
+
}
|
|
3599
4397
|
return { applied: true };
|
|
3600
4398
|
}
|
|
3601
4399
|
/**
|
|
@@ -3612,7 +4410,9 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3612
4410
|
}
|
|
3613
4411
|
await this.authenticateUser();
|
|
3614
4412
|
const details = await this.getUserDetails(asset);
|
|
3615
|
-
|
|
4413
|
+
const chainConfigured = details.chains?.includes(chainId) ?? false;
|
|
4414
|
+
const protocolsConfigured = (details.protocols?.length ?? 0) > 0;
|
|
4415
|
+
if (chainConfigured && protocolsConfigured) {
|
|
3616
4416
|
return { applied: false };
|
|
3617
4417
|
}
|
|
3618
4418
|
const allProtocols = await this.httpClient.get(
|
|
@@ -3630,8 +4430,20 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3630
4430
|
`First-deposit setup did not persist ${asset} chain ${chainId} configuration.`
|
|
3631
4431
|
);
|
|
3632
4432
|
}
|
|
4433
|
+
if ((afterSetup.protocols?.length ?? 0) === 0) {
|
|
4434
|
+
throw new Error(
|
|
4435
|
+
`First-deposit setup did not persist ${asset} protocol configuration.`
|
|
4436
|
+
);
|
|
4437
|
+
}
|
|
3633
4438
|
return { applied: true };
|
|
3634
4439
|
}
|
|
4440
|
+
/**
|
|
4441
|
+
* Persist the deposit asset on one chain before a signing page runs the transfer or swap.
|
|
4442
|
+
* Does not build calldata and does not check the portfolio minimum.
|
|
4443
|
+
*/
|
|
4444
|
+
async ensureDepositAssetOnChain(params) {
|
|
4445
|
+
return this.ensureDepositProfileForPrepare(params);
|
|
4446
|
+
}
|
|
3635
4447
|
/**
|
|
3636
4448
|
* Prepare a deposit: persist first-deposit profile when needed and return
|
|
3637
4449
|
* ERC-20 transfer calldata for the user to sign (does not broadcast).
|
|
@@ -4363,6 +5175,51 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4363
5175
|
// ============================================================================
|
|
4364
5176
|
// TVL & Volume Methods
|
|
4365
5177
|
// ============================================================================
|
|
5178
|
+
/**
|
|
5179
|
+
* Get Zyfai's Merkl incentive campaigns on Base with their current APR.
|
|
5180
|
+
*
|
|
5181
|
+
* @group Analytics and opportunities
|
|
5182
|
+
*
|
|
5183
|
+
* @remarks Data API. Partner API key only; SIWE not required.
|
|
5184
|
+
*
|
|
5185
|
+
* @returns One entry per campaign slot (rZFI, new TVL, season campaign)
|
|
5186
|
+
*
|
|
5187
|
+
* @example
|
|
5188
|
+
* ```typescript
|
|
5189
|
+
* const campaigns = await sdk.getBaseMerklCampaigns();
|
|
5190
|
+
* for (const c of campaigns.filter((c) => c.isActive)) {
|
|
5191
|
+
* console.log(c.name, `${(c.apr * 100).toFixed(2)}%`);
|
|
5192
|
+
* }
|
|
5193
|
+
* ```
|
|
5194
|
+
*/
|
|
5195
|
+
async getBaseMerklCampaigns() {
|
|
5196
|
+
const ids = ["base/rzfi", "base-new-tvl", "base"];
|
|
5197
|
+
try {
|
|
5198
|
+
const now = Date.now();
|
|
5199
|
+
return await Promise.all(
|
|
5200
|
+
ids.map(async (id) => {
|
|
5201
|
+
const { campaign, currentStats } = await this.httpClient.dataGet(DATA_ENDPOINTS.MERKL_CAMPAIGN_INFO(id));
|
|
5202
|
+
return {
|
|
5203
|
+
id,
|
|
5204
|
+
name: campaign.name,
|
|
5205
|
+
apr: Number(currentStats.currentAPR),
|
|
5206
|
+
tvlUsd: currentStats.totalTVL,
|
|
5207
|
+
activeWallets: currentStats.activeWallets,
|
|
5208
|
+
rewardToken: campaign.rewardToken,
|
|
5209
|
+
totalRewards: campaign.totalRewards,
|
|
5210
|
+
dailyRewardAmount: campaign.dailyRewardAmount,
|
|
5211
|
+
startDate: campaign.startDate,
|
|
5212
|
+
endDate: campaign.endDate,
|
|
5213
|
+
isActive: now >= Date.parse(campaign.startDate) && now < Date.parse(campaign.endDate)
|
|
5214
|
+
};
|
|
5215
|
+
})
|
|
5216
|
+
);
|
|
5217
|
+
} catch (error) {
|
|
5218
|
+
throw new Error(
|
|
5219
|
+
`Failed to get Base Merkl campaigns: ${error.message}`
|
|
5220
|
+
);
|
|
5221
|
+
}
|
|
5222
|
+
}
|
|
4366
5223
|
/**
|
|
4367
5224
|
* Get total value locked (TVL) across all Zyfai accounts.
|
|
4368
5225
|
*
|
|
@@ -6254,6 +7111,7 @@ function createBankrProvider(config) {
|
|
|
6254
7111
|
API_ENDPOINT,
|
|
6255
7112
|
DATA_API_BASE_URLS,
|
|
6256
7113
|
DATA_API_ENDPOINT,
|
|
7114
|
+
DEFAULT_RELAY_SLIPPAGE_BPS,
|
|
6257
7115
|
DEFAULT_TOKEN_ADDRESSES,
|
|
6258
7116
|
ERC7579_LAUNCHPAD_ADDRESS,
|
|
6259
7117
|
EXECUTION_API_BASE_URLS,
|
|
@@ -6262,9 +7120,11 @@ function createBankrProvider(config) {
|
|
|
6262
7120
|
LOCAL_DATA_API_BASE_URL,
|
|
6263
7121
|
LOCAL_EXECUTION_API_BASE_URL,
|
|
6264
7122
|
LOCAL_WS_URL,
|
|
7123
|
+
MAX_RELAY_SLIPPAGE_BPS,
|
|
6265
7124
|
OWNABLE_VALIDATOR,
|
|
6266
7125
|
POOL_MODULE_ADDRESSES,
|
|
6267
7126
|
PROXY_EXECUTOR,
|
|
7127
|
+
RELAY_SLIPPAGE_PRESETS_BPS,
|
|
6268
7128
|
SAFE_7579_ADDRESS,
|
|
6269
7129
|
SMART_SESSIONS_FALLBACK,
|
|
6270
7130
|
SMART_SESSIONS_VALIDATOR,
|
|
@@ -6283,5 +7143,7 @@ function createBankrProvider(config) {
|
|
|
6283
7143
|
getManagedAssets,
|
|
6284
7144
|
getSupportedChainIds,
|
|
6285
7145
|
getWsUrl,
|
|
6286
|
-
isSupportedChain
|
|
7146
|
+
isSupportedChain,
|
|
7147
|
+
resolveDepositRoute,
|
|
7148
|
+
resolveRelaySlippageBps
|
|
6287
7149
|
});
|