Skip to content

Commit

Permalink
changed osmosis swap type URL (#1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs authored Aug 28, 2024
1 parent 930d4eb commit f4cee77
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 1,653 deletions.
2 changes: 1 addition & 1 deletion dockernet/config/ica_host.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"/ibc.applications.transfer.v1.MsgTransfer",
"/cosmwasm.wasm.v1.MsgExecuteContract",
"/cosmwasm.wasm.v1.MsgInstantiateContract",
"/osmosis.gamm.v1beta1.MsgSwapExactAmountIn"
"/osmosis.poolmanager.v1beta1.MsgSwapExactAmountIn"
]
}
}
Expand Down
88 changes: 0 additions & 88 deletions proto/osmosis/gamm/v1beta1/osmosis.proto

This file was deleted.

2 changes: 2 additions & 0 deletions proto/stride/stakeibc/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ message MsgToggleTradeController {
AuthzPermissionChange permission_change = 3;
// Address of trade operator
string address = 4;
// Option to grant/revoke the legacy osmosis swap message
bool legacy = 5;
}
message MsgToggleTradeControllerResponse {}

Expand Down
1 change: 0 additions & 1 deletion scripts/protocgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ cd proto

generate_protos "./stride"
generate_protos "./cosmos"
generate_protos "./osmosis"

cd ..

Expand Down
8 changes: 8 additions & 0 deletions x/stakeibc/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
FlagMaxRedemptionRate = "max-redemption-rate"
FlagCommunityPoolTreasuryAddress = "community-pool-treasury-address"
FlagMaxMessagesPerIcaTx = "max-messages-per-ica-tx"
FlagLegacy = "legacy"
)

var DefaultRelativePacketTimeoutTimestamp = cast.ToUint64((time.Duration(10) * time.Minute).Nanoseconds())
Expand Down Expand Up @@ -798,6 +799,11 @@ Ex:
}
permissionChange := types.AuthzPermissionChange(permissionChangeInt)

legacy, err := cmd.Flags().GetBool(FlagLegacy)
if err != nil {
return err
}

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
Expand All @@ -808,6 +814,7 @@ Ex:
chainId,
permissionChange,
address,
legacy,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand All @@ -817,6 +824,7 @@ Ex:
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().Bool(FlagLegacy, false, "Use legacy osmosis swap message from gamm")

return cmd
}
2 changes: 1 addition & 1 deletion x/stakeibc/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ func (k msgServer) ToggleTradeController(
}

// Build the authz message that grants or revokes trade permissions to the specified address
authzMsg, err := k.BuildTradeAuthzMsg(ctx, tradeRoute, msg.PermissionChange, msg.Address)
authzMsg, err := k.BuildTradeAuthzMsg(ctx, tradeRoute, msg.PermissionChange, msg.Address, msg.Legacy)
if err != nil {
return nil, err
}
Expand Down
15 changes: 12 additions & 3 deletions x/stakeibc/keeper/reward_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import (
"github.com/Stride-Labs/stride/v23/x/stakeibc/types"
)

const (
OsmosisSwapTypeUrl = "/osmosis.poolmanager.v1beta1.MsgSwapExactAmountIn"
LegacyOsmosisSwapTypeUrl = "/osmosis.gamm.v1beta1.MsgSwapExactAmountIn"
)

// JSON Memo for PFM transfers
type PacketForwardMetadata struct {
Forward *ForwardMetadata `json:"forward"`
Expand Down Expand Up @@ -133,12 +138,16 @@ func (k Keeper) BuildTradeAuthzMsg(
tradeRoute types.TradeRoute,
permissionChange types.AuthzPermissionChange,
grantee string,
legacy bool,
) (authzMsg []proto.Message, err error) {
swapMsgTypeUrl := "/" + proto.MessageName(&types.MsgSwapExactAmountIn{})
messageTypeUrl := OsmosisSwapTypeUrl
if legacy {
messageTypeUrl = LegacyOsmosisSwapTypeUrl
}

switch permissionChange {
case types.AuthzPermissionChange_GRANT:
authorization := authz.NewGenericAuthorization(swapMsgTypeUrl)
authorization := authz.NewGenericAuthorization(messageTypeUrl)
expiration := ctx.BlockTime().Add(time.Hour * 24 * 365 * 100) // 100 years

grant, err := authz.NewGrant(ctx.BlockTime(), authorization, &expiration)
Expand All @@ -155,7 +164,7 @@ func (k Keeper) BuildTradeAuthzMsg(
authzMsg = []proto.Message{&authz.MsgRevoke{
Granter: tradeRoute.TradeAccount.Address,
Grantee: grantee,
MsgTypeUrl: swapMsgTypeUrl,
MsgTypeUrl: messageTypeUrl,
}}

default:
Expand Down
78 changes: 48 additions & 30 deletions x/stakeibc/keeper/reward_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,38 +332,56 @@ func (s *KeeperTestSuite) TestBuildTradeAuthzMsg() {
},
}

expectedTypeUrl := "/osmosis.gamm.v1beta1.MsgSwapExactAmountIn"

// Test granting trade permissions
msgs, err := s.App.StakeibcKeeper.BuildTradeAuthzMsg(s.Ctx, tradeRoute, types.AuthzPermissionChange_GRANT, granteeAddress)
s.Require().NoError(err, "no error expected when building grant message")
s.Require().Len(msgs, 1, "there should be one message")
testCases := map[bool]string{
false: "/osmosis.poolmanager.v1beta1.MsgSwapExactAmountIn",
true: "/osmosis.gamm.v1beta1.MsgSwapExactAmountIn",
}

grantMsg, ok := msgs[0].(*authz.MsgGrant)
s.Require().True(ok, "message should be of type grant")
s.Require().Equal(granterAddress, grantMsg.Granter, "granter of grant message")
s.Require().Equal(granteeAddress, grantMsg.Grantee, "grantee of grant message")
for legacy, expectedTypeUrl := range testCases {
// Test granting trade permissions
msgs, err := s.App.StakeibcKeeper.BuildTradeAuthzMsg(
s.Ctx,
tradeRoute,
types.AuthzPermissionChange_GRANT,
granteeAddress,
legacy,
)
s.Require().NoError(err, "no error expected when building grant message")
s.Require().Len(msgs, 1, "there should be one message")

grantMsg, ok := msgs[0].(*authz.MsgGrant)
s.Require().True(ok, "message should be of type grant")
s.Require().Equal(granterAddress, grantMsg.Granter, "granter of grant message")
s.Require().Equal(granteeAddress, grantMsg.Grantee, "grantee of grant message")

authorization, err := grantMsg.Grant.GetAuthorization()
expectedExpiration := s.Ctx.BlockTime().Add(time.Hour * 24 * 365 * 100)
s.Require().NoError(err)
s.Require().Equal(expectedTypeUrl, authorization.MsgTypeURL(), "grant msg type url")
s.Require().Equal(expectedExpiration, *grantMsg.Grant.Expiration, "expiration should be one year from the current block time")

// Test revoking trade permissions
msgs, err = s.App.StakeibcKeeper.BuildTradeAuthzMsg(
s.Ctx,
tradeRoute,
types.AuthzPermissionChange_REVOKE,
granteeAddress,
legacy,
)
s.Require().NoError(err, "no error expected when building revoke message")
s.Require().Len(msgs, 1, "there should be one message")

revokeMsg, ok := msgs[0].(*authz.MsgRevoke)
s.Require().True(ok, "message should be of type revoke")
s.Require().Equal(granterAddress, revokeMsg.Granter, "granter of revoke message")
s.Require().Equal(granteeAddress, revokeMsg.Grantee, "grantee of revoke message")
s.Require().Equal(expectedTypeUrl, revokeMsg.MsgTypeUrl, "revoke msg type url")

// Test invalid permissions
_, err = s.App.StakeibcKeeper.BuildTradeAuthzMsg(s.Ctx, tradeRoute, 100, granteeAddress, legacy)
s.Require().ErrorContains(err, "invalid permission change")
}

authorization, err := grantMsg.Grant.GetAuthorization()
expectedExpiration := s.Ctx.BlockTime().Add(time.Hour * 24 * 365 * 100)
s.Require().NoError(err)
s.Require().Equal(expectedTypeUrl, authorization.MsgTypeURL(), "grant msg type url")
s.Require().Equal(expectedExpiration, *grantMsg.Grant.Expiration, "expiration should be one year from the current block time")

// Test revoking trade permissions
msgs, err = s.App.StakeibcKeeper.BuildTradeAuthzMsg(s.Ctx, tradeRoute, types.AuthzPermissionChange_REVOKE, granteeAddress)
s.Require().NoError(err, "no error expected when building revoke message")
s.Require().Len(msgs, 1, "there should be one message")

revokeMsg, ok := msgs[0].(*authz.MsgRevoke)
s.Require().True(ok, "message should be of type revoke")
s.Require().Equal(granterAddress, revokeMsg.Granter, "granter of revoke message")
s.Require().Equal(granteeAddress, revokeMsg.Grantee, "grantee of revoke message")
s.Require().Equal(expectedTypeUrl, revokeMsg.MsgTypeUrl, "revoke msg type url")

// Test invalid permissions
_, err = s.App.StakeibcKeeper.BuildTradeAuthzMsg(s.Ctx, tradeRoute, 100, granteeAddress)
s.Require().ErrorContains(err, "invalid permission change")
}

// --------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion x/stakeibc/types/message_toggle_trade_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ const TypeMsgToggleTradeController = "toggle_trade_controller"

var _ sdk.Msg = &MsgToggleTradeController{}

func NewMsgToggleTradeController(creator, chainId string, permissionChange AuthzPermissionChange, address string) *MsgToggleTradeController {
func NewMsgToggleTradeController(creator, chainId string, permissionChange AuthzPermissionChange, address string, legacy bool) *MsgToggleTradeController {
return &MsgToggleTradeController{
Creator: creator,
ChainId: chainId,
PermissionChange: permissionChange,
Address: address,
Legacy: legacy,
}
}

Expand Down
Loading

0 comments on commit f4cee77

Please sign in to comment.