-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
3,023 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package keeper | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/White-Whale-Defi-Platform/migaloo-chain/v3/x/feeburn/keeper" | ||
"github.com/White-Whale-Defi-Platform/migaloo-chain/v3/x/feeburn/types" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
"github.com/cosmos/cosmos-sdk/store" | ||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tendermint/tendermint/libs/log" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
tmdb "github.com/tendermint/tm-db" | ||
) | ||
|
||
func FeeburnKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { | ||
storeKey := sdk.NewKVStoreKey(types.StoreKey) | ||
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) | ||
|
||
db := tmdb.NewMemDB() | ||
stateStore := store.NewCommitMultiStore(db) | ||
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db) | ||
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil) | ||
require.NoError(t, stateStore.LoadLatestVersion()) | ||
|
||
registry := codectypes.NewInterfaceRegistry() | ||
cdc := codec.NewProtoCodec(registry) | ||
|
||
k := keeper.NewKeeper( | ||
cdc, | ||
storeKey, | ||
memStoreKey, | ||
authtypes.NewModuleAddress(govtypes.ModuleName), | ||
) | ||
|
||
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()) | ||
|
||
// Initialize params | ||
_ = k.SetParams(ctx, types.DefaultParams()) | ||
|
||
return k, ctx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Package nullify provides methods to init nil values structs for test assertion. | ||
package nullify | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
var ( | ||
coinType = reflect.TypeOf(sdk.Coin{}) | ||
coinsType = reflect.TypeOf(sdk.Coins{}) | ||
) | ||
|
||
// Fill analyze all struct fields and slices with | ||
// reflection and initialize the nil and empty slices, | ||
// structs, and pointers. | ||
func Fill(x interface{}) interface{} { | ||
v := reflect.Indirect(reflect.ValueOf(x)) | ||
switch v.Kind() { | ||
case reflect.Slice: | ||
for i := 0; i < v.Len(); i++ { | ||
obj := v.Index(i) | ||
objPt := reflect.NewAt(obj.Type(), unsafe.Pointer(obj.UnsafeAddr())).Interface() | ||
objPt = Fill(objPt) | ||
obj.Set(reflect.ValueOf(objPt)) | ||
} | ||
case reflect.Struct: | ||
for i := 0; i < v.NumField(); i++ { | ||
f := reflect.Indirect(v.Field(i)) | ||
if !f.CanSet() { | ||
continue | ||
} | ||
switch f.Kind() { | ||
case reflect.Slice: | ||
f.Set(reflect.MakeSlice(f.Type(), 0, 0)) | ||
case reflect.Struct: | ||
switch f.Type() { | ||
case coinType: | ||
coin := reflect.New(coinType).Interface() | ||
s := reflect.ValueOf(coin).Elem() | ||
f.Set(s) | ||
case coinsType: | ||
coins := reflect.New(coinsType).Interface() | ||
s := reflect.ValueOf(coins).Elem() | ||
f.Set(s) | ||
default: | ||
objPt := reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).Interface() | ||
s := Fill(objPt) | ||
f.Set(reflect.ValueOf(s)) | ||
} | ||
} | ||
} | ||
} | ||
return reflect.Indirect(v).Interface() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package sample | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// AccAddress returns a sample account address | ||
func AccAddress() string { | ||
pk := ed25519.GenPrivKey().PubKey() | ||
addr := pk.Address() | ||
return sdk.AccAddress(addr).String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ante | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// BankKeeper defines the contract needed for supply related APIs (noalias) | ||
type BankKeeper interface { | ||
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error | ||
BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package ante | ||
|
||
import ( | ||
"fmt" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
sdkmath "cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/cosmos/cosmos-sdk/x/auth/types" | ||
|
||
feeburnkeeper "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/x/feeburn/keeper" | ||
"github.com/cosmos/cosmos-sdk/x/auth/ante" | ||
) | ||
|
||
// DeductFeeDecorator deducts fees from the first signer of the tx | ||
// If the first signer does not have the funds to pay for the fees, return with InsufficientFunds error | ||
// Call next AnteHandler if fees successfully deducted | ||
// CONTRACT: Tx must implement FeeTx interface to use DeductFeeDecorator | ||
type DeductFeeDecorator struct { | ||
accountKeeper ante.AccountKeeper | ||
bankKeeper BankKeeper | ||
feegrantKeeper ante.FeegrantKeeper | ||
txFeeChecker ante.TxFeeChecker | ||
feeburnKeeper feeburnkeeper.Keeper | ||
} | ||
|
||
func NewDeductFeeDecorator(ak ante.AccountKeeper, bk BankKeeper, fk ante.FeegrantKeeper, tfc ante.TxFeeChecker, fbk feeburnkeeper.Keeper) DeductFeeDecorator { | ||
if tfc == nil { | ||
tfc = checkTxFeeWithValidatorMinGasPrices | ||
} | ||
|
||
return DeductFeeDecorator{ | ||
accountKeeper: ak, | ||
bankKeeper: bk, | ||
feegrantKeeper: fk, | ||
txFeeChecker: tfc, | ||
feeburnKeeper: fbk, | ||
} | ||
} | ||
|
||
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") | ||
} | ||
|
||
if !simulate && ctx.BlockHeight() > 0 && feeTx.GetGas() == 0 { | ||
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidGasLimit, "must provide positive gas") | ||
} | ||
|
||
var ( | ||
priority int64 | ||
err error | ||
) | ||
|
||
fee := feeTx.GetFee() | ||
if !simulate { | ||
fee, priority, err = dfd.txFeeChecker(ctx, tx) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
} | ||
if err := dfd.checkDeductFee(ctx, tx, fee); err != nil { | ||
return ctx, err | ||
} | ||
|
||
newCtx := ctx.WithPriority(priority) | ||
|
||
return next(newCtx, tx, simulate) | ||
} | ||
|
||
func (dfd DeductFeeDecorator) checkDeductFee(ctx sdk.Context, sdkTx sdk.Tx, fee sdk.Coins) error { | ||
feeTx, ok := sdkTx.(sdk.FeeTx) | ||
if !ok { | ||
return errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx") | ||
} | ||
|
||
if addr := dfd.accountKeeper.GetModuleAddress(types.FeeCollectorName); addr == nil { | ||
return fmt.Errorf("fee collector module account (%s) has not been set", types.FeeCollectorName) | ||
} | ||
|
||
feePayer := feeTx.FeePayer() | ||
feeGranter := feeTx.FeeGranter() | ||
deductFeesFrom := feePayer | ||
|
||
// if feegranter set deduct fee from feegranter account. | ||
// this works with only when feegrant enabled. | ||
if feeGranter != nil { | ||
if dfd.feegrantKeeper == nil { | ||
return sdkerrors.ErrInvalidRequest.Wrap("fee grants are not enabled") | ||
} else if !feeGranter.Equals(feePayer) { | ||
err := dfd.feegrantKeeper.UseGrantedFees(ctx, feeGranter, feePayer, fee, sdkTx.GetMsgs()) | ||
if err != nil { | ||
return errorsmod.Wrapf(err, "%s does not allow to pay fees for %s", feeGranter, feePayer) | ||
} | ||
} | ||
|
||
deductFeesFrom = feeGranter | ||
} | ||
|
||
deductFeesFromAcc := dfd.accountKeeper.GetAccount(ctx, deductFeesFrom) | ||
if deductFeesFromAcc == nil { | ||
return sdkerrors.ErrUnknownAddress.Wrapf("fee payer address: %s does not exist", deductFeesFrom) | ||
} | ||
|
||
// deduct the fees | ||
if !fee.IsZero() { | ||
feeBurnPercent, ok := sdk.NewIntFromString(dfd.feeburnKeeper.GetTxFeeBurnPercent(ctx)) | ||
if !ok { | ||
return sdkerrors.ErrInvalidType | ||
} | ||
err := DeductFees(dfd.bankKeeper, ctx, deductFeesFromAcc, fee, feeBurnPercent) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
events := sdk.Events{ | ||
sdk.NewEvent( | ||
sdk.EventTypeTx, | ||
sdk.NewAttribute(sdk.AttributeKeyFee, fee.String()), | ||
sdk.NewAttribute(sdk.AttributeKeyFeePayer, deductFeesFrom.String()), | ||
), | ||
} | ||
ctx.EventManager().EmitEvents(events) | ||
|
||
return nil | ||
} | ||
|
||
// DeductFees deducts fees from the given account. | ||
func DeductFees(bankKeeper BankKeeper, ctx sdk.Context, acc types.AccountI, fees sdk.Coins, bp sdkmath.Int) error { | ||
if !fees.IsValid() { | ||
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "invalid fee amount: %s", fees) | ||
} | ||
|
||
// Calculate burning amounts by given percentage and fee amounts | ||
burningFees := sdk.Coins{} | ||
for _, fee := range fees { | ||
burningAmount := fee.Amount.Mul(bp).Quo(sdk.NewInt(100)) | ||
burningFees = burningFees.Add(sdk.NewCoin(fee.Denom, burningAmount)) | ||
} | ||
|
||
err := bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), types.FeeCollectorName, fees) | ||
if err != nil { | ||
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error()) | ||
} | ||
|
||
err = bankKeeper.BurnCoins(ctx, types.FeeCollectorName, burningFees) | ||
if err != nil { | ||
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error()) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.