diff --git a/pkg/postage/postagecontract/contract.go b/pkg/postage/postagecontract/contract.go index 94c3dc99061..e24d74b00f5 100644 --- a/pkg/postage/postagecontract/contract.go +++ b/pkg/postage/postagecontract/contract.go @@ -199,13 +199,7 @@ func (c *postageContract) sendTransaction(ctx context.Context, callData []byte, } if receipt.Status == 0 { - err := transaction.ErrTransactionReverted - if res, cErr := c.transactionService.Call(ctx, request); cErr == nil { - if reason, uErr := abi.UnpackRevert(res); uErr == nil { - err = fmt.Errorf("%w: reason: %s", err, reason) - } - } - return nil, err + return nil, c.transactionService.UnwrapRevertReason(ctx, request, transaction.ErrTransactionReverted) } return receipt, nil diff --git a/pkg/storageincentives/agent.go b/pkg/storageincentives/agent.go index b856625025e..16e7770c859 100644 --- a/pkg/storageincentives/agent.go +++ b/pkg/storageincentives/agent.go @@ -24,7 +24,7 @@ import ( "github.com/ethersphere/bee/pkg/storage" "github.com/ethersphere/bee/pkg/storageincentives/redistribution" "github.com/ethersphere/bee/pkg/storageincentives/staking" - storer "github.com/ethersphere/bee/pkg/storer" + "github.com/ethersphere/bee/pkg/storer" "github.com/ethersphere/bee/pkg/swarm" "github.com/ethersphere/bee/pkg/transaction" ) diff --git a/pkg/storageincentives/redistribution/redistribution.go b/pkg/storageincentives/redistribution/redistribution.go index 2c163f5a76c..c35eda83592 100644 --- a/pkg/storageincentives/redistribution/redistribution.go +++ b/pkg/storageincentives/redistribution/redistribution.go @@ -191,7 +191,7 @@ func (c *contract) sendAndWait(ctx context.Context, request *transaction.TxReque } if receipt.Status == 0 { - return txHash, transaction.ErrTransactionReverted + return txHash, c.txService.UnwrapRevertReason(ctx, request, transaction.ErrTransactionReverted) } return txHash, nil } diff --git a/pkg/transaction/mock/transaction.go b/pkg/transaction/mock/transaction.go index f9a557f00be..5138d8ef907 100644 --- a/pkg/transaction/mock/transaction.go +++ b/pkg/transaction/mock/transaction.go @@ -97,6 +97,10 @@ func (m *transactionServiceMock) TransactionFee(ctx context.Context, txHash comm return big.NewInt(0), nil } +func (m *transactionServiceMock) UnwrapReason(_ context.Context, _ *transaction.TxRequest, err error) error { + return err +} + // Option is the option passed to the mock Chequebook service type Option interface { apply(*transactionServiceMock) diff --git a/pkg/transaction/transaction.go b/pkg/transaction/transaction.go index d8cb6d797d3..c4246631023 100644 --- a/pkg/transaction/transaction.go +++ b/pkg/transaction/transaction.go @@ -14,6 +14,7 @@ import ( "time" "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethersphere/bee/pkg/crypto" @@ -94,6 +95,9 @@ type Service interface { CancelTransaction(ctx context.Context, originalTxHash common.Hash) (common.Hash, error) // TransactionFee retrieves the transaction fee TransactionFee(ctx context.Context, txHash common.Hash) (*big.Int, error) + // UnwrapRevertReason tries to unwrap the revert reason if the given error is not nil. + // The original error is wrapped in case the revert reason exists. + UnwrapRevertReason(ctx context.Context, req *TxRequest, err error) error } type transactionService struct { @@ -584,3 +588,17 @@ func (t *transactionService) TransactionFee(ctx context.Context, txHash common.H } return trx.Cost(), nil } + +func (t *transactionService) UnwrapRevertReason(ctx context.Context, req *TxRequest, err error) error { + if err == nil { + return nil + } + + if res, cErr := t.Call(ctx, req); cErr == nil { + if reason, uErr := abi.UnpackRevert(res); uErr == nil { + err = fmt.Errorf("%w: reason: %s", err, reason) + } + } + + return err +}