Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TFM keeper test #209

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions x/transfermiddleware/keeper/abci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package keeper_test

import (
"github.com/notional-labs/centauri/v4/x/transfermiddleware/types"
)

func (suite *TransferMiddlewareKeeperTestSuite) TestBeginBlocker() {
suite.SetupTest()

infos := [5]types.ParachainIBCTokenInfo{
{
IbcDenom: "ibc-test-1",
ChannelId: "channel-1",
NativeDenom: "native-1",
AssetId: "2",
},
{
IbcDenom: "ibc-test-2",
ChannelId: "channel-1",
NativeDenom: "native-2",
AssetId: "3",
},
{
IbcDenom: "ibc-test-3",
ChannelId: "channel-1",
NativeDenom: "native-3",
AssetId: "4",
},
{
IbcDenom: "ibc-test-4",
ChannelId: "channel-2",
NativeDenom: "native-4",
AssetId: "5",
},
{
IbcDenom: "ibc-test-5",
ChannelId: "channel-2",
NativeDenom: "native-5",
AssetId: "6",
},
}

count := 0
for _, info := range infos {
suite.app.TransferMiddlewareKeeper.AddParachainIBCInfo(
suite.ctx,
info.IbcDenom,
info.ChannelId,
info.NativeDenom,
info.AssetId,
)
}

suite.app.TransferMiddlewareKeeper.IterateRemoveListInfo(suite.ctx, func(_ types.RemoveParachainIBCTokenInfo) (stop bool) {
count++
return false
})

suite.Require().Equal(count, 0)

for _, info := range infos {
suite.app.TransferMiddlewareKeeper.AddParachainIBCInfoToRemoveList(
suite.ctx,
info.NativeDenom,
)
}

suite.app.TransferMiddlewareKeeper.IterateRemoveListInfo(suite.ctx, func(_ types.RemoveParachainIBCTokenInfo) (stop bool) {
count++
return false
})

suite.Require().Equal(count, 5)

suite.app.TransferMiddlewareKeeper.BeginBlocker(suite.ctx)

countRemove := 0
suite.app.TransferMiddlewareKeeper.IterateRemoveListInfo(suite.ctx, func(removeList types.RemoveParachainIBCTokenInfo) (stop bool) {
if suite.ctx.BlockTime().After(removeList.RemoveTime) {
countRemove++
count--
}
return false
})

suite.Require().Equal(countRemove+count, 5)
}
101 changes: 51 additions & 50 deletions x/transfermiddleware/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package keeper_test

import (
"testing"

helpers "github.com/notional-labs/centauri/v4/app/helpers"
"github.com/notional-labs/centauri/v4/x/transfermiddleware/types"
"github.com/stretchr/testify/require"
)

func TestTFMInitGenesis(t *testing.T) {
app := helpers.SetupCentauriAppWithValSet(t)
ctx := helpers.NewContextForApp(*app)

func (suite *TransferMiddlewareKeeperTestSuite) TestTFMInitGenesis() {
tokenInfos := make([]types.ParachainIBCTokenInfo, 1)
tokenInfos[0] = types.ParachainIBCTokenInfo{
IbcDenom: "ibc-test",
Expand All @@ -20,62 +13,70 @@ func TestTFMInitGenesis(t *testing.T) {
AssetId: "1",
}

app.TransferMiddlewareKeeper.InitGenesis(ctx, types.GenesisState{
suite.app.TransferMiddlewareKeeper.InitGenesis(suite.ctx, types.GenesisState{
TokenInfos: tokenInfos,
})

info := app.TransferMiddlewareKeeper.GetParachainIBCTokenInfoByNativeDenom(ctx, "pica")
require.Equal(t, info, app.TransferMiddlewareKeeper.GetParachainIBCTokenInfoByNativeDenom(ctx, "pica"))
require.Equal(t, "1", info.AssetId)
require.Equal(t, "pica", info.NativeDenom)
require.Equal(t, "ibc-test", info.IbcDenom)
require.Equal(t, "channel-0", info.ChannelId)
info := suite.app.TransferMiddlewareKeeper.GetParachainIBCTokenInfoByNativeDenom(suite.ctx, "pica")
suite.Require().Equal(info, suite.app.TransferMiddlewareKeeper.GetParachainIBCTokenInfoByNativeDenom(suite.ctx, "pica"))
suite.Require().Equal("1", info.AssetId)
suite.Require().Equal("pica", info.NativeDenom)
suite.Require().Equal("ibc-test", info.IbcDenom)
suite.Require().Equal("channel-0", info.ChannelId)
}

func TestTFMExportGenesis(t *testing.T) {
app := helpers.SetupCentauriAppWithValSet(t)
ctx := helpers.NewContextForApp(*app)

err := app.TransferMiddlewareKeeper.AddParachainIBCInfo(ctx, "ibc-test", "channel-0", "pica", "1")
require.NoError(t, err)
err = app.TransferMiddlewareKeeper.AddParachainIBCInfo(ctx, "ibc-test2", "channel-1", "poke", "2")
require.NoError(t, err)
genesis := app.TransferMiddlewareKeeper.ExportGenesis(ctx)

require.Equal(t, "1", genesis.TokenInfos[0].AssetId)
require.Equal(t, "pica", genesis.TokenInfos[0].NativeDenom)
require.Equal(t, "channel-0", genesis.TokenInfos[0].ChannelId)
require.Equal(t, "ibc-test", genesis.TokenInfos[0].IbcDenom)

require.Equal(t, "2", genesis.TokenInfos[1].AssetId)
require.Equal(t, "poke", genesis.TokenInfos[1].NativeDenom)
require.Equal(t, "channel-1", genesis.TokenInfos[1].ChannelId)
require.Equal(t, "ibc-test2", genesis.TokenInfos[1].IbcDenom)
func (suite *TransferMiddlewareKeeperTestSuite) TestTFMExportGenesis() {
suite.SetupTest()

err := suite.app.TransferMiddlewareKeeper.AddParachainIBCInfo(suite.ctx, "ibc-test2", "channel-0", "paca", "2")
suite.Require().NoError(err)
err = suite.app.TransferMiddlewareKeeper.AddParachainIBCInfo(suite.ctx, "ibc-test3", "channel-1", "poke", "3")
suite.Require().NoError(err)
genesis := suite.app.TransferMiddlewareKeeper.ExportGenesis(suite.ctx)

suite.Require().Equal("1", genesis.TokenInfos[0].AssetId)
suite.Require().Equal("pica", genesis.TokenInfos[0].NativeDenom)
suite.Require().Equal("channel-0", genesis.TokenInfos[0].ChannelId)
suite.Require().Equal("ibc-test", genesis.TokenInfos[0].IbcDenom)

suite.Require().Equal("2", genesis.TokenInfos[1].AssetId)
suite.Require().Equal("paca", genesis.TokenInfos[1].NativeDenom)
suite.Require().Equal("channel-0", genesis.TokenInfos[1].ChannelId)
suite.Require().Equal("ibc-test2", genesis.TokenInfos[1].IbcDenom)

suite.Require().Equal("3", genesis.TokenInfos[2].AssetId)
suite.Require().Equal("poke", genesis.TokenInfos[2].NativeDenom)
suite.Require().Equal("channel-1", genesis.TokenInfos[2].ChannelId)
suite.Require().Equal("ibc-test3", genesis.TokenInfos[2].IbcDenom)
}

func TestIterateParaTokenInfos(t *testing.T) {
app := helpers.SetupCentauriAppWithValSet(t)
ctx := helpers.NewContextForApp(*app)
func (suite *TransferMiddlewareKeeperTestSuite) TestIterateParaTokenInfos() {
suite.SetupTest()

err := app.TransferMiddlewareKeeper.AddParachainIBCInfo(ctx, "ibc-test", "channel-0", "pica", "1")
require.NoError(t, err)
err = app.TransferMiddlewareKeeper.AddParachainIBCInfo(ctx, "ibc-test2", "channel-1", "poke", "2")
require.NoError(t, err)
err := suite.app.TransferMiddlewareKeeper.AddParachainIBCInfo(suite.ctx, "ibc-test2", "channel-0", "paca", "2")
suite.Require().NoError(err)
err = suite.app.TransferMiddlewareKeeper.AddParachainIBCInfo(suite.ctx, "ibc-test3", "channel-1", "poke", "3")
suite.Require().NoError(err)

infos := []types.ParachainIBCTokenInfo{}

app.TransferMiddlewareKeeper.IterateParaTokenInfos(ctx, func(index int64, info types.ParachainIBCTokenInfo) (stop bool) {
suite.app.TransferMiddlewareKeeper.IterateParaTokenInfos(suite.ctx, func(index int64, info types.ParachainIBCTokenInfo) (stop bool) {
infos = append(infos, info)
return false
})

require.Equal(t, "1", infos[0].AssetId)
require.Equal(t, "pica", infos[0].NativeDenom)
require.Equal(t, "channel-0", infos[0].ChannelId)
require.Equal(t, "ibc-test", infos[0].IbcDenom)
suite.Require().Equal("1", infos[0].AssetId)
suite.Require().Equal("pica", infos[0].NativeDenom)
suite.Require().Equal("channel-0", infos[0].ChannelId)
suite.Require().Equal("ibc-test", infos[0].IbcDenom)

suite.Require().Equal("2", infos[1].AssetId)
suite.Require().Equal("paca", infos[1].NativeDenom)
suite.Require().Equal("channel-0", infos[1].ChannelId)
suite.Require().Equal("ibc-test2", infos[1].IbcDenom)

require.Equal(t, "2", infos[1].AssetId)
require.Equal(t, "poke", infos[1].NativeDenom)
require.Equal(t, "channel-1", infos[1].ChannelId)
require.Equal(t, "ibc-test2", infos[1].IbcDenom)
suite.Require().Equal("3", infos[2].AssetId)
suite.Require().Equal("poke", infos[2].NativeDenom)
suite.Require().Equal("channel-1", infos[2].ChannelId)
suite.Require().Equal("ibc-test3", infos[2].IbcDenom)
}
47 changes: 47 additions & 0 deletions x/transfermiddleware/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package keeper_test

import (
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
"github.com/notional-labs/centauri/v4/x/transfermiddleware/types"
)

func (suite *TransferMiddlewareKeeperTestSuite) TestParaTokenInfo() {
suite.SetupTest()
tokenInfos := make([]types.ParachainIBCTokenInfo, 1)
tokenInfos[0] = types.ParachainIBCTokenInfo{
IbcDenom: "ibc-test",
ChannelId: "channel-0",
NativeDenom: "pica",
AssetId: "1",
}
suite.app.TransferMiddlewareKeeper.InitGenesis(suite.ctx, types.GenesisState{
TokenInfos: tokenInfos,
})

info, err := suite.app.TransferMiddlewareKeeper.ParaTokenInfo(suite.ctx, &types.QueryParaTokenInfoRequest{NativeDenom: "pica"})

suite.Require().NoError(err)
suite.Require().Equal("1", info.AssetId)
suite.Require().Equal("pica", info.NativeDenom)
suite.Require().Equal("ibc-test", info.IbcDenom)
suite.Require().Equal("channel-0", info.ChannelId)
}

func (suite *TransferMiddlewareKeeperTestSuite) TestEscrowAddress() {
suite.SetupTest()

tokenInfos := make([]types.ParachainIBCTokenInfo, 1)
tokenInfos[0] = types.ParachainIBCTokenInfo{
IbcDenom: "ibc-test",
ChannelId: "channel-0",
NativeDenom: "pica",
AssetId: "1",
}
suite.app.TransferMiddlewareKeeper.InitGenesis(suite.ctx, types.GenesisState{
TokenInfos: tokenInfos,
})

escrowResponse, err := suite.app.TransferMiddlewareKeeper.EscrowAddress(suite.ctx, &types.QueryEscrowAddressRequest{ChannelId: "channel-0"})
suite.Require().NoError(err)
suite.Require().Equal(escrowResponse.EscrowAddress, transfertypes.GetEscrowAddress(transfertypes.PortID, "channel-0").String())
}
Loading
Loading