@frak-labs/core-sdk 0.0.19-beta.f259d7fc → 0.1.0-beta.00226d62

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/cdn/bundle.iife.js +14 -0
  2. package/dist/actions-CEEObPYc.js +1 -0
  3. package/dist/actions-DbQhWYx8.cjs +1 -0
  4. package/dist/actions.cjs +1 -1
  5. package/dist/actions.d.cts +3 -1400
  6. package/dist/actions.d.ts +3 -1400
  7. package/dist/actions.js +1 -1
  8. package/dist/bundle.cjs +1 -13
  9. package/dist/bundle.d.cts +6 -1927
  10. package/dist/bundle.d.ts +6 -1927
  11. package/dist/bundle.js +1 -13
  12. package/dist/index-7OZ39x1U.d.ts +195 -0
  13. package/dist/index-C6FxkWPC.d.cts +511 -0
  14. package/dist/index-UFX7xCg3.d.ts +351 -0
  15. package/dist/index-d8xS4ryI.d.ts +511 -0
  16. package/dist/index-p4FqSp8z.d.cts +351 -0
  17. package/dist/index-zDq-VlKx.d.cts +195 -0
  18. package/dist/index.cjs +1 -13
  19. package/dist/index.d.cts +4 -1269
  20. package/dist/index.d.ts +4 -1269
  21. package/dist/index.js +1 -13
  22. package/dist/interaction-DMJ3ZfaF.d.cts +45 -0
  23. package/dist/interaction-KX1h9a7V.d.ts +45 -0
  24. package/dist/interactions-DnfM3oe0.js +1 -0
  25. package/dist/interactions-EIXhNLf6.cjs +1 -0
  26. package/dist/interactions.cjs +1 -1
  27. package/dist/interactions.d.cts +2 -182
  28. package/dist/interactions.d.ts +2 -182
  29. package/dist/interactions.js +1 -1
  30. package/dist/openSso-D--Airj6.d.cts +1018 -0
  31. package/dist/openSso-DsKJ4y0j.d.ts +1018 -0
  32. package/dist/productTypes-BUkXJKZ7.cjs +1 -0
  33. package/dist/productTypes-CGb1MmBF.js +1 -0
  34. package/dist/src-B_xO0AR6.cjs +13 -0
  35. package/dist/src-D2d52OZa.js +13 -0
  36. package/dist/trackEvent-CHnYa85W.js +1 -0
  37. package/dist/trackEvent-GuQm_1Nm.cjs +1 -0
  38. package/package.json +24 -19
  39. package/src/actions/displayEmbeddedWallet.test.ts +194 -0
  40. package/src/actions/displayEmbeddedWallet.ts +20 -0
  41. package/src/actions/displayModal.test.ts +387 -0
  42. package/src/actions/displayModal.ts +131 -0
  43. package/src/actions/getProductInformation.test.ts +133 -0
  44. package/src/actions/getProductInformation.ts +14 -0
  45. package/src/actions/index.ts +29 -0
  46. package/src/actions/openSso.test.ts +407 -0
  47. package/src/actions/openSso.ts +116 -0
  48. package/src/actions/prepareSso.test.ts +223 -0
  49. package/src/actions/prepareSso.ts +48 -0
  50. package/src/actions/referral/processReferral.test.ts +357 -0
  51. package/src/actions/referral/processReferral.ts +230 -0
  52. package/src/actions/referral/referralInteraction.test.ts +153 -0
  53. package/src/actions/referral/referralInteraction.ts +57 -0
  54. package/src/actions/sendInteraction.test.ts +219 -0
  55. package/src/actions/sendInteraction.ts +32 -0
  56. package/src/actions/trackPurchaseStatus.test.ts +287 -0
  57. package/src/actions/trackPurchaseStatus.ts +53 -0
  58. package/src/actions/watchWalletStatus.test.ts +372 -0
  59. package/src/actions/watchWalletStatus.ts +94 -0
  60. package/src/actions/wrapper/modalBuilder.test.ts +253 -0
  61. package/src/actions/wrapper/modalBuilder.ts +212 -0
  62. package/src/actions/wrapper/sendTransaction.test.ts +164 -0
  63. package/src/actions/wrapper/sendTransaction.ts +62 -0
  64. package/src/actions/wrapper/siweAuthenticate.test.ts +290 -0
  65. package/src/actions/wrapper/siweAuthenticate.ts +94 -0
  66. package/src/bundle.ts +3 -0
  67. package/src/clients/DebugInfo.test.ts +418 -0
  68. package/src/clients/DebugInfo.ts +182 -0
  69. package/src/clients/createIFrameFrakClient.ts +287 -0
  70. package/src/clients/index.ts +3 -0
  71. package/src/clients/setupClient.test.ts +343 -0
  72. package/src/clients/setupClient.ts +73 -0
  73. package/src/clients/transports/iframeLifecycleManager.test.ts +399 -0
  74. package/src/clients/transports/iframeLifecycleManager.ts +90 -0
  75. package/src/constants/interactionTypes.test.ts +128 -0
  76. package/src/constants/interactionTypes.ts +44 -0
  77. package/src/constants/locales.ts +14 -0
  78. package/src/constants/productTypes.test.ts +130 -0
  79. package/src/constants/productTypes.ts +33 -0
  80. package/src/index.ts +101 -0
  81. package/src/interactions/index.ts +5 -0
  82. package/src/interactions/pressEncoder.test.ts +215 -0
  83. package/src/interactions/pressEncoder.ts +53 -0
  84. package/src/interactions/purchaseEncoder.test.ts +291 -0
  85. package/src/interactions/purchaseEncoder.ts +99 -0
  86. package/src/interactions/referralEncoder.test.ts +170 -0
  87. package/src/interactions/referralEncoder.ts +47 -0
  88. package/src/interactions/retailEncoder.test.ts +107 -0
  89. package/src/interactions/retailEncoder.ts +37 -0
  90. package/src/interactions/webshopEncoder.test.ts +56 -0
  91. package/src/interactions/webshopEncoder.ts +30 -0
  92. package/src/types/client.ts +14 -0
  93. package/src/types/compression.ts +22 -0
  94. package/src/types/config.ts +111 -0
  95. package/src/types/context.ts +13 -0
  96. package/src/types/index.ts +71 -0
  97. package/src/types/lifecycle/client.ts +46 -0
  98. package/src/types/lifecycle/iframe.ts +35 -0
  99. package/src/types/lifecycle/index.ts +2 -0
  100. package/src/types/rpc/displayModal.ts +84 -0
  101. package/src/types/rpc/embedded/index.ts +68 -0
  102. package/src/types/rpc/embedded/loggedIn.ts +55 -0
  103. package/src/types/rpc/embedded/loggedOut.ts +28 -0
  104. package/src/types/rpc/interaction.ts +43 -0
  105. package/src/types/rpc/modal/final.ts +46 -0
  106. package/src/types/rpc/modal/generic.ts +46 -0
  107. package/src/types/rpc/modal/index.ts +20 -0
  108. package/src/types/rpc/modal/login.ts +32 -0
  109. package/src/types/rpc/modal/openSession.ts +25 -0
  110. package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
  111. package/src/types/rpc/modal/transaction.ts +33 -0
  112. package/src/types/rpc/productInformation.ts +59 -0
  113. package/src/types/rpc/sso.ts +80 -0
  114. package/src/types/rpc/walletStatus.ts +35 -0
  115. package/src/types/rpc.ts +158 -0
  116. package/src/types/transport.ts +34 -0
  117. package/src/utils/FrakContext.test.ts +407 -0
  118. package/src/utils/FrakContext.ts +158 -0
  119. package/src/utils/compression/b64.test.ts +181 -0
  120. package/src/utils/compression/b64.ts +29 -0
  121. package/src/utils/compression/compress.test.ts +123 -0
  122. package/src/utils/compression/compress.ts +11 -0
  123. package/src/utils/compression/decompress.test.ts +145 -0
  124. package/src/utils/compression/decompress.ts +11 -0
  125. package/src/utils/compression/index.ts +3 -0
  126. package/src/utils/computeProductId.test.ts +80 -0
  127. package/src/utils/computeProductId.ts +11 -0
  128. package/src/utils/constants.test.ts +23 -0
  129. package/src/utils/constants.ts +4 -0
  130. package/src/utils/formatAmount.test.ts +113 -0
  131. package/src/utils/formatAmount.ts +18 -0
  132. package/src/utils/getCurrencyAmountKey.test.ts +44 -0
  133. package/src/utils/getCurrencyAmountKey.ts +15 -0
  134. package/src/utils/getSupportedCurrency.test.ts +51 -0
  135. package/src/utils/getSupportedCurrency.ts +14 -0
  136. package/src/utils/getSupportedLocale.test.ts +64 -0
  137. package/src/utils/getSupportedLocale.ts +16 -0
  138. package/src/utils/iframeHelper.test.ts +450 -0
  139. package/src/utils/iframeHelper.ts +143 -0
  140. package/src/utils/index.ts +21 -0
  141. package/src/utils/sso.test.ts +361 -0
  142. package/src/utils/sso.ts +119 -0
  143. package/src/utils/ssoUrlListener.test.ts +252 -0
  144. package/src/utils/ssoUrlListener.ts +60 -0
  145. package/src/utils/trackEvent.test.ts +162 -0
  146. package/src/utils/trackEvent.ts +26 -0
  147. package/cdn/bundle.js +0 -19
  148. package/cdn/bundle.js.LICENSE.txt +0 -10
@@ -0,0 +1,230 @@
1
+ import { FrakRpcError, RpcErrorCodes } from "@frak-labs/frame-connector";
2
+ import { type Address, type Hex, isAddressEqual } from "viem";
3
+ import { ReferralInteractionEncoder } from "../../interactions";
4
+ import type {
5
+ DisplayEmbeddedWalletParamsType,
6
+ FrakClient,
7
+ FrakContext,
8
+ WalletStatusReturnType,
9
+ } from "../../types";
10
+ import { FrakContextManager, trackEvent } from "../../utils";
11
+ import { displayEmbeddedWallet, sendInteraction } from "../index";
12
+
13
+ /**
14
+ * The different states of the referral process
15
+ * @inline
16
+ */
17
+ type ReferralState =
18
+ | "idle"
19
+ | "processing"
20
+ | "success"
21
+ | "no-wallet"
22
+ | "no-session"
23
+ | "error"
24
+ | "no-referrer"
25
+ | "self-referral";
26
+
27
+ /**
28
+ * Options for the referral auto-interaction process
29
+ */
30
+ export type ProcessReferralOptions = {
31
+ /**
32
+ * If we want to always append the url with the frak context or not
33
+ * @defaultValue false
34
+ */
35
+ alwaysAppendUrl?: boolean;
36
+ };
37
+
38
+ /**
39
+ * This function handle all the heavy lifting of the referral interaction process
40
+ * 1. Check if the user has been referred or not (if not, early exit)
41
+ * 2. Then check if the user is logged in or not
42
+ * 2.1 If not logged in, try a soft login, if it fail, display a modal for the user to login
43
+ * 3. Check if that's not a self-referral (if yes, early exit)
44
+ * 4. Check if the user has an interaction session or not
45
+ * 4.1 If not, display a modal for the user to open a session
46
+ * 5. Push the referred interaction
47
+ * 6. Update the current url with the right data
48
+ * 7. Return the resulting referral state
49
+ *
50
+ * If any error occurs during the process, the function will catch it and return an error state
51
+ *
52
+ * @param client - The current Frak Client
53
+ * @param args
54
+ * @param args.walletStatus - The current user wallet status
55
+ * @param args.frakContext - The current frak context
56
+ * @param args.modalConfig - The modal configuration to display if the user is not logged in
57
+ * @param args.productId - The product id to interact with (if not specified will be recomputed from the current domain)
58
+ * @param args.options - Some options for the referral interaction
59
+ * @returns A promise with the resulting referral state
60
+ *
61
+ * @see {@link displayModal} for more details about the displayed modal
62
+ * @see {@link sendInteraction} for more details on the interaction submission part
63
+ * @see {@link ReferralInteractionEncoder} for more details about the referred interaction
64
+ * @see {@link @frak-labs/core-sdk!ModalStepTypes} for more details on each modal steps types
65
+ */
66
+ export async function processReferral(
67
+ client: FrakClient,
68
+ {
69
+ walletStatus,
70
+ frakContext,
71
+ modalConfig,
72
+ productId,
73
+ options,
74
+ }: {
75
+ walletStatus?: WalletStatusReturnType;
76
+ frakContext?: Partial<FrakContext> | null;
77
+ modalConfig?: DisplayEmbeddedWalletParamsType;
78
+ productId?: Hex;
79
+ options?: ProcessReferralOptions;
80
+ }
81
+ ) {
82
+ // Helper to fetch a fresh wallet status
83
+ let walletRequest = false;
84
+ async function getFreshWalletStatus() {
85
+ if (walletRequest) {
86
+ return;
87
+ }
88
+ walletRequest = true;
89
+ return ensureWalletConnected(client, {
90
+ modalConfig: {
91
+ ...modalConfig,
92
+ loggedIn: {
93
+ action: {
94
+ key: "referred",
95
+ },
96
+ },
97
+ },
98
+ walletStatus,
99
+ });
100
+ }
101
+
102
+ // Helper function to push the interaction
103
+ async function pushReferralInteraction(referrer: Address) {
104
+ const interaction = ReferralInteractionEncoder.referred({
105
+ referrer,
106
+ });
107
+ await Promise.allSettled([
108
+ // Send the interaction
109
+ sendInteraction(client, { productId, interaction }),
110
+ // Track the event
111
+ trackEvent(client, "user_referred", {
112
+ properties: {
113
+ referrer: referrer,
114
+ },
115
+ }),
116
+ ]);
117
+ }
118
+
119
+ try {
120
+ // Do the core processing logic
121
+ const { status, currentWallet } = await processReferralLogic({
122
+ initialWalletStatus: walletStatus,
123
+ getFreshWalletStatus,
124
+ pushReferralInteraction,
125
+ frakContext,
126
+ });
127
+
128
+ // Update the current url with the right data
129
+ FrakContextManager.replaceUrl({
130
+ url: window.location?.href,
131
+ context: options?.alwaysAppendUrl ? { r: currentWallet } : null,
132
+ });
133
+
134
+ return status;
135
+ } catch (error) {
136
+ console.log("Error processing referral", { error });
137
+ // Update the current url with the right data
138
+ FrakContextManager.replaceUrl({
139
+ url: window.location?.href,
140
+ context: options?.alwaysAppendUrl
141
+ ? { r: walletStatus?.wallet }
142
+ : null,
143
+ });
144
+
145
+ // And map the error a state
146
+ return mapErrorToState(error);
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Automatically submit a referral interaction when detected
152
+ * -> And automatically set the referral context in the url
153
+ * @param walletStatus
154
+ * @param frakContext
155
+ */
156
+ async function processReferralLogic({
157
+ initialWalletStatus,
158
+ getFreshWalletStatus,
159
+ pushReferralInteraction,
160
+ frakContext,
161
+ }: {
162
+ initialWalletStatus?: WalletStatusReturnType;
163
+ getFreshWalletStatus: () => Promise<Address | undefined>;
164
+ pushReferralInteraction: (referrer: Address) => Promise<void>;
165
+ frakContext?: Partial<FrakContext> | null;
166
+ }) {
167
+ // Get the current wallet, without auto displaying the modal
168
+ let currentWallet = initialWalletStatus?.wallet;
169
+ if (!frakContext?.r) {
170
+ return { status: "no-referrer", currentWallet } as const;
171
+ }
172
+
173
+ // We have a referral, so if we don't have a current wallet, display the modal
174
+ if (!currentWallet) {
175
+ currentWallet = await getFreshWalletStatus();
176
+ }
177
+
178
+ if (currentWallet && isAddressEqual(frakContext.r, currentWallet)) {
179
+ return { status: "self-referral", currentWallet } as const;
180
+ }
181
+
182
+ // If the current wallet doesn't have an interaction session, display the modal
183
+ if (!initialWalletStatus?.interactionSession) {
184
+ currentWallet = await getFreshWalletStatus();
185
+ }
186
+
187
+ // Push the referred interaction
188
+ await pushReferralInteraction(frakContext.r);
189
+ return { status: "success", currentWallet } as const;
190
+ }
191
+
192
+ /**
193
+ * Helper to ensure a wallet is connected, and display a modal if we got everything needed
194
+ */
195
+ async function ensureWalletConnected(
196
+ client: FrakClient,
197
+ {
198
+ modalConfig,
199
+ walletStatus,
200
+ }: {
201
+ modalConfig?: DisplayEmbeddedWalletParamsType;
202
+ walletStatus?: WalletStatusReturnType;
203
+ }
204
+ ) {
205
+ // If wallet not connected, or no interaction session
206
+ if (!walletStatus?.interactionSession) {
207
+ const result = await displayEmbeddedWallet(client, modalConfig ?? {});
208
+ return result?.wallet ?? undefined;
209
+ }
210
+
211
+ return walletStatus.wallet ?? undefined;
212
+ }
213
+
214
+ /**
215
+ * Helper to map an error to a state
216
+ * @param error
217
+ */
218
+ function mapErrorToState(error: unknown): ReferralState {
219
+ if (error instanceof FrakRpcError) {
220
+ switch (error.code) {
221
+ case RpcErrorCodes.walletNotConnected:
222
+ return "no-wallet";
223
+ case RpcErrorCodes.serverErrorForInteractionDelegation:
224
+ return "no-session";
225
+ default:
226
+ return "error";
227
+ }
228
+ }
229
+ return "error";
230
+ }
@@ -0,0 +1,153 @@
1
+ import type { Hex } from "viem";
2
+ import { beforeEach, describe, expect, test, vi } from "vitest";
3
+ import { referralInteraction } from "./referralInteraction";
4
+
5
+ vi.mock("../../utils", () => ({
6
+ FrakContextManager: {
7
+ parse: vi.fn(),
8
+ },
9
+ }));
10
+
11
+ vi.mock("../index", () => ({
12
+ watchWalletStatus: vi.fn(),
13
+ }));
14
+
15
+ vi.mock("./processReferral", () => ({
16
+ processReferral: vi.fn(),
17
+ }));
18
+
19
+ describe("referralInteraction", () => {
20
+ const mockClient = {
21
+ request: vi.fn(),
22
+ } as any;
23
+
24
+ const mockProductId =
25
+ "0x0000000000000000000000000000000000000000000000000000000000000002" as Hex;
26
+
27
+ beforeEach(() => {
28
+ vi.clearAllMocks();
29
+ Object.defineProperty(global, "window", {
30
+ value: { location: { href: "https://example.com?frak=test" } },
31
+ writable: true,
32
+ });
33
+ });
34
+
35
+ test("should parse context from window location", async () => {
36
+ const { FrakContextManager } = await import("../../utils");
37
+ const { watchWalletStatus } = await import("../index");
38
+ const { processReferral } = await import("./processReferral");
39
+
40
+ vi.mocked(FrakContextManager.parse).mockReturnValue({} as any);
41
+ vi.mocked(watchWalletStatus).mockResolvedValue(null as any);
42
+ vi.mocked(processReferral).mockResolvedValue("success");
43
+
44
+ await referralInteraction(mockClient);
45
+
46
+ expect(FrakContextManager.parse).toHaveBeenCalledWith({
47
+ url: "https://example.com?frak=test",
48
+ });
49
+ });
50
+
51
+ test("should get current wallet status", async () => {
52
+ const { FrakContextManager } = await import("../../utils");
53
+ const { watchWalletStatus } = await import("../index");
54
+ const { processReferral } = await import("./processReferral");
55
+
56
+ vi.mocked(FrakContextManager.parse).mockReturnValue({} as any);
57
+ vi.mocked(watchWalletStatus).mockResolvedValue({
58
+ wallet: "0x123" as Hex,
59
+ interactionSession: true,
60
+ } as any);
61
+ vi.mocked(processReferral).mockResolvedValue("success");
62
+
63
+ await referralInteraction(mockClient);
64
+
65
+ expect(watchWalletStatus).toHaveBeenCalledWith(mockClient);
66
+ });
67
+
68
+ test("should call processReferral with all parameters", async () => {
69
+ const { FrakContextManager } = await import("../../utils");
70
+ const { watchWalletStatus } = await import("../index");
71
+ const { processReferral } = await import("./processReferral");
72
+
73
+ const mockContext = { r: "0xreferrer" as Hex };
74
+ const mockWalletStatus = { wallet: "0x123" as Hex };
75
+ const mockModalConfig = { type: "login" };
76
+ const mockOptions = { alwaysAppendUrl: true };
77
+
78
+ vi.mocked(FrakContextManager.parse).mockReturnValue(mockContext as any);
79
+ vi.mocked(watchWalletStatus).mockResolvedValue(mockWalletStatus as any);
80
+ vi.mocked(processReferral).mockResolvedValue("success");
81
+
82
+ await referralInteraction(mockClient, {
83
+ productId: mockProductId,
84
+ modalConfig: mockModalConfig as any,
85
+ options: mockOptions,
86
+ });
87
+
88
+ expect(processReferral).toHaveBeenCalledWith(mockClient, {
89
+ walletStatus: mockWalletStatus,
90
+ frakContext: mockContext,
91
+ modalConfig: mockModalConfig,
92
+ productId: mockProductId,
93
+ options: mockOptions,
94
+ });
95
+ });
96
+
97
+ test("should return result from processReferral", async () => {
98
+ const { FrakContextManager } = await import("../../utils");
99
+ const { watchWalletStatus } = await import("../index");
100
+ const { processReferral } = await import("./processReferral");
101
+
102
+ vi.mocked(FrakContextManager.parse).mockReturnValue({} as any);
103
+ vi.mocked(watchWalletStatus).mockResolvedValue(null as any);
104
+ vi.mocked(processReferral).mockResolvedValue("success");
105
+
106
+ const result = await referralInteraction(mockClient);
107
+
108
+ expect(result).toBe("success");
109
+ });
110
+
111
+ test("should return undefined on error", async () => {
112
+ const { FrakContextManager } = await import("../../utils");
113
+ const { watchWalletStatus } = await import("../index");
114
+ const { processReferral } = await import("./processReferral");
115
+
116
+ vi.mocked(FrakContextManager.parse).mockReturnValue({} as any);
117
+ vi.mocked(watchWalletStatus).mockResolvedValue(null as any);
118
+ vi.mocked(processReferral).mockRejectedValue(new Error("Test error"));
119
+
120
+ const consoleSpy = vi
121
+ .spyOn(console, "warn")
122
+ .mockImplementation(() => {});
123
+
124
+ const result = await referralInteraction(mockClient);
125
+
126
+ expect(result).toBeUndefined();
127
+ expect(consoleSpy).toHaveBeenCalled();
128
+
129
+ consoleSpy.mockRestore();
130
+ });
131
+
132
+ test("should work with empty options", async () => {
133
+ const { FrakContextManager } = await import("../../utils");
134
+ const { watchWalletStatus } = await import("../index");
135
+ const { processReferral } = await import("./processReferral");
136
+
137
+ vi.mocked(FrakContextManager.parse).mockReturnValue({} as any);
138
+ vi.mocked(watchWalletStatus).mockResolvedValue(null as any);
139
+ vi.mocked(processReferral).mockResolvedValue("no-referrer");
140
+
141
+ const result = await referralInteraction(mockClient, {});
142
+
143
+ expect(result).toBe("no-referrer");
144
+ expect(processReferral).toHaveBeenCalledWith(
145
+ mockClient,
146
+ expect.objectContaining({
147
+ modalConfig: undefined,
148
+ productId: undefined,
149
+ options: undefined,
150
+ })
151
+ );
152
+ });
153
+ });
@@ -0,0 +1,57 @@
1
+ import type { Hex } from "viem";
2
+ import type { DisplayEmbeddedWalletParamsType, FrakClient } from "../../types";
3
+ import { FrakContextManager } from "../../utils";
4
+ import { watchWalletStatus } from "../index";
5
+ import {
6
+ type ProcessReferralOptions,
7
+ processReferral,
8
+ } from "./processReferral";
9
+
10
+ /**
11
+ * Function used to display a modal
12
+ * @param client - The current Frak Client
13
+ * @param args
14
+ * @param args.productId - The product id to interact with (if not specified will be recomputed from the current domain)
15
+ * @param args.modalConfig - The modal configuration to display if the user is not logged in
16
+ * @param args.options - Some options for the referral interaction
17
+ *
18
+ * @returns A promise with the resulting referral state, or undefined in case of an error
19
+ *
20
+ * @description This function will automatically handle the referral interaction process
21
+ *
22
+ * @see {@link processReferral} for more details on the automatic referral handling process
23
+ * @see {@link @frak-labs/core-sdk!ModalStepTypes} for more details on each modal steps types
24
+ */
25
+ export async function referralInteraction(
26
+ client: FrakClient,
27
+ {
28
+ productId,
29
+ modalConfig,
30
+ options,
31
+ }: {
32
+ productId?: Hex;
33
+ modalConfig?: DisplayEmbeddedWalletParamsType;
34
+ options?: ProcessReferralOptions;
35
+ } = {}
36
+ ) {
37
+ // Get the current frak context
38
+ const frakContext = FrakContextManager.parse({
39
+ url: window.location.href,
40
+ });
41
+
42
+ // Get the current wallet status
43
+ const currentWalletStatus = await watchWalletStatus(client);
44
+
45
+ try {
46
+ return await processReferral(client, {
47
+ walletStatus: currentWalletStatus,
48
+ frakContext,
49
+ modalConfig,
50
+ productId,
51
+ options,
52
+ });
53
+ } catch (error) {
54
+ console.warn("Error processing referral", { error });
55
+ }
56
+ return;
57
+ }
@@ -0,0 +1,219 @@
1
+ /**
2
+ * Tests for sendInteraction action
3
+ * Tests sending user interactions via RPC
4
+ */
5
+
6
+ import type { Hex } from "viem";
7
+ import { vi } from "vitest";
8
+
9
+ // Mock computeProductId - must be before imports
10
+ vi.mock("../utils/computeProductId", () => ({
11
+ computeProductId: vi.fn(
12
+ () =>
13
+ "0xcomputed1234567890123456789012345678901234567890123456789012" as Hex
14
+ ),
15
+ }));
16
+
17
+ import { describe, expect, it } from "../../tests/vitest-fixtures";
18
+ import type {
19
+ FrakClient,
20
+ PreparedInteraction,
21
+ SendInteractionParamsType,
22
+ SendInteractionReturnType,
23
+ } from "../types";
24
+ import { sendInteraction } from "./sendInteraction";
25
+
26
+ describe("sendInteraction", () => {
27
+ const mockInteraction: PreparedInteraction = {
28
+ interactionData: "0xdata" as Hex,
29
+ handlerTypeDenominator: "0x01" as Hex,
30
+ };
31
+
32
+ describe("with productId provided", () => {
33
+ it("should use provided productId", async () => {
34
+ const mockResponse: SendInteractionReturnType = {
35
+ delegationId: "delegation-123",
36
+ };
37
+
38
+ const mockClient = {
39
+ config: {
40
+ domain: "example.com",
41
+ },
42
+ request: vi.fn().mockResolvedValue(mockResponse),
43
+ } as unknown as FrakClient;
44
+
45
+ const params: SendInteractionParamsType = {
46
+ productId:
47
+ "0xprovidedid567890123456789012345678901234567890123456789012" as Hex,
48
+ interaction: mockInteraction,
49
+ };
50
+
51
+ await sendInteraction(mockClient, params);
52
+
53
+ expect(mockClient.request).toHaveBeenCalledWith({
54
+ method: "frak_sendInteraction",
55
+ params: [
56
+ "0xprovidedid567890123456789012345678901234567890123456789012",
57
+ mockInteraction,
58
+ undefined,
59
+ ],
60
+ });
61
+ });
62
+
63
+ it("should return delegationId", async () => {
64
+ const mockResponse: SendInteractionReturnType = {
65
+ delegationId: "delegation-456",
66
+ };
67
+
68
+ const mockClient = {
69
+ config: {
70
+ domain: "example.com",
71
+ },
72
+ request: vi.fn().mockResolvedValue(mockResponse),
73
+ } as unknown as FrakClient;
74
+
75
+ const params: SendInteractionParamsType = {
76
+ productId:
77
+ "0xprovidedid567890123456789012345678901234567890123456789012" as Hex,
78
+ interaction: mockInteraction,
79
+ };
80
+
81
+ const result = await sendInteraction(mockClient, params);
82
+
83
+ expect(result).toEqual(mockResponse);
84
+ expect(result.delegationId).toBe("delegation-456");
85
+ });
86
+
87
+ it("should include validation signature when provided", async () => {
88
+ const mockResponse: SendInteractionReturnType = {
89
+ delegationId: "delegation-789",
90
+ };
91
+
92
+ const mockClient = {
93
+ config: {
94
+ domain: "example.com",
95
+ },
96
+ request: vi.fn().mockResolvedValue(mockResponse),
97
+ } as unknown as FrakClient;
98
+
99
+ const params: SendInteractionParamsType = {
100
+ productId:
101
+ "0xprovidedid567890123456789012345678901234567890123456789012" as Hex,
102
+ interaction: mockInteraction,
103
+ validation: "0xsignature1234567890" as Hex,
104
+ };
105
+
106
+ await sendInteraction(mockClient, params);
107
+
108
+ expect(mockClient.request).toHaveBeenCalledWith({
109
+ method: "frak_sendInteraction",
110
+ params: [
111
+ "0xprovidedid567890123456789012345678901234567890123456789012",
112
+ mockInteraction,
113
+ "0xsignature1234567890",
114
+ ],
115
+ });
116
+ });
117
+ });
118
+
119
+ describe("without productId provided", () => {
120
+ it("should compute productId from client.config", async () => {
121
+ const { computeProductId } = await import(
122
+ "../utils/computeProductId"
123
+ );
124
+
125
+ const mockResponse: SendInteractionReturnType = {
126
+ delegationId: "delegation-computed",
127
+ };
128
+
129
+ const mockClient = {
130
+ config: {
131
+ domain: "example.com",
132
+ },
133
+ request: vi.fn().mockResolvedValue(mockResponse),
134
+ } as unknown as FrakClient;
135
+
136
+ const params: SendInteractionParamsType = {
137
+ interaction: mockInteraction,
138
+ };
139
+
140
+ await sendInteraction(mockClient, params);
141
+
142
+ expect(computeProductId).toHaveBeenCalledWith(mockClient.config);
143
+ expect(mockClient.request).toHaveBeenCalledWith({
144
+ method: "frak_sendInteraction",
145
+ params: [
146
+ "0xcomputed1234567890123456789012345678901234567890123456789012",
147
+ mockInteraction,
148
+ undefined,
149
+ ],
150
+ });
151
+ });
152
+
153
+ it("should work with different interaction types", async () => {
154
+ const mockResponse: SendInteractionReturnType = {
155
+ delegationId: "delegation-different",
156
+ };
157
+
158
+ const mockClient = {
159
+ config: {
160
+ domain: "example.com",
161
+ },
162
+ request: vi.fn().mockResolvedValue(mockResponse),
163
+ } as unknown as FrakClient;
164
+
165
+ const differentInteraction: PreparedInteraction = {
166
+ interactionData: "0xdifferentdata" as Hex,
167
+ handlerTypeDenominator: "0x02" as Hex,
168
+ };
169
+
170
+ const params: SendInteractionParamsType = {
171
+ interaction: differentInteraction,
172
+ };
173
+
174
+ const result = await sendInteraction(mockClient, params);
175
+
176
+ expect(result).toEqual(mockResponse);
177
+ });
178
+ });
179
+
180
+ describe("error handling", () => {
181
+ it("should propagate errors from client.request", async () => {
182
+ const error = new Error("Send interaction failed");
183
+ const mockClient = {
184
+ config: {
185
+ domain: "example.com",
186
+ },
187
+ request: vi.fn().mockRejectedValue(error),
188
+ } as unknown as FrakClient;
189
+
190
+ const params: SendInteractionParamsType = {
191
+ productId:
192
+ "0xprovidedid567890123456789012345678901234567890123456789012" as Hex,
193
+ interaction: mockInteraction,
194
+ };
195
+
196
+ await expect(sendInteraction(mockClient, params)).rejects.toThrow(
197
+ "Send interaction failed"
198
+ );
199
+ });
200
+
201
+ it("should handle network errors", async () => {
202
+ const error = new Error("Network timeout");
203
+ const mockClient = {
204
+ config: {
205
+ domain: "example.com",
206
+ },
207
+ request: vi.fn().mockRejectedValue(error),
208
+ } as unknown as FrakClient;
209
+
210
+ const params: SendInteractionParamsType = {
211
+ interaction: mockInteraction,
212
+ };
213
+
214
+ await expect(sendInteraction(mockClient, params)).rejects.toThrow(
215
+ "Network timeout"
216
+ );
217
+ });
218
+ });
219
+ });
@@ -0,0 +1,32 @@
1
+ import type {
2
+ FrakClient,
3
+ SendInteractionParamsType,
4
+ SendInteractionReturnType,
5
+ } from "../types";
6
+ import { computeProductId } from "../utils/computeProductId";
7
+
8
+ /**
9
+ * Function used to send an interaction
10
+ * @param client - The current Frak Client
11
+ * @param args
12
+ *
13
+ * @example
14
+ * const interaction = PressInteractionEncoder.openArticle({
15
+ * articleId: keccak256(toHex("article-slug")),
16
+ * });
17
+ * const { delegationId } = await sendInteraction(frakConfig, {
18
+ * interaction,
19
+ * });
20
+ * console.log("Delegated interaction id", delegationId);
21
+ */
22
+ export async function sendInteraction(
23
+ client: FrakClient,
24
+ { productId, interaction, validation }: SendInteractionParamsType
25
+ ): Promise<SendInteractionReturnType> {
26
+ const pId = productId ?? computeProductId(client.config);
27
+
28
+ return await client.request({
29
+ method: "frak_sendInteraction",
30
+ params: [pId, interaction, validation],
31
+ });
32
+ }