@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,223 @@
1
+ /**
2
+ * Tests for prepareSso action
3
+ * Tests SSO URL generation via RPC
4
+ */
5
+
6
+ import { describe, expect, it, vi } from "../../tests/vitest-fixtures";
7
+ import type {
8
+ FrakClient,
9
+ PrepareSsoParamsType,
10
+ PrepareSsoReturnType,
11
+ } from "../types";
12
+ import { prepareSso } from "./prepareSso";
13
+
14
+ describe("prepareSso", () => {
15
+ describe("success cases", () => {
16
+ it("should call client.request with correct method and params", async () => {
17
+ const mockResponse: PrepareSsoReturnType = {
18
+ ssoUrl: "https://wallet.frak.id/sso?params=xyz",
19
+ };
20
+
21
+ const mockClient = {
22
+ config: {
23
+ metadata: {
24
+ name: "Test App",
25
+ },
26
+ },
27
+ request: vi.fn().mockResolvedValue(mockResponse),
28
+ } as unknown as FrakClient;
29
+
30
+ const params: PrepareSsoParamsType = {
31
+ directExit: true,
32
+ };
33
+
34
+ await prepareSso(mockClient, params);
35
+
36
+ expect(mockClient.request).toHaveBeenCalledWith({
37
+ method: "frak_prepareSso",
38
+ params: [params, "Test App", undefined],
39
+ });
40
+ });
41
+
42
+ it("should return SSO URL", async () => {
43
+ const mockResponse: PrepareSsoReturnType = {
44
+ ssoUrl: "https://wallet.frak.id/sso?params=abc123",
45
+ };
46
+
47
+ const mockClient = {
48
+ config: {
49
+ metadata: {
50
+ name: "Test App",
51
+ },
52
+ },
53
+ request: vi.fn().mockResolvedValue(mockResponse),
54
+ } as unknown as FrakClient;
55
+
56
+ const params: PrepareSsoParamsType = {
57
+ directExit: false,
58
+ };
59
+
60
+ const result = await prepareSso(mockClient, params);
61
+
62
+ expect(result).toEqual(mockResponse);
63
+ expect(result.ssoUrl).toBe(
64
+ "https://wallet.frak.id/sso?params=abc123"
65
+ );
66
+ });
67
+
68
+ it("should include custom CSS when provided", async () => {
69
+ const mockResponse: PrepareSsoReturnType = {
70
+ ssoUrl: "https://wallet.frak.id/sso?params=custom",
71
+ };
72
+
73
+ const mockClient = {
74
+ config: {
75
+ metadata: {
76
+ name: "Styled App",
77
+ },
78
+ customizations: {
79
+ css: "body { background: red; }",
80
+ },
81
+ },
82
+ request: vi.fn().mockResolvedValue(mockResponse),
83
+ } as unknown as FrakClient;
84
+
85
+ const params: PrepareSsoParamsType = {
86
+ directExit: true,
87
+ };
88
+
89
+ await prepareSso(mockClient, params);
90
+
91
+ expect(mockClient.request).toHaveBeenCalledWith({
92
+ method: "frak_prepareSso",
93
+ params: [params, "Styled App", "body { background: red; }"],
94
+ });
95
+ });
96
+
97
+ it("should handle params with redirectUrl", async () => {
98
+ const mockResponse: PrepareSsoReturnType = {
99
+ ssoUrl: "https://wallet.frak.id/sso?redirect=https://example.com",
100
+ };
101
+
102
+ const mockClient = {
103
+ config: {
104
+ metadata: {
105
+ name: "Test App",
106
+ },
107
+ },
108
+ request: vi.fn().mockResolvedValue(mockResponse),
109
+ } as unknown as FrakClient;
110
+
111
+ const params: PrepareSsoParamsType = {
112
+ redirectUrl: "https://example.com/callback",
113
+ directExit: false,
114
+ };
115
+
116
+ await prepareSso(mockClient, params);
117
+
118
+ expect(mockClient.request).toHaveBeenCalledWith({
119
+ method: "frak_prepareSso",
120
+ params: [params, "Test App", undefined],
121
+ });
122
+ });
123
+
124
+ it("should handle params with metadata", async () => {
125
+ const mockResponse: PrepareSsoReturnType = {
126
+ ssoUrl: "https://wallet.frak.id/sso?metadata=xyz",
127
+ };
128
+
129
+ const mockClient = {
130
+ config: {
131
+ metadata: {
132
+ name: "App with Metadata",
133
+ logoUrl: "https://example.com/logo.png",
134
+ },
135
+ },
136
+ request: vi.fn().mockResolvedValue(mockResponse),
137
+ } as unknown as FrakClient;
138
+
139
+ const params: PrepareSsoParamsType = {
140
+ metadata: {
141
+ logoUrl: "https://custom.com/logo.png",
142
+ homepageLink: "https://custom.com",
143
+ },
144
+ directExit: true,
145
+ };
146
+
147
+ await prepareSso(mockClient, params);
148
+
149
+ expect(mockClient.request).toHaveBeenCalledWith({
150
+ method: "frak_prepareSso",
151
+ params: [params, "App with Metadata", undefined],
152
+ });
153
+ });
154
+
155
+ it("should pass client metadata name to request", async () => {
156
+ const mockResponse: PrepareSsoReturnType = {
157
+ ssoUrl: "https://wallet.frak.id/sso?name=MyApp",
158
+ };
159
+
160
+ const mockClient = {
161
+ config: {
162
+ metadata: {
163
+ name: "MyApp",
164
+ logoUrl: "https://example.com/logo.png",
165
+ },
166
+ customizations: {
167
+ css: "body { color: blue; }",
168
+ },
169
+ },
170
+ request: vi.fn().mockResolvedValue(mockResponse),
171
+ } as unknown as FrakClient;
172
+
173
+ const params: PrepareSsoParamsType = {};
174
+
175
+ await prepareSso(mockClient, params);
176
+
177
+ expect(mockClient.request).toHaveBeenCalledWith({
178
+ method: "frak_prepareSso",
179
+ params: [params, "MyApp", "body { color: blue; }"],
180
+ });
181
+ });
182
+ });
183
+
184
+ describe("error handling", () => {
185
+ it("should propagate errors from client.request", async () => {
186
+ const error = new Error("SSO preparation failed");
187
+ const mockClient = {
188
+ config: {
189
+ metadata: {
190
+ name: "Test App",
191
+ },
192
+ },
193
+ request: vi.fn().mockRejectedValue(error),
194
+ } as unknown as FrakClient;
195
+
196
+ const params: PrepareSsoParamsType = {
197
+ directExit: true,
198
+ };
199
+
200
+ await expect(prepareSso(mockClient, params)).rejects.toThrow(
201
+ "SSO preparation failed"
202
+ );
203
+ });
204
+
205
+ it("should handle network errors", async () => {
206
+ const error = new Error("Network timeout");
207
+ const mockClient = {
208
+ config: {
209
+ metadata: {
210
+ name: "Test App",
211
+ },
212
+ },
213
+ request: vi.fn().mockRejectedValue(error),
214
+ } as unknown as FrakClient;
215
+
216
+ const params: PrepareSsoParamsType = {};
217
+
218
+ await expect(prepareSso(mockClient, params)).rejects.toThrow(
219
+ "Network timeout"
220
+ );
221
+ });
222
+ });
223
+ });
@@ -0,0 +1,48 @@
1
+ import type {
2
+ FrakClient,
3
+ PrepareSsoParamsType,
4
+ PrepareSsoReturnType,
5
+ } from "../types";
6
+
7
+ /**
8
+ * Generate SSO URL without opening popup
9
+ *
10
+ * This is a **synchronous**, client-side function that generates the SSO URL
11
+ * without any RPC calls to the wallet iframe. Use this when you need:
12
+ * - Custom URL modifications before opening popup
13
+ * - Pre-generation for advanced popup strategies
14
+ * - URL inspection/logging before SSO flow
15
+ *
16
+ * @param client - The current Frak Client
17
+ * @param args - The SSO parameters
18
+ * @returns Object containing the generated ssoUrl
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * // Generate URL for inspection
23
+ * const { ssoUrl } = prepareSso(client, {
24
+ * metadata: { logoUrl: "..." },
25
+ * directExit: true
26
+ * });
27
+ * console.log("Opening SSO:", ssoUrl);
28
+ *
29
+ * // Add custom params
30
+ * const customUrl = `${ssoUrl}&tracking=abc123`;
31
+ * await openSso(client, { metadata, ssoPopupUrl: customUrl });
32
+ * ```
33
+ *
34
+ * @remarks
35
+ * For most use cases, just use `openSso()` which handles URL generation automatically.
36
+ * Only use `prepareSso()` when you need explicit control over the URL.
37
+ */
38
+ export async function prepareSso(
39
+ client: FrakClient,
40
+ args: PrepareSsoParamsType
41
+ ): Promise<PrepareSsoReturnType> {
42
+ const { metadata, customizations } = client.config;
43
+
44
+ return await client.request({
45
+ method: "frak_prepareSso",
46
+ params: [args, metadata.name, customizations?.css],
47
+ });
48
+ }
@@ -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,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
+ }