Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
likesToEatFish committed Jul 24, 2024
1 parent 09db0b6 commit fd3ca3f
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 1 deletion.
87 changes: 87 additions & 0 deletions x/meshsecurityprovider/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,90 @@ func NewSetConsumerCommissionRateCmd() *cobra.Command {

return cmd
}

// NewDelegateCmd returns a CLI command handler for creating a MsgDelegate transaction.
func NewDelegateCmd() *cobra.Command {
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()

cmd := &cobra.Command{
Use: "delegate [validator-addr] [amount]",
Args: cobra.ExactArgs(2),
Short: "Delegate liquid tokens to a validator",
Long: strings.TrimSpace(
fmt.Sprintf(`Delegate an amount of liquid coins to a validator from your wallet.
Example:
$ %s tx staking delegate %s1l2rsakp388kuv9k8qzq6lrm9taddae7fpx59wm 1000stake --from mykey
`,
version.AppName, bech32PrefixValAddr,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
amount, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}

delAddr := clientCtx.GetFromAddress()
valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}

msg := types.NewMsgDelegate(delAddr, valAddr, amount)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func NewUnbondCmd() *cobra.Command {
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()

cmd := &cobra.Command{
Use: "unbond [validator-addr] [amount]",
Short: "Unbond shares from a validator",
Args: cobra.ExactArgs(2),
Long: strings.TrimSpace(
fmt.Sprintf(`Unbond an amount of bonded shares from a validator.
Example:
$ %s tx staking unbond %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 100stake --from mykey
`,
version.AppName, bech32PrefixValAddr,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
delAddr := clientCtx.GetFromAddress()
valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}

amount, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}

msg := types.NewMsgUndelegate(delAddr, valAddr, amount)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
17 changes: 17 additions & 0 deletions x/meshsecurityprovider/contract/in_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,20 @@ type (
Amount wasmvmtypes.Coin `json:"amount"`
}
)

type (
CustomMsg struct {
VaultCustomMsg *VaultCustomMsg `json:"vault,omitempty"`
}
VaultCustomMsg struct {
Bond *BondMsg `json:"bond,omitempty"`
Unbond *UnbondMsg `json:"unbond,omitempty"`
}
BondMsg struct {
Amount wasmvmtypes.Coin `json:"amount"`
Validator string `json:"validator"`
}
UnbondMsg struct {
Amount wasmvmtypes.Coin `json:"amount"`
}
)
29 changes: 29 additions & 0 deletions x/meshsecurityprovider/keeper/delegate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

// stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -61,3 +63,30 @@ func (k Keeper) RemoteStake(ctx sdk.Context, denomDelegate string, token sdk.Coi
k.SetIntermediary(ctx, inter)
return nil
}

func (k Keeper) Undelegate(ctx sdk.Context, msg types.MsgUndelegate) error {
amount := wasmkeeper.ConvertSdkCoinToWasmCoin(msg.Amount)
unbondMsg := contract.VaultCustomMsg{
Unbond: &contract.UnbondMsg{
Amount: amount,
},
}
err := k.SendCustomUnbond(ctx, unbondMsg)
if err != nil {
return err
}

inter, found := k.GetIntermediary(ctx, msg.Amount.Denom)
if !found {
return fmt.Errorf("can not found Intermediary by denom %s", msg.Amount.Denom)
}
if inter.Token.IsLT(msg.Amount) {
return fmt.Errorf("failed to undelegate; total inter token %s is smaller than %s", inter.Token, msg.Amount)
}

newAmount := inter.Token.Sub(msg.Amount)
inter.Token = &newAmount

k.SetIntermediary(ctx, inter)
return nil
}
38 changes: 38 additions & 0 deletions x/meshsecurityprovider/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,43 @@ func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*typ
}

func (k msgServer) Undelegate(goCtx context.Context, msg *types.MsgUndelegate) (*types.MsgUndelegateResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

err := msg.Amount.Validate()
if err != nil {
return nil, fmt.Errorf("failed to undelegate; Validate fail")
}
denomUndelegate := msg.Amount.Denom

vaultAdress := sdk.AccAddress(k.Keeper.GetParams(ctx).GetVaultContractAddress())
delegatorAddress, err := sdk.AccAddressFromBech32(msg.DelegatorAddress)
if err != nil {
return nil, err
}

depositors, found := k.GetDepositors(ctx, msg.DelegatorAddress)
if !found {
return nil, fmt.Errorf("failed to undelegate; not found Depositors")
}

found, totalDelegate := depositors.Tokens.Find(denomUndelegate)
if !found {
return nil, fmt.Errorf("failed to undelegate; not found token %s in Depositors", denomUndelegate)
}

if totalDelegate.IsLT(msg.Amount) {
return nil, fmt.Errorf("failed to delegate; %s is smaller than %s", totalDelegate, msg.Amount)
}

// call back contract and consumerchain
err = k.Keeper.Undelegate(ctx, *msg)
if err != nil {
return nil, err
}

if err := k.bank.SendCoins(ctx, vaultAdress, delegatorAddress, sdk.NewCoins([]sdk.Coin{msg.Amount}...)); err != nil {
return nil, err
}

return &types.MsgUndelegateResponse{}, nil
}
16 changes: 16 additions & 0 deletions x/meshsecurityprovider/keeper/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,19 @@ func (k Keeper) doSudoCall(ctx sdk.Context, contractAddr sdk.AccAddress, msg con
_, err = k.wasm.Sudo(ctx, contractAddr, bz)
return err
}

func (k Keeper) SendCustomUnbond(ctx sdk.Context, v contract.VaultCustomMsg) error {
msg := contract.CustomMsg{
VaultCustomMsg: &v,
}
return k.doExcCall(ctx, k.GetParams(ctx).GetVaultContractAddress(), msg)
}

func (k Keeper) doExcCall(ctx sdk.Context, contractAddr sdk.AccAddress, msg contract.CustomMsg) error {
bz, err := json.Marshal(msg)
if err != nil {
return errorsmod.Wrap(err, "marshal sudo msg")
}
_, err = k.wasm.Sudo(ctx, contractAddr, bz)
return err
}
3 changes: 2 additions & 1 deletion x/meshsecurityprovider/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import sdk "github.com/cosmos/cosmos-sdk/types"
// DefaultParams returns default mesh-security parameters
func DefaultParams(denom string) Params {
return Params{ // todo: revisit and set proper defaults
TimeoutPeriod: 60,
TimeoutPeriod: 60,
VaultContractAddress: "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr",
}
}

Expand Down
16 changes: 16 additions & 0 deletions x/meshsecurityprovider/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

func NewMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Coin) *MsgDelegate {
return &MsgDelegate{
DelegatorAddress: delAddr.String(),
ValidatorAddress: valAddr.String(),
Amount: amount,
}
}

// GetSignBytes implements the LegacyMsg interface.
func (msg MsgDelegate) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
Expand All @@ -34,6 +42,14 @@ func (msg MsgDelegate) ValidateBasic() error {
return nil
}

func NewMsgUndelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Coin) *MsgUndelegate {
return &MsgUndelegate{
DelegatorAddress: delAddr.String(),
ValidatorAddress: valAddr.String(),
Amount: amount,
}
}

// GetSignBytes implements the LegacyMsg interface.
func (msg MsgUndelegate) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
Expand Down

0 comments on commit fd3ca3f

Please sign in to comment.