Skip to content
41 changes: 40 additions & 1 deletion accounts/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,46 @@ func (s *RPCServer) UpdateAccount(ctx context.Context,
func (s *RPCServer) UpdateBalance(ctx context.Context,
req *litrpc.UpdateAccountBalanceRequest) (*litrpc.Account, error) {

return nil, fmt.Errorf("not implemented")
var (
isAddition bool
amount lnwire.MilliSatoshi
)

switch reqType := req.GetUpdate().(type) {
case *litrpc.UpdateAccountBalanceRequest_Add:
log.Infof("[addbalance] id=%s, label=%v, amount=%d",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think of using "credit/debit" instead of "add/deduct"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah definitely! I was struggling with naming, and agree that credit & debit is better.

req.Id, req.Label, reqType.Add)

isAddition = true
amount = lnwire.MilliSatoshi(reqType.Add * 1000)

case *litrpc.UpdateAccountBalanceRequest_Deduct:
log.Infof("[deductbalance] id=%s, label=%v, amount=%d",
req.Id, req.Label, reqType.Deduct)

isAddition = false
amount = lnwire.MilliSatoshi(reqType.Deduct * 1000)
}

if amount <= 0 {
return nil, fmt.Errorf("amount %v must be greater than 0",
int64(amount/1000))
}
Comment on lines +161 to +164
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use uints in the proto definitions then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Addressed in the new PR.


accountID, err := s.findAccount(ctx, req.Id, req.Label)
if err != nil {
return nil, err
}

// Ask the service to update the account.
account, err := s.service.UpdateBalance(
ctx, accountID, amount, isAddition,
)
if err != nil {
return nil, err
}

return marshalAccount(account), nil
}

// ListAccounts returns all accounts that are currently stored in the account
Expand Down
26 changes: 26 additions & 0 deletions accounts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,32 @@ func (s *InterceptorService) UpdateAccount(ctx context.Context,
return s.store.Account(ctx, accountID)
}

// UpdateBalance adds or deducts an amount from an existing account in the
// account database.
func (s *InterceptorService) UpdateBalance(ctx context.Context,
accountID AccountID, amount lnwire.MilliSatoshi,
isAddition bool) (*OffChainBalanceAccount, error) {

s.Lock()
defer s.Unlock()

// As this function updates account balances, we require that the
// service is running before we execute it.
if !s.isRunningUnsafe() {
// This case can only happen if the service is disabled while
// we're processing a request.
return nil, ErrAccountServiceDisabled
}

// Adjust the balance of the account in the db.
err := s.store.AdjustAccountBalance(ctx, accountID, amount, isAddition)
if err != nil {
return nil, fmt.Errorf("unable to update account: %w", err)
}

return s.store.Account(ctx, accountID)
}

// Account retrieves an account from the bolt DB and un-marshals it. If the
// account cannot be found, then ErrAccNotFound is returned.
func (s *InterceptorService) Account(ctx context.Context,
Expand Down