Skip to content

Commit 0a333eb

Browse files
committed
accounts: implement CreditAccount & DebitAccount
This commit implements the `CreditAccount` and `DebitAccount` endpoints for the accounts subsystem.
1 parent f05adb2 commit 0a333eb

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

accounts/checkers_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ type mockService struct {
6363
*requestValuesStore
6464
}
6565

66+
func (m *mockService) CreditAccount(_ context.Context, _ AccountID,
67+
_ lnwire.MilliSatoshi) (*OffChainBalanceAccount, error) {
68+
69+
return nil, nil
70+
}
71+
72+
func (m *mockService) DebitAccount(_ context.Context, _ AccountID,
73+
_ lnwire.MilliSatoshi) (*OffChainBalanceAccount, error) {
74+
75+
return nil, nil
76+
}
77+
6678
func newMockService() *mockService {
6779
return &mockService{
6880
acctBalanceMsat: 0,

accounts/interface.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,16 @@ type Service interface {
304304
PaymentErrored(ctx context.Context, id AccountID,
305305
hash lntypes.Hash) error
306306

307+
// CreditAccount increases the balance of an existing account in the
308+
// database.
309+
CreditAccount(ctx context.Context, accountID AccountID,
310+
amount lnwire.MilliSatoshi) (*OffChainBalanceAccount, error)
311+
312+
// DebitAccount decreases the balance of an existing account in the
313+
// database.
314+
DebitAccount(ctx context.Context, accountID AccountID,
315+
amount lnwire.MilliSatoshi) (*OffChainBalanceAccount, error)
316+
307317
RequestValuesStore
308318
}
309319

accounts/rpcserver.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,21 @@ func (s *RPCServer) CreditAccount(ctx context.Context,
138138
log.Infof("[creditaccount] id=%s, label=%v, amount=%d",
139139
req.Id, req.Label, req.Amount)
140140

141-
return nil, fmt.Errorf("not implemented")
141+
amount := lnwire.MilliSatoshi(req.Amount * 1000)
142+
143+
accountID, err := s.findAccount(ctx, req.Id, req.Label)
144+
if err != nil {
145+
return nil, err
146+
}
147+
148+
account, err := s.service.CreditAccount(
149+
ctx, accountID, amount,
150+
)
151+
if err != nil {
152+
return nil, err
153+
}
154+
155+
return marshalAccount(account), nil
142156
}
143157

144158
// DebitAccount decreases the balance of an existing account in the account
@@ -149,7 +163,21 @@ func (s *RPCServer) DebitAccount(ctx context.Context,
149163
log.Infof("[debitaccount] id=%s, label=%v, amount=%d",
150164
req.Id, req.Label, req.Amount)
151165

152-
return nil, fmt.Errorf("not implemented")
166+
amount := lnwire.MilliSatoshi(req.Amount * 1000)
167+
168+
accountID, err := s.findAccount(ctx, req.Id, req.Label)
169+
if err != nil {
170+
return nil, err
171+
}
172+
173+
account, err := s.service.DebitAccount(
174+
ctx, accountID, amount,
175+
)
176+
if err != nil {
177+
return nil, err
178+
}
179+
180+
return marshalAccount(account), nil
153181
}
154182

155183
// ListAccounts returns all accounts that are currently stored in the account

accounts/service.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,56 @@ func (s *InterceptorService) UpdateAccount(ctx context.Context,
345345
return s.store.Account(ctx, accountID)
346346
}
347347

348+
// CreditAccount increases the balance of an existing account in the database.
349+
func (s *InterceptorService) CreditAccount(ctx context.Context,
350+
accountID AccountID,
351+
amount lnwire.MilliSatoshi) (*OffChainBalanceAccount, error) {
352+
353+
s.Lock()
354+
defer s.Unlock()
355+
356+
// As this function updates account balances, we require that the
357+
// service is running before we execute it.
358+
if !s.isRunningUnsafe() {
359+
// This case can only happen if the service is disabled while
360+
// we're processing a request.
361+
return nil, ErrAccountServiceDisabled
362+
}
363+
364+
// Credit the account in the db.
365+
err := s.store.CreditAccount(ctx, accountID, amount)
366+
if err != nil {
367+
return nil, fmt.Errorf("unable to credit account: %w", err)
368+
}
369+
370+
return s.store.Account(ctx, accountID)
371+
}
372+
373+
// DebitAccount decreases the balance of an existing account in the database.
374+
func (s *InterceptorService) DebitAccount(ctx context.Context,
375+
accountID AccountID,
376+
amount lnwire.MilliSatoshi) (*OffChainBalanceAccount, error) {
377+
378+
s.Lock()
379+
defer s.Unlock()
380+
381+
// As this function updates account balances, we require that the
382+
// service is running before we execute it.
383+
if !s.isRunningUnsafe() {
384+
// This case can only happen if the service is disabled while
385+
// we're processing a request.
386+
return nil, ErrAccountServiceDisabled
387+
}
388+
389+
// Debit the account in the db.
390+
err := s.store.DebitAccount(ctx, accountID, amount)
391+
if err != nil {
392+
return nil, fmt.Errorf("unable to debit account: %w", err)
393+
}
394+
395+
return s.store.Account(ctx, accountID)
396+
}
397+
348398
// Account retrieves an account from the bolt DB and un-marshals it. If the
349399
// account cannot be found, then ErrAccNotFound is returned.
350400
func (s *InterceptorService) Account(ctx context.Context,

0 commit comments

Comments
 (0)