@zoralabs/protocol-sdk 0.7.3 → 0.7.5

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 (50) hide show
  1. package/.turbo/turbo-build.log +18 -16
  2. package/CHANGELOG.md +17 -0
  3. package/LICENSE +21 -0
  4. package/dist/anvil.d.ts +2 -2
  5. package/dist/anvil.d.ts.map +1 -1
  6. package/dist/apis/http-api-base.d.ts.map +1 -1
  7. package/dist/index.cjs +2272 -361
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +2 -3
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +2297 -362
  12. package/dist/index.js.map +1 -1
  13. package/dist/ipfs/token-metadata.d.ts.map +1 -1
  14. package/dist/mint/mint-client.d.ts +1 -1
  15. package/dist/mint/mint-queries.d.ts +1 -1
  16. package/dist/mint/types.d.ts.map +1 -1
  17. package/dist/premint/conversions.d.ts +5 -5
  18. package/dist/premint/conversions.d.ts.map +1 -1
  19. package/dist/premint/premint-api-client.d.ts +1 -1
  20. package/dist/premint/premint-api-client.d.ts.map +1 -1
  21. package/dist/premint/premint-client.d.ts +4 -4
  22. package/dist/premint/premint-client.d.ts.map +1 -1
  23. package/dist/premint/preminter.d.ts +3 -3
  24. package/dist/premint/preminter.d.ts.map +1 -1
  25. package/dist/preminter.d.ts +2 -2
  26. package/dist/preminter.d.ts.map +1 -1
  27. package/dist/sparks/mints-queries.d.ts.map +1 -0
  28. package/dist/{mints/mints-contracts.d.ts → sparks/sparks-contracts.d.ts} +4 -3
  29. package/dist/sparks/sparks-contracts.d.ts.map +1 -0
  30. package/dist/test-utils.d.ts.map +1 -1
  31. package/package.json +17 -16
  32. package/src/index.ts +2 -4
  33. package/src/premint/preminter.test.ts +1 -1
  34. package/src/{mints/mints-contracts.test.ts → sparks/sparks-contracts-test.ts} +64 -57
  35. package/src/{mints/mints-contracts.ts → sparks/sparks-contracts.ts} +7 -3
  36. package/src/sparks/sparks-sponsored-sparks-spender.test.ts +183 -0
  37. package/tsconfig.build.json +2 -2
  38. package/tsconfig.json +1 -1
  39. package/dist/mints/mints-contracts.d.ts.map +0 -1
  40. package/dist/mints/mints-eth-unwrapper-and-caller.d.ts +0 -55
  41. package/dist/mints/mints-eth-unwrapper-and-caller.d.ts.map +0 -1
  42. package/dist/mints/mints-queries.d.ts.map +0 -1
  43. package/dist/mints/mints-relay-example.d.ts +0 -60
  44. package/dist/mints/mints-relay-example.d.ts.map +0 -1
  45. package/src/mints/mints-eth-unwrapper-and-caller.test.ts +0 -467
  46. package/src/mints/mints-eth-unwrapper-and-caller.ts +0 -83
  47. package/src/mints/mints-relay-example.ts +0 -322
  48. /package/dist/{mints → sparks}/mints-queries.d.ts +0 -0
  49. /package/src/{mints → sparks}/mints-queries.test.ts +0 -0
  50. /package/src/{mints → sparks}/mints-queries.ts +0 -0
@@ -1,322 +0,0 @@
1
- import {
2
- Address,
3
- Hex,
4
- PublicClient,
5
- SignTypedDataParameters,
6
- TypedData,
7
- WalletClient,
8
- recoverTypedDataAddress,
9
- } from "viem";
10
- import { paths, TransactionStepItem } from "@reservoir0x/relay-sdk";
11
- import { PermitSafeTransferBatch } from "./mints-contracts";
12
- import {
13
- mintsEthUnwrapperAndCallerABI,
14
- mintsEthUnwrapperAndCallerAddress,
15
- } from "@zoralabs/protocol-deployments";
16
- import { makeCallWithEthSafeTransferData } from "./mints-eth-unwrapper-and-caller";
17
-
18
- type RelayCallBody =
19
- paths["/execute/call"]["post"]["requestBody"]["content"]["application/json"];
20
- type RelayCallResponse =
21
- paths["/execute/call"]["post"]["responses"]["200"]["content"]["application/json"];
22
-
23
- const postToRelay = async ({
24
- data,
25
- }: {
26
- data: RelayCallBody;
27
- }): Promise<RelayCallResponse> => {
28
- const request = {
29
- url: "https://api.relay.link/execute/call",
30
- method: "post",
31
- data,
32
- };
33
-
34
- const response = await fetch(request.url, {
35
- method: "POST",
36
- body: JSON.stringify(request.data),
37
- headers: { "content-type": "application/json" },
38
- });
39
-
40
- if (response.ok) {
41
- const responseJson = await response.json();
42
- return responseJson as RelayCallResponse;
43
- } else {
44
- throw new Error("Bad response from relay");
45
- }
46
- };
47
-
48
- export const getRelayCall = async ({
49
- tx,
50
- depositingAccount,
51
- originChainId,
52
- toChainId,
53
- }: {
54
- tx: {
55
- to: Address;
56
- value: bigint;
57
- data: Hex;
58
- };
59
- depositingAccount: Address;
60
- originChainId: number;
61
- toChainId: number;
62
- }) => {
63
- const data: RelayCallBody = {
64
- user: depositingAccount,
65
- txs: [
66
- {
67
- to: tx.to,
68
- value: tx.value.toString(),
69
- data: tx.data,
70
- },
71
- ],
72
- originChainId: originChainId,
73
- destinationChainId: toChainId,
74
- };
75
-
76
- const response = await postToRelay({
77
- data,
78
- });
79
-
80
- if (response.steps!.length !== 1) {
81
- throw new Error("should only be a single step.");
82
- }
83
-
84
- const step = response.steps![0]!;
85
-
86
- if (step.items!.length !== 1) {
87
- throw new Error("should only be a single item.");
88
- }
89
-
90
- const stepItem = step.items![0]! as TransactionStepItem;
91
-
92
- const relayCall = stepItem.data!;
93
-
94
- // compute relay fee by subtracting cross-chain call value,
95
- // from value to send to relay
96
- const relayFee = BigInt(relayCall.value) - tx.value;
97
-
98
- return {
99
- relayCall,
100
- relayFee,
101
- };
102
- };
103
-
104
- // call relay to get the relay call data
105
- // then sign a message with an executing account
106
- // signaling intent to pay the fee later, with a 30 second deadline
107
- export const makeAndSignSponsoredRelayCall = async ({
108
- originChainId,
109
- toChainId,
110
- tx,
111
- executingAccount,
112
- walletClient,
113
- }: {
114
- // the chain to call from (current chain)
115
- originChainId: keyof typeof mintsEthUnwrapperAndCallerAddress;
116
- // the chain that the call will be executed on
117
- toChainId: number;
118
- // the tx to call on the other chain:
119
- tx: {
120
- to: Address;
121
- value: bigint;
122
- data: Hex;
123
- };
124
- executingAccount: Address;
125
- walletClient: WalletClient;
126
- }) => {
127
- // call relay to get the cross-chain calldata
128
- const { relayCall: relayCall, relayFee: relayFee } = await getRelayCall({
129
- originChainId,
130
- toChainId,
131
- tx,
132
- depositingAccount: mintsEthUnwrapperAndCallerAddress[originChainId],
133
- });
134
-
135
- // build call to forward the relay call to the other chain that will be
136
- // set as the `safeTransferData` in the permit
137
- const safeTransferData = makeCallWithEthSafeTransferData({
138
- address: relayCall.to,
139
- call: relayCall.data,
140
- value: BigInt(relayCall.value),
141
- });
142
-
143
- const deadline = BigInt(new Date().getTime() + 30 * 1000);
144
-
145
- // build and sign a message indicating intent to execute this call
146
- // and pay the relay fee
147
- const typedData = permitWithAdditionalValueTypedDataDefinition({
148
- additionalValueToSend: relayFee,
149
- chainId: originChainId,
150
- // make a deadline 30 seconds from now:
151
- deadline,
152
- safeTransferData,
153
- });
154
-
155
- // have the account that is to execute the transaction later
156
- // sign the typed data
157
- const signature = await walletClient.signTypedData({
158
- ...typedData,
159
- account: executingAccount,
160
- });
161
-
162
- return {
163
- safeTransferData,
164
- additionalValueToSend: relayFee,
165
- signature,
166
- deadline,
167
- };
168
- };
169
-
170
- // recovers the signer of the sponsored relay call
171
- // and throws an error if the signer is not the executing account
172
- export const validateSponsoredRelayCall = async ({
173
- safeTransferData,
174
- additionalValueToSend,
175
- deadline,
176
- chainId,
177
- // account that should ahve signed the message
178
- executingAccount,
179
- signature,
180
- }: {
181
- safeTransferData: Hex;
182
- additionalValueToSend: bigint;
183
- deadline: bigint;
184
- executingAccount: Address;
185
- chainId: keyof typeof mintsEthUnwrapperAndCallerAddress;
186
- signature: Hex;
187
- }) => {
188
- const typedData = permitWithAdditionalValueTypedDataDefinition({
189
- additionalValueToSend,
190
- chainId,
191
- deadline,
192
- safeTransferData,
193
- });
194
-
195
- const recovered = await recoverTypedDataAddress({
196
- ...typedData,
197
- signature,
198
- });
199
-
200
- if (recovered !== executingAccount) {
201
- throw new Error("Invalid signature");
202
- }
203
- };
204
-
205
- // validate that the executingAccount signed a message
206
- // indicating intent to pay the relay fee, and execute the call
207
- export const validateAndExecuteSponsoredRelayCall = async ({
208
- permit,
209
- permitSignature,
210
- additionalValueToSend,
211
- deadline,
212
- chainId,
213
- // account that should ahve signed the message
214
- executingAccount,
215
- sponsoredCallSignature,
216
- walletClient,
217
- publicClient,
218
- }: {
219
- permit: PermitSafeTransferBatch;
220
- permitSignature: Hex;
221
- // additional value to send to the unwrapper
222
- additionalValueToSend: bigint;
223
- // deadline to execute the call
224
- deadline: bigint;
225
- // account that is to execute the call, and must have signed the sponsored call
226
- executingAccount: Address;
227
- chainId: keyof typeof mintsEthUnwrapperAndCallerAddress;
228
- // signature of the sponsored call
229
- sponsoredCallSignature: Hex;
230
- publicClient: PublicClient;
231
- walletClient: WalletClient;
232
- }) => {
233
- if (deadline < BigInt(new Date().getTime())) {
234
- throw new Error("Deadline has passed");
235
- }
236
-
237
- await validateSponsoredRelayCall({
238
- safeTransferData: permit.safeTransferData,
239
- additionalValueToSend,
240
- deadline,
241
- chainId,
242
- signature: sponsoredCallSignature,
243
- executingAccount,
244
- });
245
-
246
- // now we call a payable function on the mints eth unwrapper and caller contract,
247
- // with the permit and corresponding signature to transfer the mints to the unwrapper,
248
- // and call relay with the unwrapped value + relay fee.
249
- // the payable value on this call is the relay fee, which is added to the unwrapped
250
- // value of the mints and sent to the other chain.
251
- // any remaining value from the unwrapped MINTs is refunded to the original caller.
252
- const simulated = await publicClient.simulateContract({
253
- abi: mintsEthUnwrapperAndCallerABI,
254
- address: mintsEthUnwrapperAndCallerAddress[chainId],
255
- functionName: "permitWithAdditionalValue",
256
- args: [permit, permitSignature],
257
- account: executingAccount,
258
- // we must call this functio
259
- value: additionalValueToSend,
260
- });
261
-
262
- // wait for the transaction to succeed.
263
- const hash = await walletClient.writeContract(simulated.request);
264
-
265
- const receipt = await publicClient.waitForTransactionReceipt({
266
- hash,
267
- });
268
-
269
- if (receipt.status !== "success") {
270
- throw new Error("Transaction failed");
271
- }
272
- };
273
-
274
- function makeTypeData<
275
- const TTypedData extends TypedData | { [key: string]: unknown },
276
- TPrimaryType extends string,
277
- >(args: Omit<SignTypedDataParameters<TTypedData, TPrimaryType>, "account">) {
278
- return args;
279
- }
280
-
281
- function permitWithAdditionalValueTypedDataDefinition({
282
- safeTransferData,
283
- additionalValueToSend,
284
- deadline,
285
- chainId,
286
- }: {
287
- safeTransferData: Hex;
288
- additionalValueToSend: bigint;
289
- deadline: bigint;
290
- chainId: keyof typeof mintsEthUnwrapperAndCallerAddress;
291
- }) {
292
- return makeTypeData({
293
- primaryType: "PermitWithAdditionalValue",
294
- types: {
295
- PermitWithAdditionalValue: [
296
- {
297
- name: "safeTransferData",
298
- type: "bytes",
299
- },
300
- {
301
- name: "additionalValueToSend",
302
- type: "uint256",
303
- },
304
- {
305
- name: "deadline",
306
- type: "uint256",
307
- },
308
- ],
309
- },
310
- message: {
311
- safeTransferData,
312
- additionalValueToSend: additionalValueToSend,
313
- deadline: deadline,
314
- },
315
- domain: {
316
- chainId,
317
- name: "Relay",
318
- version: "1",
319
- verifyingContract: mintsEthUnwrapperAndCallerAddress[chainId],
320
- },
321
- });
322
- }
File without changes
File without changes
File without changes