@zoralabs/protocol-sdk 0.11.3-COMMENTS.1 → 0.11.3

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.
@@ -1,223 +0,0 @@
1
- import { describe, expect } from "vitest";
2
- import { forkUrls, makeAnvilTest, writeContractWithRetries } from "src/anvil";
3
- import { base, zoraSepolia } from "viem/chains";
4
- import {
5
- commentsABI,
6
- commentsAddress,
7
- emptyCommentIdentifier,
8
- PermitComment,
9
- PermitSparkComment,
10
- sparkValue,
11
- CommentIdentifier,
12
- permitSparkCommentTypedDataDefinition,
13
- } from "@zoralabs/protocol-deployments";
14
- import {
15
- Address,
16
- keccak256,
17
- toBytes,
18
- zeroAddress,
19
- parseEther,
20
- TransactionReceipt,
21
- parseEventLogs,
22
- } from "viem";
23
- import { createCreatorClient } from "src/sdk";
24
- import { randomNewContract, waitForSuccess } from "src/test-utils";
25
- import { demoTokenMetadataURI } from "src/create/1155-create-helper.test";
26
- import { permitCommentTypedDataDefinition } from "@zoralabs/protocol-deployments";
27
-
28
- const getCommentIdentifierFromReceipt = (
29
- receipt: TransactionReceipt,
30
- ): CommentIdentifier => {
31
- const logs = parseEventLogs({
32
- abi: commentsABI,
33
- logs: receipt.logs,
34
- eventName: "Commented",
35
- });
36
-
37
- if (logs.length === 0) {
38
- throw new Error("No Commented event found in receipt");
39
- }
40
-
41
- return logs[0]!.args.comment;
42
- };
43
-
44
- // todo: move this to protocol-deployments
45
- describe("comments", () => {
46
- makeAnvilTest({
47
- forkUrl: forkUrls.zoraSepolia,
48
- forkBlockNumber: 14918747,
49
- anvilChainId: zoraSepolia.id,
50
- })(
51
- "can sign and execute a cross-chain comment, and sign and execute a cross-chain spark comment",
52
- async ({
53
- viemClients: { publicClient, walletClient, chain, testClient },
54
- }) => {
55
- // Get the chain ID and set up addresses for different roles
56
- const chainId = chain.id;
57
- const [
58
- collectorAddress,
59
- creatorAddress,
60
- executorAddress,
61
- sparkerAddress,
62
- ] = (await walletClient.getAddresses()!) as [
63
- Address,
64
- Address,
65
- Address,
66
- Address,
67
- ];
68
-
69
- // Create a creator client for interacting with the protocol
70
- const creatorClient = createCreatorClient({
71
- chainId,
72
- publicClient,
73
- });
74
-
75
- // Step 1: Create a new 1155 contract and token
76
- const { contractAddress, newTokenId, parameters, prepareMint } =
77
- await creatorClient.create1155({
78
- contract: randomNewContract(),
79
- token: {
80
- tokenMetadataURI: demoTokenMetadataURI,
81
- },
82
- account: creatorAddress,
83
- });
84
-
85
- // Deploy the new contract
86
- const { request } = await publicClient.simulateContract(parameters);
87
- await writeContractWithRetries({
88
- request,
89
- walletClient,
90
- publicClient,
91
- });
92
-
93
- // Prepare to mint a token
94
- const { parameters: collectParameters } = await prepareMint({
95
- quantityToMint: 1n,
96
- minterAccount: collectorAddress,
97
- });
98
-
99
- // Step 2: Mint an 1155 token on the new contract
100
- const { request: mintRequest } =
101
- await publicClient.simulateContract(collectParameters);
102
- await writeContractWithRetries({
103
- request: mintRequest,
104
- walletClient,
105
- publicClient,
106
- });
107
-
108
- // Set up cross-chain comment parameters
109
- const sourceChainId = base.id; // The chain ID where the comment originates
110
- const randomNonce = () =>
111
- keccak256(toBytes(Math.round(Math.random() * 1000)));
112
- const thirtySecondsFromNow =
113
- BigInt(Math.round(new Date().getTime() / 1000)) + 30n;
114
-
115
- // Step 3: Prepare a cross-chain comment
116
- const permitComment: PermitComment = {
117
- sourceChainId,
118
- contractAddress,
119
- destinationChainId: chainId,
120
- tokenId: newTokenId,
121
- commenter: collectorAddress,
122
- text: "hello world",
123
- deadline: thirtySecondsFromNow,
124
- nonce: randomNonce(),
125
- referrer: zeroAddress,
126
- replyTo: emptyCommentIdentifier(),
127
- };
128
-
129
- // Generate typed data for signing
130
- const typedData = permitCommentTypedDataDefinition(permitComment);
131
-
132
- const commentsAddressForChainId =
133
- commentsAddress[chainId as keyof typeof commentsAddress];
134
-
135
- // Verify the typed data is correctly set up
136
- expect(typedData.domain!.chainId).toEqual(sourceChainId);
137
- expect(typedData.account).toEqual(collectorAddress);
138
- expect(typedData.domain!.verifyingContract).toEqual(
139
- commentsAddressForChainId,
140
- );
141
-
142
- // Ensure the commenter has enough balance for the spark value
143
- await testClient.setBalance({
144
- address: collectorAddress,
145
- value: sparkValue(),
146
- });
147
-
148
- // Step 4: Sign the cross-chain comment
149
- const signature = await walletClient.signTypedData(typedData);
150
-
151
- // Ensure the executor has enough balance to execute the transaction
152
- await testClient.setBalance({
153
- address: executorAddress,
154
- value: parseEther("1"),
155
- });
156
-
157
- // Step 5: Simulate and execute the cross-chain comment
158
- const { request: commentRequest } = await publicClient.simulateContract({
159
- abi: commentsABI,
160
- address: commentsAddressForChainId,
161
- functionName: "permitComment",
162
- args: [permitComment, signature],
163
- account: executorAddress,
164
- value: sparkValue(),
165
- });
166
-
167
- const commentHash = await walletClient.writeContract(commentRequest);
168
- const receipt = await publicClient.waitForTransactionReceipt({
169
- hash: commentHash,
170
- });
171
-
172
- if (receipt.status !== "success") {
173
- expect(receipt.status).toBe("success");
174
- }
175
-
176
- // Extract the comment identifier from the transaction receipt
177
- const commentIdentifier = getCommentIdentifierFromReceipt(receipt);
178
-
179
- // Step 6: Prepare a spark (like) for the comment
180
- const sparkComment: PermitSparkComment = {
181
- sparksQuantity: 3n,
182
- sparker: sparkerAddress,
183
- deadline: thirtySecondsFromNow,
184
- nonce: randomNonce(),
185
- referrer: zeroAddress,
186
- sourceChainId,
187
- destinationChainId: chainId,
188
- comment: commentIdentifier,
189
- };
190
-
191
- const sparkTypedData =
192
- permitSparkCommentTypedDataDefinition(sparkComment);
193
-
194
- // Step 7: Sign the spark comment
195
- const sparkSignature = await walletClient.signTypedData(sparkTypedData);
196
-
197
- // Step 8: Simulate and execute the spark comment
198
- const { request: sparkRequest } = await publicClient.simulateContract({
199
- abi: commentsABI,
200
- address: commentsAddressForChainId,
201
- functionName: "permitSparkComment",
202
- args: [sparkComment, sparkSignature],
203
- account: executorAddress,
204
- value: sparkValue() * sparkComment.sparksQuantity,
205
- });
206
-
207
- const sparkHash = await walletClient.writeContract(sparkRequest);
208
-
209
- await waitForSuccess(sparkHash, publicClient);
210
-
211
- // Step 9: Verify the spark count
212
- const sparkCount = await publicClient.readContract({
213
- abi: commentsABI,
214
- address: commentsAddressForChainId,
215
- functionName: "commentSparksQuantity",
216
- args: [commentIdentifier],
217
- });
218
-
219
- expect(sparkCount).toEqual(sparkComment.sparksQuantity);
220
- },
221
- 20_000,
222
- );
223
- });