feat: add local transfer fee configuration to support dynamic fee calculation#382
feat: add local transfer fee configuration to support dynamic fee calculation#382onahprosper merged 2 commits intomainfrom
Conversation
📝 WalkthroughWalkthroughAdds two new environment variables and corresponding config exports to make the local transfer fee percent and cap configurable, and updates fee calculation to read those values instead of using hard-coded numbers. Changes
Sequence Diagram(s)(omitted) Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
.env.example (1)
15-16: Optional: alphabetise the new keys to satisfy dotenv-linter.
NEXT_PUBLIC_LOCAL_TRANSFER_FEE_CAPsorts beforeNEXT_PUBLIC_LOCAL_TRANSFER_FEE_PERCENT; swapping them resolves the[UnorderedKey]lint warning.🔧 Proposed fix
-NEXT_PUBLIC_LOCAL_TRANSFER_FEE_PERCENT=0.1 -NEXT_PUBLIC_LOCAL_TRANSFER_FEE_CAP=10000 +NEXT_PUBLIC_LOCAL_TRANSFER_FEE_CAP=10000 +NEXT_PUBLIC_LOCAL_TRANSFER_FEE_PERCENT=0.1🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.env.example around lines 15 - 16, The dotenv keys in .env.example are out of alphabetical order causing dotenv-linter's [UnorderedKey] warning; reorder the two keys so NEXT_PUBLIC_LOCAL_TRANSFER_FEE_CAP appears before NEXT_PUBLIC_LOCAL_TRANSFER_FEE_PERCENT (swap the positions of NEXT_PUBLIC_LOCAL_TRANSFER_FEE_CAP and NEXT_PUBLIC_LOCAL_TRANSFER_FEE_PERCENT) to satisfy the linter while keeping their values unchanged.app/utils.ts (1)
14-19: Combine both./lib/configimports into a single statement.Lines 14 and 15–19 both import from the same module.
♻️ Proposed refactor
-import config from "./lib/config"; -import { - feeRecipientAddress, - localTransferFeePercent, - localTransferFeeCap, -} from "./lib/config"; +import config, { + feeRecipientAddress, + localTransferFeePercent, + localTransferFeeCap, +} from "./lib/config";🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/utils.ts` around lines 14 - 19, Duplicate imports from "./lib/config" should be combined: replace the two import statements with a single line that imports the default export config and the named exports feeRecipientAddress, localTransferFeePercent, and localTransferFeeCap together (e.g. import config, { feeRecipientAddress, localTransferFeePercent, localTransferFeeCap } from "./lib/config";), removing the separate import blocks so only one import for that module remains.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@app/lib/config.ts`:
- Around line 40-47: The exports localTransferFeePercent and localTransferFeeCap
currently export parseFloat results without validation, allowing NaN or
non-integer values to propagate; update the logic to parse the env strings,
validate Number.isFinite(parsed) for percent (fallback to 0.1 on invalid) and
validate Number.isFinite(parsed) && Number.isInteger(parsed) for cap (fallback
to 10000 on invalid or non-integer), and ensure these validated fallback
assignments replace the current ternary expressions in the config so downstream
code (e.g., calculateSenderFee) never receives NaN or fractional BigInt inputs.
In `@app/utils.ts`:
- Around line 1243-1244: The calculation of maxFeeCapInBaseUnits uses
BigInt(localTransferFeeCap) which throws for non-integer values; change the
conversion to first coerce to an integer (e.g., Math.floor) before converting to
BigInt so decimals are truncated safely. Locate the expression assigning
maxFeeCapInBaseUnits that references localTransferFeeCap and decimalsMultiplier
and replace BigInt(localTransferFeeCap) with
BigInt(Math.floor(Number(localTransferFeeCap))) (or equivalent safe integer
coercion) to avoid RangeError while keeping the multiplier usage unchanged.
---
Nitpick comments:
In @.env.example:
- Around line 15-16: The dotenv keys in .env.example are out of alphabetical
order causing dotenv-linter's [UnorderedKey] warning; reorder the two keys so
NEXT_PUBLIC_LOCAL_TRANSFER_FEE_CAP appears before
NEXT_PUBLIC_LOCAL_TRANSFER_FEE_PERCENT (swap the positions of
NEXT_PUBLIC_LOCAL_TRANSFER_FEE_CAP and NEXT_PUBLIC_LOCAL_TRANSFER_FEE_PERCENT)
to satisfy the linter while keeping their values unchanged.
In `@app/utils.ts`:
- Around line 14-19: Duplicate imports from "./lib/config" should be combined:
replace the two import statements with a single line that imports the default
export config and the named exports feeRecipientAddress,
localTransferFeePercent, and localTransferFeeCap together (e.g. import config, {
feeRecipientAddress, localTransferFeePercent, localTransferFeeCap } from
"./lib/config";), removing the separate import blocks so only one import for
that module remains.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
app/lib/config.ts (1)
38-48: Harden fee config against negative values.Right now negative percent/cap values pass validation; a misconfigured env could invert fees or create negative caps. Recommend enforcing non-negative values before exporting.
♻️ Suggested guardrails
-const parsedFeePercent = parseFloat(process.env.NEXT_PUBLIC_LOCAL_TRANSFER_FEE_PERCENT ?? ""); -export const localTransferFeePercent: number = Number.isFinite(parsedFeePercent) - ? parsedFeePercent - : 0.1; +const parsedFeePercent = parseFloat( + process.env.NEXT_PUBLIC_LOCAL_TRANSFER_FEE_PERCENT ?? "", +); +export const localTransferFeePercent: number = + Number.isFinite(parsedFeePercent) && parsedFeePercent >= 0 + ? parsedFeePercent + : 0.1; -const parsedFeeCap = parseFloat(process.env.NEXT_PUBLIC_LOCAL_TRANSFER_FEE_CAP ?? ""); +const parsedFeeCap = parseFloat( + process.env.NEXT_PUBLIC_LOCAL_TRANSFER_FEE_CAP ?? "", +); export const localTransferFeeCap: number = - Number.isFinite(parsedFeeCap) && Number.isInteger(parsedFeeCap) + Number.isFinite(parsedFeeCap) && + Number.isInteger(parsedFeeCap) && + parsedFeeCap >= 0 ? parsedFeeCap : 10000;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/lib/config.ts` around lines 38 - 48, The current parsing for parsedFeePercent/localTransferFeePercent and parsedFeeCap/localTransferFeeCap allows negative values; update the validation to enforce non-negative numbers: when computing parsedFeePercent and parsedFeeCap ensure you clamp negatives to 0 (e.g., validate Number.isFinite(parsedFeePercent) && parsedFeePercent >= 0 before using it, and for parsedFeeCap require Number.isFinite(parsedFeeCap), Number.isInteger(parsedFeeCap) and parsedFeeCap >= 0), falling back to the existing defaults (0.1 for percent, 10000 for cap) if validation fails.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@app/lib/config.ts`:
- Around line 38-48: The current parsing for
parsedFeePercent/localTransferFeePercent and parsedFeeCap/localTransferFeeCap
allows negative values; update the validation to enforce non-negative numbers:
when computing parsedFeePercent and parsedFeeCap ensure you clamp negatives to 0
(e.g., validate Number.isFinite(parsedFeePercent) && parsedFeePercent >= 0
before using it, and for parsedFeeCap require Number.isFinite(parsedFeeCap),
Number.isInteger(parsedFeeCap) and parsedFeeCap >= 0), falling back to the
existing defaults (0.1 for percent, 10000 for cap) if validation fails.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
app/lib/config.tsapp/utils.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- app/utils.ts
Description
This pull request introduces configurable environment variables for local transfer fees and updates the fee calculation logic to use these new settings. The main goal is to make the local transfer fee percentage and fee cap easily adjustable through environment variables, rather than hardcoding them in the codebase.
Configuration enhancements:
NEXT_PUBLIC_LOCAL_TRANSFER_FEE_PERCENTandNEXT_PUBLIC_LOCAL_TRANSFER_FEE_CAPto.env.exampleto allow configuration of the local transfer fee percentage and cap via environment variables.app/lib/config.tsto read and exportlocalTransferFeePercentandlocalTransferFeeCapfrom environment variables, providing default values if not set.Fee calculation logic updates:
app/utils.tsto import the new fee configuration values and use them in thecalculateSenderFeefunction, replacing the previously hardcoded values for local transfer fee percent and cap. [1] [2]References
Testing
Checklist
mainBy submitting a PR, I agree to Paycrest's Contributor Code of Conduct and Contribution Guide.
Summary by CodeRabbit