Skip to content

Commit

Permalink
add support 2 denoms (#206)
Browse files Browse the repository at this point in the history
* add support for 2 denom(1 native, 1 ibc)

* add TODO

* add test two ibc fee denoms
  • Loading branch information
tuantran1702 authored Jun 9, 2024
1 parent e06bbc3 commit ea9badf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 40 deletions.
67 changes: 47 additions & 20 deletions x/feeabs/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,53 @@ func TestMempoolDecorator(t *testing.T) {
false,
nil,
},
// TODO: Add support for multiple denom fees(--fees 50ibc,50native)
// {
// "half native fee, half ibc fee, should pass",
// sdk.NewCoins(sdk.NewInt64Coin("native", 500*int64(gasLimit)), sdk.NewInt64Coin("ibcfee", 500*int64(gasLimit))),
// sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...),
// func(suite *AnteTestSuite) {
// err := suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, types.HostChainFeeAbsConfig{
// IbcDenom: "ibcfee",
// OsmosisPoolTokenDenomIn: "osmosis",
// PoolId: 1,
// Status: types.HostChainFeeAbsStatus_UPDATED,
// MinSwapAmount: 0,
// })
// require.NoError(t, err)
// suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", math.LegacyNewDec(1))
// suite.stakingKeeper.EXPECT().BondDenom(gomock.Any()).Return("native").MinTimes(1)
// },
// false,
// nil,
// },
{
"half native fee, half ibc fee, should pass",
sdk.NewCoins(sdk.NewInt64Coin("native", 500*int64(gasLimit)), sdk.NewInt64Coin("ibcfee", 500*int64(gasLimit))),
sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...),
func(suite *AnteTestSuite) {
err := suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, types.HostChainFeeAbsConfig{
IbcDenom: "ibcfee",
OsmosisPoolTokenDenomIn: "osmosis",
PoolId: 1,
Status: types.HostChainFeeAbsStatus_UPDATED,
// MinSwapAmount: 0,
})
require.NoError(t, err)
suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", math.LegacyNewDec(1))
suite.stakingKeeper.EXPECT().BondDenom(gomock.Any()).Return("native", nil).MinTimes(1)
},
false,
nil,
},
{
"3/4 ibc fee, 1/4 another ibc fee, should pass",
sdk.NewCoins(sdk.NewInt64Coin("ibcfee", 750*int64(gasLimit)), sdk.NewInt64Coin("ibcfee2", 250*int64(gasLimit))),
sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...),
func(suite *AnteTestSuite) {
err := suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, types.HostChainFeeAbsConfig{
IbcDenom: "ibcfee",
OsmosisPoolTokenDenomIn: "osmosis",
PoolId: 1,
Status: types.HostChainFeeAbsStatus_UPDATED,
// MinSwapAmount: 0,
})
require.NoError(t, err)
err = suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, types.HostChainFeeAbsConfig{
IbcDenom: "ibcfee2",
OsmosisPoolTokenDenomIn: "osmosis",
PoolId: 2,
Status: types.HostChainFeeAbsStatus_UPDATED,
// MinSwapAmount: 0,
})
require.NoError(t, err)
suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", math.LegacyNewDec(1))
suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee2", math.LegacyNewDec(1))
suite.stakingKeeper.EXPECT().BondDenom(gomock.Any()).Return("native", nil).MinTimes(1)
},
false,
nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
44 changes: 24 additions & 20 deletions x/feeabs/ante/decorate.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,28 +271,32 @@ func (famfd FeeAbstrationMempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk
// Check if feeDenom is defined in feeabs
// If so, replace the amount of feeDenom in feeCoins with the
// corresponding amount of native denom that allow to pay fee
// TODO: Support more fee token in feeRequired for fee-abstraction
if feeCoinsNonZeroDenom.Len() == 1 {
feeDenom := feeCoinsNonZeroDenom.GetDenomByIndex(0)
hostChainConfig, found := famfd.feeabsKeeper.GetHostZoneConfig(ctx, feeDenom)
if found {
if hostChainConfig.Status == feeabstypes.HostChainFeeAbsStatus_FROZEN {
return ctx, sdkerrors.Wrapf(feeabstypes.ErrHostZoneFrozen, "cannot deduct fee as host zone is frozen")
// Support more fee tokens in feeRequired for fee-abstraction
if feeCoinsNonZeroDenom.Len() > 0 {
nativeCoinsFees := sdk.NewCoins()
for _, feeCoin := range feeCoinsNonZeroDenom {
feeDenom := feeCoin.Denom
hostChainConfig, found := famfd.feeabsKeeper.GetHostZoneConfig(ctx, feeDenom)
if found {
if hostChainConfig.Status == feeabstypes.HostChainFeeAbsStatus_FROZEN {
return ctx, sdkerrors.Wrapf(feeabstypes.ErrHostZoneFrozen, "cannot deduct fee as host zone is frozen")
}

// if hostChainConfig.Status == feeabstypes.HostChainFeeAbsStatus_OUTDATED {
// return ctx, sdkerrors.Wrapf(feeabstypes.ErrHostZoneOutdated, "cannot deduct fee as host zone is outdated")
// }

nativeCoinFee, err := famfd.feeabsKeeper.CalculateNativeFromIBCCoins(ctx, sdk.NewCoins(feeCoin), hostChainConfig)
if err != nil {
return ctx, sdkerrors.Wrapf(errorstypes.ErrInsufficientFee, "unable to calculate native fee from ibc fee: %s", err)
}
nativeCoinsFees = nativeCoinsFees.Add(nativeCoinFee...)
} else {
nativeCoinsFees = nativeCoinsFees.Add(feeCoin)
}

// if hostChainConfig.Status == feeabstypes.HostChainFeeAbsStatus_OUTDATED {
// return ctx, sdkerrors.Wrapf(feeabstypes.ErrHostZoneOutdated, "cannot deduct fee as host zone is outdated")
// }

nativeCoinsFees, err := famfd.feeabsKeeper.CalculateNativeFromIBCCoins(ctx, feeCoinsNonZeroDenom, hostChainConfig)
if err != nil {
return ctx, sdkerrors.Wrapf(errorstypes.ErrInsufficientFee, "unable to calculate native fees from ibc fees: %s", err)
}
fmt.Println("nativeCoinsFees", nativeCoinsFees)
feeCoinsNonZeroDenom = nativeCoinsFees
}
} else if feeCoinsNonZeroDenom.Len() > 1 {
return ctx, sdkerrors.Wrapf(errorstypes.ErrNotSupported, "should have only one fee denom in feeCoinsNonZeroDenom, got %d", feeCoinsNonZeroDenom.Len())
fmt.Println("nativeCoinsFees", nativeCoinsFees)
feeCoinsNonZeroDenom = nativeCoinsFees
}

// After replace the feeCoinsNonZeroDenom, feeCoinsNonZeroDenom must be in denom subset of nonZeroCoinFeesReq
Expand Down

0 comments on commit ea9badf

Please sign in to comment.