Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/father/category-fees.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ describe("SwapFather category fees", () => {
expect(parseAffiliateFeePercent("0.01", 1_500)).toBe(1);
expect(parseAffiliateFeePercent("10", 1_500)).toBe(1_000);
expect(parseAffiliateFeePercent("12.5", 1_500)).toBe(1_250);
expect(parseAffiliateFeePercent("7.25%", 1_500)).toBe(725);
expect(parseAffiliateFeePercent(" 7.25% ", 1_500)).toBe(725);
expect(parseAffiliateFeePercent("7.25$", 1_500)).toBe(725);
expect(parseAffiliateFeePercent("15", 1_500)).toBe(1_500);
expect(parseAffiliateFeePercent("0", 1_500)).toBeNull();
expect(parseAffiliateFeePercent("15.01", 1_500)).toBeNull();
expect(parseAffiliateFeePercent("10.123", 1_500)).toBeNull();
expect(parseAffiliateFeePercent("7.25%$", 1_500)).toBeNull();
expect(parseAffiliateFeePercent("fee 7.25", 1_500)).toBeNull();
expect(parseAffiliateFeePercent("2", 200)).toBe(200);
expect(parseAffiliateFeePercent("2.01", 200)).toBeNull();
expect(formatAffiliateFeeBps(1_250)).toBe("12.5%");
Expand Down
7 changes: 4 additions & 3 deletions src/father/father.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export function registerFather(bot: Bot, fleet: Fleet, opts: FatherOptions = {})
const totalFeeBps = parseAffiliateFeePercent(text, setting.fee_cap_bps);
if (totalFeeBps === null) {
await ctx.reply(
`Enter a percentage from 0.01 to ${formatAffiliateFeeInput(setting.fee_cap_bps)} (for example, <code>10</code> or <code>12.5</code>).`,
`Enter a percentage from 0.01 to ${formatAffiliateFeeInput(setting.fee_cap_bps)} (for example, <code>7.25</code> or <code>7.25%</code>).`,
{ parse_mode: "HTML" },
);
return;
Expand Down Expand Up @@ -1227,8 +1227,9 @@ export function visibleAffiliateFeeItems(
}

export function parseAffiliateFeePercent(value: string, capBps: number): number | null {
if (!/^\d+(\.\d{1,2})?$/.test(value.trim())) return null;
const bps = Math.round(Number(value.trim()) * 100);
const normalized = value.trim().replace(/(?:%|\$)$/, "").trim();
if (!/^\d+(\.\d{1,2})?$/.test(normalized)) return null;
const bps = Math.round(Number(normalized) * 100);
return Number.isInteger(bps) && bps >= 1 && bps <= capBps ? bps : null;
}

Expand Down
Loading