@frak-labs/core-sdk 0.1.0-beta.afa252b0 → 0.1.0-beta.d9302e66

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 (116) hide show
  1. package/package.json +22 -17
  2. package/src/actions/displayEmbeddedWallet.test.ts +194 -0
  3. package/src/actions/displayEmbeddedWallet.ts +20 -0
  4. package/src/actions/displayModal.test.ts +387 -0
  5. package/src/actions/displayModal.ts +131 -0
  6. package/src/actions/getProductInformation.test.ts +133 -0
  7. package/src/actions/getProductInformation.ts +14 -0
  8. package/src/actions/index.ts +29 -0
  9. package/src/actions/openSso.test.ts +407 -0
  10. package/src/actions/openSso.ts +116 -0
  11. package/src/actions/prepareSso.test.ts +223 -0
  12. package/src/actions/prepareSso.ts +48 -0
  13. package/src/actions/referral/processReferral.ts +230 -0
  14. package/src/actions/referral/referralInteraction.ts +57 -0
  15. package/src/actions/sendInteraction.test.ts +219 -0
  16. package/src/actions/sendInteraction.ts +32 -0
  17. package/src/actions/trackPurchaseStatus.test.ts +287 -0
  18. package/src/actions/trackPurchaseStatus.ts +53 -0
  19. package/src/actions/watchWalletStatus.test.ts +372 -0
  20. package/src/actions/watchWalletStatus.ts +94 -0
  21. package/src/actions/wrapper/modalBuilder.ts +212 -0
  22. package/src/actions/wrapper/sendTransaction.ts +62 -0
  23. package/src/actions/wrapper/siweAuthenticate.ts +94 -0
  24. package/src/bundle.ts +3 -0
  25. package/src/clients/DebugInfo.ts +182 -0
  26. package/src/clients/createIFrameFrakClient.ts +287 -0
  27. package/src/clients/index.ts +3 -0
  28. package/src/clients/setupClient.ts +73 -0
  29. package/src/clients/transports/iframeLifecycleManager.ts +90 -0
  30. package/src/constants/interactionTypes.ts +44 -0
  31. package/src/constants/locales.ts +14 -0
  32. package/src/constants/productTypes.ts +33 -0
  33. package/src/index.ts +101 -0
  34. package/src/interactions/index.ts +5 -0
  35. package/src/interactions/pressEncoder.test.ts +215 -0
  36. package/src/interactions/pressEncoder.ts +53 -0
  37. package/src/interactions/purchaseEncoder.test.ts +291 -0
  38. package/src/interactions/purchaseEncoder.ts +99 -0
  39. package/src/interactions/referralEncoder.test.ts +170 -0
  40. package/src/interactions/referralEncoder.ts +47 -0
  41. package/src/interactions/retailEncoder.test.ts +107 -0
  42. package/src/interactions/retailEncoder.ts +37 -0
  43. package/src/interactions/webshopEncoder.test.ts +56 -0
  44. package/src/interactions/webshopEncoder.ts +30 -0
  45. package/src/types/client.ts +14 -0
  46. package/src/types/compression.ts +22 -0
  47. package/src/types/config.ts +111 -0
  48. package/src/types/context.ts +13 -0
  49. package/src/types/index.ts +71 -0
  50. package/src/types/lifecycle/client.ts +46 -0
  51. package/src/types/lifecycle/iframe.ts +35 -0
  52. package/src/types/lifecycle/index.ts +2 -0
  53. package/src/types/rpc/displayModal.ts +84 -0
  54. package/src/types/rpc/embedded/index.ts +68 -0
  55. package/src/types/rpc/embedded/loggedIn.ts +55 -0
  56. package/src/types/rpc/embedded/loggedOut.ts +28 -0
  57. package/src/types/rpc/interaction.ts +43 -0
  58. package/src/types/rpc/modal/final.ts +46 -0
  59. package/src/types/rpc/modal/generic.ts +46 -0
  60. package/src/types/rpc/modal/index.ts +20 -0
  61. package/src/types/rpc/modal/login.ts +32 -0
  62. package/src/types/rpc/modal/openSession.ts +25 -0
  63. package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
  64. package/src/types/rpc/modal/transaction.ts +33 -0
  65. package/src/types/rpc/productInformation.ts +59 -0
  66. package/src/types/rpc/sso.ts +80 -0
  67. package/src/types/rpc/walletStatus.ts +35 -0
  68. package/src/types/rpc.ts +158 -0
  69. package/src/types/transport.ts +34 -0
  70. package/src/utils/FrakContext.test.ts +338 -0
  71. package/src/utils/FrakContext.ts +158 -0
  72. package/src/utils/compression/b64.test.ts +181 -0
  73. package/src/utils/compression/b64.ts +29 -0
  74. package/src/utils/compression/compress.test.ts +123 -0
  75. package/src/utils/compression/compress.ts +11 -0
  76. package/src/utils/compression/decompress.test.ts +145 -0
  77. package/src/utils/compression/decompress.ts +11 -0
  78. package/src/utils/compression/index.ts +3 -0
  79. package/src/utils/computeProductId.test.ts +80 -0
  80. package/src/utils/computeProductId.ts +11 -0
  81. package/src/utils/constants.test.ts +23 -0
  82. package/src/utils/constants.ts +4 -0
  83. package/src/utils/formatAmount.test.ts +113 -0
  84. package/src/utils/formatAmount.ts +18 -0
  85. package/src/utils/getCurrencyAmountKey.test.ts +44 -0
  86. package/src/utils/getCurrencyAmountKey.ts +15 -0
  87. package/src/utils/getSupportedCurrency.test.ts +51 -0
  88. package/src/utils/getSupportedCurrency.ts +14 -0
  89. package/src/utils/getSupportedLocale.test.ts +64 -0
  90. package/src/utils/getSupportedLocale.ts +16 -0
  91. package/src/utils/iframeHelper.test.ts +450 -0
  92. package/src/utils/iframeHelper.ts +143 -0
  93. package/src/utils/index.ts +21 -0
  94. package/src/utils/sso.test.ts +361 -0
  95. package/src/utils/sso.ts +119 -0
  96. package/src/utils/ssoUrlListener.ts +60 -0
  97. package/src/utils/trackEvent.test.ts +162 -0
  98. package/src/utils/trackEvent.ts +26 -0
  99. package/cdn/bundle.js +0 -19
  100. package/cdn/bundle.js.LICENSE.txt +0 -10
  101. package/dist/actions.cjs +0 -1
  102. package/dist/actions.d.cts +0 -1481
  103. package/dist/actions.d.ts +0 -1481
  104. package/dist/actions.js +0 -1
  105. package/dist/bundle.cjs +0 -13
  106. package/dist/bundle.d.cts +0 -2087
  107. package/dist/bundle.d.ts +0 -2087
  108. package/dist/bundle.js +0 -13
  109. package/dist/index.cjs +0 -13
  110. package/dist/index.d.cts +0 -1387
  111. package/dist/index.d.ts +0 -1387
  112. package/dist/index.js +0 -13
  113. package/dist/interactions.cjs +0 -1
  114. package/dist/interactions.d.cts +0 -182
  115. package/dist/interactions.d.ts +0 -182
  116. package/dist/interactions.js +0 -1
@@ -0,0 +1,291 @@
1
+ /**
2
+ * Tests for PurchaseInteractionEncoder
3
+ * Tests encoding of purchase-related user interactions
4
+ */
5
+
6
+ import type { Hex } from "viem";
7
+ import { encodeAbiParameters, pad, toHex } from "viem";
8
+ import { describe, expect, test } from "../../tests/vitest-fixtures";
9
+ import { interactionTypes } from "../constants/interactionTypes";
10
+ import { productTypes } from "../constants/productTypes";
11
+ import { PurchaseInteractionEncoder } from "./purchaseEncoder";
12
+
13
+ describe("PurchaseInteractionEncoder", () => {
14
+ describe("startPurchase", () => {
15
+ test("should encode start purchase interaction with correct structure", ({
16
+ mockPurchaseId,
17
+ }) => {
18
+ const interaction = PurchaseInteractionEncoder.startPurchase({
19
+ purchaseId: mockPurchaseId,
20
+ });
21
+
22
+ // Should return PreparedInteraction structure
23
+ expect(interaction).toHaveProperty("handlerTypeDenominator");
24
+ expect(interaction).toHaveProperty("interactionData");
25
+ });
26
+
27
+ test("should use purchase product type in handlerTypeDenominator", ({
28
+ mockPurchaseId,
29
+ }) => {
30
+ const interaction = PurchaseInteractionEncoder.startPurchase({
31
+ purchaseId: mockPurchaseId,
32
+ });
33
+
34
+ // Should use purchase product type (31)
35
+ const expectedDenominator = toHex(productTypes.purchase);
36
+ expect(interaction.handlerTypeDenominator).toBe(
37
+ expectedDenominator
38
+ );
39
+ });
40
+
41
+ test("should include started interaction type in data", ({
42
+ mockPurchaseId,
43
+ }) => {
44
+ const interaction = PurchaseInteractionEncoder.startPurchase({
45
+ purchaseId: mockPurchaseId,
46
+ });
47
+
48
+ // Should start with started interaction type
49
+ expect(interaction.interactionData).toContain(
50
+ interactionTypes.purchase.started
51
+ );
52
+ });
53
+
54
+ test("should pad purchase ID to 32 bytes", ({ mockPurchaseId }) => {
55
+ const interaction = PurchaseInteractionEncoder.startPurchase({
56
+ purchaseId: mockPurchaseId,
57
+ });
58
+
59
+ // Purchase ID should be padded to 32 bytes
60
+ const paddedPurchaseId = pad(mockPurchaseId, { size: 32 });
61
+ expect(interaction.interactionData).toContain(
62
+ paddedPurchaseId.slice(2)
63
+ );
64
+ });
65
+
66
+ test("should produce consistent output for same purchase ID", ({
67
+ mockPurchaseId,
68
+ }) => {
69
+ const interaction1 = PurchaseInteractionEncoder.startPurchase({
70
+ purchaseId: mockPurchaseId,
71
+ });
72
+ const interaction2 = PurchaseInteractionEncoder.startPurchase({
73
+ purchaseId: mockPurchaseId,
74
+ });
75
+
76
+ // Same input should produce same output
77
+ expect(interaction1).toEqual(interaction2);
78
+ });
79
+ });
80
+
81
+ describe("completedPurchase", () => {
82
+ test("should encode completed purchase with proof", ({
83
+ mockPurchaseId,
84
+ mockProof,
85
+ }) => {
86
+ const interaction = PurchaseInteractionEncoder.completedPurchase({
87
+ purchaseId: mockPurchaseId,
88
+ proof: mockProof,
89
+ });
90
+
91
+ // Should return PreparedInteraction structure
92
+ expect(interaction).toHaveProperty("handlerTypeDenominator");
93
+ expect(interaction).toHaveProperty("interactionData");
94
+ });
95
+
96
+ test("should use purchase product type in handlerTypeDenominator", ({
97
+ mockPurchaseId,
98
+ mockProof,
99
+ }) => {
100
+ const interaction = PurchaseInteractionEncoder.completedPurchase({
101
+ purchaseId: mockPurchaseId,
102
+ proof: mockProof,
103
+ });
104
+
105
+ // Should use purchase product type (31)
106
+ const expectedDenominator = toHex(productTypes.purchase);
107
+ expect(interaction.handlerTypeDenominator).toBe(
108
+ expectedDenominator
109
+ );
110
+ });
111
+
112
+ test("should include completed interaction type in data", ({
113
+ mockPurchaseId,
114
+ mockProof,
115
+ }) => {
116
+ const interaction = PurchaseInteractionEncoder.completedPurchase({
117
+ purchaseId: mockPurchaseId,
118
+ proof: mockProof,
119
+ });
120
+
121
+ // Should start with completed interaction type
122
+ expect(interaction.interactionData).toContain(
123
+ interactionTypes.purchase.completed
124
+ );
125
+ });
126
+
127
+ test("should use ABI encoding for inner data", ({
128
+ mockPurchaseId,
129
+ mockProof,
130
+ }) => {
131
+ const interaction = PurchaseInteractionEncoder.completedPurchase({
132
+ purchaseId: mockPurchaseId,
133
+ proof: mockProof,
134
+ });
135
+
136
+ // Inner data should be ABI encoded (uint256 + bytes32[])
137
+ const expectedInnerData = encodeAbiParameters(
138
+ [{ type: "uint256" }, { type: "bytes32[]" }],
139
+ [BigInt(mockPurchaseId), mockProof]
140
+ );
141
+ expect(interaction.interactionData).toContain(
142
+ expectedInnerData.slice(2)
143
+ );
144
+ });
145
+
146
+ test("should handle empty proof array", ({ mockPurchaseId }) => {
147
+ const interaction = PurchaseInteractionEncoder.completedPurchase({
148
+ purchaseId: mockPurchaseId,
149
+ proof: [],
150
+ });
151
+
152
+ // Should still encode properly with empty proof
153
+ expect(interaction.interactionData).toBeDefined();
154
+ expect(interaction.handlerTypeDenominator).toBe(
155
+ toHex(productTypes.purchase)
156
+ );
157
+ });
158
+
159
+ test("should handle multiple proof elements", ({ mockPurchaseId }) => {
160
+ const largeProof: Hex[] = [
161
+ "0x0000000000000000000000000000000000000000000000000000000000000001",
162
+ "0x0000000000000000000000000000000000000000000000000000000000000002",
163
+ "0x0000000000000000000000000000000000000000000000000000000000000003",
164
+ "0x0000000000000000000000000000000000000000000000000000000000000004",
165
+ "0x0000000000000000000000000000000000000000000000000000000000000005",
166
+ ];
167
+
168
+ const interaction = PurchaseInteractionEncoder.completedPurchase({
169
+ purchaseId: mockPurchaseId,
170
+ proof: largeProof,
171
+ });
172
+
173
+ // Should handle larger proof arrays
174
+ expect(interaction.interactionData).toBeDefined();
175
+ expect(interaction.handlerTypeDenominator).toBe(
176
+ toHex(productTypes.purchase)
177
+ );
178
+ });
179
+ });
180
+
181
+ describe("unsafeCompletedPurchase", () => {
182
+ test("should encode unsafe completed purchase", ({
183
+ mockPurchaseId,
184
+ }) => {
185
+ const interaction =
186
+ PurchaseInteractionEncoder.unsafeCompletedPurchase({
187
+ purchaseId: mockPurchaseId,
188
+ });
189
+
190
+ // Should return PreparedInteraction structure
191
+ expect(interaction).toHaveProperty("handlerTypeDenominator");
192
+ expect(interaction).toHaveProperty("interactionData");
193
+ });
194
+
195
+ test("should use purchase product type in handlerTypeDenominator", ({
196
+ mockPurchaseId,
197
+ }) => {
198
+ const interaction =
199
+ PurchaseInteractionEncoder.unsafeCompletedPurchase({
200
+ purchaseId: mockPurchaseId,
201
+ });
202
+
203
+ // Should use purchase product type (31)
204
+ const expectedDenominator = toHex(productTypes.purchase);
205
+ expect(interaction.handlerTypeDenominator).toBe(
206
+ expectedDenominator
207
+ );
208
+ });
209
+
210
+ test("should include unsafeCompleted interaction type in data", ({
211
+ mockPurchaseId,
212
+ }) => {
213
+ const interaction =
214
+ PurchaseInteractionEncoder.unsafeCompletedPurchase({
215
+ purchaseId: mockPurchaseId,
216
+ });
217
+
218
+ // Should start with unsafeCompleted interaction type
219
+ expect(interaction.interactionData).toContain(
220
+ interactionTypes.purchase.unsafeCompleted
221
+ );
222
+ });
223
+
224
+ test("should pad purchase ID to 32 bytes", ({ mockPurchaseId }) => {
225
+ const interaction =
226
+ PurchaseInteractionEncoder.unsafeCompletedPurchase({
227
+ purchaseId: mockPurchaseId,
228
+ });
229
+
230
+ // Purchase ID should be padded to 32 bytes
231
+ const paddedPurchaseId = pad(mockPurchaseId, { size: 32 });
232
+ expect(interaction.interactionData).toContain(
233
+ paddedPurchaseId.slice(2)
234
+ );
235
+ });
236
+
237
+ test("should differ from safe completed purchase", ({
238
+ mockPurchaseId,
239
+ }) => {
240
+ const safeInteraction =
241
+ PurchaseInteractionEncoder.completedPurchase({
242
+ purchaseId: mockPurchaseId,
243
+ proof: [],
244
+ });
245
+ const unsafeInteraction =
246
+ PurchaseInteractionEncoder.unsafeCompletedPurchase({
247
+ purchaseId: mockPurchaseId,
248
+ });
249
+
250
+ // Unsafe and safe versions should be different
251
+ expect(unsafeInteraction.interactionData).not.toBe(
252
+ safeInteraction.interactionData
253
+ );
254
+ });
255
+ });
256
+
257
+ describe("interaction data format", () => {
258
+ test("should produce valid hex strings", ({ mockPurchaseId }) => {
259
+ const startInteraction = PurchaseInteractionEncoder.startPurchase({
260
+ purchaseId: mockPurchaseId,
261
+ });
262
+ const unsafeInteraction =
263
+ PurchaseInteractionEncoder.unsafeCompletedPurchase({
264
+ purchaseId: mockPurchaseId,
265
+ });
266
+
267
+ // Both should be valid hex strings starting with 0x
268
+ expect(startInteraction.interactionData).toMatch(/^0x[0-9a-f]+$/);
269
+ expect(unsafeInteraction.interactionData).toMatch(/^0x[0-9a-f]+$/);
270
+ });
271
+
272
+ test("should handle different purchase IDs correctly", () => {
273
+ const purchaseId1 =
274
+ "0x0000000000000000000000000000000000000000000000000000000000000001" as const;
275
+ const purchaseId2 =
276
+ "0x0000000000000000000000000000000000000000000000000000000000000002" as const;
277
+
278
+ const interaction1 = PurchaseInteractionEncoder.startPurchase({
279
+ purchaseId: purchaseId1,
280
+ });
281
+ const interaction2 = PurchaseInteractionEncoder.startPurchase({
282
+ purchaseId: purchaseId2,
283
+ });
284
+
285
+ // Different purchase IDs should produce different interaction data
286
+ expect(interaction1.interactionData).not.toBe(
287
+ interaction2.interactionData
288
+ );
289
+ });
290
+ });
291
+ });
@@ -0,0 +1,99 @@
1
+ import { concatHex, encodeAbiParameters, type Hex, pad, toHex } from "viem";
2
+ import { interactionTypes } from "../constants/interactionTypes";
3
+ import { productTypes } from "../constants/productTypes";
4
+ import type { PreparedInteraction } from "../types";
5
+
6
+ /**
7
+ * Purchase interactions allow you to track user purchases on your platform.
8
+ * After setting up these interactions, you can create acquisition campaign based on the user purchase (starting a new one, completed, or even purchase dropped).
9
+ *
10
+ * :::info
11
+ * To properly handle purchase interactions, ensure that the "Purchase" product type is enabled in your Business dashboard, and that you have set up everything correctly in the `Purchasetracker` section.
12
+ * :::
13
+ *
14
+ * :::note
15
+ * The `purchaseId` is used on both interactions. It can be computed like this:
16
+ *
17
+ * ```ts
18
+ * const purchaseId = keccak256(concatHex([productId, toHex(externalPurchaseId)]));
19
+ * ```
20
+ *
21
+ * With:
22
+ * - `productId`: The id of your product, you can find it in the product dashboard.
23
+ * - `externalPurchaseId`: The id of the purchase in your system (e.g. the shopify `order_id`).
24
+ * :::
25
+ *
26
+ * @description Encode purchase related user interactions
27
+ *
28
+ * @group Interactions Encoder
29
+ *
30
+ * @see {@link !actions.sendInteraction | `sendInteraction()`} Action used to send the prepared interaction to the Frak Wallet
31
+ * @see {@link PreparedInteraction} The prepared interaction object that can be sent
32
+ * @see {@link !actions.trackPurchaseStatus | `trackPurchaseStatus()`} Action that will automatically send the purchase upon completion
33
+ * @see [Purchase Webhooks](/wallet-sdk/references-api/webhook) Webhooks to be implemented on your side to confirm a purchase
34
+ * @see [Purchase Proof](/wallet-sdk/references-api/purchaseProof) Get a merklee proof for the purchase
35
+ */
36
+ export const PurchaseInteractionEncoder = {
37
+ /**
38
+ * Encode a start purchase interaction
39
+ * @param args
40
+ * @param args.purchaseId - The id of the purchase that is being started.
41
+ */
42
+ startPurchase({ purchaseId }: { purchaseId: Hex }): PreparedInteraction {
43
+ const interactionData = concatHex([
44
+ interactionTypes.purchase.started,
45
+ pad(purchaseId, { size: 32 }),
46
+ ]);
47
+ return {
48
+ handlerTypeDenominator: toHex(productTypes.purchase),
49
+ interactionData,
50
+ };
51
+ },
52
+
53
+ /**
54
+ * Encode a complete purchase interaction
55
+ * @param args
56
+ * @param args.purchaseId - The id of the purchase that is being completed.
57
+ * @param args.proof - The merkle proof that the user has completed the purchase (see [Purchase Webhooks](/wallet-sdk/references-api/webhook) for more details).
58
+ */
59
+ completedPurchase({
60
+ purchaseId,
61
+ proof,
62
+ }: {
63
+ purchaseId: Hex;
64
+ proof: Hex[];
65
+ }): PreparedInteraction {
66
+ const innerData = encodeAbiParameters(
67
+ [{ type: "uint256" }, { type: "bytes32[]" }],
68
+ [BigInt(purchaseId), proof]
69
+ );
70
+ const interactionData = concatHex([
71
+ interactionTypes.purchase.completed,
72
+ innerData,
73
+ ]);
74
+ return {
75
+ handlerTypeDenominator: toHex(productTypes.purchase),
76
+ interactionData,
77
+ };
78
+ },
79
+
80
+ /**
81
+ * Encode an unsafe complete purchase interaction (when we can't provide the proof)
82
+ * @param args
83
+ * @param args.purchaseId - The id of the purchase that is being completed.
84
+ */
85
+ unsafeCompletedPurchase({
86
+ purchaseId,
87
+ }: {
88
+ purchaseId: Hex;
89
+ }): PreparedInteraction {
90
+ const interactionData = concatHex([
91
+ interactionTypes.purchase.unsafeCompleted,
92
+ pad(purchaseId, { size: 32 }),
93
+ ]);
94
+ return {
95
+ handlerTypeDenominator: toHex(productTypes.purchase),
96
+ interactionData,
97
+ };
98
+ },
99
+ };
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Tests for ReferralInteractionEncoder
3
+ * Tests encoding of referral-related user interactions
4
+ */
5
+
6
+ import { pad, toHex } from "viem";
7
+ import { describe, expect, it, test } from "../../tests/vitest-fixtures";
8
+ import { interactionTypes } from "../constants/interactionTypes";
9
+ import { productTypes } from "../constants/productTypes";
10
+ import { ReferralInteractionEncoder } from "./referralEncoder";
11
+
12
+ describe("ReferralInteractionEncoder", () => {
13
+ describe("createLink", () => {
14
+ it("should encode create link interaction with correct structure", () => {
15
+ const interaction = ReferralInteractionEncoder.createLink();
16
+
17
+ // Should return PreparedInteraction structure
18
+ expect(interaction).toHaveProperty("handlerTypeDenominator");
19
+ expect(interaction).toHaveProperty("interactionData");
20
+ });
21
+
22
+ it("should use referral product type in handlerTypeDenominator", () => {
23
+ const interaction = ReferralInteractionEncoder.createLink();
24
+
25
+ // Should use referral product type (30)
26
+ const expectedDenominator = toHex(productTypes.referral);
27
+ expect(interaction.handlerTypeDenominator).toBe(
28
+ expectedDenominator
29
+ );
30
+ });
31
+
32
+ it("should use createLink interaction type in data", () => {
33
+ const interaction = ReferralInteractionEncoder.createLink();
34
+
35
+ // Should use createLink interaction type
36
+ expect(interaction.interactionData).toBe(
37
+ interactionTypes.referral.createLink
38
+ );
39
+ });
40
+
41
+ it("should produce consistent output (no parameters)", () => {
42
+ const interaction1 = ReferralInteractionEncoder.createLink();
43
+ const interaction2 = ReferralInteractionEncoder.createLink();
44
+
45
+ // Should always produce the same output
46
+ expect(interaction1).toEqual(interaction2);
47
+ });
48
+ });
49
+
50
+ describe("referred", () => {
51
+ test("should encode referred interaction with correct structure", ({
52
+ mockReferrerAddress,
53
+ }) => {
54
+ const interaction = ReferralInteractionEncoder.referred({
55
+ referrer: mockReferrerAddress,
56
+ });
57
+
58
+ // Should return PreparedInteraction structure
59
+ expect(interaction).toHaveProperty("handlerTypeDenominator");
60
+ expect(interaction).toHaveProperty("interactionData");
61
+ });
62
+
63
+ test("should use referral product type in handlerTypeDenominator", ({
64
+ mockReferrerAddress,
65
+ }) => {
66
+ const interaction = ReferralInteractionEncoder.referred({
67
+ referrer: mockReferrerAddress,
68
+ });
69
+
70
+ // Should use referral product type (30)
71
+ const expectedDenominator = toHex(productTypes.referral);
72
+ expect(interaction.handlerTypeDenominator).toBe(
73
+ expectedDenominator
74
+ );
75
+ });
76
+
77
+ test("should include referred interaction type in data", ({
78
+ mockReferrerAddress,
79
+ }) => {
80
+ const interaction = ReferralInteractionEncoder.referred({
81
+ referrer: mockReferrerAddress,
82
+ });
83
+
84
+ // Should start with referred interaction type
85
+ expect(interaction.interactionData).toContain(
86
+ interactionTypes.referral.referred
87
+ );
88
+ });
89
+
90
+ test("should pad referrer address to 32 bytes", ({
91
+ mockReferrerAddress,
92
+ }) => {
93
+ const interaction = ReferralInteractionEncoder.referred({
94
+ referrer: mockReferrerAddress,
95
+ });
96
+
97
+ // Referrer address should be padded to 32 bytes
98
+ const paddedAddress = pad(mockReferrerAddress, { size: 32 });
99
+ expect(interaction.interactionData).toContain(
100
+ paddedAddress.slice(2)
101
+ );
102
+ });
103
+
104
+ test("should produce different output for different referrers", () => {
105
+ const referrer1 =
106
+ "0x1234567890123456789012345678901234567890" as const;
107
+ const referrer2 =
108
+ "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd" as const;
109
+
110
+ const interaction1 = ReferralInteractionEncoder.referred({
111
+ referrer: referrer1,
112
+ });
113
+ const interaction2 = ReferralInteractionEncoder.referred({
114
+ referrer: referrer2,
115
+ });
116
+
117
+ // Different referrers should produce different interaction data
118
+ expect(interaction1.interactionData).not.toBe(
119
+ interaction2.interactionData
120
+ );
121
+ });
122
+
123
+ test("should produce consistent output for same referrer", ({
124
+ mockReferrerAddress,
125
+ }) => {
126
+ const interaction1 = ReferralInteractionEncoder.referred({
127
+ referrer: mockReferrerAddress,
128
+ });
129
+ const interaction2 = ReferralInteractionEncoder.referred({
130
+ referrer: mockReferrerAddress,
131
+ });
132
+
133
+ // Same referrer should produce same output
134
+ expect(interaction1).toEqual(interaction2);
135
+ });
136
+ });
137
+
138
+ describe("interaction data format", () => {
139
+ test("should produce valid hex strings", ({ mockReferrerAddress }) => {
140
+ const createLinkInteraction =
141
+ ReferralInteractionEncoder.createLink();
142
+ const referredInteraction = ReferralInteractionEncoder.referred({
143
+ referrer: mockReferrerAddress,
144
+ });
145
+
146
+ // Both should be valid hex strings starting with 0x
147
+ expect(createLinkInteraction.interactionData).toMatch(
148
+ /^0x[0-9a-f]+$/
149
+ );
150
+ expect(referredInteraction.interactionData).toMatch(
151
+ /^0x[0-9a-f]+$/
152
+ );
153
+ });
154
+
155
+ test("should have different interaction types", ({
156
+ mockReferrerAddress,
157
+ }) => {
158
+ const createLinkInteraction =
159
+ ReferralInteractionEncoder.createLink();
160
+ const referredInteraction = ReferralInteractionEncoder.referred({
161
+ referrer: mockReferrerAddress,
162
+ });
163
+
164
+ // Different interaction types should produce different data
165
+ expect(createLinkInteraction.interactionData).not.toBe(
166
+ referredInteraction.interactionData
167
+ );
168
+ });
169
+ });
170
+ });
@@ -0,0 +1,47 @@
1
+ import { type Address, concatHex, pad, toHex } from "viem";
2
+ import { interactionTypes } from "../constants/interactionTypes";
3
+ import { productTypes } from "../constants/productTypes";
4
+ import type { PreparedInteraction } from "../types";
5
+
6
+ /**
7
+ * Referral interactions allow you to track user sharing activities.
8
+ * These interactions are essential for platforms looking to grow their user base through user-to-user referrals and reward systems.
9
+ *
10
+ * :::info
11
+ * To properly handle referral interactions, ensure that the "Referral" product type is enabled in your Business dashboard.
12
+ * :::
13
+ *
14
+ * @description Encode referral related user interactions
15
+ *
16
+ * @group Interactions Encoder
17
+ *
18
+ * @see {@link PreparedInteraction} The prepared interaction object that can be sent
19
+ * @see {@link !actions.sendInteraction | `sendInteraction()`} Action used to send the prepared interaction to the Frak Wallet
20
+ */
21
+ export const ReferralInteractionEncoder = {
22
+ /**
23
+ * Records the event of a user creating a referral link. Note that this interaction doesn't actually create the link itself; it only sends an event to track that a link was created.
24
+ */
25
+ createLink(): PreparedInteraction {
26
+ return {
27
+ handlerTypeDenominator: toHex(productTypes.referral),
28
+ interactionData: interactionTypes.referral.createLink,
29
+ };
30
+ },
31
+
32
+ /**
33
+ * Encode a referred interaction
34
+ * @param args
35
+ * @param args.referrer - The Ethereum address of the user who made the referral
36
+ */
37
+ referred({ referrer }: { referrer: Address }): PreparedInteraction {
38
+ const interactionData = concatHex([
39
+ interactionTypes.referral.referred,
40
+ pad(referrer, { size: 32 }),
41
+ ]);
42
+ return {
43
+ handlerTypeDenominator: toHex(productTypes.referral),
44
+ interactionData,
45
+ };
46
+ },
47
+ };