-
Notifications
You must be signed in to change notification settings - Fork 48
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
1 parent
99156fb
commit 3fc211d
Showing
7 changed files
with
204 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
|
||
"github.com/sideprotocol/side/x/yield/types" | ||
) | ||
|
||
// SetHostChain set a specific hostChain in the store | ||
func (k Keeper) SetHostChain(ctx sdk.Context, hostChain types.HostChain) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.HostChainKey)) | ||
b := k.cdc.MustMarshal(&hostChain) | ||
store.Set([]byte(hostChain.ChainId), b) | ||
} | ||
|
||
// GetHostChain returns a hostChain from its id | ||
func (k Keeper) GetHostChain(ctx sdk.Context, chainID string) (val types.HostChain, found bool) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.HostChainKey)) | ||
b := store.Get([]byte(chainID)) | ||
if b == nil { | ||
return val, false | ||
} | ||
k.cdc.MustUnmarshal(b, &val) | ||
return val, true | ||
} | ||
|
||
// GetHostChainFromHostDenom returns a HostChain from a HostDenom | ||
func (k Keeper) GetHostChainFromHostDenom(ctx sdk.Context, denom string) (*types.HostChain, error) { | ||
var matchChain types.HostChain | ||
k.IterateHostChains(ctx, func(ctx sdk.Context, index int64, chainInfo types.HostChain) error { | ||
if chainInfo.HostDenom == denom { | ||
matchChain = chainInfo | ||
return nil | ||
} | ||
return nil | ||
}) | ||
if matchChain.ChainId != "" { | ||
return &matchChain, nil | ||
} | ||
return nil, errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "No HostChain for %s found", denom) | ||
} | ||
|
||
// GetHostChainFromTransferChannelID returns a HostChain from a transfer channel ID | ||
func (k Keeper) GetHostChainFromTransferChannelID(ctx sdk.Context, channelID string) (hostChain types.HostChain, found bool) { | ||
for _, hostChain := range k.GetAllHostChain(ctx) { | ||
if hostChain.TransferChannelId == channelID { | ||
return hostChain, true | ||
} | ||
} | ||
return types.HostChain{}, false | ||
} | ||
|
||
// RemoveHostChain removes a hostChain from the store | ||
func (k Keeper) RemoveHostChain(ctx sdk.Context, chainID string) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.HostChainKey)) | ||
store.Delete([]byte(chainID)) | ||
} | ||
|
||
// GetAllHostChain returns all hostChain | ||
func (k Keeper) GetAllHostChain(ctx sdk.Context) (list []types.HostChain) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.HostChainKey)) | ||
iterator := sdk.KVStorePrefixIterator(store, []byte{}) | ||
|
||
defer iterator.Close() | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
var val types.HostChain | ||
k.cdc.MustUnmarshal(iterator.Value(), &val) | ||
list = append(list, val) | ||
} | ||
|
||
return | ||
} | ||
|
||
// IterateHostChains iterates chains | ||
func (k Keeper) IterateHostChains(ctx sdk.Context, fn func(ctx sdk.Context, index int64, zoneInfo types.HostChain) error) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.HostChainKey)) | ||
|
||
iterator := sdk.KVStorePrefixIterator(store, nil) | ||
defer iterator.Close() | ||
|
||
i := int64(0) | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
k.Logger(ctx).Debug(fmt.Sprintf("Iterating hostChain %d", i)) | ||
zone := types.HostChain{} | ||
k.cdc.MustUnmarshal(iterator.Value(), &zone) | ||
|
||
error := fn(ctx, i, zone) | ||
|
||
if error != nil { | ||
break | ||
} | ||
i++ | ||
} | ||
} |
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,86 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
keepertest "github.com/sideprotocol/side/testutil/keeper" | ||
"github.com/sideprotocol/side/testutil/nullify" | ||
"github.com/sideprotocol/side/x/yield/keeper" | ||
"github.com/sideprotocol/side/x/yield/types" | ||
) | ||
|
||
func createNHostChain(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.HostChain { | ||
items := make([]types.HostChain, n) | ||
for i := range items { | ||
items[i].ChainId = strconv.Itoa(i) | ||
keeper.SetHostChain(ctx, items[i]) | ||
} | ||
return items | ||
} | ||
|
||
func TestHostChainGet(t *testing.T) { | ||
keeper, ctx := keepertest.YieldKeeper(t) | ||
items := createNHostChain(keeper, ctx, 10) | ||
for _, item := range items { | ||
got, found := keeper.GetHostChain(ctx, item.ChainId) | ||
require.True(t, found) | ||
require.Equal(t, | ||
nullify.Fill(&item), | ||
nullify.Fill(&got), | ||
) | ||
} | ||
} | ||
|
||
func TestHostChainRemove(t *testing.T) { | ||
keeper, ctx := keepertest.YieldKeeper(t) | ||
items := createNHostChain(keeper, ctx, 10) | ||
for _, item := range items { | ||
keeper.RemoveHostChain(ctx, item.ChainId) | ||
_, found := keeper.GetHostChain(ctx, item.ChainId) | ||
require.False(t, found) | ||
} | ||
} | ||
|
||
func TestHostChainGetAll(t *testing.T) { | ||
keeper, ctx := keepertest.YieldKeeper(t) | ||
items := createNHostChain(keeper, ctx, 10) | ||
require.ElementsMatch(t, | ||
nullify.Fill(items), | ||
nullify.Fill(keeper.GetAllHostChain(ctx)), | ||
) | ||
} | ||
|
||
// func (s *KeeperTestSuite) TestGetHostChainFromTransferChannelID() { | ||
// // Store 5 host chains | ||
// expectedHostChains := map[string]types.HostChain{} | ||
// for i := 0; i < 5; i++ { | ||
// chainId := fmt.Sprintf("chain-%d", i) | ||
// channelId := fmt.Sprintf("channel-%d", i) | ||
|
||
// HostChain := types.HostChain{ | ||
// ChainId: chainId, | ||
// TransferChannelId: channelId, | ||
// } | ||
// s.App.StakeibcKeeper.SetHostChain(s.Ctx, HostChain) | ||
// expectedHostChains[channelId] = HostChain | ||
// } | ||
|
||
// // Look up each host chain by the channel ID | ||
// for i := 0; i < 5; i++ { | ||
// channelId := fmt.Sprintf("channel-%d", i) | ||
|
||
// expectedHostChain := expectedHostChains[channelId] | ||
// actualHostChain, found := s.App.StakeibcKeeper.GetHostChainFromTransferChannelID(s.Ctx, channelId) | ||
|
||
// s.Require().True(found, "found host chain %d", i) | ||
// s.Require().Equal(expectedHostChain.ChainId, actualHostChain.ChainId, "host chain %d chain-id", i) | ||
// } | ||
|
||
// // Lookup a non-existent host chain - should not be found | ||
// _, found := s.App.StakeibcKeeper.GetHostChainFromTransferChannelID(s.Ctx, "fake_channel") | ||
// s.Require().False(found, "fake channel should not be found") | ||
// } |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package types | ||
|
||
func FormatICAAccountOwner(chainId string, accountType string) (result string) { | ||
return chainId + "." + accountType | ||
func FormatICAAccountOwner(chainID string, accountType string) (result string) { | ||
return chainID + "." + accountType | ||
} |
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