Skip to content

Commit

Permalink
refactor: processRefund function signature
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp committed Dec 6, 2023
1 parent 9db814c commit effd1a2
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions app/posthandler/unspent_gas_refund_decorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ func (frd UnspentGasRefundDecorator) maybeRefund(ctx sdk.Context, tx sdk.Tx) err

coinsToRefund := getCoinsToRefund(gasMeter, feeTx)
refundRecipient := getRefundRecipient(feeTx)
refundRecipientAccount := frd.accountKeeper.GetAccount(ctx, refundRecipient)
if refundRecipientAccount == nil {
return errors.Wrapf(sdkerrors.ErrUnknownAddress, "refund recipient address: %s does not exist", refundRecipientAccount)
}

if err := frd.processRefund(frd.bankKeeper, ctx, refundRecipientAccount, coinsToRefund); err != nil {
if err := frd.processRefund(ctx, coinsToRefund, refundRecipient); err != nil {
return err
}

Expand All @@ -76,19 +72,22 @@ func getCoinsToRefund(gasMeter sdk.GasMeter, feeTx sdk.FeeTx) sdk.Coins {
}

// processRefund sends amountToRefund from the fee collector module account to the refundRecipient.
func (frd UnspentGasRefundDecorator) processRefund(bankKeeper types.BankKeeper, ctx sdk.Context, refundRecipient types.AccountI, amountToRefund sdk.Coins) error {
to := refundRecipient.GetAddress()
func (frd UnspentGasRefundDecorator) processRefund(ctx sdk.Context, amountToRefund sdk.Coins, refundRecipient sdk.AccAddress) error {
from := frd.accountKeeper.GetModuleAddress(types.FeeCollectorName)
if from == nil {
return fmt.Errorf("fee collector module account (%s) has not been set", types.FeeCollectorName)
}

if refundRecipientAccount := frd.accountKeeper.GetAccount(ctx, refundRecipient); refundRecipientAccount == nil {
return errors.Wrapf(sdkerrors.ErrUnknownAddress, "refund recipient address: %s does not exist", refundRecipientAccount)
}

if !amountToRefund.IsValid() {
return fmt.Errorf("invalid amount to refund: %s", amountToRefund)
}

if err := bankKeeper.SendCoins(ctx, from, to, amountToRefund); err != nil {
return errors.Wrapf(err, "error refunding %s from fee collector module account to %s", amountToRefund, to)
if err := frd.bankKeeper.SendCoins(ctx, from, refundRecipient, amountToRefund); err != nil {
return errors.Wrapf(err, "error refunding %s from fee collector module account to %s", amountToRefund, refundRecipient)
}

return nil
Expand Down

0 comments on commit effd1a2

Please sign in to comment.