Skip to content

Commit

Permalink
Explorer: update TokenLargestAccounts to use uiAmountString. (solana-…
Browse files Browse the repository at this point in the history
…labs#15743)

* feat: bump web3 to 0.94.2

* fix: update token largest accounts component to support uiAmountString

* fix: format code
  • Loading branch information
oJshua authored Mar 5, 2021
1 parent 11c154d commit a43a783
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
6 changes: 3 additions & 3 deletions explorer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@react-hook/debounce": "^3.0.0",
"@sentry/react": "^6.2.0",
"@solana/spl-token-registry": "^0.1.9",
"@solana/web3.js": "^0.94.1",
"@solana/web3.js": "^0.94.2",
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.8.0",
Expand Down
30 changes: 14 additions & 16 deletions explorer/src/components/account/TokenLargestAccountsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { FetchStatus } from "providers/cache";
import { useMintAccountInfo } from "providers/accounts";
import { normalizeTokenAmount } from "utils";
import { useTokenRegistry } from "providers/mints/token-registry";
import BigNumber from "bignumber.js";

export function TokenLargestAccountsCard({ pubkey }: { pubkey: PublicKey }) {
const mintAddress = pubkey.toBase58();
Expand Down Expand Up @@ -59,7 +60,7 @@ export function TokenLargestAccountsCard({ pubkey }: { pubkey: PublicKey }) {
// Find largest fixed point in accounts array
const balanceFixedPoint = accounts.reduce(
(prev: number, current: TokenAccountBalancePairWithOwner) => {
const amount = `${current.uiAmount}`;
const amount = `${current.uiAmountString}`;
const length = amount.length;
const decimalIndex = amount.indexOf(".");
if (decimalIndex >= 0 && length - decimalIndex - 1 > prev) {
Expand Down Expand Up @@ -113,10 +114,17 @@ const renderAccountRow = (
supply: number
) => {
let percent = "-";
if (supply > 0) {
percent = `${((100 * account.uiAmount) / supply).toFixed(3)}%`;
if (supply > 0 && account.uiAmountString) {
let uiAmountPercent = new BigNumber(account.uiAmountString)
.times(100)
.dividedBy(supply);

if (parseFloat(percent) === 0 && account.uiAmount > 0) {
percent = `${uiAmountPercent.toFormat(3)}%`;

if (
parseFloat(percent) === 0 &&
new BigNumber(account.uiAmountString).gt(0)
) {
percent = `~${percent}`;
}
}
Expand All @@ -132,20 +140,10 @@ const renderAccountRow = (
{account.owner && <Address pubkey={account.owner} link truncate />}
</td>
<td className="text-right text-monospace">
{fixedLocaleNumber(account.uiAmount, balanceFixedPoint)}
{account.uiAmountString &&
new BigNumber(account.uiAmountString).toFormat(balanceFixedPoint)}
</td>
<td className="text-right text-monospace">{percent}</td>
</tr>
);
};

function fixedLocaleNumber(value: number, fixedPoint: number) {
const fixed = value.toFixed(fixedPoint);
const split = fixed.split(".");

if (fixedPoint < 1) {
return parseInt(split[0], 10).toLocaleString("en");
}

return [parseInt(split[0], 10).toLocaleString("en"), split[1]].join(".");
}

0 comments on commit a43a783

Please sign in to comment.