@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.mjs
CHANGED
|
@@ -18,6 +18,9 @@ var ENDPOINTS = {
|
|
|
18
18
|
LOG_DEPOSIT: "/users/log_deposit",
|
|
19
19
|
// Safe Deployment (single endpoint)
|
|
20
20
|
SAFE_DEPLOY: "/users/safe-deploy",
|
|
21
|
+
// Multichain deploy for predeployed (pool) wallets — deploys + rotates the
|
|
22
|
+
// same address on the requested chains (sponsored, no user signature).
|
|
23
|
+
DEPLOY_CHAINS: "/users/deploy-chains",
|
|
21
24
|
// Session Keys
|
|
22
25
|
SESSION_KEYS_CONFIG: "/session-keys/config",
|
|
23
26
|
SESSION_KEYS_ADD: "/session-keys/add",
|
|
@@ -460,7 +463,6 @@ var VAULT_ABI = [
|
|
|
460
463
|
var VAULT_ADDRESS = "0xD580071c47d4a667858B5FafAb85BC9C609beC5D";
|
|
461
464
|
|
|
462
465
|
// src/config/constants.ts
|
|
463
|
-
var ZYFI_FEE_RATE = 0.1;
|
|
464
466
|
var MIN_PORTFOLIO_BALANCE = {
|
|
465
467
|
1: {
|
|
466
468
|
USDC: 100n * 10n ** 6n,
|
|
@@ -1013,310 +1015,6 @@ var hasMatchingPool = (data, chains, asset) => {
|
|
|
1013
1015
|
return false;
|
|
1014
1016
|
};
|
|
1015
1017
|
|
|
1016
|
-
// src/utils/zyfi-fees.ts
|
|
1017
|
-
var CHAIN_NAME_TO_ID = {
|
|
1018
|
-
ethereum: "1",
|
|
1019
|
-
"ethereum mainnet": "1",
|
|
1020
|
-
mainnet: "1",
|
|
1021
|
-
base: "8453",
|
|
1022
|
-
arbitrum: "42161",
|
|
1023
|
-
"arbitrum one": "42161"
|
|
1024
|
-
};
|
|
1025
|
-
function symbolToAssetType(symbol) {
|
|
1026
|
-
const upper = symbol.trim().toUpperCase();
|
|
1027
|
-
if (upper === "USDC") return "usdc";
|
|
1028
|
-
if (upper === "WETH" || upper === "ETH") return "eth";
|
|
1029
|
-
if (upper === "EURC") return "eurc";
|
|
1030
|
-
if (upper === "CBBTC") return "cbbtc";
|
|
1031
|
-
if (upper === "CBETH") return "cbeth";
|
|
1032
|
-
return symbol.trim().toLowerCase();
|
|
1033
|
-
}
|
|
1034
|
-
function assetTypeToSymbol(assetType) {
|
|
1035
|
-
const lower = assetType.trim().toLowerCase();
|
|
1036
|
-
if (lower === "usdc") return "USDC";
|
|
1037
|
-
if (lower === "eth") return "WETH";
|
|
1038
|
-
if (lower === "eurc") return "EURC";
|
|
1039
|
-
if (lower === "cbbtc") return "CBBTC";
|
|
1040
|
-
if (lower === "cbeth") return "CBETH";
|
|
1041
|
-
return assetType.trim().toUpperCase();
|
|
1042
|
-
}
|
|
1043
|
-
function chainNameToId(chain) {
|
|
1044
|
-
if (!chain) return void 0;
|
|
1045
|
-
const trimmed = chain.trim();
|
|
1046
|
-
if (/^\d+$/.test(trimmed)) return trimmed;
|
|
1047
|
-
return CHAIN_NAME_TO_ID[trimmed.toLowerCase()];
|
|
1048
|
-
}
|
|
1049
|
-
function parseHumanAmount(value) {
|
|
1050
|
-
if (typeof value === "number") {
|
|
1051
|
-
return Number.isFinite(value) ? value : 0;
|
|
1052
|
-
}
|
|
1053
|
-
const parsed = Number.parseFloat(String(value ?? "0"));
|
|
1054
|
-
return Number.isFinite(parsed) ? parsed : 0;
|
|
1055
|
-
}
|
|
1056
|
-
function humanAmountToRaw(human, decimals) {
|
|
1057
|
-
if (!Number.isFinite(human) || human <= 0 || decimals < 0) {
|
|
1058
|
-
return 0n;
|
|
1059
|
-
}
|
|
1060
|
-
const fixed = human.toFixed(decimals);
|
|
1061
|
-
const [whole, frac = ""] = fixed.split(".");
|
|
1062
|
-
const wholePart = BigInt(whole);
|
|
1063
|
-
const fracPart = BigInt(frac.padEnd(decimals, "0").slice(0, decimals) || "0");
|
|
1064
|
-
return wholePart * 10n ** BigInt(decimals) + fracPart;
|
|
1065
|
-
}
|
|
1066
|
-
function formatHumanAmount(value) {
|
|
1067
|
-
if (!Number.isFinite(value) || value === 0) return "0";
|
|
1068
|
-
const fixed = value.toFixed(18);
|
|
1069
|
-
return fixed.replace(/\.?0+$/, "") || "0";
|
|
1070
|
-
}
|
|
1071
|
-
function computePendingFees(currentEarningsByChain) {
|
|
1072
|
-
const byToken = {};
|
|
1073
|
-
const byChain = {};
|
|
1074
|
-
const byChainByToken = {};
|
|
1075
|
-
let total = 0;
|
|
1076
|
-
if (!currentEarningsByChain) {
|
|
1077
|
-
return { byToken, byChain, byChainByToken, total };
|
|
1078
|
-
}
|
|
1079
|
-
Object.entries(currentEarningsByChain).forEach(([chainId, chainEarnings]) => {
|
|
1080
|
-
const feeByToken = {};
|
|
1081
|
-
let chainFee = 0;
|
|
1082
|
-
Object.entries(chainEarnings || {}).forEach(([tokenSymbol, tokenAmount]) => {
|
|
1083
|
-
const numericValue = parseHumanAmount(tokenAmount);
|
|
1084
|
-
const tokenFee = numericValue > 0 ? numericValue * ZYFI_FEE_RATE : 0;
|
|
1085
|
-
feeByToken[tokenSymbol] = tokenFee;
|
|
1086
|
-
chainFee += tokenFee;
|
|
1087
|
-
byToken[tokenSymbol] = (byToken[tokenSymbol] ?? 0) + tokenFee;
|
|
1088
|
-
});
|
|
1089
|
-
byChainByToken[chainId] = feeByToken;
|
|
1090
|
-
byChain[chainId] = chainFee;
|
|
1091
|
-
total += chainFee;
|
|
1092
|
-
});
|
|
1093
|
-
return { byToken, byChain, byChainByToken, total };
|
|
1094
|
-
}
|
|
1095
|
-
function feeForAssetType(feesByToken, assetType) {
|
|
1096
|
-
const symbol = assetTypeToSymbol(assetType);
|
|
1097
|
-
if (feesByToken[symbol] !== void 0) return feesByToken[symbol] ?? 0;
|
|
1098
|
-
if (assetType.toLowerCase() === "eth") {
|
|
1099
|
-
return feesByToken["ETH"] ?? feesByToken["WETH"] ?? 0;
|
|
1100
|
-
}
|
|
1101
|
-
return feesByToken[assetType.toUpperCase()] ?? 0;
|
|
1102
|
-
}
|
|
1103
|
-
function feeForChainAsset(byChainByToken, chainId, assetType) {
|
|
1104
|
-
const chainFees = byChainByToken[chainId] || {};
|
|
1105
|
-
return feeForAssetType(chainFees, assetType);
|
|
1106
|
-
}
|
|
1107
|
-
function applyFeeToHexBalance(balanceHex, feeHuman, decimals) {
|
|
1108
|
-
let balance;
|
|
1109
|
-
try {
|
|
1110
|
-
balance = BigInt(balanceHex || "0x0");
|
|
1111
|
-
} catch {
|
|
1112
|
-
balance = 0n;
|
|
1113
|
-
}
|
|
1114
|
-
const feeRaw = humanAmountToRaw(feeHuman, decimals);
|
|
1115
|
-
const net = balance > feeRaw ? balance - feeRaw : 0n;
|
|
1116
|
-
return `0x${net.toString(16)}`;
|
|
1117
|
-
}
|
|
1118
|
-
function applyFeeToDecimalWei(amount, feeHuman, decimals) {
|
|
1119
|
-
let balance;
|
|
1120
|
-
try {
|
|
1121
|
-
balance = BigInt(amount || "0");
|
|
1122
|
-
} catch {
|
|
1123
|
-
balance = 0n;
|
|
1124
|
-
}
|
|
1125
|
-
const feeRaw = humanAmountToRaw(feeHuman, decimals);
|
|
1126
|
-
const net = balance > feeRaw ? balance - feeRaw : 0n;
|
|
1127
|
-
return net.toString();
|
|
1128
|
-
}
|
|
1129
|
-
function enrichAssetBalance(entry, feeHuman) {
|
|
1130
|
-
const balanceWithFee = applyFeeToHexBalance(
|
|
1131
|
-
entry.balance,
|
|
1132
|
-
feeHuman,
|
|
1133
|
-
entry.decimals
|
|
1134
|
-
);
|
|
1135
|
-
return {
|
|
1136
|
-
...entry,
|
|
1137
|
-
balanceWithFee
|
|
1138
|
-
};
|
|
1139
|
-
}
|
|
1140
|
-
function resolvePositionSymbol(position) {
|
|
1141
|
-
if (position.token_symbol) return position.token_symbol;
|
|
1142
|
-
if (position.assetType) return assetTypeToSymbol(position.assetType);
|
|
1143
|
-
return void 0;
|
|
1144
|
-
}
|
|
1145
|
-
function resolvePositionDecimals(position) {
|
|
1146
|
-
if (typeof position.decimals === "number") return position.decimals;
|
|
1147
|
-
const asset = position.assetType || symbolToAssetType(position.token_symbol || "");
|
|
1148
|
-
if (asset === "usdc" || asset === "eurc") return 6;
|
|
1149
|
-
if (asset === "cbbtc") return 8;
|
|
1150
|
-
return 18;
|
|
1151
|
-
}
|
|
1152
|
-
function enrichPositions(positions, byChainByToken) {
|
|
1153
|
-
const groups = /* @__PURE__ */ new Map();
|
|
1154
|
-
positions.forEach((position, index) => {
|
|
1155
|
-
const chainId = chainNameToId(position.chain || "");
|
|
1156
|
-
const symbol = resolvePositionSymbol(position);
|
|
1157
|
-
if (!chainId || !symbol) {
|
|
1158
|
-
return;
|
|
1159
|
-
}
|
|
1160
|
-
const key = `${chainId}:${symbol.toUpperCase()}`;
|
|
1161
|
-
const list = groups.get(key) || [];
|
|
1162
|
-
list.push(index);
|
|
1163
|
-
groups.set(key, list);
|
|
1164
|
-
});
|
|
1165
|
-
const feeShareByIndex = /* @__PURE__ */ new Map();
|
|
1166
|
-
groups.forEach((indices, key) => {
|
|
1167
|
-
const [chainId, symbol] = key.split(":");
|
|
1168
|
-
const chainFees = byChainByToken[chainId] || {};
|
|
1169
|
-
let totalFee = chainFees[symbol] ?? (symbol === "WETH" ? chainFees["ETH"] : void 0) ?? (symbol === "ETH" ? chainFees["WETH"] : void 0) ?? 0;
|
|
1170
|
-
if (totalFee <= 0) {
|
|
1171
|
-
indices.forEach((i) => feeShareByIndex.set(i, 0));
|
|
1172
|
-
return;
|
|
1173
|
-
}
|
|
1174
|
-
let totalUnderlying = 0n;
|
|
1175
|
-
const underlyings = indices.map((i) => {
|
|
1176
|
-
try {
|
|
1177
|
-
return BigInt(positions[i].underlyingAmount || "0");
|
|
1178
|
-
} catch {
|
|
1179
|
-
return 0n;
|
|
1180
|
-
}
|
|
1181
|
-
});
|
|
1182
|
-
underlyings.forEach((u) => {
|
|
1183
|
-
totalUnderlying += u;
|
|
1184
|
-
});
|
|
1185
|
-
if (totalUnderlying === 0n) {
|
|
1186
|
-
const share = totalFee / indices.length;
|
|
1187
|
-
indices.forEach((i) => feeShareByIndex.set(i, share));
|
|
1188
|
-
return;
|
|
1189
|
-
}
|
|
1190
|
-
indices.forEach((i, idx) => {
|
|
1191
|
-
const share = totalFee * Number(underlyings[idx]) / Number(totalUnderlying);
|
|
1192
|
-
feeShareByIndex.set(i, share);
|
|
1193
|
-
});
|
|
1194
|
-
});
|
|
1195
|
-
return positions.map((position, index) => {
|
|
1196
|
-
const underlying = position.underlyingAmount;
|
|
1197
|
-
if (underlying === void 0) {
|
|
1198
|
-
return { ...position };
|
|
1199
|
-
}
|
|
1200
|
-
const feeShare = feeShareByIndex.get(index) ?? 0;
|
|
1201
|
-
const decimals = resolvePositionDecimals(position);
|
|
1202
|
-
return {
|
|
1203
|
-
...position,
|
|
1204
|
-
underlyingAmountWithFee: applyFeeToDecimalWei(
|
|
1205
|
-
underlying,
|
|
1206
|
-
feeShare,
|
|
1207
|
-
decimals
|
|
1208
|
-
)
|
|
1209
|
-
};
|
|
1210
|
-
});
|
|
1211
|
-
}
|
|
1212
|
-
function enrichPortfolioWithFees(portfolio, currentEarningsByChain) {
|
|
1213
|
-
const { byToken, byChainByToken } = computePendingFees(currentEarningsByChain);
|
|
1214
|
-
const portfolioByAssetType = portfolio.portfolioByAssetType ? Object.fromEntries(
|
|
1215
|
-
Object.entries(portfolio.portfolioByAssetType).map(
|
|
1216
|
-
([assetType, entry]) => [
|
|
1217
|
-
assetType,
|
|
1218
|
-
enrichAssetBalance(entry, feeForAssetType(byToken, assetType))
|
|
1219
|
-
]
|
|
1220
|
-
)
|
|
1221
|
-
) : void 0;
|
|
1222
|
-
const portfolioByChain = portfolio.portfolioByChain ? Object.fromEntries(
|
|
1223
|
-
Object.entries(portfolio.portfolioByChain).map(
|
|
1224
|
-
([chainId, assets]) => [
|
|
1225
|
-
chainId,
|
|
1226
|
-
Object.fromEntries(
|
|
1227
|
-
Object.entries(assets).map(([assetType, entry]) => [
|
|
1228
|
-
assetType,
|
|
1229
|
-
enrichAssetBalance(
|
|
1230
|
-
entry,
|
|
1231
|
-
feeForChainAsset(byChainByToken, chainId, assetType)
|
|
1232
|
-
)
|
|
1233
|
-
])
|
|
1234
|
-
)
|
|
1235
|
-
]
|
|
1236
|
-
)
|
|
1237
|
-
) : void 0;
|
|
1238
|
-
const positions = portfolio.positions ? enrichPositions(portfolio.positions, byChainByToken) : void 0;
|
|
1239
|
-
return {
|
|
1240
|
-
...portfolio,
|
|
1241
|
-
...portfolioByAssetType ? { portfolioByAssetType } : {},
|
|
1242
|
-
...portfolioByChain ? { portfolioByChain } : {},
|
|
1243
|
-
...positions ? { positions } : {}
|
|
1244
|
-
};
|
|
1245
|
-
}
|
|
1246
|
-
function sumCurrentByToken(currentByChain) {
|
|
1247
|
-
const totals = {};
|
|
1248
|
-
if (!currentByChain) return totals;
|
|
1249
|
-
Object.values(currentByChain).forEach((chainEarnings) => {
|
|
1250
|
-
Object.entries(chainEarnings || {}).forEach(([symbol, amount]) => {
|
|
1251
|
-
totals[symbol] = (totals[symbol] ?? 0) + parseHumanAmount(amount);
|
|
1252
|
-
});
|
|
1253
|
-
});
|
|
1254
|
-
return totals;
|
|
1255
|
-
}
|
|
1256
|
-
function enrichOnchainEarningsTotals(apiData) {
|
|
1257
|
-
const lifetimeByToken = apiData.lifetime_earnings_by_token || {};
|
|
1258
|
-
const lifetimeByChain = apiData.lifetime_earnings_by_chain || {};
|
|
1259
|
-
const currentByChain = apiData.current_earnings_by_chain || {};
|
|
1260
|
-
const unrealizedByChain = apiData.unrealized_earnings_by_chain || apiData.unrealized_earnings || {};
|
|
1261
|
-
const currentByToken = apiData.current_earnings_by_token && Object.keys(apiData.current_earnings_by_token).length > 0 ? Object.fromEntries(
|
|
1262
|
-
Object.entries(apiData.current_earnings_by_token).map(([k, v]) => [
|
|
1263
|
-
k,
|
|
1264
|
-
parseHumanAmount(v)
|
|
1265
|
-
])
|
|
1266
|
-
) : sumCurrentByToken(currentByChain);
|
|
1267
|
-
const unrealizedByToken = apiData.unrealized_earnings_by_token && Object.keys(apiData.unrealized_earnings_by_token).length > 0 ? Object.fromEntries(
|
|
1268
|
-
Object.entries(apiData.unrealized_earnings_by_token).map(([k, v]) => [
|
|
1269
|
-
k,
|
|
1270
|
-
parseHumanAmount(v)
|
|
1271
|
-
])
|
|
1272
|
-
) : sumCurrentByToken(unrealizedByChain);
|
|
1273
|
-
const keepRate = 1 - ZYFI_FEE_RATE;
|
|
1274
|
-
const tokenKeys = /* @__PURE__ */ new Set([
|
|
1275
|
-
...Object.keys(lifetimeByToken),
|
|
1276
|
-
...Object.keys(unrealizedByToken),
|
|
1277
|
-
...Object.keys(currentByToken)
|
|
1278
|
-
]);
|
|
1279
|
-
const totalEarningsByTokenWithFee = {};
|
|
1280
|
-
tokenKeys.forEach((symbol) => {
|
|
1281
|
-
const lifetime = parseHumanAmount(lifetimeByToken[symbol]);
|
|
1282
|
-
const unrealized = unrealizedByToken[symbol] ?? 0;
|
|
1283
|
-
const current = currentByToken[symbol] ?? 0;
|
|
1284
|
-
totalEarningsByTokenWithFee[symbol] = formatHumanAmount(
|
|
1285
|
-
lifetime + unrealized + current * keepRate
|
|
1286
|
-
);
|
|
1287
|
-
});
|
|
1288
|
-
const chainKeys = /* @__PURE__ */ new Set([
|
|
1289
|
-
...Object.keys(lifetimeByChain),
|
|
1290
|
-
...Object.keys(unrealizedByChain),
|
|
1291
|
-
...Object.keys(currentByChain)
|
|
1292
|
-
]);
|
|
1293
|
-
const totalEarningsByChainWithFee = {};
|
|
1294
|
-
chainKeys.forEach((chainId) => {
|
|
1295
|
-
const lifetimeTokens = lifetimeByChain[chainId] || {};
|
|
1296
|
-
const unrealizedTokens = unrealizedByChain[chainId] || {};
|
|
1297
|
-
const currentTokens = currentByChain[chainId] || {};
|
|
1298
|
-
const symbols = /* @__PURE__ */ new Set([
|
|
1299
|
-
...Object.keys(lifetimeTokens),
|
|
1300
|
-
...Object.keys(unrealizedTokens),
|
|
1301
|
-
...Object.keys(currentTokens)
|
|
1302
|
-
]);
|
|
1303
|
-
const chainMap = {};
|
|
1304
|
-
symbols.forEach((symbol) => {
|
|
1305
|
-
const lifetime = parseHumanAmount(lifetimeTokens[symbol]);
|
|
1306
|
-
const unrealized = parseHumanAmount(unrealizedTokens[symbol]);
|
|
1307
|
-
const current = parseHumanAmount(currentTokens[symbol]);
|
|
1308
|
-
chainMap[symbol] = formatHumanAmount(
|
|
1309
|
-
lifetime + unrealized + current * keepRate
|
|
1310
|
-
);
|
|
1311
|
-
});
|
|
1312
|
-
totalEarningsByChainWithFee[chainId] = chainMap;
|
|
1313
|
-
});
|
|
1314
|
-
return {
|
|
1315
|
-
totalEarningsByTokenWithFee,
|
|
1316
|
-
totalEarningsByChainWithFee
|
|
1317
|
-
};
|
|
1318
|
-
}
|
|
1319
|
-
|
|
1320
1018
|
// src/core/ZyfaiSDK.ts
|
|
1321
1019
|
import { SiweMessage } from "siwe";
|
|
1322
1020
|
var _ZyfaiSDK = class _ZyfaiSDK {
|
|
@@ -2407,6 +2105,42 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2407
2105
|
* await sdk.depositFunds(userAddress, 8453, "10000000000", "USDC", "aggressive");
|
|
2408
2106
|
* ```
|
|
2409
2107
|
*/
|
|
2108
|
+
/**
|
|
2109
|
+
* Deploy the connected predeployed (pool) wallet on one or more chains.
|
|
2110
|
+
*
|
|
2111
|
+
* A pool wallet is only deployed on the chains it has been onboarded to (see
|
|
2112
|
+
* the `chains` array from getSmartWalletByEOA); every other supported chain is
|
|
2113
|
+
* counterfactual until deployed here. The backend deploys + rotates the same
|
|
2114
|
+
* address on each chain in parallel (sponsored, no user signature) and only
|
|
2115
|
+
* resolves once all requested chains have landed on-chain. Idempotent — chains
|
|
2116
|
+
* already deployed are a no-op.
|
|
2117
|
+
*
|
|
2118
|
+
* `depositFunds` calls this automatically when the target chain isn't deployed,
|
|
2119
|
+
* so most integrations never need it directly; use it to pre-provision chains.
|
|
2120
|
+
*
|
|
2121
|
+
* @param chainIds - Chain IDs to deploy the wallet on (e.g. [1, 42161])
|
|
2122
|
+
*/
|
|
2123
|
+
async deployOnChains(chainIds) {
|
|
2124
|
+
if (!chainIds?.length) {
|
|
2125
|
+
throw new Error("At least one chainId is required");
|
|
2126
|
+
}
|
|
2127
|
+
for (const c of chainIds) {
|
|
2128
|
+
if (!isSupportedChain(c)) throw new Error(`Unsupported chain ID: ${c}`);
|
|
2129
|
+
}
|
|
2130
|
+
await this.authenticateUser();
|
|
2131
|
+
return this.httpClient.post(ENDPOINTS.DEPLOY_CHAINS, { chainIds });
|
|
2132
|
+
}
|
|
2133
|
+
/**
|
|
2134
|
+
* Poll until the Safe has code on-chain. The backend awaits landing before it
|
|
2135
|
+
* responds, so this only guards against read-after-write RPC lag.
|
|
2136
|
+
*/
|
|
2137
|
+
async waitForSafeDeployed(safeAddress, publicClient, tries = 5, delayMs = 2e3) {
|
|
2138
|
+
for (let i = 0; i < tries; i++) {
|
|
2139
|
+
if (await isSafeDeployed(safeAddress, publicClient)) return true;
|
|
2140
|
+
await new Promise((r) => setTimeout(r, delayMs));
|
|
2141
|
+
}
|
|
2142
|
+
return isSafeDeployed(safeAddress, publicClient);
|
|
2143
|
+
}
|
|
2410
2144
|
async depositFunds(userAddress, chainId, amount, asset, strategy) {
|
|
2411
2145
|
try {
|
|
2412
2146
|
if (!userAddress) {
|
|
@@ -2443,9 +2177,22 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2443
2177
|
chainConfig.publicClient
|
|
2444
2178
|
);
|
|
2445
2179
|
if (!isDeployed) {
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2180
|
+
if (this.isPredeployed && this.isConnectedUser(userAddress)) {
|
|
2181
|
+
await this.deployOnChains([chainId]);
|
|
2182
|
+
const landed = await this.waitForSafeDeployed(
|
|
2183
|
+
safeAddress,
|
|
2184
|
+
chainConfig.publicClient
|
|
2185
|
+
);
|
|
2186
|
+
if (!landed) {
|
|
2187
|
+
throw new Error(
|
|
2188
|
+
`Safe deploy on chain ${chainId} for ${userAddress} did not land \u2014 retry the deposit.`
|
|
2189
|
+
);
|
|
2190
|
+
}
|
|
2191
|
+
} else {
|
|
2192
|
+
throw new Error(
|
|
2193
|
+
`Safe not available for ${userAddress} on chain ${chainId}.`
|
|
2194
|
+
);
|
|
2195
|
+
}
|
|
2449
2196
|
}
|
|
2450
2197
|
try {
|
|
2451
2198
|
const usdcDetails = await this.getUserDetails("USDC");
|
|
@@ -2727,23 +2474,19 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2727
2474
|
}
|
|
2728
2475
|
}
|
|
2729
2476
|
/**
|
|
2730
|
-
* Get
|
|
2731
|
-
* net-of-pending-fee fields (`balanceWithFee`, `underlyingAmountWithFee`).
|
|
2732
|
-
*
|
|
2733
|
-
* Pending fee is derived from onchain `current` earnings × fee rate.
|
|
2734
|
-
* Gross balances are unchanged. If earnings cannot be fetched, `*WithFee`
|
|
2735
|
-
* equals the gross value so the response shape stays stable.
|
|
2736
|
-
*
|
|
2737
|
-
* Note: portfolio balances are live; earnings used for the fee may be from
|
|
2738
|
-
* a snapshot, so net values can differ slightly from a fully live calc.
|
|
2477
|
+
* Get all active positions and portfolio for a user
|
|
2739
2478
|
*
|
|
2740
2479
|
* @param userAddress - User's EOA address
|
|
2741
|
-
* @
|
|
2480
|
+
* @param chainId - Optional: Filter by specific chain ID
|
|
2481
|
+
* @returns User's positions across all protocols
|
|
2742
2482
|
*
|
|
2743
2483
|
* @example
|
|
2744
2484
|
* ```typescript
|
|
2745
|
-
*
|
|
2746
|
-
*
|
|
2485
|
+
* // Get all positions across all chains
|
|
2486
|
+
* const positions = await sdk.getPositions(userAddress);
|
|
2487
|
+
*
|
|
2488
|
+
* // Get positions on a specific chain
|
|
2489
|
+
* const basePositions = await sdk.getPositions(userAddress, 8453);
|
|
2747
2490
|
* ```
|
|
2748
2491
|
*/
|
|
2749
2492
|
async getPortfolio(userAddress) {
|
|
@@ -2759,27 +2502,18 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
2759
2502
|
portfolio: {}
|
|
2760
2503
|
};
|
|
2761
2504
|
}
|
|
2762
|
-
|
|
2763
|
-
const
|
|
2764
|
-
|
|
2765
|
-
ENDPOINTS.DATA_PORTFOLIO(smartWallet)
|
|
2766
|
-
),
|
|
2767
|
-
this.httpClient.dataGet(DATA_ENDPOINTS.ONCHAIN_EARNINGS(smartWallet)).catch(() => null)
|
|
2768
|
-
]);
|
|
2769
|
-
const convertedResponse = removeUnusedFields(response);
|
|
2770
|
-
const earningsData = earningsResult?.data || earningsResult;
|
|
2771
|
-
const currentEarningsByChain = earningsData?.current_earnings_by_chain ?? null;
|
|
2772
|
-
const portfolio = enrichPortfolioWithFees(
|
|
2773
|
-
convertedResponse,
|
|
2774
|
-
currentEarningsByChain
|
|
2505
|
+
console.log("Getting portfolio for", smartWalletInfo.smartWallet);
|
|
2506
|
+
const response = await this.httpClient.get(
|
|
2507
|
+
ENDPOINTS.DATA_PORTFOLIO(smartWalletInfo.smartWallet)
|
|
2775
2508
|
);
|
|
2509
|
+
const convertedResponse = removeUnusedFields(response);
|
|
2776
2510
|
return {
|
|
2777
2511
|
success: true,
|
|
2778
2512
|
userAddress,
|
|
2779
|
-
portfolio
|
|
2513
|
+
portfolio: convertedResponse
|
|
2780
2514
|
};
|
|
2781
2515
|
} catch (error) {
|
|
2782
|
-
throw new Error(`Failed to get
|
|
2516
|
+
throw new Error(`Failed to get positions: ${error.message}`);
|
|
2783
2517
|
}
|
|
2784
2518
|
}
|
|
2785
2519
|
// ============================================================================
|
|
@@ -3078,10 +2812,6 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3078
2812
|
/**
|
|
3079
2813
|
* Get onchain earnings for a wallet
|
|
3080
2814
|
*
|
|
3081
|
-
* Includes gross totals plus net-of-fee totals:
|
|
3082
|
-
* `totalEarningsByTokenWithFee = lifetime + unrealized + current × (1 - feeRate)`.
|
|
3083
|
-
* Lifetime and unrealized are never multiplied by the keep-rate.
|
|
3084
|
-
*
|
|
3085
2815
|
* @param walletAddress - Smart wallet address
|
|
3086
2816
|
* @returns Onchain earnings data with per-token breakdowns
|
|
3087
2817
|
*
|
|
@@ -3089,7 +2819,6 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3089
2819
|
* ```typescript
|
|
3090
2820
|
* const earnings = await sdk.getOnchainEarnings("0x...");
|
|
3091
2821
|
* console.log("Total USDC:", earnings.data.totalEarningsByToken["USDC"]);
|
|
3092
|
-
* console.log("Net USDC:", earnings.data.totalEarningsByTokenWithFee["USDC"]);
|
|
3093
2822
|
* console.log("Total on Arbitrum:", earnings.data.totalEarningsByChain?.["42161"]);
|
|
3094
2823
|
* ```
|
|
3095
2824
|
*/
|
|
@@ -3102,16 +2831,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3102
2831
|
DATA_ENDPOINTS.ONCHAIN_EARNINGS(walletAddress)
|
|
3103
2832
|
);
|
|
3104
2833
|
const data = response.data || response;
|
|
3105
|
-
console.log(JSON.stringify(data, null, 2));
|
|
3106
|
-
const withFee = enrichOnchainEarningsTotals(data);
|
|
3107
2834
|
return {
|
|
3108
2835
|
success: true,
|
|
3109
2836
|
data: {
|
|
3110
2837
|
walletAddress,
|
|
3111
2838
|
totalEarningsByToken: data.total_earnings_by_token || {},
|
|
3112
2839
|
totalEarningsByChain: data.total_earnings_by_chain || {},
|
|
3113
|
-
totalEarningsByTokenWithFee: withFee.totalEarningsByTokenWithFee,
|
|
3114
|
-
totalEarningsByChainWithFee: withFee.totalEarningsByChainWithFee,
|
|
3115
2840
|
lastCheckTimestamp: data.last_check_timestamp,
|
|
3116
2841
|
lastLogDate: data.last_log_date
|
|
3117
2842
|
}
|
|
@@ -3124,8 +2849,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3124
2849
|
}
|
|
3125
2850
|
/**
|
|
3126
2851
|
* Calculate/refresh onchain earnings for a wallet
|
|
3127
|
-
* This triggers a recalculation of earnings on the backend
|
|
3128
|
-
* Response shape matches `getOnchainEarnings` (includes `*WithFee` nets).
|
|
2852
|
+
* This triggers a recalculation of earnings on the backend
|
|
3129
2853
|
*
|
|
3130
2854
|
* @param walletAddress - Smart wallet address
|
|
3131
2855
|
* @returns Updated onchain earnings data with per-token breakdowns
|
|
@@ -3134,7 +2858,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3134
2858
|
* ```typescript
|
|
3135
2859
|
* const earnings = await sdk.calculateOnchainEarnings("0x...");
|
|
3136
2860
|
* console.log("Total USDC:", earnings.data.totalEarningsByToken["USDC"]);
|
|
3137
|
-
* console.log("
|
|
2861
|
+
* console.log("Per-chain total:", earnings.data.totalEarningsByChain);
|
|
3138
2862
|
* ```
|
|
3139
2863
|
*/
|
|
3140
2864
|
async calculateOnchainEarnings(walletAddress) {
|
|
@@ -3147,15 +2871,12 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3147
2871
|
{}
|
|
3148
2872
|
);
|
|
3149
2873
|
const data = response.data || response;
|
|
3150
|
-
const withFee = enrichOnchainEarningsTotals(data);
|
|
3151
2874
|
return {
|
|
3152
2875
|
success: true,
|
|
3153
2876
|
data: {
|
|
3154
2877
|
walletAddress,
|
|
3155
2878
|
totalEarningsByToken: data.total_earnings_by_token || {},
|
|
3156
2879
|
totalEarningsByChain: data.total_earnings_by_chain || {},
|
|
3157
|
-
totalEarningsByTokenWithFee: withFee.totalEarningsByTokenWithFee,
|
|
3158
|
-
totalEarningsByChainWithFee: withFee.totalEarningsByChainWithFee,
|
|
3159
2880
|
lastCheckTimestamp: data.last_check_timestamp,
|
|
3160
2881
|
lastLogDate: data.last_log_date
|
|
3161
2882
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zyfai/sdk",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.47",
|
|
4
4
|
"description": "TypeScript SDK for Zyfai Yield Optimization Engine - Deploy Safe smart wallets, manage session keys, and interact with DeFi protocols",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|