Skip to content

Commit

Permalink
Merge pull request #62 from b-harvest/patch/issue-27
Browse files Browse the repository at this point in the history
refactor: `CustomGetSigners` for `MsgSwapOrder`
  • Loading branch information
poorphd authored Jul 17, 2024
2 parents c0f23eb + 200a4ef commit 3a78be0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ import (
ibctesting "github.com/cosmos/ibc-go/v8/testing"
ibctestingtypes "github.com/cosmos/ibc-go/v8/testing/types"

coinswapv1 "github.com/Canto-Network/Canto/v7/api/canto/coinswap/v1"
erc20v1 "github.com/Canto-Network/Canto/v7/api/canto/erc20/v1"
evmv1 "github.com/evmos/ethermint/api/ethermint/evm/v1"
ethante "github.com/evmos/ethermint/app/ante"
Expand Down Expand Up @@ -310,9 +311,13 @@ func NewCanto(
},
}

// evm/MsgEthereumTx, erc20/MsgConvertERC20
// evm/MsgEthereumTx, erc20/MsgConvertERC20, coinswap/MsgSwapOrder
signingOptions.DefineCustomGetSigners(protov2.MessageName(&evmv1.MsgEthereumTx{}), evmtypes.GetSignersFromMsgEthereumTxV2)
signingOptions.DefineCustomGetSigners(protov2.MessageName(&erc20v1.MsgConvertERC20{}), erc20types.GetSignersFromMsgConvertERC20V2)
signingOptions.DefineCustomGetSigners(
protov2.MessageName(&coinswapv1.MsgSwapOrder{}),
coinswaptypes.CreateGetSignersFromMsgSwapOrderV2(&signingOptions),
)

interfaceRegistry, _ := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
Expand Down
71 changes: 71 additions & 0 deletions app/signer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package app

import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
protov2 "google.golang.org/protobuf/proto"

"cosmossdk.io/x/tx/signing"
"github.com/cosmos/cosmos-sdk/codec/address"
sdk "github.com/cosmos/cosmos-sdk/types"

coinswapv1 "github.com/Canto-Network/Canto/v7/api/canto/coinswap/v1"
erc20v1 "github.com/Canto-Network/Canto/v7/api/canto/erc20/v1"
coinswaptypes "github.com/Canto-Network/Canto/v7/x/coinswap/types"
erc20types "github.com/Canto-Network/Canto/v7/x/erc20/types"
)

func TestDefineCustomGetSigners(t *testing.T) {
addr := "canto13e9t6s6ra8caz5zzmy5w9v23dm2dr5nrr9sz03"
accAddr, err := sdk.AccAddressFromBech32(addr)
require.NoError(t, err)

signingOptions := signing.Options{
AddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
},
ValidatorAddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
},
}
signingOptions.DefineCustomGetSigners(protov2.MessageName(&erc20v1.MsgConvertERC20{}), erc20types.GetSignersFromMsgConvertERC20V2)
signingOptions.DefineCustomGetSigners(protov2.MessageName(&coinswapv1.MsgSwapOrder{}), coinswaptypes.CreateGetSignersFromMsgSwapOrderV2(&signingOptions))

ctx, err := signing.NewContext(signingOptions)
require.NoError(t, err)

tests := []struct {
name string
msg protov2.Message
want [][]byte
wantErr bool
}{
{
name: "MsgConvertERC20",
msg: &erc20v1.MsgConvertERC20{
Sender: common.BytesToAddress(accAddr.Bytes()).String(),
},
want: [][]byte{accAddr.Bytes()},
},
{
name: "MsgSwapOrder",
msg: &coinswapv1.MsgSwapOrder{
Input: &coinswapv1.Input{Address: addr},
},
want: [][]byte{accAddr.Bytes()},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
signers, err := ctx.GetSigners(test.msg)
if test.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
require.Equal(t, test.want, signers)
})
}
}
21 changes: 21 additions & 0 deletions x/coinswap/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package types

import (
"fmt"

sdkmath "cosmossdk.io/math"
"cosmossdk.io/x/tx/signing"
coinswapv1 "github.com/Canto-Network/Canto/v7/api/canto/coinswap/v1"
sdk "github.com/cosmos/cosmos-sdk/types"
protov2 "google.golang.org/protobuf/proto"
)

var (
Expand Down Expand Up @@ -72,3 +77,19 @@ func NewMsgRemoveLiquidity(
Sender: sender,
}
}

func CreateGetSignersFromMsgSwapOrderV2(options *signing.Options) func(msg protov2.Message) ([][]byte, error) {
return func(msg protov2.Message) ([][]byte, error) {
msgv2, ok := msg.(*coinswapv1.MsgSwapOrder)
if !ok {
return nil, fmt.Errorf("invalid x/coinswap/MsgSwapOrder msg v2: %v", msg)
}

addr, err := options.AddressCodec.StringToBytes(msgv2.Input.Address)
if err != nil {
return nil, err
}

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

0 comments on commit 3a78be0

Please sign in to comment.