From 201dbcc37ae92d727a3c890d4bab3ccac5a6c442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=ADghearn=C3=A1n=20Carroll?= Date: Tue, 5 Oct 2021 18:46:37 +0100 Subject: [PATCH] Enhancement: Named err on tx not fully funded (#65) * added named error for tx.Fund func * testing fix --- .github/workflows/run-tests.yml | 4 ++-- txinput.go | 22 ++++++++++++++++------ txinput_test.go | 4 ++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 05297cf6..31851c55 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -6,11 +6,11 @@ on: - '*' branches: - master - - v1 + - 'v1' pull_request: branches: - master - - v1 + - 'v1' jobs: golangci: diff --git a/txinput.go b/txinput.go index 1be5131f..1d296bc2 100644 --- a/txinput.go +++ b/txinput.go @@ -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 @@ -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 @@ -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 { @@ -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 diff --git a/txinput_test.go b/txinput_test.go index f247c9a3..19c2849d 100644 --- a/txinput_test.go +++ b/txinput_test.go @@ -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 { @@ -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 {