@misonetwork/sdk 0.1.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 (238) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +644 -0
  3. package/dist/client.d.ts +197 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +218 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/contracts/composition_credits/composition_credits.d.ts +181 -0
  8. package/dist/contracts/composition_credits/composition_credits.d.ts.map +1 -0
  9. package/dist/contracts/composition_credits/composition_credits.js +109 -0
  10. package/dist/contracts/composition_credits/composition_credits.js.map +1 -0
  11. package/dist/contracts/composition_credits/composition_party_role.d.ts +119 -0
  12. package/dist/contracts/composition_credits/composition_party_role.d.ts.map +1 -0
  13. package/dist/contracts/composition_credits/composition_party_role.js +139 -0
  14. package/dist/contracts/composition_credits/composition_party_role.js.map +1 -0
  15. package/dist/contracts/composition_credits/deps/miso_credit/credit.d.ts +24 -0
  16. package/dist/contracts/composition_credits/deps/miso_credit/credit.d.ts.map +1 -0
  17. package/dist/contracts/composition_credits/deps/miso_credit/credit.js +23 -0
  18. package/dist/contracts/composition_credits/deps/miso_credit/credit.js.map +1 -0
  19. package/dist/contracts/composition_credits/deps/sui/vec_map.d.ts +37 -0
  20. package/dist/contracts/composition_credits/deps/sui/vec_map.d.ts.map +1 -0
  21. package/dist/contracts/composition_credits/deps/sui/vec_map.js +28 -0
  22. package/dist/contracts/composition_credits/deps/sui/vec_map.js.map +1 -0
  23. package/dist/contracts/composition_royalty_pool/composition_royalty_pool.d.ts +87 -0
  24. package/dist/contracts/composition_royalty_pool/composition_royalty_pool.d.ts.map +1 -0
  25. package/dist/contracts/composition_royalty_pool/composition_royalty_pool.js +80 -0
  26. package/dist/contracts/composition_royalty_pool/composition_royalty_pool.js.map +1 -0
  27. package/dist/contracts/cover_art/cover_art.d.ts +85 -0
  28. package/dist/contracts/cover_art/cover_art.d.ts.map +1 -0
  29. package/dist/contracts/cover_art/cover_art.js +68 -0
  30. package/dist/contracts/cover_art/cover_art.js.map +1 -0
  31. package/dist/contracts/cover_art/deps/ori/walrus_data.d.ts +52 -0
  32. package/dist/contracts/cover_art/deps/ori/walrus_data.d.ts.map +1 -0
  33. package/dist/contracts/cover_art/deps/ori/walrus_data.js +37 -0
  34. package/dist/contracts/cover_art/deps/ori/walrus_data.js.map +1 -0
  35. package/dist/contracts/miso/composition.d.ts +231 -0
  36. package/dist/contracts/miso/composition.d.ts.map +1 -0
  37. package/dist/contracts/miso/composition.js +231 -0
  38. package/dist/contracts/miso/composition.js.map +1 -0
  39. package/dist/contracts/miso/deal.d.ts +184 -0
  40. package/dist/contracts/miso/deal.d.ts.map +1 -0
  41. package/dist/contracts/miso/deal.js +184 -0
  42. package/dist/contracts/miso/deal.js.map +1 -0
  43. package/dist/contracts/miso/deps/bps/bps.d.ts +19 -0
  44. package/dist/contracts/miso/deps/bps/bps.d.ts.map +1 -0
  45. package/dist/contracts/miso/deps/bps/bps.js +21 -0
  46. package/dist/contracts/miso/deps/bps/bps.js.map +1 -0
  47. package/dist/contracts/miso/recording.d.ts +228 -0
  48. package/dist/contracts/miso/recording.d.ts.map +1 -0
  49. package/dist/contracts/miso/recording.js +221 -0
  50. package/dist/contracts/miso/recording.js.map +1 -0
  51. package/dist/contracts/miso/release.d.ts +280 -0
  52. package/dist/contracts/miso/release.d.ts.map +1 -0
  53. package/dist/contracts/miso/release.js +282 -0
  54. package/dist/contracts/miso/release.js.map +1 -0
  55. package/dist/contracts/miso/track.d.ts +118 -0
  56. package/dist/contracts/miso/track.d.ts.map +1 -0
  57. package/dist/contracts/miso/track.js +118 -0
  58. package/dist/contracts/miso/track.js.map +1 -0
  59. package/dist/contracts/recording_credits/deps/miso_credit/credit.d.ts +24 -0
  60. package/dist/contracts/recording_credits/deps/miso_credit/credit.d.ts.map +1 -0
  61. package/dist/contracts/recording_credits/deps/miso_credit/credit.js +23 -0
  62. package/dist/contracts/recording_credits/deps/miso_credit/credit.js.map +1 -0
  63. package/dist/contracts/recording_credits/deps/sui/vec_map.d.ts +37 -0
  64. package/dist/contracts/recording_credits/deps/sui/vec_map.d.ts.map +1 -0
  65. package/dist/contracts/recording_credits/deps/sui/vec_map.js +28 -0
  66. package/dist/contracts/recording_credits/deps/sui/vec_map.js.map +1 -0
  67. package/dist/contracts/recording_credits/deps/sui/vec_set.d.ts +21 -0
  68. package/dist/contracts/recording_credits/deps/sui/vec_set.d.ts.map +1 -0
  69. package/dist/contracts/recording_credits/deps/sui/vec_set.js +20 -0
  70. package/dist/contracts/recording_credits/deps/sui/vec_set.js.map +1 -0
  71. package/dist/contracts/recording_credits/recording_credits.d.ts +2313 -0
  72. package/dist/contracts/recording_credits/recording_credits.d.ts.map +1 -0
  73. package/dist/contracts/recording_credits/recording_credits.js +270 -0
  74. package/dist/contracts/recording_credits/recording_credits.js.map +1 -0
  75. package/dist/contracts/recording_credits/recording_party_role.d.ts +1174 -0
  76. package/dist/contracts/recording_credits/recording_party_role.d.ts.map +1 -0
  77. package/dist/contracts/recording_credits/recording_party_role.js +692 -0
  78. package/dist/contracts/recording_credits/recording_party_role.js.map +1 -0
  79. package/dist/contracts/recording_royalty_pool/recording_royalty_pool.d.ts +90 -0
  80. package/dist/contracts/recording_royalty_pool/recording_royalty_pool.d.ts.map +1 -0
  81. package/dist/contracts/recording_royalty_pool/recording_royalty_pool.js +80 -0
  82. package/dist/contracts/recording_royalty_pool/recording_royalty_pool.js.map +1 -0
  83. package/dist/contracts/release_cover_art/deps/0x340057f2174fb59e4626742dd2b46c662237837b6187450cb59e4976ce7eac78/walrus_data.d.ts +52 -0
  84. package/dist/contracts/release_cover_art/deps/0x340057f2174fb59e4626742dd2b46c662237837b6187450cb59e4976ce7eac78/walrus_data.d.ts.map +1 -0
  85. package/dist/contracts/release_cover_art/deps/0x340057f2174fb59e4626742dd2b46c662237837b6187450cb59e4976ce7eac78/walrus_data.js +37 -0
  86. package/dist/contracts/release_cover_art/deps/0x340057f2174fb59e4626742dd2b46c662237837b6187450cb59e4976ce7eac78/walrus_data.js.map +1 -0
  87. package/dist/contracts/release_cover_art/deps/cover_art/cover_art.d.ts +46 -0
  88. package/dist/contracts/release_cover_art/deps/cover_art/cover_art.d.ts.map +1 -0
  89. package/dist/contracts/release_cover_art/deps/cover_art/cover_art.js +21 -0
  90. package/dist/contracts/release_cover_art/deps/cover_art/cover_art.js.map +1 -0
  91. package/dist/contracts/release_cover_art/deps/per_track/per_track.d.ts +33 -0
  92. package/dist/contracts/release_cover_art/deps/per_track/per_track.d.ts.map +1 -0
  93. package/dist/contracts/release_cover_art/deps/per_track/per_track.js +32 -0
  94. package/dist/contracts/release_cover_art/deps/per_track/per_track.js.map +1 -0
  95. package/dist/contracts/release_cover_art/release_cover_art.d.ts +300 -0
  96. package/dist/contracts/release_cover_art/release_cover_art.d.ts.map +1 -0
  97. package/dist/contracts/release_cover_art/release_cover_art.js +168 -0
  98. package/dist/contracts/release_cover_art/release_cover_art.js.map +1 -0
  99. package/dist/contracts/release_credits/deps/miso_credit/credit.d.ts +24 -0
  100. package/dist/contracts/release_credits/deps/miso_credit/credit.d.ts.map +1 -0
  101. package/dist/contracts/release_credits/deps/miso_credit/credit.js +23 -0
  102. package/dist/contracts/release_credits/deps/miso_credit/credit.js.map +1 -0
  103. package/dist/contracts/release_credits/deps/sui/vec_map.d.ts +37 -0
  104. package/dist/contracts/release_credits/deps/sui/vec_map.d.ts.map +1 -0
  105. package/dist/contracts/release_credits/deps/sui/vec_map.js +28 -0
  106. package/dist/contracts/release_credits/deps/sui/vec_map.js.map +1 -0
  107. package/dist/contracts/release_credits/release_credits.d.ts +142 -0
  108. package/dist/contracts/release_credits/release_credits.d.ts.map +1 -0
  109. package/dist/contracts/release_credits/release_credits.js +108 -0
  110. package/dist/contracts/release_credits/release_credits.js.map +1 -0
  111. package/dist/contracts/release_credits/release_party_role.d.ts +58 -0
  112. package/dist/contracts/release_credits/release_party_role.d.ts.map +1 -0
  113. package/dist/contracts/release_credits/release_party_role.js +66 -0
  114. package/dist/contracts/release_credits/release_party_role.js.map +1 -0
  115. package/dist/contracts/royalty_pool/deps/std/type_name.d.ts +18 -0
  116. package/dist/contracts/royalty_pool/deps/std/type_name.d.ts.map +1 -0
  117. package/dist/contracts/royalty_pool/deps/std/type_name.js +20 -0
  118. package/dist/contracts/royalty_pool/deps/std/type_name.js.map +1 -0
  119. package/dist/contracts/royalty_pool/deps/sui/balance.d.ts +13 -0
  120. package/dist/contracts/royalty_pool/deps/sui/balance.d.ts.map +1 -0
  121. package/dist/contracts/royalty_pool/deps/sui/balance.js +15 -0
  122. package/dist/contracts/royalty_pool/deps/sui/balance.js.map +1 -0
  123. package/dist/contracts/royalty_pool/deps/sui/vec_map.d.ts +37 -0
  124. package/dist/contracts/royalty_pool/deps/sui/vec_map.d.ts.map +1 -0
  125. package/dist/contracts/royalty_pool/deps/sui/vec_map.js +28 -0
  126. package/dist/contracts/royalty_pool/deps/sui/vec_map.js.map +1 -0
  127. package/dist/contracts/royalty_pool/pool.d.ts +358 -0
  128. package/dist/contracts/royalty_pool/pool.d.ts.map +1 -0
  129. package/dist/contracts/royalty_pool/pool.js +352 -0
  130. package/dist/contracts/royalty_pool/pool.js.map +1 -0
  131. package/dist/contracts/royalty_pool/stake.d.ts +202 -0
  132. package/dist/contracts/royalty_pool/stake.d.ts.map +1 -0
  133. package/dist/contracts/royalty_pool/stake.js +197 -0
  134. package/dist/contracts/royalty_pool/stake.js.map +1 -0
  135. package/dist/contracts/utils/index.d.ts +31 -0
  136. package/dist/contracts/utils/index.d.ts.map +1 -0
  137. package/dist/contracts/utils/index.js +162 -0
  138. package/dist/contracts/utils/index.js.map +1 -0
  139. package/dist/contracts.d.ts +18 -0
  140. package/dist/contracts.d.ts.map +1 -0
  141. package/dist/contracts.js +26 -0
  142. package/dist/contracts.js.map +1 -0
  143. package/dist/cover.d.ts +48 -0
  144. package/dist/cover.d.ts.map +1 -0
  145. package/dist/cover.js +68 -0
  146. package/dist/cover.js.map +1 -0
  147. package/dist/credits.d.ts +171 -0
  148. package/dist/credits.d.ts.map +1 -0
  149. package/dist/credits.js +360 -0
  150. package/dist/credits.js.map +1 -0
  151. package/dist/execute.d.ts +49 -0
  152. package/dist/execute.d.ts.map +1 -0
  153. package/dist/execute.js +114 -0
  154. package/dist/execute.js.map +1 -0
  155. package/dist/extensions/royalty-pool.d.ts +32 -0
  156. package/dist/extensions/royalty-pool.d.ts.map +1 -0
  157. package/dist/extensions/royalty-pool.js +33 -0
  158. package/dist/extensions/royalty-pool.js.map +1 -0
  159. package/dist/index.d.ts +13 -0
  160. package/dist/index.d.ts.map +1 -0
  161. package/dist/index.js +16 -0
  162. package/dist/index.js.map +1 -0
  163. package/dist/internal.d.ts +20 -0
  164. package/dist/internal.d.ts.map +1 -0
  165. package/dist/internal.js +48 -0
  166. package/dist/internal.js.map +1 -0
  167. package/dist/parsers.d.ts +9 -0
  168. package/dist/parsers.d.ts.map +1 -0
  169. package/dist/parsers.js +52 -0
  170. package/dist/parsers.js.map +1 -0
  171. package/dist/queries.d.ts +105 -0
  172. package/dist/queries.d.ts.map +1 -0
  173. package/dist/queries.js +380 -0
  174. package/dist/queries.js.map +1 -0
  175. package/dist/schemas.d.ts +99 -0
  176. package/dist/schemas.d.ts.map +1 -0
  177. package/dist/schemas.js +76 -0
  178. package/dist/schemas.js.map +1 -0
  179. package/dist/transactions.d.ts +158 -0
  180. package/dist/transactions.d.ts.map +1 -0
  181. package/dist/transactions.js +154 -0
  182. package/dist/transactions.js.map +1 -0
  183. package/dist/types.d.ts +204 -0
  184. package/dist/types.d.ts.map +1 -0
  185. package/dist/types.js +4 -0
  186. package/dist/types.js.map +1 -0
  187. package/dist/view.d.ts +21 -0
  188. package/dist/view.d.ts.map +1 -0
  189. package/dist/view.js +36 -0
  190. package/dist/view.js.map +1 -0
  191. package/package.json +111 -0
  192. package/src/client.ts +278 -0
  193. package/src/contracts/composition_credits/composition_credits.ts +169 -0
  194. package/src/contracts/composition_credits/composition_party_role.ts +189 -0
  195. package/src/contracts/composition_credits/deps/miso_credit/credit.ts +27 -0
  196. package/src/contracts/composition_credits/deps/sui/vec_map.ts +33 -0
  197. package/src/contracts/composition_royalty_pool/composition_royalty_pool.ts +135 -0
  198. package/src/contracts/cover_art/cover_art.ts +99 -0
  199. package/src/contracts/cover_art/deps/ori/walrus_data.ts +36 -0
  200. package/src/contracts/miso/composition.ts +343 -0
  201. package/src/contracts/miso/deal.ts +257 -0
  202. package/src/contracts/miso/deps/bps/bps.ts +23 -0
  203. package/src/contracts/miso/recording.ts +298 -0
  204. package/src/contracts/miso/release.ts +392 -0
  205. package/src/contracts/miso/track.ts +153 -0
  206. package/src/contracts/recording_credits/deps/miso_credit/credit.ts +27 -0
  207. package/src/contracts/recording_credits/deps/sui/vec_map.ts +33 -0
  208. package/src/contracts/recording_credits/deps/sui/vec_set.ts +21 -0
  209. package/src/contracts/recording_credits/recording_credits.ts +458 -0
  210. package/src/contracts/recording_credits/recording_party_role.ts +1041 -0
  211. package/src/contracts/recording_royalty_pool/recording_royalty_pool.ts +138 -0
  212. package/src/contracts/release_cover_art/deps/0x340057f2174fb59e4626742dd2b46c662237837b6187450cb59e4976ce7eac78/walrus_data.ts +36 -0
  213. package/src/contracts/release_cover_art/deps/cover_art/cover_art.ts +23 -0
  214. package/src/contracts/release_cover_art/deps/per_track/per_track.ts +36 -0
  215. package/src/contracts/release_cover_art/release_cover_art.ts +251 -0
  216. package/src/contracts/release_credits/deps/miso_credit/credit.ts +27 -0
  217. package/src/contracts/release_credits/deps/sui/vec_map.ts +33 -0
  218. package/src/contracts/release_credits/release_credits.ts +156 -0
  219. package/src/contracts/release_credits/release_party_role.ts +87 -0
  220. package/src/contracts/royalty_pool/deps/std/type_name.ts +22 -0
  221. package/src/contracts/royalty_pool/deps/sui/balance.ts +17 -0
  222. package/src/contracts/royalty_pool/deps/sui/vec_map.ts +33 -0
  223. package/src/contracts/royalty_pool/pool.ts +577 -0
  224. package/src/contracts/royalty_pool/stake.ts +317 -0
  225. package/src/contracts/utils/index.ts +229 -0
  226. package/src/contracts.ts +30 -0
  227. package/src/cover.ts +136 -0
  228. package/src/credits.ts +645 -0
  229. package/src/execute.ts +141 -0
  230. package/src/extensions/royalty-pool.ts +85 -0
  231. package/src/index.ts +17 -0
  232. package/src/internal.ts +85 -0
  233. package/src/parsers.ts +80 -0
  234. package/src/queries.ts +478 -0
  235. package/src/schemas.ts +96 -0
  236. package/src/transactions.ts +290 -0
  237. package/src/types.ts +244 -0
  238. package/src/view.ts +59 -0
package/src/queries.ts ADDED
@@ -0,0 +1,478 @@
1
+ // Copyright (c) Miso Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // Object reads. Single-object fetches use the Core API with `include: content`
5
+ // and parse the BCS contents through the codegen-generated structs (so parsing
6
+ // tracks the on-chain ABI). Generic-type discovery (by share type / by owner)
7
+ // uses GraphQL to find object addresses, then reads them through the Core path.
8
+ //
9
+ // Missing-object convention (null vs throw):
10
+ // - Core-object getters in this module (`getCompositionById`, `getDealById`,
11
+ // `get*AdminCapById`, …) THROW when the object is missing. Callers pass ids
12
+ // they obtained from the chain, so a miss means a broken reference — an
13
+ // exceptional state, not a normal one.
14
+ // - Extension dynamic-field readers (`getCompositionCredits`/`getRecordingCredits`/
15
+ // `getReleaseCredits` in ./credits.ts, `getReleaseCover` in ./cover.ts) return
16
+ // `null` — extension data is optional by design, and "not attached" is a
17
+ // normal, expected state.
18
+ // All null-returning readers use {@link isNotFound} to distinguish a missing
19
+ // object from a transport failure (which still throws).
20
+
21
+ import type { ClientWithCoreApi } from "@mysten/sui/client";
22
+ import type { SuiGraphQLClient } from "@mysten/sui/graphql";
23
+ import { graphql } from "@mysten/sui/graphql/schema";
24
+ import { deriveObjectID } from "@mysten/sui/utils";
25
+
26
+ import { Composition as CompositionBcs } from "./contracts/miso/composition.ts";
27
+ import { Deal as DealBcs } from "./contracts/miso/deal.ts";
28
+ import { Recording as RecordingBcs } from "./contracts/miso/recording.ts";
29
+ import { Release as ReleaseBcs } from "./contracts/miso/release.ts";
30
+ import { mapBps, mapComposition, mapRecording, mapRelease } from "./internal.ts";
31
+ import type {
32
+ Composition,
33
+ CompositionAdminCap,
34
+ Deal,
35
+ Recording,
36
+ RecordingAdminCap,
37
+ Release,
38
+ ReleaseAdminCap,
39
+ } from "./types.ts";
40
+
41
+ // ============================================================================
42
+ // Helpers
43
+ // ============================================================================
44
+
45
+ /**
46
+ * Extracts the type parameter `T` from `package::module::Type<T>`. For multi-
47
+ * parameter types this returns everything between the outer angle brackets —
48
+ * use {@link extractTypeParams2} to split two top-level parameters.
49
+ */
50
+ export function extractTypeParam(objectType: string): string {
51
+ const match = objectType.match(/<(.+)>$/);
52
+ if (!match?.[1]) throw new Error(`Could not extract type parameter from: ${objectType}`);
53
+ return match[1];
54
+ }
55
+
56
+ /** Splits the two top-level type parameters of `pkg::mod::Type<A, B>`. */
57
+ export function extractTypeParams2(objectType: string): [string, string] {
58
+ const inner = extractTypeParam(objectType);
59
+ let depth = 0;
60
+ for (let i = 0; i < inner.length; i++) {
61
+ const ch = inner[i];
62
+ if (ch === "<") depth++;
63
+ else if (ch === ">") depth--;
64
+ else if (ch === "," && depth === 0) return [inner.slice(0, i).trim(), inner.slice(i + 1).trim()];
65
+ }
66
+ throw new Error(`Expected two type parameters in: ${objectType}`);
67
+ }
68
+
69
+ /**
70
+ * True when `e` is a "this object does not exist" error from any of the Sui
71
+ * client transports, so null-returning readers can distinguish absence from
72
+ * transport failure. Matches, in order of preference:
73
+ *
74
+ * 1. Structured `ObjectError.code` values thrown by the JSON-RPC core client
75
+ * (`notExists`, `deleted`, `dynamicFieldNotFound`) and the GraphQL core
76
+ * client (`notFound`). The class itself is not exported by `@mysten/sui`,
77
+ * so we duck-type on `code`.
78
+ * 2. The message shapes those clients (and the gRPC core client, which wraps
79
+ * the server's per-object status message in a plain `Error`) produce:
80
+ * "Object 0x… does not exist" / "Object 0x… not found" / "Object 0x… has
81
+ * been deleted" / "Dynamic field not found for object 0x…" / "No object
82
+ * found for id 0x…".
83
+ *
84
+ * Transport/protocol errors must NOT match: every message pattern requires
85
+ * object-ish context ("object" / "dynamic field"), so e.g. a JSON-RPC
86
+ * "Method not found" or a gRPC "peer not found" is never treated as a missing
87
+ * object and propagates to the caller.
88
+ */
89
+ export function isNotFound(e: unknown): boolean {
90
+ if (typeof e === "object" && e !== null && "code" in e) {
91
+ const code = (e as { code: unknown }).code;
92
+ if (code === "notExists" || code === "deleted" || code === "dynamicFieldNotFound" || code === "notFound") {
93
+ return true;
94
+ }
95
+ }
96
+ const msg = e instanceof Error ? e.message : String(e);
97
+ return (
98
+ /\bobject\b[\s\S]*\b(?:not\s?found|does not exist|has been deleted)\b/i.test(msg) ||
99
+ /\bdynamic field\b[\s\S]*\bnot\s?found\b/i.test(msg) ||
100
+ /\bno object\b/i.test(msg)
101
+ );
102
+ }
103
+
104
+ /** Key bytes for Move unit structs (single `0x00` for `dummy_field: bool = false`). */
105
+ const UNIT_STRUCT_KEY_BYTES = new Uint8Array([0x00]);
106
+
107
+ /** Fetches one object's BCS content bytes (or null if absent). */
108
+ async function getContent(client: ClientWithCoreApi, objectId: string): Promise<Uint8Array | null> {
109
+ const { object } = await client.core.getObject({ objectId, include: { content: true } });
110
+ return object.content ?? null;
111
+ }
112
+
113
+ // ============================================================================
114
+ // GraphQL discovery queries
115
+ // ============================================================================
116
+
117
+ /** Object addresses matching a fully-qualified type. */
118
+ const AddressesByTypeQuery = graphql(`
119
+ query AddressesByType($type: String!) {
120
+ objects(filter: { type: $type }) {
121
+ nodes { address }
122
+ }
123
+ }
124
+ `);
125
+
126
+ /**
127
+ * Object addresses AND their instantiated types, for a type filter.
128
+ *
129
+ * Needed where the filter cannot be fully qualified: a type filter must supply
130
+ * either ALL of a type's parameters or none, so a two-parameter type that is
131
+ * only known by its first parameter (`Recording<RecordingShare, ?>`) has to be
132
+ * filtered by bare type name and disambiguated client-side on the returned
133
+ * `repr`.
134
+ */
135
+ const AddressesAndTypesByTypeQuery = graphql(`
136
+ query AddressesAndTypesByType($type: String!) {
137
+ objects(filter: { type: $type }) {
138
+ nodes { address asMoveObject { contents { type { repr } } } }
139
+ }
140
+ }
141
+ `);
142
+
143
+ // ============================================================================
144
+ // Composition
145
+ // ============================================================================
146
+
147
+ /** Fetches multiple compositions by ID in one Core request. */
148
+ export async function getCompositionsByIds(
149
+ client: ClientWithCoreApi,
150
+ compositionIds: string[],
151
+ ): Promise<Record<string, Composition>> {
152
+ if (compositionIds.length === 0) return {};
153
+ const { objects } = await client.core.getObjects({ objectIds: compositionIds, include: { content: true } });
154
+ const out: Record<string, Composition> = {};
155
+ for (const obj of objects) {
156
+ if (obj instanceof Error || !obj.content) continue;
157
+ out[obj.objectId] = mapComposition(obj.objectId, CompositionBcs.parse(obj.content));
158
+ }
159
+ return out;
160
+ }
161
+
162
+ /** Fetches a composition by its object ID. */
163
+ export async function getCompositionById(client: ClientWithCoreApi, compositionId: string): Promise<Composition> {
164
+ const content = await getContent(client, compositionId);
165
+ if (!content) throw new Error(`Composition not found: ${compositionId}`);
166
+ return mapComposition(compositionId, CompositionBcs.parse(content));
167
+ }
168
+
169
+ /** Extracts the share type `T` from a `Composition<T>` object. */
170
+ export async function getCompositionShareType(client: ClientWithCoreApi, compositionId: string): Promise<string> {
171
+ const { object } = await client.core.getObject({ objectId: compositionId });
172
+ return extractTypeParam(object.type);
173
+ }
174
+
175
+ /** Fetches a composition by its share type (GraphQL discovery + Core read). */
176
+ export async function getCompositionByShareType(
177
+ client: ClientWithCoreApi,
178
+ graphqlClient: SuiGraphQLClient,
179
+ shareType: string,
180
+ misoPackageId: string,
181
+ ): Promise<Composition> {
182
+ const type = `${misoPackageId}::composition::Composition<${shareType}>`;
183
+ const address = await firstAddressOfType(graphqlClient, type);
184
+ if (!address) throw new Error(`Composition not found for share type: ${shareType}`);
185
+ return getCompositionById(client, address);
186
+ }
187
+
188
+ export async function getCompositionAdminCapById(
189
+ client: ClientWithCoreApi,
190
+ adminCapId: string,
191
+ ): Promise<CompositionAdminCap> {
192
+ const { object } = await client.core.getObject({ objectId: adminCapId });
193
+ return { id: adminCapId, shareType: extractTypeParam(object.type) };
194
+ }
195
+
196
+ /**
197
+ * Composition admin caps owned by `owner`.
198
+ *
199
+ * Core API (no GraphQL): `listOwnedObjects` takes a type filter and returns each
200
+ * object's instantiated `type`, so the share type is read straight off
201
+ * `CompositionAdminCap<CompositionShare>` with no second round-trip.
202
+ */
203
+ export async function getOwnedCompositionAdminCaps(
204
+ client: ClientWithCoreApi,
205
+ owner: string,
206
+ misoPackageId: string,
207
+ ): Promise<CompositionAdminCap[]> {
208
+ const capType = `${misoPackageId}::composition::CompositionAdminCap`;
209
+ const { objects } = await client.core.listOwnedObjects({ owner, type: capType });
210
+ const caps: CompositionAdminCap[] = [];
211
+ for (const obj of objects) {
212
+ const match = obj.type?.match(/<(.+)>$/);
213
+ if (match?.[1]) caps.push({ id: obj.objectId, shareType: match[1] });
214
+ }
215
+ return caps;
216
+ }
217
+
218
+ export function deriveCompositionAdminCapId(compositionId: string, misoPackageId: string): string {
219
+ return deriveObjectID(compositionId, `${misoPackageId}::composition::CompositionAdminCapKey`, UNIT_STRUCT_KEY_BYTES);
220
+ }
221
+
222
+ // ============================================================================
223
+ // Recording
224
+ // ============================================================================
225
+
226
+ export async function getRecordingsByIds(
227
+ client: ClientWithCoreApi,
228
+ recordingIds: string[],
229
+ ): Promise<Record<string, Recording>> {
230
+ if (recordingIds.length === 0) return {};
231
+ const { objects } = await client.core.getObjects({ objectIds: recordingIds, include: { content: true } });
232
+ const out: Record<string, Recording> = {};
233
+ for (const obj of objects) {
234
+ if (obj instanceof Error || !obj.content) continue;
235
+ out[obj.objectId] = mapRecording(obj.objectId, RecordingBcs.parse(obj.content));
236
+ }
237
+ return out;
238
+ }
239
+
240
+ export async function getRecordingById(client: ClientWithCoreApi, recordingId: string): Promise<Recording> {
241
+ const content = await getContent(client, recordingId);
242
+ if (!content) throw new Error(`Recording not found: ${recordingId}`);
243
+ return mapRecording(recordingId, RecordingBcs.parse(content));
244
+ }
245
+
246
+ /**
247
+ * The recording's OWN share type (`RecordingShare`). `Recording` is generic over
248
+ * two phantoms — `Recording<RecordingShare, CompositionShare>` — so this splits
249
+ * them and returns the first; use {@link getRecordingShareTypes} when the
250
+ * parent composition's share type is needed too.
251
+ */
252
+ export async function getRecordingShareType(client: ClientWithCoreApi, recordingId: string): Promise<string> {
253
+ const [recordingShareType] = await getRecordingShareTypes(client, recordingId);
254
+ return recordingShareType;
255
+ }
256
+
257
+ /**
258
+ * Both of a recording's share types, as `[RecordingShare, CompositionShare]`.
259
+ * Most builders need the pair — `deal::new`, `track::new`, `recording::publish`
260
+ * and the recording credit/pool extensions are all generic over both, in this
261
+ * order.
262
+ */
263
+ export async function getRecordingShareTypes(
264
+ client: ClientWithCoreApi,
265
+ recordingId: string,
266
+ ): Promise<[string, string]> {
267
+ const { object } = await client.core.getObject({ objectId: recordingId });
268
+ return extractTypeParams2(object.type);
269
+ }
270
+
271
+ export async function getRecordingByShareType(
272
+ client: ClientWithCoreApi,
273
+ graphqlClient: SuiGraphQLClient,
274
+ shareType: string,
275
+ misoPackageId: string,
276
+ ): Promise<Recording> {
277
+ const address = await addressOfRecordingWithShareType(graphqlClient, misoPackageId, shareType);
278
+ if (!address) throw new Error(`Recording not found for share type: ${shareType}`);
279
+ return getRecordingById(client, address);
280
+ }
281
+
282
+ export async function getRecordingAdminCapById(
283
+ client: ClientWithCoreApi,
284
+ adminCapId: string,
285
+ ): Promise<RecordingAdminCap> {
286
+ const { object } = await client.core.getObject({ objectId: adminCapId });
287
+ return { id: adminCapId, shareType: extractTypeParam(object.type) };
288
+ }
289
+
290
+ /**
291
+ * Recording admin caps owned by `owner`.
292
+ *
293
+ * Core API (no GraphQL), same shape as {@link getOwnedCompositionAdminCaps}.
294
+ * Note `RecordingAdminCap<phantom RecordingShare>` is deliberately single-param,
295
+ * so this yields only the recording's own share type — its parent composition's
296
+ * share type is not recoverable from the cap alone.
297
+ */
298
+ export async function getOwnedRecordingAdminCaps(
299
+ client: ClientWithCoreApi,
300
+ owner: string,
301
+ misoPackageId: string,
302
+ ): Promise<RecordingAdminCap[]> {
303
+ const capType = `${misoPackageId}::recording::RecordingAdminCap`;
304
+ const { objects } = await client.core.listOwnedObjects({ owner, type: capType });
305
+ const caps: RecordingAdminCap[] = [];
306
+ for (const obj of objects) {
307
+ const match = obj.type?.match(/<(.+)>$/);
308
+ if (match?.[1]) caps.push({ id: obj.objectId, shareType: match[1] });
309
+ }
310
+ return caps;
311
+ }
312
+
313
+ export function deriveRecordingAdminCapId(recordingId: string, misoPackageId: string): string {
314
+ return deriveObjectID(recordingId, `${misoPackageId}::recording::RecordingAdminCapKey`, UNIT_STRUCT_KEY_BYTES);
315
+ }
316
+
317
+ // ============================================================================
318
+ // Deal
319
+ // ============================================================================
320
+
321
+ /** Fetches a deal by its object ID (share types read from the type parameters). */
322
+ export async function getDealById(client: ClientWithCoreApi, dealId: string): Promise<Deal> {
323
+ const { object } = await client.core.getObject({ objectId: dealId, include: { content: true } });
324
+ if (!object.content) throw new Error(`Deal not found: ${dealId}`);
325
+ const d = DealBcs.parse(object.content);
326
+ const [recordingShareType, compositionShareType] = extractTypeParams2(object.type);
327
+ return {
328
+ id: dealId,
329
+ releaseId: d.release_id,
330
+ trackSplitBps: mapBps(d.track_split_bps),
331
+ recordingShareType,
332
+ compositionShareType,
333
+ };
334
+ }
335
+
336
+ // ============================================================================
337
+ // Release
338
+ // ============================================================================
339
+
340
+ export async function getReleasesByIds(
341
+ client: ClientWithCoreApi,
342
+ releaseIds: string[],
343
+ ): Promise<Record<string, Release>> {
344
+ if (releaseIds.length === 0) return {};
345
+ const { objects } = await client.core.getObjects({ objectIds: releaseIds, include: { content: true } });
346
+ const out: Record<string, Release> = {};
347
+ for (const obj of objects) {
348
+ if (obj instanceof Error || !obj.content) continue;
349
+ out[obj.objectId] = mapRelease(obj.objectId, ReleaseBcs.parse(obj.content));
350
+ }
351
+ return out;
352
+ }
353
+
354
+ export async function getReleaseById(client: ClientWithCoreApi, releaseId: string): Promise<Release> {
355
+ const content = await getContent(client, releaseId);
356
+ if (!content) throw new Error(`Release not found: ${releaseId}`);
357
+ return mapRelease(releaseId, ReleaseBcs.parse(content));
358
+ }
359
+
360
+ export async function getReleaseRegistry(client: SuiGraphQLClient, misoPackageId: string): Promise<string> {
361
+ const address = await firstAddressOfType(client, `${misoPackageId}::release::ReleaseRegistry`);
362
+ if (!address) throw new Error("ReleaseRegistry not found");
363
+ return address;
364
+ }
365
+
366
+ export async function getReleaseAdminCapById(
367
+ client: ClientWithCoreApi,
368
+ adminCapId: string,
369
+ ): Promise<ReleaseAdminCap> {
370
+ const { object } = await client.core.getObject({ objectId: adminCapId, include: { json: true } });
371
+ const json = object.json as { release_id: string } | null;
372
+ if (!json?.release_id) throw new Error(`ReleaseAdminCap not found: ${adminCapId}`);
373
+ return { id: adminCapId, releaseId: json.release_id };
374
+ }
375
+
376
+ export async function getOwnedReleaseAdminCaps(
377
+ client: ClientWithCoreApi,
378
+ owner: string,
379
+ misoPackageId: string,
380
+ ): Promise<ReleaseAdminCap[]> {
381
+ const capType = `${misoPackageId}::release::ReleaseAdminCap`;
382
+ const { objects } = await client.core.listOwnedObjects({ owner, type: capType });
383
+ if (objects.length === 0) return [];
384
+ const { objects: details } = await client.core.getObjects({
385
+ objectIds: objects.map((o) => o.objectId),
386
+ include: { json: true },
387
+ });
388
+ const caps: ReleaseAdminCap[] = [];
389
+ for (const obj of details) {
390
+ if (obj instanceof Error) continue;
391
+ const json = obj.json as { release_id: string } | null;
392
+ if (json?.release_id) caps.push({ id: obj.objectId, releaseId: json.release_id });
393
+ }
394
+ return caps;
395
+ }
396
+
397
+ export function deriveReleaseAdminCapId(releaseId: string, misoPackageId: string): string {
398
+ return deriveObjectID(releaseId, `${misoPackageId}::release::ReleaseAdminCapKey`, UNIT_STRUCT_KEY_BYTES);
399
+ }
400
+
401
+ // ============================================================================
402
+ // Share Currency
403
+ // ============================================================================
404
+
405
+ /** Extracts the share type `T` from a `Currency<T>` object. */
406
+ export async function getShareCurrencyType(client: ClientWithCoreApi, shareCurrencyId: string): Promise<string> {
407
+ const { object } = await client.core.getObject({ objectId: shareCurrencyId });
408
+ return extractTypeParam(object.type);
409
+ }
410
+
411
+ /**
412
+ * Finds the `TreasuryCap<ShareType>` owned by `owner`. One Core API call.
413
+ *
414
+ * Takes the share TYPE, not the `Currency` object id, because callers almost
415
+ * always have it already — it is {@link ShareCurrencyBinding.shareType},
416
+ * threaded through every builder. Taking the object id instead would force a
417
+ * `getObject` purely to read the type parameter back off the tag, a round trip
418
+ * the caller already paid for. If you genuinely hold only the currency id,
419
+ * compose the two:
420
+ *
421
+ * ```ts
422
+ * const shareType = await getShareCurrencyType(client, shareCurrencyId);
423
+ * const capId = await getShareCurrencyTreasuryCap(client, shareType, owner);
424
+ * ```
425
+ */
426
+ export async function getShareCurrencyTreasuryCap(
427
+ client: ClientWithCoreApi,
428
+ shareType: string,
429
+ owner: string,
430
+ ): Promise<string> {
431
+ const { objects } = await client.core.listOwnedObjects({
432
+ owner,
433
+ type: `0x2::coin::TreasuryCap<${shareType}>`,
434
+ });
435
+ if (objects.length === 0) {
436
+ throw new Error(`No TreasuryCap found for ${shareType} owned by ${owner}`);
437
+ }
438
+ return objects[0]!.objectId;
439
+ }
440
+
441
+ // ============================================================================
442
+ // Private
443
+ // ============================================================================
444
+
445
+ /** Returns the first object address of a fully-qualified type, or null. */
446
+ async function firstAddressOfType(client: SuiGraphQLClient, type: string): Promise<string | null> {
447
+ const result = await client.query({ query: AddressesByTypeQuery, variables: { type } });
448
+ return result.data?.objects?.nodes?.[0]?.address ?? null;
449
+ }
450
+
451
+ /**
452
+ * Address of the `Recording` whose FIRST type parameter is `shareType`, or null.
453
+ *
454
+ * `Recording<RecordingShare, CompositionShare>` takes two parameters and a type
455
+ * filter must supply all of them or none, so filtering by
456
+ * `Recording<${shareType}>` matches nothing. Callers generally know only the
457
+ * recording's own share type — `RecordingAdminCap<phantom RecordingShare>` is
458
+ * deliberately single-param — so this filters by bare type name and matches the
459
+ * first parameter client-side. A recording's share currency is unique to it, so
460
+ * the match is unambiguous.
461
+ */
462
+ async function addressOfRecordingWithShareType(
463
+ client: SuiGraphQLClient,
464
+ misoPackageId: string,
465
+ shareType: string,
466
+ ): Promise<string | null> {
467
+ const result = await client.query({
468
+ query: AddressesAndTypesByTypeQuery,
469
+ variables: { type: `${misoPackageId}::recording::Recording` },
470
+ });
471
+ for (const node of result.data?.objects?.nodes ?? []) {
472
+ const repr = node?.asMoveObject?.contents?.type?.repr;
473
+ if (!repr || !node.address) continue;
474
+ const [recordingShareType] = extractTypeParams2(repr);
475
+ if (recordingShareType === shareType) return node.address;
476
+ }
477
+ return null;
478
+ }
package/src/schemas.ts ADDED
@@ -0,0 +1,96 @@
1
+ // Copyright (c) Miso Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { z } from "zod";
5
+ import { isValidSuiObjectId } from "@mysten/sui/utils";
6
+
7
+ // ============================================================================
8
+ // Composition
9
+ // ============================================================================
10
+
11
+ export const CompositionStateSchema = z.discriminatedUnion("type", [
12
+ z.object({ type: z.literal("Initialized") }),
13
+ z.object({ type: z.literal("Published"), timestampMs: z.number().int().min(0) }),
14
+ ]);
15
+
16
+ export const CompositionSchema = z.object({
17
+ id: z.string().refine(isValidSuiObjectId, "Invalid Sui object ID"),
18
+ state: CompositionStateSchema,
19
+ title: z.string().min(1, "Composition title cannot be empty"),
20
+ royaltyRate: z.object({ value: z.number().int().min(0).max(2000) }),
21
+ });
22
+
23
+ export const CompositionPublishedEventSchema = z.object({
24
+ compositionId: z.string().refine(isValidSuiObjectId, "Invalid Sui object ID"),
25
+ });
26
+
27
+ export const CompositionRoyaltySetEventSchema = z.object({
28
+ royaltyRateBps: z.number().int().min(0).max(2000),
29
+ });
30
+
31
+ // ============================================================================
32
+ // Recording
33
+ // ============================================================================
34
+
35
+ export const RecordingStateSchema = z.discriminatedUnion("type", [
36
+ z.object({ type: z.literal("Initialized") }),
37
+ z.object({ type: z.literal("Published"), timestampMs: z.number().int().min(0) }),
38
+ ]);
39
+
40
+ export const RecordingSchema = z.object({
41
+ id: z.string().refine(isValidSuiObjectId, "Invalid Sui object ID"),
42
+ state: RecordingStateSchema,
43
+ });
44
+
45
+ export const RecordingPublishedEventSchema = z.object({
46
+ recordingId: z.string().refine(isValidSuiObjectId, "Invalid Sui object ID"),
47
+ });
48
+
49
+ // ============================================================================
50
+ // Track
51
+ // ============================================================================
52
+
53
+ export const TrackSchema = z.object({
54
+ state: z.enum(["Unassigned", "Assigned"]),
55
+ recordingId: z.string().refine(isValidSuiObjectId, "Invalid Sui object ID"),
56
+ splitBps: z.object({ value: z.number().int().min(0).max(10000) }),
57
+ });
58
+
59
+ // ============================================================================
60
+ // Release
61
+ // ============================================================================
62
+
63
+ export const ReleaseStateSchema = z.discriminatedUnion("type", [
64
+ z.object({ type: z.literal("Initialized") }),
65
+ z.object({ type: z.literal("Published"), timestampMs: z.number().int().min(0) }),
66
+ ]);
67
+
68
+ export const ReleaseSchema = z.object({
69
+ id: z.string().refine(isValidSuiObjectId, "Invalid Sui object ID"),
70
+ state: ReleaseStateSchema,
71
+ title: z.string().min(1, "Release title cannot be empty"),
72
+ tracks: z.array(TrackSchema).max(255, "Release cannot have more than 255 tracks"),
73
+ });
74
+
75
+ export const ReleasePublishedEventSchema = z.object({
76
+ releaseId: z.string().refine(isValidSuiObjectId, "Invalid Sui object ID"),
77
+ });
78
+
79
+ // ============================================================================
80
+ // Deal Events
81
+ // ============================================================================
82
+
83
+ export const DealCreatedEventSchema = z.object({
84
+ dealId: z.string().refine(isValidSuiObjectId, "Invalid Sui object ID"),
85
+ releaseId: z.string().refine(isValidSuiObjectId, "Invalid Sui object ID"),
86
+ trackSplitBps: z.object({ value: z.number().int().min(0).max(10000) }),
87
+ });
88
+
89
+ const dealLifecycleEventShape = {
90
+ dealId: z.string().refine(isValidSuiObjectId, "Invalid Sui object ID"),
91
+ releaseId: z.string().refine(isValidSuiObjectId, "Invalid Sui object ID"),
92
+ };
93
+
94
+ export const DealAcceptedEventSchema = z.object(dealLifecycleEventShape);
95
+
96
+ export const DealRejectedEventSchema = z.object(dealLifecycleEventShape);