@ton/appkit 0.0.5-alpha.2 → 1.0.0-alpha.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.
Files changed (428) hide show
  1. package/.turbo/turbo-build.log +4 -4
  2. package/CHANGELOG.md +139 -0
  3. package/dist/cjs/actions/index.d.ts +8 -0
  4. package/dist/cjs/actions/index.d.ts.map +1 -1
  5. package/dist/cjs/actions/index.js +18 -2
  6. package/dist/cjs/actions/jettons/create-transfer-jetton-transaction.d.ts +1 -1
  7. package/dist/cjs/actions/jettons/create-transfer-jetton-transaction.d.ts.map +1 -1
  8. package/dist/cjs/actions/jettons/create-transfer-jetton-transaction.js +17 -5
  9. package/dist/cjs/actions/jettons/get-jetton-balance.d.ts +1 -1
  10. package/dist/cjs/actions/jettons/get-jetton-balance.d.ts.map +1 -1
  11. package/dist/cjs/actions/jettons/get-jetton-balance.js +8 -3
  12. package/dist/cjs/actions/jettons/get-jetton-info.d.ts.map +1 -1
  13. package/dist/cjs/actions/jettons/get-jetton-info.js +15 -6
  14. package/dist/cjs/actions/jettons/get-jetton-wallet-address.d.ts +1 -1
  15. package/dist/cjs/actions/jettons/get-jetton-wallet-address.d.ts.map +1 -1
  16. package/dist/cjs/actions/jettons/get-jetton-wallet-address.js +15 -4
  17. package/dist/cjs/actions/nft/get-nft.d.ts +1 -1
  18. package/dist/cjs/actions/nft/get-nft.d.ts.map +1 -1
  19. package/dist/cjs/actions/staking/get-staking-provider-metadata.d.ts +19 -0
  20. package/dist/cjs/actions/staking/get-staking-provider-metadata.d.ts.map +1 -0
  21. package/dist/cjs/actions/staking/get-staking-provider-metadata.js +18 -0
  22. package/dist/cjs/actions/staking/get-staking-provider.d.ts +21 -0
  23. package/dist/cjs/actions/staking/get-staking-provider.d.ts.map +1 -0
  24. package/dist/cjs/actions/staking/get-staking-provider.js +17 -0
  25. package/dist/cjs/actions/staking/get-staking-providers.d.ts +3 -2
  26. package/dist/cjs/actions/staking/get-staking-providers.d.ts.map +1 -1
  27. package/dist/cjs/actions/staking/get-staking-providers.js +2 -2
  28. package/dist/cjs/actions/staking/set-default-staking-provider.d.ts +18 -0
  29. package/dist/cjs/actions/staking/set-default-staking-provider.d.ts.map +1 -0
  30. package/dist/cjs/actions/staking/set-default-staking-provider.js +18 -0
  31. package/dist/cjs/actions/staking/watch-staking-providers.d.ts +17 -0
  32. package/dist/cjs/actions/staking/watch-staking-providers.d.ts.map +1 -0
  33. package/dist/cjs/actions/staking/watch-staking-providers.js +28 -0
  34. package/dist/cjs/actions/swap/get-swap-provider.d.ts +15 -0
  35. package/dist/cjs/actions/swap/get-swap-provider.d.ts.map +1 -0
  36. package/dist/cjs/actions/swap/get-swap-provider.js +14 -0
  37. package/dist/cjs/actions/swap/get-swap-providers.d.ts +15 -0
  38. package/dist/cjs/actions/swap/get-swap-providers.d.ts.map +1 -0
  39. package/dist/cjs/actions/swap/get-swap-providers.js +17 -0
  40. package/dist/cjs/actions/swap/get-swap-quote.d.ts.map +1 -1
  41. package/dist/cjs/actions/swap/get-swap-quote.js +6 -1
  42. package/dist/cjs/actions/swap/set-default-swap-provider.d.ts +18 -0
  43. package/dist/cjs/actions/swap/set-default-swap-provider.d.ts.map +1 -0
  44. package/dist/cjs/actions/swap/set-default-swap-provider.js +18 -0
  45. package/dist/cjs/actions/swap/watch-swap-providers.d.ts +17 -0
  46. package/dist/cjs/actions/swap/watch-swap-providers.d.ts.map +1 -0
  47. package/dist/cjs/actions/swap/watch-swap-providers.js +29 -0
  48. package/dist/cjs/connectors/tonconnect/connectors/ton-connect-connector.js +2 -2
  49. package/dist/cjs/core/app-kit/constants/events.d.ts +0 -6
  50. package/dist/cjs/core/app-kit/constants/events.d.ts.map +1 -1
  51. package/dist/cjs/core/app-kit/constants/events.js +1 -7
  52. package/dist/cjs/core/app-kit/index.d.ts +2 -2
  53. package/dist/cjs/core/app-kit/index.d.ts.map +1 -1
  54. package/dist/cjs/core/app-kit/index.js +1 -2
  55. package/dist/cjs/core/app-kit/services/app-kit.d.ts +3 -1
  56. package/dist/cjs/core/app-kit/services/app-kit.d.ts.map +1 -1
  57. package/dist/cjs/core/app-kit/services/app-kit.js +4 -2
  58. package/dist/cjs/core/app-kit/types/config.d.ts +4 -2
  59. package/dist/cjs/core/app-kit/types/config.d.ts.map +1 -1
  60. package/dist/cjs/core/app-kit/types/events.d.ts +2 -7
  61. package/dist/cjs/core/app-kit/types/events.d.ts.map +1 -1
  62. package/dist/cjs/core/cache/adapters/lru-app-kit-cache.d.ts +20 -0
  63. package/dist/cjs/core/cache/adapters/lru-app-kit-cache.d.ts.map +1 -0
  64. package/dist/cjs/core/cache/adapters/lru-app-kit-cache.js +32 -0
  65. package/dist/cjs/core/cache/adapters/lru-app-kit-cache.test.d.ts +9 -0
  66. package/dist/cjs/core/cache/adapters/lru-app-kit-cache.test.d.ts.map +1 -0
  67. package/dist/cjs/core/cache/adapters/lru-app-kit-cache.test.js +68 -0
  68. package/dist/cjs/core/cache/index.d.ts +11 -0
  69. package/dist/cjs/core/cache/index.d.ts.map +1 -0
  70. package/dist/cjs/core/cache/index.js +14 -0
  71. package/dist/cjs/core/cache/types/app-kit-cache.d.ts +14 -0
  72. package/dist/cjs/core/cache/types/app-kit-cache.d.ts.map +1 -0
  73. package/dist/cjs/core/cache/types/app-kit-cache.js +9 -0
  74. package/dist/cjs/core/cache/utils/get-cache-key.d.ts +9 -0
  75. package/dist/cjs/core/cache/utils/get-cache-key.d.ts.map +1 -0
  76. package/dist/cjs/core/cache/utils/get-cache-key.js +12 -0
  77. package/dist/cjs/core/cache/utils/get-cache-key.test.d.ts +9 -0
  78. package/dist/cjs/core/cache/utils/get-cache-key.test.d.ts.map +1 -0
  79. package/dist/cjs/core/cache/utils/get-cache-key.test.js +30 -0
  80. package/dist/cjs/index.d.ts +1 -0
  81. package/dist/cjs/index.d.ts.map +1 -1
  82. package/dist/cjs/index.js +1 -0
  83. package/dist/cjs/queries/balances/get-balance-by-address.d.ts.map +1 -1
  84. package/dist/cjs/queries/balances/get-balance-by-address.js +9 -3
  85. package/dist/cjs/queries/index.d.ts +0 -1
  86. package/dist/cjs/queries/index.d.ts.map +1 -1
  87. package/dist/cjs/queries/index.js +1 -3
  88. package/dist/cjs/queries/jettons/get-jetton-balance-by-address.d.ts.map +1 -1
  89. package/dist/cjs/queries/jettons/get-jetton-balance-by-address.js +11 -11
  90. package/dist/cjs/queries/jettons/get-jetton-info.d.ts.map +1 -1
  91. package/dist/cjs/queries/jettons/get-jetton-info.js +5 -1
  92. package/dist/cjs/queries/jettons/get-jetton-wallet-address.d.ts.map +1 -1
  93. package/dist/cjs/queries/jettons/get-jetton-wallet-address.js +6 -1
  94. package/dist/cjs/queries/jettons/get-jettons-by-address.d.ts.map +1 -1
  95. package/dist/cjs/queries/jettons/get-jettons-by-address.js +9 -3
  96. package/dist/cjs/queries/nft/get-nft.d.ts +1 -1
  97. package/dist/cjs/queries/nft/get-nft.d.ts.map +1 -1
  98. package/dist/cjs/queries/nft/get-nft.js +5 -1
  99. package/dist/cjs/queries/nft/get-nfts-by-address.d.ts.map +1 -1
  100. package/dist/cjs/queries/nft/get-nfts-by-address.js +5 -1
  101. package/dist/cjs/queries/staking/get-staked-balance.d.ts +1 -1
  102. package/dist/cjs/queries/staking/get-staked-balance.d.ts.map +1 -1
  103. package/dist/cjs/queries/staking/get-staked-balance.js +7 -1
  104. package/dist/cjs/queries/staking/get-staking-provider-info.d.ts +1 -1
  105. package/dist/cjs/queries/staking/get-staking-provider-info.d.ts.map +1 -1
  106. package/dist/cjs/queries/staking/get-staking-provider-info.js +3 -1
  107. package/dist/cjs/queries/staking/get-staking-quote.d.ts +1 -1
  108. package/dist/cjs/queries/staking/get-staking-quote.d.ts.map +1 -1
  109. package/dist/cjs/queries/staking/get-staking-quote.js +8 -2
  110. package/dist/cjs/queries/swap/get-swap-quote.d.ts.map +1 -1
  111. package/dist/cjs/queries/swap/get-swap-quote.js +1 -1
  112. package/dist/cjs/staking/index.d.ts +2 -2
  113. package/dist/cjs/staking/index.d.ts.map +1 -1
  114. package/dist/cjs/staking/index.js +3 -1
  115. package/dist/cjs/swap/index.d.ts +2 -1
  116. package/dist/cjs/swap/index.d.ts.map +1 -1
  117. package/dist/cjs/swap/index.js +6 -0
  118. package/dist/cjs/types/connector.d.ts +0 -1
  119. package/dist/cjs/types/connector.d.ts.map +1 -1
  120. package/dist/cjs/types/primitives.d.ts +1 -1
  121. package/dist/cjs/types/primitives.d.ts.map +1 -1
  122. package/dist/cjs/utils/address/is-valid-address.d.ts +11 -0
  123. package/dist/cjs/utils/address/is-valid-address.d.ts.map +1 -0
  124. package/dist/cjs/utils/address/is-valid-address.js +38 -0
  125. package/dist/cjs/utils/address/to-bounceble-address.d.ts +11 -0
  126. package/dist/cjs/utils/address/to-bounceble-address.d.ts.map +1 -0
  127. package/dist/cjs/utils/address/to-bounceble-address.js +36 -0
  128. package/dist/cjs/utils/address/to-non-bounceble-address.d.ts +11 -0
  129. package/dist/cjs/utils/address/to-non-bounceble-address.d.ts.map +1 -0
  130. package/dist/cjs/utils/address/to-non-bounceble-address.js +36 -0
  131. package/dist/cjs/utils/amount/calc-fiat-value.d.ts +13 -0
  132. package/dist/cjs/utils/amount/calc-fiat-value.d.ts.map +1 -0
  133. package/dist/cjs/utils/amount/calc-fiat-value.js +22 -0
  134. package/dist/cjs/utils/amount/calc-fiat-value.test.d.ts +9 -0
  135. package/dist/cjs/utils/amount/calc-fiat-value.test.d.ts.map +1 -0
  136. package/dist/cjs/utils/amount/calc-fiat-value.test.js +39 -0
  137. package/dist/cjs/utils/amount/format-large-value.d.ts +9 -0
  138. package/dist/cjs/utils/amount/format-large-value.d.ts.map +1 -0
  139. package/dist/cjs/utils/amount/format-large-value.js +40 -0
  140. package/dist/cjs/utils/amount/format-large-value.test.d.ts +9 -0
  141. package/dist/cjs/utils/amount/format-large-value.test.d.ts.map +1 -0
  142. package/dist/cjs/utils/amount/format-large-value.test.js +43 -0
  143. package/dist/cjs/utils/amount/truncate-decimals.d.ts +9 -0
  144. package/dist/cjs/utils/amount/truncate-decimals.d.ts.map +1 -0
  145. package/dist/cjs/utils/amount/truncate-decimals.js +59 -0
  146. package/dist/cjs/utils/amount/truncate-decimals.test.d.ts +9 -0
  147. package/dist/cjs/utils/amount/truncate-decimals.test.d.ts.map +1 -0
  148. package/dist/cjs/utils/amount/truncate-decimals.test.js +83 -0
  149. package/dist/cjs/utils/balance/calc-max-spendable.d.ts +33 -0
  150. package/dist/cjs/utils/balance/calc-max-spendable.d.ts.map +1 -0
  151. package/dist/cjs/utils/balance/calc-max-spendable.js +29 -0
  152. package/dist/cjs/utils/balance/calc-max-spendable.test.d.ts +9 -0
  153. package/dist/cjs/utils/balance/calc-max-spendable.test.d.ts.map +1 -0
  154. package/dist/cjs/utils/balance/calc-max-spendable.test.js +38 -0
  155. package/dist/cjs/utils/balance/get-ton-shortfall.d.ts +58 -0
  156. package/dist/cjs/utils/balance/get-ton-shortfall.d.ts.map +1 -0
  157. package/dist/cjs/utils/balance/get-ton-shortfall.js +46 -0
  158. package/dist/cjs/utils/balance/get-ton-shortfall.test.d.ts +9 -0
  159. package/dist/cjs/utils/balance/get-ton-shortfall.test.d.ts.map +1 -0
  160. package/dist/cjs/utils/balance/get-ton-shortfall.test.js +133 -0
  161. package/dist/cjs/utils/functions/debounce.d.ts +80 -0
  162. package/dist/cjs/utils/functions/debounce.d.ts.map +1 -0
  163. package/dist/cjs/utils/functions/debounce.js +107 -0
  164. package/dist/cjs/utils/index.d.ts +10 -0
  165. package/dist/cjs/utils/index.d.ts.map +1 -1
  166. package/dist/cjs/utils/index.js +10 -0
  167. package/dist/cjs/utils/jetton/jetton-info.d.ts +2 -1
  168. package/dist/cjs/utils/jetton/jetton-info.d.ts.map +1 -1
  169. package/dist/cjs/utils/jetton/jetton-info.js +11 -1
  170. package/dist/cjs/utils/jetton/jetton-info.test.d.ts +9 -0
  171. package/dist/cjs/utils/jetton/jetton-info.test.d.ts.map +1 -0
  172. package/dist/cjs/utils/jetton/jetton-info.test.js +54 -0
  173. package/dist/cjs/utils/predicate/is-number.d.ts +9 -0
  174. package/dist/cjs/utils/predicate/is-number.d.ts.map +1 -0
  175. package/dist/cjs/utils/predicate/is-number.js +20 -0
  176. package/dist/cjs/utils/predicate/is-string.d.ts +1 -1
  177. package/dist/cjs/utils/predicate/is-string.d.ts.map +1 -1
  178. package/dist/cjs/utils/predicate/is-string.js +4 -3
  179. package/dist/esm/actions/index.d.ts +8 -0
  180. package/dist/esm/actions/index.d.ts.map +1 -1
  181. package/dist/esm/actions/index.js +8 -0
  182. package/dist/esm/actions/jettons/create-transfer-jetton-transaction.d.ts +1 -1
  183. package/dist/esm/actions/jettons/create-transfer-jetton-transaction.d.ts.map +1 -1
  184. package/dist/esm/actions/jettons/create-transfer-jetton-transaction.js +17 -5
  185. package/dist/esm/actions/jettons/get-jetton-balance.d.ts +1 -1
  186. package/dist/esm/actions/jettons/get-jetton-balance.d.ts.map +1 -1
  187. package/dist/esm/actions/jettons/get-jetton-balance.js +7 -2
  188. package/dist/esm/actions/jettons/get-jetton-info.d.ts.map +1 -1
  189. package/dist/esm/actions/jettons/get-jetton-info.js +15 -6
  190. package/dist/esm/actions/jettons/get-jetton-wallet-address.d.ts +1 -1
  191. package/dist/esm/actions/jettons/get-jetton-wallet-address.d.ts.map +1 -1
  192. package/dist/esm/actions/jettons/get-jetton-wallet-address.js +15 -4
  193. package/dist/esm/actions/nft/get-nft.d.ts +1 -1
  194. package/dist/esm/actions/nft/get-nft.d.ts.map +1 -1
  195. package/dist/esm/actions/staking/get-staking-provider-metadata.d.ts +19 -0
  196. package/dist/esm/actions/staking/get-staking-provider-metadata.d.ts.map +1 -0
  197. package/dist/esm/actions/staking/get-staking-provider-metadata.js +14 -0
  198. package/dist/esm/actions/staking/get-staking-provider.d.ts +21 -0
  199. package/dist/esm/actions/staking/get-staking-provider.d.ts.map +1 -0
  200. package/dist/esm/actions/staking/get-staking-provider.js +13 -0
  201. package/dist/esm/actions/staking/get-staking-providers.d.ts +3 -2
  202. package/dist/esm/actions/staking/get-staking-providers.d.ts.map +1 -1
  203. package/dist/esm/actions/staking/get-staking-providers.js +2 -2
  204. package/dist/esm/actions/staking/set-default-staking-provider.d.ts +18 -0
  205. package/dist/esm/actions/staking/set-default-staking-provider.d.ts.map +1 -0
  206. package/dist/esm/actions/staking/set-default-staking-provider.js +14 -0
  207. package/dist/esm/actions/staking/watch-staking-providers.d.ts +17 -0
  208. package/dist/esm/actions/staking/watch-staking-providers.d.ts.map +1 -0
  209. package/dist/esm/actions/staking/watch-staking-providers.js +24 -0
  210. package/dist/esm/actions/swap/get-swap-provider.d.ts +15 -0
  211. package/dist/esm/actions/swap/get-swap-provider.d.ts.map +1 -0
  212. package/dist/esm/actions/swap/get-swap-provider.js +10 -0
  213. package/dist/esm/actions/swap/get-swap-providers.d.ts +15 -0
  214. package/dist/esm/actions/swap/get-swap-providers.d.ts.map +1 -0
  215. package/dist/esm/actions/swap/get-swap-providers.js +13 -0
  216. package/dist/esm/actions/swap/get-swap-quote.d.ts.map +1 -1
  217. package/dist/esm/actions/swap/get-swap-quote.js +6 -1
  218. package/dist/esm/actions/swap/set-default-swap-provider.d.ts +18 -0
  219. package/dist/esm/actions/swap/set-default-swap-provider.d.ts.map +1 -0
  220. package/dist/esm/actions/swap/set-default-swap-provider.js +14 -0
  221. package/dist/esm/actions/swap/watch-swap-providers.d.ts +17 -0
  222. package/dist/esm/actions/swap/watch-swap-providers.d.ts.map +1 -0
  223. package/dist/esm/actions/swap/watch-swap-providers.js +25 -0
  224. package/dist/esm/connectors/tonconnect/connectors/ton-connect-connector.js +2 -2
  225. package/dist/esm/core/app-kit/constants/events.d.ts +0 -6
  226. package/dist/esm/core/app-kit/constants/events.d.ts.map +1 -1
  227. package/dist/esm/core/app-kit/constants/events.js +0 -6
  228. package/dist/esm/core/app-kit/index.d.ts +2 -2
  229. package/dist/esm/core/app-kit/index.d.ts.map +1 -1
  230. package/dist/esm/core/app-kit/index.js +1 -1
  231. package/dist/esm/core/app-kit/services/app-kit.d.ts +3 -1
  232. package/dist/esm/core/app-kit/services/app-kit.d.ts.map +1 -1
  233. package/dist/esm/core/app-kit/services/app-kit.js +4 -2
  234. package/dist/esm/core/app-kit/types/config.d.ts +4 -2
  235. package/dist/esm/core/app-kit/types/config.d.ts.map +1 -1
  236. package/dist/esm/core/app-kit/types/events.d.ts +2 -7
  237. package/dist/esm/core/app-kit/types/events.d.ts.map +1 -1
  238. package/dist/esm/core/cache/adapters/lru-app-kit-cache.d.ts +20 -0
  239. package/dist/esm/core/cache/adapters/lru-app-kit-cache.d.ts.map +1 -0
  240. package/dist/esm/core/cache/adapters/lru-app-kit-cache.js +28 -0
  241. package/dist/esm/core/cache/adapters/lru-app-kit-cache.test.d.ts +9 -0
  242. package/dist/esm/core/cache/adapters/lru-app-kit-cache.test.d.ts.map +1 -0
  243. package/dist/esm/core/cache/adapters/lru-app-kit-cache.test.js +66 -0
  244. package/dist/esm/core/cache/index.d.ts +11 -0
  245. package/dist/esm/core/cache/index.d.ts.map +1 -0
  246. package/dist/esm/core/cache/index.js +9 -0
  247. package/dist/esm/core/cache/types/app-kit-cache.d.ts +14 -0
  248. package/dist/esm/core/cache/types/app-kit-cache.d.ts.map +1 -0
  249. package/dist/esm/core/cache/types/app-kit-cache.js +8 -0
  250. package/dist/esm/core/cache/utils/get-cache-key.d.ts +9 -0
  251. package/dist/esm/core/cache/utils/get-cache-key.d.ts.map +1 -0
  252. package/dist/esm/core/cache/utils/get-cache-key.js +8 -0
  253. package/dist/esm/core/cache/utils/get-cache-key.test.d.ts +9 -0
  254. package/dist/esm/core/cache/utils/get-cache-key.test.d.ts.map +1 -0
  255. package/dist/esm/core/cache/utils/get-cache-key.test.js +28 -0
  256. package/dist/esm/index.d.ts +1 -0
  257. package/dist/esm/index.d.ts.map +1 -1
  258. package/dist/esm/index.js +1 -0
  259. package/dist/esm/queries/balances/get-balance-by-address.d.ts.map +1 -1
  260. package/dist/esm/queries/balances/get-balance-by-address.js +10 -4
  261. package/dist/esm/queries/index.d.ts +0 -1
  262. package/dist/esm/queries/index.d.ts.map +1 -1
  263. package/dist/esm/queries/index.js +0 -1
  264. package/dist/esm/queries/jettons/get-jetton-balance-by-address.d.ts.map +1 -1
  265. package/dist/esm/queries/jettons/get-jetton-balance-by-address.js +12 -12
  266. package/dist/esm/queries/jettons/get-jetton-info.d.ts.map +1 -1
  267. package/dist/esm/queries/jettons/get-jetton-info.js +6 -2
  268. package/dist/esm/queries/jettons/get-jetton-wallet-address.d.ts.map +1 -1
  269. package/dist/esm/queries/jettons/get-jetton-wallet-address.js +7 -2
  270. package/dist/esm/queries/jettons/get-jettons-by-address.d.ts.map +1 -1
  271. package/dist/esm/queries/jettons/get-jettons-by-address.js +10 -4
  272. package/dist/esm/queries/nft/get-nft.d.ts +1 -1
  273. package/dist/esm/queries/nft/get-nft.d.ts.map +1 -1
  274. package/dist/esm/queries/nft/get-nft.js +6 -2
  275. package/dist/esm/queries/nft/get-nfts-by-address.d.ts.map +1 -1
  276. package/dist/esm/queries/nft/get-nfts-by-address.js +6 -2
  277. package/dist/esm/queries/staking/get-staked-balance.d.ts +1 -1
  278. package/dist/esm/queries/staking/get-staked-balance.d.ts.map +1 -1
  279. package/dist/esm/queries/staking/get-staked-balance.js +8 -2
  280. package/dist/esm/queries/staking/get-staking-provider-info.d.ts +1 -1
  281. package/dist/esm/queries/staking/get-staking-provider-info.d.ts.map +1 -1
  282. package/dist/esm/queries/staking/get-staking-provider-info.js +4 -2
  283. package/dist/esm/queries/staking/get-staking-quote.d.ts +1 -1
  284. package/dist/esm/queries/staking/get-staking-quote.d.ts.map +1 -1
  285. package/dist/esm/queries/staking/get-staking-quote.js +9 -3
  286. package/dist/esm/queries/swap/get-swap-quote.d.ts.map +1 -1
  287. package/dist/esm/queries/swap/get-swap-quote.js +1 -1
  288. package/dist/esm/staking/index.d.ts +2 -2
  289. package/dist/esm/staking/index.d.ts.map +1 -1
  290. package/dist/esm/staking/index.js +1 -1
  291. package/dist/esm/swap/index.d.ts +2 -1
  292. package/dist/esm/swap/index.d.ts.map +1 -1
  293. package/dist/esm/swap/index.js +1 -1
  294. package/dist/esm/types/connector.d.ts +0 -1
  295. package/dist/esm/types/connector.d.ts.map +1 -1
  296. package/dist/esm/types/primitives.d.ts +1 -1
  297. package/dist/esm/types/primitives.d.ts.map +1 -1
  298. package/dist/esm/utils/address/is-valid-address.d.ts +11 -0
  299. package/dist/esm/utils/address/is-valid-address.d.ts.map +1 -0
  300. package/dist/esm/utils/address/is-valid-address.js +33 -0
  301. package/dist/esm/utils/address/to-bounceble-address.d.ts +11 -0
  302. package/dist/esm/utils/address/to-bounceble-address.d.ts.map +1 -0
  303. package/dist/esm/utils/address/to-bounceble-address.js +31 -0
  304. package/dist/esm/utils/address/to-non-bounceble-address.d.ts +11 -0
  305. package/dist/esm/utils/address/to-non-bounceble-address.d.ts.map +1 -0
  306. package/dist/esm/utils/address/to-non-bounceble-address.js +31 -0
  307. package/dist/esm/utils/amount/calc-fiat-value.d.ts +13 -0
  308. package/dist/esm/utils/amount/calc-fiat-value.d.ts.map +1 -0
  309. package/dist/esm/utils/amount/calc-fiat-value.js +19 -0
  310. package/dist/esm/utils/amount/calc-fiat-value.test.d.ts +9 -0
  311. package/dist/esm/utils/amount/calc-fiat-value.test.d.ts.map +1 -0
  312. package/dist/esm/utils/amount/calc-fiat-value.test.js +37 -0
  313. package/dist/esm/utils/amount/format-large-value.d.ts +9 -0
  314. package/dist/esm/utils/amount/format-large-value.d.ts.map +1 -0
  315. package/dist/esm/utils/amount/format-large-value.js +36 -0
  316. package/dist/esm/utils/amount/format-large-value.test.d.ts +9 -0
  317. package/dist/esm/utils/amount/format-large-value.test.d.ts.map +1 -0
  318. package/dist/esm/utils/amount/format-large-value.test.js +41 -0
  319. package/dist/esm/utils/amount/truncate-decimals.d.ts +9 -0
  320. package/dist/esm/utils/amount/truncate-decimals.d.ts.map +1 -0
  321. package/dist/esm/utils/amount/truncate-decimals.js +55 -0
  322. package/dist/esm/utils/amount/truncate-decimals.test.d.ts +9 -0
  323. package/dist/esm/utils/amount/truncate-decimals.test.d.ts.map +1 -0
  324. package/dist/esm/utils/amount/truncate-decimals.test.js +81 -0
  325. package/dist/esm/utils/balance/calc-max-spendable.d.ts +33 -0
  326. package/dist/esm/utils/balance/calc-max-spendable.d.ts.map +1 -0
  327. package/dist/esm/utils/balance/calc-max-spendable.js +25 -0
  328. package/dist/esm/utils/balance/calc-max-spendable.test.d.ts +9 -0
  329. package/dist/esm/utils/balance/calc-max-spendable.test.d.ts.map +1 -0
  330. package/dist/esm/utils/balance/calc-max-spendable.test.js +36 -0
  331. package/dist/esm/utils/balance/get-ton-shortfall.d.ts +58 -0
  332. package/dist/esm/utils/balance/get-ton-shortfall.d.ts.map +1 -0
  333. package/dist/esm/utils/balance/get-ton-shortfall.js +42 -0
  334. package/dist/esm/utils/balance/get-ton-shortfall.test.d.ts +9 -0
  335. package/dist/esm/utils/balance/get-ton-shortfall.test.d.ts.map +1 -0
  336. package/dist/esm/utils/balance/get-ton-shortfall.test.js +131 -0
  337. package/dist/esm/utils/functions/debounce.d.ts +80 -0
  338. package/dist/esm/utils/functions/debounce.d.ts.map +1 -0
  339. package/dist/esm/utils/functions/debounce.js +103 -0
  340. package/dist/esm/utils/index.d.ts +10 -0
  341. package/dist/esm/utils/index.d.ts.map +1 -1
  342. package/dist/esm/utils/index.js +10 -0
  343. package/dist/esm/utils/jetton/jetton-info.d.ts +2 -1
  344. package/dist/esm/utils/jetton/jetton-info.d.ts.map +1 -1
  345. package/dist/esm/utils/jetton/jetton-info.js +9 -0
  346. package/dist/esm/utils/jetton/jetton-info.test.d.ts +9 -0
  347. package/dist/esm/utils/jetton/jetton-info.test.d.ts.map +1 -0
  348. package/dist/esm/utils/jetton/jetton-info.test.js +52 -0
  349. package/dist/esm/utils/predicate/is-number.d.ts +9 -0
  350. package/dist/esm/utils/predicate/is-number.d.ts.map +1 -0
  351. package/dist/esm/utils/predicate/is-number.js +16 -0
  352. package/dist/esm/utils/predicate/is-string.d.ts +1 -1
  353. package/dist/esm/utils/predicate/is-string.d.ts.map +1 -1
  354. package/dist/esm/utils/predicate/is-string.js +2 -2
  355. package/docs/actions.md +57 -9
  356. package/docs/swap.md +4 -4
  357. package/package.json +4 -3
  358. package/src/actions/index.ts +32 -0
  359. package/src/actions/jettons/create-transfer-jetton-transaction.ts +22 -6
  360. package/src/actions/jettons/get-jetton-balance.ts +10 -3
  361. package/src/actions/jettons/get-jetton-info.ts +18 -6
  362. package/src/actions/jettons/get-jetton-wallet-address.ts +19 -5
  363. package/src/actions/nft/get-nft.ts +1 -1
  364. package/src/actions/staking/get-staking-provider-metadata.ts +32 -0
  365. package/src/actions/staking/get-staking-provider.ts +30 -0
  366. package/src/actions/staking/get-staking-providers.ts +5 -3
  367. package/src/actions/staking/set-default-staking-provider.ts +26 -0
  368. package/src/actions/staking/watch-staking-providers.ts +39 -0
  369. package/src/actions/swap/get-swap-provider.ts +21 -0
  370. package/src/actions/swap/get-swap-providers.ts +20 -0
  371. package/src/actions/swap/get-swap-quote.ts +7 -1
  372. package/src/actions/swap/set-default-swap-provider.ts +26 -0
  373. package/src/actions/swap/watch-swap-providers.ts +38 -0
  374. package/src/connectors/tonconnect/connectors/ton-connect-connector.ts +2 -2
  375. package/src/core/app-kit/constants/events.ts +0 -7
  376. package/src/core/app-kit/index.ts +1 -2
  377. package/src/core/app-kit/services/app-kit.ts +7 -3
  378. package/src/core/app-kit/types/config.ts +4 -2
  379. package/src/core/app-kit/types/events.ts +2 -10
  380. package/src/core/cache/adapters/lru-app-kit-cache.test.ts +77 -0
  381. package/src/core/cache/adapters/lru-app-kit-cache.ts +38 -0
  382. package/src/core/cache/index.ts +12 -0
  383. package/src/core/cache/types/app-kit-cache.ts +14 -0
  384. package/src/core/cache/utils/get-cache-key.test.ts +34 -0
  385. package/src/core/cache/utils/get-cache-key.ts +12 -0
  386. package/src/index.ts +1 -0
  387. package/src/queries/balances/get-balance-by-address.ts +11 -4
  388. package/src/queries/index.ts +0 -6
  389. package/src/queries/jettons/get-jetton-balance-by-address.ts +13 -12
  390. package/src/queries/jettons/get-jetton-info.ts +6 -2
  391. package/src/queries/jettons/get-jetton-wallet-address.ts +7 -2
  392. package/src/queries/jettons/get-jettons-by-address.ts +18 -4
  393. package/src/queries/nft/get-nft.ts +7 -3
  394. package/src/queries/nft/get-nfts-by-address.ts +6 -2
  395. package/src/queries/staking/get-staked-balance.ts +10 -3
  396. package/src/queries/staking/get-staking-provider-info.ts +5 -2
  397. package/src/queries/staking/get-staking-quote.ts +13 -4
  398. package/src/queries/swap/get-swap-quote.ts +3 -1
  399. package/src/staking/index.ts +10 -1
  400. package/src/swap/index.ts +2 -4
  401. package/src/types/connector.ts +0 -1
  402. package/src/types/primitives.ts +1 -1
  403. package/src/utils/address/is-valid-address.ts +40 -0
  404. package/src/utils/address/to-bounceble-address.ts +35 -0
  405. package/src/utils/address/to-non-bounceble-address.ts +35 -0
  406. package/src/utils/amount/calc-fiat-value.test.ts +46 -0
  407. package/src/utils/amount/calc-fiat-value.ts +18 -0
  408. package/src/utils/amount/format-large-value.test.ts +50 -0
  409. package/src/utils/amount/format-large-value.ts +43 -0
  410. package/src/utils/amount/truncate-decimals.test.ts +97 -0
  411. package/src/utils/amount/truncate-decimals.ts +56 -0
  412. package/src/utils/balance/calc-max-spendable.test.ts +46 -0
  413. package/src/utils/balance/calc-max-spendable.ts +44 -0
  414. package/src/utils/balance/get-ton-shortfall.test.ts +148 -0
  415. package/src/utils/balance/get-ton-shortfall.ts +82 -0
  416. package/src/utils/functions/debounce.ts +169 -0
  417. package/src/utils/index.ts +10 -0
  418. package/src/utils/jetton/jetton-info.test.ts +65 -0
  419. package/src/utils/jetton/jetton-info.ts +12 -1
  420. package/src/utils/predicate/is-number.ts +19 -0
  421. package/src/utils/predicate/is-string.ts +2 -2
  422. package/dist/cjs/queries/staking/get-staking-providers.d.ts +0 -20
  423. package/dist/cjs/queries/staking/get-staking-providers.d.ts.map +0 -1
  424. package/dist/cjs/queries/staking/get-staking-providers.js +0 -25
  425. package/dist/esm/queries/staking/get-staking-providers.d.ts +0 -20
  426. package/dist/esm/queries/staking/get-staking-providers.d.ts.map +0 -1
  427. package/dist/esm/queries/staking/get-staking-providers.js +0 -20
  428. package/src/queries/staking/get-staking-providers.ts +0 -52
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) TonTech.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ *
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const vitest_1 = require("vitest");
11
+ const truncate_decimals_1 = require("./truncate-decimals");
12
+ (0, vitest_1.describe)('truncateDecimals', () => {
13
+ (0, vitest_1.it)('should return integers unchanged', () => {
14
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('123', 2)).toBe('123');
15
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('0', 5)).toBe('0');
16
+ });
17
+ (0, vitest_1.it)('should truncate decimals exceeding maxDecimals', () => {
18
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('1.23456', 2)).toBe('1.23');
19
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('1.23456', 4)).toBe('1.2345');
20
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('0.123456789', 9)).toBe('0.123456789');
21
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('0.1234567890', 9)).toBe('0.123456789');
22
+ });
23
+ (0, vitest_1.it)('should return value unchanged when decimals are within limit', () => {
24
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('1.23', 5)).toBe('1.23');
25
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('1.2', 2)).toBe('1.2');
26
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('5.0', 1)).toBe('5.0');
27
+ });
28
+ (0, vitest_1.it)('should handle maxDecimals of 0', () => {
29
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('1.999', 0)).toBe('1');
30
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('42.1', 0)).toBe('42');
31
+ });
32
+ (0, vitest_1.it)('should strip trailing dot', () => {
33
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('1.', 3)).toBe('1');
34
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('1.', 0)).toBe('1');
35
+ });
36
+ (0, vitest_1.it)('should handle empty string', () => {
37
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('', 2)).toBe('');
38
+ });
39
+ (0, vitest_1.it)('should handle number input', () => {
40
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(1.23456, 2)).toBe('1.23');
41
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(42, 3)).toBe('42');
42
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(0.1, 5)).toBe('0.1');
43
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(3.14159, 0)).toBe('3');
44
+ });
45
+ (0, vitest_1.it)('should handle scientific notation strings', () => {
46
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('1.095492908209088e-9', 9)).toBe('0.000000001');
47
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('5e-7', 9)).toBe('0.0000005');
48
+ });
49
+ (0, vitest_1.it)('should handle numbers that produce scientific notation', () => {
50
+ const tiny = 0.000000001 * 1.095492908209088; // 1.095492908209088e-9
51
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(tiny, 9)).toBe('0.000000001');
52
+ });
53
+ (0, vitest_1.it)('should handle negative numbers and negative scientific notation', () => {
54
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('-1.23456', 2)).toBe('-1.23');
55
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('-5e-7', 9)).toBe('-0.0000005');
56
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(-0.0000005, 9)).toBe('-0.0000005');
57
+ });
58
+ (0, vitest_1.it)('should handle scientific notation with positive exponents', () => {
59
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('1.2345e2', 2)).toBe('123.45');
60
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('1.2345e+2', 0)).toBe('123');
61
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('5.2e9', 0)).toBe('5200000000');
62
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('-1.23e+9', 0)).toBe('-1230000000');
63
+ });
64
+ (0, vitest_1.it)('should handle extreme scientific exponents', () => {
65
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('1e-100', 100)).toBe('0.' + '0'.repeat(99) + '1');
66
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('1e21', 0)).toBe('1000000000000000000000');
67
+ });
68
+ (0, vitest_1.it)('should handle NaN and Infinity', () => {
69
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(NaN, 2)).toBe('NaN');
70
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(Infinity, 2)).toBe('Infinity');
71
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(-Infinity, 2)).toBe('-Infinity');
72
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('NaN', 2)).toBe('NaN');
73
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)('Infinity', 2)).toBe('Infinity');
74
+ });
75
+ (0, vitest_1.it)('should handle native numbers with e (scientific notation)', () => {
76
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(1e-10, 10)).toBe('0.0000000001');
77
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(1.23e-8, 10)).toBe('0.0000000123');
78
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(-5.43e-7, 9)).toBe('-0.000000543');
79
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(5.2e9, 0)).toBe('5200000000');
80
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(-1.23e9, 0)).toBe('-1230000000');
81
+ (0, vitest_1.expect)((0, truncate_decimals_1.truncateDecimals)(1e21, 0)).toBe('1000000000000000000000');
82
+ });
83
+ });
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Copyright (c) TonTech.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ /**
9
+ * Default TON reserve subtracted from balance when computing the max spendable amount
10
+ * for a native TON operation — leaves room for network fees. 0.1 TON.
11
+ */
12
+ export declare const DEFAULT_TON_FEE_RESERVE_NANOS = 100000000n;
13
+ export interface CalcMaxSpendableParams {
14
+ /** User's balance of `token`, as a decimal string. */
15
+ balance: string;
16
+ /** The token the user is going to spend. */
17
+ token: {
18
+ address: string;
19
+ decimals: number;
20
+ };
21
+ /**
22
+ * TON reserve (in nanos) subtracted from balance when the token is native TON.
23
+ * Ignored for jettons. Defaults to {@link DEFAULT_TON_FEE_RESERVE_NANOS}.
24
+ */
25
+ feeReserveNanos?: bigint;
26
+ }
27
+ /**
28
+ * Compute the max spendable amount a user can place into an input when they click MAX.
29
+ * For native TON — subtracts a fixed reserve so the user still has room for network fees.
30
+ * For jettons — returns the full balance (gas is paid from TON separately).
31
+ */
32
+ export declare const calcMaxSpendable: ({ balance, token, feeReserveNanos, }: CalcMaxSpendableParams) => string;
33
+ //# sourceMappingURL=calc-max-spendable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"calc-max-spendable.d.ts","sourceRoot":"","sources":["../../../../src/utils/balance/calc-max-spendable.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH;;;GAGG;AACH,eAAO,MAAM,6BAA6B,aAAe,CAAC;AAE1D,MAAM,WAAW,sBAAsB;IACnC,sDAAsD;IACtD,OAAO,EAAE,MAAM,CAAC;IAChB,4CAA4C;IAC5C,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IAC7C;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,GAAI,sCAI9B,sBAAsB,KAAG,MAM3B,CAAC"}
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) TonTech.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ *
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.calcMaxSpendable = exports.DEFAULT_TON_FEE_RESERVE_NANOS = void 0;
11
+ const walletkit_1 = require("@ton/walletkit");
12
+ /**
13
+ * Default TON reserve subtracted from balance when computing the max spendable amount
14
+ * for a native TON operation — leaves room for network fees. 0.1 TON.
15
+ */
16
+ exports.DEFAULT_TON_FEE_RESERVE_NANOS = 100000000n;
17
+ /**
18
+ * Compute the max spendable amount a user can place into an input when they click MAX.
19
+ * For native TON — subtracts a fixed reserve so the user still has room for network fees.
20
+ * For jettons — returns the full balance (gas is paid from TON separately).
21
+ */
22
+ const calcMaxSpendable = ({ balance, token, feeReserveNanos = exports.DEFAULT_TON_FEE_RESERVE_NANOS, }) => {
23
+ if (token.address !== 'ton')
24
+ return balance;
25
+ const balanceNanos = (0, walletkit_1.parseUnits)(balance, token.decimals);
26
+ const reducedNanos = balanceNanos > feeReserveNanos ? balanceNanos - feeReserveNanos : 0n;
27
+ return (0, walletkit_1.formatUnits)(reducedNanos, token.decimals);
28
+ };
29
+ exports.calcMaxSpendable = calcMaxSpendable;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Copyright (c) TonTech.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ export {};
9
+ //# sourceMappingURL=calc-max-spendable.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"calc-max-spendable.test.d.ts","sourceRoot":"","sources":["../../../../src/utils/balance/calc-max-spendable.test.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) TonTech.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ *
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const vitest_1 = require("vitest");
11
+ const calc_max_spendable_1 = require("./calc-max-spendable");
12
+ const TON = { address: 'ton', decimals: 9 };
13
+ const JETTON = { address: 'EQA_jetton', decimals: 6 };
14
+ (0, vitest_1.describe)('calcMaxSpendable', () => {
15
+ (0, vitest_1.it)('returns balance minus the TON fee reserve for native TON', () => {
16
+ // 1 TON balance, default reserve 0.1 TON → 0.9 TON spendable.
17
+ (0, vitest_1.expect)((0, calc_max_spendable_1.calcMaxSpendable)({ balance: '1', token: TON })).toBe('0.9');
18
+ });
19
+ (0, vitest_1.it)('returns the full balance for a jetton (no reserve)', () => {
20
+ (0, vitest_1.expect)((0, calc_max_spendable_1.calcMaxSpendable)({ balance: '100', token: JETTON })).toBe('100');
21
+ });
22
+ (0, vitest_1.it)('clamps to zero when balance is below the reserve', () => {
23
+ (0, vitest_1.expect)((0, calc_max_spendable_1.calcMaxSpendable)({ balance: '0.05', token: TON })).toBe('0');
24
+ });
25
+ (0, vitest_1.it)('clamps to zero when balance equals the reserve', () => {
26
+ (0, vitest_1.expect)((0, calc_max_spendable_1.calcMaxSpendable)({ balance: '0.1', token: TON })).toBe('0');
27
+ });
28
+ (0, vitest_1.it)('respects a custom feeReserveNanos', () => {
29
+ // 1 TON balance, 0.5 TON reserve → 0.5 TON spendable.
30
+ (0, vitest_1.expect)((0, calc_max_spendable_1.calcMaxSpendable)({ balance: '1', token: TON, feeReserveNanos: 500000000n })).toBe('0.5');
31
+ });
32
+ (0, vitest_1.it)('ignores feeReserveNanos for jettons', () => {
33
+ (0, vitest_1.expect)((0, calc_max_spendable_1.calcMaxSpendable)({ balance: '100', token: JETTON, feeReserveNanos: 999999999999n })).toBe('100');
34
+ });
35
+ (0, vitest_1.it)('exports a sane default reserve', () => {
36
+ (0, vitest_1.expect)(calc_max_spendable_1.DEFAULT_TON_FEE_RESERVE_NANOS).toBe(100000000n);
37
+ });
38
+ });
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Copyright (c) TonTech.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ /** Default extra TON buffer added on top of built transaction outflow when checking balance before sending. */
9
+ export declare const DEFAULT_GAS_BUFFER_NANOS = 100000000n;
10
+ /**
11
+ * Default extra headroom baked into the suggested reduced amount, on top of the gas buffer.
12
+ * Covers quote/gas drift between the current built tx and the one that will actually be sent
13
+ * after the amount changes. 0.02 TON.
14
+ */
15
+ export declare const DEFAULT_SAFETY_MARGIN_NANOS = 20000000n;
16
+ export type TonShortfall = {
17
+ mode: 'reduce';
18
+ requiredNanos: bigint;
19
+ suggestedFromAmount: string;
20
+ } | {
21
+ mode: 'topup';
22
+ requiredNanos: bigint;
23
+ suggestedFromAmount: '';
24
+ };
25
+ export interface GetTonShortfallParams {
26
+ /** Messages of the built transaction. Each `amount` is the TON value in nanos attached to the message. */
27
+ messages: Array<{
28
+ amount: string;
29
+ }>;
30
+ /** User's current TON balance as a decimal string (same format `formatUnits(balance, 9)` returns). */
31
+ tonBalance: string | undefined;
32
+ /** The outgoing token — `{ address: 'ton' }` when the user spends TON, otherwise a jetton. */
33
+ fromToken: {
34
+ address: string;
35
+ };
36
+ /** Amount the user intends to spend in `fromToken` units, as a decimal string. */
37
+ fromAmount: string;
38
+ /**
39
+ * Extra TON headroom on top of built-tx outflow when checking balance before sending.
40
+ * Defaults to {@link DEFAULT_GAS_BUFFER_NANOS}.
41
+ */
42
+ gasBufferNanos?: bigint;
43
+ /**
44
+ * Extra headroom baked into the suggested reduced amount, on top of the gas buffer.
45
+ * Defaults to {@link DEFAULT_SAFETY_MARGIN_NANOS}.
46
+ */
47
+ safetyMarginNanos?: bigint;
48
+ }
49
+ /**
50
+ * Check whether the user's TON balance covers the built transaction.
51
+ * - Returns `null` if the balance is sufficient.
52
+ * - Returns `{ mode: 'reduce', ... }` with a smaller suggested `fromAmount` when `fromToken` is TON
53
+ * and the balance is enough to cover at least gas (user can fix it by reducing the amount).
54
+ * - Returns `{ mode: 'topup', ... }` when `fromToken` is a jetton (reducing jetton amount won't free up TON gas),
55
+ * or when `fromToken` is TON but the balance can't even cover gas (reducing won't help — user must top up).
56
+ */
57
+ export declare const getTonShortfall: ({ messages, tonBalance, fromToken, fromAmount, gasBufferNanos, safetyMarginNanos, }: GetTonShortfallParams) => TonShortfall | undefined;
58
+ //# sourceMappingURL=get-ton-shortfall.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-ton-shortfall.d.ts","sourceRoot":"","sources":["../../../../src/utils/balance/get-ton-shortfall.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,+GAA+G;AAC/G,eAAO,MAAM,wBAAwB,aAAe,CAAC;AAErD;;;;GAIG;AACH,eAAO,MAAM,2BAA2B,YAAc,CAAC;AAEvD,MAAM,MAAM,YAAY,GAClB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,aAAa,EAAE,MAAM,CAAC;IAAC,mBAAmB,EAAE,MAAM,CAAA;CAAE,GACtE;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,aAAa,EAAE,MAAM,CAAC;IAAC,mBAAmB,EAAE,EAAE,CAAA;CAAE,CAAC;AAExE,MAAM,WAAW,qBAAqB;IAClC,0GAA0G;IAC1G,QAAQ,EAAE,KAAK,CAAC;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACpC,sGAAsG;IACtG,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B,8FAA8F;IAC9F,SAAS,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAC/B,kFAAkF;IAClF,UAAU,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAAI,qFAO7B,qBAAqB,KAAG,YAAY,GAAG,SAqBzC,CAAC"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) TonTech.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ *
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.getTonShortfall = exports.DEFAULT_SAFETY_MARGIN_NANOS = exports.DEFAULT_GAS_BUFFER_NANOS = void 0;
11
+ const walletkit_1 = require("@ton/walletkit");
12
+ /** Default extra TON buffer added on top of built transaction outflow when checking balance before sending. */
13
+ exports.DEFAULT_GAS_BUFFER_NANOS = 100000000n;
14
+ /**
15
+ * Default extra headroom baked into the suggested reduced amount, on top of the gas buffer.
16
+ * Covers quote/gas drift between the current built tx and the one that will actually be sent
17
+ * after the amount changes. 0.02 TON.
18
+ */
19
+ exports.DEFAULT_SAFETY_MARGIN_NANOS = 20000000n;
20
+ /**
21
+ * Check whether the user's TON balance covers the built transaction.
22
+ * - Returns `null` if the balance is sufficient.
23
+ * - Returns `{ mode: 'reduce', ... }` with a smaller suggested `fromAmount` when `fromToken` is TON
24
+ * and the balance is enough to cover at least gas (user can fix it by reducing the amount).
25
+ * - Returns `{ mode: 'topup', ... }` when `fromToken` is a jetton (reducing jetton amount won't free up TON gas),
26
+ * or when `fromToken` is TON but the balance can't even cover gas (reducing won't help — user must top up).
27
+ */
28
+ const getTonShortfall = ({ messages, tonBalance, fromToken, fromAmount, gasBufferNanos = exports.DEFAULT_GAS_BUFFER_NANOS, safetyMarginNanos = exports.DEFAULT_SAFETY_MARGIN_NANOS, }) => {
29
+ const totalOutNanos = messages.reduce((acc, m) => acc + BigInt(m.amount), 0n);
30
+ const requiredNanos = totalOutNanos + gasBufferNanos;
31
+ const tonBalanceNanos = tonBalance ? (0, walletkit_1.parseUnits)(tonBalance, 9) : 0n;
32
+ if (tonBalanceNanos >= requiredNanos)
33
+ return;
34
+ if (fromToken.address === 'ton') {
35
+ const gasOnlyNanos = totalOutNanos - (0, walletkit_1.parseUnits)(fromAmount, 9);
36
+ const nonSwapReservedNanos = gasOnlyNanos + gasBufferNanos + safetyMarginNanos;
37
+ // Balance can't cover even gas for a minimal outgoing tx — reducing the amount won't help.
38
+ if (tonBalanceNanos <= nonSwapReservedNanos) {
39
+ return { mode: 'topup', requiredNanos, suggestedFromAmount: '' };
40
+ }
41
+ const suggestedFromAmount = (0, walletkit_1.formatUnits)(tonBalanceNanos - nonSwapReservedNanos, 9);
42
+ return { mode: 'reduce', requiredNanos, suggestedFromAmount };
43
+ }
44
+ return { mode: 'topup', requiredNanos, suggestedFromAmount: '' };
45
+ };
46
+ exports.getTonShortfall = getTonShortfall;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Copyright (c) TonTech.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ export {};
9
+ //# sourceMappingURL=get-ton-shortfall.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-ton-shortfall.test.d.ts","sourceRoot":"","sources":["../../../../src/utils/balance/get-ton-shortfall.test.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG"}
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) TonTech.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ *
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const vitest_1 = require("vitest");
11
+ const get_ton_shortfall_1 = require("./get-ton-shortfall");
12
+ const TON = { address: 'ton' };
13
+ const JETTON = { address: 'EQA_jetton_address' };
14
+ // Helper — convert a decimal string of TON to nanos as a string (for message amounts).
15
+ const toNanos = (ton) => BigInt(Math.round(Number(ton) * 1e9)).toString();
16
+ (0, vitest_1.describe)('getTonShortfall', () => {
17
+ (0, vitest_1.it)('returns undefined when TON balance covers the built transaction', () => {
18
+ // Swap 1 TON, total tx outflow 1.3 TON (1 TON swap + 0.3 TON attached gas), balance 2 TON.
19
+ const result = (0, get_ton_shortfall_1.getTonShortfall)({
20
+ messages: [{ amount: toNanos('1.3') }],
21
+ tonBalance: '2',
22
+ fromToken: TON,
23
+ fromAmount: '1',
24
+ });
25
+ (0, vitest_1.expect)(result).toBeUndefined();
26
+ });
27
+ (0, vitest_1.it)('returns topup when fromToken is a jetton and TON balance is below outflow + buffer', () => {
28
+ // Jetton swap, tx needs 0.05 TON for gas, user has 0 TON.
29
+ const result = (0, get_ton_shortfall_1.getTonShortfall)({
30
+ messages: [{ amount: toNanos('0.05') }],
31
+ tonBalance: '0',
32
+ fromToken: JETTON,
33
+ fromAmount: '100',
34
+ });
35
+ (0, vitest_1.expect)(result).toEqual({
36
+ mode: 'topup',
37
+ requiredNanos: 50000000n + get_ton_shortfall_1.DEFAULT_GAS_BUFFER_NANOS,
38
+ suggestedFromAmount: '',
39
+ });
40
+ });
41
+ (0, vitest_1.it)('returns reduce for TON-from when balance covers gas but not full amount', () => {
42
+ // User wants 1 TON swap, total outflow 1.3 TON, has 1 TON. Gas only = 0.3 TON.
43
+ // Balance (1) > nonSwapReserved (0.3 gas + 0.1 buffer + 0.02 safety = 0.42) → reduce mode.
44
+ const result = (0, get_ton_shortfall_1.getTonShortfall)({
45
+ messages: [{ amount: toNanos('1.3') }],
46
+ tonBalance: '1',
47
+ fromToken: TON,
48
+ fromAmount: '1',
49
+ });
50
+ (0, vitest_1.expect)(result?.mode).toBe('reduce');
51
+ if (result?.mode !== 'reduce')
52
+ return;
53
+ // Suggested = 1 - 0.42 = 0.58
54
+ (0, vitest_1.expect)(result.suggestedFromAmount).toBe('0.58');
55
+ (0, vitest_1.expect)(result.requiredNanos).toBe(1300000000n + get_ton_shortfall_1.DEFAULT_GAS_BUFFER_NANOS);
56
+ });
57
+ (0, vitest_1.it)('returns topup for TON-from when balance cannot cover even gas (reducing would not help)', () => {
58
+ // User wants 1 TON swap, total outflow 1.3 TON → gas = 0.3 TON. Balance 0.2 TON.
59
+ // nonSwapReserved = 0.3 + 0.1 buffer + 0.02 safety = 0.42 TON → balance (0.2) <= 0.42 → topup.
60
+ const result = (0, get_ton_shortfall_1.getTonShortfall)({
61
+ messages: [{ amount: toNanos('1.3') }],
62
+ tonBalance: '0.2',
63
+ fromToken: TON,
64
+ fromAmount: '1',
65
+ });
66
+ (0, vitest_1.expect)(result).toEqual({
67
+ mode: 'topup',
68
+ requiredNanos: 1300000000n + get_ton_shortfall_1.DEFAULT_GAS_BUFFER_NANOS,
69
+ suggestedFromAmount: '',
70
+ });
71
+ });
72
+ (0, vitest_1.it)('treats an undefined tonBalance as zero', () => {
73
+ const result = (0, get_ton_shortfall_1.getTonShortfall)({
74
+ messages: [{ amount: toNanos('0.05') }],
75
+ tonBalance: undefined,
76
+ fromToken: JETTON,
77
+ fromAmount: '100',
78
+ });
79
+ (0, vitest_1.expect)(result?.mode).toBe('topup');
80
+ });
81
+ (0, vitest_1.it)('sums amounts across multiple messages', () => {
82
+ // Two messages attaching 0.6 TON each → totalOut = 1.2 TON. Balance 1 TON → shortfall.
83
+ const result = (0, get_ton_shortfall_1.getTonShortfall)({
84
+ messages: [{ amount: toNanos('0.6') }, { amount: toNanos('0.6') }],
85
+ tonBalance: '1',
86
+ fromToken: JETTON,
87
+ fromAmount: '100',
88
+ });
89
+ (0, vitest_1.expect)(result?.mode).toBe('topup');
90
+ (0, vitest_1.expect)(result?.requiredNanos).toBe(1200000000n + get_ton_shortfall_1.DEFAULT_GAS_BUFFER_NANOS);
91
+ });
92
+ (0, vitest_1.it)('respects a custom gasBufferNanos', () => {
93
+ const baseParams = {
94
+ messages: [{ amount: toNanos('1.3') }],
95
+ tonBalance: '1.3',
96
+ fromToken: TON,
97
+ fromAmount: '1',
98
+ };
99
+ // With the default buffer (0.1 TON), balance 1.3 is insufficient (needs 1.4).
100
+ (0, vitest_1.expect)((0, get_ton_shortfall_1.getTonShortfall)(baseParams)?.mode).toBe('reduce');
101
+ // With a zero buffer, 1.3 TON balance covers the 1.3 TON outflow exactly.
102
+ (0, vitest_1.expect)((0, get_ton_shortfall_1.getTonShortfall)({ ...baseParams, gasBufferNanos: 0n })).toBeUndefined();
103
+ });
104
+ (0, vitest_1.it)('respects a custom safetyMarginNanos in the reduce branch', () => {
105
+ // Default margin: suggested = balance - gas - buffer - 0.02 = 1 - 0.3 - 0.1 - 0.02 = 0.58
106
+ // Zero margin: suggested = balance - gas - buffer = 1 - 0.3 - 0.1 = 0.6
107
+ const baseParams = {
108
+ messages: [{ amount: toNanos('1.3') }],
109
+ tonBalance: '1',
110
+ fromToken: TON,
111
+ fromAmount: '1',
112
+ };
113
+ const withDefault = (0, get_ton_shortfall_1.getTonShortfall)(baseParams);
114
+ (0, vitest_1.expect)(withDefault?.mode === 'reduce' ? withDefault.suggestedFromAmount : null).toBe('0.58');
115
+ const withZero = (0, get_ton_shortfall_1.getTonShortfall)({ ...baseParams, safetyMarginNanos: 0n });
116
+ (0, vitest_1.expect)(withZero?.mode === 'reduce' ? withZero.suggestedFromAmount : null).toBe('0.6');
117
+ });
118
+ (0, vitest_1.it)('flips from reduce to topup when a large custom safetyMarginNanos eats the balance', () => {
119
+ // Balance 1 TON, gas 0.3 TON, buffer 0.1 TON. With a 1 TON safety margin, nonSwapReserved becomes 1.4 TON → topup.
120
+ const result = (0, get_ton_shortfall_1.getTonShortfall)({
121
+ messages: [{ amount: toNanos('1.3') }],
122
+ tonBalance: '1',
123
+ fromToken: TON,
124
+ fromAmount: '1',
125
+ safetyMarginNanos: 1000000000n,
126
+ });
127
+ (0, vitest_1.expect)(result?.mode).toBe('topup');
128
+ });
129
+ (0, vitest_1.it)('exports sane default constants', () => {
130
+ (0, vitest_1.expect)(get_ton_shortfall_1.DEFAULT_GAS_BUFFER_NANOS).toBe(100000000n);
131
+ (0, vitest_1.expect)(get_ton_shortfall_1.DEFAULT_SAFETY_MARGIN_NANOS).toBe(20000000n);
132
+ });
133
+ });
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Copyright (c) TonTech.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ export interface DebounceOptions {
9
+ /**
10
+ * An optional AbortSignal to cancel the debounced function.
11
+ */
12
+ signal?: AbortSignal;
13
+ /**
14
+ * An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
15
+ * If `edges` include "leading", the function will be invoked at the start of the delay period.
16
+ * If `edges` include "trailing", the function will be invoked at the end of the delay period.
17
+ * If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
18
+ * @default ["trailing"]
19
+ */
20
+ edges?: Array<'leading' | 'trailing'>;
21
+ }
22
+ export interface DebouncedFunction<F extends (...args: unknown[]) => void> {
23
+ (...args: Parameters<F>): void;
24
+ /**
25
+ * Schedules the execution of the debounced function after the specified debounced delay.
26
+ * This method resets any existing timer, ensuring that the function is only invoked
27
+ * after the delay has elapsed since the last call to the debounced function.
28
+ * It is typically called internally whenever the debounced function is invoked.
29
+ *
30
+ * @returns {void}
31
+ */
32
+ schedule: () => void;
33
+ /**
34
+ * Cancels any pending execution of the debounced function.
35
+ * This method clears the active timer and resets any stored context or arguments.
36
+ */
37
+ cancel: () => void;
38
+ /**
39
+ * Immediately invokes the debounced function if there is a pending execution.
40
+ * This method executes the function right away if there is a pending execution.
41
+ */
42
+ flush: () => void;
43
+ }
44
+ /**
45
+ * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
46
+ * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
47
+ * method to cancel any pending execution.
48
+ *
49
+ * @template F - The type of function.
50
+ * @param {F} func - The function to debounce.
51
+ * @param {number} debounceMs - The number of milliseconds to delay.
52
+ * @param {DebounceOptions} options - The options object
53
+ * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
54
+ * @returns A new debounced function with a `cancel` method.
55
+ *
56
+ * @example
57
+ * const debouncedFunction = debounce(() => {
58
+ * console.log('Function executed');
59
+ * }, 1000);
60
+ *
61
+ * // Will log 'Function executed' after 1 second if not called again in that time
62
+ * debouncedFunction();
63
+ *
64
+ * // Will not log anything as the previous call is canceled
65
+ * debouncedFunction.cancel();
66
+ *
67
+ * // With AbortSignal
68
+ * const controller = new AbortController();
69
+ * const signal = controller.signal;
70
+ * const debouncedWithSignal = debounce(() => {
71
+ * console.log('Function executed');
72
+ * }, 1000, { signal });
73
+ *
74
+ * debouncedWithSignal();
75
+ *
76
+ * // Will cancel the debounced function call
77
+ * controller.abort();
78
+ */
79
+ export declare const debounce: <F extends (...args: unknown[]) => void>(func: F, debounceMs: number, { signal, edges }?: DebounceOptions) => DebouncedFunction<F>;
80
+ //# sourceMappingURL=debounce.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debounce.d.ts","sourceRoot":"","sources":["../../../../src/utils/functions/debounce.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,WAAW,eAAe;IAC5B;;OAEG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;IAErB;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,KAAK,CAAC,SAAS,GAAG,UAAU,CAAC,CAAC;CACzC;AAED,MAAM,WAAW,iBAAiB,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI;IACrE,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IAE/B;;;;;;;OAOG;IACH,QAAQ,EAAE,MAAM,IAAI,CAAC;IAErB;;;OAGG;IACH,MAAM,EAAE,MAAM,IAAI,CAAC;IAEnB;;;OAGG;IACH,KAAK,EAAE,MAAM,IAAI,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,eAAO,MAAM,QAAQ,GAAI,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,EAC3D,MAAM,CAAC,EACP,YAAY,MAAM,EAClB,oBAAmB,eAAoB,KACxC,iBAAiB,CAAC,CAAC,CA+ErB,CAAC"}
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) TonTech.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ *
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.debounce = void 0;
11
+ /**
12
+ * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
13
+ * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
14
+ * method to cancel any pending execution.
15
+ *
16
+ * @template F - The type of function.
17
+ * @param {F} func - The function to debounce.
18
+ * @param {number} debounceMs - The number of milliseconds to delay.
19
+ * @param {DebounceOptions} options - The options object
20
+ * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
21
+ * @returns A new debounced function with a `cancel` method.
22
+ *
23
+ * @example
24
+ * const debouncedFunction = debounce(() => {
25
+ * console.log('Function executed');
26
+ * }, 1000);
27
+ *
28
+ * // Will log 'Function executed' after 1 second if not called again in that time
29
+ * debouncedFunction();
30
+ *
31
+ * // Will not log anything as the previous call is canceled
32
+ * debouncedFunction.cancel();
33
+ *
34
+ * // With AbortSignal
35
+ * const controller = new AbortController();
36
+ * const signal = controller.signal;
37
+ * const debouncedWithSignal = debounce(() => {
38
+ * console.log('Function executed');
39
+ * }, 1000, { signal });
40
+ *
41
+ * debouncedWithSignal();
42
+ *
43
+ * // Will cancel the debounced function call
44
+ * controller.abort();
45
+ */
46
+ const debounce = (func, debounceMs, { signal, edges } = {}) => {
47
+ let pendingThis = undefined;
48
+ let pendingArgs = null;
49
+ const leading = edges != null && edges.includes('leading');
50
+ const trailing = edges == null || edges.includes('trailing');
51
+ const invoke = () => {
52
+ if (pendingArgs !== null) {
53
+ func.apply(pendingThis, pendingArgs);
54
+ pendingThis = undefined;
55
+ pendingArgs = null;
56
+ }
57
+ };
58
+ const onTimerEnd = () => {
59
+ if (trailing) {
60
+ invoke();
61
+ }
62
+ cancel();
63
+ };
64
+ let timeoutId = null;
65
+ const schedule = () => {
66
+ if (timeoutId != null) {
67
+ clearTimeout(timeoutId);
68
+ }
69
+ timeoutId = setTimeout(() => {
70
+ timeoutId = null;
71
+ onTimerEnd();
72
+ }, debounceMs);
73
+ };
74
+ const cancelTimer = () => {
75
+ if (timeoutId !== null) {
76
+ clearTimeout(timeoutId);
77
+ timeoutId = null;
78
+ }
79
+ };
80
+ const cancel = () => {
81
+ cancelTimer();
82
+ pendingThis = undefined;
83
+ pendingArgs = null;
84
+ };
85
+ const flush = () => {
86
+ invoke();
87
+ };
88
+ const debounced = function (...args) {
89
+ if (signal?.aborted) {
90
+ return;
91
+ }
92
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
93
+ pendingThis = this;
94
+ pendingArgs = args;
95
+ const isFirstCall = timeoutId == null;
96
+ schedule();
97
+ if (leading && isFirstCall) {
98
+ invoke();
99
+ }
100
+ };
101
+ debounced.schedule = schedule;
102
+ debounced.cancel = cancel;
103
+ debounced.flush = flush;
104
+ signal?.addEventListener('abort', cancel, { once: true });
105
+ return debounced;
106
+ };
107
+ exports.debounce = debounce;