@bze/bze-ui-kit 1.0.6 → 1.0.8

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/dist/index.mjs CHANGED
@@ -603,12 +603,82 @@ function truncateDenom(denom) {
603
603
  }
604
604
  var isIbcAsset = (asset) => asset.type === ASSET_TYPE_IBC;
605
605
 
606
+ // src/utils/coins.ts
607
+ var coin = (amount, denom) => {
608
+ let outAmount;
609
+ if (typeof amount === "number") {
610
+ if (!Number.isInteger(amount) || amount < 0 || amount > Number.MAX_SAFE_INTEGER) {
611
+ throw new Error("Given amount is not a safe integer. Consider using a string instead to overcome the limitations of JS numbers.");
612
+ }
613
+ outAmount = String(amount);
614
+ } else {
615
+ if (!amount.match(/^[0-9]+$/)) {
616
+ throw new Error("Invalid unsigned integer string format");
617
+ }
618
+ outAmount = amount.replace(/^0*/, "") || "0";
619
+ }
620
+ return { amount: outAmount, denom };
621
+ };
622
+ var coins = (amount, denom) => {
623
+ return [coin(amount, denom)];
624
+ };
625
+ var parseCoins = (input) => {
626
+ return input.replace(/\s/g, "").split(",").filter(Boolean).map((part) => {
627
+ const match = part.match(/^([0-9]+)([a-zA-Z][a-zA-Z0-9/:._-]{2,127})$/);
628
+ if (!match) throw new Error("Got an invalid coin string");
629
+ return {
630
+ amount: match[1].replace(/^0+/, "") || "0",
631
+ denom: match[2]
632
+ };
633
+ });
634
+ };
635
+
606
636
  // src/utils/events.ts
607
637
  var getMarketOrderBookChangedEvent = (marketId) => getMarketEventKey(ORDER_BOOK_CHANGED_EVENT, marketId);
608
638
  var getMarketEventKey = (eventType, marketId) => `${eventType}:${marketId}`;
609
639
  var mapEventAttributes = (attributes) => {
610
640
  return attributes.reduce((acc, attr) => __spreadProps(__spreadValues({}, acc), { [attr.key]: attr.value.replace('"', "").replace('"', "") }), {});
611
641
  };
642
+ var getEventKeyValue = (event, key) => {
643
+ var _a2;
644
+ return (_a2 = event.attributes.find((attribute) => attribute.key === key)) == null ? void 0 : _a2.value;
645
+ };
646
+ var getEventMarketId = (event) => {
647
+ var _a2;
648
+ return (_a2 = getEventKeyValue(event, "market_id")) == null ? void 0 : _a2.replaceAll('"', "");
649
+ };
650
+ var isAddressTransfer = (address, event) => {
651
+ if (address === "" || event.type !== "transfer") return false;
652
+ return event.attributes.find((attribute) => attribute.value === address) !== void 0;
653
+ };
654
+ var isOrderBookEvent = (event) => {
655
+ return event.type.includes("bze.tradebin.Order");
656
+ };
657
+ var isOrderExecutedEvent = (event) => {
658
+ return event.type.includes("bze.tradebin.OrderExecutedEvent");
659
+ };
660
+ var isSwapEvent = (event) => {
661
+ return event.type.includes("bze.tradebin.SwapEvent");
662
+ };
663
+ var isCoinbaseEvent = (event) => {
664
+ return event.type.includes("coinbase");
665
+ };
666
+ var isBurnEvent = (event) => {
667
+ return event.type.includes("burn");
668
+ };
669
+ var isEpochStartEvent = (event) => {
670
+ return event.type.includes("bze.epochs.EpochStartEvent");
671
+ };
672
+ var getMintedAmount = (event) => {
673
+ const defaultCoin = coins(0, getChainNativeAssetDenom());
674
+ try {
675
+ const amountAttribute = event.attributes.find((attribute) => attribute.key === "amount");
676
+ return amountAttribute ? parseCoins(amountAttribute.value) : defaultCoin;
677
+ } catch (e) {
678
+ console.error("Failed to parse minted amount from coinbase event", e);
679
+ return defaultCoin;
680
+ }
681
+ };
612
682
 
613
683
  // src/utils/formatter.ts
614
684
  import BigNumber2 from "bignumber.js";
@@ -723,36 +793,6 @@ var openExternalLink = (url) => {
723
793
  window.open(url, "_blank", "noopener,noreferrer");
724
794
  };
725
795
 
726
- // src/utils/coins.ts
727
- var coin = (amount, denom) => {
728
- let outAmount;
729
- if (typeof amount === "number") {
730
- if (!Number.isInteger(amount) || amount < 0 || amount > Number.MAX_SAFE_INTEGER) {
731
- throw new Error("Given amount is not a safe integer. Consider using a string instead to overcome the limitations of JS numbers.");
732
- }
733
- outAmount = String(amount);
734
- } else {
735
- if (!amount.match(/^[0-9]+$/)) {
736
- throw new Error("Invalid unsigned integer string format");
737
- }
738
- outAmount = amount.replace(/^0*/, "") || "0";
739
- }
740
- return { amount: outAmount, denom };
741
- };
742
- var coins = (amount, denom) => {
743
- return [coin(amount, denom)];
744
- };
745
- var parseCoins = (input) => {
746
- return input.replace(/\s/g, "").split(",").filter(Boolean).map((part) => {
747
- const match = part.match(/^([0-9]+)([a-zA-Z][a-zA-Z0-9/:._-]{2,127})$/);
748
- if (!match) throw new Error("Got an invalid coin string");
749
- return {
750
- amount: match[1].replace(/^0+/, "") || "0",
751
- denom: match[2]
752
- };
753
- });
754
- };
755
-
756
796
  // src/utils/ibc.ts
757
797
  import BigNumber3 from "bignumber.js";
758
798
  var canDepositFromIBC = (ibcData2) => {
@@ -1419,6 +1459,114 @@ var isPoolSupportedByValidator = (baseDenom, quoteDenom) => {
1419
1459
  return supportedDenoms.includes(baseDenom) || supportedDenoms.includes(quoteDenom);
1420
1460
  };
1421
1461
 
1462
+ // src/utils/ws_rpc_client.ts
1463
+ var socket = null;
1464
+ var activeUrl = "";
1465
+ var socketConnected = false;
1466
+ var connectingPromise = null;
1467
+ var msgId = 0;
1468
+ var reconnectAttempts = 0;
1469
+ var reconnectTimeout = null;
1470
+ var MAX_RECONNECT_ATTEMPTS = 10;
1471
+ var activeSubscriptions = /* @__PURE__ */ new Map();
1472
+ var handleMessage = (event) => {
1473
+ var _a2;
1474
+ try {
1475
+ const msg = JSON.parse(event.data);
1476
+ const id = String((_a2 = msg == null ? void 0 : msg.id) != null ? _a2 : "");
1477
+ if (id && activeSubscriptions.has(id) && msg.result && Object.keys(msg.result).length > 0) {
1478
+ activeSubscriptions.get(id).handler(msg.result);
1479
+ }
1480
+ } catch (e) {
1481
+ }
1482
+ };
1483
+ var resubscribeAll = () => {
1484
+ for (const [id, sub] of activeSubscriptions) {
1485
+ socket.send(JSON.stringify({
1486
+ jsonrpc: "2.0",
1487
+ method: "subscribe",
1488
+ id,
1489
+ params: { query: sub.query }
1490
+ }));
1491
+ }
1492
+ };
1493
+ var scheduleReconnect = () => {
1494
+ if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
1495
+ console.error("[WS] Max reconnect attempts reached");
1496
+ return;
1497
+ }
1498
+ reconnectAttempts++;
1499
+ const delay = Math.min(1e3 * Math.pow(2, reconnectAttempts - 1), 3e4);
1500
+ reconnectTimeout = setTimeout(async () => {
1501
+ try {
1502
+ await openSocket(activeUrl);
1503
+ resubscribeAll();
1504
+ reconnectAttempts = 0;
1505
+ } catch (e) {
1506
+ scheduleReconnect();
1507
+ }
1508
+ }, delay);
1509
+ };
1510
+ var openSocket = (url) => {
1511
+ return new Promise((resolve, reject) => {
1512
+ const ws = new WebSocket(url);
1513
+ ws.onopen = () => {
1514
+ socket = ws;
1515
+ socketConnected = true;
1516
+ resolve();
1517
+ };
1518
+ ws.onmessage = handleMessage;
1519
+ ws.onclose = () => {
1520
+ socketConnected = false;
1521
+ scheduleReconnect();
1522
+ };
1523
+ ws.onerror = (err) => {
1524
+ socketConnected = false;
1525
+ reject(err);
1526
+ };
1527
+ });
1528
+ };
1529
+ var getOrCreateSocket = async (rpcEndpoint) => {
1530
+ const url = rpcEndpoint.replace(/\/?$/, "") + "/websocket";
1531
+ if (socket && socketConnected && activeUrl === url) return;
1532
+ if (connectingPromise && activeUrl === url) {
1533
+ return connectingPromise;
1534
+ }
1535
+ if (socket) {
1536
+ socket.onclose = null;
1537
+ socket.close(1e3, "Endpoint changed");
1538
+ socket = null;
1539
+ socketConnected = false;
1540
+ if (reconnectTimeout) {
1541
+ clearTimeout(reconnectTimeout);
1542
+ reconnectTimeout = null;
1543
+ }
1544
+ reconnectAttempts = 0;
1545
+ }
1546
+ activeUrl = url;
1547
+ connectingPromise = openSocket(url).finally(() => {
1548
+ connectingPromise = null;
1549
+ });
1550
+ return connectingPromise;
1551
+ };
1552
+ var subscribeToBlockchainEvents = async (rpcEndpoint, query, handler) => {
1553
+ await getOrCreateSocket(rpcEndpoint);
1554
+ const id = String(++msgId);
1555
+ activeSubscriptions.set(id, { query, handler });
1556
+ socket.send(JSON.stringify({ jsonrpc: "2.0", method: "subscribe", id, params: { query } }));
1557
+ return () => {
1558
+ activeSubscriptions.delete(id);
1559
+ if ((socket == null ? void 0 : socket.readyState) === WebSocket.OPEN) {
1560
+ socket.send(JSON.stringify({
1561
+ jsonrpc: "2.0",
1562
+ method: "unsubscribe",
1563
+ id: String(++msgId),
1564
+ params: { query }
1565
+ }));
1566
+ }
1567
+ };
1568
+ };
1569
+
1422
1570
  // src/constants/market.ts
1423
1571
  var EXCLUDED_MARKETS = {
1424
1572
  "factory/bze1f0qgels0eu96ev6a67znu70q7rquy9eragn8nw/ucorey/factory/bze13gzq40che93tgfm9kzmkpjamah5nj0j73pyhqk/uvdl": true,
@@ -5365,6 +5513,8 @@ export {
5365
5513
  getEcosystemApps,
5366
5514
  getEpochDurationByIdentifier,
5367
5515
  getEpochsInfo,
5516
+ getEventKeyValue,
5517
+ getEventMarketId,
5368
5518
  getFactoryDenomAdminAddress,
5369
5519
  getFromLocalStorage,
5370
5520
  getHardcodedLockAddress,
@@ -5390,6 +5540,7 @@ export {
5390
5540
  getMarketSellOrders,
5391
5541
  getMarkets,
5392
5542
  getMinAmount,
5543
+ getMintedAmount,
5393
5544
  getModuleAddress,
5394
5545
  getNextBurning,
5395
5546
  getNoOfIntervalsNeeded,
@@ -5423,12 +5574,19 @@ export {
5423
5574
  getWalletChainsNames,
5424
5575
  getWeekEpochInfo,
5425
5576
  intlDateFormat,
5577
+ isAddressTransfer,
5578
+ isBurnEvent,
5579
+ isCoinbaseEvent,
5580
+ isEpochStartEvent,
5426
5581
  isFactoryDenom,
5427
5582
  isIbcAsset,
5428
5583
  isIbcDenom,
5429
5584
  isLpDenom,
5430
5585
  isNativeDenom,
5586
+ isOrderBookEvent,
5587
+ isOrderExecutedEvent,
5431
5588
  isPoolSupportedByValidator,
5589
+ isSwapEvent,
5432
5590
  isTestnetChain,
5433
5591
  keplrSuggestChain,
5434
5592
  mapEventAttributes,
@@ -5452,6 +5610,7 @@ export {
5452
5610
  shortNumberFormat,
5453
5611
  sleep,
5454
5612
  stringTruncateFromCenter,
5613
+ subscribeToBlockchainEvents,
5455
5614
  toBigNumber,
5456
5615
  toPercentage,
5457
5616
  truncateAddress,