@ecency/sdk 1.5.21 → 1.5.22

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.
@@ -2378,8 +2378,8 @@ function toEntryArray(x) {
2378
2378
  return Array.isArray(x) ? x : [];
2379
2379
  }
2380
2380
  async function getVisibleFirstLevelThreadItems(container) {
2381
- const queryOptions88 = getDiscussionsQueryOptions(container, "created" /* created */, true);
2382
- const discussionItemsRaw = await CONFIG.queryClient.fetchQuery(queryOptions88);
2381
+ const queryOptions89 = getDiscussionsQueryOptions(container, "created" /* created */, true);
2382
+ const discussionItemsRaw = await CONFIG.queryClient.fetchQuery(queryOptions89);
2383
2383
  const discussionItems = toEntryArray(discussionItemsRaw);
2384
2384
  if (discussionItems.length <= 1) {
2385
2385
  return [];
@@ -4951,6 +4951,163 @@ function getRecurrentTransfersQueryOptions(username) {
4951
4951
  enabled: !!username
4952
4952
  });
4953
4953
  }
4954
+ function normalizeString(value) {
4955
+ if (typeof value === "string") {
4956
+ const trimmed = value.trim();
4957
+ return trimmed.length > 0 ? trimmed : void 0;
4958
+ }
4959
+ return void 0;
4960
+ }
4961
+ function normalizeNumber(value) {
4962
+ if (typeof value === "number" && Number.isFinite(value)) {
4963
+ return value;
4964
+ }
4965
+ if (typeof value === "string") {
4966
+ const trimmed = value.trim();
4967
+ if (!trimmed) {
4968
+ return void 0;
4969
+ }
4970
+ const direct = Number.parseFloat(trimmed);
4971
+ if (Number.isFinite(direct)) {
4972
+ return direct;
4973
+ }
4974
+ const sanitized = trimmed.replace(/,/g, "");
4975
+ const match = sanitized.match(/[-+]?\d+(?:\.\d+)?/);
4976
+ if (match) {
4977
+ const parsed = Number.parseFloat(match[0]);
4978
+ if (Number.isFinite(parsed)) {
4979
+ return parsed;
4980
+ }
4981
+ }
4982
+ }
4983
+ return void 0;
4984
+ }
4985
+ function parseToken(rawToken) {
4986
+ if (!rawToken || typeof rawToken !== "object") {
4987
+ return void 0;
4988
+ }
4989
+ const token = rawToken;
4990
+ return {
4991
+ name: normalizeString(token.name) ?? "",
4992
+ symbol: normalizeString(token.symbol) ?? "",
4993
+ layer: normalizeString(token.layer) ?? "hive",
4994
+ balance: normalizeNumber(token.balance) ?? 0,
4995
+ fiatRate: normalizeNumber(token.fiatRate) ?? 0,
4996
+ currency: normalizeString(token.currency) ?? "usd",
4997
+ precision: normalizeNumber(token.precision) ?? 3,
4998
+ address: normalizeString(token.address),
4999
+ error: normalizeString(token.error),
5000
+ pendingRewards: normalizeNumber(token.pendingRewards),
5001
+ pendingRewardsFiat: normalizeNumber(token.pendingRewardsFiat),
5002
+ liquid: normalizeNumber(token.liquid),
5003
+ liquidFiat: normalizeNumber(token.liquidFiat),
5004
+ savings: normalizeNumber(token.savings),
5005
+ savingsFiat: normalizeNumber(token.savingsFiat),
5006
+ staked: normalizeNumber(token.staked),
5007
+ stakedFiat: normalizeNumber(token.stakedFiat),
5008
+ iconUrl: normalizeString(token.iconUrl),
5009
+ actions: token.actions ?? [],
5010
+ extraData: token.extraData ?? [],
5011
+ apr: normalizeNumber(token.apr)
5012
+ };
5013
+ }
5014
+ function extractTokens(payload) {
5015
+ if (!payload || typeof payload !== "object") {
5016
+ return [];
5017
+ }
5018
+ const containers = [payload];
5019
+ const record = payload;
5020
+ if (record.data && typeof record.data === "object") {
5021
+ containers.push(record.data);
5022
+ }
5023
+ if (record.result && typeof record.result === "object") {
5024
+ containers.push(record.result);
5025
+ }
5026
+ if (record.portfolio && typeof record.portfolio === "object") {
5027
+ containers.push(record.portfolio);
5028
+ }
5029
+ for (const container of containers) {
5030
+ if (Array.isArray(container)) {
5031
+ return container;
5032
+ }
5033
+ if (container && typeof container === "object") {
5034
+ for (const key of [
5035
+ "wallets",
5036
+ "tokens",
5037
+ "assets",
5038
+ "items",
5039
+ "portfolio",
5040
+ "balances"
5041
+ ]) {
5042
+ const value = container[key];
5043
+ if (Array.isArray(value)) {
5044
+ return value;
5045
+ }
5046
+ }
5047
+ }
5048
+ }
5049
+ return [];
5050
+ }
5051
+ function resolveUsername(payload) {
5052
+ if (!payload || typeof payload !== "object") {
5053
+ return void 0;
5054
+ }
5055
+ const record = payload;
5056
+ return normalizeString(record.username) ?? normalizeString(record.name) ?? normalizeString(record.account);
5057
+ }
5058
+ function getPortfolioQueryOptions(username, currency = "usd", onlyEnabled = true) {
5059
+ return reactQuery.queryOptions({
5060
+ queryKey: [
5061
+ "wallet",
5062
+ "portfolio",
5063
+ "v2",
5064
+ username,
5065
+ onlyEnabled ? "only-enabled" : "all",
5066
+ currency
5067
+ ],
5068
+ enabled: Boolean(username),
5069
+ staleTime: 6e4,
5070
+ refetchInterval: 12e4,
5071
+ queryFn: async () => {
5072
+ if (!username) {
5073
+ throw new Error("[SDK][Wallet] \u2013 username is required");
5074
+ }
5075
+ if (CONFIG.privateApiHost === void 0 || CONFIG.privateApiHost === null) {
5076
+ throw new Error(
5077
+ "[SDK][Wallet] \u2013 privateApiHost isn't configured for portfolio"
5078
+ );
5079
+ }
5080
+ const endpoint = `${CONFIG.privateApiHost}/wallet-api/portfolio-v2`;
5081
+ const response = await fetch(endpoint, {
5082
+ method: "POST",
5083
+ headers: {
5084
+ Accept: "application/json",
5085
+ "Content-Type": "application/json"
5086
+ },
5087
+ body: JSON.stringify({ username, onlyEnabled, currency })
5088
+ });
5089
+ if (!response.ok) {
5090
+ throw new Error(
5091
+ `[SDK][Wallet] \u2013 Portfolio request failed (${response.status})`
5092
+ );
5093
+ }
5094
+ const payload = await response.json();
5095
+ const tokens = extractTokens(payload).map((item) => parseToken(item)).filter((item) => Boolean(item));
5096
+ if (!tokens.length) {
5097
+ throw new Error(
5098
+ "[SDK][Wallet] \u2013 Portfolio payload contained no tokens"
5099
+ );
5100
+ }
5101
+ return {
5102
+ username: resolveUsername(payload) ?? username,
5103
+ currency: normalizeString(
5104
+ payload?.fiatCurrency ?? payload?.currency
5105
+ )?.toUpperCase(),
5106
+ wallets: tokens
5107
+ };
5108
+ }
5109
+ });
5110
+ }
4954
5111
  function getWitnessesInfiniteQueryOptions(limit) {
4955
5112
  return reactQuery.infiniteQueryOptions({
4956
5113
  queryKey: ["witnesses", "list", limit],
@@ -5942,6 +6099,7 @@ exports.getOrderBookQueryOptions = getOrderBookQueryOptions;
5942
6099
  exports.getOutgoingRcDelegationsInfiniteQueryOptions = getOutgoingRcDelegationsInfiniteQueryOptions;
5943
6100
  exports.getPageStatsQueryOptions = getPageStatsQueryOptions;
5944
6101
  exports.getPointsQueryOptions = getPointsQueryOptions;
6102
+ exports.getPortfolioQueryOptions = getPortfolioQueryOptions;
5945
6103
  exports.getPost = getPost;
5946
6104
  exports.getPostHeader = getPostHeader;
5947
6105
  exports.getPostHeaderQueryOptions = getPostHeaderQueryOptions;