@frak-labs/core-sdk 0.0.19 → 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/cdn/bundle.js +5 -5
- package/dist/actions.cjs +1 -1
- package/dist/actions.d.cts +154 -73
- package/dist/actions.d.ts +154 -73
- package/dist/actions.js +1 -1
- package/dist/bundle.cjs +2 -2
- package/dist/bundle.d.cts +257 -192
- package/dist/bundle.d.ts +257 -192
- package/dist/bundle.js +2 -2
- package/dist/index.cjs +11 -11
- package/dist/index.d.cts +199 -185
- package/dist/index.d.ts +199 -185
- package/dist/index.js +2 -2
- package/package.json +7 -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
|
@@ -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
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function used to track the status of a purchase
|
|
3
|
+
* when a purchase is tracked, the `purchaseCompleted` interactions will be automatically send for the user when we receive the purchase confirmation via webhook.
|
|
4
|
+
*
|
|
5
|
+
* @param args.customerId - The customer id that made the purchase (on your side)
|
|
6
|
+
* @param args.orderId - The order id of the purchase (on your side)
|
|
7
|
+
* @param args.token - The token of the purchase
|
|
8
|
+
*
|
|
9
|
+
* @description This function will send a request to the backend to listen for the purchase status.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* async function trackPurchase(checkout) {
|
|
13
|
+
* const payload = {
|
|
14
|
+
* customerId: checkout.order.customer.id,
|
|
15
|
+
* orderId: checkout.order.id,
|
|
16
|
+
* token: checkout.token,
|
|
17
|
+
* };
|
|
18
|
+
*
|
|
19
|
+
* await trackPurchaseStatus(payload);
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* - The `trackPurchaseStatus` function requires the `frak-wallet-interaction-token` stored in the session storage to authenticate the request.
|
|
24
|
+
* - This function will print a warning if used in a non-browser environment or if the wallet interaction token is not available.
|
|
25
|
+
*/
|
|
26
|
+
export async function trackPurchaseStatus(args: {
|
|
27
|
+
customerId: string | number;
|
|
28
|
+
orderId: string | number;
|
|
29
|
+
token: string;
|
|
30
|
+
}) {
|
|
31
|
+
if (typeof window === "undefined") {
|
|
32
|
+
console.warn("[Frak] No window found, can't track purchase");
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const interactionToken = window.sessionStorage.getItem(
|
|
36
|
+
"frak-wallet-interaction-token"
|
|
37
|
+
);
|
|
38
|
+
if (!interactionToken) {
|
|
39
|
+
console.warn("[Frak] No frak session found, skipping purchase check");
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Submit the listening request
|
|
44
|
+
await fetch("https://backend.frak.id/interactions/listenForPurchase", {
|
|
45
|
+
method: "POST",
|
|
46
|
+
headers: {
|
|
47
|
+
Accept: "application/json",
|
|
48
|
+
"Content-Type": "application/json",
|
|
49
|
+
"x-wallet-sdk-auth": interactionToken,
|
|
50
|
+
},
|
|
51
|
+
body: JSON.stringify(args),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Deferred } from "@frak-labs/frame-connector";
|
|
2
|
+
import type { FrakClient } from "../types/client";
|
|
3
|
+
import type { WalletStatusReturnType } from "../types/rpc/walletStatus";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Function used to watch the current frak wallet status
|
|
7
|
+
* @param client - The current Frak Client
|
|
8
|
+
* @param callback - The callback that will receive any wallet status change
|
|
9
|
+
* @returns A promise resolving with the initial wallet status
|
|
10
|
+
*
|
|
11
|
+
* @description This function will return the current wallet status, and will listen to any change in the wallet status.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* await watchWalletStatus(frakConfig, (status: WalletStatusReturnType) => {
|
|
15
|
+
* if (status.key === "connected") {
|
|
16
|
+
* console.log("Wallet connected:", status.wallet);
|
|
17
|
+
* console.log("Current interaction session:", status.interactionSession);
|
|
18
|
+
* } else {
|
|
19
|
+
* console.log("Wallet not connected");
|
|
20
|
+
* }
|
|
21
|
+
* });
|
|
22
|
+
*/
|
|
23
|
+
export function watchWalletStatus(
|
|
24
|
+
client: FrakClient,
|
|
25
|
+
callback?: (status: WalletStatusReturnType) => void
|
|
26
|
+
): Promise<WalletStatusReturnType> {
|
|
27
|
+
// If no callback is provided, just do a request with deferred result
|
|
28
|
+
if (!callback) {
|
|
29
|
+
return client
|
|
30
|
+
.request({ method: "frak_listenToWalletStatus" })
|
|
31
|
+
.then((result) => {
|
|
32
|
+
// Handle side effects of this request
|
|
33
|
+
walletStatusSideEffect(client, result);
|
|
34
|
+
|
|
35
|
+
// Return the result
|
|
36
|
+
return result;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Otherwise, listen to the wallet status and return the first one received
|
|
41
|
+
const firstResult = new Deferred<WalletStatusReturnType>();
|
|
42
|
+
let hasResolved = false;
|
|
43
|
+
|
|
44
|
+
// Start the listening request, and return the first result
|
|
45
|
+
client.listenerRequest(
|
|
46
|
+
{
|
|
47
|
+
method: "frak_listenToWalletStatus",
|
|
48
|
+
},
|
|
49
|
+
(status) => {
|
|
50
|
+
// Handle side effects of this request
|
|
51
|
+
walletStatusSideEffect(client, status);
|
|
52
|
+
|
|
53
|
+
// Transmit the status to the callback
|
|
54
|
+
callback(status);
|
|
55
|
+
|
|
56
|
+
// If the promise hasn't resolved yet, resolve it
|
|
57
|
+
if (!hasResolved) {
|
|
58
|
+
firstResult.resolve(status);
|
|
59
|
+
hasResolved = true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return firstResult.promise;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Helper to save a potential interaction token
|
|
69
|
+
* @param interactionToken
|
|
70
|
+
*/
|
|
71
|
+
function walletStatusSideEffect(
|
|
72
|
+
client: FrakClient,
|
|
73
|
+
status: WalletStatusReturnType
|
|
74
|
+
) {
|
|
75
|
+
if (typeof window === "undefined") {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Update the global properties
|
|
80
|
+
client.openPanel?.setGlobalProperties({
|
|
81
|
+
wallet: status.wallet ?? null,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
if (status.interactionToken) {
|
|
85
|
+
// If we got an interaction token, save it
|
|
86
|
+
window.sessionStorage.setItem(
|
|
87
|
+
"frak-wallet-interaction-token",
|
|
88
|
+
status.interactionToken
|
|
89
|
+
);
|
|
90
|
+
} else {
|
|
91
|
+
// Otherwise, remove it
|
|
92
|
+
window.sessionStorage.removeItem("frak-wallet-interaction-token");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DisplayModalParamsType,
|
|
3
|
+
FinalActionType,
|
|
4
|
+
FinalModalStepType,
|
|
5
|
+
FrakClient,
|
|
6
|
+
LoginModalStepType,
|
|
7
|
+
ModalRpcMetadata,
|
|
8
|
+
ModalRpcStepsResultType,
|
|
9
|
+
ModalStepTypes,
|
|
10
|
+
OpenInteractionSessionModalStepType,
|
|
11
|
+
SendTransactionModalStepType,
|
|
12
|
+
} from "../../types";
|
|
13
|
+
import { displayModal } from "../displayModal";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Represent the type of the modal step builder
|
|
17
|
+
*/
|
|
18
|
+
export type ModalStepBuilder<
|
|
19
|
+
Steps extends ModalStepTypes[] = ModalStepTypes[],
|
|
20
|
+
> = {
|
|
21
|
+
/**
|
|
22
|
+
* The current modal params
|
|
23
|
+
*/
|
|
24
|
+
params: DisplayModalParamsType<Steps>;
|
|
25
|
+
/**
|
|
26
|
+
* Add a send transaction step to the modal
|
|
27
|
+
*/
|
|
28
|
+
sendTx: (
|
|
29
|
+
options: SendTransactionModalStepType["params"]
|
|
30
|
+
) => ModalStepBuilder<[...Steps, SendTransactionModalStepType]>;
|
|
31
|
+
/**
|
|
32
|
+
* Add a final step of type reward to the modal
|
|
33
|
+
*/
|
|
34
|
+
reward: (
|
|
35
|
+
options?: Omit<FinalModalStepType["params"], "action">
|
|
36
|
+
) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
|
|
37
|
+
/**
|
|
38
|
+
* Add a final step of type sharing to the modal
|
|
39
|
+
*/
|
|
40
|
+
sharing: (
|
|
41
|
+
sharingOptions?: Extract<
|
|
42
|
+
FinalActionType,
|
|
43
|
+
{ key: "sharing" }
|
|
44
|
+
>["options"],
|
|
45
|
+
options?: Omit<FinalModalStepType["params"], "action">
|
|
46
|
+
) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
|
|
47
|
+
/**
|
|
48
|
+
* Display the modal
|
|
49
|
+
* @param metadataOverride - Function returning optional metadata to override the current modal metadata
|
|
50
|
+
*/
|
|
51
|
+
display: (
|
|
52
|
+
metadataOverride?: (
|
|
53
|
+
current?: ModalRpcMetadata
|
|
54
|
+
) => ModalRpcMetadata | undefined
|
|
55
|
+
) => Promise<ModalRpcStepsResultType<Steps>>;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Represent the output type of the modal builder
|
|
60
|
+
*/
|
|
61
|
+
export type ModalBuilder = ModalStepBuilder<
|
|
62
|
+
[LoginModalStepType, OpenInteractionSessionModalStepType]
|
|
63
|
+
>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Helper to craft Frak modal, and share a base initial config
|
|
67
|
+
* @param client - The current Frak Client
|
|
68
|
+
* @param args
|
|
69
|
+
* @param args.metadata - Common modal metadata (customisation, language etc)
|
|
70
|
+
* @param args.login - Login step parameters
|
|
71
|
+
* @param args.openSession - Open session step parameters
|
|
72
|
+
*
|
|
73
|
+
* @description This function will create a modal builder with the provided metadata, login and open session parameters.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* Here is an example of how to use the `modalBuilder` to create and display a sharing modal:
|
|
77
|
+
*
|
|
78
|
+
* ```js
|
|
79
|
+
* // Create the modal builder
|
|
80
|
+
* const modalBuilder = window.FrakSDK.modalBuilder(frakClient, baseModalConfig);
|
|
81
|
+
*
|
|
82
|
+
* // Configure the information to be shared via the sharing link
|
|
83
|
+
* const sharingConfig = {
|
|
84
|
+
* popupTitle: "Share this with your friends",
|
|
85
|
+
* text: "Discover our product!",
|
|
86
|
+
* link: window.location.href,
|
|
87
|
+
* };
|
|
88
|
+
*
|
|
89
|
+
* // Display the sharing modal
|
|
90
|
+
* function modalShare() {
|
|
91
|
+
* modalBuilder.sharing(sharingConfig).display();
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @see {@link ModalStepTypes} for more info about each modal step types and their parameters
|
|
96
|
+
* @see {@link ModalRpcMetadata} for more info about the metadata that can be passed to the modal
|
|
97
|
+
* @see {@link ModalRpcStepsResultType} for more info about the result of each modal steps
|
|
98
|
+
* @see {@link displayModal} for more info about how the modal is displayed
|
|
99
|
+
*/
|
|
100
|
+
export function modalBuilder(
|
|
101
|
+
client: FrakClient,
|
|
102
|
+
{
|
|
103
|
+
metadata,
|
|
104
|
+
login,
|
|
105
|
+
openSession,
|
|
106
|
+
}: {
|
|
107
|
+
metadata?: ModalRpcMetadata;
|
|
108
|
+
login?: LoginModalStepType["params"];
|
|
109
|
+
openSession?: OpenInteractionSessionModalStepType["params"];
|
|
110
|
+
}
|
|
111
|
+
): ModalBuilder {
|
|
112
|
+
// Build the initial modal params
|
|
113
|
+
const baseParams: DisplayModalParamsType<
|
|
114
|
+
[LoginModalStepType, OpenInteractionSessionModalStepType]
|
|
115
|
+
> = {
|
|
116
|
+
steps: {
|
|
117
|
+
login: login ?? {},
|
|
118
|
+
openSession: openSession ?? {},
|
|
119
|
+
},
|
|
120
|
+
metadata,
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// Return the step builder
|
|
124
|
+
return modalStepsBuilder(client, baseParams);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Modal step builder, allowing to add new steps to the modal, and to build and display it
|
|
129
|
+
*/
|
|
130
|
+
function modalStepsBuilder<CurrentSteps extends ModalStepTypes[]>(
|
|
131
|
+
client: FrakClient,
|
|
132
|
+
params: DisplayModalParamsType<CurrentSteps>
|
|
133
|
+
): ModalStepBuilder<CurrentSteps> {
|
|
134
|
+
// Function add the send tx step
|
|
135
|
+
function sendTx(options: SendTransactionModalStepType["params"]) {
|
|
136
|
+
return modalStepsBuilder<
|
|
137
|
+
[...CurrentSteps, SendTransactionModalStepType]
|
|
138
|
+
>(client, {
|
|
139
|
+
...params,
|
|
140
|
+
steps: {
|
|
141
|
+
...params.steps,
|
|
142
|
+
sendTransaction: options,
|
|
143
|
+
},
|
|
144
|
+
} as DisplayModalParamsType<
|
|
145
|
+
[...CurrentSteps, SendTransactionModalStepType]
|
|
146
|
+
>);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Function to add a reward step at the end
|
|
150
|
+
function reward(options?: Omit<FinalModalStepType["params"], "action">) {
|
|
151
|
+
return modalStepsBuilder<[...CurrentSteps, FinalModalStepType]>(
|
|
152
|
+
client,
|
|
153
|
+
{
|
|
154
|
+
...params,
|
|
155
|
+
steps: {
|
|
156
|
+
...params.steps,
|
|
157
|
+
final: {
|
|
158
|
+
...options,
|
|
159
|
+
action: { key: "reward" },
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
} as DisplayModalParamsType<[...CurrentSteps, FinalModalStepType]>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Function to add sharing step at the end
|
|
167
|
+
function sharing(
|
|
168
|
+
sharingOptions?: Extract<
|
|
169
|
+
FinalActionType,
|
|
170
|
+
{ key: "sharing" }
|
|
171
|
+
>["options"],
|
|
172
|
+
options?: Omit<FinalModalStepType["params"], "action">
|
|
173
|
+
) {
|
|
174
|
+
return modalStepsBuilder<[...CurrentSteps, FinalModalStepType]>(
|
|
175
|
+
client,
|
|
176
|
+
{
|
|
177
|
+
...params,
|
|
178
|
+
steps: {
|
|
179
|
+
...params.steps,
|
|
180
|
+
final: {
|
|
181
|
+
...options,
|
|
182
|
+
action: { key: "sharing", options: sharingOptions },
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
} as DisplayModalParamsType<[...CurrentSteps, FinalModalStepType]>
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Function to display it
|
|
190
|
+
async function display(
|
|
191
|
+
metadataOverride?: (
|
|
192
|
+
current?: ModalRpcMetadata
|
|
193
|
+
) => ModalRpcMetadata | undefined
|
|
194
|
+
) {
|
|
195
|
+
// If we have a metadata override, apply it
|
|
196
|
+
if (metadataOverride) {
|
|
197
|
+
params.metadata = metadataOverride(params.metadata ?? {});
|
|
198
|
+
}
|
|
199
|
+
return await displayModal(client, params);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
// Access current modal params
|
|
204
|
+
params,
|
|
205
|
+
// Function to add new steps
|
|
206
|
+
sendTx,
|
|
207
|
+
reward,
|
|
208
|
+
sharing,
|
|
209
|
+
// Display the modal
|
|
210
|
+
display,
|
|
211
|
+
};
|
|
212
|
+
}
|