Skip to content

Commit a851302

Browse files
accounts: implement UpdateBalance endpoint
With all underlying functionality in place to support updating an existing off-chain account’s balance by a specified amount, this commit implements the `UpdateBalance` endpoint to utilize that functionality.
1 parent a096de8 commit a851302

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

accounts/rpcserver.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,47 @@ func (s *RPCServer) UpdateAccount(ctx context.Context,
137137
func (s *RPCServer) UpdateBalance(ctx context.Context,
138138
req *litrpc.UpdateAccountBalanceRequest) (*litrpc.Account, error) {
139139

140-
return nil, fmt.Errorf("not implemented")
140+
var (
141+
isAddition bool
142+
amount lnwire.MilliSatoshi
143+
)
144+
145+
switch reqType := req.GetUpdate().(type) {
146+
case *litrpc.UpdateAccountBalanceRequest_Add:
147+
log.Infof("[addbalance] id=%s, label=%v, amount=%d",
148+
req.Id, req.Label, reqType.Add)
149+
150+
isAddition = true
151+
amount = lnwire.MilliSatoshi(reqType.Add * 1000)
152+
153+
case *litrpc.UpdateAccountBalanceRequest_Deduct:
154+
log.Infof("[deductbalance] id=%s, label=%v, amount=%d",
155+
req.Id, req.Label, reqType.Deduct)
156+
157+
isAddition = false
158+
amount = lnwire.MilliSatoshi(reqType.Deduct * 1000)
159+
160+
}
161+
162+
if amount <= 0 {
163+
return nil, fmt.Errorf("amount %v must be greater than 0",
164+
int64(amount/1000))
165+
}
166+
167+
accountID, err := s.findAccount(ctx, req.Id, req.Label)
168+
if err != nil {
169+
return nil, err
170+
}
171+
172+
// Ask the service to update the account.
173+
account, err := s.service.UpdateBalance(
174+
ctx, accountID, amount, isAddition,
175+
)
176+
if err != nil {
177+
return nil, err
178+
}
179+
180+
return marshalAccount(account), nil
141181
}
142182

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

accounts/service.go

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

348+
// UpdateBalance adds or deducts an amount from an existing account in the
349+
// account database.
350+
func (s *InterceptorService) UpdateBalance(ctx context.Context,
351+
accountID AccountID, amount lnwire.MilliSatoshi,
352+
isAddition bool) (*OffChainBalanceAccount, error) {
353+
354+
s.Lock()
355+
defer s.Unlock()
356+
357+
// As this function updates account balances, we require that the
358+
// service is running before we execute it.
359+
if !s.isRunningUnsafe() {
360+
// This case can only happen if the service is disabled while
361+
// we're processing a request.
362+
return nil, ErrAccountServiceDisabled
363+
}
364+
365+
// Adjust the balance of the account in the db.
366+
err := s.store.AdjustAccountBalance(ctx, accountID, amount, isAddition)
367+
if err != nil {
368+
return nil, fmt.Errorf("unable to update account: %w", err)
369+
}
370+
371+
return s.store.Account(ctx, accountID)
372+
}
373+
348374
// Account retrieves an account from the bolt DB and un-marshals it. If the
349375
// account cannot be found, then ErrAccNotFound is returned.
350376
func (s *InterceptorService) Account(ctx context.Context,

0 commit comments

Comments
 (0)