-
-
Notifications
You must be signed in to change notification settings - Fork 37
fix(pdf): render dynamic native token and network fee symbol #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Atharva0506
wants to merge
7
commits into
StabilityNexus:main
Choose a base branch
from
Atharva0506:fix/native-token-symbol-pdf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2ec5420
fix(pdf): render dynamic native token and network fee symbol
Atharva0506 2a93697
refactor(pdf): implement string-based decimal truncation for huge Big…
Atharva0506 05bd589
Fix native payment detection and chain metadata handling
Atharva0506 e2909ca
Handle unsupported chain metadata in PDF generation
Atharva0506 1e82a29
Harden PDF token metadata validation and total precision
Atharva0506 e80d04a
Remove unused tokenDecimals from PDF total builder
Atharva0506 92164f3
Use nullish coalescing for nativeDecimals in formatNetworkFeeValue
Atharva0506 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { ethers } from "ethers"; | ||
|
|
||
| export const resolveInvoicePaymentContext = (invoice, network) => { | ||
| const tokenAddress = invoice.paymentToken?.address; | ||
| const isNativePayment = !tokenAddress || tokenAddress === ethers.ZeroAddress; | ||
|
|
||
| if (!network?.nativeCurrency) { | ||
| throw new Error("Missing native currency metadata for invoice payment context"); | ||
| } | ||
|
|
||
| // Use Wagmi network info to get correct native token symbol, name, and decimals | ||
| // wagmi chain object typically has nativeCurrency: { name: 'Matic', symbol: 'MATIC', decimals: 18 } | ||
| const { | ||
| symbol: nativeSymbol, | ||
| decimals: nativeDecimals, | ||
| name: nativeName, | ||
| } = network.nativeCurrency; | ||
|
|
||
| // Use provided token info, or default to the native chain info | ||
| const tokenName = isNativePayment ? nativeName : (invoice.paymentToken?.name ?? nativeName); | ||
| const tokenSymbol = isNativePayment ? nativeSymbol : (invoice.paymentToken?.symbol ?? nativeSymbol); | ||
| const tokenDecimals = isNativePayment ? nativeDecimals : (invoice.paymentToken?.decimals ?? nativeDecimals); | ||
|
|
||
| return { | ||
| nativeSymbol, | ||
| nativeDecimals, | ||
| isNativePayment, | ||
| tokenName, | ||
| tokenSymbol, | ||
| tokenDecimals, | ||
| }; | ||
| }; | ||
|
|
||
| export const formatNetworkFeeValue = (fee, nativeDecimals) => { | ||
| try { | ||
| return ethers.formatUnits(fee || "0", nativeDecimals ?? 18); | ||
| } catch { | ||
| return "0.0"; | ||
| } | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| export const buildInvoiceTotalText = ({ | ||
| isNativePayment, | ||
| amountDue, | ||
| tokenSymbol, | ||
| fee, | ||
| networkFee, | ||
| nativeSymbol, | ||
| nativeDecimals, | ||
| }) => { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| // If payment is in native currency, sum up the amount due and network fee safely using BigInt | ||
| if (isNativePayment) { | ||
| let amountDueWei, networkFeeWei; | ||
|
|
||
| try { | ||
| amountDueWei = ethers.parseUnits(amountDue || "0", nativeDecimals ?? 18); | ||
| } catch { | ||
| amountDueWei = 0n; | ||
| } | ||
|
|
||
| try { | ||
| networkFeeWei = BigInt(fee || "0"); | ||
| } catch { | ||
| networkFeeWei = 0n; | ||
| } | ||
|
|
||
| const totalWei = amountDueWei + networkFeeWei; | ||
| const totalAmount = ethers.formatUnits(totalWei, nativeDecimals ?? 18); | ||
|
|
||
| return `${totalAmount} ${nativeSymbol}`; | ||
| } | ||
|
|
||
| // If ERC20 payment, show token amount + network fee in native token | ||
| return `${amountDue || "0"} ${tokenSymbol} + ${networkFee} ${nativeSymbol}`; | ||
| }; | ||
|
Atharva0506 marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.