Skip to content

Commit

Permalink
Enhancement: Named err on tx not fully funded (#65)
Browse files Browse the repository at this point in the history
* added named error for tx.Fund func

* testing fix
  • Loading branch information
tigh-latte authored Oct 5, 2021
1 parent 37f94af commit 201dbcc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ on:
- '*'
branches:
- master
- v1
- 'v1'
pull_request:
branches:
- master
- v1
- 'v1'

jobs:
golangci:
Expand Down
22 changes: 16 additions & 6 deletions txinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ import (
"github.com/libsv/go-bt/v2/bscript"
)

// ErrNoUTXO signals the UTXOGetterFunc has reached the end of its input.
var ErrNoUTXO = errors.New("no remaining utxos")
var (
// ErrNoUTXO signals the UTXOGetterFunc has reached the end of its input.
ErrNoUTXO = errors.New("no remaining utxos")

// ErrInsufficientFunds insufficient funds provided for funding
ErrInsufficientFunds = errors.New("insufficient funds provided")
)

// UTXOGetterFunc is used for tx.Fund(...). It provides the amount of satoshis required
// for funding as `deficit`, and expects []*bt.UTXO to be returned containing
Expand Down Expand Up @@ -136,8 +141,10 @@ func (tx *Tx) FromUTXOs(utxos ...*UTXO) error {
// Note, this function works under the assumption that receiver *bt.Tx already has all the outputs
// which need covered.
//
// Example usage, for when working with a list:
// tx.Fund(ctx, bt.NewFeeQuote(), func(ctx context.Context, deficit satoshis) ([]*bt.UTXO, error) {
// If insufficient utxos are provided from the UTXOGetterFunc, a bt.ErrInsufficientFunds is returned.
//
// Example usage:
// if err := tx.Fund(ctx, bt.NewFeeQuote(), func(ctx context.Context, deficit satoshis) ([]*bt.UTXO, error) {
// utxos := make([]*bt.UTXO, 0)
// for _, f := range funds {
// deficit -= satoshis
Expand All @@ -152,7 +159,10 @@ func (tx *Tx) FromUTXOs(utxos ...*UTXO) error {
// }
// }
// return nil, bt.ErrNoUTXO
// })
// }); err != nil {
// if errors.Is(err, bt.ErrInsufficientFunds) { /* handle */ }
// return err
// }
func (tx *Tx) Fund(ctx context.Context, fq *FeeQuote, next UTXOGetterFunc) error {
deficit, err := tx.estimateDeficit(fq)
if err != nil {
Expand All @@ -178,7 +188,7 @@ func (tx *Tx) Fund(ctx context.Context, fq *FeeQuote, next UTXOGetterFunc) error
}
}
if deficit != 0 {
return errors.New("insufficient utxos provided")
return ErrInsufficientFunds
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions txinput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func TestTx_Fund(t *testing.T) {
return tx
}(),
utxos: []*bt.UTXO{},
expErr: errors.New("insufficient utxos provided"),
expErr: bt.ErrInsufficientFunds,
},
"getter with insufficient utxos errors": {
tx: func() *bt.Tx {
Expand Down Expand Up @@ -356,7 +356,7 @@ func TestTx_Fund(t *testing.T) {
txid, 0, script, 650,
}}
}(),
expErr: errors.New("insufficient utxos provided"),
expErr: bt.ErrInsufficientFunds,
},
"error is returned to the user": {
tx: func() *bt.Tx {
Expand Down

0 comments on commit 201dbcc

Please sign in to comment.