@zing-protocol/zing-sdk 0.0.1

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 (87) hide show
  1. package/README.md +12 -0
  2. package/package.json +37 -0
  3. package/src/_generated/index.js +6 -0
  4. package/src/_generated/utils/index.ts +181 -0
  5. package/src/_generated/zing_framework/coin_utils.ts +41 -0
  6. package/src/_generated/zing_framework/deps/sui/object.ts +16 -0
  7. package/src/_generated/zing_framework/derived_object_bag.ts +400 -0
  8. package/src/_generated/zing_framework/derived_table.ts +366 -0
  9. package/src/_generated/zing_governance/admin.ts +47 -0
  10. package/src/_generated/zing_governance/deps/sui/object.ts +16 -0
  11. package/src/_generated/zing_governance/treasury.ts +134 -0
  12. package/src/_generated/zing_identity/config.ts +191 -0
  13. package/src/_generated/zing_identity/deps/std/type_name.ts +25 -0
  14. package/src/_generated/zing_identity/deps/sui/object.ts +16 -0
  15. package/src/_generated/zing_identity/deps/sui/object_table.ts +26 -0
  16. package/src/_generated/zing_identity/deps/sui/table.ts +38 -0
  17. package/src/_generated/zing_identity/deps/sui/vec_set.ts +28 -0
  18. package/src/_generated/zing_identity/ecdsa.ts +166 -0
  19. package/src/_generated/zing_identity/identity.ts +661 -0
  20. package/src/_generated/zing_identity/reclaim.ts +803 -0
  21. package/src/_generated/zing_studio/app.ts +855 -0
  22. package/src/_generated/zing_studio/article.ts +598 -0
  23. package/src/_generated/zing_studio/config.ts +475 -0
  24. package/src/_generated/zing_studio/deps/std/type_name.ts +25 -0
  25. package/src/_generated/zing_studio/deps/sui/bag.ts +43 -0
  26. package/src/_generated/zing_studio/deps/sui/balance.ts +20 -0
  27. package/src/_generated/zing_studio/deps/sui/coin.ts +21 -0
  28. package/src/_generated/zing_studio/deps/sui/dynamic_field.ts +272 -0
  29. package/src/_generated/zing_studio/deps/sui/object.ts +16 -0
  30. package/src/_generated/zing_studio/deps/sui/table.ts +38 -0
  31. package/src/_generated/zing_studio/deps/sui/vec_map.ts +39 -0
  32. package/src/_generated/zing_studio/deps/sui/vec_set.ts +28 -0
  33. package/src/_generated/zing_studio/deps/walrus/blob.ts +22 -0
  34. package/src/_generated/zing_studio/deps/walrus/events.ts +124 -0
  35. package/src/_generated/zing_studio/deps/walrus/metadata.ts +137 -0
  36. package/src/_generated/zing_studio/deps/walrus/storage_resource.ts +17 -0
  37. package/src/_generated/zing_studio/deps/zing_framework/derived_object_bag.ts +17 -0
  38. package/src/_generated/zing_studio/deps/zing_framework/derived_table.ts +17 -0
  39. package/src/_generated/zing_studio/donation.ts +18 -0
  40. package/src/_generated/zing_studio/enclave.ts +466 -0
  41. package/src/_generated/zing_studio/nitro_attestation.ts +308 -0
  42. package/src/_generated/zing_studio/referral.ts +447 -0
  43. package/src/_generated/zing_studio/storage.ts +664 -0
  44. package/src/_generated/zing_studio/studio.ts +682 -0
  45. package/src/bcs.ts +139 -0
  46. package/src/client/index.ts +557 -0
  47. package/src/client/types.ts +28 -0
  48. package/src/components/index.ts +3 -0
  49. package/src/components/wallet-provider.tsx +53 -0
  50. package/src/components/walrus-provider.tsx +212 -0
  51. package/src/components/zing-provider.tsx +54 -0
  52. package/src/config/common.ts +82 -0
  53. package/src/config/index.ts +45 -0
  54. package/src/config/mainnet.ts +109 -0
  55. package/src/config/testnet.ts +111 -0
  56. package/src/config/types.ts +69 -0
  57. package/src/const.ts +32 -0
  58. package/src/getters.ts +523 -0
  59. package/src/hooks/index.ts +14 -0
  60. package/src/hooks/useGetStudio.ts +22 -0
  61. package/src/hooks/useGetTierPlan.ts +115 -0
  62. package/src/hooks/useReclaim.ts +165 -0
  63. package/src/hooks/useSignAndExecuteTransaction.ts +57 -0
  64. package/src/hooks/useSignPersonalMessage.ts +28 -0
  65. package/src/hooks/useZingInfiniteQueries.ts +92 -0
  66. package/src/hooks/useZingMutation.ts +66 -0
  67. package/src/hooks/useZingQueries.ts +107 -0
  68. package/src/hooks/useZingQuery.ts +88 -0
  69. package/src/index.ts +15 -0
  70. package/src/lib/utils/colorful-logger.ts +27 -0
  71. package/src/lib/utils/const.ts +25 -0
  72. package/src/lib/utils/helpers.ts +78 -0
  73. package/src/lib/utils/index.ts +5 -0
  74. package/src/lib/utils/init-app-with-shadow.ts +42 -0
  75. package/src/lib/utils/quilt.ts +317 -0
  76. package/src/lib/utils/reclaim.ts +148 -0
  77. package/src/lib/utils/types.ts +14 -0
  78. package/src/mutations/index.ts +3 -0
  79. package/src/mutations/seal.ts +78 -0
  80. package/src/mutations/signer.ts +241 -0
  81. package/src/mutations/walrus.ts +862 -0
  82. package/src/stores/index.ts +3 -0
  83. package/src/stores/walletStore.ts +360 -0
  84. package/src/stores/walrusStore.ts +118 -0
  85. package/src/stores/zkloginStore.ts +53 -0
  86. package/src/types.ts +120 -0
  87. package/tsconfig.json +12 -0
package/src/bcs.ts ADDED
@@ -0,0 +1,139 @@
1
+ import { bcs, TypeTagSerializer } from "@mysten/sui/bcs";
2
+ import {
3
+ fromBase64,
4
+ normalizeStructTag,
5
+ parseStructTag,
6
+ SUI_FRAMEWORK_ADDRESS,
7
+ SUI_SYSTEM_ADDRESS,
8
+ } from "@mysten/sui/utils";
9
+ import { blobIdFromInt, blobIdToInt } from "@mysten/walrus";
10
+ import type { ObjectOwner } from "@mysten/sui/client";
11
+
12
+ function compareBcsBytes(a: Uint8Array, b: Uint8Array) {
13
+ // sort by length first, because bcs bytes prefix length
14
+ if (a.length !== b.length) {
15
+ return a.length - b.length;
16
+ }
17
+
18
+ for (let i = 0; i < a.length; i++) {
19
+ if (a[i] !== b[i]) {
20
+ return a[i] - b[i];
21
+ }
22
+ }
23
+
24
+ return 0;
25
+ }
26
+
27
+ export const BlobId = bcs.u256().transform({
28
+ input: (blobId: string | bigint) =>
29
+ typeof blobId === "string" ? blobIdToInt(blobId) : blobId,
30
+ output: (id: string) => blobIdFromInt(id),
31
+ });
32
+
33
+ export const QuiltPatchId = bcs.struct("QuiltPatchId", {
34
+ quiltId: BlobId,
35
+ patchId: bcs.struct("InternalQuiltPatchId", {
36
+ version: bcs.u8(),
37
+ startIndex: bcs.u16(),
38
+ endIndex: bcs.u16(),
39
+ }),
40
+ });
41
+ export const QuiltPatchTags = bcs.map(bcs.string(), bcs.string()).transform({
42
+ // tags is a BTreeMap, so we need to sort entries before serializing
43
+ input: (tags: Record<string, string> | Map<string, string>) =>
44
+ new Map(
45
+ [...(tags instanceof Map ? tags : Object.entries(tags))].sort(
46
+ ([a], [b]) =>
47
+ compareBcsBytes(
48
+ bcs.string().serialize(a).toBytes(),
49
+ bcs.string().serialize(b).toBytes(),
50
+ ),
51
+ ),
52
+ ),
53
+ output: (tags: Map<string, string>) => Object.fromEntries(tags),
54
+ });
55
+ export const QuiltPatchV1 = bcs.struct("QuiltPatchV1", {
56
+ endIndex: bcs.u16(),
57
+ identifier: bcs.string(),
58
+ tags: QuiltPatchTags,
59
+ });
60
+ export const QuiltIndexV1 = bcs.struct("QuiltIndexV1", {
61
+ patches: bcs.vector(QuiltPatchV1),
62
+ });
63
+
64
+ export const MoveObjectType = bcs.enum("MoveObjectType", {
65
+ Other: bcs.StructTag,
66
+ GasCoin: null,
67
+ StakedSui: null,
68
+ Coin: bcs.TypeTag,
69
+ });
70
+
71
+ export const SuiMoveObject = bcs.struct("SuiMoveObject", {
72
+ data: bcs.enum("Data", {
73
+ MoveObject: bcs.struct("MoveObject", {
74
+ type: MoveObjectType.transform({
75
+ input: (objectType: string): typeof MoveObjectType.$inferType => {
76
+ const structTag = parseStructTag(objectType);
77
+
78
+ if (
79
+ structTag.address === SUI_FRAMEWORK_ADDRESS &&
80
+ structTag.module === "coin" &&
81
+ structTag.name === "Coin" &&
82
+ typeof structTag.typeParams[0] === "object"
83
+ ) {
84
+ const innerStructTag = structTag.typeParams[0];
85
+ if (
86
+ innerStructTag.address === SUI_FRAMEWORK_ADDRESS &&
87
+ innerStructTag.module === "sui" &&
88
+ innerStructTag.name === "SUI"
89
+ ) {
90
+ return { GasCoin: true, $kind: "GasCoin" };
91
+ }
92
+ return { Coin: normalizeStructTag(innerStructTag), $kind: "Coin" };
93
+ } else if (
94
+ structTag.address === SUI_SYSTEM_ADDRESS &&
95
+ structTag.module === "staking_pool" &&
96
+ structTag.name === "StakedSui"
97
+ ) {
98
+ return { StakedSui: true, $kind: "StakedSui" };
99
+ }
100
+ return {
101
+ Other: {
102
+ ...structTag,
103
+ typeParams: structTag.typeParams.map((typeParam) =>
104
+ TypeTagSerializer.parseFromStr(normalizeStructTag(typeParam)),
105
+ ),
106
+ },
107
+ $kind: "Other",
108
+ };
109
+ },
110
+ }),
111
+ hasPublicTransfer: bcs.bool(),
112
+ version: bcs.u64(),
113
+ contents: bcs.byteVector().transform({ input: fromBase64 }),
114
+ }),
115
+ }),
116
+ owner: bcs.Owner.transform({
117
+ input: (objectOwner: ObjectOwner) => {
118
+ if (objectOwner === "Immutable") {
119
+ return { Immutable: null };
120
+ } else if ("Shared" in objectOwner) {
121
+ return {
122
+ Shared: {
123
+ initialSharedVersion: objectOwner.Shared.initial_shared_version,
124
+ },
125
+ };
126
+ } else if ("ConsensusAddressOwner" in objectOwner) {
127
+ return {
128
+ ConsensusAddressOwner: {
129
+ owner: objectOwner.ConsensusAddressOwner.owner,
130
+ startVersion: objectOwner.ConsensusAddressOwner.start_version,
131
+ },
132
+ };
133
+ }
134
+ return objectOwner;
135
+ },
136
+ }),
137
+ previousTransaction: bcs.ObjectDigest,
138
+ storageRebate: bcs.u64(),
139
+ });
@@ -0,0 +1,557 @@
1
+ import { burnOrSendPositiveCoin } from "../_generated/zing_framework/coin_utils.js";
2
+ import {
3
+ deleteArticle,
4
+ donate,
5
+ purchaseStorageTier,
6
+ setMonthlySubscriptionFee,
7
+ subscribeToStudio,
8
+ updateArticleSubscriptionLevel,
9
+ } from "../_generated/zing_studio/app.js";
10
+ import { addReferralToStudio } from "../_generated/zing_studio/referral.js";
11
+ import { shareStorageSpace } from "../_generated/zing_studio/storage.js";
12
+ import {
13
+ _new as newStudio,
14
+ sealApproveStudioOwner,
15
+ shareStudio,
16
+ } from "../_generated/zing_studio/studio.js";
17
+ import { getConfig } from "../config/index.js";
18
+ import {
19
+ getDerivedMembershipID,
20
+ getDerivedStorageID,
21
+ getDerivedStudioID,
22
+ getDerivedWorksID,
23
+ getIdentifierByAddress,
24
+ getIdentifierInfoByIdentifier,
25
+ getMember,
26
+ getMembers,
27
+ getStorageSpace,
28
+ getStorageTreasury,
29
+ getStudio,
30
+ getWork,
31
+ getWorks,
32
+ } from "../getters.js";
33
+ import { importFileKey } from "../mutations/seal.js";
34
+ import { signPersonalMessage } from "../mutations/signer.js";
35
+ import { SessionKey } from "@mysten/seal";
36
+ import { Transaction, coinWithBalance } from "@mysten/sui/transactions";
37
+ import { fromHex } from "@mysten/sui/utils";
38
+ import type {
39
+ PurchaseStorageTierParams,
40
+ SetMonthlySubscriptionFeeParams,
41
+ SubscribeParams,
42
+ } from "./types.js";
43
+ import type { CoinSymbol, PlatformType, WorkType } from "../config/index.js";
44
+ import type { Config, WalrusSystemState } from "../config/types.js";
45
+ import type { SuiNetwork, ZKLoginSignerState } from "../types.js";
46
+ import type { SealClient } from "@mysten/seal";
47
+ import type { SuiGrpcClient } from "@mysten/sui/grpc";
48
+ import type { SuiJsonRpcClient } from "@mysten/sui/jsonRpc";
49
+
50
+ interface Params {
51
+ network: SuiNetwork;
52
+ suiGrpcClient: SuiGrpcClient;
53
+ suiJsonRpcClient: SuiJsonRpcClient;
54
+ gasPoolBearerToken: string;
55
+ }
56
+
57
+ export class ZingClient {
58
+ suiGrpcClient: SuiGrpcClient;
59
+ suiJsonRpcClient: SuiJsonRpcClient;
60
+ network: SuiNetwork;
61
+ config: Config;
62
+ gasPoolBearerToken: string;
63
+
64
+ constructor({
65
+ network,
66
+ suiGrpcClient,
67
+ suiJsonRpcClient,
68
+ gasPoolBearerToken,
69
+ }: Params) {
70
+ this.suiGrpcClient = suiGrpcClient;
71
+ this.suiJsonRpcClient = suiJsonRpcClient;
72
+ this.network = network || "testnet";
73
+ this.config = getConfig(network);
74
+ this.gasPoolBearerToken = gasPoolBearerToken;
75
+ }
76
+
77
+ public getConfig() {
78
+ return this.config;
79
+ }
80
+
81
+ // Getters
82
+ getDerivedStudioID(address: string): string {
83
+ return getDerivedStudioID(
84
+ this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF.objectId,
85
+ address,
86
+ );
87
+ }
88
+
89
+ getDerivedStorageID(address: string) {
90
+ return getDerivedStorageID(
91
+ this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF.objectId,
92
+ address,
93
+ );
94
+ }
95
+
96
+ getDerivedWorksID(address: string) {
97
+ return getDerivedWorksID(
98
+ this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF.objectId,
99
+ address,
100
+ );
101
+ }
102
+
103
+ getDerivedMembershipID(address: string) {
104
+ return getDerivedMembershipID(
105
+ this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF.objectId,
106
+ address,
107
+ );
108
+ }
109
+
110
+ async getIdentifierByAddress(platform: PlatformType, address: string) {
111
+ return getIdentifierByAddress(
112
+ this.suiGrpcClient,
113
+ this.config.zing.IDENTITY_PLATFORMS[platform]
114
+ .IDENTITY_MANAGER_ADDRESS_TO_IDENTIFIER_DF_ID,
115
+ address,
116
+ );
117
+ }
118
+
119
+ async getIdentifierInfoByIdentifier(
120
+ platform: PlatformType,
121
+ identifier: string,
122
+ ) {
123
+ return getIdentifierInfoByIdentifier(
124
+ this.suiGrpcClient,
125
+ this.config.zing.IDENTITY_PLATFORMS[platform]
126
+ .IDENTITY_MANAGER_IDENTIFIER_INFO_DF_ID,
127
+ identifier,
128
+ );
129
+ }
130
+
131
+ // async getDonationByIdentifier(identifier: string) {
132
+ // const donations = await getDonationByIdentifier(
133
+ // this.suiGrpcClient,
134
+ // this.config.zing.DONATIONS_TABLE_DF_ID,
135
+ // identifier,
136
+ // );
137
+ //
138
+ // return Object.keys(this.config.coins).reduce(
139
+ // (acc, coinSymbol) => {
140
+ // const symbol = coinSymbol as CoinSymbol;
141
+ // const donation = donations?.contents.find(
142
+ // (c) => c.key.name === this.config.coins[symbol],
143
+ // );
144
+ // if (donation) acc[symbol] = donation.value;
145
+ // return acc;
146
+ // },
147
+ // {} as Record<CoinSymbol, string>,
148
+ // );
149
+ // }
150
+
151
+ async getStudio(owner: string) {
152
+ return getStudio(this.suiGrpcClient, this.getDerivedStudioID(owner));
153
+ }
154
+
155
+ async getStorageSpace(owner: string) {
156
+ return getStorageSpace(this.suiGrpcClient, this.getDerivedStorageID(owner));
157
+ }
158
+
159
+ async getStorageTreasury() {
160
+ return getStorageTreasury(
161
+ this.suiGrpcClient,
162
+ this.config.zing.ZING_STORAGE_TREASURY_SHARED_OBJECT_REF.objectId,
163
+ );
164
+ }
165
+
166
+ async getMember(owner: string, member: string) {
167
+ return getMember(
168
+ this.suiGrpcClient,
169
+ this.getDerivedMembershipID(owner),
170
+ member,
171
+ );
172
+ }
173
+
174
+ async getMembers(owner: string, cursor?: string, limit = 20) {
175
+ return getMembers(
176
+ this.suiGrpcClient,
177
+ this.getDerivedMembershipID(owner),
178
+ cursor,
179
+ limit,
180
+ );
181
+ }
182
+
183
+ async getWork(
184
+ walrusSystem: WalrusSystemState,
185
+ workId: string,
186
+ workType?: WorkType,
187
+ ) {
188
+ return getWork(
189
+ this.suiGrpcClient,
190
+ walrusSystem,
191
+ workId,
192
+ workType ? this.config.zing.ZING_WORKS_TYPE[workType] : undefined,
193
+ );
194
+ }
195
+
196
+ async getWorks(
197
+ walrusSystem: WalrusSystemState,
198
+ owner: string,
199
+ workType?: WorkType,
200
+ cursor?: string,
201
+ limit = 20,
202
+ ) {
203
+ return getWorks(
204
+ this.suiGrpcClient,
205
+ walrusSystem,
206
+ this.getDerivedWorksID(owner),
207
+ workType ? this.config.zing.ZING_WORKS_TYPE[workType] : undefined,
208
+ cursor,
209
+ limit,
210
+ );
211
+ }
212
+
213
+ async getBalance(owner: string, coinSymbol: CoinSymbol) {
214
+ return this.suiGrpcClient.stateService
215
+ .getBalance({
216
+ owner,
217
+ coinType: this.config.coins[coinSymbol],
218
+ })
219
+ .then((res) => res.response);
220
+ }
221
+
222
+ async getSessionKey(signer: ZKLoginSignerState, owner: string) {
223
+ const { suiAddress, ephemeralKeyPair, decodedJwt, zkProof, maxEpoch } =
224
+ signer;
225
+ if (
226
+ !suiAddress ||
227
+ !ephemeralKeyPair ||
228
+ !decodedJwt ||
229
+ !zkProof ||
230
+ !maxEpoch
231
+ ) {
232
+ throw new Error("Missing required wallet data for session key creation");
233
+ }
234
+
235
+ const sessionKey = await SessionKey.create({
236
+ address: owner,
237
+ packageId: this.config.zing.ZING_STUDIO_V0_PACKAGE_ADDRESS,
238
+ ttlMin: 10,
239
+ suiClient: this.suiJsonRpcClient,
240
+ });
241
+ const message = sessionKey.getPersonalMessage();
242
+ const { zkLoginSignature } = await signPersonalMessage(signer, message);
243
+
244
+ sessionKey.setPersonalMessageSignature(zkLoginSignature);
245
+
246
+ return sessionKey;
247
+ }
248
+
249
+ async buildSealApproveTransactionBytes(owner: string) {
250
+ const tx = new Transaction();
251
+ // codegen didn't generate interface for entry fun
252
+ const studioId = this.getDerivedStudioID(owner);
253
+
254
+ tx.add(
255
+ sealApproveStudioOwner({
256
+ package: this.config.zing.ZING_STUDIO_PACKAGE_ADDRESS,
257
+ arguments: {
258
+ id: tx.pure.vector("u8", fromHex(studioId)),
259
+ config: tx.sharedObjectRef(
260
+ this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF,
261
+ ),
262
+ self: tx.object(studioId),
263
+ },
264
+ }),
265
+ );
266
+
267
+ return tx.build({
268
+ client: this.suiJsonRpcClient,
269
+ onlyTransactionKind: true,
270
+ });
271
+ }
272
+
273
+ async getFileKey(
274
+ sealClient: SealClient,
275
+ sessionKey: SessionKey,
276
+ owner: string,
277
+ encryptedFileKeyBytes: number[],
278
+ ) {
279
+ const txBytes = await this.buildSealApproveTransactionBytes(owner);
280
+ const decryptedKey = await sealClient.decrypt({
281
+ data: new Uint8Array(encryptedFileKeyBytes),
282
+ sessionKey,
283
+ txBytes,
284
+ });
285
+ return await importFileKey(decryptedKey);
286
+ }
287
+
288
+ // --- Transaction Builders ---
289
+
290
+ /**
291
+ * Constructs the transaction for subscribing to a studio.
292
+ * Does not execute; returns the Transaction object to be signed.
293
+ */
294
+ public subscribeToStudioTransaction({
295
+ sender,
296
+ creatorAddress,
297
+ subscriptionAmount,
298
+ level,
299
+ }: SubscribeParams) {
300
+ return (tx: Transaction) => {
301
+ const usdcCoin = tx.add(
302
+ coinWithBalance({
303
+ type: this.config.coins.USDC,
304
+ balance: subscriptionAmount,
305
+ useGasCoin: false,
306
+ }),
307
+ );
308
+
309
+ const studioId = this.getDerivedStudioID(creatorAddress);
310
+
311
+ tx.add(
312
+ subscribeToStudio({
313
+ package: this.config.zing.ZING_STUDIO_PACKAGE_ADDRESS,
314
+ typeArguments: [this.config.coins.USDC],
315
+ arguments: {
316
+ studio: tx.object(studioId),
317
+ treasury: tx.sharedObjectRef(
318
+ this.config.zing.ZING_TREASURY_SHARED_OBJECT_REF,
319
+ ),
320
+ subscriptionLevel: tx.pure.u8(level),
321
+ payment: usdcCoin,
322
+ config: tx.sharedObjectRef(
323
+ this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF,
324
+ ),
325
+ },
326
+ }),
327
+ );
328
+
329
+ tx.add(
330
+ burnOrSendPositiveCoin({
331
+ package: this.config.zing.ZING_FRAMEWORK_PACKAGE_ADDRESS,
332
+ typeArguments: [this.config.coins.USDC],
333
+ arguments: {
334
+ coin: usdcCoin,
335
+ recipient: tx.pure.address(sender),
336
+ },
337
+ }),
338
+ );
339
+ };
340
+ }
341
+
342
+ public purchaseStorageTierTransaction({
343
+ sender,
344
+ objs,
345
+ newTierIdx,
346
+ balance,
347
+ referrer,
348
+ }: PurchaseStorageTierParams) {
349
+ return (tx: Transaction) => {
350
+ const [studio, storageSpace] =
351
+ objs === null
352
+ ? tx.add(
353
+ newStudio({
354
+ package: this.config.zing.ZING_STUDIO_PACKAGE_ADDRESS,
355
+ arguments: [
356
+ tx.sharedObjectRef({
357
+ ...this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF,
358
+ mutable: true,
359
+ }), // mutable for new derived Object
360
+ ],
361
+ }),
362
+ )
363
+ : [tx.object(objs.studioId), tx.object(objs.storageSpaceId)];
364
+
365
+ if (objs == null && referrer !== null && sender !== referrer) {
366
+ tx.add(
367
+ addReferralToStudio({
368
+ package: this.config.zing.ZING_STUDIO_PACKAGE_ADDRESS,
369
+ arguments: {
370
+ studio,
371
+ config: tx.sharedObjectRef(
372
+ this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF,
373
+ ),
374
+ referrer,
375
+ },
376
+ }),
377
+ );
378
+ }
379
+
380
+ const usdcCoin = tx.add(
381
+ coinWithBalance({
382
+ type: this.config.coins.USDC,
383
+ balance,
384
+ useGasCoin: false,
385
+ }),
386
+ );
387
+
388
+ tx.add(
389
+ purchaseStorageTier({
390
+ package: this.config.zing.ZING_STUDIO_PACKAGE_ADDRESS,
391
+ typeArguments: [this.config.coins.USDC],
392
+ arguments: {
393
+ config: tx.sharedObjectRef(
394
+ this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF,
395
+ ),
396
+ studio,
397
+ treasury: tx.sharedObjectRef(
398
+ this.config.zing.ZING_TREASURY_SHARED_OBJECT_REF,
399
+ ),
400
+ walrusSystem: tx.sharedObjectRef(
401
+ this.config.walrus.WALRUS_SYSTEM_SHARED_OBJECT_REF,
402
+ ),
403
+ storageTreasury: tx.sharedObjectRef(
404
+ this.config.zing.ZING_STORAGE_TREASURY_SHARED_OBJECT_REF,
405
+ ),
406
+ storageSpace,
407
+ newTierIdx: tx.pure.u8(newTierIdx),
408
+ payment: usdcCoin,
409
+ },
410
+ }),
411
+ );
412
+
413
+ if (objs === null) {
414
+ tx.add(
415
+ shareStudio({
416
+ package: this.config.zing.ZING_STUDIO_PACKAGE_ADDRESS,
417
+ arguments: [studio],
418
+ }),
419
+ );
420
+
421
+ tx.add(
422
+ shareStorageSpace({
423
+ package: this.config.zing.ZING_STUDIO_PACKAGE_ADDRESS,
424
+ arguments: [storageSpace],
425
+ }),
426
+ );
427
+ }
428
+
429
+ tx.add(
430
+ burnOrSendPositiveCoin({
431
+ package: this.config.zing.ZING_FRAMEWORK_PACKAGE_ADDRESS,
432
+ typeArguments: [this.config.coins.USDC],
433
+ arguments: {
434
+ coin: usdcCoin,
435
+ recipient: tx.pure.address(sender),
436
+ },
437
+ }),
438
+ );
439
+ };
440
+ }
441
+
442
+ public setMonthlySubscriptionFeeTransaction({
443
+ sender,
444
+ monthlyFee,
445
+ level,
446
+ }: SetMonthlySubscriptionFeeParams) {
447
+ return (tx: Transaction) => {
448
+ tx.add(
449
+ setMonthlySubscriptionFee({
450
+ package: this.config.zing.ZING_STUDIO_PACKAGE_ADDRESS,
451
+ arguments: {
452
+ config: tx.sharedObjectRef(
453
+ this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF,
454
+ ),
455
+ studio: tx.object(this.getDerivedStudioID(sender)),
456
+ level: tx.pure.u8(level),
457
+ monthlySubscriptionFee: tx.pure.u64(monthlyFee),
458
+ },
459
+ }),
460
+ );
461
+ };
462
+ }
463
+
464
+ public updateArticleSubscriptionLevelTransaction({
465
+ sender,
466
+ articleId,
467
+ subscriptionLevel,
468
+ }: {
469
+ sender: string;
470
+ articleId: string;
471
+ subscriptionLevel: null | number;
472
+ }) {
473
+ return (tx: Transaction) => {
474
+ tx.add(
475
+ updateArticleSubscriptionLevel({
476
+ package: this.config.zing.ZING_STUDIO_PACKAGE_ADDRESS,
477
+ arguments: {
478
+ config: tx.sharedObjectRef(
479
+ this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF,
480
+ ),
481
+ studio: tx.object(this.getDerivedStudioID(sender)),
482
+ walrusSystem: tx.sharedObjectRef(
483
+ this.config.walrus.WALRUS_SYSTEM_SHARED_OBJECT_REF,
484
+ ),
485
+ articleId: tx.pure.id(articleId),
486
+ subscriptionLevel: tx.pure.option("u8", subscriptionLevel),
487
+ },
488
+ }),
489
+ );
490
+ };
491
+ }
492
+
493
+ public deleteArticleTransaction({
494
+ sender,
495
+ articleId,
496
+ }: {
497
+ sender: string;
498
+ articleId: string;
499
+ }) {
500
+ return (tx: Transaction) => {
501
+ tx.add(
502
+ deleteArticle({
503
+ package: this.config.zing.ZING_STUDIO_PACKAGE_ADDRESS,
504
+ arguments: {
505
+ config: tx.sharedObjectRef(
506
+ this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF,
507
+ ),
508
+ studio: tx.object(this.getDerivedStudioID(sender)),
509
+ storageSpace: tx.object(this.getDerivedStorageID(sender)),
510
+ walrusSystem: tx.sharedObjectRef(
511
+ this.config.walrus.WALRUS_SYSTEM_SHARED_OBJECT_REF,
512
+ ),
513
+ articleId: tx.pure.id(articleId),
514
+ },
515
+ }),
516
+ );
517
+ };
518
+ }
519
+
520
+ public donateTransaction({
521
+ recipient,
522
+ message,
523
+ value,
524
+ }: {
525
+ recipient: string;
526
+ message: string;
527
+ value: number;
528
+ }) {
529
+ return (tx: Transaction) => {
530
+ const usdcCoin = tx.add(
531
+ coinWithBalance({
532
+ type: this.config.coins.USDC,
533
+ balance: value,
534
+ useGasCoin: false,
535
+ }),
536
+ );
537
+
538
+ tx.add(
539
+ donate({
540
+ package: this.config.zing.ZING_STUDIO_PACKAGE_ADDRESS,
541
+ typeArguments: [this.config.coins.USDC],
542
+ arguments: {
543
+ config: tx.sharedObjectRef(
544
+ this.config.zing.ZING_STUDIO_CONFIG_SHARED_OBJECT_REF,
545
+ ),
546
+ studio: tx.object(this.getDerivedStudioID(recipient)),
547
+ treasury: tx.sharedObjectRef(
548
+ this.config.zing.ZING_TREASURY_SHARED_OBJECT_REF,
549
+ ),
550
+ message: tx.pure.string(message),
551
+ donation: usdcCoin,
552
+ },
553
+ }),
554
+ );
555
+ };
556
+ }
557
+ }
@@ -0,0 +1,28 @@
1
+ import type { Transaction } from "@mysten/sui/transactions";
2
+
3
+ export type TransactionBuilderFn<TParams> = (
4
+ tx: Transaction,
5
+ params: TParams,
6
+ ) => void;
7
+ export interface SubscribeParams {
8
+ sender: string;
9
+ creatorAddress: string;
10
+ subscriptionAmount: number;
11
+ level: number;
12
+ }
13
+ export interface PurchaseStorageTierParams {
14
+ sender: string;
15
+ objs: null | {
16
+ studioId: string;
17
+ storageSpaceId: string;
18
+ };
19
+ newTierIdx: number;
20
+ referrer: string | null;
21
+ balance: number;
22
+ }
23
+
24
+ export interface SetMonthlySubscriptionFeeParams {
25
+ sender: string;
26
+ monthlyFee: number;
27
+ level: number;
28
+ }