11import { BalanceProvider } from "@/Models/BalanceProvider" ;
22import { TokenBalance } from "@/Models/Balance" ;
33import KnownInternalNames from "@/lib/knownIds" ;
4- import { AztecAddress } from "@aztec/aztec.js/addresses" ;
5- import { Fr } from "@aztec/aztec.js/fields" ;
6- import { createAztecNodeClient } from "@aztec/aztec.js/node" ;
7- import { deriveStorageSlotInMap } from "@aztec/stdlib/hash" ;
84import { formatUnits } from "viem" ;
95import { getNetworkRpcUrl } from "@/lib/rpc/resolveNetworkRpcUrl" ;
106
117// Storage slot for public_balances map in the Token contract (slot 9 for standard Aztec token)
12- const TOKEN_PUBLIC_BALANCES_SLOT = new Fr ( 9n )
8+ const TOKEN_PUBLIC_BALANCES_SLOT_INDEX = 9n
139
1410export class AztecBalanceProvider extends BalanceProvider {
1511 supportsNetwork : BalanceProvider [ 'supportsNetwork' ] = ( network ) => {
@@ -22,6 +18,18 @@ export class AztecBalanceProvider extends BalanceProvider {
2218 const nodeUrl = getNetworkRpcUrl ( network )
2319 if ( ! nodeUrl ) return [ ]
2420
21+ // Lazy-load Aztec SDK so it never enters the SSR import graph.
22+ // Top-level imports of @aztec/* drag in @aztec/foundation, whose pino logger
23+ // tries to dynamically require pino-pretty at module init and crashes Next's
24+ // serverless bundle (HTTP 500 on every page → social previews break).
25+ const [ { AztecAddress } , { Fr } , { createAztecNodeClient } , { deriveStorageSlotInMap } ] = await Promise . all ( [
26+ import ( "@aztec/aztec.js/addresses" ) ,
27+ import ( "@aztec/aztec.js/fields" ) ,
28+ import ( "@aztec/aztec.js/node" ) ,
29+ import ( "@aztec/stdlib/hash" ) ,
30+ ] )
31+
32+ const tokenPublicBalancesSlot = new Fr ( TOKEN_PUBLIC_BALANCES_SLOT_INDEX )
2533 const client = createAztecNodeClient ( nodeUrl )
2634 const owner = AztecAddress . fromString ( address )
2735 const balances : TokenBalance [ ] = [ ]
@@ -30,7 +38,7 @@ export class AztecBalanceProvider extends BalanceProvider {
3038 try {
3139 const tokenAddr = AztecAddress . fromString ( token . contract )
3240 // Type assertions needed: @aztec /aztec.js and @aztec /stdlib resolve to different @aztec /foundation versions
33- const slot = await deriveStorageSlotInMap ( TOKEN_PUBLIC_BALANCES_SLOT as any , owner as any )
41+ const slot = await deriveStorageSlotInMap ( tokenPublicBalancesSlot as any , owner as any )
3442 const balanceField = await client . getPublicStorageAt ( 'latest' , tokenAddr , slot as any )
3543 const raw = balanceField . toBigInt ( )
3644 const amount = Number ( formatUnits ( raw , token . decimals ) )
0 commit comments