Skip to content

Commit

Permalink
wip: add cdp signers
Browse files Browse the repository at this point in the history
  • Loading branch information
boodyvo committed Nov 5, 2024
1 parent 78d7ba7 commit fedeac5
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 10 deletions.
9 changes: 9 additions & 0 deletions app/params/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package params

import (
"cosmossdk.io/x/tx/signing"
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/address"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/cosmos/gogoproto/proto"
enccodec "github.com/evmos/ethermint/encoding/codec"
evmtypes "github.com/evmos/ethermint/x/evm/types"
cdptypes "github.com/kava-labs/kava/x/cdp/types"
evmutiltypes "github.com/kava-labs/kava/x/evmutil/types"
"google.golang.org/protobuf/reflect/protoreflect"
)
Expand Down Expand Up @@ -41,8 +43,15 @@ func MakeEncodingConfig() EncodingConfig {
evmutiltypes.MsgConvertERC20ToCoinGetSigners.MsgType: evmutiltypes.MsgConvertERC20ToCoinGetSigners.Fn,
evmutiltypes.MsgConvertCosmosCoinToERC20GetSigners.MsgType: evmutiltypes.MsgConvertCosmosCoinToERC20GetSigners.Fn,
evmutiltypes.MsgConvertCosmosCoinFromERC20GetSigners.MsgType: evmutiltypes.MsgConvertCosmosCoinFromERC20GetSigners.Fn,
cdptypes.MsgCreateCDPGetSigners.MsgType: cdptypes.MsgCreateCDPGetSigners.Fn,
cdptypes.MsgDepositGetSigners.MsgType: cdptypes.MsgDepositGetSigners.Fn,
cdptypes.MsgWithdrawGetSigners.MsgType: cdptypes.MsgWithdrawGetSigners.Fn,
cdptypes.MsgDrawDebtGetSigners.MsgType: cdptypes.MsgDrawDebtGetSigners.Fn,
cdptypes.MsgRepayDebtGetSigners.MsgType: cdptypes.MsgRepayDebtGetSigners.Fn,
cdptypes.MsgLiquidateGetSigners.MsgType: cdptypes.MsgLiquidateGetSigners.Fn,
},
}
fmt.Println("registering customer signers", signingOptions.CustomGetSigners)
interfaceRegistry, _ := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
SigningOptions: signingOptions,
Expand Down
185 changes: 175 additions & 10 deletions x/cdp/types/msg.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package types

import (
txsigning "cosmossdk.io/x/tx/signing"
"errors"
"fmt"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
protov1 "github.com/golang/protobuf/proto"
protov2 "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/protoadapt"
"google.golang.org/protobuf/reflect/protoreflect"
"strings"

errorsmod "cosmossdk.io/errors"
Expand All @@ -20,6 +26,33 @@ var (
_ sdk.Msg = &MsgLiquidate{}
)

var (
MsgCreateCDPGetSigners = txsigning.CustomGetSigner{
MsgType: protoreflect.FullName(protov1.MessageName(&MsgCreateCDP{})),
Fn: GetSignersMsgCreateCDP,
}
MsgDepositGetSigners = txsigning.CustomGetSigner{
MsgType: protoreflect.FullName(protov1.MessageName(&MsgDeposit{})),
Fn: GetSignersMsgDeposit,
}
MsgWithdrawGetSigners = txsigning.CustomGetSigner{
MsgType: protoreflect.FullName(protov1.MessageName(&MsgWithdraw{})),
Fn: GetSignersMsgWithdraw,
}
MsgDrawDebtGetSigners = txsigning.CustomGetSigner{
MsgType: protoreflect.FullName(protov1.MessageName(&MsgDrawDebt{})),
Fn: GetSignersMsgDrawDebt,
}
MsgRepayDebtGetSigners = txsigning.CustomGetSigner{
MsgType: protoreflect.FullName(protov1.MessageName(&MsgRepayDebt{})),
Fn: GetSignersMsgRepayDebt,
}
MsgLiquidateGetSigners = txsigning.CustomGetSigner{
MsgType: protoreflect.FullName(protov1.MessageName(&MsgLiquidate{})),
Fn: GetSignersMsgLiquidate,
}
)

// NewMsgCreateCDP returns a new MsgPlaceBid.
func NewMsgCreateCDP(sender sdk.AccAddress, collateral sdk.Coin, principal sdk.Coin, collateralType string) MsgCreateCDP {
return MsgCreateCDP{
Expand Down Expand Up @@ -61,7 +94,7 @@ func (msg MsgCreateCDP) GetSignBytes() []byte {
return sdk.MustSortJSON(bz)
}

// GetSigners returns the addresses of signers that must sign.
// GetSignersMsgCreateCDP returns the addresses of signers that must sign.
func (msg MsgCreateCDP) GetSigners() []sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
Expand All @@ -70,6 +103,28 @@ func (msg MsgCreateCDP) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sender}
}

func GetSignersMsgCreateCDP(msg protov2.Message) ([][]byte, error) {
msgV1 := protoadapt.MessageV1Of(msg)

tryingTypeAnyV1, err := codectypes.NewAnyWithValue(msgV1)
if err != nil {
return nil, err
}

msgTyped := &MsgCreateCDP{}
err = msgTyped.Unmarshal(tryingTypeAnyV1.Value)
if err != nil {
return nil, err
}

sender, err := sdk.AccAddressFromBech32(msgTyped.Sender)
if err != nil {
return nil, err
}

return [][]byte{sender}, nil
}

// NewMsgDeposit returns a new MsgDeposit
func NewMsgDeposit(owner sdk.AccAddress, depositor sdk.AccAddress, collateral sdk.Coin, collateralType string) MsgDeposit {
return MsgDeposit{
Expand Down Expand Up @@ -112,13 +167,35 @@ func (msg MsgDeposit) GetSignBytes() []byte {
return sdk.MustSortJSON(bz)
}

// GetSigners returns the addresses of signers that must sign.
func (msg MsgDeposit) GetSigners() []sdk.AccAddress {
depositor, err := sdk.AccAddressFromBech32(msg.Depositor)
// GetSignersMsgCreateCDP returns the addresses of signers that must sign.
//func (msg MsgDeposit) GetSigners() []sdk.AccAddress {
// depositor, err := sdk.AccAddressFromBech32(msg.Depositor)
// if err != nil {
// panic(err)
// }
// return []sdk.AccAddress{depositor}
//}

func GetSignersMsgDeposit(msg protov2.Message) ([][]byte, error) {
msgV1 := protoadapt.MessageV1Of(msg)

tryingTypeAnyV1, err := codectypes.NewAnyWithValue(msgV1)
if err != nil {
panic(err)
return nil, err
}
return []sdk.AccAddress{depositor}

msgTyped := &MsgDeposit{}
err = msgTyped.Unmarshal(tryingTypeAnyV1.Value)
if err != nil {
return nil, err
}

depositor, err := sdk.AccAddressFromBech32(msgTyped.Depositor)
if err != nil {
return nil, err
}

return [][]byte{depositor}, nil
}

// NewMsgWithdraw returns a new MsgDeposit
Expand Down Expand Up @@ -163,7 +240,7 @@ func (msg MsgWithdraw) GetSignBytes() []byte {
return sdk.MustSortJSON(bz)
}

// GetSigners returns the addresses of signers that must sign.
// GetSignersMsgCreateCDP returns the addresses of signers that must sign.
func (msg MsgWithdraw) GetSigners() []sdk.AccAddress {
depositor, err := sdk.AccAddressFromBech32(msg.Depositor)
if err != nil {
Expand All @@ -172,6 +249,28 @@ func (msg MsgWithdraw) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{depositor}
}

func GetSignersMsgWithdraw(msg protov2.Message) ([][]byte, error) {
msgV1 := protoadapt.MessageV1Of(msg)

tryingTypeAnyV1, err := codectypes.NewAnyWithValue(msgV1)
if err != nil {
return nil, err
}

msgTyped := &MsgWithdraw{}
err = msgTyped.Unmarshal(tryingTypeAnyV1.Value)
if err != nil {
return nil, err
}

depositor, err := sdk.AccAddressFromBech32(msgTyped.Depositor)
if err != nil {
return nil, err
}

return [][]byte{depositor}, nil
}

// NewMsgDrawDebt returns a new MsgDrawDebt
func NewMsgDrawDebt(sender sdk.AccAddress, collateralType string, principal sdk.Coin) MsgDrawDebt {
return MsgDrawDebt{
Expand Down Expand Up @@ -209,7 +308,7 @@ func (msg MsgDrawDebt) GetSignBytes() []byte {
return sdk.MustSortJSON(bz)
}

// GetSigners returns the addresses of signers that must sign.
// GetSignersMsgCreateCDP returns the addresses of signers that must sign.
func (msg MsgDrawDebt) GetSigners() []sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
Expand All @@ -218,6 +317,28 @@ func (msg MsgDrawDebt) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sender}
}

func GetSignersMsgDrawDebt(msg protov2.Message) ([][]byte, error) {
msgV1 := protoadapt.MessageV1Of(msg)

tryingTypeAnyV1, err := codectypes.NewAnyWithValue(msgV1)
if err != nil {
return nil, err
}

msgTyped := &MsgDrawDebt{}
err = msgTyped.Unmarshal(tryingTypeAnyV1.Value)
if err != nil {
return nil, err
}

sender, err := sdk.AccAddressFromBech32(msgTyped.Sender)
if err != nil {
return nil, err
}

return [][]byte{sender}, nil
}

// NewMsgRepayDebt returns a new MsgRepayDebt
func NewMsgRepayDebt(sender sdk.AccAddress, collateralType string, payment sdk.Coin) MsgRepayDebt {
return MsgRepayDebt{
Expand Down Expand Up @@ -255,7 +376,7 @@ func (msg MsgRepayDebt) GetSignBytes() []byte {
return sdk.MustSortJSON(bz)
}

// GetSigners returns the addresses of signers that must sign.
// GetSignersMsgCreateCDP returns the addresses of signers that must sign.
func (msg MsgRepayDebt) GetSigners() []sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
Expand All @@ -264,6 +385,28 @@ func (msg MsgRepayDebt) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sender}
}

func GetSignersMsgRepayDebt(msg protov2.Message) ([][]byte, error) {
msgV1 := protoadapt.MessageV1Of(msg)

tryingTypeAnyV1, err := codectypes.NewAnyWithValue(msgV1)
if err != nil {
return nil, err
}

msgTyped := &MsgRepayDebt{}
err = msgTyped.Unmarshal(tryingTypeAnyV1.Value)
if err != nil {
return nil, err
}

sender, err := sdk.AccAddressFromBech32(msgTyped.Sender)
if err != nil {
return nil, err
}

return [][]byte{sender}, nil
}

// NewMsgLiquidate returns a new MsgLiquidate
func NewMsgLiquidate(keeper, borrower sdk.AccAddress, ctype string) MsgLiquidate {
return MsgLiquidate{
Expand Down Expand Up @@ -302,11 +445,33 @@ func (msg MsgLiquidate) GetSignBytes() []byte {
return sdk.MustSortJSON(bz)
}

// GetSigners returns the addresses of signers that must sign.
// GetSignersMsgCreateCDP returns the addresses of signers that must sign.
func (msg MsgLiquidate) GetSigners() []sdk.AccAddress {
keeper, err := sdk.AccAddressFromBech32(msg.Keeper)
if err != nil {
panic(err)
}
return []sdk.AccAddress{keeper}
}

func GetSignersMsgLiquidate(msg protov2.Message) ([][]byte, error) {
msgV1 := protoadapt.MessageV1Of(msg)

tryingTypeAnyV1, err := codectypes.NewAnyWithValue(msgV1)
if err != nil {
return nil, err
}

msgTyped := &MsgLiquidate{}
err = msgTyped.Unmarshal(tryingTypeAnyV1.Value)
if err != nil {
return nil, err
}

keeper, err := sdk.AccAddressFromBech32(msgTyped.Keeper)
if err != nil {
return nil, err
}

return [][]byte{keeper}, nil
}

0 comments on commit fedeac5

Please sign in to comment.