Skip to content

Commit

Permalink
Add min swap amount to each host zone config for improve cross chain …
Browse files Browse the repository at this point in the history
…swap call (#171)

* Add min swap amount to each host zone config for improve cross chain swap call

* fix min_swap_amount value in proposal.json

* fix min swap amount type

* add unit tests for min swap amount

* linting

---------

Co-authored-by: Tuan Tran <[email protected]>
  • Loading branch information
tnv1 and tuantran1702 authored Mar 27, 2024
1 parent 0db361e commit afe5e17
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 42 deletions.
2 changes: 2 additions & 0 deletions proto/feeabstraction/feeabs/v1beta1/proposal.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ message HostChainFeeAbsConfig {
uint64 pool_id = 3;
// Host chain fee abstraction connection status
HostChainFeeAbsStatus status = 4;
// A minimum amount threshold allow cross-chain swap call
uint64 min_swap_amount = 5;
}

message AddHostZoneProposal {
Expand Down
9 changes: 5 additions & 4 deletions tests/interchaintest/feeabs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ const (

type HostChainFeeAbsConfig struct {
// ibc token is allowed to be used as fee token
IbcDenom string `protobuf:"bytes,1,opt,name=ibc_denom,json=ibcDenom,proto3" json:"ibc_denom,omitempty" yaml:"allowed_token"`
IbcDenom string `json:"ibc_denom,omitempty"`
// token_in in cross_chain swap contract.
OsmosisPoolTokenDenomIn string `protobuf:"bytes,2,opt,name=osmosis_pool_token_denom_in,json=osmosisPoolTokenDenomIn,proto3" json:"osmosis_pool_token_denom_in,omitempty"`
OsmosisPoolTokenDenomIn string `json:"osmosis_pool_token_denom_in,omitempty"`
// pool id
PoolId string `protobuf:"varint,3,opt,name=pool_id,json=poolId,proto3" json:"pool_id,omitempty"`
PoolId string `json:"pool_id,omitempty"`
// Host chain fee abstraction connection status
Status string `protobuf:"varint,4,opt,name=status,proto3,enum=feeabstraction.feeabs.v1beta1.HostChainFeeAbsStatus" json:"status,omitempty"`
Status string `json:"status,omitempty"`
MinSwapAmount string `json:"min_swap_amount,omitempty"`
}
2 changes: 2 additions & 0 deletions tests/interchaintest/host_zone_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestHostZoneProposal(t *testing.T) {
OsmosisPoolTokenDenomIn: "ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9",
PoolId: "1",
Status: feeabsCli.HostChainFeeAbsStatus_UPDATED,
MinSwapAmount: "0",
}})

// Start testing for set host zone proposal
Expand All @@ -64,6 +65,7 @@ func TestHostZoneProposal(t *testing.T) {
OsmosisPoolTokenDenomIn: "ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9",
PoolId: "1",
Status: feeabsCli.HostChainFeeAbsStatus_FROZEN,
MinSwapAmount: "10",
}})

// Start testing for delete host zone proposal
Expand Down
3 changes: 2 additions & 1 deletion tests/interchaintest/proposal/add_host_zone.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"ibc_denom": "ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9",
"osmosis_pool_token_denom_in": "ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9",
"pool_id": "1",
"status": 0
"status": 0,
"min_swap_amount": "0"
},
"deposit": "100000000stake"
}
3 changes: 2 additions & 1 deletion tests/interchaintest/proposal/set_host_zone.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"ibc_denom": "ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9",
"osmosis_pool_token_denom_in": "ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9",
"pool_id": "1",
"status": 2
"status": 2,
"min_swap_amount": "10"
},
"deposit": "100000000stake"
}
9 changes: 7 additions & 2 deletions x/feeabs/keeper/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ func (k Keeper) transferOsmosisCrosschainSwap(ctx sdk.Context, hostChainConfig t
nativeDenomIBCedInOsmosis := params.NativeIbcedInOsmosis
chainName := params.ChainName

if !token.Amount.IsPositive() {
return nil
if !token.Amount.IsPositive() || token.Amount.LT(sdk.NewIntFromUint64(hostChainConfig.MinSwapAmount)) {
return fmt.Errorf("invalid amount to transfer, expect minimum %v, got %v", hostChainConfig.MinSwapAmount, token.Amount)
}

memo, err := types.BuildCrossChainSwapMemo(nativeDenomIBCedInOsmosis, params.OsmosisCrosschainSwapAddress, moduleAccountAddress.String(), chainName)
Expand Down Expand Up @@ -389,3 +389,8 @@ func (k Keeper) IbcQueryHostZoneFilter(ctx sdk.Context, hostZoneConfig types.Hos

return false
}

// for testing
func (k Keeper) TransferOsmosisCrosschainSwap(ctx sdk.Context, hostChainConfig types.HostChainFeeAbsConfig) error {
return k.transferOsmosisCrosschainSwap(ctx, hostChainConfig)
}
65 changes: 65 additions & 0 deletions x/feeabs/keeper/ibc_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package keeper_test

import (
"fmt"
"testing"

channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/stretchr/testify/require"

sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"

abci "github.com/cometbft/cometbft/abci/types"

"github.com/osmosis-labs/fee-abstraction/v7/x/feeabs/types"
Expand Down Expand Up @@ -215,3 +221,62 @@ func (s *KeeperTestSuite) generateQueryRequest() []abci.RequestQuery {
Data: icqReqDataBz,
}}
}

// test unsuccessful osmosisCrooschainSwap due to below swap threshold
func (s *KeeperTestSuite) TestTransferOsmosisCrosschainSwap() {
tests := []struct {
name string
AmountToTransfer uint64
hostConfig types.HostChainFeeAbsConfig
wantErr bool
}{
{
"swap zero token",
0,
types.HostChainFeeAbsConfig{
IbcDenom: IBCDenom,
OsmosisPoolTokenDenomIn: OsmosisIBCDenom,
Status: types.HostChainFeeAbsStatus_UPDATED,
PoolId: 1,
MinSwapAmount: 1000,
},
true,
},
{
"swap amount below min",
500,
types.HostChainFeeAbsConfig{
IbcDenom: IBCDenom,
OsmosisPoolTokenDenomIn: OsmosisIBCDenom,
Status: types.HostChainFeeAbsStatus_UPDATED,
PoolId: 1,
MinSwapAmount: 1000,
},
true,
},
}

for _, tc := range tests {
tc := tc
s.T().Run(tc.name, func(t *testing.T) {
s.FundFeeAbsModuleAccount(s.ctx, tc.AmountToTransfer)
err := s.feeAbsKeeper.TransferOsmosisCrosschainSwap(s.ctx, tc.hostConfig)
if tc.wantErr {
require.Error(t, err)
require.Contains(t, err.Error(), fmt.Sprintf("invalid amount to transfer, expect minimum %v, got %v", tc.hostConfig.MinSwapAmount, tc.AmountToTransfer))

} else {
require.NoError(t, err)
}
})
}
}

// helper function to fund fee abs module account for testing
func (s *KeeperTestSuite) FundFeeAbsModuleAccount(ctx sdk.Context, amount uint64) {
s.T().Helper()
err := s.feeAbsApp.BankKeeper.MintCoins(ctx, minttypes.ModuleName, sdk.NewCoins(sdk.NewCoin(IBCDenom, sdk.NewIntFromUint64(amount))))
require.NoError(s.T(), err)
err = s.feeAbsApp.BankKeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, types.ModuleName, sdk.NewCoins(sdk.NewCoin(IBCDenom, sdk.NewIntFromUint64(amount))))
require.NoError(s.T(), err)
}
106 changes: 72 additions & 34 deletions x/feeabs/types/proposal.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit afe5e17

Please sign in to comment.