@zyfai/sdk 0.2.46 → 0.2.47
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 +0 -36
- package/dist/index.d.mts +36 -33
- package/dist/index.d.ts +36 -33
- package/dist/index.js +71 -350
- package/dist/index.mjs +71 -350
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -70,6 +70,9 @@ var ENDPOINTS = {
|
|
|
70
70
|
LOG_DEPOSIT: "/users/log_deposit",
|
|
71
71
|
// Safe Deployment (single endpoint)
|
|
72
72
|
SAFE_DEPLOY: "/users/safe-deploy",
|
|
73
|
+
// Multichain deploy for predeployed (pool) wallets — deploys + rotates the
|
|
74
|
+
// same address on the requested chains (sponsored, no user signature).
|
|
75
|
+
DEPLOY_CHAINS: "/users/deploy-chains",
|
|
73
76
|
// Session Keys
|
|
74
77
|
SESSION_KEYS_CONFIG: "/session-keys/config",
|
|
75
78
|
SESSION_KEYS_ADD: "/session-keys/add",
|
|
@@ -512,7 +515,6 @@ var VAULT_ABI = [
|
|
|
512
515
|
var VAULT_ADDRESS = "0xD580071c47d4a667858B5FafAb85BC9C609beC5D";
|
|
513
516
|
|
|
514
517
|
// src/config/constants.ts
|
|
515
|
-
var ZYFI_FEE_RATE = 0.1;
|
|
516
518
|
var MIN_PORTFOLIO_BALANCE = {
|
|
517
519
|
1: {
|
|
518
520
|
USDC: 100n * 10n ** 6n,
|
|
@@ -1045,310 +1047,6 @@ var hasMatchingPool = (data, chains, asset) => {
|
|
|
1045
1047
|
return false;
|
|
1046
1048
|
};
|
|
1047
1049
|
|
|
1048
|
-
// src/utils/zyfi-fees.ts
|
|
1049
|
-
var CHAIN_NAME_TO_ID = {
|
|
1050
|
-
ethereum: "1",
|
|
1051
|
-
"ethereum mainnet": "1",
|
|
1052
|
-
mainnet: "1",
|
|
1053
|
-
base: "8453",
|
|
1054
|
-
arbitrum: "42161",
|
|
1055
|
-
"arbitrum one": "42161"
|
|
1056
|
-
};
|
|
1057
|
-
function symbolToAssetType(symbol) {
|
|
1058
|
-
const upper = symbol.trim().toUpperCase();
|
|
1059
|
-
if (upper === "USDC") return "usdc";
|
|
1060
|
-
if (upper === "WETH" || upper === "ETH") return "eth";
|
|
1061
|
-
if (upper === "EURC") return "eurc";
|
|
1062
|
-
if (upper === "CBBTC") return "cbbtc";
|
|
1063
|
-
if (upper === "CBETH") return "cbeth";
|
|
1064
|
-
return symbol.trim().toLowerCase();
|
|
1065
|
-
}
|
|
1066
|
-
function assetTypeToSymbol(assetType) {
|
|
1067
|
-
const lower = assetType.trim().toLowerCase();
|
|
1068
|
-
if (lower === "usdc") return "USDC";
|
|
1069
|
-
if (lower === "eth") return "WETH";
|
|
1070
|
-
if (lower === "eurc") return "EURC";
|
|
1071
|
-
if (lower === "cbbtc") return "CBBTC";
|
|
1072
|
-
if (lower === "cbeth") return "CBETH";
|
|
1073
|
-
return assetType.trim().toUpperCase();
|
|
1074
|
-
}
|
|
1075
|
-
function chainNameToId(chain) {
|
|
1076
|
-
if (!chain) return void 0;
|
|
1077
|
-
const trimmed = chain.trim();
|
|
1078
|
-
if (/^\d+$/.test(trimmed)) return trimmed;
|
|
1079
|
-
return CHAIN_NAME_TO_ID[trimmed.toLowerCase()];
|
|
1080
|
-
}
|
|
1081
|
-
function parseHumanAmount(value) {
|
|
1082
|
-
if (typeof value === "number") {
|
|
1083
|
-
return Number.isFinite(value) ? value : 0;
|
|
1084
|
-
}
|
|
1085
|
-
const parsed = Number.parseFloat(String(value ?? "0"));
|
|
1086
|
-
return Number.isFinite(parsed) ? parsed : 0;
|
|
1087
|
-
}
|
|
1088
|
-
function humanAmountToRaw(human, decimals) {
|
|
1089
|
-
if (!Number.isFinite(human) || human <= 0 || decimals < 0) {
|
|
1090
|
-
return 0n;
|
|
1091
|
-
}
|
|
1092
|
-
const fixed = human.toFixed(decimals);
|
|
1093
|
-
const [whole, frac = ""] = fixed.split(".");
|
|
1094
|
-
const wholePart = BigInt(whole);
|
|
1095
|
-
const fracPart = BigInt(frac.padEnd(decimals, "0").slice(0, decimals) || "0");
|
|
1096
|
-
return wholePart * 10n ** BigInt(decimals) + fracPart;
|
|
1097
|
-
}
|
|
1098
|
-
function formatHumanAmount(value) {
|
|
1099
|
-
if (!Number.isFinite(value) || value === 0) return "0";
|
|
1100
|
-
const fixed = value.toFixed(18);
|
|
1101
|
-
return fixed.replace(/\.?0+$/, "") || "0";
|
|
1102
|
-
}
|
|
1103
|
-
function computePendingFees(currentEarningsByChain) {
|
|
1104
|
-
const byToken = {};
|
|
1105
|
-
const byChain = {};
|
|
1106
|
-
const byChainByToken = {};
|
|
1107
|
-
let total = 0;
|
|
1108
|
-
if (!currentEarningsByChain) {
|
|
1109
|
-
return { byToken, byChain, byChainByToken, total };
|
|
1110
|
-
}
|
|
1111
|
-
Object.entries(currentEarningsByChain).forEach(([chainId, chainEarnings]) => {
|
|
1112
|
-
const feeByToken = {};
|
|
1113
|
-
let chainFee = 0;
|
|
1114
|
-
Object.entries(chainEarnings || {}).forEach(([tokenSymbol, tokenAmount]) => {
|
|
1115
|
-
const numericValue = parseHumanAmount(tokenAmount);
|
|
1116
|
-
const tokenFee = numericValue > 0 ? numericValue * ZYFI_FEE_RATE : 0;
|
|
1117
|
-
feeByToken[tokenSymbol] = tokenFee;
|
|
1118
|
-
chainFee += tokenFee;
|
|
1119
|
-
byToken[tokenSymbol] = (byToken[tokenSymbol] ?? 0) + tokenFee;
|
|
1120
|
-
});
|
|
1121
|
-
byChainByToken[chainId] = feeByToken;
|
|
1122
|
-
byChain[chainId] = chainFee;
|
|
1123
|
-
total += chainFee;
|
|
1124
|
-
});
|
|
1125
|
-
return { byToken, byChain, byChainByToken, total };
|
|
1126
|
-
}
|
|
1127
|
-
function feeForAssetType(feesByToken, assetType) {
|
|
1128
|
-
const symbol = assetTypeToSymbol(assetType);
|
|
1129
|
-
if (feesByToken[symbol] !== void 0) return feesByToken[symbol] ?? 0;
|
|
1130
|
-
if (assetType.toLowerCase() === "eth") {
|
|
1131
|
-
return feesByToken["ETH"] ?? feesByToken["WETH"] ?? 0;
|
|
1132
|
-
}
|
|
1133
|
-
return feesByToken[assetType.toUpperCase()] ?? 0;
|
|
1134
|
-
}
|
|
1135
|
-
function feeForChainAsset(byChainByToken, chainId, assetType) {
|
|
1136
|
-
const chainFees = byChainByToken[chainId] || {};
|
|
1137
|
-
return feeForAssetType(chainFees, assetType);
|
|
1138
|
-
}
|
|
1139
|
-
function applyFeeToHexBalance(balanceHex, feeHuman, decimals) {
|
|
1140
|
-
let balance;
|
|
1141
|
-
try {
|
|
1142
|
-
balance = BigInt(balanceHex || "0x0");
|
|
1143
|
-
} catch {
|
|
1144
|
-
balance = 0n;
|
|
1145
|
-
}
|
|
1146
|
-
const feeRaw = humanAmountToRaw(feeHuman, decimals);
|
|
1147
|
-
const net = balance > feeRaw ? balance - feeRaw : 0n;
|
|
1148
|
-
return `0x${net.toString(16)}`;
|
|
1149
|
-
}
|
|
1150
|
-
function applyFeeToDecimalWei(amount, feeHuman, decimals) {
|
|
1151
|
-
let balance;
|
|
1152
|
-
try {
|
|
1153
|
-
balance = BigInt(amount || "0");
|
|
1154
|
-
} catch {
|
|
1155
|
-
balance = 0n;
|
|
1156
|
-
}
|
|
1157
|
-
const feeRaw = humanAmountToRaw(feeHuman, decimals);
|
|
1158
|
-
const net = balance > feeRaw ? balance - feeRaw : 0n;
|
|
1159
|
-
return net.toString();
|
|
1160
|
-
}
|
|
1161
|
-
function enrichAssetBalance(entry, feeHuman) {
|
|
1162
|
-
const balanceWithFee = applyFeeToHexBalance(
|
|
1163
|
-
entry.balance,
|
|
1164
|
-
feeHuman,
|
|
1165
|
-
entry.decimals
|
|
1166
|
-
);
|
|
1167
|
-
return {
|
|
1168
|
-
...entry,
|
|
1169
|
-
balanceWithFee
|
|
1170
|
-
};
|
|
1171
|
-
}
|
|
1172
|
-
function resolvePositionSymbol(position) {
|
|
1173
|
-
if (position.token_symbol) return position.token_symbol;
|
|
1174
|
-
if (position.assetType) return assetTypeToSymbol(position.assetType);
|
|
1175
|
-
return void 0;
|
|
1176
|
-
}
|
|
1177
|
-
function resolvePositionDecimals(position) {
|
|
1178
|
-
if (typeof position.decimals === "number") return position.decimals;
|
|
1179
|
-
const asset = position.assetType || symbolToAssetType(position.token_symbol || "");
|
|
1180
|
-
if (asset === "usdc" || asset === "eurc") return 6;
|
|
1181
|
-
if (asset === "cbbtc") return 8;
|
|
1182
|
-
return 18;
|
|
1183
|
-
}
|
|
1184
|
-
function enrichPositions(positions, byChainByToken) {
|
|
1185
|
-
const groups = /* @__PURE__ */ new Map();
|
|
1186
|
-
positions.forEach((position, index) => {
|
|
1187
|
-
const chainId = chainNameToId(position.chain || "");
|
|
1188
|
-
const symbol = resolvePositionSymbol(position);
|
|
1189
|
-
if (!chainId || !symbol) {
|
|
1190
|
-
return;
|
|
1191
|
-
}
|
|
1192
|
-
const key = `${chainId}:${symbol.toUpperCase()}`;
|
|
1193
|
-
const list = groups.get(key) || [];
|
|
1194
|
-
list.push(index);
|
|
1195
|
-
groups.set(key, list);
|
|
1196
|
-
});
|
|
1197
|
-
const feeShareByIndex = /* @__PURE__ */ new Map();
|
|
1198
|
-
groups.forEach((indices, key) => {
|
|
1199
|
-
const [chainId, symbol] = key.split(":");
|
|
1200
|
-
const chainFees = byChainByToken[chainId] || {};
|
|
1201
|
-
let totalFee = chainFees[symbol] ?? (symbol === "WETH" ? chainFees["ETH"] : void 0) ?? (symbol === "ETH" ? chainFees["WETH"] : void 0) ?? 0;
|
|
1202
|
-
if (totalFee <= 0) {
|
|
1203
|
-
indices.forEach((i) => feeShareByIndex.set(i, 0));
|
|
1204
|
-
return;
|
|
1205
|
-
}
|
|
1206
|
-
let totalUnderlying = 0n;
|
|
1207
|
-
const underlyings = indices.map((i) => {
|
|
1208
|
-
try {
|
|
1209
|
-
return BigInt(positions[i].underlyingAmount || "0");
|
|
1210
|
-
} catch {
|
|
1211
|
-
return 0n;
|
|
1212
|
-
}
|
|
1213
|
-
});
|
|
1214
|
-
underlyings.forEach((u) => {
|
|
1215
|
-
totalUnderlying += u;
|
|
1216
|
-
});
|
|
1217
|
-
if (totalUnderlying === 0n) {
|
|
1218
|
-
const share = totalFee / indices.length;
|
|
1219
|
-
indices.forEach((i) => feeShareByIndex.set(i, share));
|
|
1220
|
-
return;
|
|
1221
|
-
}
|
|
1222
|
-
indices.forEach((i, idx) => {
|
|
1223
|
-
const share = totalFee * Number(underlyings[idx]) / Number(totalUnderlying);
|
|
1224
|
-
feeShareByIndex.set(i, share);
|
|
1225
|
-
});
|
|
1226
|
-
});
|
|
1227
|
-
return positions.map((position, index) => {
|
|
1228
|
-
const underlying = position.underlyingAmount;
|
|
1229
|
-
if (underlying === void 0) {
|
|
1230
|
-
return { ...position };
|
|
1231
|
-
}
|
|
1232
|
-
const feeShare = feeShareByIndex.get(index) ?? 0;
|
|
1233
|
-
const decimals = resolvePositionDecimals(position);
|
|
1234
|
-
return {
|
|
1235
|
-
...position,
|
|
1236
|
-
underlyingAmountWithFee: applyFeeToDecimalWei(
|
|
1237
|
-
underlying,
|
|
1238
|
-
feeShare,
|
|
1239
|
-
decimals
|
|
1240
|
-
)
|
|
1241
|
-
};
|
|
1242
|
-
});
|
|
1243
|
-
}
|
|
1244
|
-
function enrichPortfolioWithFees(portfolio, currentEarningsByChain) {
|
|
1245
|
-
const { byToken, byChainByToken } = computePendingFees(currentEarningsByChain);
|
|
1246
|
-
const portfolioByAssetType = portfolio.portfolioByAssetType ? Object.fromEntries(
|
|
1247
|
-
Object.entries(portfolio.portfolioByAssetType).map(
|
|
1248
|
-
([assetType, entry]) => [
|
|
1249
|
-
assetType,
|
|
1250
|
-
enrichAssetBalance(entry, feeForAssetType(byToken, assetType))
|
|
1251
|
-
]
|
|
1252
|
-
)
|
|
1253
|
-
) : void 0;
|
|
1254
|
-
const portfolioByChain = portfolio.portfolioByChain ? Object.fromEntries(
|
|
1255
|
-
Object.entries(portfolio.portfolioByChain).map(
|
|
1256
|
-
([chainId, assets]) => [
|
|
1257
|
-
chainId,
|
|
1258
|
-
Object.fromEntries(
|
|
1259
|
-
Object.entries(assets).map(([assetType, entry]) => [
|
|
1260
|
-
assetType,
|
|
1261
|
-
enrichAssetBalance(
|
|
1262
|
-
entry,
|
|
1263
|
-
feeForChainAsset(byChainByToken, chainId, assetType)
|
|
1264
|
-
)
|
|
1265
|
-
])
|
|
1266
|
-
)
|
|
1267
|
-
]
|
|
1268
|
-
)
|
|
1269
|
-
) : void 0;
|
|
1270
|
-
const positions = portfolio.positions ? enrichPositions(portfolio.positions, byChainByToken) : void 0;
|
|
1271
|
-
return {
|
|
1272
|
-
...portfolio,
|
|
1273
|
-
...portfolioByAssetType ? { portfolioByAssetType } : {},
|
|
1274
|
-
...portfolioByChain ? { portfolioByChain } : {},
|
|
1275
|
-
...positions ? { positions } : {}
|
|
1276
|
-
};
|
|
1277
|
-
}
|
|
1278
|
-
function sumCurrentByToken(currentByChain) {
|
|
1279
|
-
const totals = {};
|
|
1280
|
-
if (!currentByChain) return totals;
|
|
1281
|
-
Object.values(currentByChain).forEach((chainEarnings) => {
|
|
1282
|
-
Object.entries(chainEarnings || {}).forEach(([symbol, amount]) => {
|
|
1283
|
-
totals[symbol] = (totals[symbol] ?? 0) + parseHumanAmount(amount);
|
|
1284
|
-
});
|
|
1285
|
-
});
|
|
1286
|
-
return totals;
|
|
1287
|
-
}
|
|
1288
|
-
function enrichOnchainEarningsTotals(apiData) {
|
|
1289
|
-
const lifetimeByToken = apiData.lifetime_earnings_by_token || {};
|
|
1290
|
-
const lifetimeByChain = apiData.lifetime_earnings_by_chain || {};
|
|
1291
|
-
const currentByChain = apiData.current_earnings_by_chain || {};
|
|
1292
|
-
const unrealizedByChain = apiData.unrealized_earnings_by_chain || apiData.unrealized_earnings || {};
|
|
1293
|
-
const currentByToken = apiData.current_earnings_by_token && Object.keys(apiData.current_earnings_by_token).length > 0 ? Object.fromEntries(
|
|
1294
|
-
Object.entries(apiData.current_earnings_by_token).map(([k, v]) => [
|
|
1295
|
-
k,
|
|
1296
|
-
parseHumanAmount(v)
|
|
1297
|
-
])
|
|
1298
|
-
) : sumCurrentByToken(currentByChain);
|
|
1299
|
-
const unrealizedByToken = apiData.unrealized_earnings_by_token && Object.keys(apiData.unrealized_earnings_by_token).length > 0 ? Object.fromEntries(
|
|
1300
|
-
Object.entries(apiData.unrealized_earnings_by_token).map(([k, v]) => [
|
|
1301
|
-
k,
|
|
1302
|
-
parseHumanAmount(v)
|
|
1303
|
-
])
|
|
1304
|
-
) : sumCurrentByToken(unrealizedByChain);
|
|
1305
|
-
const keepRate = 1 - ZYFI_FEE_RATE;
|
|
1306
|
-
const tokenKeys = /* @__PURE__ */ new Set([
|
|
1307
|
-
...Object.keys(lifetimeByToken),
|
|
1308
|
-
...Object.keys(unrealizedByToken),
|
|
1309
|
-
...Object.keys(currentByToken)
|
|
1310
|
-
]);
|
|
1311
|
-
const totalEarningsByTokenWithFee = {};
|
|
1312
|
-
tokenKeys.forEach((symbol) => {
|
|
1313
|
-
const lifetime = parseHumanAmount(lifetimeByToken[symbol]);
|
|
1314
|
-
const unrealized = unrealizedByToken[symbol] ?? 0;
|
|
1315
|
-
const current = currentByToken[symbol] ?? 0;
|
|
1316
|
-
totalEarningsByTokenWithFee[symbol] = formatHumanAmount(
|
|
1317
|
-
lifetime + unrealized + current * keepRate
|
|
1318
|
-
);
|
|
1319
|
-
});
|
|
1320
|
-
const chainKeys = /* @__PURE__ */ new Set([
|
|
1321
|
-
...Object.keys(lifetimeByChain),
|
|
1322
|
-
...Object.keys(unrealizedByChain),
|
|
1323
|
-
...Object.keys(currentByChain)
|
|
1324
|
-
]);
|
|
1325
|
-
const totalEarningsByChainWithFee = {};
|
|
1326
|
-
chainKeys.forEach((chainId) => {
|
|
1327
|
-
const lifetimeTokens = lifetimeByChain[chainId] || {};
|
|
1328
|
-
const unrealizedTokens = unrealizedByChain[chainId] || {};
|
|
1329
|
-
const currentTokens = currentByChain[chainId] || {};
|
|
1330
|
-
const symbols = /* @__PURE__ */ new Set([
|
|
1331
|
-
...Object.keys(lifetimeTokens),
|
|
1332
|
-
...Object.keys(unrealizedTokens),
|
|
1333
|
-
...Object.keys(currentTokens)
|
|
1334
|
-
]);
|
|
1335
|
-
const chainMap = {};
|
|
1336
|
-
symbols.forEach((symbol) => {
|
|
1337
|
-
const lifetime = parseHumanAmount(lifetimeTokens[symbol]);
|
|
1338
|
-
const unrealized = parseHumanAmount(unrealizedTokens[symbol]);
|
|
1339
|
-
const current = parseHumanAmount(currentTokens[symbol]);
|
|
1340
|
-
chainMap[symbol] = formatHumanAmount(
|
|
1341
|
-
lifetime + unrealized + current * keepRate
|
|
1342
|
-
);
|
|
1343
|
-
});
|
|
1344
|
-
totalEarningsByChainWithFee[chainId] = chainMap;
|
|
1345
|
-
});
|
|
1346
|
-
return {
|
|
1347
|
-
totalEarningsByTokenWithFee,
|
|
1348
|
-
totalEarningsByChainWithFee
|
|
1349
|
-
};
|
|
1350
|
-
}
|
|
1351
|
-
|
|
1352
1050
|
// src/core/ZyfaiSDK.ts
|
|
1353
1051
|
var import_siwe = require("siwe");
|
|
1354
1052
|
var _ZyfaiSDK = class _ZyfaiSDK {
|
|
@@ -2439,6 +2137,42 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2439
2137
|
* await sdk.depositFunds(userAddress, 8453, "10000000000", "USDC", "aggressive");
|
|
2440
2138
|
* ```
|
|
2441
2139
|
*/
|
|
2140
|
+
/**
|
|
2141
|
+
* Deploy the connected predeployed (pool) wallet on one or more chains.
|
|
2142
|
+
*
|
|
2143
|
+
* A pool wallet is only deployed on the chains it has been onboarded to (see
|
|
2144
|
+
* the `chains` array from getSmartWalletByEOA); every other supported chain is
|
|
2145
|
+
* counterfactual until deployed here. The backend deploys + rotates the same
|
|
2146
|
+
* address on each chain in parallel (sponsored, no user signature) and only
|
|
2147
|
+
* resolves once all requested chains have landed on-chain. Idempotent — chains
|
|
2148
|
+
* already deployed are a no-op.
|
|
2149
|
+
*
|
|
2150
|
+
* `depositFunds` calls this automatically when the target chain isn't deployed,
|
|
2151
|
+
* so most integrations never need it directly; use it to pre-provision chains.
|
|
2152
|
+
*
|
|
2153
|
+
* @param chainIds - Chain IDs to deploy the wallet on (e.g. [1, 42161])
|
|
2154
|
+
*/
|
|
2155
|
+
async deployOnChains(chainIds) {
|
|
2156
|
+
if (!chainIds?.length) {
|
|
2157
|
+
throw new Error("At least one chainId is required");
|
|
2158
|
+
}
|
|
2159
|
+
for (const c of chainIds) {
|
|
2160
|
+
if (!isSupportedChain(c)) throw new Error(`Unsupported chain ID: ${c}`);
|
|
2161
|
+
}
|
|
2162
|
+
await this.authenticateUser();
|
|
2163
|
+
return this.httpClient.post(ENDPOINTS.DEPLOY_CHAINS, { chainIds });
|
|
2164
|
+
}
|
|
2165
|
+
/**
|
|
2166
|
+
* Poll until the Safe has code on-chain. The backend awaits landing before it
|
|
2167
|
+
* responds, so this only guards against read-after-write RPC lag.
|
|
2168
|
+
*/
|
|
2169
|
+
async waitForSafeDeployed(safeAddress, publicClient, tries = 5, delayMs = 2e3) {
|
|
2170
|
+
for (let i = 0; i < tries; i++) {
|
|
2171
|
+
if (await isSafeDeployed(safeAddress, publicClient)) return true;
|
|
2172
|
+
await new Promise((r) => setTimeout(r, delayMs));
|
|
2173
|
+
}
|
|
2174
|
+
return isSafeDeployed(safeAddress, publicClient);
|
|
2175
|
+
}
|
|
2442
2176
|
async depositFunds(userAddress, chainId, amount, asset, strategy) {
|
|
2443
2177
|
try {
|
|
2444
2178
|
if (!userAddress) {
|
|
@@ -2475,9 +2209,22 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2475
2209
|
chainConfig.publicClient
|
|
2476
2210
|
);
|
|
2477
2211
|
if (!isDeployed) {
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2212
|
+
if (this.isPredeployed && this.isConnectedUser(userAddress)) {
|
|
2213
|
+
await this.deployOnChains([chainId]);
|
|
2214
|
+
const landed = await this.waitForSafeDeployed(
|
|
2215
|
+
safeAddress,
|
|
2216
|
+
chainConfig.publicClient
|
|
2217
|
+
);
|
|
2218
|
+
if (!landed) {
|
|
2219
|
+
throw new Error(
|
|
2220
|
+
`Safe deploy on chain ${chainId} for ${userAddress} did not land \u2014 retry the deposit.`
|
|
2221
|
+
);
|
|
2222
|
+
}
|
|
2223
|
+
} else {
|
|
2224
|
+
throw new Error(
|
|
2225
|
+
`Safe not available for ${userAddress} on chain ${chainId}.`
|
|
2226
|
+
);
|
|
2227
|
+
}
|
|
2481
2228
|
}
|
|
2482
2229
|
try {
|
|
2483
2230
|
const usdcDetails = await this.getUserDetails("USDC");
|
|
@@ -2759,23 +2506,19 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2759
2506
|
}
|
|
2760
2507
|
}
|
|
2761
2508
|
/**
|
|
2762
|
-
* Get
|
|
2763
|
-
* net-of-pending-fee fields (`balanceWithFee`, `underlyingAmountWithFee`).
|
|
2764
|
-
*
|
|
2765
|
-
* Pending fee is derived from onchain `current` earnings × fee rate.
|
|
2766
|
-
* Gross balances are unchanged. If earnings cannot be fetched, `*WithFee`
|
|
2767
|
-
* equals the gross value so the response shape stays stable.
|
|
2768
|
-
*
|
|
2769
|
-
* Note: portfolio balances are live; earnings used for the fee may be from
|
|
2770
|
-
* a snapshot, so net values can differ slightly from a fully live calc.
|
|
2509
|
+
* Get all active positions and portfolio for a user
|
|
2771
2510
|
*
|
|
2772
2511
|
* @param userAddress - User's EOA address
|
|
2773
|
-
* @
|
|
2512
|
+
* @param chainId - Optional: Filter by specific chain ID
|
|
2513
|
+
* @returns User's positions across all protocols
|
|
2774
2514
|
*
|
|
2775
2515
|
* @example
|
|
2776
2516
|
* ```typescript
|
|
2777
|
-
*
|
|
2778
|
-
*
|
|
2517
|
+
* // Get all positions across all chains
|
|
2518
|
+
* const positions = await sdk.getPositions(userAddress);
|
|
2519
|
+
*
|
|
2520
|
+
* // Get positions on a specific chain
|
|
2521
|
+
* const basePositions = await sdk.getPositions(userAddress, 8453);
|
|
2779
2522
|
* ```
|
|
2780
2523
|
*/
|
|
2781
2524
|
async getPortfolio(userAddress) {
|
|
@@ -2791,27 +2534,18 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2791
2534
|
portfolio: {}
|
|
2792
2535
|
};
|
|
2793
2536
|
}
|
|
2794
|
-
|
|
2795
|
-
const
|
|
2796
|
-
|
|
2797
|
-
ENDPOINTS.DATA_PORTFOLIO(smartWallet)
|
|
2798
|
-
),
|
|
2799
|
-
this.httpClient.dataGet(DATA_ENDPOINTS.ONCHAIN_EARNINGS(smartWallet)).catch(() => null)
|
|
2800
|
-
]);
|
|
2801
|
-
const convertedResponse = removeUnusedFields(response);
|
|
2802
|
-
const earningsData = earningsResult?.data || earningsResult;
|
|
2803
|
-
const currentEarningsByChain = earningsData?.current_earnings_by_chain ?? null;
|
|
2804
|
-
const portfolio = enrichPortfolioWithFees(
|
|
2805
|
-
convertedResponse,
|
|
2806
|
-
currentEarningsByChain
|
|
2537
|
+
console.log("Getting portfolio for", smartWalletInfo.smartWallet);
|
|
2538
|
+
const response = await this.httpClient.get(
|
|
2539
|
+
ENDPOINTS.DATA_PORTFOLIO(smartWalletInfo.smartWallet)
|
|
2807
2540
|
);
|
|
2541
|
+
const convertedResponse = removeUnusedFields(response);
|
|
2808
2542
|
return {
|
|
2809
2543
|
success: true,
|
|
2810
2544
|
userAddress,
|
|
2811
|
-
portfolio
|
|
2545
|
+
portfolio: convertedResponse
|
|
2812
2546
|
};
|
|
2813
2547
|
} catch (error) {
|
|
2814
|
-
throw new Error(`Failed to get
|
|
2548
|
+
throw new Error(`Failed to get positions: ${error.message}`);
|
|
2815
2549
|
}
|
|
2816
2550
|
}
|
|
2817
2551
|
// ============================================================================
|
|
@@ -3110,10 +2844,6 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3110
2844
|
/**
|
|
3111
2845
|
* Get onchain earnings for a wallet
|
|
3112
2846
|
*
|
|
3113
|
-
* Includes gross totals plus net-of-fee totals:
|
|
3114
|
-
* `totalEarningsByTokenWithFee = lifetime + unrealized + current × (1 - feeRate)`.
|
|
3115
|
-
* Lifetime and unrealized are never multiplied by the keep-rate.
|
|
3116
|
-
*
|
|
3117
2847
|
* @param walletAddress - Smart wallet address
|
|
3118
2848
|
* @returns Onchain earnings data with per-token breakdowns
|
|
3119
2849
|
*
|
|
@@ -3121,7 +2851,6 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3121
2851
|
* ```typescript
|
|
3122
2852
|
* const earnings = await sdk.getOnchainEarnings("0x...");
|
|
3123
2853
|
* console.log("Total USDC:", earnings.data.totalEarningsByToken["USDC"]);
|
|
3124
|
-
* console.log("Net USDC:", earnings.data.totalEarningsByTokenWithFee["USDC"]);
|
|
3125
2854
|
* console.log("Total on Arbitrum:", earnings.data.totalEarningsByChain?.["42161"]);
|
|
3126
2855
|
* ```
|
|
3127
2856
|
*/
|
|
@@ -3134,16 +2863,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3134
2863
|
DATA_ENDPOINTS.ONCHAIN_EARNINGS(walletAddress)
|
|
3135
2864
|
);
|
|
3136
2865
|
const data = response.data || response;
|
|
3137
|
-
console.log(JSON.stringify(data, null, 2));
|
|
3138
|
-
const withFee = enrichOnchainEarningsTotals(data);
|
|
3139
2866
|
return {
|
|
3140
2867
|
success: true,
|
|
3141
2868
|
data: {
|
|
3142
2869
|
walletAddress,
|
|
3143
2870
|
totalEarningsByToken: data.total_earnings_by_token || {},
|
|
3144
2871
|
totalEarningsByChain: data.total_earnings_by_chain || {},
|
|
3145
|
-
totalEarningsByTokenWithFee: withFee.totalEarningsByTokenWithFee,
|
|
3146
|
-
totalEarningsByChainWithFee: withFee.totalEarningsByChainWithFee,
|
|
3147
2872
|
lastCheckTimestamp: data.last_check_timestamp,
|
|
3148
2873
|
lastLogDate: data.last_log_date
|
|
3149
2874
|
}
|
|
@@ -3156,8 +2881,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3156
2881
|
}
|
|
3157
2882
|
/**
|
|
3158
2883
|
* Calculate/refresh onchain earnings for a wallet
|
|
3159
|
-
* This triggers a recalculation of earnings on the backend
|
|
3160
|
-
* Response shape matches `getOnchainEarnings` (includes `*WithFee` nets).
|
|
2884
|
+
* This triggers a recalculation of earnings on the backend
|
|
3161
2885
|
*
|
|
3162
2886
|
* @param walletAddress - Smart wallet address
|
|
3163
2887
|
* @returns Updated onchain earnings data with per-token breakdowns
|
|
@@ -3166,7 +2890,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3166
2890
|
* ```typescript
|
|
3167
2891
|
* const earnings = await sdk.calculateOnchainEarnings("0x...");
|
|
3168
2892
|
* console.log("Total USDC:", earnings.data.totalEarningsByToken["USDC"]);
|
|
3169
|
-
* console.log("
|
|
2893
|
+
* console.log("Per-chain total:", earnings.data.totalEarningsByChain);
|
|
3170
2894
|
* ```
|
|
3171
2895
|
*/
|
|
3172
2896
|
async calculateOnchainEarnings(walletAddress) {
|
|
@@ -3179,15 +2903,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3179
2903
|
{}
|
|
3180
2904
|
);
|
|
3181
2905
|
const data = response.data || response;
|
|
3182
|
-
const withFee = enrichOnchainEarningsTotals(data);
|
|
3183
2906
|
return {
|
|
3184
2907
|
success: true,
|
|
3185
2908
|
data: {
|
|
3186
2909
|
walletAddress,
|
|
3187
2910
|
totalEarningsByToken: data.total_earnings_by_token || {},
|
|
3188
2911
|
totalEarningsByChain: data.total_earnings_by_chain || {},
|
|
3189
|
-
totalEarningsByTokenWithFee: withFee.totalEarningsByTokenWithFee,
|
|
3190
|
-
totalEarningsByChainWithFee: withFee.totalEarningsByChainWithFee,
|
|
3191
2912
|
lastCheckTimestamp: data.last_check_timestamp,
|
|
3192
2913
|
lastLogDate: data.last_log_date
|
|
3193
2914
|
}
|