|
| 1 | +package ante |
| 2 | + |
| 3 | +import ( |
| 4 | + errorsmod "cosmossdk.io/errors" |
| 5 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 6 | + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" |
| 7 | + authante "github.com/cosmos/cosmos-sdk/x/auth/ante" |
| 8 | + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" |
| 9 | + |
| 10 | + "github.com/sideprotocol/side/x/btcbridge/types" |
| 11 | +) |
| 12 | + |
| 13 | +// FeeSponsorshipDecorator implements fee sponsorship |
| 14 | +// The gas fee is sponsored if the tx satisfies the sponsorship requirement |
| 15 | +// Fallback to the default DeductFeeDecorator otherwise |
| 16 | +type FeeSponsorshipDecorator struct { |
| 17 | + accountKeeper types.AccountKeeper |
| 18 | + bankKeeper types.BankKeeper |
| 19 | + feegrantKeeper FeeGrantKeeper |
| 20 | + btcbridgeKeeper BtcBridgeKeeper |
| 21 | + |
| 22 | + txFeeChecker authante.TxFeeChecker |
| 23 | + |
| 24 | + defaultFeeDecorator authante.DeductFeeDecorator |
| 25 | +} |
| 26 | + |
| 27 | +// NewFeeSponsorshipDecorator creates a new decorator for fee sponsorship |
| 28 | +func NewFeeSponsorshipDecorator(authKeeper types.AccountKeeper, bankKeeper types.BankKeeper, feegrantKeeper FeeGrantKeeper, btcbridgeKeeper BtcBridgeKeeper, txFeeChecker authante.TxFeeChecker) FeeSponsorshipDecorator { |
| 29 | + if txFeeChecker == nil { |
| 30 | + txFeeChecker = checkTxFeeWithValidatorMinGasPrices |
| 31 | + } |
| 32 | + |
| 33 | + return FeeSponsorshipDecorator{ |
| 34 | + accountKeeper: authKeeper, |
| 35 | + bankKeeper: bankKeeper, |
| 36 | + feegrantKeeper: feegrantKeeper, |
| 37 | + btcbridgeKeeper: btcbridgeKeeper, |
| 38 | + txFeeChecker: txFeeChecker, |
| 39 | + defaultFeeDecorator: authante.NewDeductFeeDecorator(authKeeper, bankKeeper, feegrantKeeper, txFeeChecker), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// AnteHandle implements sdk.AnteDecorator |
| 44 | +func (fsd FeeSponsorshipDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { |
| 45 | + feeTx, ok := tx.(sdk.FeeTx) |
| 46 | + if !ok { |
| 47 | + return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx") |
| 48 | + } |
| 49 | + |
| 50 | + if !simulate && ctx.BlockHeight() > 0 && feeTx.GetGas() == 0 { |
| 51 | + return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidGasLimit, "must provide positive gas") |
| 52 | + } |
| 53 | + |
| 54 | + var ( |
| 55 | + priority int64 |
| 56 | + err error |
| 57 | + ) |
| 58 | + |
| 59 | + fee := feeTx.GetFee() |
| 60 | + if !simulate { |
| 61 | + fee, priority, err = fsd.txFeeChecker(ctx, tx) |
| 62 | + if err != nil { |
| 63 | + return ctx, err |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if !fsd.checkSponsorship(ctx, tx, fee) { |
| 68 | + // fallback to default fee decorator |
| 69 | + return fsd.defaultFeeDecorator.AnteHandle(ctx, tx, simulate, next) |
| 70 | + } |
| 71 | + |
| 72 | + // sponsor fee |
| 73 | + if err := fsd.sponsorFee(ctx, fee); err != nil { |
| 74 | + return ctx, err |
| 75 | + } |
| 76 | + |
| 77 | + newCtx := ctx.WithPriority(priority) |
| 78 | + |
| 79 | + return next(newCtx, tx, simulate) |
| 80 | +} |
| 81 | + |
| 82 | +// sponsorFee performs the fee sponsorship for the given tx |
| 83 | +func (fsd FeeSponsorshipDecorator) sponsorFee(ctx sdk.Context, fee sdk.Coins) error { |
| 84 | + sponsorAddress := fsd.getFeeSponsorAddress() |
| 85 | + |
| 86 | + sponsorAccount := fsd.accountKeeper.GetAccount(ctx, sponsorAddress) |
| 87 | + if sponsorAccount == nil { |
| 88 | + return sdkerrors.ErrUnknownAddress.Wrapf("fee sponsor address: %s does not exist", sponsorAddress) |
| 89 | + } |
| 90 | + |
| 91 | + // deduct fees from the sponsor account |
| 92 | + if !fee.IsZero() { |
| 93 | + err := authante.DeductFees(fsd.bankKeeper, ctx, sponsorAccount, fee) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + events := sdk.Events{ |
| 100 | + sdk.NewEvent( |
| 101 | + sdk.EventTypeTx, |
| 102 | + sdk.NewAttribute(sdk.AttributeKeyFee, fee.String()), |
| 103 | + sdk.NewAttribute(sdk.AttributeKeyFeePayer, sponsorAddress.String()), |
| 104 | + ), |
| 105 | + } |
| 106 | + ctx.EventManager().EmitEvents(events) |
| 107 | + |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +// checkSponsorship returns true if the given tx should be sponsored, false otherwise |
| 112 | +// NOTE: The following requirements must be satisfied for fee sponsorship |
| 113 | +// 1. Fee sponsorship is enabled |
| 114 | +// 2. The tx is a BTCT transfer tx |
| 115 | +// 3. The gas fee does not exceed the maximum sponsorship fee |
| 116 | +// 4. The fee sponsor account has sufficient balances |
| 117 | +func (fsd FeeSponsorshipDecorator) checkSponsorship(ctx sdk.Context, tx sdk.Tx, fee sdk.Coins) bool { |
| 118 | + if !fsd.btcbridgeKeeper.FeeSponsorshipEnabled(ctx) { |
| 119 | + return false |
| 120 | + } |
| 121 | + |
| 122 | + if !fsd.isSponsorableTx(ctx, tx) { |
| 123 | + return false |
| 124 | + } |
| 125 | + |
| 126 | + if !fee.IsAllLTE(fsd.btcbridgeKeeper.MaxSponsorFee(ctx)) { |
| 127 | + return false |
| 128 | + } |
| 129 | + |
| 130 | + if !fsd.bankKeeper.SpendableCoins(ctx, fsd.getFeeSponsorAddress()).IsAllGTE(fee) { |
| 131 | + return false |
| 132 | + } |
| 133 | + |
| 134 | + return true |
| 135 | +} |
| 136 | + |
| 137 | +// isSponsorableTx returns true if the given tx is sponsorable, false otherwise |
| 138 | +func (fsd FeeSponsorshipDecorator) isSponsorableTx(ctx sdk.Context, tx sdk.Tx) bool { |
| 139 | + for _, m := range tx.GetMsgs() { |
| 140 | + switch msg := m.(type) { |
| 141 | + case *banktypes.MsgSend: |
| 142 | + denoms := msg.Amount.Denoms() |
| 143 | + if len(denoms) != 1 || denoms[0] != fsd.btcbridgeKeeper.BtcDenom(ctx) { |
| 144 | + return false |
| 145 | + } |
| 146 | + |
| 147 | + case *banktypes.MsgMultiSend: |
| 148 | + denoms := msg.Inputs[0].Coins.Denoms() |
| 149 | + if len(denoms) != 1 || denoms[0] != fsd.btcbridgeKeeper.BtcDenom(ctx) { |
| 150 | + return false |
| 151 | + } |
| 152 | + |
| 153 | + default: |
| 154 | + return false |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return true |
| 159 | +} |
| 160 | + |
| 161 | +// getFeeSponsorAddress gets the fee sponsor address |
| 162 | +func (fsd FeeSponsorshipDecorator) getFeeSponsorAddress() sdk.AccAddress { |
| 163 | + return fsd.accountKeeper.GetModuleAddress(types.FeeSponsorName) |
| 164 | +} |
0 commit comments