Skip to content

Commit 5b5bc89

Browse files
Soumya Mohapatraadecaro
authored andcommitted
Exercise WithSortBy and WithTimeRange in tests; rename WithBalanceType; add GoDocs
Signed-off-by: Soumya Mohapatra <soumyaranjanmohapatra8@gmail.com> Signed-off-by: Soumya Mohapatra <mohapatras@microsoft.com>
1 parent 7f53ffd commit 5b5bc89

8 files changed

Lines changed: 74 additions & 17 deletions

File tree

integration/token/dvp/views/balance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (b *BalanceView) Call(context view.Context) (interface{}, error) {
4040
return nil, fmt.Errorf("wallet %s not found: %w", b.Wallet, err)
4141
}
4242

43-
balance, err := wallet.Balance(context.Context(), token.WithBalanceType(b.Type))
43+
balance, err := wallet.Balance(context.Context(), token.WithBalanceTokenType(b.Type))
4444
if err != nil {
4545
return nil, err
4646
}

integration/token/fungible/views/accept.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (a *AcceptCashView) Call(context view.Context) (interface{}, error) {
4242
if output.Type == "MAX" {
4343
continue
4444
}
45-
balance, err := ttx.MyWallet(context, token.WithTMSID(tx.TMSID())).Balance(context.Context(), token.WithBalanceType(output.Type))
45+
balance, err := ttx.MyWallet(context, token.WithTMSID(tx.TMSID())).Balance(context.Context(), token.WithBalanceTokenType(output.Type))
4646
assert.NoError(err, "failed retrieving balance for type [%s]", output.Type)
4747
assert.True(balance <= 3000, "cannot have more than 3000 unspent quantity for type [%s]", output.Type)
4848
}

integration/token/fungible/views/balance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (b *BalanceView) Call(context view.Context) (interface{}, error) {
6363
assert.NoError(err, "failed to compute the sum of the co-owned tokens")
6464

6565
if !b.SkipCheck {
66-
balance, err := wallet.Balance(context.Context(), token.WithBalanceType(b.Type))
66+
balance, err := wallet.Balance(context.Context(), token.WithBalanceTokenType(b.Type))
6767
if err != nil {
6868
return nil, err
6969
}

integration/token/fungible/views/history.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (p *IssuedBalanceView) Call(context view.Context) (interface{}, error) {
200200
return nil, errors.Errorf("wallet [%s] not found", p.Wallet)
201201
}
202202

203-
return wallet.IssuedBalance(context.Context(), token.WithBalanceType(p.TokenType))
203+
return wallet.IssuedBalance(context.Context(), token.WithBalanceTokenType(p.TokenType))
204204
}
205205

206206
type IssuedBalanceViewFactory struct{}
@@ -225,7 +225,7 @@ func (p *RedeemedBalanceView) Call(context view.Context) (interface{}, error) {
225225
return nil, errors.Errorf("wallet [%s] not found", p.Wallet)
226226
}
227227

228-
return wallet.RedeemedBalance(context.Context(), token.WithBalanceType(p.TokenType))
228+
return wallet.RedeemedBalance(context.Context(), token.WithBalanceTokenType(p.TokenType))
229229
}
230230

231231
type RedeemedBalanceViewFactory struct{}
@@ -250,7 +250,7 @@ func (p *OutstandingBalanceView) Call(context view.Context) (interface{}, error)
250250
return nil, errors.Errorf("wallet [%s] not found", p.Wallet)
251251
}
252252

253-
return wallet.OutstandingBalance(context.Context(), token.WithBalanceType(p.TokenType))
253+
return wallet.OutstandingBalance(context.Context(), token.WithBalanceTokenType(p.TokenType))
254254
}
255255

256256
type OutstandingBalanceViewFactory struct{}

integration/token/interop/views/balance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (b *BalanceView) Call(context view.Context) (interface{}, error) {
4848
precision := tms.PublicParametersManager().PublicParameters().Precision()
4949

5050
// owned
51-
balance, err := wallet.Balance(context.Context(), token.WithBalanceType(b.Type))
51+
balance, err := wallet.Balance(context.Context(), token.WithBalanceTokenType(b.Type))
5252
assert.NoError(err, "failed to get unspent tokens")
5353

5454
htlcWallet := htlc.Wallet(context, wallet)

token/vault.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,20 @@ func (q *QueryEngine) ListHistoryIssuedTokens(ctx context.Context) (*token.Issue
121121
return q.qe.ListHistoryIssuedTokens(ctx)
122122
}
123123

124+
// IssuedBalance returns the total amount of tokens issued by the given issuer that have not been redeemed,
125+
// optionally filtered by token type and time range.
124126
func (q *QueryEngine) IssuedBalance(ctx context.Context, tokenType token.Type, issuerRaw driver.Identity, from, to *time.Time) (uint64, error) {
125127
return q.qe.IssuedBalance(ctx, tokenType, issuerRaw, from, to)
126128
}
127129

130+
// ListRedeemedTokens returns the list of redeemed tokens originally issued by the given issuer,
131+
// optionally filtered by token type, time range, and sorted by the specified field and direction.
128132
func (q *QueryEngine) ListRedeemedTokens(ctx context.Context, tokenType token.Type, issuerRaw driver.Identity, from, to *time.Time, sortBy driver.SortField, sortDirection driver.SortDirection) (*token.IssuedTokens, error) {
129133
return q.qe.ListRedeemedTokens(ctx, tokenType, issuerRaw, from, to, sortBy, sortDirection)
130134
}
131135

136+
// RedeemedBalance returns the total amount of redeemed tokens originally issued by the given issuer,
137+
// optionally filtered by token type and time range.
132138
func (q *QueryEngine) RedeemedBalance(ctx context.Context, tokenType token.Type, issuerRaw driver.Identity, from, to *time.Time) (uint64, error) {
133139
return q.qe.RedeemedBalance(ctx, tokenType, issuerRaw, from, to)
134140
}

token/wallet.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,18 @@ func WithSortBy(field driver.SortField, dir driver.SortDirection) ListTokensOpti
5656
}
5757
}
5858

59-
// BalanceOpts contains options for balance queries.
59+
// BalanceOpts contains options for balance queries (methods that return a scalar
60+
// total rather than a list of tokens). Sort fields are intentionally excluded
61+
// because the result is a single number.
6062
type BalanceOpts = driver.BalanceOpts
6163

62-
// BalanceOption is a function that configures BalanceOpts
64+
// BalanceOption is a functional option that configures a BalanceOpts struct.
65+
// Use WithBalanceTokenType and WithTimeRange to construct values.
6366
type BalanceOption func(*BalanceOpts) error
6467

65-
// WithBalanceType returns a balance option that filters by the passed token type.
66-
func WithBalanceType(tokenType token.Type) BalanceOption {
68+
// WithBalanceTokenType returns a BalanceOption that restricts the balance
69+
// computation to the given token type. An empty token type means all types.
70+
func WithBalanceTokenType(tokenType token.Type) BalanceOption {
6771
return func(o *BalanceOpts) error {
6872
o.TokenType = tokenType
6973

@@ -394,7 +398,7 @@ func (i *IssuerWallet) ListIssuedTokens(ctx context.Context, opts ...ListTokensO
394398
}
395399

396400
// IssuedBalance returns the total amount of non-redeemed tokens issued by this wallet.
397-
// Options: WithBalanceType, WithTimeRange
401+
// Options: WithBalanceTokenType, WithTimeRange
398402
func (i *IssuerWallet) IssuedBalance(ctx context.Context, opts ...BalanceOption) (uint64, error) {
399403
compiledOpts, err := CompileBalanceOption(opts...)
400404
if err != nil {
@@ -416,7 +420,7 @@ func (i *IssuerWallet) ListRedeemedTokens(ctx context.Context, opts ...ListToken
416420
}
417421

418422
// RedeemedBalance returns the total amount of redeemed tokens originally issued by this wallet.
419-
// Options: WithBalanceType, WithTimeRange
423+
// Options: WithBalanceTokenType, WithTimeRange
420424
func (i *IssuerWallet) RedeemedBalance(ctx context.Context, opts ...BalanceOption) (uint64, error) {
421425
compiledOpts, err := CompileBalanceOption(opts...)
422426
if err != nil {
@@ -427,7 +431,7 @@ func (i *IssuerWallet) RedeemedBalance(ctx context.Context, opts ...BalanceOptio
427431
}
428432

429433
// OutstandingBalance returns the net amount still in circulation: IssuedBalance − RedeemedBalance.
430-
// Options: WithBalanceType, WithTimeRange
434+
// Options: WithBalanceTokenType, WithTimeRange
431435
func (i *IssuerWallet) OutstandingBalance(ctx context.Context, opts ...BalanceOption) (uint64, error) {
432436
compiledOpts, err := CompileBalanceOption(opts...)
433437
if err != nil {
@@ -456,7 +460,8 @@ func CompileListTokensOption(opts ...ListTokensOption) (*driver.ListTokensOption
456460
}, nil
457461
}
458462

459-
// CompileBalanceOption compiles variadic BalanceOption values into a single BalanceOpts struct.
463+
// CompileBalanceOption compiles variadic BalanceOption values into a single
464+
// BalanceOpts struct ready to be passed to a driver-level balance method.
460465
func CompileBalanceOption(opts ...BalanceOption) (*driver.BalanceOpts, error) {
461466
bo := &driver.BalanceOpts{}
462467
for _, opt := range opts {

token/wallet_test.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package token
1010
import (
1111
"context"
1212
"testing"
13+
"time"
1314

1415
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1516
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
@@ -572,7 +573,7 @@ func TestOwnerWallet_Balance(t *testing.T) {
572573
wallet := &OwnerWallet{w: mockOW}
573574
ctx := context.Background()
574575

575-
balance, err := wallet.Balance(ctx, WithBalanceType("USD"))
576+
balance, err := wallet.Balance(ctx, WithBalanceTokenType("USD"))
576577

577578
require.NoError(t, err)
578579
assert.Equal(t, uint64(1000), balance)
@@ -623,6 +624,51 @@ func TestIssuerWallet_ListIssuedTokens(t *testing.T) {
623624
assert.Equal(t, expectedTokens, tokens)
624625
}
625626

627+
// TestIssuerWallet_ListIssuedTokens_WithSortBy verifies that WithSortBy passes the sort
628+
// field and direction through to the driver.
629+
func TestIssuerWallet_ListIssuedTokens_WithSortBy(t *testing.T) {
630+
mockIW := &mock.IssuerWallet{}
631+
expectedTokens := &token.IssuedTokens{}
632+
mockIW.HistoryTokensReturns(expectedTokens, nil)
633+
634+
wallet := &IssuerWallet{w: mockIW}
635+
ctx := context.Background()
636+
637+
tokens, err := wallet.ListIssuedTokens(ctx, WithType("USD"), WithSortBy(driver.SortByQuantity, driver.SortDescending))
638+
639+
require.NoError(t, err)
640+
assert.Equal(t, expectedTokens, tokens)
641+
642+
// Verify the compiled options include sort fields.
643+
_, opts := mockIW.HistoryTokensArgsForCall(0)
644+
assert.Equal(t, driver.SortByQuantity, opts.SortBy)
645+
assert.Equal(t, driver.SortDescending, opts.SortDirection)
646+
}
647+
648+
// TestOwnerWallet_Balance_WithTimeRange verifies that WithTimeRange passes the
649+
// time bounds through to the driver.
650+
func TestOwnerWallet_Balance_WithTimeRange(t *testing.T) {
651+
mockOW := &mock.OwnerWallet{}
652+
mockOW.BalanceReturns(uint64(500), nil)
653+
654+
wallet := &OwnerWallet{w: mockOW}
655+
ctx := context.Background()
656+
657+
from := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
658+
to := time.Date(2025, 12, 31, 23, 59, 59, 0, time.UTC)
659+
660+
balance, err := wallet.Balance(ctx, WithBalanceTokenType("USD"), WithTimeRange(&from, &to))
661+
662+
require.NoError(t, err)
663+
assert.Equal(t, uint64(500), balance)
664+
665+
// Verify the compiled options include time range.
666+
_, opts := mockOW.BalanceArgsForCall(0)
667+
assert.Equal(t, &from, opts.From)
668+
assert.Equal(t, &to, opts.To)
669+
assert.Equal(t, token.Type("USD"), opts.TokenType)
670+
}
671+
626672
// TestWalletManager_OwnerWalletIDs_Error verifies error handling
627673
func TestWalletManager_OwnerWalletIDs_Error(t *testing.T) {
628674
mockWS := &mock.WalletService{}
@@ -763,7 +809,7 @@ func TestOwnerWallet_Balance_Error(t *testing.T) {
763809
wallet := &OwnerWallet{w: mockOW}
764810
ctx := context.Background()
765811

766-
balance, err := wallet.Balance(ctx, WithBalanceType("USD"))
812+
balance, err := wallet.Balance(ctx, WithBalanceTokenType("USD"))
767813

768814
require.Error(t, err)
769815
assert.Equal(t, expectedErr, err)

0 commit comments

Comments
 (0)