Skip to content

Commit a744666

Browse files
klausundklausclaude
andcommitted
fix: apply same decimals + Math.round fixes to Privy example
Mirror of the wallet-adapter fixes: - useUnifiedBalance: fetch actual mint decimals via getMint() - useTransfer, useWrap, useUnwrap: Math.floor → Math.round Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5c226e5 commit a744666

4 files changed

Lines changed: 20 additions & 7 deletions

File tree

toolkits/sign-with-privy/react/src/hooks/useTransfer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function useTransfer() {
3939
const owner = new PublicKey(ownerPublicKey);
4040
const mintPubkey = new PublicKey(mint);
4141
const recipient = new PublicKey(toAddress);
42-
const tokenAmount = Math.floor(amount * Math.pow(10, decimals));
42+
const tokenAmount = Math.round(amount * Math.pow(10, decimals));
4343

4444
// Returns TransactionInstruction[][].
4545
// Each inner array is one transaction.

toolkits/sign-with-privy/react/src/hooks/useUnifiedBalance.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState, useCallback } from 'react';
22
import { PublicKey, LAMPORTS_PER_SOL } from '@solana/web3.js';
3-
import { TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID } from '@solana/spl-token';
3+
import { TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, getMint } from '@solana/spl-token';
44
import { createRpc } from '@lightprotocol/stateless.js';
55
import {
66
getAssociatedTokenAddressInterface,
@@ -84,8 +84,21 @@ export function useUnifiedBalance() {
8484
// No Token 2022 accounts
8585
}
8686

87-
// 4. Hot balance from Light Token associated token account
87+
// 4. Fetch actual decimals for each mint
8888
const mintKeys = [...mintMap.keys()];
89+
await Promise.allSettled(
90+
mintKeys.map(async (mintStr) => {
91+
try {
92+
const mint = new PublicKey(mintStr);
93+
const mintInfo = await getMint(rpc, mint);
94+
getOrCreate(mintStr).decimals = mintInfo.decimals;
95+
} catch {
96+
// Keep default decimals if mint fetch fails
97+
}
98+
}),
99+
);
100+
101+
// 5. Hot balance from Light Token associated token account
89102
await Promise.allSettled(
90103
mintKeys.map(async (mintStr) => {
91104
try {
@@ -100,7 +113,7 @@ export function useUnifiedBalance() {
100113
}),
101114
);
102115

103-
// 5. Cold balance from compressed token accounts
116+
// 6. Cold balance from compressed token accounts
104117
try {
105118
const compressed = await rpc.getCompressedTokenBalancesByOwnerV2(owner);
106119
for (const item of compressed.value.items) {
@@ -111,7 +124,7 @@ export function useUnifiedBalance() {
111124
// No compressed accounts
112125
}
113126

114-
// 6. Assemble TokenBalance[]
127+
// 7. Assemble TokenBalance[]
115128
const result: TokenBalance[] = [];
116129

117130
// SOL entry

toolkits/sign-with-privy/react/src/hooks/useUnwrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function useUnwrap() {
3838

3939
const owner = new PublicKey(ownerPublicKey);
4040
const mintPubkey = new PublicKey(mint);
41-
const tokenAmount = BigInt(Math.floor(amount * Math.pow(10, decimals)));
41+
const tokenAmount = BigInt(Math.round(amount * Math.pow(10, decimals)));
4242

4343
// Auto-detect token program (SPL vs T22) from mint account owner
4444
const mintAccountInfo = await rpc.getAccountInfo(mintPubkey);

toolkits/sign-with-privy/react/src/hooks/useWrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function useWrap() {
4040

4141
const owner = new PublicKey(ownerPublicKey);
4242
const mintPubkey = new PublicKey(mint);
43-
const tokenAmount = BigInt(Math.floor(amount * Math.pow(10, decimals)));
43+
const tokenAmount = BigInt(Math.round(amount * Math.pow(10, decimals)));
4444

4545
// Get SPL interface info — determines whether mint uses SPL or T22
4646
const splInterfaceInfos = await getSplInterfaceInfos(rpc, mintPubkey);

0 commit comments

Comments
 (0)