-
Notifications
You must be signed in to change notification settings - Fork 2
/
refund.go
39 lines (33 loc) · 1.44 KB
/
refund.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package refund
import (
sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
)
func RefundFees(supplyKeeper types.SupplyKeeper, ctx sdk.Context, acc sdk.AccAddress, refundFees sdk.Coins) error {
blockTime := ctx.BlockTime()
feeCollector := supplyKeeper.GetModuleAccount(ctx, types.FeeCollectorName)
coins := feeCollector.GetCoins()
if !refundFees.IsValid() {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFee, "invalid refund fee amount: %s", refundFees)
}
// verify the account has enough funds to pay for fees
_, hasNeg := coins.SafeSub(refundFees)
if hasNeg {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds,
"insufficient funds to refund for fees; %s < %s", coins, refundFees)
}
// Validate the account has enough "spendable" coins as this will cover cases
// such as vesting accounts.
spendableCoins := feeCollector.SpendableCoins(blockTime)
if _, hasNeg := spendableCoins.SafeSub(refundFees); hasNeg {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds,
"insufficient funds to pay for refund fees; %s < %s", spendableCoins, refundFees)
}
ctx.UpdateFromAccountCache(feeCollector, 0)
err := supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.FeeCollectorName, acc, refundFees)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
return nil
}