@frak-labs/core-sdk 0.1.0-beta.afa252b0 → 0.1.0-beta.c7e026e5
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.iife.js +14 -0
- package/dist/actions-CEEObPYc.js +1 -0
- package/dist/actions-DbQhWYx8.cjs +1 -0
- package/dist/actions.cjs +1 -1
- package/dist/actions.d.cts +3 -1481
- package/dist/actions.d.ts +3 -1481
- package/dist/actions.js +1 -1
- package/dist/bundle.cjs +1 -13
- package/dist/bundle.d.cts +6 -2087
- package/dist/bundle.d.ts +6 -2087
- package/dist/bundle.js +1 -13
- package/dist/index-7OZ39x1U.d.ts +195 -0
- package/dist/index-C6FxkWPC.d.cts +511 -0
- package/dist/index-UFX7xCg3.d.ts +351 -0
- package/dist/index-d8xS4ryI.d.ts +511 -0
- package/dist/index-p4FqSp8z.d.cts +351 -0
- package/dist/index-zDq-VlKx.d.cts +195 -0
- package/dist/index.cjs +1 -13
- package/dist/index.d.cts +4 -1387
- package/dist/index.d.ts +4 -1387
- package/dist/index.js +1 -13
- package/dist/interaction-DMJ3ZfaF.d.cts +45 -0
- package/dist/interaction-KX1h9a7V.d.ts +45 -0
- package/dist/interactions-DnfM3oe0.js +1 -0
- package/dist/interactions-EIXhNLf6.cjs +1 -0
- package/dist/interactions.cjs +1 -1
- package/dist/interactions.d.cts +2 -182
- package/dist/interactions.d.ts +2 -182
- package/dist/interactions.js +1 -1
- package/dist/openSso-D--Airj6.d.cts +1018 -0
- package/dist/openSso-DsKJ4y0j.d.ts +1018 -0
- package/dist/productTypes-BUkXJKZ7.cjs +1 -0
- package/dist/productTypes-CGb1MmBF.js +1 -0
- package/dist/src-B_xO0AR6.cjs +13 -0
- package/dist/src-D2d52OZa.js +13 -0
- package/dist/trackEvent-CHnYa85W.js +1 -0
- package/dist/trackEvent-GuQm_1Nm.cjs +1 -0
- package/package.json +23 -18
- package/src/actions/displayEmbeddedWallet.test.ts +194 -0
- package/src/actions/displayEmbeddedWallet.ts +20 -0
- package/src/actions/displayModal.test.ts +387 -0
- package/src/actions/displayModal.ts +131 -0
- package/src/actions/getProductInformation.test.ts +133 -0
- package/src/actions/getProductInformation.ts +14 -0
- package/src/actions/index.ts +29 -0
- package/src/actions/openSso.test.ts +407 -0
- package/src/actions/openSso.ts +116 -0
- package/src/actions/prepareSso.test.ts +223 -0
- package/src/actions/prepareSso.ts +48 -0
- package/src/actions/referral/processReferral.ts +230 -0
- package/src/actions/referral/referralInteraction.test.ts +153 -0
- package/src/actions/referral/referralInteraction.ts +57 -0
- package/src/actions/sendInteraction.test.ts +219 -0
- package/src/actions/sendInteraction.ts +32 -0
- package/src/actions/trackPurchaseStatus.test.ts +287 -0
- package/src/actions/trackPurchaseStatus.ts +53 -0
- package/src/actions/watchWalletStatus.test.ts +372 -0
- package/src/actions/watchWalletStatus.ts +94 -0
- package/src/actions/wrapper/modalBuilder.test.ts +253 -0
- package/src/actions/wrapper/modalBuilder.ts +212 -0
- package/src/actions/wrapper/sendTransaction.test.ts +164 -0
- package/src/actions/wrapper/sendTransaction.ts +62 -0
- package/src/actions/wrapper/siweAuthenticate.test.ts +290 -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.test.ts +343 -0
- package/src/clients/setupClient.ts +73 -0
- package/src/clients/transports/iframeLifecycleManager.test.ts +399 -0
- package/src/clients/transports/iframeLifecycleManager.ts +90 -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 +101 -0
- package/src/interactions/index.ts +5 -0
- package/src/interactions/pressEncoder.test.ts +215 -0
- package/src/interactions/pressEncoder.ts +53 -0
- package/src/interactions/purchaseEncoder.test.ts +291 -0
- package/src/interactions/purchaseEncoder.ts +99 -0
- package/src/interactions/referralEncoder.test.ts +170 -0
- package/src/interactions/referralEncoder.ts +47 -0
- package/src/interactions/retailEncoder.test.ts +107 -0
- package/src/interactions/retailEncoder.ts +37 -0
- package/src/interactions/webshopEncoder.test.ts +56 -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 +71 -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.test.ts +407 -0
- package/src/utils/FrakContext.ts +158 -0
- package/src/utils/compression/b64.test.ts +181 -0
- package/src/utils/compression/b64.ts +29 -0
- package/src/utils/compression/compress.test.ts +123 -0
- package/src/utils/compression/compress.ts +11 -0
- package/src/utils/compression/decompress.test.ts +145 -0
- package/src/utils/compression/decompress.ts +11 -0
- package/src/utils/compression/index.ts +3 -0
- package/src/utils/computeProductId.test.ts +80 -0
- package/src/utils/computeProductId.ts +11 -0
- package/src/utils/constants.test.ts +23 -0
- package/src/utils/constants.ts +4 -0
- package/src/utils/formatAmount.test.ts +113 -0
- package/src/utils/formatAmount.ts +18 -0
- package/src/utils/getCurrencyAmountKey.test.ts +44 -0
- package/src/utils/getCurrencyAmountKey.ts +15 -0
- package/src/utils/getSupportedCurrency.test.ts +51 -0
- package/src/utils/getSupportedCurrency.ts +14 -0
- package/src/utils/getSupportedLocale.test.ts +64 -0
- package/src/utils/getSupportedLocale.ts +16 -0
- package/src/utils/iframeHelper.test.ts +450 -0
- package/src/utils/iframeHelper.ts +143 -0
- package/src/utils/index.ts +21 -0
- package/src/utils/sso.test.ts +361 -0
- package/src/utils/sso.ts +119 -0
- package/src/utils/ssoUrlListener.ts +60 -0
- package/src/utils/trackEvent.test.ts +162 -0
- package/src/utils/trackEvent.ts +26 -0
- package/cdn/bundle.js +0 -19
- package/cdn/bundle.js.LICENSE.txt +0 -10
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("../displayModal", () => ({
|
|
4
|
+
displayModal: vi.fn(),
|
|
5
|
+
}));
|
|
6
|
+
|
|
7
|
+
import type { Address } from "viem";
|
|
8
|
+
import { describe, expect, it } from "../../../tests/vitest-fixtures";
|
|
9
|
+
import type { FrakClient } from "../../types";
|
|
10
|
+
import { modalBuilder } from "./modalBuilder";
|
|
11
|
+
|
|
12
|
+
describe("modalBuilder", () => {
|
|
13
|
+
const mockClient = {
|
|
14
|
+
config: {
|
|
15
|
+
metadata: {
|
|
16
|
+
name: "Test App",
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
request: vi.fn(),
|
|
20
|
+
} as unknown as FrakClient;
|
|
21
|
+
|
|
22
|
+
describe("initialization", () => {
|
|
23
|
+
it("should create builder with base params", () => {
|
|
24
|
+
const builder = modalBuilder(mockClient, {});
|
|
25
|
+
|
|
26
|
+
expect(builder.params).toBeDefined();
|
|
27
|
+
expect(builder.params.steps.login).toEqual({});
|
|
28
|
+
expect(builder.params.steps.openSession).toEqual({});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should create builder with custom login params", () => {
|
|
32
|
+
const builder = modalBuilder(mockClient, {
|
|
33
|
+
login: { allowSso: true },
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
expect(builder.params.steps.login).toEqual({ allowSso: true });
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should create builder with custom openSession params", () => {
|
|
40
|
+
const builder = modalBuilder(mockClient, {
|
|
41
|
+
openSession: {},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(builder.params.steps.openSession).toEqual({});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should create builder with metadata", () => {
|
|
48
|
+
const builder = modalBuilder(mockClient, {
|
|
49
|
+
metadata: {
|
|
50
|
+
header: { title: "Test Title" },
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
expect(builder.params.metadata).toEqual({
|
|
55
|
+
header: { title: "Test Title" },
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("sendTx step", () => {
|
|
61
|
+
it("should add sendTransaction step", () => {
|
|
62
|
+
const builder = modalBuilder(mockClient, {});
|
|
63
|
+
const withTx = builder.sendTx({
|
|
64
|
+
tx: {
|
|
65
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
66
|
+
data: "0xdata" as `0x${string}`,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(withTx.params.steps.sendTransaction).toEqual({
|
|
71
|
+
tx: {
|
|
72
|
+
to: "0x1234567890123456789012345678901234567890",
|
|
73
|
+
data: "0xdata",
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should preserve previous steps when adding sendTx", () => {
|
|
79
|
+
const builder = modalBuilder(mockClient, {
|
|
80
|
+
login: { allowSso: true },
|
|
81
|
+
});
|
|
82
|
+
const withTx = builder.sendTx({
|
|
83
|
+
tx: {
|
|
84
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(withTx.params.steps.login).toEqual({ allowSso: true });
|
|
89
|
+
expect(withTx.params.steps.sendTransaction).toBeDefined();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe("reward step", () => {
|
|
94
|
+
it("should add reward final step", () => {
|
|
95
|
+
const builder = modalBuilder(mockClient, {});
|
|
96
|
+
const withReward = builder.reward();
|
|
97
|
+
|
|
98
|
+
expect(withReward.params.steps.final).toEqual({
|
|
99
|
+
action: { key: "reward" },
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should add reward step with options", () => {
|
|
104
|
+
const builder = modalBuilder(mockClient, {});
|
|
105
|
+
const withReward = builder.reward({
|
|
106
|
+
autoSkip: true,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
expect(withReward.params.steps.final).toEqual({
|
|
110
|
+
autoSkip: true,
|
|
111
|
+
action: { key: "reward" },
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("sharing step", () => {
|
|
117
|
+
it("should add sharing final step", () => {
|
|
118
|
+
const builder = modalBuilder(mockClient, {});
|
|
119
|
+
const withSharing = builder.sharing({
|
|
120
|
+
popupTitle: "Share!",
|
|
121
|
+
text: "Check this out",
|
|
122
|
+
link: "https://example.com",
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
expect(withSharing.params.steps.final).toEqual({
|
|
126
|
+
action: {
|
|
127
|
+
key: "sharing",
|
|
128
|
+
options: {
|
|
129
|
+
popupTitle: "Share!",
|
|
130
|
+
text: "Check this out",
|
|
131
|
+
link: "https://example.com",
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("should add sharing step with additional options", () => {
|
|
138
|
+
const builder = modalBuilder(mockClient, {});
|
|
139
|
+
const withSharing = builder.sharing(
|
|
140
|
+
{ text: "Share text", link: "https://example.com" },
|
|
141
|
+
{ autoSkip: false }
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
expect(withSharing.params.steps.final).toEqual({
|
|
145
|
+
autoSkip: false,
|
|
146
|
+
action: {
|
|
147
|
+
key: "sharing",
|
|
148
|
+
options: {
|
|
149
|
+
text: "Share text",
|
|
150
|
+
link: "https://example.com",
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("chaining", () => {
|
|
158
|
+
it("should chain sendTx and reward", () => {
|
|
159
|
+
const builder = modalBuilder(mockClient, {});
|
|
160
|
+
const chained = builder
|
|
161
|
+
.sendTx({
|
|
162
|
+
tx: {
|
|
163
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
164
|
+
},
|
|
165
|
+
})
|
|
166
|
+
.reward();
|
|
167
|
+
|
|
168
|
+
expect(chained.params.steps.sendTransaction).toBeDefined();
|
|
169
|
+
expect(chained.params.steps.final).toEqual({
|
|
170
|
+
action: { key: "reward" },
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("should chain sendTx and sharing", () => {
|
|
175
|
+
const builder = modalBuilder(mockClient, {});
|
|
176
|
+
const chained = builder
|
|
177
|
+
.sendTx({
|
|
178
|
+
tx: {
|
|
179
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
180
|
+
},
|
|
181
|
+
})
|
|
182
|
+
.sharing({ link: "https://example.com" });
|
|
183
|
+
|
|
184
|
+
expect(chained.params.steps.sendTransaction).toBeDefined();
|
|
185
|
+
expect(chained.params.steps.final?.action).toEqual({
|
|
186
|
+
key: "sharing",
|
|
187
|
+
options: { link: "https://example.com" },
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
describe("display", () => {
|
|
193
|
+
it("should call displayModal when display is invoked", async () => {
|
|
194
|
+
const { displayModal } = await import("../displayModal");
|
|
195
|
+
|
|
196
|
+
const mockResponse = {
|
|
197
|
+
login: {
|
|
198
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
199
|
+
},
|
|
200
|
+
openSession: {
|
|
201
|
+
startTimestamp: 1234567890,
|
|
202
|
+
endTimestamp: 1234567900,
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
vi.mocked(displayModal).mockResolvedValue(mockResponse as any);
|
|
206
|
+
|
|
207
|
+
const builder = modalBuilder(mockClient, {});
|
|
208
|
+
await builder.display();
|
|
209
|
+
|
|
210
|
+
expect(displayModal).toHaveBeenCalledWith(
|
|
211
|
+
mockClient,
|
|
212
|
+
builder.params
|
|
213
|
+
);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("should apply metadata override when provided", async () => {
|
|
217
|
+
const { displayModal } = await import("../displayModal");
|
|
218
|
+
|
|
219
|
+
vi.mocked(displayModal).mockResolvedValue({} as any);
|
|
220
|
+
|
|
221
|
+
const builder = modalBuilder(mockClient, {
|
|
222
|
+
metadata: { header: { title: "Original" } },
|
|
223
|
+
});
|
|
224
|
+
await builder.display(() => ({
|
|
225
|
+
header: { title: "Overridden" },
|
|
226
|
+
}));
|
|
227
|
+
|
|
228
|
+
expect(displayModal).toHaveBeenCalledWith(
|
|
229
|
+
mockClient,
|
|
230
|
+
expect.objectContaining({
|
|
231
|
+
metadata: { header: { title: "Overridden" } },
|
|
232
|
+
})
|
|
233
|
+
);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("should return displayModal result", async () => {
|
|
237
|
+
const { displayModal } = await import("../displayModal");
|
|
238
|
+
|
|
239
|
+
const mockResponse = {
|
|
240
|
+
login: {
|
|
241
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
242
|
+
},
|
|
243
|
+
openSession: {},
|
|
244
|
+
};
|
|
245
|
+
vi.mocked(displayModal).mockResolvedValue(mockResponse as any);
|
|
246
|
+
|
|
247
|
+
const builder = modalBuilder(mockClient, {});
|
|
248
|
+
const result = await builder.display();
|
|
249
|
+
|
|
250
|
+
expect(result).toEqual(mockResponse);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
});
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("../displayModal", () => ({
|
|
4
|
+
displayModal: vi.fn(),
|
|
5
|
+
}));
|
|
6
|
+
|
|
7
|
+
import type { Address, Hex } from "viem";
|
|
8
|
+
import { describe, expect, it } from "../../../tests/vitest-fixtures";
|
|
9
|
+
import type { FrakClient } from "../../types";
|
|
10
|
+
import { sendTransaction } from "./sendTransaction";
|
|
11
|
+
|
|
12
|
+
describe("sendTransaction", () => {
|
|
13
|
+
const mockClient = {
|
|
14
|
+
config: {
|
|
15
|
+
metadata: {
|
|
16
|
+
name: "Test App",
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
request: vi.fn(),
|
|
20
|
+
} as unknown as FrakClient;
|
|
21
|
+
|
|
22
|
+
describe("basic usage", () => {
|
|
23
|
+
it("should call displayModal with correct params for single tx", async () => {
|
|
24
|
+
const { displayModal } = await import("../displayModal");
|
|
25
|
+
|
|
26
|
+
const mockTxHash =
|
|
27
|
+
"0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as Hex;
|
|
28
|
+
vi.mocked(displayModal).mockResolvedValue({
|
|
29
|
+
login: {
|
|
30
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
31
|
+
},
|
|
32
|
+
sendTransaction: { hash: mockTxHash },
|
|
33
|
+
} as any);
|
|
34
|
+
|
|
35
|
+
const txParams = {
|
|
36
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
37
|
+
data: "0xdeadbeef" as Hex,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
await sendTransaction(mockClient, { tx: txParams });
|
|
41
|
+
|
|
42
|
+
expect(displayModal).toHaveBeenCalledWith(mockClient, {
|
|
43
|
+
metadata: undefined,
|
|
44
|
+
steps: {
|
|
45
|
+
login: {},
|
|
46
|
+
sendTransaction: { tx: txParams },
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("should return transaction hash", async () => {
|
|
52
|
+
const { displayModal } = await import("../displayModal");
|
|
53
|
+
|
|
54
|
+
const mockTxHash =
|
|
55
|
+
"0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as Hex;
|
|
56
|
+
vi.mocked(displayModal).mockResolvedValue({
|
|
57
|
+
login: {
|
|
58
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
59
|
+
},
|
|
60
|
+
sendTransaction: { hash: mockTxHash },
|
|
61
|
+
} as any);
|
|
62
|
+
|
|
63
|
+
const result = await sendTransaction(mockClient, {
|
|
64
|
+
tx: {
|
|
65
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
expect(result).toEqual({ hash: mockTxHash });
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("batched transactions", () => {
|
|
74
|
+
it("should handle multiple transactions", async () => {
|
|
75
|
+
const { displayModal } = await import("../displayModal");
|
|
76
|
+
|
|
77
|
+
const mockTxHash =
|
|
78
|
+
"0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as Hex;
|
|
79
|
+
vi.mocked(displayModal).mockResolvedValue({
|
|
80
|
+
login: {},
|
|
81
|
+
sendTransaction: { hash: mockTxHash },
|
|
82
|
+
} as any);
|
|
83
|
+
|
|
84
|
+
const txParams = [
|
|
85
|
+
{
|
|
86
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
87
|
+
data: "0xdata1" as Hex,
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
to: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd" as Address,
|
|
91
|
+
data: "0xdata2" as Hex,
|
|
92
|
+
},
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
await sendTransaction(mockClient, { tx: txParams });
|
|
96
|
+
|
|
97
|
+
expect(displayModal).toHaveBeenCalledWith(mockClient, {
|
|
98
|
+
metadata: undefined,
|
|
99
|
+
steps: {
|
|
100
|
+
login: {},
|
|
101
|
+
sendTransaction: { tx: txParams },
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("with metadata", () => {
|
|
108
|
+
it("should pass metadata to displayModal", async () => {
|
|
109
|
+
const { displayModal } = await import("../displayModal");
|
|
110
|
+
|
|
111
|
+
vi.mocked(displayModal).mockResolvedValue({
|
|
112
|
+
login: {},
|
|
113
|
+
sendTransaction: { hash: "0x123" as Hex },
|
|
114
|
+
} as any);
|
|
115
|
+
|
|
116
|
+
const metadata = {
|
|
117
|
+
header: {
|
|
118
|
+
title: "Send ETH",
|
|
119
|
+
icon: "https://example.com/icon.png",
|
|
120
|
+
},
|
|
121
|
+
context: "Send 100 wei to recipient",
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
await sendTransaction(mockClient, {
|
|
125
|
+
tx: {
|
|
126
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
127
|
+
value: "0x64" as Hex,
|
|
128
|
+
},
|
|
129
|
+
metadata,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
expect(displayModal).toHaveBeenCalledWith(mockClient, {
|
|
133
|
+
metadata,
|
|
134
|
+
steps: {
|
|
135
|
+
login: {},
|
|
136
|
+
sendTransaction: {
|
|
137
|
+
tx: {
|
|
138
|
+
to: "0x1234567890123456789012345678901234567890",
|
|
139
|
+
value: "0x64",
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe("error handling", () => {
|
|
148
|
+
it("should propagate errors from displayModal", async () => {
|
|
149
|
+
const { displayModal } = await import("../displayModal");
|
|
150
|
+
|
|
151
|
+
vi.mocked(displayModal).mockRejectedValue(
|
|
152
|
+
new Error("Transaction rejected")
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
await expect(
|
|
156
|
+
sendTransaction(mockClient, {
|
|
157
|
+
tx: {
|
|
158
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
159
|
+
},
|
|
160
|
+
})
|
|
161
|
+
).rejects.toThrow("Transaction rejected");
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FrakClient,
|
|
3
|
+
ModalRpcMetadata,
|
|
4
|
+
SendTransactionModalStepType,
|
|
5
|
+
SendTransactionReturnType,
|
|
6
|
+
} from "../../types";
|
|
7
|
+
import { displayModal } from "../displayModal";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Parameters to directly show a modal used to send a transaction
|
|
11
|
+
* @inline
|
|
12
|
+
*/
|
|
13
|
+
export type SendTransactionParams = {
|
|
14
|
+
/**
|
|
15
|
+
* The transaction to be sent (either a single tx or multiple ones)
|
|
16
|
+
*/
|
|
17
|
+
tx: SendTransactionModalStepType["params"]["tx"];
|
|
18
|
+
/**
|
|
19
|
+
* Custom metadata to be passed to the modal
|
|
20
|
+
*/
|
|
21
|
+
metadata?: ModalRpcMetadata;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Function used to send a user transaction, simple wrapper around the displayModal function to ease the send transaction process
|
|
26
|
+
* @param client - The current Frak Client
|
|
27
|
+
* @param args - The parameters
|
|
28
|
+
* @returns The hash of the transaction that was sent in a promise
|
|
29
|
+
*
|
|
30
|
+
* @description This function will display a modal to the user with the provided transaction and metadata.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const { hash } = await sendTransaction(frakConfig, {
|
|
34
|
+
* tx: {
|
|
35
|
+
* to: "0xdeadbeef",
|
|
36
|
+
* value: toHex(100n),
|
|
37
|
+
* },
|
|
38
|
+
* metadata: {
|
|
39
|
+
* header: {
|
|
40
|
+
* title: "Sending eth",
|
|
41
|
+
* },
|
|
42
|
+
* context: "Send 100wei to 0xdeadbeef",
|
|
43
|
+
* },
|
|
44
|
+
* });
|
|
45
|
+
* console.log("Transaction hash:", hash);
|
|
46
|
+
*/
|
|
47
|
+
export async function sendTransaction(
|
|
48
|
+
client: FrakClient,
|
|
49
|
+
{ tx, metadata }: SendTransactionParams
|
|
50
|
+
): Promise<SendTransactionReturnType> {
|
|
51
|
+
// Trigger a modal with login options
|
|
52
|
+
const result = await displayModal(client, {
|
|
53
|
+
metadata,
|
|
54
|
+
steps: {
|
|
55
|
+
login: {},
|
|
56
|
+
sendTransaction: { tx },
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Return the tx result only
|
|
61
|
+
return result.sendTransaction;
|
|
62
|
+
}
|