@@ -55,6 +55,28 @@ type State struct {
5555 current int64
5656}
5757
58+ // Calculate calculates a token account's balance using a starting point and a set
59+ // of strategies. Each may be incomplete individually, but in total must form a
60+ // complete balance calculation.
61+ func Calculate (ctx context.Context , tokenAccount * common.Account , initialBalance uint64 , strategies ... Strategy ) (balance uint64 , err error ) {
62+ balanceState := & State {
63+ current : int64 (initialBalance ),
64+ }
65+
66+ for _ , strategy := range strategies {
67+ balanceState , err = strategy (ctx , tokenAccount , balanceState )
68+ if err != nil {
69+ return 0 , err
70+ }
71+ }
72+
73+ if balanceState .current < 0 {
74+ return 0 , ErrNegativeBalance
75+ }
76+
77+ return uint64 (balanceState .current ), nil
78+ }
79+
5880// CalculateFromCache is the default and recommended strategy for reliably estimating
5981// a token account's balance using cached values.
6082//
@@ -168,28 +190,6 @@ func CalculateFromBlockchain(ctx context.Context, data code_data.Provider, token
168190 return quarks , BlockchainSource , nil
169191}
170192
171- // Calculate calculates a token account's balance using a starting point and a set
172- // of strategies. Each may be incomplete individually, but in total must form a
173- // complete balance calculation.
174- func Calculate (ctx context.Context , tokenAccount * common.Account , initialBalance uint64 , strategies ... Strategy ) (balance uint64 , err error ) {
175- balanceState := & State {
176- current : int64 (initialBalance ),
177- }
178-
179- for _ , strategy := range strategies {
180- balanceState , err = strategy (ctx , tokenAccount , balanceState )
181- if err != nil {
182- return 0 , err
183- }
184- }
185-
186- if balanceState .current < 0 {
187- return 0 , ErrNegativeBalance
188- }
189-
190- return uint64 (balanceState .current ), nil
191- }
192-
193193// NetBalanceFromIntentActions is a balance calculation strategy that incorporates
194194// the net balance by applying payment intents to the current balance.
195195func NetBalanceFromIntentActions (ctx context.Context , data code_data.Provider ) Strategy {
@@ -243,14 +243,39 @@ type BatchState struct {
243243 current map [string ]int64
244244}
245245
246+ // CalculateBatch calculates a set of token accounts' balance using a starting point
247+ // and a set of strategies. Each may be incomplete individually, but in total must
248+ // form a complete balance calculation.
249+ func CalculateBatch (ctx context.Context , tokenAccounts []string , strategies ... BatchStrategy ) (balanceByTokenAccount map [string ]uint64 , err error ) {
250+ balanceState := & BatchState {
251+ current : make (map [string ]int64 ),
252+ }
253+
254+ for _ , strategy := range strategies {
255+ balanceState , err = strategy (ctx , tokenAccounts , balanceState )
256+ if err != nil {
257+ return nil , err
258+ }
259+ }
260+
261+ res := make (map [string ]uint64 )
262+ for tokenAccount , balance := range balanceState .current {
263+ if balance < 0 {
264+ return nil , ErrNegativeBalance
265+ }
266+
267+ res [tokenAccount ] = uint64 (balance )
268+ }
269+
270+ return res , nil
271+ }
272+
246273// BatchCalculateFromCacheWithAccountRecords is the default and recommended batch strategy
247274// or reliably estimating a set of token accounts' balance when common.AccountRecords are
248275// available.
249276//
250277// Note: Use this method when calculating balances for accounts that are managed by
251278// Code (ie. Timelock account) and operate within the L2 system.
252- //
253- // Note: This only supports post-privacy accounts. Use CalculateFromCache instead.
254279func BatchCalculateFromCacheWithAccountRecords (ctx context.Context , data code_data.Provider , accountRecordsBatch ... * common.AccountRecords ) (map [string ]uint64 , error ) {
255280 tracer := metrics .TraceMethodCall (ctx , metricsPackageName , "BatchCalculateFromCacheWithAccountRecords" )
256281 defer tracer .End ()
@@ -279,8 +304,6 @@ func BatchCalculateFromCacheWithAccountRecords(ctx context.Context, data code_da
279304//
280305// Note: Use this method when calculating balances for accounts that are managed by
281306// Code (ie. Timelock account) and operate within the L2 system.
282- //
283- // Note: This only supports post-privacy accounts. Use CalculateFromCache instead.
284307func BatchCalculateFromCacheWithTokenAccounts (ctx context.Context , data code_data.Provider , tokenAccounts ... * common.Account ) (map [string ]uint64 , error ) {
285308 tracer := metrics .TraceMethodCall (ctx , metricsPackageName , "BatchCalculateFromCacheWithTokenAccounts" )
286309 defer tracer .End ()
@@ -333,33 +356,6 @@ func defaultBatchCalculationFromCache(ctx context.Context, data code_data.Provid
333356 )
334357}
335358
336- // CalculateBatch calculates a set of token accounts' balance using a starting point
337- // and a set of strategies. Each may be incomplete individually, but in total must
338- // form a complete balance calculation.
339- func CalculateBatch (ctx context.Context , tokenAccounts []string , strategies ... BatchStrategy ) (balanceByTokenAccount map [string ]uint64 , err error ) {
340- balanceState := & BatchState {
341- current : make (map [string ]int64 ),
342- }
343-
344- for _ , strategy := range strategies {
345- balanceState , err = strategy (ctx , tokenAccounts , balanceState )
346- if err != nil {
347- return nil , err
348- }
349- }
350-
351- res := make (map [string ]uint64 )
352- for tokenAccount , balance := range balanceState .current {
353- if balance < 0 {
354- return nil , ErrNegativeBalance
355- }
356-
357- res [tokenAccount ] = uint64 (balance )
358- }
359-
360- return res , nil
361- }
362-
363359// NetBalanceFromIntentActionsBatch is a balance calculation strategy that incorporates
364360// the net balance by applying payment intents to the current balance.
365361func NetBalanceFromIntentActionsBatch (ctx context.Context , data code_data.Provider ) BatchStrategy {
0 commit comments