@mysten/sui 2.39.1 → 3.0.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 (427) hide show
  1. package/CHANGELOG.md +160 -0
  2. package/README.md +5 -188
  3. package/dist/bcs/bcs.d.mts +6 -6
  4. package/dist/bcs/bcs.d.mts.map +1 -1
  5. package/dist/bcs/bcs.mjs +49 -22
  6. package/dist/bcs/bcs.mjs.map +1 -1
  7. package/dist/bcs/effects.mjs +49 -20
  8. package/dist/bcs/effects.mjs.map +1 -1
  9. package/dist/bcs/index.d.mts +1441 -624
  10. package/dist/bcs/index.d.mts.map +1 -1
  11. package/dist/bcs/index.mjs +2 -1
  12. package/dist/bcs/index.mjs.map +1 -1
  13. package/dist/bcs/transactions.d.mts +1059 -0
  14. package/dist/bcs/transactions.d.mts.map +1 -0
  15. package/dist/bcs/transactions.mjs +33 -14
  16. package/dist/bcs/transactions.mjs.map +1 -1
  17. package/dist/bcs/types.d.mts +6 -1
  18. package/dist/bcs/types.d.mts.map +1 -1
  19. package/dist/client/core-resolver.mjs +4 -4
  20. package/dist/client/core-resolver.mjs.map +1 -1
  21. package/dist/client/core.d.mts +55 -4
  22. package/dist/client/core.d.mts.map +1 -1
  23. package/dist/client/core.mjs +98 -1
  24. package/dist/client/core.mjs.map +1 -1
  25. package/dist/client/errors.mjs +11 -1
  26. package/dist/client/errors.mjs.map +1 -1
  27. package/dist/client/index.d.mts +4 -2
  28. package/dist/client/index.mjs +5 -2
  29. package/dist/client/mvr.d.mts +12 -2
  30. package/dist/client/mvr.d.mts.map +1 -1
  31. package/dist/client/mvr.mjs +34 -13
  32. package/dist/client/mvr.mjs.map +1 -1
  33. package/dist/client/query-filters.mjs +80 -76
  34. package/dist/client/query-filters.mjs.map +1 -1
  35. package/dist/client/stream/adapter.d.mts +102 -0
  36. package/dist/client/stream/adapter.d.mts.map +1 -0
  37. package/dist/client/stream/errors.d.mts +14 -0
  38. package/dist/client/stream/errors.d.mts.map +1 -0
  39. package/dist/client/stream/errors.mjs +25 -0
  40. package/dist/client/stream/errors.mjs.map +1 -0
  41. package/dist/client/stream/filter.mjs +18 -0
  42. package/dist/client/stream/filter.mjs.map +1 -0
  43. package/dist/client/stream/follow.mjs +48 -0
  44. package/dist/client/stream/follow.mjs.map +1 -0
  45. package/dist/client/stream/index.d.mts +4 -0
  46. package/dist/client/stream/pager.mjs +123 -0
  47. package/dist/client/stream/pager.mjs.map +1 -0
  48. package/dist/client/stream/plan.mjs +79 -0
  49. package/dist/client/stream/plan.mjs.map +1 -0
  50. package/dist/client/stream/position.d.mts +30 -0
  51. package/dist/client/stream/position.d.mts.map +1 -0
  52. package/dist/client/stream/position.mjs +121 -0
  53. package/dist/client/stream/position.mjs.map +1 -0
  54. package/dist/client/stream/run.d.mts +2 -0
  55. package/dist/client/stream/run.mjs +99 -0
  56. package/dist/client/stream/run.mjs.map +1 -0
  57. package/dist/client/stream/session.mjs +205 -0
  58. package/dist/client/stream/session.mjs.map +1 -0
  59. package/dist/client/stream/status.mjs +47 -0
  60. package/dist/client/stream/status.mjs.map +1 -0
  61. package/dist/client/stream/token.mjs +80 -0
  62. package/dist/client/stream/token.mjs.map +1 -0
  63. package/dist/client/transaction-data.d.mts +12 -0
  64. package/dist/client/transaction-data.d.mts.map +1 -0
  65. package/dist/client/transaction-data.mjs +469 -0
  66. package/dist/client/transaction-data.mjs.map +1 -0
  67. package/dist/client/transaction-resolver.mjs +102 -58
  68. package/dist/client/transaction-resolver.mjs.map +1 -1
  69. package/dist/client/types.d.mts +560 -240
  70. package/dist/client/types.d.mts.map +1 -1
  71. package/dist/client/utils.d.mts +3 -1
  72. package/dist/client/utils.d.mts.map +1 -1
  73. package/dist/client/utils.mjs +91 -26
  74. package/dist/client/utils.mjs.map +1 -1
  75. package/dist/cryptography/index.d.mts +1 -1
  76. package/dist/cryptography/keypair.d.mts +1 -1
  77. package/dist/cryptography/publickey.d.mts +11 -0
  78. package/dist/cryptography/publickey.d.mts.map +1 -1
  79. package/dist/cryptography/publickey.mjs +12 -1
  80. package/dist/cryptography/publickey.mjs.map +1 -1
  81. package/dist/cryptography/signature.d.mts +2 -2
  82. package/dist/cryptography/signature.mjs +18 -7
  83. package/dist/cryptography/signature.mjs.map +1 -1
  84. package/dist/graphql/client.d.mts +51 -35
  85. package/dist/graphql/client.d.mts.map +1 -1
  86. package/dist/graphql/client.mjs +125 -69
  87. package/dist/graphql/client.mjs.map +1 -1
  88. package/dist/graphql/core.d.mts +14 -9
  89. package/dist/graphql/core.d.mts.map +1 -1
  90. package/dist/graphql/core.mjs +293 -117
  91. package/dist/graphql/core.mjs.map +1 -1
  92. package/dist/graphql/errors.d.mts +20 -0
  93. package/dist/graphql/errors.d.mts.map +1 -0
  94. package/dist/graphql/errors.mjs +19 -0
  95. package/dist/graphql/errors.mjs.map +1 -0
  96. package/dist/graphql/filters.mjs +32 -0
  97. package/dist/graphql/filters.mjs.map +1 -0
  98. package/dist/graphql/generated/queries.d.mts.map +1 -1
  99. package/dist/graphql/generated/queries.mjs +1216 -208
  100. package/dist/graphql/generated/queries.mjs.map +1 -1
  101. package/dist/graphql/generated/tada-env.d.mts +820 -11
  102. package/dist/graphql/index.d.mts +2 -1
  103. package/dist/graphql/index.mjs +2 -1
  104. package/dist/graphql/sse-parser.mjs +82 -0
  105. package/dist/graphql/sse-parser.mjs.map +1 -0
  106. package/dist/graphql/stream/families.mjs +306 -0
  107. package/dist/graphql/stream/families.mjs.map +1 -0
  108. package/dist/graphql/stream/index.mjs +38 -0
  109. package/dist/graphql/stream/index.mjs.map +1 -0
  110. package/dist/graphql/stream/live.mjs +60 -0
  111. package/dist/graphql/stream/live.mjs.map +1 -0
  112. package/dist/graphql/stream/page.mjs +38 -0
  113. package/dist/graphql/stream/page.mjs.map +1 -0
  114. package/dist/graphql/stream/query.mjs +47 -0
  115. package/dist/graphql/stream/query.mjs.map +1 -0
  116. package/dist/graphql/subscribe.mjs +70 -0
  117. package/dist/graphql/subscribe.mjs.map +1 -0
  118. package/dist/grpc/client.d.mts +31 -27
  119. package/dist/grpc/client.d.mts.map +1 -1
  120. package/dist/grpc/client.mjs +42 -46
  121. package/dist/grpc/client.mjs.map +1 -1
  122. package/dist/grpc/core.d.mts +13 -6
  123. package/dist/grpc/core.d.mts.map +1 -1
  124. package/dist/grpc/core.mjs +254 -166
  125. package/dist/grpc/core.mjs.map +1 -1
  126. package/dist/grpc/filters.mjs +44 -34
  127. package/dist/grpc/filters.mjs.map +1 -1
  128. package/dist/grpc/grpc-web-pull.mjs +245 -0
  129. package/dist/grpc/grpc-web-pull.mjs.map +1 -0
  130. package/dist/grpc/index.d.mts +2 -1
  131. package/dist/grpc/index.mjs +1 -1
  132. package/dist/grpc/network-error.mjs +31 -0
  133. package/dist/grpc/network-error.mjs.map +1 -0
  134. package/dist/grpc/proto/google/rpc/error_details.mjs +306 -0
  135. package/dist/grpc/proto/google/rpc/error_details.mjs.map +1 -0
  136. package/dist/grpc/proto/sui/rpc/v2/error_reason.d.mts +176 -1
  137. package/dist/grpc/proto/sui/rpc/v2/error_reason.d.mts.map +1 -1
  138. package/dist/grpc/proto/sui/rpc/v2/error_reason.mjs +175 -0
  139. package/dist/grpc/proto/sui/rpc/v2/error_reason.mjs.map +1 -1
  140. package/dist/grpc/proto/sui/rpc/v2/executed_transaction.d.mts +2 -2
  141. package/dist/grpc/proto/sui/rpc/v2/ledger_service.client.d.mts +4 -4
  142. package/dist/grpc/proto/sui/rpc/v2/ledger_service.d.mts +111 -0
  143. package/dist/grpc/proto/sui/rpc/v2/ledger_service.d.mts.map +1 -1
  144. package/dist/grpc/proto/sui/rpc/v2/ledger_service.mjs +46 -0
  145. package/dist/grpc/proto/sui/rpc/v2/ledger_service.mjs.map +1 -1
  146. package/dist/grpc/proto/sui/rpc/v2/move_package_service.client.d.mts +4 -4
  147. package/dist/grpc/proto/sui/rpc/v2/move_package_service.d.mts +24 -1
  148. package/dist/grpc/proto/sui/rpc/v2/move_package_service.d.mts.map +1 -1
  149. package/dist/grpc/proto/sui/rpc/v2/move_package_service.mjs +25 -7
  150. package/dist/grpc/proto/sui/rpc/v2/move_package_service.mjs.map +1 -1
  151. package/dist/grpc/proto/sui/rpc/v2/object.mjs +1 -1
  152. package/dist/grpc/proto/sui/rpc/v2/query_options.d.mts +111 -9
  153. package/dist/grpc/proto/sui/rpc/v2/query_options.d.mts.map +1 -1
  154. package/dist/grpc/proto/sui/rpc/v2/query_options.mjs +63 -19
  155. package/dist/grpc/proto/sui/rpc/v2/query_options.mjs.map +1 -1
  156. package/dist/grpc/proto/sui/rpc/v2/signature.d.mts +5 -5
  157. package/dist/grpc/proto/sui/rpc/v2/signature.mjs.map +1 -1
  158. package/dist/grpc/proto/sui/rpc/v2/signature_scheme.d.mts +5 -1
  159. package/dist/grpc/proto/sui/rpc/v2/signature_scheme.d.mts.map +1 -1
  160. package/dist/grpc/proto/sui/rpc/v2/signature_scheme.mjs +4 -0
  161. package/dist/grpc/proto/sui/rpc/v2/signature_scheme.mjs.map +1 -1
  162. package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.client.d.mts +4 -4
  163. package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.d.mts +20 -1
  164. package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.d.mts.map +1 -1
  165. package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.mjs +30 -13
  166. package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.mjs.map +1 -1
  167. package/dist/grpc/proto/sui/rpc/v2/state_service.client.d.mts +4 -4
  168. package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.d.mts +16 -10
  169. package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.d.mts.map +1 -1
  170. package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.mjs +8 -5
  171. package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.mjs.map +1 -1
  172. package/dist/grpc/proto/sui/rpc/v2/transaction.d.mts +11 -5
  173. package/dist/grpc/proto/sui/rpc/v2/transaction.d.mts.map +1 -1
  174. package/dist/grpc/proto/sui/rpc/v2/transaction.mjs +6 -0
  175. package/dist/grpc/proto/sui/rpc/v2/transaction.mjs.map +1 -1
  176. package/dist/grpc/proto/sui/rpc/v2/transaction_execution_service.d.mts +6 -2
  177. package/dist/grpc/proto/sui/rpc/v2/transaction_execution_service.d.mts.map +1 -1
  178. package/dist/grpc/proto/sui/rpc/v2/transaction_execution_service.mjs.map +1 -1
  179. package/dist/grpc/proto/types.d.mts +7 -7
  180. package/dist/grpc/proto/types.mjs +3 -2
  181. package/dist/grpc/pull-call.d.mts +20 -0
  182. package/dist/grpc/pull-call.d.mts.map +1 -0
  183. package/dist/grpc/pull-call.mjs +28 -0
  184. package/dist/grpc/pull-call.mjs.map +1 -0
  185. package/dist/grpc/stream/call.mjs +238 -0
  186. package/dist/grpc/stream/call.mjs.map +1 -0
  187. package/dist/grpc/stream/families.d.mts +26 -0
  188. package/dist/grpc/stream/families.d.mts.map +1 -0
  189. package/dist/grpc/stream/families.mjs +145 -0
  190. package/dist/grpc/stream/families.mjs.map +1 -0
  191. package/dist/grpc/stream/index.d.mts +41 -0
  192. package/dist/grpc/stream/index.d.mts.map +1 -0
  193. package/dist/grpc/stream/index.mjs +127 -0
  194. package/dist/grpc/stream/index.mjs.map +1 -0
  195. package/dist/grpc/stream/live.mjs +31 -0
  196. package/dist/grpc/stream/live.mjs.map +1 -0
  197. package/dist/grpc/stream/page.mjs +120 -0
  198. package/dist/grpc/stream/page.mjs.map +1 -0
  199. package/dist/grpc/stream-types.d.mts +28 -0
  200. package/dist/grpc/stream-types.d.mts.map +1 -0
  201. package/dist/grpc/transport.d.mts +8 -3
  202. package/dist/grpc/transport.d.mts.map +1 -1
  203. package/dist/grpc/transport.mjs +88 -45
  204. package/dist/grpc/transport.mjs.map +1 -1
  205. package/dist/jsonRpc/index.d.mts +34 -12
  206. package/dist/jsonRpc/index.d.mts.map +1 -0
  207. package/dist/jsonRpc/index.mjs +39 -5
  208. package/dist/jsonRpc/index.mjs.map +1 -0
  209. package/dist/jsonRpc/types.d.mts +420 -0
  210. package/dist/jsonRpc/types.d.mts.map +1 -0
  211. package/dist/keypairs/passkey/keypair.d.mts +1 -1
  212. package/dist/multisig/publickey.d.mts +12 -4
  213. package/dist/multisig/publickey.d.mts.map +1 -1
  214. package/dist/multisig/publickey.mjs +5 -5
  215. package/dist/multisig/publickey.mjs.map +1 -1
  216. package/dist/multisig/signer.d.mts +1 -1
  217. package/dist/transactions/ObjectCache.d.mts +1 -1
  218. package/dist/transactions/ObjectCache.mjs +5 -5
  219. package/dist/transactions/ObjectCache.mjs.map +1 -1
  220. package/dist/transactions/Transaction.d.mts +11 -10
  221. package/dist/transactions/Transaction.d.mts.map +1 -1
  222. package/dist/transactions/Transaction.mjs +17 -6
  223. package/dist/transactions/Transaction.mjs.map +1 -1
  224. package/dist/transactions/data/internal.d.mts +135 -133
  225. package/dist/transactions/data/internal.d.mts.map +1 -1
  226. package/dist/transactions/data/internal.mjs +2 -1
  227. package/dist/transactions/data/internal.mjs.map +1 -1
  228. package/dist/transactions/data/v1.d.mts +239 -239
  229. package/dist/transactions/data/v1.d.mts.map +1 -1
  230. package/dist/transactions/data/v2.d.mts +16 -16
  231. package/dist/transactions/data/v2.d.mts.map +1 -1
  232. package/dist/transactions/executor/parallel.d.mts +2 -2
  233. package/dist/transactions/executor/serial.d.mts +1 -1
  234. package/dist/transactions/executor/serial.mjs +2 -2
  235. package/dist/transactions/executor/serial.mjs.map +1 -1
  236. package/dist/transactions/index.d.mts +3 -2
  237. package/dist/transactions/index.mjs +4 -3
  238. package/dist/transactions/intents/AllowanceBalance.mjs +1 -1
  239. package/dist/transactions/intents/CoinWithBalance.mjs +1 -1
  240. package/dist/transactions/resolve.d.mts +6 -0
  241. package/dist/transactions/resolve.d.mts.map +1 -1
  242. package/dist/transactions/resolve.mjs.map +1 -1
  243. package/dist/transactions/restricted-move-calls.d.mts +25 -0
  244. package/dist/transactions/restricted-move-calls.d.mts.map +1 -0
  245. package/dist/transactions/restricted-move-calls.mjs +47 -0
  246. package/dist/transactions/restricted-move-calls.mjs.map +1 -0
  247. package/dist/transactions/serializer.d.mts +1 -4
  248. package/dist/transactions/serializer.d.mts.map +1 -1
  249. package/dist/transactions/serializer.mjs +1 -44
  250. package/dist/transactions/serializer.mjs.map +1 -1
  251. package/dist/transactions/utils.d.mts +0 -1
  252. package/dist/transactions/utils.d.mts.map +1 -1
  253. package/dist/transactions/utils.mjs.map +1 -1
  254. package/dist/utils/abort.mjs +89 -0
  255. package/dist/utils/abort.mjs.map +1 -0
  256. package/dist/utils/coin-reservation.mjs +1 -4
  257. package/dist/utils/coin-reservation.mjs.map +1 -1
  258. package/dist/utils/index.mjs +1 -1
  259. package/dist/utils/sui-types.mjs +1 -1
  260. package/dist/verify/index.d.mts +2 -2
  261. package/dist/verify/verify.d.mts +74 -24
  262. package/dist/verify/verify.d.mts.map +1 -1
  263. package/dist/verify/verify.mjs +128 -42
  264. package/dist/verify/verify.mjs.map +1 -1
  265. package/dist/version.mjs +1 -1
  266. package/dist/version.mjs.map +1 -1
  267. package/dist/zklogin/bcs.d.mts +14 -14
  268. package/dist/zklogin/publickey.d.mts.map +1 -1
  269. package/dist/zklogin/publickey.mjs +15 -20
  270. package/dist/zklogin/publickey.mjs.map +1 -1
  271. package/docs/bcs.md +2 -2
  272. package/docs/clients/core.md +19 -18
  273. package/docs/clients/executing.md +53 -8
  274. package/docs/clients/graphql.md +81 -11
  275. package/docs/clients/grpc.md +31 -15
  276. package/docs/clients/index.md +8 -7
  277. package/docs/clients/querying.md +134 -30
  278. package/docs/clients/streaming.md +324 -0
  279. package/docs/cryptography/index.md +85 -58
  280. package/docs/llms-index.md +4 -2
  281. package/docs/migrations/sui-2.0/dapp-kit.md +3 -2
  282. package/docs/migrations/sui-2.0/json-rpc-migration.md +106 -79
  283. package/docs/migrations/sui-3.0/index.md +250 -0
  284. package/docs/plugins.md +10 -10
  285. package/docs/sdk-building.md +3 -4
  286. package/docs/transactions/coins-and-balances.md +6 -11
  287. package/docs/transactions/reference.md +36 -1
  288. package/package.json +2 -3
  289. package/src/bcs/bcs.ts +80 -41
  290. package/src/bcs/effects.ts +61 -25
  291. package/src/bcs/index.ts +2 -0
  292. package/src/bcs/transactions.ts +36 -17
  293. package/src/bcs/types.ts +10 -4
  294. package/src/client/core-resolver.ts +3 -5
  295. package/src/client/core.ts +180 -8
  296. package/src/client/errors.ts +12 -0
  297. package/src/client/index.ts +4 -1
  298. package/src/client/mvr.ts +37 -10
  299. package/src/client/query-filters.ts +151 -97
  300. package/src/client/stream/adapter.ts +110 -0
  301. package/src/client/stream/errors.ts +40 -0
  302. package/src/client/stream/filter.ts +17 -0
  303. package/src/client/stream/follow.ts +63 -0
  304. package/src/client/stream/index.ts +9 -0
  305. package/src/client/stream/pager.ts +158 -0
  306. package/src/client/stream/plan.ts +147 -0
  307. package/src/client/stream/position.ts +143 -0
  308. package/src/client/stream/run.ts +147 -0
  309. package/src/client/stream/session.ts +259 -0
  310. package/src/client/stream/status.ts +44 -0
  311. package/src/client/stream/token.ts +104 -0
  312. package/src/client/transaction-data.ts +587 -0
  313. package/src/client/transaction-resolver.ts +158 -69
  314. package/src/client/types.ts +530 -115
  315. package/src/client/utils.ts +147 -50
  316. package/src/cryptography/publickey.ts +12 -1
  317. package/src/cryptography/signature.ts +6 -3
  318. package/src/graphql/client.ts +204 -135
  319. package/src/graphql/core.ts +446 -139
  320. package/src/graphql/errors.ts +24 -0
  321. package/src/graphql/filters.ts +48 -0
  322. package/src/graphql/generated/queries.ts +1455 -277
  323. package/src/graphql/generated/schema.graphql +282 -9
  324. package/src/graphql/generated/tada-env.ts +933 -4
  325. package/src/graphql/queries/checkpoints.graphql +34 -0
  326. package/src/graphql/queries/getDynamicFields.graphql +1 -0
  327. package/src/graphql/queries/listEvents.graphql +16 -24
  328. package/src/graphql/queries/streams.graphql +212 -0
  329. package/src/graphql/queries/transactions.graphql +71 -28
  330. package/src/graphql/queries/verifySignature.graphql +17 -0
  331. package/src/graphql/sse-parser.ts +99 -0
  332. package/src/graphql/stream/families.ts +515 -0
  333. package/src/graphql/stream/index.ts +55 -0
  334. package/src/graphql/stream/live.ts +70 -0
  335. package/src/graphql/stream/page.ts +47 -0
  336. package/src/graphql/stream/query.ts +52 -0
  337. package/src/graphql/subscribe.ts +83 -0
  338. package/src/grpc/client.ts +81 -79
  339. package/src/grpc/core.ts +388 -274
  340. package/src/grpc/filters.ts +59 -60
  341. package/src/grpc/grpc-web-pull.ts +360 -0
  342. package/src/grpc/index.ts +5 -0
  343. package/src/grpc/network-error.ts +33 -0
  344. package/src/grpc/proto/sui/rpc/v2/error_reason.ts +175 -0
  345. package/src/grpc/proto/sui/rpc/v2/ledger_service.ts +145 -0
  346. package/src/grpc/proto/sui/rpc/v2/move_package_service.ts +43 -1
  347. package/src/grpc/proto/sui/rpc/v2/query_options.ts +132 -8
  348. package/src/grpc/proto/sui/rpc/v2/signature.ts +4 -4
  349. package/src/grpc/proto/sui/rpc/v2/signature_scheme.ts +4 -0
  350. package/src/grpc/proto/sui/rpc/v2/signature_verification_service.ts +30 -0
  351. package/src/grpc/proto/sui/rpc/v2/subscription_service.client.ts +16 -10
  352. package/src/grpc/proto/sui/rpc/v2/transaction.ts +6 -0
  353. package/src/grpc/proto/sui/rpc/v2/transaction_execution_service.ts +5 -1
  354. package/src/grpc/pull-call.ts +54 -0
  355. package/src/grpc/stream/call.ts +317 -0
  356. package/src/grpc/stream/families.ts +228 -0
  357. package/src/grpc/stream/index.ts +182 -0
  358. package/src/grpc/stream/live.ts +33 -0
  359. package/src/grpc/stream/page.ts +161 -0
  360. package/src/grpc/stream-types.ts +33 -0
  361. package/src/grpc/transport.ts +116 -59
  362. package/src/jsonRpc/index.ts +60 -17
  363. package/src/jsonRpc/types.ts +421 -0
  364. package/src/multisig/publickey.ts +19 -7
  365. package/src/transactions/ObjectCache.ts +6 -6
  366. package/src/transactions/Transaction.ts +30 -6
  367. package/src/transactions/data/internal.ts +2 -0
  368. package/src/transactions/executor/serial.ts +2 -2
  369. package/src/transactions/index.ts +8 -1
  370. package/src/transactions/resolve.ts +6 -0
  371. package/src/transactions/restricted-move-calls.ts +87 -0
  372. package/src/transactions/serializer.ts +0 -70
  373. package/src/transactions/utils.ts +0 -37
  374. package/src/utils/abort.ts +101 -0
  375. package/src/verify/index.ts +1 -0
  376. package/src/verify/verify.ts +190 -62
  377. package/src/version.ts +1 -1
  378. package/src/zklogin/publickey.ts +18 -34
  379. package/dist/client/core-resolver.d.mts +0 -9
  380. package/dist/client/core-resolver.d.mts.map +0 -1
  381. package/dist/jsonRpc/client.d.mts +0 -507
  382. package/dist/jsonRpc/client.d.mts.map +0 -1
  383. package/dist/jsonRpc/client.mjs +0 -868
  384. package/dist/jsonRpc/client.mjs.map +0 -1
  385. package/dist/jsonRpc/core.d.mts +0 -168
  386. package/dist/jsonRpc/core.d.mts.map +0 -1
  387. package/dist/jsonRpc/core.mjs +0 -1054
  388. package/dist/jsonRpc/core.mjs.map +0 -1
  389. package/dist/jsonRpc/errors.d.mts +0 -27
  390. package/dist/jsonRpc/errors.d.mts.map +0 -1
  391. package/dist/jsonRpc/errors.mjs +0 -50
  392. package/dist/jsonRpc/errors.mjs.map +0 -1
  393. package/dist/jsonRpc/http-transport.d.mts +0 -62
  394. package/dist/jsonRpc/http-transport.d.mts.map +0 -1
  395. package/dist/jsonRpc/http-transport.mjs +0 -60
  396. package/dist/jsonRpc/http-transport.mjs.map +0 -1
  397. package/dist/jsonRpc/network.d.mts +0 -9
  398. package/dist/jsonRpc/network.d.mts.map +0 -1
  399. package/dist/jsonRpc/network.mjs +0 -18
  400. package/dist/jsonRpc/network.mjs.map +0 -1
  401. package/dist/jsonRpc/types/chain.d.mts +0 -169
  402. package/dist/jsonRpc/types/chain.d.mts.map +0 -1
  403. package/dist/jsonRpc/types/changes.d.mts +0 -48
  404. package/dist/jsonRpc/types/changes.d.mts.map +0 -1
  405. package/dist/jsonRpc/types/coins.d.mts +0 -15
  406. package/dist/jsonRpc/types/coins.d.mts.map +0 -1
  407. package/dist/jsonRpc/types/common.d.mts +0 -9
  408. package/dist/jsonRpc/types/common.d.mts.map +0 -1
  409. package/dist/jsonRpc/types/generated.d.mts +0 -1976
  410. package/dist/jsonRpc/types/generated.d.mts.map +0 -1
  411. package/dist/jsonRpc/types/index.d.mts +0 -6
  412. package/dist/jsonRpc/types/params.d.mts +0 -898
  413. package/dist/jsonRpc/types/params.d.mts.map +0 -1
  414. package/src/graphql/generated/tsconfig.tada.json +0 -11
  415. package/src/graphql/queries/verifyZkLoginSignature.graphql +0 -15
  416. package/src/jsonRpc/client.ts +0 -1252
  417. package/src/jsonRpc/core.ts +0 -1557
  418. package/src/jsonRpc/errors.ts +0 -57
  419. package/src/jsonRpc/http-transport.ts +0 -118
  420. package/src/jsonRpc/network.ts +0 -21
  421. package/src/jsonRpc/types/chain.ts +0 -190
  422. package/src/jsonRpc/types/changes.ts +0 -35
  423. package/src/jsonRpc/types/coins.ts +0 -14
  424. package/src/jsonRpc/types/common.ts +0 -8
  425. package/src/jsonRpc/types/generated.ts +0 -2165
  426. package/src/jsonRpc/types/index.ts +0 -9
  427. package/src/jsonRpc/types/params.ts +0 -908
@@ -0,0 +1,158 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * The shared pager. Every paged read is one pass of it: finite ranges, polling, and history before
6
+ * a live handoff. A pass reads pages from its start until the server reports no more, then commits
7
+ * the boundary after what the server covered, so a quiet range still moves its resume point. A
8
+ * pass is complete once the index has reached its end; otherwise the caller waits and reads again.
9
+ */
10
+
11
+ import type { SuiClientTypes } from '../types.js';
12
+ import type {
13
+ Bound,
14
+ LedgerStreamAdapter,
15
+ LiveEvent,
16
+ PageEnd,
17
+ PageRequest,
18
+ StreamFamily,
19
+ } from './adapter.js';
20
+ import { SuiStreamError } from './errors.js';
21
+ import { boundaryPosition, insideCheckpoint, progressPosition } from './position.js';
22
+ import { sameBytes } from './token.js';
23
+
24
+ interface Pass {
25
+ start?: Bound;
26
+ /** Exclusive checkpoint end in traversal order. */
27
+ end?: bigint;
28
+ order: SuiClientTypes.Order;
29
+ signal: AbortSignal;
30
+ }
31
+
32
+ /**
33
+ * `complete: false`: the index has not reached the end yet. `retentionLimit`: a read toward
34
+ * genesis stopped at the oldest retained checkpoint.
35
+ */
36
+ interface PassResult {
37
+ complete: boolean;
38
+ retentionLimit: boolean;
39
+ }
40
+
41
+ /** The request that reads everything after `start` and before `end`. */
42
+ export function passRequest(
43
+ start: Bound | undefined,
44
+ end: bigint | undefined,
45
+ order: SuiClientTypes.Order,
46
+ family: StreamFamily,
47
+ ): Omit<PageRequest, 'signal'> {
48
+ const ascending = order === 'ascending';
49
+ const request: Omit<PageRequest, 'signal'> = { range: {}, order };
50
+ if (start && 'checkpoint' in start) {
51
+ if (ascending) request.range.start = start.checkpoint;
52
+ else request.range.end = start.checkpoint + 1n;
53
+ } else if (start) {
54
+ const { checkpoint, cursor } = start.position;
55
+ // A delivered checkpoint and a plain boundary resume by checkpoint; anything inside a
56
+ // checkpoint resumes by its cursor.
57
+ const whole = family === 'checkpoints' && start.position.kind === 'Item';
58
+ const inside = !whole && insideCheckpoint(start.position);
59
+ if (cursor && (checkpoint === null || inside)) {
60
+ if (ascending) request.after = cursor;
61
+ else request.before = cursor;
62
+ }
63
+ if (checkpoint !== null) {
64
+ if (ascending) request.range.start = whole ? checkpoint + 1n : checkpoint;
65
+ else request.range.end = inside ? checkpoint + 1n : checkpoint;
66
+ }
67
+ }
68
+ if (end !== undefined) {
69
+ if (ascending) request.range.end = end;
70
+ else request.range.start = end + 1n;
71
+ }
72
+ return request;
73
+ }
74
+
75
+ export async function* readPages<Item>(
76
+ adapter: LedgerStreamAdapter<Item, any>,
77
+ pass: Pass,
78
+ ): AsyncGenerator<LiveEvent<Item>, PassResult> {
79
+ const { order, end, signal } = pass;
80
+ const ascending = order === 'ascending';
81
+ const request: PageRequest = {
82
+ ...passRequest(pass.start, end, order, adapter.family),
83
+ signal,
84
+ };
85
+
86
+ // A boundary from what a replica has covered can sit behind a cursor without a checkpoint,
87
+ // whose place is unknown, so it is committed only after a position with a checkpoint.
88
+ let placed = !pass.start || 'checkpoint' in pass.start || pass.start.position.checkpoint !== null;
89
+ while (true) {
90
+ const page = adapter.fetchPage({ ...request, range: { ...request.range } });
91
+ let empty = true;
92
+ let pageEnd: PageEnd;
93
+ try {
94
+ let read = await page.next();
95
+ for (; !read.done; read = await page.next()) {
96
+ const { position, item } = read.value;
97
+ placed = position.checkpoint !== null;
98
+ if (item === undefined) {
99
+ yield { $kind: 'Progress', position };
100
+ continue;
101
+ }
102
+ empty = false;
103
+ yield { $kind: 'Item', position, item };
104
+ }
105
+ pageEnd = read.value;
106
+ } finally {
107
+ await page.return?.();
108
+ }
109
+ if (pageEnd.notIndexed) return { complete: false, retentionLimit: false };
110
+
111
+ const { more, cursor } = pageEnd.next;
112
+ if (more) {
113
+ if (!cursor) throw protocol('Page has more items but no cursor');
114
+ const sent = ascending ? request.after : request.before;
115
+ if (sent && sameBytes(sent, cursor)) throw protocol('Pagination did not advance');
116
+ // A page end that reports where the scan stopped, or an empty page, moves the resume point,
117
+ // so sparse filters save their place.
118
+ const position = pageEnd.next.position ?? (empty ? progressPosition(cursor) : undefined);
119
+ if (position) {
120
+ placed = position.checkpoint !== null;
121
+ yield { $kind: 'Progress', position };
122
+ }
123
+ if (ascending) request.after = cursor;
124
+ else request.before = cursor;
125
+ continue;
126
+ }
127
+
128
+ if (!ascending) {
129
+ // Covered down to the end, so a finished range resumes as empty.
130
+ if (end !== undefined) yield { $kind: 'Progress', position: boundaryPosition(end + 1n) };
131
+ const retentionLimit = end === undefined && (pageEnd.retainedFrom ?? 0n) > 0n;
132
+ return { complete: true, retentionLimit };
133
+ }
134
+
135
+ const { covered } = pageEnd;
136
+ const boundary = boundaryAfter(covered, request.range);
137
+ if (boundary !== undefined && placed) {
138
+ yield { $kind: 'Progress', position: boundaryPosition(boundary) };
139
+ }
140
+ return {
141
+ complete: end === undefined || covered === undefined || covered >= end - 1n,
142
+ retentionLimit: false,
143
+ };
144
+ }
145
+ }
146
+
147
+ /** The boundary after what an exhausted ascending read covered, within its range. */
148
+ function boundaryAfter(covered: bigint | undefined, { start, end }: PageRequest['range']) {
149
+ let boundary = covered === undefined ? end : covered + 1n;
150
+ if (boundary === undefined) return undefined;
151
+ if (end !== undefined && boundary > end) boundary = end;
152
+ if (start !== undefined && boundary < start) boundary = start;
153
+ return boundary;
154
+ }
155
+
156
+ function protocol(message: string) {
157
+ return new SuiStreamError('Protocol', message);
158
+ }
@@ -0,0 +1,147 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { parseCheckpointNumber } from '../query-filters.js';
5
+ import type { SuiClientTypes } from '../types.js';
6
+ import type { Bound, LedgerStreamAdapter } from './adapter.js';
7
+ import { SuiStreamError } from './errors.js';
8
+ import { passRequest } from './pager.js';
9
+ import { decodeToken, filterHash, sameBytes, type StreamToken } from './token.js';
10
+
11
+ /** Milliseconds between polls when `pollInterval` isn't set. */
12
+ export const DEFAULT_POLL_INTERVAL = 1000;
13
+
14
+ /** finite: read to the end. poll: read to the tip repeatedly. follow: the live handoff. */
15
+ type StreamMode = 'finite' | 'poll' | 'follow';
16
+
17
+ export interface StreamPlan {
18
+ mode: StreamMode;
19
+ order: SuiClientTypes.Order;
20
+ pollInterval: number;
21
+ token?: StreamToken;
22
+ /** Where the stream starts; undefined for a fresh follow at the live tip. */
23
+ start?: Bound;
24
+ /** The exclusive checkpoint end of a finite stream, in traversal order. */
25
+ end?: bigint;
26
+ /** Why a finite stream completes, unless it stops at the retention limit. */
27
+ completion: SuiClientTypes.StreamCompletionFrame['reason'];
28
+ }
29
+
30
+ type Adapter = Pick<LedgerStreamAdapter<unknown, any>, 'transport' | 'family' | 'live'>;
31
+
32
+ /**
33
+ * Checks every option that needs no network, so a failing or retrying request can never hide an
34
+ * option or token error.
35
+ */
36
+ export function validateOptions(
37
+ options: SuiClientTypes.StreamOptions<SuiClientTypes.StreamFrameOptions>,
38
+ adapter: Adapter,
39
+ ): StreamPlan {
40
+ const token =
41
+ options.start?.resumeToken !== undefined ? decodeToken(options.start.resumeToken) : undefined;
42
+ if (token && (token.transport !== adapter.transport || token.family !== adapter.family)) {
43
+ throw incompatibleToken();
44
+ }
45
+ const order = options.order ?? token?.order ?? 'ascending';
46
+ if (token && token.order !== order) {
47
+ throw new SuiStreamError('IncompatibleToken', 'A resume token cannot change traversal order');
48
+ }
49
+ if (options.end && options.end.checkpoint === undefined) {
50
+ throw new SuiStreamError('InvalidOptions', 'end must be a checkpoint');
51
+ }
52
+
53
+ const follow = options.follow ?? (order === 'ascending' && !options.end);
54
+ if (follow && (order === 'descending' || options.end)) {
55
+ throw new SuiStreamError(
56
+ 'InvalidOptions',
57
+ 'Following requires ascending order without an end bound',
58
+ );
59
+ }
60
+ if (!follow && order === 'ascending' && !options.start) {
61
+ throw new SuiStreamError('InvalidOptions', 'Ascending finite streams require a start');
62
+ }
63
+ const pollInterval = options.pollInterval ?? DEFAULT_POLL_INTERVAL;
64
+ if (!(pollInterval > 0)) {
65
+ throw new SuiStreamError('InvalidOptions', 'pollInterval must be a positive number');
66
+ }
67
+
68
+ const startCheckpoint = checkpointOption(options.start?.checkpoint, 'start');
69
+ const end = checkpointOption(options.end?.checkpoint, 'end');
70
+ if (startCheckpoint !== undefined && end !== undefined) {
71
+ if (order === 'ascending' ? startCheckpoint > end : startCheckpoint < end) {
72
+ throw new SuiStreamError('InvalidOptions', 'Stream start and end bounds are reversed');
73
+ }
74
+ }
75
+
76
+ let completion: StreamPlan['completion'] = 'checkpointBound';
77
+ if (end === undefined) completion = order === 'ascending' ? 'indexedTip' : 'genesis';
78
+ return {
79
+ mode: !follow ? 'finite' : options.delivery === 'poll' ? 'poll' : 'follow',
80
+ order,
81
+ pollInterval,
82
+ token,
83
+ start: token
84
+ ? { position: token.position }
85
+ : startCheckpoint !== undefined
86
+ ? { checkpoint: startCheckpoint }
87
+ : undefined,
88
+ end,
89
+ completion,
90
+ };
91
+ }
92
+
93
+ /**
94
+ * Checks the token against the stream's identity and fills in what depends on the indexed tip,
95
+ * reading it at most once.
96
+ */
97
+ export async function planStream(
98
+ plan: StreamPlan,
99
+ identity: { chain: string; filter: unknown },
100
+ adapter: Adapter,
101
+ readIndexedTip: () => Promise<bigint>,
102
+ ): Promise<StreamPlan> {
103
+ const { token, order, mode } = plan;
104
+ if (
105
+ token &&
106
+ (token.chain !== identity.chain || !sameBytes(token.filter, filterHash(identity.filter)))
107
+ ) {
108
+ throw incompatibleToken();
109
+ }
110
+ const anchorsAtLiveTip = mode === 'follow' && adapter.live === 'fromTip';
111
+ const needsStart = !plan.start && !anchorsAtLiveTip;
112
+ const needsEnd = mode === 'finite' && order === 'ascending' && plan.end === undefined;
113
+ if (!needsStart && !needsEnd) return plan;
114
+
115
+ const tip = await readIndexedTip();
116
+ return {
117
+ ...plan,
118
+ start: needsStart ? { checkpoint: order === 'ascending' ? tip + 1n : tip } : plan.start,
119
+ end: needsEnd ? tip + 1n : plan.end,
120
+ };
121
+ }
122
+
123
+ /** Whether a finite stream's bounds leave nothing to read. */
124
+ export function isEmptyRange(plan: StreamPlan, family: Adapter['family']): boolean {
125
+ if (plan.mode !== 'finite' || !plan.start) return false;
126
+ const { range } = passRequest(plan.start, plan.end, plan.order, family);
127
+ return range.start !== undefined && range.end !== undefined && range.start >= range.end;
128
+ }
129
+
130
+ function incompatibleToken() {
131
+ return new SuiStreamError(
132
+ 'IncompatibleToken',
133
+ 'Resume token belongs to a different transport, chain, stream family or filter',
134
+ );
135
+ }
136
+
137
+ function checkpointOption(
138
+ value: SuiClientTypes.CheckpointNumber | undefined,
139
+ label: string,
140
+ ): bigint | undefined {
141
+ if (value === undefined) return undefined;
142
+ try {
143
+ return parseCheckpointNumber(value, label);
144
+ } catch (error) {
145
+ throw new SuiStreamError('InvalidOptions', (error as Error).message, { cause: error });
146
+ }
147
+ }
@@ -0,0 +1,143 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { bcs } from '@mysten/bcs';
5
+
6
+ import type { StreamFamily } from './adapter.js';
7
+
8
+ /**
9
+ * Stream positions, shared by every transport. Adapters build positions from what the server
10
+ * reports and turn them back into request cursors; cursors themselves are never parsed or compared.
11
+ *
12
+ * - `Item`: a delivered item in `checkpoint`. Its place within the checkpoint is
13
+ * `transactionIndex`/`eventIndex` where reported, else only `itemId` identifies it.
14
+ * - `Boundary`: just before `checkpoint`, or with `transactionIndex`, just before that key within
15
+ * it (resumed by its cursor).
16
+ * - `Progress`: a cursor the server reported without a checkpoint (GraphQL only).
17
+ *
18
+ * `nextUncovered` is the first checkpoint not known to be delivered at the position, in ascending
19
+ * order: every matching item before it was delivered. Null makes no claim.
20
+ */
21
+ export interface StreamPosition {
22
+ kind: 'Item' | 'Boundary' | 'Progress';
23
+ cursor: Uint8Array | null;
24
+ /** Null only for `Progress`. */
25
+ checkpoint: bigint | null;
26
+ transactionIndex: bigint | null;
27
+ eventIndex: number | null;
28
+ itemId: string | null;
29
+ nextUncovered: bigint | null;
30
+ }
31
+
32
+ const PositionBcs = bcs.struct('StreamPosition', {
33
+ kind: bcs.enum('StreamPositionKind', { Item: null, Boundary: null, Progress: null }),
34
+ cursor: bcs.option(bcs.byteVector()),
35
+ checkpoint: bcs.option(bcs.u64()),
36
+ transactionIndex: bcs.option(bcs.u64()),
37
+ eventIndex: bcs.option(bcs.u32()),
38
+ itemId: bcs.option(bcs.string()),
39
+ nextUncovered: bcs.option(bcs.u64()),
40
+ });
41
+
42
+ const optionalBigInt = (value: string | null) => (value === null ? null : BigInt(value));
43
+
44
+ export const positionSchema = PositionBcs.transform({
45
+ input: (position: StreamPosition) => ({
46
+ ...position,
47
+ kind: { [position.kind]: true } as { Item: true } | { Boundary: true } | { Progress: true },
48
+ }),
49
+ output: (value): StreamPosition => ({
50
+ kind: value.kind.$kind,
51
+ cursor: value.cursor === null ? null : Uint8Array.from(value.cursor),
52
+ checkpoint: optionalBigInt(value.checkpoint),
53
+ transactionIndex: optionalBigInt(value.transactionIndex),
54
+ eventIndex: value.eventIndex,
55
+ itemId: value.itemId,
56
+ nextUncovered: optionalBigInt(value.nextUncovered),
57
+ }),
58
+ });
59
+
60
+ const EMPTY: Omit<StreamPosition, 'kind'> = {
61
+ cursor: null,
62
+ checkpoint: null,
63
+ transactionIndex: null,
64
+ eventIndex: null,
65
+ itemId: null,
66
+ nextUncovered: null,
67
+ };
68
+
69
+ export function itemPosition(
70
+ checkpoint: bigint,
71
+ fields: Partial<Omit<StreamPosition, 'kind' | 'checkpoint'>> = {},
72
+ ): StreamPosition {
73
+ return { ...EMPTY, ...fields, kind: 'Item', checkpoint };
74
+ }
75
+
76
+ /**
77
+ * The boundary just before `checkpoint`, or with indexes, just before that key within it. Zero
78
+ * indexes are the plain boundary before `checkpoint`, which resumes without a cursor.
79
+ */
80
+ export function boundaryPosition(
81
+ checkpoint: bigint,
82
+ fields: Partial<Omit<StreamPosition, 'kind' | 'checkpoint'>> = {},
83
+ ): StreamPosition {
84
+ const position: StreamPosition = { ...EMPTY, ...fields, kind: 'Boundary', checkpoint };
85
+ if (fields.transactionIndex || fields.eventIndex) return position;
86
+ return { ...position, cursor: null, transactionIndex: null, eventIndex: null };
87
+ }
88
+
89
+ export function progressPosition(cursor: Uint8Array): StreamPosition {
90
+ return { ...EMPTY, kind: 'Progress', cursor };
91
+ }
92
+
93
+ /** Whether items of the position's checkpoint can lie on both sides of it. */
94
+ export function insideCheckpoint(position: StreamPosition): boolean {
95
+ return position.kind === 'Item' || position.transactionIndex !== null;
96
+ }
97
+
98
+ /**
99
+ * Ledger order of two positions from their coordinates. Undefined where coordinates don't decide
100
+ * it: a `Progress` position, or two items of one checkpoint whose places are not reported.
101
+ */
102
+ export function comparePositions(a: StreamPosition, b: StreamPosition): -1 | 0 | 1 | undefined {
103
+ if (a.checkpoint === null || b.checkpoint === null) return undefined;
104
+ if (a.checkpoint !== b.checkpoint) return order(a.checkpoint, b.checkpoint);
105
+ if (a.kind === 'Boundary' && b.kind === 'Boundary') return compareKeys(a, b);
106
+ if (a.kind === 'Boundary') return boundaryToItem(a, b);
107
+ if (b.kind === 'Boundary') {
108
+ const comparison = boundaryToItem(b, a);
109
+ return comparison && (-comparison as -1 | 1);
110
+ }
111
+ if (a.itemId !== null || b.itemId !== null) return a.itemId === b.itemId ? 0 : undefined;
112
+ return compareKeys(a, b);
113
+ }
114
+
115
+ function order(a: bigint, b: bigint): -1 | 0 | 1 {
116
+ return a === b ? 0 : a < b ? -1 : 1;
117
+ }
118
+
119
+ /** Keys within one checkpoint, unset indexes counting as 0. */
120
+ function compareKeys(a: StreamPosition, b: StreamPosition): -1 | 0 | 1 {
121
+ return (
122
+ order(a.transactionIndex ?? 0n, b.transactionIndex ?? 0n) ||
123
+ order(BigInt(a.eventIndex ?? 0), BigInt(b.eventIndex ?? 0))
124
+ );
125
+ }
126
+
127
+ /** A boundary against an item of its checkpoint: it sits before the item with its key. */
128
+ function boundaryToItem(boundary: StreamPosition, item: StreamPosition): -1 | 1 | undefined {
129
+ if (boundary.transactionIndex === null) return -1;
130
+ if (item.transactionIndex === null) return undefined;
131
+ return compareKeys(boundary, item) <= 0 ? -1 : 1;
132
+ }
133
+
134
+ /**
135
+ * The first checkpoint not covered at `position`: what was recorded there, else what its kind
136
+ * implies. A checkpoint item covers its checkpoint; anything else only the checkpoints before it.
137
+ */
138
+ export function nextUncoveredAt(position: StreamPosition, family: StreamFamily): bigint | null {
139
+ if (position.nextUncovered !== null) return position.nextUncovered;
140
+ const { kind, checkpoint } = position;
141
+ if (checkpoint === null) return null;
142
+ return kind === 'Item' && family === 'checkpoints' ? checkpoint + 1n : checkpoint;
143
+ }
@@ -0,0 +1,147 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { abortableAsyncGenerator, sleep } from '../../utils/abort.js';
5
+ import { MvrRequestError } from '../mvr.js';
6
+ import type { SuiClientTypes } from '../types.js';
7
+ import type { CoreFrame, LedgerStreamAdapter } from './adapter.js';
8
+ import { StreamDisconnected, SuiStreamError } from './errors.js';
9
+ import { followLive } from './follow.js';
10
+ import { isEmptyRange, planStream, validateOptions, type StreamPlan } from './plan.js';
11
+ import { StreamSession } from './session.js';
12
+ import { StatusReporter } from './status.js';
13
+
14
+ /**
15
+ * About two minutes of failures without progress with the default delays: long enough for a node
16
+ * restart or a load balancer failover, short enough that a dead endpoint or an error that recurs
17
+ * on every attempt surfaces. Progress resets the count.
18
+ */
19
+ const DEFAULT_MAX_ATTEMPTS = 10;
20
+
21
+ /** A resumable ledger stream. Nothing runs, not even `createAdapter`, before the first `next()`. */
22
+ export function createLedgerStream<Item, Frame extends { resumeToken: string }>(
23
+ options: SuiClientTypes.StreamOptions<SuiClientTypes.StreamFrameOptions>,
24
+ createAdapter: () => LedgerStreamAdapter<Item, Frame>,
25
+ ): AsyncGenerator<CoreFrame<Frame>> {
26
+ return abortableAsyncGenerator(
27
+ (signal) => runStream(options, createAdapter(), signal),
28
+ options.signal,
29
+ );
30
+ }
31
+
32
+ async function* runStream<Item, Frame extends { resumeToken: string }>(
33
+ options: SuiClientTypes.StreamOptions<SuiClientTypes.StreamFrameOptions>,
34
+ adapter: LedgerStreamAdapter<Item, Frame>,
35
+ signal: AbortSignal,
36
+ ): AsyncGenerator<CoreFrame<Frame>, void> {
37
+ const status = new StatusReporter(options.onStatus);
38
+ const retry = retryDelays(options.retry);
39
+ const validated = validateOptions(options, adapter);
40
+ let attempt = 0;
41
+
42
+ /** Waits, then reports the next attempt, or throws when `error` ends the stream. */
43
+ async function backOff(error: unknown, advanced: boolean) {
44
+ const reason =
45
+ signal.aborted || status.callbackFailed || error instanceof SuiStreamError
46
+ ? undefined
47
+ : error instanceof StreamDisconnected
48
+ ? 'Unavailable'
49
+ : error instanceof MvrRequestError
50
+ ? error.reason
51
+ : adapter.retryReason(error);
52
+ if (!reason) throw error;
53
+ attempt = advanced ? 1 : attempt + 1;
54
+ if (attempt > retry.maxAttempts) {
55
+ throw error instanceof StreamDisconnected
56
+ ? new SuiStreamError('Disconnected', 'Live feed kept disconnecting without progress', {
57
+ cause: error,
58
+ })
59
+ : error;
60
+ }
61
+ const delay = retry.delay(attempt);
62
+ status.retrying(attempt, delay, reason, error);
63
+ await sleep(delay, signal);
64
+ status.connecting(attempt);
65
+ }
66
+ async function withRetries<T>(request: () => Promise<T>): Promise<T> {
67
+ while (true) {
68
+ try {
69
+ return await request();
70
+ } catch (error) {
71
+ await backOff(error, false);
72
+ }
73
+ }
74
+ }
75
+
76
+ status.connecting(0);
77
+ const identity = await withRetries(() => adapter.initialize(signal));
78
+ if (!identity.chain) throw new SuiStreamError('Protocol', 'Stream chain identity is missing');
79
+ const plan = await planStream(
80
+ validated,
81
+ identity,
82
+ adapter,
83
+ async () => identity.indexedTip ?? (await withRetries(() => adapter.indexedTip(signal))),
84
+ );
85
+ const session = new StreamSession({
86
+ plan,
87
+ identity,
88
+ adapter,
89
+ frames: options.frames,
90
+ status,
91
+ signal,
92
+ });
93
+ // Startup retries don't spend the stream's budget.
94
+ attempt = 0;
95
+ if (isEmptyRange(plan, adapter.family)) {
96
+ yield* session.complete();
97
+ return;
98
+ }
99
+
100
+ while (true) {
101
+ session.beginAttempt();
102
+ try {
103
+ yield* runAttempt(plan, session, adapter);
104
+ return;
105
+ } catch (error) {
106
+ await backOff(error, session.advancedThisAttempt());
107
+ }
108
+ }
109
+ }
110
+
111
+ // One attempt. Only a finite stream returns; the other modes end by throwing.
112
+ async function* runAttempt<Item, Frame extends { resumeToken: string }>(
113
+ plan: StreamPlan,
114
+ session: StreamSession<Item, Frame>,
115
+ adapter: LedgerStreamAdapter<Item, Frame>,
116
+ ): AsyncGenerator<CoreFrame<Frame>, void> {
117
+ switch (plan.mode) {
118
+ case 'finite':
119
+ yield* session.replay(plan.end);
120
+ yield* session.complete();
121
+ return;
122
+ case 'poll':
123
+ while (true) {
124
+ yield* session.replay(undefined);
125
+ await sleep(plan.pollInterval, session.signal);
126
+ }
127
+ case 'follow':
128
+ return yield* followLive(session, adapter);
129
+ }
130
+ }
131
+
132
+ /** Exponential backoff, less up to 20% jitter. */
133
+ function retryDelays(options: SuiClientTypes.StreamRetryOptions = {}) {
134
+ const initialDelay = nonNegative(options.initialDelay ?? 250, 'retry.initialDelay');
135
+ const maxDelay = nonNegative(options.maxDelay ?? 30_000, 'retry.maxDelay');
136
+ const maxAttempts = nonNegative(options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS, 'retry.maxAttempts');
137
+ return {
138
+ maxAttempts,
139
+ delay: (attempt: number) =>
140
+ Math.min(maxDelay, initialDelay * 2 ** Math.min(attempt - 1, 52)) * (1 - Math.random() / 5),
141
+ };
142
+ }
143
+
144
+ function nonNegative(value: number, name: string): number {
145
+ if (!(value >= 0)) throw new SuiStreamError('InvalidOptions', `${name} must not be negative`);
146
+ return value;
147
+ }