@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.
- package/CHANGELOG.md +160 -0
- package/README.md +5 -188
- package/dist/bcs/bcs.d.mts +6 -6
- package/dist/bcs/bcs.d.mts.map +1 -1
- package/dist/bcs/bcs.mjs +49 -22
- package/dist/bcs/bcs.mjs.map +1 -1
- package/dist/bcs/effects.mjs +49 -20
- package/dist/bcs/effects.mjs.map +1 -1
- package/dist/bcs/index.d.mts +1441 -624
- package/dist/bcs/index.d.mts.map +1 -1
- package/dist/bcs/index.mjs +2 -1
- package/dist/bcs/index.mjs.map +1 -1
- package/dist/bcs/transactions.d.mts +1059 -0
- package/dist/bcs/transactions.d.mts.map +1 -0
- package/dist/bcs/transactions.mjs +33 -14
- package/dist/bcs/transactions.mjs.map +1 -1
- package/dist/bcs/types.d.mts +6 -1
- package/dist/bcs/types.d.mts.map +1 -1
- package/dist/client/core-resolver.mjs +4 -4
- package/dist/client/core-resolver.mjs.map +1 -1
- package/dist/client/core.d.mts +55 -4
- package/dist/client/core.d.mts.map +1 -1
- package/dist/client/core.mjs +98 -1
- package/dist/client/core.mjs.map +1 -1
- package/dist/client/errors.mjs +11 -1
- package/dist/client/errors.mjs.map +1 -1
- package/dist/client/index.d.mts +4 -2
- package/dist/client/index.mjs +5 -2
- package/dist/client/mvr.d.mts +12 -2
- package/dist/client/mvr.d.mts.map +1 -1
- package/dist/client/mvr.mjs +34 -13
- package/dist/client/mvr.mjs.map +1 -1
- package/dist/client/query-filters.mjs +80 -76
- package/dist/client/query-filters.mjs.map +1 -1
- package/dist/client/stream/adapter.d.mts +102 -0
- package/dist/client/stream/adapter.d.mts.map +1 -0
- package/dist/client/stream/errors.d.mts +14 -0
- package/dist/client/stream/errors.d.mts.map +1 -0
- package/dist/client/stream/errors.mjs +25 -0
- package/dist/client/stream/errors.mjs.map +1 -0
- package/dist/client/stream/filter.mjs +18 -0
- package/dist/client/stream/filter.mjs.map +1 -0
- package/dist/client/stream/follow.mjs +48 -0
- package/dist/client/stream/follow.mjs.map +1 -0
- package/dist/client/stream/index.d.mts +4 -0
- package/dist/client/stream/pager.mjs +123 -0
- package/dist/client/stream/pager.mjs.map +1 -0
- package/dist/client/stream/plan.mjs +79 -0
- package/dist/client/stream/plan.mjs.map +1 -0
- package/dist/client/stream/position.d.mts +30 -0
- package/dist/client/stream/position.d.mts.map +1 -0
- package/dist/client/stream/position.mjs +121 -0
- package/dist/client/stream/position.mjs.map +1 -0
- package/dist/client/stream/run.d.mts +2 -0
- package/dist/client/stream/run.mjs +99 -0
- package/dist/client/stream/run.mjs.map +1 -0
- package/dist/client/stream/session.mjs +205 -0
- package/dist/client/stream/session.mjs.map +1 -0
- package/dist/client/stream/status.mjs +47 -0
- package/dist/client/stream/status.mjs.map +1 -0
- package/dist/client/stream/token.mjs +80 -0
- package/dist/client/stream/token.mjs.map +1 -0
- package/dist/client/transaction-data.d.mts +12 -0
- package/dist/client/transaction-data.d.mts.map +1 -0
- package/dist/client/transaction-data.mjs +469 -0
- package/dist/client/transaction-data.mjs.map +1 -0
- package/dist/client/transaction-resolver.mjs +102 -58
- package/dist/client/transaction-resolver.mjs.map +1 -1
- package/dist/client/types.d.mts +560 -240
- package/dist/client/types.d.mts.map +1 -1
- package/dist/client/utils.d.mts +3 -1
- package/dist/client/utils.d.mts.map +1 -1
- package/dist/client/utils.mjs +91 -26
- package/dist/client/utils.mjs.map +1 -1
- package/dist/cryptography/index.d.mts +1 -1
- package/dist/cryptography/keypair.d.mts +1 -1
- package/dist/cryptography/publickey.d.mts +11 -0
- package/dist/cryptography/publickey.d.mts.map +1 -1
- package/dist/cryptography/publickey.mjs +12 -1
- package/dist/cryptography/publickey.mjs.map +1 -1
- package/dist/cryptography/signature.d.mts +2 -2
- package/dist/cryptography/signature.mjs +18 -7
- package/dist/cryptography/signature.mjs.map +1 -1
- package/dist/graphql/client.d.mts +51 -35
- package/dist/graphql/client.d.mts.map +1 -1
- package/dist/graphql/client.mjs +125 -69
- package/dist/graphql/client.mjs.map +1 -1
- package/dist/graphql/core.d.mts +14 -9
- package/dist/graphql/core.d.mts.map +1 -1
- package/dist/graphql/core.mjs +293 -117
- package/dist/graphql/core.mjs.map +1 -1
- package/dist/graphql/errors.d.mts +20 -0
- package/dist/graphql/errors.d.mts.map +1 -0
- package/dist/graphql/errors.mjs +19 -0
- package/dist/graphql/errors.mjs.map +1 -0
- package/dist/graphql/filters.mjs +32 -0
- package/dist/graphql/filters.mjs.map +1 -0
- package/dist/graphql/generated/queries.d.mts.map +1 -1
- package/dist/graphql/generated/queries.mjs +1216 -208
- package/dist/graphql/generated/queries.mjs.map +1 -1
- package/dist/graphql/generated/tada-env.d.mts +820 -11
- package/dist/graphql/index.d.mts +2 -1
- package/dist/graphql/index.mjs +2 -1
- package/dist/graphql/sse-parser.mjs +82 -0
- package/dist/graphql/sse-parser.mjs.map +1 -0
- package/dist/graphql/stream/families.mjs +306 -0
- package/dist/graphql/stream/families.mjs.map +1 -0
- package/dist/graphql/stream/index.mjs +38 -0
- package/dist/graphql/stream/index.mjs.map +1 -0
- package/dist/graphql/stream/live.mjs +60 -0
- package/dist/graphql/stream/live.mjs.map +1 -0
- package/dist/graphql/stream/page.mjs +38 -0
- package/dist/graphql/stream/page.mjs.map +1 -0
- package/dist/graphql/stream/query.mjs +47 -0
- package/dist/graphql/stream/query.mjs.map +1 -0
- package/dist/graphql/subscribe.mjs +70 -0
- package/dist/graphql/subscribe.mjs.map +1 -0
- package/dist/grpc/client.d.mts +31 -27
- package/dist/grpc/client.d.mts.map +1 -1
- package/dist/grpc/client.mjs +42 -46
- package/dist/grpc/client.mjs.map +1 -1
- package/dist/grpc/core.d.mts +13 -6
- package/dist/grpc/core.d.mts.map +1 -1
- package/dist/grpc/core.mjs +254 -166
- package/dist/grpc/core.mjs.map +1 -1
- package/dist/grpc/filters.mjs +44 -34
- package/dist/grpc/filters.mjs.map +1 -1
- package/dist/grpc/grpc-web-pull.mjs +245 -0
- package/dist/grpc/grpc-web-pull.mjs.map +1 -0
- package/dist/grpc/index.d.mts +2 -1
- package/dist/grpc/index.mjs +1 -1
- package/dist/grpc/network-error.mjs +31 -0
- package/dist/grpc/network-error.mjs.map +1 -0
- package/dist/grpc/proto/google/rpc/error_details.mjs +306 -0
- package/dist/grpc/proto/google/rpc/error_details.mjs.map +1 -0
- package/dist/grpc/proto/sui/rpc/v2/error_reason.d.mts +176 -1
- package/dist/grpc/proto/sui/rpc/v2/error_reason.d.mts.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/error_reason.mjs +175 -0
- package/dist/grpc/proto/sui/rpc/v2/error_reason.mjs.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/executed_transaction.d.mts +2 -2
- package/dist/grpc/proto/sui/rpc/v2/ledger_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/ledger_service.d.mts +111 -0
- package/dist/grpc/proto/sui/rpc/v2/ledger_service.d.mts.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/ledger_service.mjs +46 -0
- package/dist/grpc/proto/sui/rpc/v2/ledger_service.mjs.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/move_package_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/move_package_service.d.mts +24 -1
- package/dist/grpc/proto/sui/rpc/v2/move_package_service.d.mts.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/move_package_service.mjs +25 -7
- package/dist/grpc/proto/sui/rpc/v2/move_package_service.mjs.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/object.mjs +1 -1
- package/dist/grpc/proto/sui/rpc/v2/query_options.d.mts +111 -9
- package/dist/grpc/proto/sui/rpc/v2/query_options.d.mts.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/query_options.mjs +63 -19
- package/dist/grpc/proto/sui/rpc/v2/query_options.mjs.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/signature.d.mts +5 -5
- package/dist/grpc/proto/sui/rpc/v2/signature.mjs.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/signature_scheme.d.mts +5 -1
- package/dist/grpc/proto/sui/rpc/v2/signature_scheme.d.mts.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/signature_scheme.mjs +4 -0
- package/dist/grpc/proto/sui/rpc/v2/signature_scheme.mjs.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.d.mts +20 -1
- package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.d.mts.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.mjs +30 -13
- package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.mjs.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/state_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.d.mts +16 -10
- package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.d.mts.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.mjs +8 -5
- package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.mjs.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/transaction.d.mts +11 -5
- package/dist/grpc/proto/sui/rpc/v2/transaction.d.mts.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/transaction.mjs +6 -0
- package/dist/grpc/proto/sui/rpc/v2/transaction.mjs.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/transaction_execution_service.d.mts +6 -2
- package/dist/grpc/proto/sui/rpc/v2/transaction_execution_service.d.mts.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/transaction_execution_service.mjs.map +1 -1
- package/dist/grpc/proto/types.d.mts +7 -7
- package/dist/grpc/proto/types.mjs +3 -2
- package/dist/grpc/pull-call.d.mts +20 -0
- package/dist/grpc/pull-call.d.mts.map +1 -0
- package/dist/grpc/pull-call.mjs +28 -0
- package/dist/grpc/pull-call.mjs.map +1 -0
- package/dist/grpc/stream/call.mjs +238 -0
- package/dist/grpc/stream/call.mjs.map +1 -0
- package/dist/grpc/stream/families.d.mts +26 -0
- package/dist/grpc/stream/families.d.mts.map +1 -0
- package/dist/grpc/stream/families.mjs +145 -0
- package/dist/grpc/stream/families.mjs.map +1 -0
- package/dist/grpc/stream/index.d.mts +41 -0
- package/dist/grpc/stream/index.d.mts.map +1 -0
- package/dist/grpc/stream/index.mjs +127 -0
- package/dist/grpc/stream/index.mjs.map +1 -0
- package/dist/grpc/stream/live.mjs +31 -0
- package/dist/grpc/stream/live.mjs.map +1 -0
- package/dist/grpc/stream/page.mjs +120 -0
- package/dist/grpc/stream/page.mjs.map +1 -0
- package/dist/grpc/stream-types.d.mts +28 -0
- package/dist/grpc/stream-types.d.mts.map +1 -0
- package/dist/grpc/transport.d.mts +8 -3
- package/dist/grpc/transport.d.mts.map +1 -1
- package/dist/grpc/transport.mjs +88 -45
- package/dist/grpc/transport.mjs.map +1 -1
- package/dist/jsonRpc/index.d.mts +34 -12
- package/dist/jsonRpc/index.d.mts.map +1 -0
- package/dist/jsonRpc/index.mjs +39 -5
- package/dist/jsonRpc/index.mjs.map +1 -0
- package/dist/jsonRpc/types.d.mts +420 -0
- package/dist/jsonRpc/types.d.mts.map +1 -0
- package/dist/keypairs/passkey/keypair.d.mts +1 -1
- package/dist/multisig/publickey.d.mts +12 -4
- package/dist/multisig/publickey.d.mts.map +1 -1
- package/dist/multisig/publickey.mjs +5 -5
- package/dist/multisig/publickey.mjs.map +1 -1
- package/dist/multisig/signer.d.mts +1 -1
- package/dist/transactions/ObjectCache.d.mts +1 -1
- package/dist/transactions/ObjectCache.mjs +5 -5
- package/dist/transactions/ObjectCache.mjs.map +1 -1
- package/dist/transactions/Transaction.d.mts +11 -10
- package/dist/transactions/Transaction.d.mts.map +1 -1
- package/dist/transactions/Transaction.mjs +17 -6
- package/dist/transactions/Transaction.mjs.map +1 -1
- package/dist/transactions/data/internal.d.mts +135 -133
- package/dist/transactions/data/internal.d.mts.map +1 -1
- package/dist/transactions/data/internal.mjs +2 -1
- package/dist/transactions/data/internal.mjs.map +1 -1
- package/dist/transactions/data/v1.d.mts +239 -239
- package/dist/transactions/data/v1.d.mts.map +1 -1
- package/dist/transactions/data/v2.d.mts +16 -16
- package/dist/transactions/data/v2.d.mts.map +1 -1
- package/dist/transactions/executor/parallel.d.mts +2 -2
- package/dist/transactions/executor/serial.d.mts +1 -1
- package/dist/transactions/executor/serial.mjs +2 -2
- package/dist/transactions/executor/serial.mjs.map +1 -1
- package/dist/transactions/index.d.mts +3 -2
- package/dist/transactions/index.mjs +4 -3
- package/dist/transactions/intents/AllowanceBalance.mjs +1 -1
- package/dist/transactions/intents/CoinWithBalance.mjs +1 -1
- package/dist/transactions/resolve.d.mts +6 -0
- package/dist/transactions/resolve.d.mts.map +1 -1
- package/dist/transactions/resolve.mjs.map +1 -1
- package/dist/transactions/restricted-move-calls.d.mts +25 -0
- package/dist/transactions/restricted-move-calls.d.mts.map +1 -0
- package/dist/transactions/restricted-move-calls.mjs +47 -0
- package/dist/transactions/restricted-move-calls.mjs.map +1 -0
- package/dist/transactions/serializer.d.mts +1 -4
- package/dist/transactions/serializer.d.mts.map +1 -1
- package/dist/transactions/serializer.mjs +1 -44
- package/dist/transactions/serializer.mjs.map +1 -1
- package/dist/transactions/utils.d.mts +0 -1
- package/dist/transactions/utils.d.mts.map +1 -1
- package/dist/transactions/utils.mjs.map +1 -1
- package/dist/utils/abort.mjs +89 -0
- package/dist/utils/abort.mjs.map +1 -0
- package/dist/utils/coin-reservation.mjs +1 -4
- package/dist/utils/coin-reservation.mjs.map +1 -1
- package/dist/utils/index.mjs +1 -1
- package/dist/utils/sui-types.mjs +1 -1
- package/dist/verify/index.d.mts +2 -2
- package/dist/verify/verify.d.mts +74 -24
- package/dist/verify/verify.d.mts.map +1 -1
- package/dist/verify/verify.mjs +128 -42
- package/dist/verify/verify.mjs.map +1 -1
- package/dist/version.mjs +1 -1
- package/dist/version.mjs.map +1 -1
- package/dist/zklogin/bcs.d.mts +14 -14
- package/dist/zklogin/publickey.d.mts.map +1 -1
- package/dist/zklogin/publickey.mjs +15 -20
- package/dist/zklogin/publickey.mjs.map +1 -1
- package/docs/bcs.md +2 -2
- package/docs/clients/core.md +19 -18
- package/docs/clients/executing.md +53 -8
- package/docs/clients/graphql.md +81 -11
- package/docs/clients/grpc.md +31 -15
- package/docs/clients/index.md +8 -7
- package/docs/clients/querying.md +134 -30
- package/docs/clients/streaming.md +324 -0
- package/docs/cryptography/index.md +85 -58
- package/docs/llms-index.md +4 -2
- package/docs/migrations/sui-2.0/dapp-kit.md +3 -2
- package/docs/migrations/sui-2.0/json-rpc-migration.md +106 -79
- package/docs/migrations/sui-3.0/index.md +250 -0
- package/docs/plugins.md +10 -10
- package/docs/sdk-building.md +3 -4
- package/docs/transactions/coins-and-balances.md +6 -11
- package/docs/transactions/reference.md +36 -1
- package/package.json +2 -3
- package/src/bcs/bcs.ts +80 -41
- package/src/bcs/effects.ts +61 -25
- package/src/bcs/index.ts +2 -0
- package/src/bcs/transactions.ts +36 -17
- package/src/bcs/types.ts +10 -4
- package/src/client/core-resolver.ts +3 -5
- package/src/client/core.ts +180 -8
- package/src/client/errors.ts +12 -0
- package/src/client/index.ts +4 -1
- package/src/client/mvr.ts +37 -10
- package/src/client/query-filters.ts +151 -97
- package/src/client/stream/adapter.ts +110 -0
- package/src/client/stream/errors.ts +40 -0
- package/src/client/stream/filter.ts +17 -0
- package/src/client/stream/follow.ts +63 -0
- package/src/client/stream/index.ts +9 -0
- package/src/client/stream/pager.ts +158 -0
- package/src/client/stream/plan.ts +147 -0
- package/src/client/stream/position.ts +143 -0
- package/src/client/stream/run.ts +147 -0
- package/src/client/stream/session.ts +259 -0
- package/src/client/stream/status.ts +44 -0
- package/src/client/stream/token.ts +104 -0
- package/src/client/transaction-data.ts +587 -0
- package/src/client/transaction-resolver.ts +158 -69
- package/src/client/types.ts +530 -115
- package/src/client/utils.ts +147 -50
- package/src/cryptography/publickey.ts +12 -1
- package/src/cryptography/signature.ts +6 -3
- package/src/graphql/client.ts +204 -135
- package/src/graphql/core.ts +446 -139
- package/src/graphql/errors.ts +24 -0
- package/src/graphql/filters.ts +48 -0
- package/src/graphql/generated/queries.ts +1455 -277
- package/src/graphql/generated/schema.graphql +282 -9
- package/src/graphql/generated/tada-env.ts +933 -4
- package/src/graphql/queries/checkpoints.graphql +34 -0
- package/src/graphql/queries/getDynamicFields.graphql +1 -0
- package/src/graphql/queries/listEvents.graphql +16 -24
- package/src/graphql/queries/streams.graphql +212 -0
- package/src/graphql/queries/transactions.graphql +71 -28
- package/src/graphql/queries/verifySignature.graphql +17 -0
- package/src/graphql/sse-parser.ts +99 -0
- package/src/graphql/stream/families.ts +515 -0
- package/src/graphql/stream/index.ts +55 -0
- package/src/graphql/stream/live.ts +70 -0
- package/src/graphql/stream/page.ts +47 -0
- package/src/graphql/stream/query.ts +52 -0
- package/src/graphql/subscribe.ts +83 -0
- package/src/grpc/client.ts +81 -79
- package/src/grpc/core.ts +388 -274
- package/src/grpc/filters.ts +59 -60
- package/src/grpc/grpc-web-pull.ts +360 -0
- package/src/grpc/index.ts +5 -0
- package/src/grpc/network-error.ts +33 -0
- package/src/grpc/proto/sui/rpc/v2/error_reason.ts +175 -0
- package/src/grpc/proto/sui/rpc/v2/ledger_service.ts +145 -0
- package/src/grpc/proto/sui/rpc/v2/move_package_service.ts +43 -1
- package/src/grpc/proto/sui/rpc/v2/query_options.ts +132 -8
- package/src/grpc/proto/sui/rpc/v2/signature.ts +4 -4
- package/src/grpc/proto/sui/rpc/v2/signature_scheme.ts +4 -0
- package/src/grpc/proto/sui/rpc/v2/signature_verification_service.ts +30 -0
- package/src/grpc/proto/sui/rpc/v2/subscription_service.client.ts +16 -10
- package/src/grpc/proto/sui/rpc/v2/transaction.ts +6 -0
- package/src/grpc/proto/sui/rpc/v2/transaction_execution_service.ts +5 -1
- package/src/grpc/pull-call.ts +54 -0
- package/src/grpc/stream/call.ts +317 -0
- package/src/grpc/stream/families.ts +228 -0
- package/src/grpc/stream/index.ts +182 -0
- package/src/grpc/stream/live.ts +33 -0
- package/src/grpc/stream/page.ts +161 -0
- package/src/grpc/stream-types.ts +33 -0
- package/src/grpc/transport.ts +116 -59
- package/src/jsonRpc/index.ts +60 -17
- package/src/jsonRpc/types.ts +421 -0
- package/src/multisig/publickey.ts +19 -7
- package/src/transactions/ObjectCache.ts +6 -6
- package/src/transactions/Transaction.ts +30 -6
- package/src/transactions/data/internal.ts +2 -0
- package/src/transactions/executor/serial.ts +2 -2
- package/src/transactions/index.ts +8 -1
- package/src/transactions/resolve.ts +6 -0
- package/src/transactions/restricted-move-calls.ts +87 -0
- package/src/transactions/serializer.ts +0 -70
- package/src/transactions/utils.ts +0 -37
- package/src/utils/abort.ts +101 -0
- package/src/verify/index.ts +1 -0
- package/src/verify/verify.ts +190 -62
- package/src/version.ts +1 -1
- package/src/zklogin/publickey.ts +18 -34
- package/dist/client/core-resolver.d.mts +0 -9
- package/dist/client/core-resolver.d.mts.map +0 -1
- package/dist/jsonRpc/client.d.mts +0 -507
- package/dist/jsonRpc/client.d.mts.map +0 -1
- package/dist/jsonRpc/client.mjs +0 -868
- package/dist/jsonRpc/client.mjs.map +0 -1
- package/dist/jsonRpc/core.d.mts +0 -168
- package/dist/jsonRpc/core.d.mts.map +0 -1
- package/dist/jsonRpc/core.mjs +0 -1054
- package/dist/jsonRpc/core.mjs.map +0 -1
- package/dist/jsonRpc/errors.d.mts +0 -27
- package/dist/jsonRpc/errors.d.mts.map +0 -1
- package/dist/jsonRpc/errors.mjs +0 -50
- package/dist/jsonRpc/errors.mjs.map +0 -1
- package/dist/jsonRpc/http-transport.d.mts +0 -62
- package/dist/jsonRpc/http-transport.d.mts.map +0 -1
- package/dist/jsonRpc/http-transport.mjs +0 -60
- package/dist/jsonRpc/http-transport.mjs.map +0 -1
- package/dist/jsonRpc/network.d.mts +0 -9
- package/dist/jsonRpc/network.d.mts.map +0 -1
- package/dist/jsonRpc/network.mjs +0 -18
- package/dist/jsonRpc/network.mjs.map +0 -1
- package/dist/jsonRpc/types/chain.d.mts +0 -169
- package/dist/jsonRpc/types/chain.d.mts.map +0 -1
- package/dist/jsonRpc/types/changes.d.mts +0 -48
- package/dist/jsonRpc/types/changes.d.mts.map +0 -1
- package/dist/jsonRpc/types/coins.d.mts +0 -15
- package/dist/jsonRpc/types/coins.d.mts.map +0 -1
- package/dist/jsonRpc/types/common.d.mts +0 -9
- package/dist/jsonRpc/types/common.d.mts.map +0 -1
- package/dist/jsonRpc/types/generated.d.mts +0 -1976
- package/dist/jsonRpc/types/generated.d.mts.map +0 -1
- package/dist/jsonRpc/types/index.d.mts +0 -6
- package/dist/jsonRpc/types/params.d.mts +0 -898
- package/dist/jsonRpc/types/params.d.mts.map +0 -1
- package/src/graphql/generated/tsconfig.tada.json +0 -11
- package/src/graphql/queries/verifyZkLoginSignature.graphql +0 -15
- package/src/jsonRpc/client.ts +0 -1252
- package/src/jsonRpc/core.ts +0 -1557
- package/src/jsonRpc/errors.ts +0 -57
- package/src/jsonRpc/http-transport.ts +0 -118
- package/src/jsonRpc/network.ts +0 -21
- package/src/jsonRpc/types/chain.ts +0 -190
- package/src/jsonRpc/types/changes.ts +0 -35
- package/src/jsonRpc/types/coins.ts +0 -14
- package/src/jsonRpc/types/common.ts +0 -8
- package/src/jsonRpc/types/generated.ts +0 -2165
- package/src/jsonRpc/types/index.ts +0 -9
- package/src/jsonRpc/types/params.ts +0 -908
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { bcs } from "@mysten/bcs";
|
|
2
|
+
|
|
3
|
+
//#region src/client/stream/position.ts
|
|
4
|
+
const PositionBcs = bcs.struct("StreamPosition", {
|
|
5
|
+
kind: bcs.enum("StreamPositionKind", {
|
|
6
|
+
Item: null,
|
|
7
|
+
Boundary: null,
|
|
8
|
+
Progress: null
|
|
9
|
+
}),
|
|
10
|
+
cursor: bcs.option(bcs.byteVector()),
|
|
11
|
+
checkpoint: bcs.option(bcs.u64()),
|
|
12
|
+
transactionIndex: bcs.option(bcs.u64()),
|
|
13
|
+
eventIndex: bcs.option(bcs.u32()),
|
|
14
|
+
itemId: bcs.option(bcs.string()),
|
|
15
|
+
nextUncovered: bcs.option(bcs.u64())
|
|
16
|
+
});
|
|
17
|
+
const optionalBigInt = (value) => value === null ? null : BigInt(value);
|
|
18
|
+
const positionSchema = PositionBcs.transform({
|
|
19
|
+
input: (position) => ({
|
|
20
|
+
...position,
|
|
21
|
+
kind: { [position.kind]: true }
|
|
22
|
+
}),
|
|
23
|
+
output: (value) => ({
|
|
24
|
+
kind: value.kind.$kind,
|
|
25
|
+
cursor: value.cursor === null ? null : Uint8Array.from(value.cursor),
|
|
26
|
+
checkpoint: optionalBigInt(value.checkpoint),
|
|
27
|
+
transactionIndex: optionalBigInt(value.transactionIndex),
|
|
28
|
+
eventIndex: value.eventIndex,
|
|
29
|
+
itemId: value.itemId,
|
|
30
|
+
nextUncovered: optionalBigInt(value.nextUncovered)
|
|
31
|
+
})
|
|
32
|
+
});
|
|
33
|
+
const EMPTY = {
|
|
34
|
+
cursor: null,
|
|
35
|
+
checkpoint: null,
|
|
36
|
+
transactionIndex: null,
|
|
37
|
+
eventIndex: null,
|
|
38
|
+
itemId: null,
|
|
39
|
+
nextUncovered: null
|
|
40
|
+
};
|
|
41
|
+
function itemPosition(checkpoint, fields = {}) {
|
|
42
|
+
return {
|
|
43
|
+
...EMPTY,
|
|
44
|
+
...fields,
|
|
45
|
+
kind: "Item",
|
|
46
|
+
checkpoint
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The boundary just before `checkpoint`, or with indexes, just before that key within it. Zero
|
|
51
|
+
* indexes are the plain boundary before `checkpoint`, which resumes without a cursor.
|
|
52
|
+
*/
|
|
53
|
+
function boundaryPosition(checkpoint, fields = {}) {
|
|
54
|
+
const position = {
|
|
55
|
+
...EMPTY,
|
|
56
|
+
...fields,
|
|
57
|
+
kind: "Boundary",
|
|
58
|
+
checkpoint
|
|
59
|
+
};
|
|
60
|
+
if (fields.transactionIndex || fields.eventIndex) return position;
|
|
61
|
+
return {
|
|
62
|
+
...position,
|
|
63
|
+
cursor: null,
|
|
64
|
+
transactionIndex: null,
|
|
65
|
+
eventIndex: null
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function progressPosition(cursor) {
|
|
69
|
+
return {
|
|
70
|
+
...EMPTY,
|
|
71
|
+
kind: "Progress",
|
|
72
|
+
cursor
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** Whether items of the position's checkpoint can lie on both sides of it. */
|
|
76
|
+
function insideCheckpoint(position) {
|
|
77
|
+
return position.kind === "Item" || position.transactionIndex !== null;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Ledger order of two positions from their coordinates. Undefined where coordinates don't decide
|
|
81
|
+
* it: a `Progress` position, or two items of one checkpoint whose places are not reported.
|
|
82
|
+
*/
|
|
83
|
+
function comparePositions(a, b) {
|
|
84
|
+
if (a.checkpoint === null || b.checkpoint === null) return void 0;
|
|
85
|
+
if (a.checkpoint !== b.checkpoint) return order(a.checkpoint, b.checkpoint);
|
|
86
|
+
if (a.kind === "Boundary" && b.kind === "Boundary") return compareKeys(a, b);
|
|
87
|
+
if (a.kind === "Boundary") return boundaryToItem(a, b);
|
|
88
|
+
if (b.kind === "Boundary") {
|
|
89
|
+
const comparison = boundaryToItem(b, a);
|
|
90
|
+
return comparison && -comparison;
|
|
91
|
+
}
|
|
92
|
+
if (a.itemId !== null || b.itemId !== null) return a.itemId === b.itemId ? 0 : void 0;
|
|
93
|
+
return compareKeys(a, b);
|
|
94
|
+
}
|
|
95
|
+
function order(a, b) {
|
|
96
|
+
return a === b ? 0 : a < b ? -1 : 1;
|
|
97
|
+
}
|
|
98
|
+
/** Keys within one checkpoint, unset indexes counting as 0. */
|
|
99
|
+
function compareKeys(a, b) {
|
|
100
|
+
return order(a.transactionIndex ?? 0n, b.transactionIndex ?? 0n) || order(BigInt(a.eventIndex ?? 0), BigInt(b.eventIndex ?? 0));
|
|
101
|
+
}
|
|
102
|
+
/** A boundary against an item of its checkpoint: it sits before the item with its key. */
|
|
103
|
+
function boundaryToItem(boundary, item) {
|
|
104
|
+
if (boundary.transactionIndex === null) return -1;
|
|
105
|
+
if (item.transactionIndex === null) return void 0;
|
|
106
|
+
return compareKeys(boundary, item) <= 0 ? -1 : 1;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The first checkpoint not covered at `position`: what was recorded there, else what its kind
|
|
110
|
+
* implies. A checkpoint item covers its checkpoint; anything else only the checkpoints before it.
|
|
111
|
+
*/
|
|
112
|
+
function nextUncoveredAt(position, family) {
|
|
113
|
+
if (position.nextUncovered !== null) return position.nextUncovered;
|
|
114
|
+
const { kind, checkpoint } = position;
|
|
115
|
+
if (checkpoint === null) return null;
|
|
116
|
+
return kind === "Item" && family === "checkpoints" ? checkpoint + 1n : checkpoint;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
//#endregion
|
|
120
|
+
export { boundaryPosition, comparePositions, insideCheckpoint, itemPosition, nextUncoveredAt, positionSchema, progressPosition };
|
|
121
|
+
//# sourceMappingURL=position.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"position.mjs","names":[],"sources":["../../../src/client/stream/position.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { bcs } from '@mysten/bcs';\n\nimport type { StreamFamily } from './adapter.js';\n\n/**\n * Stream positions, shared by every transport. Adapters build positions from what the server\n * reports and turn them back into request cursors; cursors themselves are never parsed or compared.\n *\n * - `Item`: a delivered item in `checkpoint`. Its place within the checkpoint is\n * `transactionIndex`/`eventIndex` where reported, else only `itemId` identifies it.\n * - `Boundary`: just before `checkpoint`, or with `transactionIndex`, just before that key within\n * it (resumed by its cursor).\n * - `Progress`: a cursor the server reported without a checkpoint (GraphQL only).\n *\n * `nextUncovered` is the first checkpoint not known to be delivered at the position, in ascending\n * order: every matching item before it was delivered. Null makes no claim.\n */\nexport interface StreamPosition {\n\tkind: 'Item' | 'Boundary' | 'Progress';\n\tcursor: Uint8Array | null;\n\t/** Null only for `Progress`. */\n\tcheckpoint: bigint | null;\n\ttransactionIndex: bigint | null;\n\teventIndex: number | null;\n\titemId: string | null;\n\tnextUncovered: bigint | null;\n}\n\nconst PositionBcs = bcs.struct('StreamPosition', {\n\tkind: bcs.enum('StreamPositionKind', { Item: null, Boundary: null, Progress: null }),\n\tcursor: bcs.option(bcs.byteVector()),\n\tcheckpoint: bcs.option(bcs.u64()),\n\ttransactionIndex: bcs.option(bcs.u64()),\n\teventIndex: bcs.option(bcs.u32()),\n\titemId: bcs.option(bcs.string()),\n\tnextUncovered: bcs.option(bcs.u64()),\n});\n\nconst optionalBigInt = (value: string | null) => (value === null ? null : BigInt(value));\n\nexport const positionSchema = PositionBcs.transform({\n\tinput: (position: StreamPosition) => ({\n\t\t...position,\n\t\tkind: { [position.kind]: true } as { Item: true } | { Boundary: true } | { Progress: true },\n\t}),\n\toutput: (value): StreamPosition => ({\n\t\tkind: value.kind.$kind,\n\t\tcursor: value.cursor === null ? null : Uint8Array.from(value.cursor),\n\t\tcheckpoint: optionalBigInt(value.checkpoint),\n\t\ttransactionIndex: optionalBigInt(value.transactionIndex),\n\t\teventIndex: value.eventIndex,\n\t\titemId: value.itemId,\n\t\tnextUncovered: optionalBigInt(value.nextUncovered),\n\t}),\n});\n\nconst EMPTY: Omit<StreamPosition, 'kind'> = {\n\tcursor: null,\n\tcheckpoint: null,\n\ttransactionIndex: null,\n\teventIndex: null,\n\titemId: null,\n\tnextUncovered: null,\n};\n\nexport function itemPosition(\n\tcheckpoint: bigint,\n\tfields: Partial<Omit<StreamPosition, 'kind' | 'checkpoint'>> = {},\n): StreamPosition {\n\treturn { ...EMPTY, ...fields, kind: 'Item', checkpoint };\n}\n\n/**\n * The boundary just before `checkpoint`, or with indexes, just before that key within it. Zero\n * indexes are the plain boundary before `checkpoint`, which resumes without a cursor.\n */\nexport function boundaryPosition(\n\tcheckpoint: bigint,\n\tfields: Partial<Omit<StreamPosition, 'kind' | 'checkpoint'>> = {},\n): StreamPosition {\n\tconst position: StreamPosition = { ...EMPTY, ...fields, kind: 'Boundary', checkpoint };\n\tif (fields.transactionIndex || fields.eventIndex) return position;\n\treturn { ...position, cursor: null, transactionIndex: null, eventIndex: null };\n}\n\nexport function progressPosition(cursor: Uint8Array): StreamPosition {\n\treturn { ...EMPTY, kind: 'Progress', cursor };\n}\n\n/** Whether items of the position's checkpoint can lie on both sides of it. */\nexport function insideCheckpoint(position: StreamPosition): boolean {\n\treturn position.kind === 'Item' || position.transactionIndex !== null;\n}\n\n/**\n * Ledger order of two positions from their coordinates. Undefined where coordinates don't decide\n * it: a `Progress` position, or two items of one checkpoint whose places are not reported.\n */\nexport function comparePositions(a: StreamPosition, b: StreamPosition): -1 | 0 | 1 | undefined {\n\tif (a.checkpoint === null || b.checkpoint === null) return undefined;\n\tif (a.checkpoint !== b.checkpoint) return order(a.checkpoint, b.checkpoint);\n\tif (a.kind === 'Boundary' && b.kind === 'Boundary') return compareKeys(a, b);\n\tif (a.kind === 'Boundary') return boundaryToItem(a, b);\n\tif (b.kind === 'Boundary') {\n\t\tconst comparison = boundaryToItem(b, a);\n\t\treturn comparison && (-comparison as -1 | 1);\n\t}\n\tif (a.itemId !== null || b.itemId !== null) return a.itemId === b.itemId ? 0 : undefined;\n\treturn compareKeys(a, b);\n}\n\nfunction order(a: bigint, b: bigint): -1 | 0 | 1 {\n\treturn a === b ? 0 : a < b ? -1 : 1;\n}\n\n/** Keys within one checkpoint, unset indexes counting as 0. */\nfunction compareKeys(a: StreamPosition, b: StreamPosition): -1 | 0 | 1 {\n\treturn (\n\t\torder(a.transactionIndex ?? 0n, b.transactionIndex ?? 0n) ||\n\t\torder(BigInt(a.eventIndex ?? 0), BigInt(b.eventIndex ?? 0))\n\t);\n}\n\n/** A boundary against an item of its checkpoint: it sits before the item with its key. */\nfunction boundaryToItem(boundary: StreamPosition, item: StreamPosition): -1 | 1 | undefined {\n\tif (boundary.transactionIndex === null) return -1;\n\tif (item.transactionIndex === null) return undefined;\n\treturn compareKeys(boundary, item) <= 0 ? -1 : 1;\n}\n\n/**\n * The first checkpoint not covered at `position`: what was recorded there, else what its kind\n * implies. A checkpoint item covers its checkpoint; anything else only the checkpoints before it.\n */\nexport function nextUncoveredAt(position: StreamPosition, family: StreamFamily): bigint | null {\n\tif (position.nextUncovered !== null) return position.nextUncovered;\n\tconst { kind, checkpoint } = position;\n\tif (checkpoint === null) return null;\n\treturn kind === 'Item' && family === 'checkpoints' ? checkpoint + 1n : checkpoint;\n}\n"],"mappings":";;;AA+BA,MAAM,cAAc,IAAI,OAAO,kBAAkB;CAChD,MAAM,IAAI,KAAK,sBAAsB;EAAE,MAAM;EAAM,UAAU;EAAM,UAAU;EAAM,CAAC;CACpF,QAAQ,IAAI,OAAO,IAAI,YAAY,CAAC;CACpC,YAAY,IAAI,OAAO,IAAI,KAAK,CAAC;CACjC,kBAAkB,IAAI,OAAO,IAAI,KAAK,CAAC;CACvC,YAAY,IAAI,OAAO,IAAI,KAAK,CAAC;CACjC,QAAQ,IAAI,OAAO,IAAI,QAAQ,CAAC;CAChC,eAAe,IAAI,OAAO,IAAI,KAAK,CAAC;CACpC,CAAC;AAEF,MAAM,kBAAkB,UAA0B,UAAU,OAAO,OAAO,OAAO,MAAM;AAEvF,MAAa,iBAAiB,YAAY,UAAU;CACnD,QAAQ,cAA8B;EACrC,GAAG;EACH,MAAM,GAAG,SAAS,OAAO,MAAM;EAC/B;CACD,SAAS,WAA2B;EACnC,MAAM,MAAM,KAAK;EACjB,QAAQ,MAAM,WAAW,OAAO,OAAO,WAAW,KAAK,MAAM,OAAO;EACpE,YAAY,eAAe,MAAM,WAAW;EAC5C,kBAAkB,eAAe,MAAM,iBAAiB;EACxD,YAAY,MAAM;EAClB,QAAQ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AI,KAAK;;;AAInC,SAAS,YAAY,GAAmB,GAA+B;AACtE,QACC,MAAM,EAAE,oBAAoB,IAAI,EAAE,oBAAoB,GAAG,IACzD,MAAM,OAAO,EAAE,cAAc,EAAE,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC;;;AAK7D,SAAS,eAAe,UAA0B,MAA0C;AAC3F,KAAI,SAAS,qBAAqB,KAAM,QAAO;AAC/C,KAAI,KAAK,qBAAqB,KAAM,QAAO;AAC3C,QAAO,YAAY,UAAU,KAAK,IAAI,IAAI,KAAK;;;;;;AAOhD,SAAgB,gBAAgB,UAA0B,QAAqC;AAC9F,KAAI,SAAS,kBAAkB,KAAM,QAAO,SAAS;CACrD,MAAM,EAAE,MAAM,eAAe;AAC7B,KAAI,eAAe,KAAM,QAAO;AAChC,QAAO,SAAS,UAAU,WAAW,gBAAgB,aAAa,KAAK"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { MvrRequestError } from "../mvr.mjs";
|
|
2
|
+
import { StreamDisconnected, SuiStreamError } from "./errors.mjs";
|
|
3
|
+
import { abortableAsyncGenerator, sleep } from "../../utils/abort.mjs";
|
|
4
|
+
import { followLive } from "./follow.mjs";
|
|
5
|
+
import { isEmptyRange, planStream, validateOptions } from "./plan.mjs";
|
|
6
|
+
import { StreamSession } from "./session.mjs";
|
|
7
|
+
import { StatusReporter } from "./status.mjs";
|
|
8
|
+
|
|
9
|
+
//#region src/client/stream/run.ts
|
|
10
|
+
/**
|
|
11
|
+
* About two minutes of failures without progress with the default delays: long enough for a node
|
|
12
|
+
* restart or a load balancer failover, short enough that a dead endpoint or an error that recurs
|
|
13
|
+
* on every attempt surfaces. Progress resets the count.
|
|
14
|
+
*/
|
|
15
|
+
const DEFAULT_MAX_ATTEMPTS = 10;
|
|
16
|
+
/** A resumable ledger stream. Nothing runs, not even `createAdapter`, before the first `next()`. */
|
|
17
|
+
function createLedgerStream(options, createAdapter) {
|
|
18
|
+
return abortableAsyncGenerator((signal) => runStream(options, createAdapter(), signal), options.signal);
|
|
19
|
+
}
|
|
20
|
+
async function* runStream(options, adapter, signal) {
|
|
21
|
+
const status = new StatusReporter(options.onStatus);
|
|
22
|
+
const retry = retryDelays(options.retry);
|
|
23
|
+
const validated = validateOptions(options, adapter);
|
|
24
|
+
let attempt = 0;
|
|
25
|
+
/** Waits, then reports the next attempt, or throws when `error` ends the stream. */
|
|
26
|
+
async function backOff(error, advanced) {
|
|
27
|
+
const reason = signal.aborted || status.callbackFailed || error instanceof SuiStreamError ? void 0 : error instanceof StreamDisconnected ? "Unavailable" : error instanceof MvrRequestError ? error.reason : adapter.retryReason(error);
|
|
28
|
+
if (!reason) throw error;
|
|
29
|
+
attempt = advanced ? 1 : attempt + 1;
|
|
30
|
+
if (attempt > retry.maxAttempts) throw error instanceof StreamDisconnected ? new SuiStreamError("Disconnected", "Live feed kept disconnecting without progress", { cause: error }) : error;
|
|
31
|
+
const delay = retry.delay(attempt);
|
|
32
|
+
status.retrying(attempt, delay, reason, error);
|
|
33
|
+
await sleep(delay, signal);
|
|
34
|
+
status.connecting(attempt);
|
|
35
|
+
}
|
|
36
|
+
async function withRetries(request) {
|
|
37
|
+
while (true) try {
|
|
38
|
+
return await request();
|
|
39
|
+
} catch (error) {
|
|
40
|
+
await backOff(error, false);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
status.connecting(0);
|
|
44
|
+
const identity = await withRetries(() => adapter.initialize(signal));
|
|
45
|
+
if (!identity.chain) throw new SuiStreamError("Protocol", "Stream chain identity is missing");
|
|
46
|
+
const plan = await planStream(validated, identity, adapter, async () => identity.indexedTip ?? await withRetries(() => adapter.indexedTip(signal)));
|
|
47
|
+
const session = new StreamSession({
|
|
48
|
+
plan,
|
|
49
|
+
identity,
|
|
50
|
+
adapter,
|
|
51
|
+
frames: options.frames,
|
|
52
|
+
status,
|
|
53
|
+
signal
|
|
54
|
+
});
|
|
55
|
+
attempt = 0;
|
|
56
|
+
if (isEmptyRange(plan, adapter.family)) {
|
|
57
|
+
yield* session.complete();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
while (true) {
|
|
61
|
+
session.beginAttempt();
|
|
62
|
+
try {
|
|
63
|
+
yield* runAttempt(plan, session, adapter);
|
|
64
|
+
return;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
await backOff(error, session.advancedThisAttempt());
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async function* runAttempt(plan, session, adapter) {
|
|
71
|
+
switch (plan.mode) {
|
|
72
|
+
case "finite":
|
|
73
|
+
yield* session.replay(plan.end);
|
|
74
|
+
yield* session.complete();
|
|
75
|
+
return;
|
|
76
|
+
case "poll": while (true) {
|
|
77
|
+
yield* session.replay(void 0);
|
|
78
|
+
await sleep(plan.pollInterval, session.signal);
|
|
79
|
+
}
|
|
80
|
+
case "follow": return yield* followLive(session, adapter);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Exponential backoff, less up to 20% jitter. */
|
|
84
|
+
function retryDelays(options = {}) {
|
|
85
|
+
const initialDelay = nonNegative(options.initialDelay ?? 250, "retry.initialDelay");
|
|
86
|
+
const maxDelay = nonNegative(options.maxDelay ?? 3e4, "retry.maxDelay");
|
|
87
|
+
return {
|
|
88
|
+
maxAttempts: nonNegative(options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS, "retry.maxAttempts"),
|
|
89
|
+
delay: (attempt) => Math.min(maxDelay, initialDelay * 2 ** Math.min(attempt - 1, 52)) * (1 - Math.random() / 5)
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function nonNegative(value, name) {
|
|
93
|
+
if (!(value >= 0)) throw new SuiStreamError("InvalidOptions", `${name} must not be negative`);
|
|
94
|
+
return value;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
//#endregion
|
|
98
|
+
export { createLedgerStream };
|
|
99
|
+
//# sourceMappingURL=run.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.mjs","names":[],"sources":["../../../src/client/stream/run.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { abortableAsyncGenerator, sleep } from '../../utils/abort.js';\nimport { MvrRequestError } from '../mvr.js';\nimport type { SuiClientTypes } from '../types.js';\nimport type { CoreFrame, LedgerStreamAdapter } from './adapter.js';\nimport { StreamDisconnected, SuiStreamError } from './errors.js';\nimport { followLive } from './follow.js';\nimport { isEmptyRange, planStream, validateOptions, type StreamPlan } from './plan.js';\nimport { StreamSession } from './session.js';\nimport { StatusReporter } from './status.js';\n\n/**\n * About two minutes of failures without progress with the default delays: long enough for a node\n * restart or a load balancer failover, short enough that a dead endpoint or an error that recurs\n * on every attempt surfaces. Progress resets the count.\n */\nconst DEFAULT_MAX_ATTEMPTS = 10;\n\n/** A resumable ledger stream. Nothing runs, not even `createAdapter`, before the first `next()`. */\nexport function createLedgerStream<Item, Frame extends { resumeToken: string }>(\n\toptions: SuiClientTypes.StreamOptions<SuiClientTypes.StreamFrameOptions>,\n\tcreateAdapter: () => LedgerStreamAdapter<Item, Frame>,\n): AsyncGenerator<CoreFrame<Frame>> {\n\treturn abortableAsyncGenerator(\n\t\t(signal) => runStream(options, createAdapter(), signal),\n\t\toptions.signal,\n\t);\n}\n\nasync function* runStream<Item, Frame extends { resumeToken: string }>(\n\toptions: SuiClientTypes.StreamOptions<SuiClientTypes.StreamFrameOptions>,\n\tadapter: LedgerStreamAdapter<Item, Frame>,\n\tsignal: AbortSignal,\n): AsyncGenerator<CoreFrame<Frame>, void> {\n\tconst status = new StatusReporter(options.onStatus);\n\tconst retry = retryDelays(options.retry);\n\tconst validated = validateOptions(options, adapter);\n\tlet attempt = 0;\n\n\t/** Waits, then reports the next attempt, or throws when `error` ends the stream. */\n\tasync function backOff(error: unknown, advanced: boolean) {\n\t\tconst reason =\n\t\t\tsignal.aborted || status.callbackFailed || error instanceof SuiStreamError\n\t\t\t\t? undefined\n\t\t\t\t: error instanceof StreamDisconnected\n\t\t\t\t\t? 'Unavailable'\n\t\t\t\t\t: error instanceof MvrRequestError\n\t\t\t\t\t\t? error.reason\n\t\t\t\t\t\t: adapter.retryReason(error);\n\t\tif (!reason) throw error;\n\t\tattempt = advanced ? 1 : attempt + 1;\n\t\tif (attempt > retry.maxAttempts) {\n\t\t\tthrow error instanceof StreamDisconnected\n\t\t\t\t? new SuiStreamError('Disconnected', 'Live feed kept disconnecting without progress', {\n\t\t\t\t\t\tcause: error,\n\t\t\t\t\t})\n\t\t\t\t: error;\n\t\t}\n\t\tconst delay = retry.delay(attempt);\n\t\tstatus.retrying(attempt, delay, reason, error);\n\t\tawait sleep(delay, signal);\n\t\tstatus.connecting(attempt);\n\t}\n\tasync function withRetries<T>(request: () => Promise<T>): Promise<T> {\n\t\twhile (true) {\n\t\t\ttry {\n\t\t\t\treturn await request();\n\t\t\t} catch (error) {\n\t\t\t\tawait backOff(error, false);\n\t\t\t}\n\t\t}\n\t}\n\n\tstatus.connecting(0);\n\tconst identity = await withRetries(() => adapter.initialize(signal));\n\tif (!identity.chain) throw new SuiStreamError('Protocol', 'Stream chain identity is missing');\n\tconst plan = await planStream(\n\t\tvalidated,\n\t\tidentity,\n\t\tadapter,\n\t\tasync () => identity.indexedTip ?? (await withRetries(() => adapter.indexedTip(signal))),\n\t);\n\tconst session = new StreamSession({\n\t\tplan,\n\t\tidentity,\n\t\tadapter,\n\t\tframes: options.frames,\n\t\tstatus,\n\t\tsignal,\n\t});\n\t// Startup retries don't spend the stream's budget.\n\tattempt = 0;\n\tif (isEmptyRange(plan, adapter.family)) {\n\t\tyield* session.complete();\n\t\treturn;\n\t}\n\n\twhile (true) {\n\t\tsession.beginAttempt();\n\t\ttry {\n\t\t\tyield* runAttempt(plan, session, adapter);\n\t\t\treturn;\n\t\t} catch (error) {\n\t\t\tawait backOff(error, session.advancedThisAttempt());\n\t\t}\n\t}\n}\n\n// One attempt. Only a finite stream returns; the other modes end by throwing.\nasync function* runAttempt<Item, Frame extends { resumeToken: string }>(\n\tplan: StreamPlan,\n\tsession: StreamSession<Item, Frame>,\n\tadapter: LedgerStreamAdapter<Item, Frame>,\n): AsyncGenerator<CoreFrame<Frame>, void> {\n\tswitch (plan.mode) {\n\t\tcase 'finite':\n\t\t\tyield* session.replay(plan.end);\n\t\t\tyield* session.complete();\n\t\t\treturn;\n\t\tcase 'poll':\n\t\t\twhile (true) {\n\t\t\t\tyield* session.replay(undefined);\n\t\t\t\tawait sleep(plan.pollInterval, session.signal);\n\t\t\t}\n\t\tcase 'follow':\n\t\t\treturn yield* followLive(session, adapter);\n\t}\n}\n\n/** Exponential backoff, less up to 20% jitter. */\nfunction retryDelays(options: SuiClientTypes.StreamRetryOptions = {}) {\n\tconst initialDelay = nonNegative(options.initialDelay ?? 250, 'retry.initialDelay');\n\tconst maxDelay = nonNegative(options.maxDelay ?? 30_000, 'retry.maxDelay');\n\tconst maxAttempts = nonNegative(options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS, 'retry.maxAttempts');\n\treturn {\n\t\tmaxAttempts,\n\t\tdelay: (attempt: number) =>\n\t\t\tMath.min(maxDelay, initialDelay * 2 ** Math.min(attempt - 1, 52)) * (1 - Math.random() / 5),\n\t};\n}\n\nfunction nonNegative(value: number, name: string): number {\n\tif (!(value >= 0)) throw new SuiStreamError('InvalidOptions', `${name} must not be negative`);\n\treturn value;\n}\n"],"mappings":";;;;;;;;;;;;;;AAkBA,MAAM,uBAAuB;;AAG7B,SAAgB,mBACf,SACA,eACmC;AACnC,QAAO,yBACL,WAAW,UAAU,SAAS,eAAe,EAAE,OAAO,EACvD,QAAQ,OACR;;AAGF,gBAAgB,UACf,SACA,SACA,QACyC;CACzC,MAAM,SAAS,IAAI,eAAe,QAAQ,SAAS;CACnD,MAAM,QAAQ,YAAY,QAAQ,MAAM;CACxC,MAAM,YAAY,gBAAgB,SAAS,QAAQ;CACnD,IAAI,UAAU;;CAGd,eAAe,QAAQ,OAAgB,UAAmB;EACzD,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AK,IAAI;AAC/B,UAAO,QAAQ,UAAU;AACzB;EACD,KAAK,OACJ,QAAO,MAAM;AACZ,UAAO,QAAQ,OAAO,OAAU;AAChC,SAAM,MAAM,KAAK,cAAc,QAAQ,OAAO;;EAEhD,KAAK,SACJ,QAAO,OAAO,WAAW,SAAS,QAAQ;;;;AAK7C,SAAS,YAAY,UAA6C,EAAE,EAAE;CACrE,MAAM,eAAe,YAAY,QAAQ,gBAAgB,KAAK,qBAAqB;CACnF,MAAM,WAAW,YAAY,QAAQ,YAAY,KAAQ,iBAAiB;AAE1E,QAAO;EACN,aAFmB,YAAY,QAAQ,eAAe,sBAAsB,oBAAoB;EAGhG,QAAQ,YACP,KAAK,IAAI,UAAU,eAAe,KAAK,KAAK,IAAI,UAAU,GAAG,GAAG,CAAC,IAAI,IAAI,KAAK,QAAQ,GAAG;EAC1F;;AAGF,SAAS,YAAY,OAAe,MAAsB;AACzD,KAAI,EAAE,SAAS,GAAI,OAAM,IAAI,eAAe,kBAAkB,GAAG,KAAK,uBAAuB;AAC7F,QAAO"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { StreamDisconnected } from "./errors.mjs";
|
|
2
|
+
import { sleep } from "../../utils/abort.mjs";
|
|
3
|
+
import { comparePositions, nextUncoveredAt } from "./position.mjs";
|
|
4
|
+
import { encodeToken, filterHash, sameBytes } from "./token.mjs";
|
|
5
|
+
import { readPages } from "./pager.mjs";
|
|
6
|
+
|
|
7
|
+
//#region src/client/stream/session.ts
|
|
8
|
+
/**
|
|
9
|
+
* The committed state of one stream, shared by every attempt. Tokens and reconnects resume from
|
|
10
|
+
* the committed position. Every event goes through `#isStale` then `#commit`, which keeps delivery
|
|
11
|
+
* exactly-once across reconnects and overlapping gap replays.
|
|
12
|
+
*/
|
|
13
|
+
var StreamSession = class {
|
|
14
|
+
#plan;
|
|
15
|
+
#adapter;
|
|
16
|
+
#frames;
|
|
17
|
+
#status;
|
|
18
|
+
#identity;
|
|
19
|
+
#ascending;
|
|
20
|
+
#committed;
|
|
21
|
+
#nextUncovered = null;
|
|
22
|
+
#attemptStart;
|
|
23
|
+
#retentionLimit = false;
|
|
24
|
+
constructor(options) {
|
|
25
|
+
const { plan, adapter } = options;
|
|
26
|
+
this.signal = options.signal;
|
|
27
|
+
this.#plan = plan;
|
|
28
|
+
this.#adapter = adapter;
|
|
29
|
+
this.#frames = options.frames ?? {};
|
|
30
|
+
this.#status = options.status;
|
|
31
|
+
this.#ascending = plan.order === "ascending";
|
|
32
|
+
this.#identity = {
|
|
33
|
+
transport: adapter.transport,
|
|
34
|
+
family: adapter.family,
|
|
35
|
+
chain: options.identity.chain,
|
|
36
|
+
filter: filterHash(options.identity.filter),
|
|
37
|
+
order: plan.order
|
|
38
|
+
};
|
|
39
|
+
const start = plan.start;
|
|
40
|
+
if (start && "position" in start) this.#committed = start.position;
|
|
41
|
+
if (start && this.#ascending) this.#nextUncovered = "checkpoint" in start ? start.checkpoint : nextUncoveredAt(start.position, adapter.family);
|
|
42
|
+
}
|
|
43
|
+
/** Resume point: the last committed position, else the planned start. */
|
|
44
|
+
get start() {
|
|
45
|
+
return this.#committed ? { position: this.#committed } : this.#plan.start;
|
|
46
|
+
}
|
|
47
|
+
get committed() {
|
|
48
|
+
return this.#committed;
|
|
49
|
+
}
|
|
50
|
+
/** Ascending: the first checkpoint not covered by what was delivered so far, if known. */
|
|
51
|
+
get nextUncovered() {
|
|
52
|
+
return this.#nextUncovered;
|
|
53
|
+
}
|
|
54
|
+
beginAttempt() {
|
|
55
|
+
this.#attemptStart = this.#committed;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Whether this attempt committed a new position; if so, the retry budget resets. `subscribing()`
|
|
59
|
+
* and `deliver()` rebaseline the attempt, so a live feed failure counts only what the feed
|
|
60
|
+
* delivered, not the gap replay that repeats on every attempt.
|
|
61
|
+
*/
|
|
62
|
+
advancedThisAttempt() {
|
|
63
|
+
return this.#committed !== this.#attemptStart;
|
|
64
|
+
}
|
|
65
|
+
/** The live subscription is about to open. */
|
|
66
|
+
subscribing() {
|
|
67
|
+
this.#attemptStart = this.#committed;
|
|
68
|
+
}
|
|
69
|
+
/** Reads passes until one is complete, waiting `pollInterval` while the index lags. */
|
|
70
|
+
async *replay(end) {
|
|
71
|
+
while (!(yield* this.#pass(end))) await sleep(this.#plan.pollInterval, this.signal);
|
|
72
|
+
}
|
|
73
|
+
/** Commits a position no event reported, so a token exists before the first match. */
|
|
74
|
+
async *baseline(position) {
|
|
75
|
+
yield* this.#commit({
|
|
76
|
+
$kind: "Progress",
|
|
77
|
+
position
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/** This attempt received a response. Reports `Connected`; progress is counted separately. */
|
|
81
|
+
connected() {
|
|
82
|
+
this.#status.connected();
|
|
83
|
+
}
|
|
84
|
+
openFeed(events) {
|
|
85
|
+
return new FeedReader(events, () => this.connected());
|
|
86
|
+
}
|
|
87
|
+
/** Commits each new event of the feed. A feed's end is a disconnect. */
|
|
88
|
+
async *deliver(feed) {
|
|
89
|
+
this.#attemptStart = this.#committed;
|
|
90
|
+
for await (const event of feed) if (!this.#isStale(event)) yield* this.#commit(event);
|
|
91
|
+
throw new StreamDisconnected();
|
|
92
|
+
}
|
|
93
|
+
async *complete() {
|
|
94
|
+
this.signal.throwIfAborted();
|
|
95
|
+
if (!this.#frames.completion) return;
|
|
96
|
+
yield {
|
|
97
|
+
$kind: "Complete",
|
|
98
|
+
reason: this.#retentionLimit ? "retentionLimit" : this.#plan.completion,
|
|
99
|
+
...this.#committed && { resumeToken: this.#token(this.#committed) }
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
async *#pass(end) {
|
|
103
|
+
const pass = readPages(this.#adapter, {
|
|
104
|
+
start: this.start,
|
|
105
|
+
end,
|
|
106
|
+
order: this.#plan.order,
|
|
107
|
+
signal: this.signal
|
|
108
|
+
});
|
|
109
|
+
try {
|
|
110
|
+
while (true) {
|
|
111
|
+
const next = await pass.next();
|
|
112
|
+
this.#status.connected();
|
|
113
|
+
if (next.done) {
|
|
114
|
+
this.#retentionLimit ||= next.value.retentionLimit;
|
|
115
|
+
return next.value.complete;
|
|
116
|
+
}
|
|
117
|
+
if (!this.#isStale(next.value)) yield* this.#commit(next.value);
|
|
118
|
+
}
|
|
119
|
+
} finally {
|
|
120
|
+
await pass.return({
|
|
121
|
+
complete: false,
|
|
122
|
+
retentionLimit: false
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/** Whether everything at `event`'s position was already delivered. */
|
|
127
|
+
#isStale({ $kind, position }) {
|
|
128
|
+
const committed = this.#committed;
|
|
129
|
+
if (position.checkpoint === null && committed?.checkpoint === null && sameBytes(position.cursor, committed.cursor)) return true;
|
|
130
|
+
const next = this.#ascending ? this.#nextUncovered : null;
|
|
131
|
+
if ($kind === "Progress") {
|
|
132
|
+
const at = this.#nextUncoveredAt(position);
|
|
133
|
+
return next !== null && at !== null && at < next || this.#isBehind(position, committed, true);
|
|
134
|
+
}
|
|
135
|
+
return next !== null && position.checkpoint !== null && position.checkpoint < next || this.#isBehind(position, committed, true);
|
|
136
|
+
}
|
|
137
|
+
async *#commit(event) {
|
|
138
|
+
this.signal.throwIfAborted();
|
|
139
|
+
const { position } = event;
|
|
140
|
+
const at = this.#ascending ? this.#nextUncoveredAt(position) : null;
|
|
141
|
+
if (at !== null && (this.#nextUncovered === null || at > this.#nextUncovered)) this.#nextUncovered = at;
|
|
142
|
+
this.#committed = position;
|
|
143
|
+
if (event.$kind === "Item") yield {
|
|
144
|
+
...this.#adapter.mapItem(event.item),
|
|
145
|
+
resumeToken: this.#token(position)
|
|
146
|
+
};
|
|
147
|
+
else if (this.#frames.progress) yield {
|
|
148
|
+
$kind: "Progress",
|
|
149
|
+
resumeToken: this.#token(position)
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Whether `a` comes before `b` (or equals it, when `inclusive`) in traversal order. An unknown
|
|
154
|
+
* order counts as not behind: the event is delivered at least once, never lost.
|
|
155
|
+
*/
|
|
156
|
+
#isBehind(a, b, inclusive) {
|
|
157
|
+
const comparison = b && comparePositions(a, b);
|
|
158
|
+
if (comparison === void 0) return false;
|
|
159
|
+
const traversal = this.#ascending ? comparison : -comparison;
|
|
160
|
+
return traversal < 0 || inclusive && traversal === 0;
|
|
161
|
+
}
|
|
162
|
+
#nextUncoveredAt(position) {
|
|
163
|
+
return nextUncoveredAt(position, this.#adapter.family);
|
|
164
|
+
}
|
|
165
|
+
#token(position) {
|
|
166
|
+
return encodeToken({
|
|
167
|
+
...this.#identity,
|
|
168
|
+
position
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
/** Reports `Connected` on the feed's first event, and buffers it so the handoff can inspect it. */
|
|
173
|
+
var FeedReader = class {
|
|
174
|
+
#iterator;
|
|
175
|
+
#onEvent;
|
|
176
|
+
#pending;
|
|
177
|
+
constructor(events, onEvent) {
|
|
178
|
+
this.#iterator = events[Symbol.asyncIterator]();
|
|
179
|
+
this.#onEvent = onEvent;
|
|
180
|
+
}
|
|
181
|
+
async first() {
|
|
182
|
+
this.#pending ??= await this.#read();
|
|
183
|
+
if (!this.#pending) throw new StreamDisconnected("Live feed ended before its first event");
|
|
184
|
+
return this.#pending;
|
|
185
|
+
}
|
|
186
|
+
async *[Symbol.asyncIterator]() {
|
|
187
|
+
const pending = this.#pending;
|
|
188
|
+
this.#pending = void 0;
|
|
189
|
+
if (pending) yield pending;
|
|
190
|
+
for (let event = await this.#read(); event; event = await this.#read()) yield event;
|
|
191
|
+
}
|
|
192
|
+
async close() {
|
|
193
|
+
await this.#iterator.return?.();
|
|
194
|
+
}
|
|
195
|
+
async #read() {
|
|
196
|
+
const result = await this.#iterator.next();
|
|
197
|
+
if (result.done) return void 0;
|
|
198
|
+
this.#onEvent();
|
|
199
|
+
return result.value;
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
//#endregion
|
|
204
|
+
export { StreamSession };
|
|
205
|
+
//# sourceMappingURL=session.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.mjs","names":["#plan","#adapter","#frames","#status","#identity","#ascending","#committed","#nextUncovered","#attemptStart","#pass","#commit","#isStale","#retentionLimit","#token","#nextUncoveredAt","#isBehind","#iterator","#onEvent","#pending","#read"],"sources":["../../../src/client/stream/session.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { sleep } from '../../utils/abort.js';\nimport type { SuiClientTypes } from '../types.js';\nimport type { Bound, CoreFrame, LedgerStreamAdapter, LiveEvent } from './adapter.js';\nimport { StreamDisconnected } from './errors.js';\nimport { readPages } from './pager.js';\nimport type { StreamPlan } from './plan.js';\nimport { comparePositions, nextUncoveredAt, type StreamPosition } from './position.js';\nimport type { StatusReporter } from './status.js';\nimport { encodeToken, filterHash, sameBytes, type StreamToken } from './token.js';\n\n/** A live feed opened through `StreamSession.openFeed`. */\nexport interface LiveFeed<Item> extends AsyncIterable<LiveEvent<Item>> {\n\t/** Reads the first Item or Progress. Iterating the feed yields it again first. */\n\tfirst(): Promise<LiveEvent<Item>>;\n\tclose(): Promise<void>;\n}\n\n/**\n * The committed state of one stream, shared by every attempt. Tokens and reconnects resume from\n * the committed position. Every event goes through `#isStale` then `#commit`, which keeps delivery\n * exactly-once across reconnects and overlapping gap replays.\n */\nexport class StreamSession<Item, Frame extends { resumeToken: string }> {\n\treadonly signal: AbortSignal;\n\treadonly #plan: StreamPlan;\n\treadonly #adapter: LedgerStreamAdapter<Item, Frame>;\n\treadonly #frames: SuiClientTypes.StreamFrameOptions;\n\treadonly #status: StatusReporter;\n\treadonly #identity: Omit<StreamToken, 'position'>;\n\treadonly #ascending: boolean;\n\n\t#committed: StreamPosition | undefined;\n\t#nextUncovered: bigint | null = null;\n\t#attemptStart: StreamPosition | undefined;\n\t#retentionLimit = false;\n\n\tconstructor(options: {\n\t\tplan: StreamPlan;\n\t\tidentity: { chain: string; filter: unknown };\n\t\tadapter: LedgerStreamAdapter<Item, Frame>;\n\t\tframes: SuiClientTypes.StreamFrameOptions | undefined;\n\t\tstatus: StatusReporter;\n\t\tsignal: AbortSignal;\n\t}) {\n\t\tconst { plan, adapter } = options;\n\t\tthis.signal = options.signal;\n\t\tthis.#plan = plan;\n\t\tthis.#adapter = adapter;\n\t\tthis.#frames = options.frames ?? {};\n\t\tthis.#status = options.status;\n\t\tthis.#ascending = plan.order === 'ascending';\n\t\tthis.#identity = {\n\t\t\ttransport: adapter.transport,\n\t\t\tfamily: adapter.family,\n\t\t\tchain: options.identity.chain,\n\t\t\tfilter: filterHash(options.identity.filter),\n\t\t\torder: plan.order,\n\t\t};\n\t\tconst start = plan.start;\n\t\tif (start && 'position' in start) this.#committed = start.position;\n\t\tif (start && this.#ascending) {\n\t\t\tthis.#nextUncovered =\n\t\t\t\t'checkpoint' in start ? start.checkpoint : nextUncoveredAt(start.position, adapter.family);\n\t\t}\n\t}\n\n\t/** Resume point: the last committed position, else the planned start. */\n\tget start(): Bound | undefined {\n\t\treturn this.#committed ? { position: this.#committed } : this.#plan.start;\n\t}\n\n\tget committed(): StreamPosition | undefined {\n\t\treturn this.#committed;\n\t}\n\n\t/** Ascending: the first checkpoint not covered by what was delivered so far, if known. */\n\tget nextUncovered(): bigint | null {\n\t\treturn this.#nextUncovered;\n\t}\n\n\tbeginAttempt() {\n\t\tthis.#attemptStart = this.#committed;\n\t}\n\n\t/**\n\t * Whether this attempt committed a new position; if so, the retry budget resets. `subscribing()`\n\t * and `deliver()` rebaseline the attempt, so a live feed failure counts only what the feed\n\t * delivered, not the gap replay that repeats on every attempt.\n\t */\n\tadvancedThisAttempt(): boolean {\n\t\treturn this.#committed !== this.#attemptStart;\n\t}\n\n\t/** The live subscription is about to open. */\n\tsubscribing() {\n\t\tthis.#attemptStart = this.#committed;\n\t}\n\n\t/** Reads passes until one is complete, waiting `pollInterval` while the index lags. */\n\tasync *replay(end: bigint | undefined): AsyncGenerator<CoreFrame<Frame>, void> {\n\t\twhile (!(yield* this.#pass(end))) await sleep(this.#plan.pollInterval, this.signal);\n\t}\n\n\t/** Commits a position no event reported, so a token exists before the first match. */\n\tasync *baseline(position: StreamPosition): AsyncGenerator<CoreFrame<Frame>, void> {\n\t\tyield* this.#commit({ $kind: 'Progress', position });\n\t}\n\n\t/** This attempt received a response. Reports `Connected`; progress is counted separately. */\n\tconnected() {\n\t\tthis.#status.connected();\n\t}\n\n\topenFeed(events: AsyncIterable<LiveEvent<Item>>): LiveFeed<Item> {\n\t\treturn new FeedReader(events, () => this.connected());\n\t}\n\n\t/** Commits each new event of the feed. A feed's end is a disconnect. */\n\tasync *deliver(feed: LiveFeed<Item>): AsyncGenerator<CoreFrame<Frame>, never> {\n\t\tthis.#attemptStart = this.#committed;\n\t\tfor await (const event of feed) {\n\t\t\tif (!this.#isStale(event)) yield* this.#commit(event);\n\t\t}\n\t\tthrow new StreamDisconnected();\n\t}\n\n\tasync *complete(): AsyncGenerator<SuiClientTypes.StreamCompletionFrame, void> {\n\t\tthis.signal.throwIfAborted();\n\t\tif (!this.#frames.completion) return;\n\t\tyield {\n\t\t\t$kind: 'Complete',\n\t\t\treason: this.#retentionLimit ? 'retentionLimit' : this.#plan.completion,\n\t\t\t...(this.#committed && { resumeToken: this.#token(this.#committed) }),\n\t\t};\n\t}\n\n\t// One pager pass. Returns whether it is complete.\n\tasync *#pass(end: bigint | undefined): AsyncGenerator<CoreFrame<Frame>, boolean> {\n\t\tconst pass = readPages(this.#adapter, {\n\t\t\tstart: this.start,\n\t\t\tend,\n\t\t\torder: this.#plan.order,\n\t\t\tsignal: this.signal,\n\t\t});\n\t\ttry {\n\t\t\twhile (true) {\n\t\t\t\tconst next = await pass.next();\n\t\t\t\tthis.#status.connected();\n\t\t\t\tif (next.done) {\n\t\t\t\t\tthis.#retentionLimit ||= next.value.retentionLimit;\n\t\t\t\t\treturn next.value.complete;\n\t\t\t\t}\n\t\t\t\tif (!this.#isStale(next.value)) yield* this.#commit(next.value);\n\t\t\t}\n\t\t} finally {\n\t\t\tawait pass.return({ complete: false, retentionLimit: false });\n\t\t}\n\t}\n\n\t/** Whether everything at `event`'s position was already delivered. */\n\t#isStale({ $kind, position }: LiveEvent<Item>): boolean {\n\t\tconst committed = this.#committed;\n\t\t// A reconnect that repeats a resume cursor without a checkpoint; positions with one are\n\t\t// compared by `#isBehind`.\n\t\tif (\n\t\t\tposition.checkpoint === null &&\n\t\t\tcommitted?.checkpoint === null &&\n\t\t\tsameBytes(position.cursor!, committed.cursor!)\n\t\t) {\n\t\t\treturn true;\n\t\t}\n\t\tconst next = this.#ascending ? this.#nextUncovered : null;\n\t\tif ($kind === 'Progress') {\n\t\t\tconst at = this.#nextUncoveredAt(position);\n\t\t\treturn (\n\t\t\t\t(next !== null && at !== null && at < next) || this.#isBehind(position, committed, true)\n\t\t\t);\n\t\t}\n\t\treturn (\n\t\t\t(next !== null && position.checkpoint !== null && position.checkpoint < next) ||\n\t\t\tthis.#isBehind(position, committed, true)\n\t\t);\n\t}\n\n\tasync *#commit(event: LiveEvent<Item>): AsyncGenerator<CoreFrame<Frame>, void> {\n\t\t// An event the adapter already buffered is neither committed nor delivered after an abort.\n\t\tthis.signal.throwIfAborted();\n\t\tconst { position } = event;\n\t\tconst at = this.#ascending ? this.#nextUncoveredAt(position) : null;\n\t\tif (at !== null && (this.#nextUncovered === null || at > this.#nextUncovered)) {\n\t\t\tthis.#nextUncovered = at;\n\t\t}\n\t\tthis.#committed = position;\n\t\tif (event.$kind === 'Item') {\n\t\t\tconst frame = this.#adapter.mapItem(event.item);\n\t\t\tyield { ...frame, resumeToken: this.#token(position) } as Frame;\n\t\t} else if (this.#frames.progress) {\n\t\t\tyield { $kind: 'Progress', resumeToken: this.#token(position) };\n\t\t}\n\t}\n\n\t/**\n\t * Whether `a` comes before `b` (or equals it, when `inclusive`) in traversal order. An unknown\n\t * order counts as not behind: the event is delivered at least once, never lost.\n\t */\n\t#isBehind(a: StreamPosition, b: StreamPosition | undefined, inclusive: boolean): boolean {\n\t\tconst comparison = b && comparePositions(a, b);\n\t\tif (comparison === undefined) return false;\n\t\tconst traversal = this.#ascending ? comparison : -comparison;\n\t\treturn traversal < 0 || (inclusive && traversal === 0);\n\t}\n\n\t#nextUncoveredAt(position: StreamPosition): bigint | null {\n\t\treturn nextUncoveredAt(position, this.#adapter.family);\n\t}\n\n\t#token(position: StreamPosition): string {\n\t\treturn encodeToken({ ...this.#identity, position });\n\t}\n}\n\n/** Reports `Connected` on the feed's first event, and buffers it so the handoff can inspect it. */\nclass FeedReader<Item> implements LiveFeed<Item> {\n\treadonly #iterator: AsyncIterator<LiveEvent<Item>>;\n\treadonly #onEvent: () => void;\n\t#pending: LiveEvent<Item> | undefined;\n\n\tconstructor(events: AsyncIterable<LiveEvent<Item>>, onEvent: () => void) {\n\t\tthis.#iterator = events[Symbol.asyncIterator]();\n\t\tthis.#onEvent = onEvent;\n\t}\n\n\tasync first(): Promise<LiveEvent<Item>> {\n\t\tthis.#pending ??= await this.#read();\n\t\tif (!this.#pending) throw new StreamDisconnected('Live feed ended before its first event');\n\t\treturn this.#pending;\n\t}\n\n\tasync *[Symbol.asyncIterator](): AsyncGenerator<LiveEvent<Item>> {\n\t\tconst pending = this.#pending;\n\t\tthis.#pending = undefined;\n\t\tif (pending) yield pending;\n\t\tfor (let event = await this.#read(); event; event = await this.#read()) yield event;\n\t}\n\n\tasync close() {\n\t\tawait this.#iterator.return?.();\n\t}\n\n\tasync #read(): Promise<LiveEvent<Item> | undefined> {\n\t\tconst result = await this.#iterator.next();\n\t\tif (result.done) return undefined;\n\t\tthis.#onEvent();\n\t\treturn result.value;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;AAyBA,IAAa,gBAAb,MAAwE;CAEvE,CAASA;CACT,CAASC;CACT,CAASC;CACT,CAASC;CACT,CAASC;CACT,CAASC;CAET;CACA,iBAAgC;CAChC;CACA,kBAAkB;CAElB,YAAY,SAOT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,MAAKC,MAAO,EAAE,OAAO,QAAQ,MAAM,MAAKA,MAAO,CAAE,OAAM;;CAG/E,MAAM,QAAQ;AACb,QAAM,MAAKH,SAAU,UAAU;;CAGhC,OAAMG,OAA8C;EACnD,MAAM,SAAS,MAAM,MAAKH,SAAU,MAAM;AAC1C,MAAI,OAAO,KAAM,QAAO;AACxB,QAAKC,SAAU;AACf,SAAO,OAAO"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//#region src/client/stream/status.ts
|
|
2
|
+
/** The only caller of `onStatus`. `Connected` is reported once per attempt. */
|
|
3
|
+
var StatusReporter = class {
|
|
4
|
+
#onStatus;
|
|
5
|
+
#connected = false;
|
|
6
|
+
#callbackFailed = false;
|
|
7
|
+
constructor(onStatus) {
|
|
8
|
+
this.#onStatus = onStatus;
|
|
9
|
+
}
|
|
10
|
+
/** Whether `onStatus` threw. Its error then ends the stream, never retried. */
|
|
11
|
+
get callbackFailed() {
|
|
12
|
+
return this.#callbackFailed;
|
|
13
|
+
}
|
|
14
|
+
connecting(attempt) {
|
|
15
|
+
this.#connected = false;
|
|
16
|
+
this.#emit({
|
|
17
|
+
$kind: "Connecting",
|
|
18
|
+
attempt
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
connected() {
|
|
22
|
+
if (this.#connected) return;
|
|
23
|
+
this.#connected = true;
|
|
24
|
+
this.#emit({ $kind: "Connected" });
|
|
25
|
+
}
|
|
26
|
+
retrying(attempt, delay, reason, error) {
|
|
27
|
+
this.#emit({
|
|
28
|
+
$kind: "Retrying",
|
|
29
|
+
attempt,
|
|
30
|
+
delay,
|
|
31
|
+
reason,
|
|
32
|
+
error
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
#emit(status) {
|
|
36
|
+
try {
|
|
37
|
+
this.#onStatus?.(status);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
this.#callbackFailed = true;
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
//#endregion
|
|
46
|
+
export { StatusReporter };
|
|
47
|
+
//# sourceMappingURL=status.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"status.mjs","names":["#onStatus","#callbackFailed","#connected","#emit"],"sources":["../../../src/client/stream/status.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SuiClientTypes } from '../types.js';\n\n/** The only caller of `onStatus`. `Connected` is reported once per attempt. */\nexport class StatusReporter {\n\treadonly #onStatus: ((status: SuiClientTypes.StreamStatus) => void) | undefined;\n\t#connected = false;\n\t#callbackFailed = false;\n\n\tconstructor(onStatus: ((status: SuiClientTypes.StreamStatus) => void) | undefined) {\n\t\tthis.#onStatus = onStatus;\n\t}\n\n\t/** Whether `onStatus` threw. Its error then ends the stream, never retried. */\n\tget callbackFailed() {\n\t\treturn this.#callbackFailed;\n\t}\n\n\tconnecting(attempt: number) {\n\t\tthis.#connected = false;\n\t\tthis.#emit({ $kind: 'Connecting', attempt });\n\t}\n\n\tconnected() {\n\t\tif (this.#connected) return;\n\t\tthis.#connected = true;\n\t\tthis.#emit({ $kind: 'Connected' });\n\t}\n\n\tretrying(attempt: number, delay: number, reason: SuiClientTypes.RetryReason, error: unknown) {\n\t\tthis.#emit({ $kind: 'Retrying', attempt, delay, reason, error });\n\t}\n\n\t#emit(status: SuiClientTypes.StreamStatus) {\n\t\ttry {\n\t\t\tthis.#onStatus?.(status);\n\t\t} catch (error) {\n\t\t\tthis.#callbackFailed = true;\n\t\t\tthrow error;\n\t\t}\n\t}\n}\n"],"mappings":";;AAMA,IAAa,iBAAb,MAA4B;CAC3B,CAASA;CACT,aAAa;CACb,kBAAkB;CAElB,YAAY,UAAuE;AAClF,QAAKA,WAAY;;;CAIlB,IAAI,iBAAiB;AACpB,SAAO,MAAKC;;CAGb,WAAW,SAAiB;AAC3B,QAAKC,YAAa;AAClB,QAAKC,KAAM;GAAE,OAAO;GAAc;GAAS,CAAC;;CAG7C,YAAY;AACX,MAAI,MAAKD,UAAY;AACrB,QAAKA,YAAa;AAClB,QAAKC,KAAM,EAAE,OAAO,aAAa,CAAC;;CAGnC,SAAS,SAAiB,OAAe,QAAoC,OAAgB;AAC5F,QAAKA,KAAM;GAAE,OAAO;GAAY;GAAS;GAAO;GAAQ;GAAO,CAAC;;CAGjE,MAAM,QAAqC;AAC1C,MAAI;AACH,SAAKH,WAAY,OAAO;WAChB,OAAO;AACf,SAAKC,iBAAkB;AACvB,SAAM"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { SuiStreamError } from "./errors.mjs";
|
|
2
|
+
import { positionSchema } from "./position.mjs";
|
|
3
|
+
import { bcs } from "@mysten/bcs";
|
|
4
|
+
import { fromBase64 as fromBase64$1 } from "@mysten/utils";
|
|
5
|
+
import { blake2b } from "@noble/hashes/blake2.js";
|
|
6
|
+
|
|
7
|
+
//#region src/client/stream/token.ts
|
|
8
|
+
/** Resume token format: `sui-stream:` + base64(BCS envelope). Every enum here is append-only. */
|
|
9
|
+
const TOKEN_PREFIX = "sui-stream:";
|
|
10
|
+
const Envelope = bcs.enum("StreamToken", { V1: bcs.struct("StreamTokenV1", {
|
|
11
|
+
transport: bcs.enum("StreamTransport", {
|
|
12
|
+
grpc: null,
|
|
13
|
+
graphql: null
|
|
14
|
+
}),
|
|
15
|
+
family: bcs.enum("StreamFamily", {
|
|
16
|
+
checkpoints: null,
|
|
17
|
+
transactions: null,
|
|
18
|
+
events: null
|
|
19
|
+
}),
|
|
20
|
+
chain: bcs.string(),
|
|
21
|
+
filter: bcs.bytes(32),
|
|
22
|
+
order: bcs.enum("StreamOrder", {
|
|
23
|
+
ascending: null,
|
|
24
|
+
descending: null
|
|
25
|
+
}),
|
|
26
|
+
position: positionSchema
|
|
27
|
+
}) });
|
|
28
|
+
function encodeToken(token) {
|
|
29
|
+
const envelope = {
|
|
30
|
+
...token,
|
|
31
|
+
transport: { [token.transport]: true },
|
|
32
|
+
family: { [token.family]: true },
|
|
33
|
+
order: { [token.order]: true }
|
|
34
|
+
};
|
|
35
|
+
return TOKEN_PREFIX + Envelope.serialize({ V1: envelope }).toBase64();
|
|
36
|
+
}
|
|
37
|
+
/** Rejects anything `encodeToken` would not produce byte for byte. */
|
|
38
|
+
function decodeToken(value) {
|
|
39
|
+
let token;
|
|
40
|
+
try {
|
|
41
|
+
if (!value.startsWith(TOKEN_PREFIX)) throw new Error("missing prefix");
|
|
42
|
+
const { V1 } = Envelope.parse(fromBase64$1(value.slice(11)));
|
|
43
|
+
token = {
|
|
44
|
+
transport: V1.transport.$kind,
|
|
45
|
+
family: V1.family.$kind,
|
|
46
|
+
chain: V1.chain,
|
|
47
|
+
filter: Uint8Array.from(V1.filter),
|
|
48
|
+
order: V1.order.$kind,
|
|
49
|
+
position: V1.position
|
|
50
|
+
};
|
|
51
|
+
} catch (cause) {
|
|
52
|
+
throw invalidToken(cause);
|
|
53
|
+
}
|
|
54
|
+
if (encodeToken(token) !== value) throw invalidToken();
|
|
55
|
+
const { kind, checkpoint, cursor } = token.position;
|
|
56
|
+
if (kind === "Progress" ? checkpoint !== null || !cursor : checkpoint === null) throw invalidToken();
|
|
57
|
+
return token;
|
|
58
|
+
}
|
|
59
|
+
function sameBytes(a, b) {
|
|
60
|
+
return a.length === b.length && a.every((byte, index) => byte === b[index]);
|
|
61
|
+
}
|
|
62
|
+
/** Binds tokens to the resolved filter without growing with it. */
|
|
63
|
+
function filterHash(filter) {
|
|
64
|
+
return blake2b(new TextEncoder().encode(canonical(filter)), { dkLen: 32 });
|
|
65
|
+
}
|
|
66
|
+
/** JSON with sorted keys, so equal filters hash equally regardless of property order. */
|
|
67
|
+
function canonical(value) {
|
|
68
|
+
if (value === void 0) return "null";
|
|
69
|
+
if (typeof value === "bigint") return JSON.stringify(value.toString());
|
|
70
|
+
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
71
|
+
if (value && typeof value === "object") return `{${Object.entries(value).filter(([, item]) => item !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`).join(",")}}`;
|
|
72
|
+
return JSON.stringify(value);
|
|
73
|
+
}
|
|
74
|
+
function invalidToken(cause) {
|
|
75
|
+
return new SuiStreamError("InvalidToken", "Invalid or unsupported stream resume token", { cause });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
export { decodeToken, encodeToken, filterHash, sameBytes };
|
|
80
|
+
//# sourceMappingURL=token.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token.mjs","names":["fromBase64"],"sources":["../../../src/client/stream/token.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { bcs } from '@mysten/bcs';\nimport { fromBase64 } from '@mysten/utils';\nimport { blake2b } from '@noble/hashes/blake2.js';\n\nimport type { SuiClientTypes } from '../types.js';\nimport type { StreamFamily, StreamTransport } from './adapter.js';\nimport { SuiStreamError } from './errors.js';\nimport { positionSchema, type StreamPosition } from './position.js';\n\n/** Resume token format: `sui-stream:` + base64(BCS envelope). Every enum here is append-only. */\nconst TOKEN_PREFIX = 'sui-stream:';\n\nconst Envelope = bcs.enum('StreamToken', {\n\tV1: bcs.struct('StreamTokenV1', {\n\t\ttransport: bcs.enum('StreamTransport', { grpc: null, graphql: null }),\n\t\tfamily: bcs.enum('StreamFamily', { checkpoints: null, transactions: null, events: null }),\n\t\tchain: bcs.string(),\n\t\t/** blake2b-256 of the canonical resolved filter. */\n\t\tfilter: bcs.bytes(32),\n\t\torder: bcs.enum('StreamOrder', { ascending: null, descending: null }),\n\t\tposition: positionSchema,\n\t}),\n});\n\n/** A decoded token: the stream it belongs to, and where it resumes. */\nexport interface StreamToken {\n\ttransport: StreamTransport;\n\tfamily: StreamFamily;\n\tchain: string;\n\tfilter: Uint8Array;\n\torder: SuiClientTypes.Order;\n\tposition: StreamPosition;\n}\n\nexport function encodeToken(token: StreamToken): string {\n\tconst envelope = {\n\t\t...token,\n\t\ttransport: { [token.transport]: true } as { grpc: true } | { graphql: true },\n\t\tfamily: { [token.family]: true } as\n\t\t\t{ checkpoints: true } | { transactions: true } | { events: true },\n\t\torder: { [token.order]: true } as { ascending: true } | { descending: true },\n\t};\n\treturn TOKEN_PREFIX + Envelope.serialize({ V1: envelope }).toBase64();\n}\n\n/** Rejects anything `encodeToken` would not produce byte for byte. */\nexport function decodeToken(value: string): StreamToken {\n\tlet token: StreamToken;\n\ttry {\n\t\tif (!value.startsWith(TOKEN_PREFIX)) throw new Error('missing prefix');\n\t\tconst { V1 } = Envelope.parse(fromBase64(value.slice(TOKEN_PREFIX.length)));\n\t\ttoken = {\n\t\t\ttransport: V1!.transport.$kind,\n\t\t\tfamily: V1!.family.$kind,\n\t\t\tchain: V1!.chain,\n\t\t\tfilter: Uint8Array.from(V1!.filter),\n\t\t\torder: V1!.order.$kind,\n\t\t\tposition: V1!.position,\n\t\t};\n\t} catch (cause) {\n\t\tthrow invalidToken(cause);\n\t}\n\t// Re-encoding rejects trailing bytes and non-canonical BCS or base64.\n\tif (encodeToken(token) !== value) throw invalidToken();\n\t// Progress has only a cursor; every other position has a checkpoint.\n\tconst { kind, checkpoint, cursor } = token.position;\n\tif (kind === 'Progress' ? checkpoint !== null || !cursor : checkpoint === null) {\n\t\tthrow invalidToken();\n\t}\n\treturn token;\n}\n\nexport function sameBytes(a: Uint8Array, b: Uint8Array): boolean {\n\treturn a.length === b.length && a.every((byte, index) => byte === b[index]);\n}\n\n/** Binds tokens to the resolved filter without growing with it. */\nexport function filterHash(filter: unknown): Uint8Array {\n\treturn blake2b(new TextEncoder().encode(canonical(filter)), { dkLen: 32 });\n}\n\n/** JSON with sorted keys, so equal filters hash equally regardless of property order. */\nfunction canonical(value: unknown): string {\n\tif (value === undefined) return 'null';\n\tif (typeof value === 'bigint') return JSON.stringify(value.toString());\n\tif (Array.isArray(value)) return `[${value.map(canonical).join(',')}]`;\n\tif (value && typeof value === 'object') {\n\t\treturn `{${Object.entries(value)\n\t\t\t.filter(([, item]) => item !== undefined)\n\t\t\t.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))\n\t\t\t.map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`)\n\t\t\t.join(',')}}`;\n\t}\n\treturn JSON.stringify(value);\n}\n\nfunction invalidToken(cause?: unknown) {\n\treturn new SuiStreamError('InvalidToken', 'Invalid or unsupported stream resume token', {\n\t\tcause,\n\t});\n}\n"],"mappings":";;;;;;;;AAaA,MAAM,eAAe;AAErB,MAAM,WAAW,IAAI,KAAK,eAAe,EACxC,IAAI,IAAI,OAAO,iBAAiB;CAC/B,WAAW,IAAI,KAAK,mBAAmB;EAAE,MAAM;EAAM,SAAS;EAAM,CAAC;CACrE,QAAQ,IAAI,KAAK,gBAAgB;EAAE,aAAa;EAAM,cAAc;EAAM,QAAQ;EAAM,CAAC;CACzF,OAAO,IAAI,QAAQ;CAEnB,QAAQ,IAAI,MAAM,GAAG;CACrB,OAAO,IAAI,KAAK,eAAe;EAAE,WAAW;EAAM,YAAY;EAAM,CAAC;CACrE,UAAU;CACV,CAAC,EACF,CAAC;AAYF,SAAgB,YAAY,OAA4B;CACvD,MAAM,WAAW;EAChB,GAAG;EACH,WAAW,GAAG,MAAM,YAAY,MAAM;EACtC,QAAQ,GAAG,MAAM,SAAS,MAAM;EAEhC,OAAO,GAAG,MAAM,QAAQ,MAAM;EAC9B;AACD,QAAO,eAAe,SAAS,UAAU,EAAE,IAAI,UAAU,CAAC,CAAC,UAAU;;;AAItE,SAAgB,YAAY,OAA4B;CACvD,IAAI;AACJ,KAAI;AACH,MAAI,CAAC,MAAM,WAAW,aAAa,CAAE,OAAM,IAAI,MAAM,iBAAiB;EACtE,MAAM,EAAE,OAAO,SAAS,MAAMA,aAAW,MAAM,MAAM,GAAoB,CAAC,CAAC;AAC3E,UAAQ;GACP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-0,0 +1,12 @@
|
|
|
1
|
+
import { SuiClientTypes } from "./types.mjs";
|
|
2
|
+
import "../grpc/proto/sui/rpc/v2/transaction.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/client/transaction-data.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Decode ledger transaction data from BCS. Unlike the transport mappers, which return `Unknown`,
|
|
7
|
+
* this throws on a transaction kind or variant this SDK version does not recognize.
|
|
8
|
+
*/
|
|
9
|
+
declare function parseTransactionDataBcs(bytes: Uint8Array): SuiClientTypes.TransactionData;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { parseTransactionDataBcs };
|
|
12
|
+
//# sourceMappingURL=transaction-data.d.mts.map
|