Skip to content

Commit

Permalink
Refactor queries, txs, & keeper
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsmith-2019 committed Sep 19, 2023
1 parent bd89399 commit 9de6640
Show file tree
Hide file tree
Showing 17 changed files with 498 additions and 602 deletions.
4 changes: 2 additions & 2 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func appModules(
ibcfee.NewAppModule(app.AppKeepers.IBCFeeKeeper),
tokenfactory.NewAppModule(app.AppKeepers.TokenFactoryKeeper, app.AppKeepers.AccountKeeper, app.AppKeepers.BankKeeper, app.GetSubspace(tokenfactorytypes.ModuleName)),
globalfee.NewAppModule(appCodec, app.AppKeepers.GlobalFeeKeeper, bondDenom),
feepay.NewAppModule(app.AppKeepers.FeePayKeeper, app.AppKeepers.AccountKeeper, app.GetSubspace(feepaytypes.ModuleName)),
feepay.NewAppModule(app.AppKeepers.FeePayKeeper, app.AppKeepers.AccountKeeper),
feeshare.NewAppModule(app.AppKeepers.FeeShareKeeper, app.AppKeepers.AccountKeeper, app.GetSubspace(feesharetypes.ModuleName)),
wasm.NewAppModule(appCodec, &app.AppKeepers.WasmKeeper, app.AppKeepers.StakingKeeper, app.AppKeepers.AccountKeeper, app.AppKeepers.BankKeeper, app.MsgServiceRouter(), app.GetSubspace(wasmtypes.ModuleName)),
ica.NewAppModule(&app.AppKeepers.ICAControllerKeeper, &app.AppKeepers.ICAHostKeeper),
Expand Down Expand Up @@ -191,7 +191,7 @@ func simulationModules(
wasm.NewAppModule(appCodec, &app.AppKeepers.WasmKeeper, app.AppKeepers.StakingKeeper, app.AppKeepers.AccountKeeper, app.AppKeepers.BankKeeper, app.MsgServiceRouter(), app.GetSubspace(wasmtypes.ModuleName)),
ibc.NewAppModule(app.AppKeepers.IBCKeeper),
transfer.NewAppModule(app.AppKeepers.TransferKeeper),
feepay.NewAppModule(app.AppKeepers.FeePayKeeper, app.AppKeepers.AccountKeeper, app.GetSubspace(feepaytypes.ModuleName)),
feepay.NewAppModule(app.AppKeepers.FeePayKeeper, app.AppKeepers.AccountKeeper),
feeshare.NewAppModule(app.AppKeepers.FeeShareKeeper, app.AppKeepers.AccountKeeper, app.GetSubspace(feesharetypes.ModuleName)),
ibcfee.NewAppModule(app.AppKeepers.IBCFeeKeeper),
}
Expand Down
6 changes: 2 additions & 4 deletions proto/juno/feepay/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ message QueryFeePayContract {
// QueryFeePayContractResponse defines the response for retrieving a single fee pay contract
message QueryFeePayContractResponse {
// contract defines the fee pay contract
FeePayContract contract = 1;
FeePayContract fee_pay_contract = 1;
}

// Message for querying a list of fee pay contracts
Expand All @@ -53,7 +53,7 @@ message QueryFeePayContracts {
// The response for querying all fee pay contracts
message QueryFeePayContractsResponse {
// A slice of all the stored fee pay contracts
repeated FeePayContract contracts = 1 [ (gogoproto.nullable) = false ];
repeated FeePayContract fee_pay_contracts = 1 [ (gogoproto.nullable) = false ];
// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
Expand Down Expand Up @@ -84,6 +84,4 @@ message QueryFeePayWalletIsEligible {
message QueryFeePayWalletIsEligibleResponse {
// The eligibility of the wallet for fee pay contract interactions
bool eligible = 1;
// The reason (if any) for the wallet being ineligible for fee pay contract interactions
string reason = 2;
}
4 changes: 2 additions & 2 deletions proto/juno/feepay/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ message MsgRegisterFeePayContract {
string sender_address = 1;

// The fee pay contract to register.
FeePayContract contract = 2;
FeePayContract fee_pay_contract = 2;
}

// The response message for registering a fee pay contract.
Expand Down Expand Up @@ -95,7 +95,7 @@ message MsgUpdateParams {
// authority is the address that controls the module (defaults to x/gov unless overwritten).
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// params defines the x/feeshare parameters to update.
// params defines the x/feepay parameters to update.
//
// NOTE: All parameters must be supplied.
Params params = 2 [(gogoproto.nullable) = false];
Expand Down
42 changes: 14 additions & 28 deletions x/feepay/ante/dedcuct_fee.go → x/feepay/ante/deduct_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type DeductFeeDecorator struct {
// type TxFeeChecker func(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error)
txFeeChecker ante.TxFeeChecker

// TODO: test this.
bondDenom string
}

Expand All @@ -58,7 +59,6 @@ func NewDeductFeeDecorator(fpk feepaykeeper.Keeper, gfk globalfeekeeper.Keeper,
func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
// return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}

Expand Down Expand Up @@ -151,9 +151,10 @@ func (dfd DeductFeeDecorator) handleZeroFees(ctx sdk.Context, deductFeesFromAcc
msg := tx.GetMsgs()[0]
cw := msg.(*wasmtypes.MsgExecuteContract)

// We need to check if it is a valid contract. Utilize the FeePay Keeper for validation
if !dfd.feepayKeeper.IsRegisteredContract(ctx, cw.GetContract()) {
return feepaytypes.ErrContractNotRegistered
// Get the fee pay contract
feepayContract, err := dfd.feepayKeeper.GetContract(ctx, cw.GetContract())
if err != nil {
return errorsmod.Wrapf(err, "error getting contract %s", cw.GetContract())
}

// Get the fee price in the chain denom
Expand All @@ -176,19 +177,14 @@ func (dfd DeductFeeDecorator) handleZeroFees(ctx sdk.Context, deductFeesFromAcc

ctx.Logger().Error("HandleZeroFees", "RequiredFee", requiredFee)

// Get the fee pay contract
feepayContract, err := dfd.feepayKeeper.GetContract(ctx, cw.GetContract())
if err != nil {
return err
}

// Check if wallet exceeded usage limit on contract
if dfd.feepayKeeper.HasWalletExceededUsageLimit(ctx, cw.GetContract(), deductFeesFromAcc.GetAddress().String()) {
accBech32 := deductFeesFromAcc.GetAddress().String()
if dfd.feepayKeeper.HasWalletExceededUsageLimit(ctx, feepayContract, accBech32) {
return errorsmod.Wrapf(feepaytypes.ErrWalletExceededUsageLimit, "wallet has exceeded usage limit (%d)", feepayContract.WalletLimit)
}

// Check if the contract has enough funds to cover the fee
if !dfd.feepayKeeper.CanContractCoverFee(ctx, cw.GetContract(), requiredFee.Uint64()) {
if !dfd.feepayKeeper.CanContractCoverFee(ctx, feepayContract, requiredFee.Uint64()) {
return errorsmod.Wrapf(feepaytypes.ErrContractNotEnoughFunds, "contract has insufficient funds; expected: %d, got: %d", requiredFee.Uint64(), feepayContract.Balance)
}

Expand All @@ -198,23 +194,16 @@ func (dfd DeductFeeDecorator) handleZeroFees(ctx sdk.Context, deductFeesFromAcc
ctx.Logger().Error("HandleZeroFees", "Payment", payment)

// Cover the fees of the transaction, send from FeePay Module to FeeCollector Module
err = dfd.bankKeeper.SendCoinsFromModuleToModule(ctx, feepaytypes.ModuleName, types.FeeCollectorName, payment)
if err != nil {
if err := dfd.bankKeeper.SendCoinsFromModuleToModule(ctx, feepaytypes.ModuleName, types.FeeCollectorName, payment); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, "error transfering funds from FeePay to FeeCollector; %s", err)
}

// Deduct the fee from the contract balance
dfd.feepayKeeper.UpdateContractBalance(ctx, cw.GetContract(), feepayContract.Balance-requiredFee.Uint64())
dfd.feepayKeeper.SetContractBalance(ctx, feepayContract, feepayContract.Balance-requiredFee.Uint64())

// Increment wallet usage
uses, err := dfd.feepayKeeper.GetContractUses(ctx, cw.GetContract(), deductFeesFromAcc.GetAddress().String())
if err != nil {
return err
}
dfd.feepayKeeper.IncrementContractUses(ctx, feepayContract, accBech32, 1)

if err := dfd.feepayKeeper.SetContractUses(ctx, cw.GetContract(), deductFeesFromAcc.GetAddress().String(), uses+1); err != nil {
return err
}
return nil
}

Expand All @@ -224,7 +213,6 @@ func DeductFees(bankKeeper types.BankKeeper, ctx sdk.Context, acc types.AccountI
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "invalid fee amount: %s", fees)
}

// TODO: if 0 fees are sent, then the module account needs to pay it. (prepay module) ELSE have the standard user
err := bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), types.FeeCollectorName, fees)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
Expand All @@ -234,6 +222,7 @@ func DeductFees(bankKeeper types.BankKeeper, ctx sdk.Context, acc types.AccountI
}

// from the SDK pulled out
// TODO: modify this in part with globalfee for bypasses with ibc, force set 0? need an override in the event of DOS attacks
func checkTxFeeWithValidatorMinGasPrices(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
Expand All @@ -246,6 +235,7 @@ func checkTxFeeWithValidatorMinGasPrices(ctx sdk.Context, tx sdk.Tx) (sdk.Coins,
// Ensure that the provided fees meet a minimum threshold for the validator,
// if this is a CheckTx. This is only for local mempool purposes, and thus
// is only ran on check tx.
// TODO: see if we can remove, since we do call this twice.
if ctx.IsCheckTx() && !isValidFeePayTransaction(ctx, tx, feeTx.GetFee()) {
minGasPrices := ctx.MinGasPrices()
if !minGasPrices.IsZero() {
Expand Down Expand Up @@ -292,15 +282,11 @@ func getTxPriority(fee sdk.Coins, gas int64) int64 {
// Check if a transaction should be processed as a FeePay transaction.
// A valid FeePay transaction has no fee, and only 1 message for executing a contract.
func isValidFeePayTransaction(ctx sdk.Context, tx sdk.Tx, fee sdk.Coins) bool {

ctx.Logger().Error("FeePayAnte", "IsZero", fee.IsZero(), "Msgs", len(tx.GetMsgs()))
// TODO: Future allow for multiple msgs.

// Check if fee is zero, and tx has only 1 message for executing a contract
if fee.IsZero() && len(tx.GetMsgs()) == 1 {
_, ok := (tx.GetMsgs()[0]).(*wasmtypes.MsgExecuteContract)

ctx.Logger().Error("FeePayAnte", "IsCWExecuteContract", ok)

return ok
}

Expand Down
2 changes: 1 addition & 1 deletion x/feepay/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func NewQueryFeePayContractUsage() *cobra.Command {
// Query if a wallet is eligible
func NewQueryWalletIsEligible() *cobra.Command {
cmd := &cobra.Command{
Use: "eligible [contract_address] [wallet_address]",
Use: "is-eligible [contract_address] [wallet_address]",
Short: "Query if a wallet is eligible to interact with a FeePay contract",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
6 changes: 3 additions & 3 deletions x/feepay/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewTxCmd() *cobra.Command {
NewRegisterFeePayContract(),
NewUnregisterFeePayContract(),
NewFundFeePayContract(),
// TODO: update wallet limit for usage. (0 = disabled)
)
return txCmd
}
Expand All @@ -38,7 +39,6 @@ func NewRegisterFeePayContract() *cobra.Command {
cmd := &cobra.Command{
Use: "register [contract_bech32] [wallet_limit]",
Short: "Register a contract for fee pay. Only the contract admin can register a contract.",
Long: "Register a contract for fee pay. Only the contract admin can register a contract.",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
Expand All @@ -62,8 +62,8 @@ func NewRegisterFeePayContract() *cobra.Command {
}

msg := &types.MsgRegisterFeePayContract{
SenderAddress: deployer_address.String(),
Contract: fpc,
SenderAddress: deployer_address.String(),
FeePayContract: fpc,
}

if err := msg.ValidateBasic(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/feepay/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func InitGenesis(
k keeper.Keeper,
data types.GenesisState,
) {

// TODO: impl, just remember that ParamsKey is going to be nil?
}

// ExportGenesis export module state
Expand Down
Loading

0 comments on commit 9de6640

Please sign in to comment.