-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(explorer): format raw transaction value (#2961)
* Format raw bigint value in transaction * Display full amount on hover
- Loading branch information
1 parent
93aa8e4
commit 9029c41
Showing
6 changed files
with
65 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,5 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore: Unreachable code error | ||
BigInt.prototype.toJSON = function (): number { | ||
return this.toString() | ||
} |
This file contains 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 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,48 @@ | ||
export const formatBigIntToString = ( | ||
bi: bigint, | ||
nativePrecision: number, | ||
decimalPlaces?: number | ||
) => { | ||
if (typeof bi !== 'bigint' && !bi) { | ||
return | ||
} | ||
try { | ||
// Check if input is zero | ||
if (bi === 0n) { | ||
return '0.0' | ||
} | ||
|
||
// Check if the input is negative | ||
const isNegative = bi < 0n | ||
if (isNegative) { | ||
// Convert to positive for the calculation | ||
bi = -bi | ||
} | ||
// Convert to string and add padding zeros if necessary | ||
let str = bi.toString().padStart(nativePrecision, '0') | ||
|
||
// Insert decimal point | ||
const idx = str.length - nativePrecision | ||
str = `${str.slice(0, idx)}.${str.slice(idx)}` | ||
|
||
// Handle values below zero by adding a '0' before the decimal point | ||
if (str.startsWith('.')) { | ||
str = '0' + str | ||
} | ||
|
||
// Trim to desired number of decimal places | ||
if (decimalPlaces !== undefined) { | ||
const decimalIdx = str.indexOf('.') | ||
str = str.slice(0, decimalIdx + decimalPlaces + 1) | ||
} | ||
|
||
// Add the negative sign back if necessary | ||
if (isNegative) { | ||
str = '-' + str | ||
} | ||
|
||
return str | ||
} catch (error) { | ||
console.error(`error`, error) | ||
} | ||
} |