@6529-collections/stream-sdk 0.0.0-stage → 0.1.0-bootstrap.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 (148) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +4 -0
  4. package/README.md +413 -2
  5. package/dist/actions/accounts.d.ts +58 -0
  6. package/dist/actions/accounts.js +73 -0
  7. package/dist/actions/content.d.ts +22 -0
  8. package/dist/actions/content.js +45 -0
  9. package/dist/actions/curated.d.ts +177 -0
  10. package/dist/actions/curated.js +279 -0
  11. package/dist/actions/editions.d.ts +100 -0
  12. package/dist/actions/editions.js +161 -0
  13. package/dist/actions/independent.d.ts +149 -0
  14. package/dist/actions/independent.js +274 -0
  15. package/dist/actions/money.d.ts +117 -0
  16. package/dist/actions/money.js +148 -0
  17. package/dist/actions/record.d.ts +144 -0
  18. package/dist/actions/record.js +229 -0
  19. package/dist/actions/sale.d.ts +82 -0
  20. package/dist/actions/sale.js +113 -0
  21. package/dist/actions/shared.d.ts +40 -0
  22. package/dist/actions/shared.js +70 -0
  23. package/dist/actions/vault.d.ts +58 -0
  24. package/dist/actions/vault.js +94 -0
  25. package/dist/constants.d.ts +130 -0
  26. package/dist/constants.js +65 -0
  27. package/dist/display.d.ts +31 -0
  28. package/dist/display.js +28 -0
  29. package/dist/encoding.d.ts +110 -0
  30. package/dist/encoding.js +138 -0
  31. package/dist/errors.d.ts +76 -0
  32. package/dist/errors.js +66 -0
  33. package/dist/files/fetch.d.ts +85 -0
  34. package/dist/files/fetch.js +208 -0
  35. package/dist/files/verify.d.ts +36 -0
  36. package/dist/files/verify.js +125 -0
  37. package/dist/generated/abis/AccessManager.d.ts +1125 -0
  38. package/dist/generated/abis/AccessManager.js +1471 -0
  39. package/dist/generated/abis/Accounts.d.ts +1568 -0
  40. package/dist/generated/abis/Accounts.js +2049 -0
  41. package/dist/generated/abis/ClaimRouter.d.ts +343 -0
  42. package/dist/generated/abis/ClaimRouter.js +452 -0
  43. package/dist/generated/abis/ConsolidationAdapter.d.ts +97 -0
  44. package/dist/generated/abis/ConsolidationAdapter.js +133 -0
  45. package/dist/generated/abis/ContentStore.d.ts +812 -0
  46. package/dist/generated/abis/ContentStore.js +1073 -0
  47. package/dist/generated/abis/EconomicsResolver.d.ts +586 -0
  48. package/dist/generated/abis/EconomicsResolver.js +769 -0
  49. package/dist/generated/abis/Eip712CurationVerifier.d.ts +304 -0
  50. package/dist/generated/abis/Eip712CurationVerifier.js +406 -0
  51. package/dist/generated/abis/EnglishAuction.d.ts +1435 -0
  52. package/dist/generated/abis/EnglishAuction.js +1865 -0
  53. package/dist/generated/abis/GovernedVault.d.ts +468 -0
  54. package/dist/generated/abis/GovernedVault.js +617 -0
  55. package/dist/generated/abis/IndependentEditionAuction.d.ts +2373 -0
  56. package/dist/generated/abis/IndependentEditionAuction.js +3076 -0
  57. package/dist/generated/abis/IndependentStream6529.d.ts +1408 -0
  58. package/dist/generated/abis/IndependentStream6529.js +1834 -0
  59. package/dist/generated/abis/IndependentStreamDeployer.d.ts +141 -0
  60. package/dist/generated/abis/IndependentStreamDeployer.js +188 -0
  61. package/dist/generated/abis/IndependentStreamEditions6529.d.ts +963 -0
  62. package/dist/generated/abis/IndependentStreamEditions6529.js +1253 -0
  63. package/dist/generated/abis/IndependentStreamRegistry.d.ts +1351 -0
  64. package/dist/generated/abis/IndependentStreamRegistry.js +1755 -0
  65. package/dist/generated/abis/PackageRegistry.d.ts +1205 -0
  66. package/dist/generated/abis/PackageRegistry.js +1573 -0
  67. package/dist/generated/abis/RevenueEscrow.d.ts +626 -0
  68. package/dist/generated/abis/RevenueEscrow.js +820 -0
  69. package/dist/generated/abis/SaleStarter.d.ts +48 -0
  70. package/dist/generated/abis/SaleStarter.js +67 -0
  71. package/dist/generated/abis/SepoliaOpenGate.d.ts +317 -0
  72. package/dist/generated/abis/SepoliaOpenGate.js +419 -0
  73. package/dist/generated/abis/SplitLedger.d.ts +1097 -0
  74. package/dist/generated/abis/SplitLedger.js +1438 -0
  75. package/dist/generated/abis/StatementLog.d.ts +958 -0
  76. package/dist/generated/abis/StatementLog.js +1248 -0
  77. package/dist/generated/abis/Stream6529.d.ts +1490 -0
  78. package/dist/generated/abis/Stream6529.js +1941 -0
  79. package/dist/generated/abis/StreamDirectory6529.d.ts +355 -0
  80. package/dist/generated/abis/StreamDirectory6529.js +468 -0
  81. package/dist/generated/abis/StreamEditions6529.d.ts +1240 -0
  82. package/dist/generated/abis/StreamEditions6529.js +1614 -0
  83. package/dist/generated/abis/StreamTerms.d.ts +312 -0
  84. package/dist/generated/abis/StreamTerms.js +409 -0
  85. package/dist/generated/abis/StreamWorkFacts6529.d.ts +810 -0
  86. package/dist/generated/abis/StreamWorkFacts6529.js +1049 -0
  87. package/dist/generated/abis/UniformEditionAuction.d.ts +2372 -0
  88. package/dist/generated/abis/UniformEditionAuction.js +3074 -0
  89. package/dist/generated/abis/WorkDelivery.d.ts +605 -0
  90. package/dist/generated/abis/WorkDelivery.js +786 -0
  91. package/dist/generated/abis/WorkRules.d.ts +1154 -0
  92. package/dist/generated/abis/WorkRules.js +1501 -0
  93. package/dist/generated/abis/index.d.ts +28 -0
  94. package/dist/generated/abis/index.js +31 -0
  95. package/dist/generated/instances.d.ts +7 -0
  96. package/dist/generated/instances.js +9 -0
  97. package/dist/generated/sepoliaR10.d.ts +1391 -0
  98. package/dist/generated/sepoliaR10.js +1545 -0
  99. package/dist/hashing/content.d.ts +42 -0
  100. package/dist/hashing/content.js +54 -0
  101. package/dist/hashing/ids.d.ts +67 -0
  102. package/dist/hashing/ids.js +73 -0
  103. package/dist/hashing/items.d.ts +30 -0
  104. package/dist/hashing/items.js +44 -0
  105. package/dist/hashing/packages.d.ts +35 -0
  106. package/dist/hashing/packages.js +76 -0
  107. package/dist/hashing/vault.d.ts +17 -0
  108. package/dist/hashing/vault.js +37 -0
  109. package/dist/hashing/workState.d.ts +31 -0
  110. package/dist/hashing/workState.js +37 -0
  111. package/dist/index.d.ts +44 -0
  112. package/dist/index.js +47 -0
  113. package/dist/instance.d.ts +236 -0
  114. package/dist/instance.js +99 -0
  115. package/dist/items.d.ts +85 -0
  116. package/dist/items.js +144 -0
  117. package/dist/reads/accounts.d.ts +86 -0
  118. package/dist/reads/accounts.js +121 -0
  119. package/dist/reads/chain.d.ts +54 -0
  120. package/dist/reads/chain.js +133 -0
  121. package/dist/reads/creation.d.ts +139 -0
  122. package/dist/reads/creation.js +256 -0
  123. package/dist/reads/editionSale.d.ts +120 -0
  124. package/dist/reads/editionSale.js +182 -0
  125. package/dist/reads/metadata.d.ts +195 -0
  126. package/dist/reads/metadata.js +191 -0
  127. package/dist/reads/money.d.ts +51 -0
  128. package/dist/reads/money.js +68 -0
  129. package/dist/reads/network.d.ts +137 -0
  130. package/dist/reads/network.js +157 -0
  131. package/dist/reads/sale.d.ts +94 -0
  132. package/dist/reads/sale.js +133 -0
  133. package/dist/reads/vault.d.ts +47 -0
  134. package/dist/reads/vault.js +75 -0
  135. package/dist/reads/work.d.ts +293 -0
  136. package/dist/reads/work.js +338 -0
  137. package/dist/reads/workPage.d.ts +197 -0
  138. package/dist/reads/workPage.js +169 -0
  139. package/dist/signing/messages.d.ts +679 -0
  140. package/dist/signing/messages.js +397 -0
  141. package/dist/testing.d.ts +17 -0
  142. package/dist/testing.js +18 -0
  143. package/dist/types.d.ts +167 -0
  144. package/dist/types.js +14 -0
  145. package/docs/publishing.md +54 -0
  146. package/docs/reference.md +335 -0
  147. package/docs/using-from-a-site.md +175 -0
  148. package/package.json +93 -4
@@ -0,0 +1,293 @@
1
+ import { type Address, type Client, type Hex } from "viem";
2
+ import { type StreamInstance } from "../instance.js";
3
+ import type { ArtistIdentity, MediaFile, PackageItem, Payee } from "../types.js";
4
+ import { type LogScanOptions } from "./chain.js";
5
+ /** Which work: a token ID of the curated `Stream6529`, or of an artist's own contract when `token` is given. */
6
+ export interface WorkRef {
7
+ /** The 1/1 token contract. Default: the instance's `Stream6529`. */
8
+ readonly token?: Address;
9
+ readonly tokenId: bigint;
10
+ }
11
+ /**
12
+ * How a 1/1's first sale ended. `"unsold"` covers every end without a buyer (no bid, or a sale
13
+ * that was closed with its bid returned); `"rescued"` means the network moved the work out of a
14
+ * removed sale contract.
15
+ */
16
+ export type FirstSaleOutcome = "none" | "sold" | "unsold" | "rescued" | "unknown";
17
+ /** A work's permanent facts and its record, as the token and the facts contract state them. */
18
+ export interface Work {
19
+ readonly token: Address;
20
+ readonly tokenId: bigint;
21
+ /** True for the curated `Stream6529`; false for an artist's own Independent Stream contract. */
22
+ readonly curated: boolean;
23
+ /** The hash of the item the work was minted from: its identity. */
24
+ readonly itemHash: Hex;
25
+ readonly artistId: bigint;
26
+ /** The artist account's identity now, or null if the accounts module does not know the ID. */
27
+ readonly artist: ArtistIdentity | null;
28
+ readonly collection: {
29
+ readonly id: bigint;
30
+ readonly name: string;
31
+ readonly metadataHash: Hex;
32
+ /** How many works the collection holds. */
33
+ readonly minted: number;
34
+ };
35
+ /** The work's number within its collection, from 1. */
36
+ readonly serial: number;
37
+ /** `ITEM_KIND`: 0 an original, 1 an edition master. */
38
+ readonly kind: number;
39
+ /** `SEED_POLICY`, and the seed (zero for an unseeded work, or until a VRF seed arrives). */
40
+ readonly seedPolicy: number;
41
+ readonly seed: Hex;
42
+ /** The holder, or null when the token does not exist any more (burned). */
43
+ readonly owner: Address | null;
44
+ /** How the first sale ended; `"none"` until it settles. */
45
+ readonly firstSale: FirstSaleOutcome;
46
+ readonly royalty: {
47
+ /** The split wallet royalties are paid to. */
48
+ readonly receiver: Address;
49
+ /** Out of 100,000. */
50
+ readonly rate: number;
51
+ /** True once the rate can never change again. */
52
+ readonly locked: boolean;
53
+ };
54
+ /**
55
+ * The work's record in `StreamWorkFacts6529`. Until someone opens it (`opened` false), the
56
+ * fields below it are empty: the token exists, but its files and license are not published yet.
57
+ */
58
+ readonly record: {
59
+ readonly opened: boolean;
60
+ /** The file ID of the descriptive record (JSON). The artist may replace it until `finalized`. */
61
+ readonly recordFileId: Hex;
62
+ readonly finalized: boolean;
63
+ /** True once every file was attested complete in the content store. */
64
+ readonly onChainComplete: boolean;
65
+ readonly renderer: Address;
66
+ readonly rendererCodeHash: Hex;
67
+ readonly licenseLocked: boolean;
68
+ readonly repinLocked: boolean;
69
+ };
70
+ /** The license now: the SPDX identifier, the file ID of its text, and its canonical key. */
71
+ readonly license: {
72
+ readonly spdxId: string;
73
+ readonly textFileId: Hex;
74
+ readonly key: Hex;
75
+ } | null;
76
+ /** The work's files, primary first: file ID and `FILE_ROLE`. Their hashes come from `readWorkFiles`. */
77
+ readonly media: readonly {
78
+ readonly fileId: Hex;
79
+ readonly role: number;
80
+ }[];
81
+ }
82
+ /**
83
+ * A work's permanent facts: its item hash, artist, collection, kind, seed, royalty, how its
84
+ * first sale ended, its record, license and files.
85
+ *
86
+ * Contract: [`Stream6529.workOf`, `ownerOf`, `collectionOf`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamBase.sol),
87
+ * [`StreamWorkFacts6529.factsOf`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamWorkFacts6529.sol),
88
+ * [`Accounts.artist`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/identity/Accounts.sol).
89
+ * An artist's own contract answers the same functions.
90
+ *
91
+ * @throws StreamSdkError `NO_SUCH_WORK` if the token was never minted (`workOf` then answers zeros).
92
+ */
93
+ export declare function readWork(client: Client, instance: StreamInstance, ref: WorkRef): Promise<Work>;
94
+ /** What the mint of a work recorded, from its `WorkMinted` event. */
95
+ export interface WorkMint {
96
+ /** SHA-256 of the primary file: the key a token contract mints once. */
97
+ readonly contentHash: Hex;
98
+ /** The package the item was minted through; zero for an Independent Stream work. */
99
+ readonly packageRoot: Hex;
100
+ readonly mediaHash: Hex;
101
+ /** The record file the item named at mint. */
102
+ readonly recordFileId: Hex;
103
+ /** The split wallet that receives the artist portion of the first sale. */
104
+ readonly seller: Address;
105
+ /** The sale contract that minted the work. */
106
+ readonly minter: Address;
107
+ readonly blockNumber: bigint;
108
+ /** The transaction that started the first sale. */
109
+ readonly transactionHash: Hex;
110
+ }
111
+ /**
112
+ * The facts a work's mint recorded: its content hash, package root, media hash, seller vault
113
+ * and the transaction that started its first sale. Returns null if no mint event is found in
114
+ * the range scanned.
115
+ *
116
+ * Checked before it is returned: the event's item hash is the token's `itemHashOf`, and the
117
+ * token contract maps the content hash back to this token (`tokenOfContent`).
118
+ *
119
+ * Contract: the `WorkMinted` event of [`Stream6529`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamBase.sol).
120
+ */
121
+ export declare function readWorkMint(client: Client, instance: StreamInstance, ref: WorkRef, options?: LogScanOptions): Promise<WorkMint | null>;
122
+ /** The item a work was minted from, proven against the hash the token stores. */
123
+ export interface WorkItem {
124
+ /** Parties, payees (the split), license at mint, seed policy, sale terms and edition terms. */
125
+ readonly item: PackageItem;
126
+ /** The media list with each file's SHA-256, or null if only a start transaction without one was found. */
127
+ readonly media: readonly MediaFile[] | null;
128
+ /** Curated only, and only when read from the start transaction: the package root and proof. */
129
+ readonly packageRoot: Hex | null;
130
+ readonly proof: readonly Hex[] | null;
131
+ /** Where the item was found: the record's `RecordOpened` event, or the first sale's start transaction. */
132
+ readonly source: "record" | "startTransaction";
133
+ /** The transaction the item was read from: the one that emitted the event, or the start transaction itself. */
134
+ readonly transactionHash: Hex;
135
+ /** For `"startTransaction"`: the contract the transaction called (the sale contract or the sale starter). Null for `"record"`. */
136
+ readonly calledContract: Address | null;
137
+ }
138
+ /**
139
+ * The full item a work was minted from: who made it, who is paid and in what shares, the
140
+ * license it was approved with, and its sale and edition terms. The chain stores only the
141
+ * item's hash; the item itself is in the `RecordOpened` event once the record is open, and
142
+ * before that in the calldata of the transaction that started the first sale.
143
+ *
144
+ * Nothing is returned unverified: `itemHash(instance.protocol, item)` must equal `Stream6529.itemHashOf(tokenId)`,
145
+ * and a media list must hash to the item's `mediaHash`. Returns null if the item cannot be
146
+ * found in the range scanned.
147
+ *
148
+ * Contract: the `RecordOpened` event of [`StreamWorkFacts6529`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamWorkFacts6529.sol);
149
+ * `EnglishAuction.start`, `startWithBid` and `SaleStarter.start` calldata.
150
+ */
151
+ export declare function readWorkItem(client: Client, instance: StreamInstance, ref: WorkRef, options?: LogScanOptions): Promise<WorkItem | null>;
152
+ /** One file of a work, as the content store and the delivery module describe it. */
153
+ export interface WorkFile {
154
+ readonly fileId: Hex;
155
+ /** `FILE_ROLE`: 0 primary, 1 preview, anything else auxiliary. */
156
+ readonly role: number;
157
+ /** SHA-256 of the decoded file, as its manifest declares it. `fetchFile` checks bytes against it. */
158
+ readonly sha256: Hex;
159
+ /** The stored length in bytes. */
160
+ readonly length: bigint;
161
+ readonly chunkCount: number;
162
+ /** `ENCODING`. */
163
+ readonly encoding: number;
164
+ readonly mimeType: string;
165
+ /** 0 for a SHA-256 digest; another kind is not handled by this release. */
166
+ readonly digestKind: number;
167
+ /** True when every chunk is in the content store, so the file can be read from the chain. */
168
+ readonly onChain: boolean;
169
+ /** Registered copies elsewhere: `ar://<txid>` (kind 1) and `ipfs://<CIDv1>` (kind 2). */
170
+ readonly locators: readonly {
171
+ readonly kind: number;
172
+ readonly uri: string;
173
+ }[];
174
+ }
175
+ /**
176
+ * The files of a work with their hashes, sizes, types and where copies can be fetched.
177
+ * The record must be open (`readWork(...).record.opened`), or the list is empty.
178
+ *
179
+ * Contract: [`StreamWorkFacts6529.factsOf`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamWorkFacts6529.sol),
180
+ * [`ContentStore.fileInfo`, `isComplete`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/content/ContentStore.sol),
181
+ * [`WorkDelivery.locators`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/stream/WorkDelivery.sol).
182
+ */
183
+ export declare function readWorkFiles(client: Client, instance: StreamInstance, ref: WorkRef): Promise<WorkFile[]>;
184
+ /** A royalty amount for a sale price, and who receives it. */
185
+ export interface RoyaltyQuote {
186
+ readonly receiver: Address;
187
+ readonly amount: bigint;
188
+ }
189
+ /**
190
+ * The ERC-2981 royalty for a sale of `salePrice`.
191
+ *
192
+ * Contract: [`Stream6529.royaltyInfo`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamBase.sol).
193
+ */
194
+ export declare function readRoyalty(client: Client, instance: StreamInstance, ref: WorkRef, salePrice: bigint): Promise<RoyaltyQuote>;
195
+ /** One payee of a split wallet, with what it can take out now. */
196
+ export interface SplitPayee {
197
+ readonly index: number;
198
+ /**
199
+ * How the payee is named: `"artist"` by an artist ID (paid at that account's payout
200
+ * address, which the artist can change), `"address"` by one fixed address.
201
+ */
202
+ readonly kind: "artist" | "address";
203
+ /** The artist ID for an `"artist"` payee; zero for an `"address"` payee. */
204
+ readonly artistId: bigint;
205
+ /**
206
+ * The fixed address of an `"address"` payee. NULL for an `"artist"` payee: such a payee
207
+ * names no address (the item holds the zero address there). To show who is paid, use
208
+ * `paidTo`; to show who the payee is, use `artistId`.
209
+ */
210
+ readonly account: Address | null;
211
+ /**
212
+ * The payee exactly as the item lists it, zero address included. For building the payee
213
+ * list a release or a vault address needs (`split.payees.map((payee) => payee.listed)`),
214
+ * never for display.
215
+ */
216
+ readonly listed: Payee;
217
+ /** Out of 100,000. */
218
+ readonly share: number;
219
+ /** `share` as a percentage, for display. */
220
+ readonly percent: number;
221
+ /**
222
+ * Where this payee's money goes now: the account's payout address for an `"artist"` payee
223
+ * (null if the account does not exist yet), or the fixed address. This is the address to
224
+ * show as the one being paid.
225
+ */
226
+ readonly paidTo: Address | null;
227
+ /** The same as `paidTo`, under its earlier name. */
228
+ readonly payoutAddress: Address | null;
229
+ /**
230
+ * Who can send `release` for this payee. `"payoutAddress"`: only the payout address itself
231
+ * (not the artist's other wallets); anyone else needs the payee's signed authorization
232
+ * (`signReleaseAuthorization`, then `releaseSigned`). `"anyone"`: a payee at a fixed address
233
+ * can be paid out by any caller, and the money still goes only to that address.
234
+ */
235
+ readonly releasableBy: "payoutAddress" | "anyone";
236
+ /** ETH this payee can release now. */
237
+ readonly releasable: bigint;
238
+ /** ETH this payee has released so far. */
239
+ readonly released: bigint;
240
+ }
241
+ /**
242
+ * Whether `viewer` can send the plain release (`releaseFromVault`) for this payee: the "take
243
+ * out" button. For an `"artist"` payee only the wallet that IS the payout address can (the
244
+ * artist's other wallets cannot; they need `signReleaseAuthorization` and `releaseSigned`).
245
+ * For an `"address"` payee any connected wallet can, and the money still goes only to
246
+ * `paidTo`. False with no viewer, and for an artist account that does not exist yet.
247
+ *
248
+ * Contract: the caller rule of [`SplitLedger.release`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/economics/SplitLedger.sol);
249
+ * nothing is read here.
250
+ */
251
+ export declare function canRelease(payee: Pick<SplitPayee, "releasableBy" | "paidTo">, viewer: Address | null | undefined): boolean;
252
+ /** The split wallet (vault) of a payee list. */
253
+ export interface Split {
254
+ /** The vault's address. ETH sent to it is held even before the wallet contract is deployed. */
255
+ readonly wallet: Address;
256
+ readonly payees: readonly SplitPayee[];
257
+ }
258
+ /**
259
+ * The seller vault of a work's payees: its address, and what each payee has released and can
260
+ * release now, in ETH. `payees` is the item's payee list (`readWorkItem(...).item.payees`).
261
+ *
262
+ * Each payee comes with where its money goes and who can send its release: a payee named by
263
+ * artist ID is paid at its account's payout address, and only that address can call
264
+ * `release` for it.
265
+ *
266
+ * Contract: [`SplitLedger.walletFor`, `releasable`, `released`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/economics/SplitLedger.sol),
267
+ * with salt zero, as the terms module derives a work's seller vault.
268
+ */
269
+ export declare function readSplit(client: Client, instance: StreamInstance, payees: readonly Payee[]): Promise<Split>;
270
+ /** A collection of a token contract. */
271
+ export interface Collection {
272
+ readonly id: bigint;
273
+ readonly name: string;
274
+ readonly metadataHash: Hex;
275
+ readonly minted: number;
276
+ }
277
+ /**
278
+ * The collections of a token contract, newest first, at most `limit` (default 100).
279
+ * Collection names are free text from the chain: clean them before display.
280
+ *
281
+ * Contract: [`Stream6529.collectionCount`, `collectionOf`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamBase.sol).
282
+ */
283
+ export declare function readCollections(client: Client, instance: StreamInstance, options?: {
284
+ readonly token?: Address;
285
+ readonly limit?: number;
286
+ }): Promise<Collection[]>;
287
+ /**
288
+ * The file ID of the CC0 1.0 legal text this instance stores: the `textFileId` of a CC0
289
+ * license (`cc0License`).
290
+ *
291
+ * Contract: [`StreamWorkFacts6529.cc0TextFileId`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamWorkFacts6529.sol).
292
+ */
293
+ export declare function readCc0TextFileId(client: Client, instance: StreamInstance): Promise<Hex>;
@@ -0,0 +1,338 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import { BaseError, ContractFunctionZeroDataError, getAbiItem, isAddressEqual, zeroAddress } from "viem";
3
+ import { getTransaction, readContract } from "viem/actions";
4
+ import { DENOMINATOR, OUTCOME, ZERO_HASH } from "../constants.js";
5
+ import { decodeStartCalldata } from "../encoding.js";
6
+ import { StreamSdkError } from "../errors.js";
7
+ import { accountsAbi } from "../generated/abis/Accounts.js";
8
+ import { contentStoreAbi } from "../generated/abis/ContentStore.js";
9
+ import { splitLedgerAbi } from "../generated/abis/SplitLedger.js";
10
+ import { stream6529Abi } from "../generated/abis/Stream6529.js";
11
+ import { streamWorkFacts6529Abi } from "../generated/abis/StreamWorkFacts6529.js";
12
+ import { workDeliveryAbi } from "../generated/abis/WorkDelivery.js";
13
+ import { itemHash, licenseKey, mediaHash } from "../hashing/items.js";
14
+ import { vaultAddress } from "../hashing/vault.js";
15
+ import { assertInstanceChain } from "../instance.js";
16
+ import { orOnRevert, scanEvents } from "./chain.js";
17
+ const OUTCOME_NAMES = {
18
+ [OUTCOME.NONE]: "none",
19
+ [OUTCOME.SOLD]: "sold",
20
+ [OUTCOME.UNSOLD]: "unsold",
21
+ [OUTCOME.RESCUED]: "rescued",
22
+ };
23
+ function tokenOf(instance, ref) {
24
+ return ref.token ?? instance.addresses.stream;
25
+ }
26
+ /**
27
+ * A work's permanent facts: its item hash, artist, collection, kind, seed, royalty, how its
28
+ * first sale ended, its record, license and files.
29
+ *
30
+ * Contract: [`Stream6529.workOf`, `ownerOf`, `collectionOf`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamBase.sol),
31
+ * [`StreamWorkFacts6529.factsOf`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamWorkFacts6529.sol),
32
+ * [`Accounts.artist`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/identity/Accounts.sol).
33
+ * An artist's own contract answers the same functions.
34
+ *
35
+ * @throws StreamSdkError `NO_SUCH_WORK` if the token was never minted (`workOf` then answers zeros).
36
+ */
37
+ export async function readWork(client, instance, ref) {
38
+ await assertInstanceChain(client, instance);
39
+ const token = tokenOf(instance, ref);
40
+ const onToken = { address: token, abi: stream6529Abi };
41
+ const [work, owner, facts] = await Promise.all([
42
+ readContract(client, { ...onToken, functionName: "workOf", args: [ref.tokenId] }).catch((error) => {
43
+ // No code at the address, or a contract that does not answer workOf: not a Stream token.
44
+ if (error instanceof BaseError && error.walk((cause) => cause instanceof ContractFunctionZeroDataError)) {
45
+ throw new StreamSdkError("NO_SUCH_WORK", `${token} is not a Stream token contract on this chain.`);
46
+ }
47
+ throw error;
48
+ }),
49
+ orOnRevert(readContract(client, { ...onToken, functionName: "ownerOf", args: [ref.tokenId] }), null),
50
+ readContract(client, {
51
+ address: instance.addresses.workFacts,
52
+ abi: streamWorkFacts6529Abi,
53
+ functionName: "factsOf",
54
+ args: [token, ref.tokenId],
55
+ }),
56
+ ]);
57
+ if (work.itemHash === ZERO_HASH) {
58
+ throw new StreamSdkError("NO_SUCH_WORK", `Token ${ref.tokenId} of ${token} was never minted.`);
59
+ }
60
+ const [collection, artist] = await Promise.all([
61
+ readContract(client, { ...onToken, functionName: "collectionOf", args: [BigInt(work.collectionId)] }),
62
+ // `Accounts.artist` reverts for an ID it does not know.
63
+ orOnRevert(readContract(client, { address: instance.addresses.accounts, abi: accountsAbi, functionName: "artist", args: [work.artistId] }), null),
64
+ ]);
65
+ return {
66
+ token,
67
+ tokenId: ref.tokenId,
68
+ curated: isAddressEqual(token, instance.addresses.stream),
69
+ itemHash: work.itemHash,
70
+ artistId: work.artistId,
71
+ artist: artist ? artist[0] : null,
72
+ collection: {
73
+ id: BigInt(work.collectionId),
74
+ name: collection.name,
75
+ metadataHash: collection.metadataHash,
76
+ minted: collection.minted,
77
+ },
78
+ serial: work.serial,
79
+ kind: work.kind,
80
+ seedPolicy: work.seedPolicy,
81
+ seed: work.seed,
82
+ owner,
83
+ firstSale: OUTCOME_NAMES[work.outcome] ?? "unknown",
84
+ royalty: { receiver: work.royaltyReceiver, rate: work.royaltyRate, locked: work.royaltyRateLocked },
85
+ record: {
86
+ opened: facts.opened,
87
+ recordFileId: facts.recordFileId,
88
+ finalized: facts.finalized,
89
+ onChainComplete: facts.onChainComplete,
90
+ renderer: facts.renderer,
91
+ rendererCodeHash: facts.rendererCodeHash,
92
+ licenseLocked: facts.licenseLocked,
93
+ repinLocked: facts.repinLocked,
94
+ },
95
+ license: facts.opened
96
+ ? { spdxId: facts.licenseId, textFileId: facts.licenseTextFileId, key: licenseKey(facts.licenseId) }
97
+ : null,
98
+ media: facts.media.map((file) => ({ fileId: file.fileId, role: file.role })),
99
+ };
100
+ }
101
+ const WORK_MINTED = /*#__PURE__*/ getAbiItem({ abi: stream6529Abi, name: "WorkMinted" });
102
+ const RECORD_OPENED = /*#__PURE__*/ getAbiItem({ abi: streamWorkFacts6529Abi, name: "RecordOpened" });
103
+ /**
104
+ * The facts a work's mint recorded: its content hash, package root, media hash, seller vault
105
+ * and the transaction that started its first sale. Returns null if no mint event is found in
106
+ * the range scanned.
107
+ *
108
+ * Checked before it is returned: the event's item hash is the token's `itemHashOf`, and the
109
+ * token contract maps the content hash back to this token (`tokenOfContent`).
110
+ *
111
+ * Contract: the `WorkMinted` event of [`Stream6529`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamBase.sol).
112
+ */
113
+ export async function readWorkMint(client, instance, ref, options = {}) {
114
+ await assertInstanceChain(client, instance);
115
+ const token = tokenOf(instance, ref);
116
+ const logs = await scanEvents(client, instance, { address: token, event: WORK_MINTED, args: { tokenId: ref.tokenId } }, options);
117
+ const log = logs[0];
118
+ if (!log)
119
+ return null;
120
+ const facts = log.args.facts;
121
+ // `workOf` keeps answering for a burned token; `itemHashOf` reverts for one.
122
+ const [minted, contentToken] = await Promise.all([
123
+ readContract(client, { address: token, abi: stream6529Abi, functionName: "workOf", args: [ref.tokenId] }).then((work) => work.itemHash),
124
+ readContract(client, { address: token, abi: stream6529Abi, functionName: "tokenOfContent", args: [facts.contentHash] }),
125
+ ]);
126
+ if (minted !== facts.itemHash) {
127
+ throw new StreamSdkError("MISMATCH", "The WorkMinted event does not carry the item hash the token stores.");
128
+ }
129
+ // A burned work's content can be released and minted again, so only a live mapping must agree.
130
+ if (contentToken !== 0n && contentToken !== ref.tokenId) {
131
+ throw new StreamSdkError("MISMATCH", "The token contract maps this content hash to another token.");
132
+ }
133
+ return {
134
+ contentHash: facts.contentHash,
135
+ packageRoot: facts.packageRoot,
136
+ mediaHash: facts.mediaHash,
137
+ recordFileId: facts.recordFileId,
138
+ seller: facts.seller,
139
+ minter: log.args.minter,
140
+ blockNumber: log.blockNumber,
141
+ transactionHash: log.transactionHash,
142
+ };
143
+ }
144
+ /**
145
+ * The full item a work was minted from: who made it, who is paid and in what shares, the
146
+ * license it was approved with, and its sale and edition terms. The chain stores only the
147
+ * item's hash; the item itself is in the `RecordOpened` event once the record is open, and
148
+ * before that in the calldata of the transaction that started the first sale.
149
+ *
150
+ * Nothing is returned unverified: `itemHash(instance.protocol, item)` must equal `Stream6529.itemHashOf(tokenId)`,
151
+ * and a media list must hash to the item's `mediaHash`. Returns null if the item cannot be
152
+ * found in the range scanned.
153
+ *
154
+ * Contract: the `RecordOpened` event of [`StreamWorkFacts6529`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamWorkFacts6529.sol);
155
+ * `EnglishAuction.start`, `startWithBid` and `SaleStarter.start` calldata.
156
+ */
157
+ export async function readWorkItem(client, instance, ref, options = {}) {
158
+ await assertInstanceChain(client, instance);
159
+ const token = tokenOf(instance, ref);
160
+ // `workOf` keeps answering for a burned token; `itemHashOf` reverts for one.
161
+ const { itemHash: expected } = await readContract(client, { address: token, abi: stream6529Abi, functionName: "workOf", args: [ref.tokenId] });
162
+ if (expected === ZERO_HASH)
163
+ return null;
164
+ const verified = (item, media) => itemHash(instance.protocol, item) === expected && (media === null || mediaHash(instance.protocol, media) === item.mediaHash);
165
+ const opened = await scanEvents(client, instance, { address: instance.addresses.workFacts, event: RECORD_OPENED, args: { token, tokenId: ref.tokenId } }, options);
166
+ for (const log of opened) {
167
+ if (verified(log.args.item, log.args.media)) {
168
+ return { item: log.args.item, media: log.args.media, packageRoot: null, proof: null, source: "record", transactionHash: log.transactionHash, calledContract: null };
169
+ }
170
+ }
171
+ const mint = await readWorkMint(client, instance, ref, options);
172
+ if (!mint)
173
+ return null;
174
+ const transaction = await getTransaction(client, { hash: mint.transactionHash });
175
+ const started = decodeStartCalldata(transaction.input);
176
+ if (!started || !isAddressEqual(started.token, token) || !verified(started.item, started.media))
177
+ return null;
178
+ return {
179
+ item: started.item,
180
+ media: started.media,
181
+ packageRoot: started.packageRoot,
182
+ proof: started.proof,
183
+ source: "startTransaction",
184
+ transactionHash: mint.transactionHash,
185
+ calledContract: transaction.to,
186
+ };
187
+ }
188
+ /**
189
+ * The files of a work with their hashes, sizes, types and where copies can be fetched.
190
+ * The record must be open (`readWork(...).record.opened`), or the list is empty.
191
+ *
192
+ * Contract: [`StreamWorkFacts6529.factsOf`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamWorkFacts6529.sol),
193
+ * [`ContentStore.fileInfo`, `isComplete`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/content/ContentStore.sol),
194
+ * [`WorkDelivery.locators`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/stream/WorkDelivery.sol).
195
+ */
196
+ export async function readWorkFiles(client, instance, ref) {
197
+ await assertInstanceChain(client, instance);
198
+ const token = tokenOf(instance, ref);
199
+ const facts = await readContract(client, {
200
+ address: instance.addresses.workFacts,
201
+ abi: streamWorkFacts6529Abi,
202
+ functionName: "factsOf",
203
+ args: [token, ref.tokenId],
204
+ });
205
+ const store = { address: instance.addresses.contentStore, abi: contentStoreAbi };
206
+ return Promise.all(facts.media.map(async (file) => {
207
+ const [info, onChain, locators] = await Promise.all([
208
+ readContract(client, { ...store, functionName: "fileInfo", args: [file.fileId] }),
209
+ readContract(client, { ...store, functionName: "isComplete", args: [file.fileId] }),
210
+ readContract(client, {
211
+ address: instance.addresses.workDelivery,
212
+ abi: workDeliveryAbi,
213
+ functionName: "locators",
214
+ args: [token, ref.tokenId, file.fileId],
215
+ }),
216
+ ]);
217
+ return {
218
+ fileId: file.fileId,
219
+ role: file.role,
220
+ sha256: info.sha256Hash,
221
+ length: info.length,
222
+ chunkCount: Number(info.chunkCount),
223
+ encoding: info.encoding,
224
+ mimeType: info.mimeType,
225
+ digestKind: info.digestKind,
226
+ onChain,
227
+ locators: locators.map((locator) => ({ kind: locator.kind, uri: locator.uri })),
228
+ };
229
+ }));
230
+ }
231
+ /**
232
+ * The ERC-2981 royalty for a sale of `salePrice`.
233
+ *
234
+ * Contract: [`Stream6529.royaltyInfo`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamBase.sol).
235
+ */
236
+ export async function readRoyalty(client, instance, ref, salePrice) {
237
+ await assertInstanceChain(client, instance);
238
+ const [receiver, amount] = await readContract(client, {
239
+ address: tokenOf(instance, ref),
240
+ abi: stream6529Abi,
241
+ functionName: "royaltyInfo",
242
+ args: [ref.tokenId, salePrice],
243
+ });
244
+ return { receiver, amount };
245
+ }
246
+ /**
247
+ * Whether `viewer` can send the plain release (`releaseFromVault`) for this payee: the "take
248
+ * out" button. For an `"artist"` payee only the wallet that IS the payout address can (the
249
+ * artist's other wallets cannot; they need `signReleaseAuthorization` and `releaseSigned`).
250
+ * For an `"address"` payee any connected wallet can, and the money still goes only to
251
+ * `paidTo`. False with no viewer, and for an artist account that does not exist yet.
252
+ *
253
+ * Contract: the caller rule of [`SplitLedger.release`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/economics/SplitLedger.sol);
254
+ * nothing is read here.
255
+ */
256
+ export function canRelease(payee, viewer) {
257
+ if (!viewer || !payee.paidTo)
258
+ return false;
259
+ return payee.releasableBy === "anyone" || isAddressEqual(viewer, payee.paidTo);
260
+ }
261
+ /**
262
+ * The seller vault of a work's payees: its address, and what each payee has released and can
263
+ * release now, in ETH. `payees` is the item's payee list (`readWorkItem(...).item.payees`).
264
+ *
265
+ * Each payee comes with where its money goes and who can send its release: a payee named by
266
+ * artist ID is paid at its account's payout address, and only that address can call
267
+ * `release` for it.
268
+ *
269
+ * Contract: [`SplitLedger.walletFor`, `releasable`, `released`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/economics/SplitLedger.sol),
270
+ * with salt zero, as the terms module derives a work's seller vault.
271
+ */
272
+ export async function readSplit(client, instance, payees) {
273
+ await assertInstanceChain(client, instance);
274
+ const ledger = { address: instance.addresses.splitLedger, abi: splitLedgerAbi };
275
+ // The address is computed here; the ledger's answer must be the same.
276
+ const wallet = vaultAddress(instance, payees);
277
+ const answered = await readContract(client, { ...ledger, functionName: "walletFor", args: [payees, ZERO_HASH] });
278
+ if (!isAddressEqual(answered, wallet)) {
279
+ throw new StreamSdkError("MISMATCH", `The ledger answers vault ${answered} for this payee list, but its address computed here is ${wallet}.`);
280
+ }
281
+ const rows = await Promise.all(payees.map(async (payee, index) => {
282
+ const byArtist = payee.artistId !== 0n;
283
+ const [releasable, released, payout] = await Promise.all([
284
+ readContract(client, { ...ledger, functionName: "releasable", args: [payees, ZERO_HASH, zeroAddress, BigInt(index)] }),
285
+ readContract(client, { ...ledger, functionName: "released", args: [wallet, zeroAddress, BigInt(index)] }),
286
+ // `payoutAddress` reverts for an account that does not exist yet.
287
+ byArtist
288
+ ? orOnRevert(readContract(client, { address: instance.addresses.accounts, abi: accountsAbi, functionName: "payoutAddress", args: [payee.artistId] }), null)
289
+ : payee.account,
290
+ ]);
291
+ return {
292
+ index,
293
+ artistId: payee.artistId,
294
+ kind: byArtist ? "artist" : "address",
295
+ account: byArtist ? null : payee.account,
296
+ listed: { artistId: payee.artistId, account: payee.account, share: payee.share },
297
+ share: payee.share,
298
+ percent: payee.share / (DENOMINATOR / 100),
299
+ paidTo: payout,
300
+ payoutAddress: payout,
301
+ releasableBy: byArtist ? "payoutAddress" : "anyone",
302
+ releasable,
303
+ released,
304
+ };
305
+ }));
306
+ return { wallet, payees: rows };
307
+ }
308
+ /**
309
+ * The collections of a token contract, newest first, at most `limit` (default 100).
310
+ * Collection names are free text from the chain: clean them before display.
311
+ *
312
+ * Contract: [`Stream6529.collectionCount`, `collectionOf`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamBase.sol).
313
+ */
314
+ export async function readCollections(client, instance, options = {}) {
315
+ await assertInstanceChain(client, instance);
316
+ const onToken = { address: options.token ?? instance.addresses.stream, abi: stream6529Abi };
317
+ const count = await readContract(client, { ...onToken, functionName: "collectionCount" });
318
+ const ids = [];
319
+ for (let id = count; id >= 1n && ids.length < (options.limit ?? 100); id--)
320
+ ids.push(id);
321
+ const collections = await Promise.all(ids.map((id) => readContract(client, { ...onToken, functionName: "collectionOf", args: [id] })));
322
+ return collections.map((collection, index) => ({
323
+ id: ids[index] ?? 0n,
324
+ name: collection.name,
325
+ metadataHash: collection.metadataHash,
326
+ minted: collection.minted,
327
+ }));
328
+ }
329
+ /**
330
+ * The file ID of the CC0 1.0 legal text this instance stores: the `textFileId` of a CC0
331
+ * license (`cc0License`).
332
+ *
333
+ * Contract: [`StreamWorkFacts6529.cc0TextFileId`](https://github.com/6529-Collections/6529-stream/blob/c6d4965861907841519cf3878c9db7052424a9a0/src/core/StreamWorkFacts6529.sol).
334
+ */
335
+ export async function readCc0TextFileId(client, instance) {
336
+ await assertInstanceChain(client, instance);
337
+ return readContract(client, { address: instance.addresses.workFacts, abi: streamWorkFacts6529Abi, functionName: "cc0TextFileId" });
338
+ }