@frak-labs/core-sdk 0.1.0-beta.6e0d8026 → 0.1.0-beta.8d103039
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.
- package/package.json +3 -2
- package/src/actions/displayEmbeddedWallet.ts +20 -0
- package/src/actions/displayModal.ts +131 -0
- package/src/actions/getProductInformation.ts +14 -0
- package/src/actions/index.ts +29 -0
- package/src/actions/openSso.ts +116 -0
- package/src/actions/prepareSso.ts +48 -0
- package/src/actions/referral/processReferral.ts +230 -0
- package/src/actions/referral/referralInteraction.ts +57 -0
- package/src/actions/sendInteraction.ts +32 -0
- package/src/actions/trackPurchaseStatus.ts +53 -0
- package/src/actions/watchWalletStatus.ts +94 -0
- package/src/actions/wrapper/modalBuilder.ts +212 -0
- package/src/actions/wrapper/sendTransaction.ts +62 -0
- package/src/actions/wrapper/siweAuthenticate.ts +94 -0
- package/src/bundle.ts +3 -0
- package/src/clients/DebugInfo.ts +182 -0
- package/src/clients/createIFrameFrakClient.ts +287 -0
- package/src/clients/index.ts +3 -0
- package/src/clients/setupClient.ts +71 -0
- package/src/clients/transports/iframeLifecycleManager.ts +88 -0
- package/src/constants/interactionTypes.ts +44 -0
- package/src/constants/locales.ts +14 -0
- package/src/constants/productTypes.ts +33 -0
- package/src/index.ts +103 -0
- package/src/interactions/index.ts +5 -0
- package/src/interactions/pressEncoder.ts +53 -0
- package/src/interactions/purchaseEncoder.ts +94 -0
- package/src/interactions/referralEncoder.ts +47 -0
- package/src/interactions/retailEncoder.ts +37 -0
- package/src/interactions/webshopEncoder.ts +30 -0
- package/src/types/client.ts +14 -0
- package/src/types/compression.ts +22 -0
- package/src/types/config.ts +111 -0
- package/src/types/context.ts +13 -0
- package/src/types/index.ts +70 -0
- package/src/types/lifecycle/client.ts +46 -0
- package/src/types/lifecycle/iframe.ts +35 -0
- package/src/types/lifecycle/index.ts +2 -0
- package/src/types/rpc/displayModal.ts +84 -0
- package/src/types/rpc/embedded/index.ts +68 -0
- package/src/types/rpc/embedded/loggedIn.ts +55 -0
- package/src/types/rpc/embedded/loggedOut.ts +28 -0
- package/src/types/rpc/interaction.ts +43 -0
- package/src/types/rpc/modal/final.ts +46 -0
- package/src/types/rpc/modal/generic.ts +46 -0
- package/src/types/rpc/modal/index.ts +20 -0
- package/src/types/rpc/modal/login.ts +32 -0
- package/src/types/rpc/modal/openSession.ts +25 -0
- package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
- package/src/types/rpc/modal/transaction.ts +33 -0
- package/src/types/rpc/productInformation.ts +59 -0
- package/src/types/rpc/sso.ts +80 -0
- package/src/types/rpc/walletStatus.ts +35 -0
- package/src/types/rpc.ts +158 -0
- package/src/types/transport.ts +34 -0
- package/src/utils/FrakContext.ts +152 -0
- package/src/utils/compression/b64.ts +29 -0
- package/src/utils/compression/compress.ts +11 -0
- package/src/utils/compression/decompress.ts +11 -0
- package/src/utils/compression/index.ts +3 -0
- package/src/utils/computeProductId.ts +11 -0
- package/src/utils/constants.ts +4 -0
- package/src/utils/formatAmount.ts +18 -0
- package/src/utils/getCurrencyAmountKey.ts +15 -0
- package/src/utils/getSupportedCurrency.ts +14 -0
- package/src/utils/getSupportedLocale.ts +16 -0
- package/src/utils/iframeHelper.ts +142 -0
- package/src/utils/index.ts +21 -0
- package/src/utils/sso.ts +119 -0
- package/src/utils/ssoUrlListener.ts +60 -0
- package/src/utils/trackEvent.ts +26 -0
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"url": "https://twitter.com/QNivelais"
|
|
12
12
|
}
|
|
13
13
|
],
|
|
14
|
-
"version": "0.1.0-beta.
|
|
14
|
+
"version": "0.1.0-beta.8d103039",
|
|
15
15
|
"description": "Core SDK of the Frak wallet, low level library to interact directly with the frak ecosystem.",
|
|
16
16
|
"repository": {
|
|
17
17
|
"url": "https://github.com/frak-id/wallet",
|
|
@@ -34,7 +34,8 @@
|
|
|
34
34
|
"type": "module",
|
|
35
35
|
"files": [
|
|
36
36
|
"/dist",
|
|
37
|
-
"/cdn"
|
|
37
|
+
"/cdn",
|
|
38
|
+
"/src"
|
|
38
39
|
],
|
|
39
40
|
"browser": "./cdn/bundle.js",
|
|
40
41
|
"exports": {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DisplayEmbeddedWalletParamsType,
|
|
3
|
+
DisplayEmbeddedWalletResultType,
|
|
4
|
+
FrakClient,
|
|
5
|
+
} from "../types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Function used to display the Frak embedded wallet popup
|
|
9
|
+
* @param client - The current Frak Client
|
|
10
|
+
* @param params - The parameter used to customise the embedded wallet
|
|
11
|
+
*/
|
|
12
|
+
export async function displayEmbeddedWallet(
|
|
13
|
+
client: FrakClient,
|
|
14
|
+
params: DisplayEmbeddedWalletParamsType
|
|
15
|
+
): Promise<DisplayEmbeddedWalletResultType> {
|
|
16
|
+
return await client.request({
|
|
17
|
+
method: "frak_displayEmbeddedWallet",
|
|
18
|
+
params: [params, client.config.metadata],
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DisplayModalParamsType,
|
|
3
|
+
FrakClient,
|
|
4
|
+
ModalRpcStepsResultType,
|
|
5
|
+
ModalStepTypes,
|
|
6
|
+
} from "../types";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Function used to display a modal
|
|
10
|
+
* @param client - The current Frak Client
|
|
11
|
+
* @param args
|
|
12
|
+
* @param args.steps - The different steps of the modal
|
|
13
|
+
* @param args.metadata - The metadata for the modal (customization, etc)
|
|
14
|
+
* @returns The result of each modal steps
|
|
15
|
+
*
|
|
16
|
+
* @description This function will display a modal to the user with the provided steps and metadata.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* - The UI of the displayed modal can be configured with the `customCss` property in the `customizations.css` field of the top-level config.
|
|
20
|
+
* - The `login` and `openSession` steps will be automatically skipped if the user is already logged in or has an active session. It's safe to include these steps in all cases to ensure proper user state.
|
|
21
|
+
* - Steps are automatically reordered in the following sequence:
|
|
22
|
+
* 1. `login` (if needed)
|
|
23
|
+
* 2. `openSession` (if needed)
|
|
24
|
+
* 3. All other steps in the order specified
|
|
25
|
+
* 4. `success` (if included, always last)
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* Simple sharing modal with steps:
|
|
29
|
+
* 1. Login (Skipped if already logged in)
|
|
30
|
+
* 2. Open a session (Skipped if already opened)
|
|
31
|
+
* 3. Display a success message with sharing link option
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const results = await displayModal(frakConfig, {
|
|
35
|
+
* steps: {
|
|
36
|
+
* // Simple login with no SSO, nor customization
|
|
37
|
+
* login: { allowSso: false },
|
|
38
|
+
* // Simple session opening, with no customization
|
|
39
|
+
* openSession: {},
|
|
40
|
+
* // Success message
|
|
41
|
+
* final: {
|
|
42
|
+
* action: { key: "reward" },
|
|
43
|
+
* // Skip this step, it will be only displayed in the stepper within the modal
|
|
44
|
+
* autoSkip: true,
|
|
45
|
+
* },
|
|
46
|
+
* },
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* console.log("Login step - wallet", results.login.wallet);
|
|
50
|
+
* console.log("Open session step - start + end", {
|
|
51
|
+
* start: results.openSession.startTimestamp,
|
|
52
|
+
* end: results.openSession.endTimestamp,
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* A full modal example, with a few customization options, with the steps:
|
|
58
|
+
* 1. Login (Skipped if already logged in)
|
|
59
|
+
* 2. Open a session (Skipped if already opened)
|
|
60
|
+
* 3. Authenticate via SIWE
|
|
61
|
+
* 4. Send a transaction
|
|
62
|
+
* 5. Display a success message with sharing link options
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* const results = await displayModal(frakConfig, {
|
|
66
|
+
* steps: {
|
|
67
|
+
* // Login step
|
|
68
|
+
* login: {
|
|
69
|
+
* allowSso: true,
|
|
70
|
+
* ssoMetadata: {
|
|
71
|
+
* logoUrl: "https://my-app.com/logo.png",
|
|
72
|
+
* homepageLink: "https://my-app.com",
|
|
73
|
+
* },
|
|
74
|
+
* },
|
|
75
|
+
* // Simple session opening, with no customisation
|
|
76
|
+
* openSession: {},
|
|
77
|
+
* // Siwe authentication
|
|
78
|
+
* siweAuthenticate: {
|
|
79
|
+
* siwe: {
|
|
80
|
+
* domain: "my-app.com",
|
|
81
|
+
* uri: "https://my-app.com/",
|
|
82
|
+
* nonce: generateSiweNonce(),
|
|
83
|
+
* version: "1",
|
|
84
|
+
* },
|
|
85
|
+
* },
|
|
86
|
+
* // Send batched transaction
|
|
87
|
+
* sendTransaction: {
|
|
88
|
+
* tx: [
|
|
89
|
+
* { to: "0xdeadbeef", data: "0xdeadbeef" },
|
|
90
|
+
* { to: "0xdeadbeef", data: "0xdeadbeef" },
|
|
91
|
+
* ],
|
|
92
|
+
* },
|
|
93
|
+
* // Success message with sharing options
|
|
94
|
+
* final: {
|
|
95
|
+
* action: {
|
|
96
|
+
* key: "sharing",
|
|
97
|
+
* options: {
|
|
98
|
+
* popupTitle: "Share the app",
|
|
99
|
+
* text: "Discover my super app website",
|
|
100
|
+
* link: "https://my-app.com",
|
|
101
|
+
* },
|
|
102
|
+
* },
|
|
103
|
+
* dismissedMetadata: {
|
|
104
|
+
* title: "Dismiss",
|
|
105
|
+
* description: "You won't be rewarded for this sharing action",
|
|
106
|
+
* },
|
|
107
|
+
* },
|
|
108
|
+
* },
|
|
109
|
+
* metadata: {
|
|
110
|
+
* // Header of desktop modals
|
|
111
|
+
* header: {
|
|
112
|
+
* title: "My-App",
|
|
113
|
+
* icon: "https://my-app.com/logo.png",
|
|
114
|
+
* },
|
|
115
|
+
* // Context that will be present in every modal steps
|
|
116
|
+
* context: "My-app overkill flow",
|
|
117
|
+
* },
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
export async function displayModal<
|
|
122
|
+
T extends ModalStepTypes[] = ModalStepTypes[],
|
|
123
|
+
>(
|
|
124
|
+
client: FrakClient,
|
|
125
|
+
{ steps, metadata }: DisplayModalParamsType<T>
|
|
126
|
+
): Promise<ModalRpcStepsResultType<T>> {
|
|
127
|
+
return (await client.request({
|
|
128
|
+
method: "frak_displayModal",
|
|
129
|
+
params: [steps, metadata, client.config.metadata],
|
|
130
|
+
})) as ModalRpcStepsResultType<T>;
|
|
131
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { FrakClient, GetProductInformationReturnType } from "../types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Function used to get the current product information
|
|
5
|
+
* @param client - The current Frak Client
|
|
6
|
+
* @returns The product information in a promise
|
|
7
|
+
*/
|
|
8
|
+
export async function getProductInformation(
|
|
9
|
+
client: FrakClient
|
|
10
|
+
): Promise<GetProductInformationReturnType> {
|
|
11
|
+
return await client.request({
|
|
12
|
+
method: "frak_getProductInformation",
|
|
13
|
+
});
|
|
14
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export { watchWalletStatus } from "./watchWalletStatus";
|
|
2
|
+
export { sendInteraction } from "./sendInteraction";
|
|
3
|
+
export { displayModal } from "./displayModal";
|
|
4
|
+
export { displayEmbeddedWallet } from "./displayEmbeddedWallet";
|
|
5
|
+
export { openSso } from "./openSso";
|
|
6
|
+
export { prepareSso } from "./prepareSso";
|
|
7
|
+
export { getProductInformation } from "./getProductInformation";
|
|
8
|
+
// Helper to track the purchase status
|
|
9
|
+
export { trackPurchaseStatus } from "./trackPurchaseStatus";
|
|
10
|
+
// Modal wrappers
|
|
11
|
+
export {
|
|
12
|
+
siweAuthenticate,
|
|
13
|
+
type SiweAuthenticateModalParams,
|
|
14
|
+
} from "./wrapper/siweAuthenticate";
|
|
15
|
+
export {
|
|
16
|
+
sendTransaction,
|
|
17
|
+
type SendTransactionParams,
|
|
18
|
+
} from "./wrapper/sendTransaction";
|
|
19
|
+
export {
|
|
20
|
+
modalBuilder,
|
|
21
|
+
type ModalStepBuilder,
|
|
22
|
+
type ModalBuilder,
|
|
23
|
+
} from "./wrapper/modalBuilder";
|
|
24
|
+
// Referral interaction
|
|
25
|
+
export { referralInteraction } from "./referral/referralInteraction";
|
|
26
|
+
export {
|
|
27
|
+
processReferral,
|
|
28
|
+
type ProcessReferralOptions,
|
|
29
|
+
} from "./referral/processReferral";
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FrakClient,
|
|
3
|
+
OpenSsoParamsType,
|
|
4
|
+
OpenSsoReturnType,
|
|
5
|
+
} from "../types";
|
|
6
|
+
import { computeProductId } from "../utils/computeProductId";
|
|
7
|
+
import { generateSsoUrl } from "../utils/sso";
|
|
8
|
+
|
|
9
|
+
// SSO popup configuration
|
|
10
|
+
export const ssoPopupFeatures =
|
|
11
|
+
"menubar=no,status=no,scrollbars=no,fullscreen=no,width=500, height=800";
|
|
12
|
+
export const ssoPopupName = "frak-sso";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Function used to open the SSO
|
|
16
|
+
* @param client - The current Frak Client
|
|
17
|
+
* @param args - The SSO parameters
|
|
18
|
+
*
|
|
19
|
+
* @description Two SSO flow modes:
|
|
20
|
+
*
|
|
21
|
+
* **Redirect Mode** (openInSameWindow: true):
|
|
22
|
+
* - Wallet generates URL and triggers redirect
|
|
23
|
+
* - Used when redirectUrl is provided
|
|
24
|
+
*
|
|
25
|
+
* **Popup Mode** (openInSameWindow: false/omitted):
|
|
26
|
+
* - SDK generates URL client-side (or uses provided ssoPopupUrl)
|
|
27
|
+
* - Opens popup synchronously (prevents popup blockers)
|
|
28
|
+
* - Waits for SSO completion via postMessage
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* First we build the sso metadata
|
|
32
|
+
* ```ts
|
|
33
|
+
* // Build the metadata
|
|
34
|
+
* const metadata: SsoMetadata = {
|
|
35
|
+
* logoUrl: "https://my-app.com/logo.png",
|
|
36
|
+
* homepageLink: "https://my-app.com",
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* Then, either use it with direct exit (and so user is directly redirected to your website), or a custom redirect URL
|
|
41
|
+
* :::code-group
|
|
42
|
+
* ```ts [Popup (default)]
|
|
43
|
+
* // Opens in popup, SDK generates URL automatically
|
|
44
|
+
* await openSso(frakConfig, {
|
|
45
|
+
* directExit: true,
|
|
46
|
+
* metadata,
|
|
47
|
+
* });
|
|
48
|
+
* ```
|
|
49
|
+
* ```ts [Redirect]
|
|
50
|
+
* // Opens in same window with redirect
|
|
51
|
+
* await openSso(frakConfig, {
|
|
52
|
+
* redirectUrl: "https://my-app.com/frak-sso",
|
|
53
|
+
* metadata,
|
|
54
|
+
* openInSameWindow: true,
|
|
55
|
+
* });
|
|
56
|
+
* ```
|
|
57
|
+
* ```ts [Custom popup URL]
|
|
58
|
+
* // Advanced: provide custom SSO URL
|
|
59
|
+
* const { ssoUrl } = await prepareSso(frakConfig, { metadata });
|
|
60
|
+
* await openSso(frakConfig, {
|
|
61
|
+
* metadata,
|
|
62
|
+
* ssoPopupUrl: `${ssoUrl}&custom=param`,
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
* :::
|
|
66
|
+
*/
|
|
67
|
+
export async function openSso(
|
|
68
|
+
client: FrakClient,
|
|
69
|
+
args: OpenSsoParamsType
|
|
70
|
+
): Promise<OpenSsoReturnType> {
|
|
71
|
+
const { metadata, customizations, walletUrl } = client.config;
|
|
72
|
+
|
|
73
|
+
// Check if redirect mode (default to true if redirectUrl present)
|
|
74
|
+
const isRedirectMode = args.openInSameWindow ?? !!args.redirectUrl;
|
|
75
|
+
|
|
76
|
+
if (isRedirectMode) {
|
|
77
|
+
// Redirect flow: Wallet generates URL and triggers redirect via lifecycle event
|
|
78
|
+
// This must happen on wallet side because only the iframe can trigger the redirect
|
|
79
|
+
return await client.request({
|
|
80
|
+
method: "frak_openSso",
|
|
81
|
+
params: [args, metadata.name, customizations?.css],
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Popup flow: Generate URL on SDK side and open synchronously
|
|
86
|
+
// This ensures window.open() is called in same tick as user gesture (no popup blocker)
|
|
87
|
+
|
|
88
|
+
// Step 1: Generate or use provided SSO URL
|
|
89
|
+
const ssoUrl =
|
|
90
|
+
args.ssoPopupUrl ??
|
|
91
|
+
generateSsoUrl(
|
|
92
|
+
walletUrl ?? "https://wallet.frak.id",
|
|
93
|
+
args,
|
|
94
|
+
computeProductId(),
|
|
95
|
+
metadata.name,
|
|
96
|
+
customizations?.css
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
// Step 2: Open popup synchronously (critical for popup blocker prevention)
|
|
100
|
+
const popup = window.open(ssoUrl, ssoPopupName, ssoPopupFeatures);
|
|
101
|
+
if (!popup) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
"Popup was blocked. Please allow popups for this site."
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
popup.focus();
|
|
107
|
+
|
|
108
|
+
// Step 3: Wait for SSO completion via RPC
|
|
109
|
+
// The wallet iframe will resolve this when SSO page sends sso_complete message
|
|
110
|
+
const result = await client.request({
|
|
111
|
+
method: "frak_openSso",
|
|
112
|
+
params: [args, metadata.name, customizations?.css],
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return result ?? {};
|
|
116
|
+
}
|
|
@@ -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 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 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
|
+
}
|