@frak-labs/core-sdk 1.1.3 → 1.1.5-beta.5b65e64f
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 +1 -1
- package/dist/actions-Cyn4asew.cjs +2 -0
- package/dist/actions-Cyn4asew.cjs.map +1 -0
- package/dist/actions-DtPVuk2e.js +2 -0
- package/dist/actions-DtPVuk2e.js.map +1 -0
- package/dist/actions.cjs +1 -1
- package/dist/actions.d.cts +3 -3
- package/dist/actions.d.ts +3 -3
- package/dist/actions.js +1 -1
- package/dist/bundle.cjs +1 -1
- package/dist/bundle.d.cts +5 -4
- package/dist/bundle.d.ts +5 -4
- package/dist/bundle.js +1 -1
- package/dist/frakContext-Bgidv-Se.js +2 -0
- package/dist/frakContext-Bgidv-Se.js.map +1 -0
- package/dist/frakContext-CeVlWFcX.cjs +2 -0
- package/dist/frakContext-CeVlWFcX.cjs.map +1 -0
- package/dist/getCurrencyAmountKey-DQcwjJiB.js +2 -0
- package/dist/getCurrencyAmountKey-DQcwjJiB.js.map +1 -0
- package/dist/getCurrencyAmountKey-h2eOiYzv.cjs +2 -0
- package/dist/getCurrencyAmountKey-h2eOiYzv.cjs.map +1 -0
- package/dist/{index-lcImYQyL.d.ts → index-BCoPwxjq.d.cts} +4 -2
- package/dist/{index-C5pP_9We.d.cts → index-CiHjUnfc.d.ts} +43 -6
- package/dist/{index-DrYZwIjb.d.ts → index-D25z5B9K.d.cts} +43 -6
- package/dist/{index-DR4n-QOe.d.cts → index-trhAN7q9.d.ts} +4 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +1 -1
- package/dist/merchantInformation--tLnowAN.d.ts +344 -0
- package/dist/merchantInformation-BSuahpjl.d.cts +344 -0
- package/dist/{openSso-BErs7_Bg.d.cts → openSso-CENLMWJx.d.ts} +4 -279
- package/dist/{openSso-DRLH4D9a.d.ts → openSso-CKvkAGts.d.cts} +4 -279
- package/dist/rewards.cjs +2 -0
- package/dist/rewards.cjs.map +1 -0
- package/dist/rewards.d.cts +101 -0
- package/dist/rewards.d.ts +101 -0
- package/dist/rewards.js +2 -0
- package/dist/rewards.js.map +1 -0
- package/dist/src-BXtnEE3a.js +2 -0
- package/dist/src-BXtnEE3a.js.map +1 -0
- package/dist/src-TpX-7MwJ.cjs +2 -0
- package/dist/src-TpX-7MwJ.cjs.map +1 -0
- package/package.json +16 -3
- package/src/actions/getMerchantInformation.test.ts +2 -0
- package/src/actions/referral/processReferral.test.ts +4 -4
- package/src/actions/referral/processReferral.ts +4 -4
- package/src/clients/createIFrameFrakClient.ts +7 -7
- package/src/clients/transports/iframeLifecycleManager.ts +5 -21
- package/src/config/sdkConfigStore.ts +6 -3
- package/src/context/frakContext.test.ts +55 -0
- package/src/context/frakContext.ts +13 -2
- package/src/index.ts +8 -0
- package/src/rewards/conditions.test.ts +60 -0
- package/src/rewards/conditions.ts +72 -0
- package/src/rewards/example.test.ts +92 -0
- package/src/rewards/example.ts +57 -0
- package/src/rewards/format.test.ts +143 -0
- package/src/rewards/format.ts +81 -0
- package/src/rewards/index.ts +27 -0
- package/src/rewards/select.test.ts +212 -0
- package/src/rewards/select.ts +132 -0
- package/src/rewards/value.test.ts +89 -0
- package/src/rewards/value.ts +73 -0
- package/src/types/index.ts +6 -0
- package/src/types/rpc/merchantInformation.ts +120 -13
- package/src/utils/analytics/events/lifecycle.ts +1 -1
- package/src/utils/analytics/events/referral.ts +3 -3
- package/src/utils/browser/deepLinkWithFallback.ts +4 -1
- package/src/utils/browser/inAppBrowser.ts +2 -2
- package/src/utils/cache/withCache.ts +30 -11
- package/src/utils/iframe/iframeHelper.test.ts +39 -0
- package/src/utils/iframe/iframeHelper.ts +31 -2
- package/src/utils/index.ts +5 -0
- package/src/utils/url/queryParams.test.ts +85 -0
- package/src/utils/url/queryParams.ts +57 -0
- package/dist/actions-D5rNeJC0.js +0 -1
- package/dist/actions-gmdNZvNh.cjs +0 -1
- package/dist/frakContext-Cfp7sSid.cjs +0 -1
- package/dist/frakContext-UEOk9Bk4.js +0 -1
- package/dist/src-BEMVYU3J.js +0 -1
- package/dist/src-C9inMs4I.cjs +0 -1
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import type { InteractionTypeKey } from "../constants/interactionTypes";
|
|
3
|
+
import type { EstimatedReward, MerchantReward, RuleConditions } from "../types";
|
|
4
|
+
import { formatBestReward, selectDisplayCampaign } from "./select";
|
|
5
|
+
|
|
6
|
+
const NOW = new Date("2025-01-15T00:00:00Z");
|
|
7
|
+
const unix = (iso: string) => Math.floor(new Date(iso).getTime() / 1000);
|
|
8
|
+
|
|
9
|
+
function fixedReward(eur: number): EstimatedReward {
|
|
10
|
+
return {
|
|
11
|
+
payoutType: "fixed",
|
|
12
|
+
amount: { amount: eur, eurAmount: eur, usdAmount: eur, gbpAmount: eur },
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function uncappedPercentage(percent: number): EstimatedReward {
|
|
17
|
+
return { payoutType: "percentage", percent, percentOf: "purchase_amount" };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function startsAtCondition(iso: string): RuleConditions {
|
|
21
|
+
return [{ field: "time.timestamp", operator: "gte", value: unix(iso) }];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function campaign(opts: {
|
|
25
|
+
id: string;
|
|
26
|
+
interactionTypeKey?: InteractionTypeKey;
|
|
27
|
+
referrer?: EstimatedReward;
|
|
28
|
+
referee?: EstimatedReward;
|
|
29
|
+
conditions?: RuleConditions;
|
|
30
|
+
expiresAt?: string | null;
|
|
31
|
+
defaultLockupSeconds?: number;
|
|
32
|
+
}): MerchantReward {
|
|
33
|
+
return {
|
|
34
|
+
campaignId: opts.id,
|
|
35
|
+
name: opts.id,
|
|
36
|
+
interactionTypeKey: opts.interactionTypeKey ?? "purchase",
|
|
37
|
+
conditions: opts.conditions ?? [],
|
|
38
|
+
referrer: opts.referrer,
|
|
39
|
+
referee: opts.referee,
|
|
40
|
+
expiresAt: opts.expiresAt,
|
|
41
|
+
defaultLockupSeconds: opts.defaultLockupSeconds,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe("selectDisplayCampaign", () => {
|
|
46
|
+
it("returns undefined for an empty set", () => {
|
|
47
|
+
expect(selectDisplayCampaign([], { now: NOW })).toBeUndefined();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("picks the highest-reward live campaign", () => {
|
|
51
|
+
const result = selectDisplayCampaign(
|
|
52
|
+
[
|
|
53
|
+
campaign({ id: "low", referrer: fixedReward(2) }),
|
|
54
|
+
campaign({ id: "high", referrer: fixedReward(9) }),
|
|
55
|
+
],
|
|
56
|
+
{ now: NOW }
|
|
57
|
+
);
|
|
58
|
+
expect(result?.status).toBe("live");
|
|
59
|
+
expect(result?.campaign.campaignId).toBe("high");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("prefers a live campaign over a richer upcoming one", () => {
|
|
63
|
+
const result = selectDisplayCampaign(
|
|
64
|
+
[
|
|
65
|
+
campaign({
|
|
66
|
+
id: "upcoming-rich",
|
|
67
|
+
referrer: fixedReward(50),
|
|
68
|
+
conditions: startsAtCondition("2025-03-01T00:00:00Z"),
|
|
69
|
+
}),
|
|
70
|
+
campaign({ id: "live-poor", referrer: fixedReward(3) }),
|
|
71
|
+
],
|
|
72
|
+
{ now: NOW }
|
|
73
|
+
);
|
|
74
|
+
expect(result?.status).toBe("live");
|
|
75
|
+
expect(result?.campaign.campaignId).toBe("live-poor");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("falls back to the soonest-starting upcoming campaign", () => {
|
|
79
|
+
const result = selectDisplayCampaign(
|
|
80
|
+
[
|
|
81
|
+
campaign({
|
|
82
|
+
id: "later",
|
|
83
|
+
referrer: fixedReward(50),
|
|
84
|
+
conditions: startsAtCondition("2025-04-01T00:00:00Z"),
|
|
85
|
+
}),
|
|
86
|
+
campaign({
|
|
87
|
+
id: "sooner",
|
|
88
|
+
referrer: fixedReward(5),
|
|
89
|
+
conditions: startsAtCondition("2025-02-01T00:00:00Z"),
|
|
90
|
+
}),
|
|
91
|
+
],
|
|
92
|
+
{ now: NOW }
|
|
93
|
+
);
|
|
94
|
+
expect(result?.status).toBe("upcoming");
|
|
95
|
+
expect(result?.campaign.campaignId).toBe("sooner");
|
|
96
|
+
expect(result?.startsAt?.toISOString()).toBe(
|
|
97
|
+
"2025-02-01T00:00:00.000Z"
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("skips expired campaigns", () => {
|
|
102
|
+
const result = selectDisplayCampaign(
|
|
103
|
+
[
|
|
104
|
+
campaign({
|
|
105
|
+
id: "expired",
|
|
106
|
+
referrer: fixedReward(99),
|
|
107
|
+
expiresAt: "2025-01-01T00:00:00Z",
|
|
108
|
+
}),
|
|
109
|
+
campaign({ id: "active", referrer: fixedReward(4) }),
|
|
110
|
+
],
|
|
111
|
+
{ now: NOW }
|
|
112
|
+
);
|
|
113
|
+
expect(result?.campaign.campaignId).toBe("active");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("only considers campaigns matching targetInteraction", () => {
|
|
117
|
+
const result = selectDisplayCampaign(
|
|
118
|
+
[
|
|
119
|
+
campaign({
|
|
120
|
+
id: "referral-rich",
|
|
121
|
+
interactionTypeKey: "referral",
|
|
122
|
+
referrer: fixedReward(50),
|
|
123
|
+
}),
|
|
124
|
+
campaign({
|
|
125
|
+
id: "purchase-poor",
|
|
126
|
+
interactionTypeKey: "purchase",
|
|
127
|
+
referrer: fixedReward(4),
|
|
128
|
+
}),
|
|
129
|
+
],
|
|
130
|
+
{ now: NOW, targetInteraction: "purchase" }
|
|
131
|
+
);
|
|
132
|
+
expect(result?.campaign.campaignId).toBe("purchase-poor");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("ranks by the referee side when audience is 'referee'", () => {
|
|
136
|
+
const rewards = [
|
|
137
|
+
campaign({
|
|
138
|
+
id: "rich-referrer",
|
|
139
|
+
referrer: fixedReward(50),
|
|
140
|
+
referee: fixedReward(2),
|
|
141
|
+
}),
|
|
142
|
+
campaign({
|
|
143
|
+
id: "rich-referee",
|
|
144
|
+
referrer: fixedReward(1),
|
|
145
|
+
referee: fixedReward(9),
|
|
146
|
+
}),
|
|
147
|
+
];
|
|
148
|
+
expect(
|
|
149
|
+
selectDisplayCampaign(rewards, { now: NOW })?.campaign.campaignId
|
|
150
|
+
).toBe("rich-referrer");
|
|
151
|
+
expect(
|
|
152
|
+
selectDisplayCampaign(rewards, { now: NOW, audience: "referee" })
|
|
153
|
+
?.campaign.campaignId
|
|
154
|
+
).toBe("rich-referee");
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe("formatBestReward", () => {
|
|
159
|
+
it("returns undefined for an empty set", () => {
|
|
160
|
+
expect(formatBestReward([], { now: NOW })).toBeUndefined();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("formats the selected campaign's referrer reward by default", () => {
|
|
164
|
+
const formatted = formatBestReward(
|
|
165
|
+
[
|
|
166
|
+
campaign({ id: "low", referrer: fixedReward(2) }),
|
|
167
|
+
campaign({ id: "high", referrer: fixedReward(50) }),
|
|
168
|
+
],
|
|
169
|
+
{ now: NOW }
|
|
170
|
+
);
|
|
171
|
+
expect(formatted).toContain("50");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("formats the referee reward when audience is 'referee'", () => {
|
|
175
|
+
const formatted = formatBestReward(
|
|
176
|
+
[
|
|
177
|
+
campaign({
|
|
178
|
+
id: "c",
|
|
179
|
+
referrer: fixedReward(50),
|
|
180
|
+
referee: fixedReward(3),
|
|
181
|
+
}),
|
|
182
|
+
],
|
|
183
|
+
{ now: NOW, audience: "referee" }
|
|
184
|
+
);
|
|
185
|
+
expect(formatted).toContain("3");
|
|
186
|
+
expect(formatted).not.toContain("50");
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("ignores expired campaigns (does not advertise a stale reward)", () => {
|
|
190
|
+
expect(
|
|
191
|
+
formatBestReward(
|
|
192
|
+
[
|
|
193
|
+
campaign({
|
|
194
|
+
id: "expired",
|
|
195
|
+
referrer: fixedReward(99),
|
|
196
|
+
expiresAt: "2025-01-01T00:00:00Z",
|
|
197
|
+
}),
|
|
198
|
+
],
|
|
199
|
+
{ now: NOW }
|
|
200
|
+
)
|
|
201
|
+
).toBeUndefined();
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("renders an uncapped percentage reward as a percent string", () => {
|
|
205
|
+
expect(
|
|
206
|
+
formatBestReward(
|
|
207
|
+
[campaign({ id: "c", referrer: uncappedPercentage(10) })],
|
|
208
|
+
{ now: NOW }
|
|
209
|
+
)
|
|
210
|
+
).toBe("10 %");
|
|
211
|
+
});
|
|
212
|
+
});
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { InteractionTypeKey } from "../constants/interactionTypes";
|
|
2
|
+
import type { Currency, MerchantReward, TokenAmountType } from "../types";
|
|
3
|
+
import { getCurrencyAmountKey } from "../utils/format/getCurrencyAmountKey";
|
|
4
|
+
import { getSupportedCurrency } from "../utils/format/getSupportedCurrency";
|
|
5
|
+
import { extractStartDate } from "./conditions";
|
|
6
|
+
import { formatRewardOrHide } from "./format";
|
|
7
|
+
import { getRewardRank } from "./value";
|
|
8
|
+
|
|
9
|
+
/** Reward side a surface cares about: the sharer (`referrer`) or the referred
|
|
10
|
+
* user (`referee`). Drives both campaign ranking and which side is displayed. */
|
|
11
|
+
export type RewardAudience = "referrer" | "referee";
|
|
12
|
+
|
|
13
|
+
export type DisplayCampaign = {
|
|
14
|
+
campaign: MerchantReward;
|
|
15
|
+
status: "live" | "upcoming";
|
|
16
|
+
startsAt?: Date;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type SelectDisplayCampaignOptions = {
|
|
20
|
+
/** Reference "now" for expiry / start gating; defaults to the current time. */
|
|
21
|
+
now?: Date;
|
|
22
|
+
/** Currency the ranking is expressed in; defaults to EUR. */
|
|
23
|
+
currency?: Currency;
|
|
24
|
+
/** When set, only campaigns triggered by this interaction are considered. */
|
|
25
|
+
targetInteraction?: InteractionTypeKey;
|
|
26
|
+
/** Reward side to rank campaigns by; defaults to `"referrer"`. */
|
|
27
|
+
audience?: RewardAudience;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function isExpired(campaign: MerchantReward, nowMs: number): boolean {
|
|
31
|
+
return (
|
|
32
|
+
campaign.expiresAt != null &&
|
|
33
|
+
new Date(campaign.expiresAt).getTime() <= nowMs
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function hasStarted(campaign: MerchantReward, nowMs: number): boolean {
|
|
38
|
+
const startsAt = extractStartDate(campaign.conditions);
|
|
39
|
+
return startsAt == null || startsAt.getTime() <= nowMs;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function audienceReward(campaign: MerchantReward, audience: RewardAudience) {
|
|
43
|
+
return audience === "referee" ? campaign.referee : campaign.referrer;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function campaignRank(
|
|
47
|
+
campaign: MerchantReward,
|
|
48
|
+
key: keyof TokenAmountType,
|
|
49
|
+
audience: RewardAudience
|
|
50
|
+
): number {
|
|
51
|
+
const reward = audienceReward(campaign, audience);
|
|
52
|
+
return reward ? getRewardRank(reward, key) : 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Pick the single campaign a merchant surface should display.
|
|
57
|
+
*
|
|
58
|
+
* Filters out expired (and, when `targetInteraction` is set, non-matching)
|
|
59
|
+
* campaigns, then prefers the highest-ranked *live* campaign — ranked by the
|
|
60
|
+
* `audience` reward side in the requested currency. When none has started yet
|
|
61
|
+
* it falls back to the soonest-starting upcoming campaign (the endpoint does
|
|
62
|
+
* not gate on the start-date condition, so future-start campaigns come
|
|
63
|
+
* through).
|
|
64
|
+
*/
|
|
65
|
+
export function selectDisplayCampaign(
|
|
66
|
+
rewards: readonly MerchantReward[],
|
|
67
|
+
options: SelectDisplayCampaignOptions = {}
|
|
68
|
+
): DisplayCampaign | undefined {
|
|
69
|
+
const nowMs = (options.now ?? new Date()).getTime();
|
|
70
|
+
const audience = options.audience ?? "referrer";
|
|
71
|
+
const key = getCurrencyAmountKey(getSupportedCurrency(options.currency));
|
|
72
|
+
|
|
73
|
+
const matching = options.targetInteraction
|
|
74
|
+
? rewards.filter(
|
|
75
|
+
(campaign) =>
|
|
76
|
+
campaign.interactionTypeKey === options.targetInteraction
|
|
77
|
+
)
|
|
78
|
+
: rewards;
|
|
79
|
+
const active = matching.filter((campaign) => !isExpired(campaign, nowMs));
|
|
80
|
+
|
|
81
|
+
const live = active.filter((campaign) => hasStarted(campaign, nowMs));
|
|
82
|
+
if (live.length > 0) {
|
|
83
|
+
const best = live.reduce((a, b) =>
|
|
84
|
+
campaignRank(b, key, audience) > campaignRank(a, key, audience)
|
|
85
|
+
? b
|
|
86
|
+
: a
|
|
87
|
+
);
|
|
88
|
+
return { campaign: best, status: "live" };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const upcoming = active
|
|
92
|
+
.map((campaign) => ({
|
|
93
|
+
campaign,
|
|
94
|
+
startsAt: extractStartDate(campaign.conditions),
|
|
95
|
+
}))
|
|
96
|
+
.filter(
|
|
97
|
+
(entry): entry is { campaign: MerchantReward; startsAt: Date } =>
|
|
98
|
+
entry.startsAt != null
|
|
99
|
+
);
|
|
100
|
+
if (upcoming.length === 0) return undefined;
|
|
101
|
+
|
|
102
|
+
const soonest = upcoming.reduce((a, b) =>
|
|
103
|
+
b.startsAt.getTime() < a.startsAt.getTime() ? b : a
|
|
104
|
+
);
|
|
105
|
+
return {
|
|
106
|
+
campaign: soonest.campaign,
|
|
107
|
+
status: "upcoming",
|
|
108
|
+
startsAt: soonest.startsAt,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Headline reward string for a merchant: picks the best campaign for `options`
|
|
114
|
+
* and formats its `audience`-side reward, or returns `undefined` when there is
|
|
115
|
+
* nothing worth showing.
|
|
116
|
+
*
|
|
117
|
+
* Single entry point shared by every "headline reward" surface (share button,
|
|
118
|
+
* wallet modal, sharing/install screens) so they all show the same number for
|
|
119
|
+
* a given merchant.
|
|
120
|
+
*/
|
|
121
|
+
export function formatBestReward(
|
|
122
|
+
rewards: readonly MerchantReward[],
|
|
123
|
+
options: SelectDisplayCampaignOptions = {}
|
|
124
|
+
): string | undefined {
|
|
125
|
+
const selected = selectDisplayCampaign(rewards, options);
|
|
126
|
+
if (!selected) return undefined;
|
|
127
|
+
const reward = audienceReward(
|
|
128
|
+
selected.campaign,
|
|
129
|
+
options.audience ?? "referrer"
|
|
130
|
+
);
|
|
131
|
+
return formatRewardOrHide(reward, options.currency);
|
|
132
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import type { EstimatedReward } from "../types";
|
|
3
|
+
import { getRewardRank, getRewardValue } from "./value";
|
|
4
|
+
|
|
5
|
+
const amount = (eur: number) => ({
|
|
6
|
+
amount: eur,
|
|
7
|
+
eurAmount: eur,
|
|
8
|
+
usdAmount: eur,
|
|
9
|
+
gbpAmount: eur,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const fixed = (eur: number): EstimatedReward => ({
|
|
13
|
+
payoutType: "fixed",
|
|
14
|
+
amount: amount(eur),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const cappedPercentage = (percent: number, cap: number): EstimatedReward => ({
|
|
18
|
+
payoutType: "percentage",
|
|
19
|
+
percent,
|
|
20
|
+
percentOf: "purchase_amount",
|
|
21
|
+
maxAmount: amount(cap),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const uncappedPercentage = (percent: number): EstimatedReward => ({
|
|
25
|
+
payoutType: "percentage",
|
|
26
|
+
percent,
|
|
27
|
+
percentOf: "purchase_amount",
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("getRewardValue", () => {
|
|
31
|
+
it("returns the fixed amount in the requested currency", () => {
|
|
32
|
+
expect(getRewardValue(fixed(5), "eurAmount")).toBe(5);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("uses the capped maxAmount for a percentage reward", () => {
|
|
36
|
+
expect(getRewardValue(cappedPercentage(8, 4.8), "eurAmount")).toBe(4.8);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("returns 0 for an uncapped percentage reward", () => {
|
|
40
|
+
expect(getRewardValue(uncappedPercentage(8), "eurAmount")).toBe(0);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("returns the richest token tier for a tiered reward", () => {
|
|
44
|
+
expect(
|
|
45
|
+
getRewardValue(
|
|
46
|
+
{
|
|
47
|
+
payoutType: "tiered",
|
|
48
|
+
tierField: "purchase.amount",
|
|
49
|
+
tiers: [
|
|
50
|
+
{ minValue: 0, maxValue: 50, amount: amount(2) },
|
|
51
|
+
{ minValue: 50, amount: amount(9) },
|
|
52
|
+
],
|
|
53
|
+
},
|
|
54
|
+
"eurAmount"
|
|
55
|
+
)
|
|
56
|
+
).toBe(9);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("getRewardRank", () => {
|
|
61
|
+
it("ranks a reward with real money by its money value", () => {
|
|
62
|
+
expect(getRewardRank(fixed(5), "eurAmount")).toBe(5);
|
|
63
|
+
expect(getRewardRank(cappedPercentage(8, 4.8), "eurAmount")).toBe(4.8);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("gives an uncapped percentage a positive weight (never buried at 0)", () => {
|
|
67
|
+
expect(
|
|
68
|
+
getRewardRank(uncappedPercentage(8), "eurAmount")
|
|
69
|
+
).toBeGreaterThan(0);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("keeps real money ranked above any uncapped percentage", () => {
|
|
73
|
+
expect(getRewardRank(fixed(1), "eurAmount")).toBeGreaterThan(
|
|
74
|
+
getRewardRank(uncappedPercentage(50), "eurAmount")
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("keeps an uncapped percentage ranked above a zero-value reward", () => {
|
|
79
|
+
expect(
|
|
80
|
+
getRewardRank(uncappedPercentage(8), "eurAmount")
|
|
81
|
+
).toBeGreaterThan(getRewardRank(fixed(0), "eurAmount"));
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("ranks a higher uncapped percentage above a lower one", () => {
|
|
85
|
+
expect(
|
|
86
|
+
getRewardRank(uncappedPercentage(20), "eurAmount")
|
|
87
|
+
).toBeGreaterThan(getRewardRank(uncappedPercentage(5), "eurAmount"));
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { EstimatedReward, TokenAmountType } from "../types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Comparable fiat value of a single reward, used to rank rewards against each
|
|
5
|
+
* other.
|
|
6
|
+
*
|
|
7
|
+
* - `fixed` → the token amount in the requested currency
|
|
8
|
+
* - `tiered` → the highest token amount across tiers
|
|
9
|
+
* - `percentage` → the capped (`maxAmount`) value when present, otherwise `0`
|
|
10
|
+
* (an uncapped percentage has no comparable fiat value)
|
|
11
|
+
*/
|
|
12
|
+
export function getRewardValue(
|
|
13
|
+
reward: EstimatedReward,
|
|
14
|
+
key: keyof TokenAmountType
|
|
15
|
+
): number {
|
|
16
|
+
switch (reward.payoutType) {
|
|
17
|
+
case "fixed":
|
|
18
|
+
return reward.amount[key];
|
|
19
|
+
case "percentage":
|
|
20
|
+
return reward.maxAmount?.[key] ?? 0;
|
|
21
|
+
case "tiered":
|
|
22
|
+
return reward.tiers.reduce(
|
|
23
|
+
(max, tier) =>
|
|
24
|
+
"amount" in tier ? Math.max(max, tier.amount[key]) : max,
|
|
25
|
+
0
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Highest percent a reward exposes: the flat `percentage` percent, or the
|
|
32
|
+
* richest percent across `tiered` tiers, or `0` when it carries none.
|
|
33
|
+
*
|
|
34
|
+
* Shared by {@link getRewardRank} (to weight a percentage-only reward) and
|
|
35
|
+
* `formatEstimatedReward` (to render it as `"X %"`), so the value the UI ranks
|
|
36
|
+
* by and the value it prints stay derived from one tier traversal.
|
|
37
|
+
*/
|
|
38
|
+
export function maxRewardPercent(reward: EstimatedReward): number {
|
|
39
|
+
if (reward.payoutType === "percentage") return reward.percent;
|
|
40
|
+
if (reward.payoutType === "tiered") {
|
|
41
|
+
return reward.tiers.reduce(
|
|
42
|
+
(max, tier) =>
|
|
43
|
+
"percent" in tier ? Math.max(max, tier.percent) : max,
|
|
44
|
+
0
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
return 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// A reward with no money value (an uncapped percentage, or a percent-only
|
|
51
|
+
// tier set) still renders as "X %", so it is worth surfacing. We give it a
|
|
52
|
+
// positive ranking weight derived from its percent but scaled far below any
|
|
53
|
+
// real-money reward, so the two invariants the UI relies on hold: (1) a reward
|
|
54
|
+
// with real money always outranks a percentage-only reward, and (2) a
|
|
55
|
+
// percentage-only reward still outranks a zero-value reward. Together they
|
|
56
|
+
// guarantee the reward the ranking picks is always one we can display.
|
|
57
|
+
const PERCENT_ONLY_RANK_WEIGHT = 1e-6;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Ranking weight used to pick the single most attractive reward to surface.
|
|
61
|
+
*
|
|
62
|
+
* Mirrors {@link getRewardValue} (money value) but lifts a percentage-only
|
|
63
|
+
* reward to a tiny positive weight instead of `0`, so it is never buried
|
|
64
|
+
* behind a zero-value reward when choosing what to display.
|
|
65
|
+
*/
|
|
66
|
+
export function getRewardRank(
|
|
67
|
+
reward: EstimatedReward,
|
|
68
|
+
key: keyof TokenAmountType
|
|
69
|
+
): number {
|
|
70
|
+
const value = getRewardValue(reward, key);
|
|
71
|
+
if (value > 0) return value;
|
|
72
|
+
return maxRewardPercent(reward) * PERCENT_ONLY_RANK_WEIGHT;
|
|
73
|
+
}
|
package/src/types/index.ts
CHANGED
|
@@ -53,9 +53,15 @@ export type {
|
|
|
53
53
|
} from "./rpc/embedded";
|
|
54
54
|
export type { SendInteractionParamsType } from "./rpc/interaction";
|
|
55
55
|
export type {
|
|
56
|
+
ConditionGroup,
|
|
57
|
+
ConditionOperator,
|
|
56
58
|
EstimatedReward,
|
|
57
59
|
GetMerchantInformationReturnType,
|
|
60
|
+
MerchantReward,
|
|
58
61
|
RewardTier,
|
|
62
|
+
RuleCondition,
|
|
63
|
+
RuleConditions,
|
|
64
|
+
RuleField,
|
|
59
65
|
TokenAmountType,
|
|
60
66
|
} from "./rpc/merchantInformation";
|
|
61
67
|
export type {
|
|
@@ -12,13 +12,20 @@ export type TokenAmountType = {
|
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* A tier definition for tiered rewards
|
|
15
|
+
* A tier definition for tiered rewards — pays either a flat token amount
|
|
16
|
+
* or a percent of the tier field value
|
|
16
17
|
*/
|
|
17
|
-
export type RewardTier =
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
export type RewardTier =
|
|
19
|
+
| {
|
|
20
|
+
minValue: number;
|
|
21
|
+
maxValue?: number;
|
|
22
|
+
amount: TokenAmountType;
|
|
23
|
+
}
|
|
24
|
+
| {
|
|
25
|
+
minValue: number;
|
|
26
|
+
maxValue?: number;
|
|
27
|
+
percent: number;
|
|
28
|
+
};
|
|
22
29
|
|
|
23
30
|
/**
|
|
24
31
|
* Estimated reward amount — discriminated union by payout type
|
|
@@ -45,6 +52,112 @@ export type EstimatedReward =
|
|
|
45
52
|
tiers: RewardTier[];
|
|
46
53
|
};
|
|
47
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Comparison operators usable in a {@link RuleCondition}.
|
|
57
|
+
* @group RPC Schema
|
|
58
|
+
*/
|
|
59
|
+
export type ConditionOperator =
|
|
60
|
+
| "eq"
|
|
61
|
+
| "neq"
|
|
62
|
+
| "gt"
|
|
63
|
+
| "gte"
|
|
64
|
+
| "lt"
|
|
65
|
+
| "lte"
|
|
66
|
+
| "in"
|
|
67
|
+
| "not_in"
|
|
68
|
+
| "contains"
|
|
69
|
+
| "starts_with"
|
|
70
|
+
| "ends_with"
|
|
71
|
+
| "exists"
|
|
72
|
+
| "not_exists"
|
|
73
|
+
| "between";
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Dot-path of the rule-evaluation context field a {@link RuleCondition} targets.
|
|
77
|
+
*
|
|
78
|
+
* Only the paths the SDK actually reads are listed (for editor autocompletion);
|
|
79
|
+
* the trailing `string` member keeps the type open to any other path the backend
|
|
80
|
+
* may emit, so it never lies at runtime. Custom interaction data is addressed
|
|
81
|
+
* through `custom.${string}`.
|
|
82
|
+
* @group RPC Schema
|
|
83
|
+
*/
|
|
84
|
+
export type RuleField =
|
|
85
|
+
// Minimum-purchase gating (read by `extractMinPurchaseAmount`)
|
|
86
|
+
| "purchase.amount"
|
|
87
|
+
// Campaign start gating (read by `extractStartDate`)
|
|
88
|
+
| "time.timestamp"
|
|
89
|
+
// Referrer attribution
|
|
90
|
+
| "attribution.referrerIdentityGroupId"
|
|
91
|
+
// Custom interaction context
|
|
92
|
+
| `custom.${string}`
|
|
93
|
+
// Escape hatch — any other context path (kept assignable to/from `string`)
|
|
94
|
+
| (string & Record<never, never>);
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* A single leaf rule condition. Compares the value found at {@link RuleField}
|
|
98
|
+
* in the evaluation context against `value` (and `valueTo` for `between`).
|
|
99
|
+
* @group RPC Schema
|
|
100
|
+
*/
|
|
101
|
+
export type RuleCondition = {
|
|
102
|
+
field: RuleField;
|
|
103
|
+
operator: ConditionOperator;
|
|
104
|
+
value: string | number | boolean | null;
|
|
105
|
+
valueTo?: string | number | boolean | null;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A recursive group of conditions combined through a boolean `logic`.
|
|
110
|
+
* @group RPC Schema
|
|
111
|
+
*/
|
|
112
|
+
export type ConditionGroup = {
|
|
113
|
+
logic: "all" | "any" | "none";
|
|
114
|
+
conditions: (RuleCondition | ConditionGroup)[];
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Campaign gating rules: a flat list of {@link RuleCondition} (implicitly
|
|
119
|
+
* AND-ed) or a nested {@link ConditionGroup} tree. Surfaced raw so integrators
|
|
120
|
+
* can inspect the rules and derive their own display (start date, minimum
|
|
121
|
+
* purchase, …) instead of relying on pre-computed fields.
|
|
122
|
+
* @group RPC Schema
|
|
123
|
+
*/
|
|
124
|
+
export type RuleConditions = RuleCondition[] | ConditionGroup;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A reward offer exposed by a merchant campaign.
|
|
128
|
+
*
|
|
129
|
+
* Mirrors the backend `EstimatedRewardItem` one-to-one — a static parity
|
|
130
|
+
* assertion on the backend keeps its runtime-validated schema in lockstep with
|
|
131
|
+
* this published contract.
|
|
132
|
+
* @group RPC Schema
|
|
133
|
+
*/
|
|
134
|
+
export type MerchantReward = {
|
|
135
|
+
/** Reward token address; falls back to the merchant token when omitted. */
|
|
136
|
+
token?: Address;
|
|
137
|
+
/** Identifier of the campaign rule this reward originates from. */
|
|
138
|
+
campaignId: string;
|
|
139
|
+
/** Campaign display name. */
|
|
140
|
+
name: string;
|
|
141
|
+
/** Interaction that triggers the reward. */
|
|
142
|
+
interactionTypeKey: InteractionTypeKey;
|
|
143
|
+
/** Reward paid to the referrer, when the campaign defines one. */
|
|
144
|
+
referrer?: EstimatedReward;
|
|
145
|
+
/** Reward paid to the referee, when the campaign defines one. */
|
|
146
|
+
referee?: EstimatedReward;
|
|
147
|
+
/** Raw gating rules — inspect to derive start date, minimum purchase, … */
|
|
148
|
+
conditions: RuleConditions;
|
|
149
|
+
/** Seconds a reward stays locked before settlement. */
|
|
150
|
+
defaultLockupSeconds?: number;
|
|
151
|
+
/** Days before a pending reward expires. */
|
|
152
|
+
pendingRewardExpirationDays?: number;
|
|
153
|
+
/** Per-user reward cap for this campaign. */
|
|
154
|
+
maxRewardsPerUser?: number;
|
|
155
|
+
/** Merchant-wide per-user reward cap across every campaign. */
|
|
156
|
+
merchantMaxRewardsPerUser?: number;
|
|
157
|
+
/** ISO-8601 campaign end date, or `null` when open-ended. */
|
|
158
|
+
expiresAt?: string | null;
|
|
159
|
+
};
|
|
160
|
+
|
|
48
161
|
/**
|
|
49
162
|
* Response of the `frak_getMerchantInformation` RPC method
|
|
50
163
|
* @group RPC Schema
|
|
@@ -67,11 +180,5 @@ export type GetMerchantInformationReturnType = {
|
|
|
67
180
|
*/
|
|
68
181
|
domain: string;
|
|
69
182
|
};
|
|
70
|
-
rewards:
|
|
71
|
-
token?: Address;
|
|
72
|
-
campaignId: string;
|
|
73
|
-
interactionTypeKey: InteractionTypeKey;
|
|
74
|
-
referrer?: EstimatedReward;
|
|
75
|
-
referee?: EstimatedReward;
|
|
76
|
-
}[];
|
|
183
|
+
rewards: MerchantReward[];
|
|
77
184
|
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export type SdkReferralEventMap = {
|
|
2
2
|
user_referred_started: {
|
|
3
3
|
referrer?: string;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
referrer_client_id?: string;
|
|
5
|
+
referrer_wallet?: string;
|
|
6
|
+
wallet_status?: string;
|
|
7
7
|
};
|
|
8
8
|
user_referred_completed: {
|
|
9
9
|
status: "success";
|