Skip to content

Commit

Permalink
Merge pull request #54 from hyle-team/epic/staking-limit-and-multisig
Browse files Browse the repository at this point in the history
Epic/staking limit and multisig
  • Loading branch information
EduardMikhrin authored Nov 21, 2024
2 parents 5de12d7 + 46102c8 commit 6e784ed
Show file tree
Hide file tree
Showing 115 changed files with 36,387 additions and 21,042 deletions.
33 changes: 24 additions & 9 deletions app/ante/evm/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,11 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
return ctx, errorsmod.Wrapf(err, "failed to verify the fees")
}

// If the account balance is not sufficient, try to withdraw enough staking rewards
err = anteutils.ClaimStakingRewardsIfNecessary(ctx, egcd.bankKeeper, egcd.distributionKeeper, egcd.stakingKeeper, from, fees)
if err != nil {
return ctx, err
}

err = egcd.evmKeeper.DeductTxCostsFromUserBalance(ctx, fees, common.HexToAddress(msgEthTx.From))
if err != nil {
return ctx, errorsmod.Wrapf(err, "failed to deduct transaction costs from user balance")
// this will check if the sender has enough balance to pay for the fees
// if not, it will try to claim enough staking rewards to cover the fees
// if fee is zero, it will skip the deduction
if err = egcd.deductFee(ctx, fees, from); err != nil {
return ctx, errorsmod.Wrap(err, "failed to deduct fee")
}

events = append(events,
Expand Down Expand Up @@ -261,6 +257,25 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
return next(newCtx, tx, simulate)
}

// deductFee checks if the fee payer has enough funds to pay for the fees and deducts them.
// If the spendable balance is not enough, it tries to claim enough staking rewards to cover the fees.
func (egcd EthGasConsumeDecorator) deductFee(ctx sdk.Context, fees sdk.Coins, feePayer sdk.AccAddress) error {
if fees.IsZero() {
return nil
}

// If the account balance is not sufficient, try to withdraw enough staking rewards
if err := anteutils.ClaimStakingRewardsIfNecessary(ctx, egcd.bankKeeper, egcd.distributionKeeper, egcd.stakingKeeper, feePayer, fees); err != nil {
return err
}

if err := egcd.evmKeeper.DeductTxCostsFromUserBalance(ctx, fees, common.BytesToAddress(feePayer)); err != nil {
return errorsmod.Wrapf(err, "failed to deduct transaction costs from user balance")
}

return nil
}

// CanTransferDecorator checks if the sender is allowed to transfer funds according to the EVM block
// context rules.
type CanTransferDecorator struct {
Expand Down
22 changes: 22 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import (
nfttypes "github.com/cosmos/cosmos-sdk/x/nft/types"
"github.com/hyle-team/bridgeless-core/v12/docs"
"github.com/hyle-team/bridgeless-core/v12/x/bridge"
multisigkeeper "github.com/hyle-team/bridgeless-core/v12/x/multisig/keeper"

"github.com/hyle-team/bridgeless-core/v12/x/multisig"
multisigtypes "github.com/hyle-team/bridgeless-core/v12/x/multisig/types"

"io"
"net/http"
Expand Down Expand Up @@ -244,6 +248,7 @@ var (
mint.AppModuleBasic{},
nft.AppModuleBasic{},
bridge.AppModuleBasic{},
multisig.AppModuleBasic{},
)

// module account permissions
Expand All @@ -262,6 +267,7 @@ var (
minttypes.ModuleName: {authtypes.Minter, authtypes.Staking, authtypes.Burner},
nfttypes.ModuleName: nil,
bridgetypes.ModuleName: nil,
multisigtypes.ModuleName: nil,
}

// module accounts that are allowed to receive tokens
Expand Down Expand Up @@ -331,6 +337,8 @@ type Bridge struct {
NFTKeeper *nftkeeper.Keeper
BridgeKeeper *bridgekeeper.Keeper

MultisigKeeper multisigkeeper.Keeper

// the module manager
mm *module.Manager

Expand Down Expand Up @@ -392,6 +400,7 @@ func NewBridge(
minttypes.StoreKey,
nfttypes.StoreKey,
bridgetypes.StoreKey,
multisigtypes.StoreKey,
)

// Add the EVM transient store key
Expand Down Expand Up @@ -465,6 +474,7 @@ func NewBridge(
appCodec, keys[distrtypes.StoreKey], app.GetSubspace(distrtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
app.StakingKeeper, app.NFTKeeper, authtypes.FeeCollectorName,
)

app.SlashingKeeper = slashingkeeper.NewKeeper(
appCodec, keys[slashingtypes.StoreKey], app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName),
)
Expand All @@ -478,6 +488,14 @@ func NewBridge(

tracer := cast.ToString(appOpts.Get(srvflags.EVMTracer))

app.MultisigKeeper = *multisigkeeper.NewKeeper(
appCodec,
keys[multisigtypes.StoreKey],
keys[multisigtypes.MemStoreKey],
app.MsgServiceRouter(),
app.AccountKeeper,
)

// Create Ethermint keepers
app.FeeMarketKeeper = feemarketkeeper.NewKeeper(
appCodec, authtypes.NewModuleAddress(govtypes.ModuleName),
Expand Down Expand Up @@ -701,6 +719,7 @@ func NewBridge(
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper),
nft.NewAppModule(appCodec, *app.NFTKeeper, app.AccountKeeper, app.BankKeeper),
bridge.NewAppModule(appCodec, *app.BridgeKeeper),
multisig.NewAppModule(appCodec, app.MultisigKeeper, app.AccountKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -740,6 +759,7 @@ func NewBridge(
minttypes.ModuleName,
nfttypes.ModuleName,
bridgetypes.ModuleName,
multisigtypes.ModuleName,
)

// NOTE: fee market module must go last in order to retrieve the block gas used.
Expand Down Expand Up @@ -777,6 +797,7 @@ func NewBridge(
minttypes.ModuleName,
nfttypes.ModuleName,
bridgetypes.ModuleName,
multisigtypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -823,6 +844,7 @@ func NewBridge(
minttypes.ModuleName,
nfttypes.ModuleName,
bridgetypes.ModuleName,
multisigtypes.ModuleName,
)

app.mm.RegisterInvariants(&app.CrisisKeeper)
Expand Down
Loading

0 comments on commit 6e784ed

Please sign in to comment.