Skip to content

Commit

Permalink
Add validation checks & fix send all logic in Transfer rpc (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
altafan authored Jan 2, 2024
1 parent fd24a9f commit 2f60093
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 7 additions & 0 deletions internal/core/application/transaction_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ func (ts *TransactionService) Transfer(
if len(balance) <= 0 {
return "", fmt.Errorf("account %s has 0 balance", accountName)
}
for asset, amount := range outputs.totalAmountByAsset() {
if (balance[asset].Confirmed + balance[asset].Unconfirmed) < amount {
return "", fmt.Errorf(
"not enough funds to cover amount %d of asset %s", amount, asset,
)
}
}

utxos, err := utxoRepo.GetSpendableUtxosForAccount(
ctx, account.Namespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,29 @@ func (s *selector) SelectUtxos(
})

targetUtxos := make([]*domain.Utxo, 0)
totalAmount := uint64(0)
balance := uint64(0)
for i := range utxos {
utxo := utxos[i]
if utxo.IsConfidential() && !utxo.IsRevealed() {
return nil, 0, ErrBlindedUtxos
}
if utxo.Asset == targetAsset {
targetUtxos = append(targetUtxos, utxo)
balance += utxo.Value
}
}

if targetAmount == balance {
return targetUtxos, 0, nil
}

indexes := selectUtxos(targetAmount, targetUtxos)
if len(indexes) <= 0 {
return nil, 0, ErrTargetAmountNotReached
}

selectedUtxos := make([]*domain.Utxo, 0)
totalAmount := uint64(0)
for _, v := range indexes {
totalAmount += targetUtxos[v].Value
selectedUtxos = append(selectedUtxos, targetUtxos[v])
Expand Down

0 comments on commit 2f60093

Please sign in to comment.