Skip to content

Commit

Permalink
Add gov proposal type to modify CW->ERC pointer registration (#1691)
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen authored May 21, 2024
1 parent c92dc5f commit 015816a
Show file tree
Hide file tree
Showing 12 changed files with 1,011 additions and 15 deletions.
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ require (
golang.org/x/sync v0.7.0
golang.org/x/text v0.15.0
golang.org/x/time v0.3.0
google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe
google.golang.org/grpc v1.61.0
google.golang.org/protobuf v1.33.0
google.golang.org/genproto/googleapis/api v0.0.0-20240513163218-0867130af1f8
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.1
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -125,6 +125,7 @@ require (
github.com/fzipp/gocyclo v0.5.1 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-critic/go-critic v0.6.3 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
Expand Down Expand Up @@ -172,6 +173,7 @@ require (
github.com/gostaticanalysis/nilerr v0.1.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-bexpr v0.1.10 // indirect
Expand Down Expand Up @@ -321,7 +323,7 @@ require (
golang.org/x/term v0.20.0 // indirect
golang.org/x/tools v0.21.0 // indirect
google.golang.org/genproto v0.0.0-20240116215550-a9fa1716bcac // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
honnef.co/go/tools v0.3.1 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 h1:BAIP2Gihuqh
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46/go.mod h1:QNpY22eby74jVhqH4WhDLDwxc/vqsern6pW+u2kbkpc=
github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/ghemawat/stream v0.0.0-20171120220530-696b145b53b9/go.mod h1:106OIgooyS7OzLDOpUGgm9fA3bQENb/cFSyyBmMoJDs=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down Expand Up @@ -765,6 +766,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
github.com/guptarohit/asciigraph v0.5.5/go.mod h1:dYl5wwK4gNsnFf9Zp+l06rFiDZ5YtXM6x7SRWZ3KGag=
Expand Down Expand Up @@ -2257,8 +2260,12 @@ google.golang.org/genproto v0.0.0-20240116215550-a9fa1716bcac h1:ZL/Teoy/ZGnzyrq
google.golang.org/genproto v0.0.0-20240116215550-a9fa1716bcac/go.mod h1:+Rvu7ElI+aLzyDQhpHMFMMltsD6m7nqpuWDd2CwJw3k=
google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe h1:0poefMBYvYbs7g5UkjS6HcxBPaTRAmznle9jnxYoAI8=
google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe/go.mod h1:4jWUdICTdgc3Ibxmr8nAJiiLHwQBY0UI0XZcEMaFKaA=
google.golang.org/genproto/googleapis/api v0.0.0-20240513163218-0867130af1f8 h1:W5Xj/70xIA4x60O/IFyXivR5MGqblAb8R3w26pnD6No=
google.golang.org/genproto/googleapis/api v0.0.0-20240513163218-0867130af1f8/go.mod h1:vPrPUTsDCYxXWjP7clS81mZ6/803D8K4iM9Ma27VKas=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe h1:bQnxqljG/wqi4NTXu2+DJ3n7APcEA882QZ1JvhQAq9o=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe/go.mod h1:PAREbraiVEVGVdTZsVWjSbbTtSyGbAgIIvni8a8CD5s=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 h1:mxSlqyb8ZAHsYDCfiXN1EDdNTdvjUJSLY+OnAUtYNYA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8/go.mod h1:I7Y+G38R2bu5j1aLzfFmQfTcU/WnFuqDwLZAbvKTKpM=
google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o=
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
Expand All @@ -2278,6 +2285,8 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
24 changes: 24 additions & 0 deletions proto/evm/gov.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,27 @@ message AddERCCW721PointerProposal {
string pointer = 4 [(gogoproto.moretags) = "yaml:\"pointer\""];
uint32 version = 5 [(gogoproto.moretags) = "yaml:\"version\""];
}

message AddCWERC20PointerProposal {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
option (gogoproto.goproto_stringer) = false;

string title = 1 [ (gogoproto.moretags) = "yaml:\"title\"" ];
string description = 2 [ (gogoproto.moretags) = "yaml:\"description\"" ];
string pointee = 3 [(gogoproto.moretags) = "yaml:\"pointee\""];
string pointer = 4 [(gogoproto.moretags) = "yaml:\"pointer\""];
uint32 version = 5 [(gogoproto.moretags) = "yaml:\"version\""];
}

message AddCWERC721PointerProposal {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
option (gogoproto.goproto_stringer) = false;

string title = 1 [ (gogoproto.moretags) = "yaml:\"title\"" ];
string description = 2 [ (gogoproto.moretags) = "yaml:\"description\"" ];
string pointee = 3 [(gogoproto.moretags) = "yaml:\"pointee\""];
string pointer = 4 [(gogoproto.moretags) = "yaml:\"pointer\""];
uint32 version = 5 [(gogoproto.moretags) = "yaml:\"version\""];
}
106 changes: 106 additions & 0 deletions x/evm/client/cli/gov_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,109 @@ func NewAddERCCW721PointerProposalTxCmd() *cobra.Command {

return cmd
}

func NewAddCWERC20PointerProposalTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add-cw-erc20-pointer title description erc20address version deposit [pointer address]",
Args: cobra.RangeArgs(5, 6),
Short: "Submit an add CW-ERC20 pointer proposal",
Long: strings.TrimSpace(`
Submit a proposal to register an CW pointer contract address for a ERC20 token.
Not specifying the pointer address means a proposal that deletes the existing pointer.
`),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

version, err := strconv.ParseUint(args[3], 10, 16)
if err != nil {
return err
}
deposit, err := sdk.ParseCoinsNormalized(args[4])
if err != nil {
return err
}
var pointer string
if len(args) == 6 {
pointer = args[5]
}

// Convert proposal to RegisterPairsProposal Type
from := clientCtx.GetFromAddress()

content := types.AddCWERC20PointerProposal{
Title: args[0],
Description: args[1],
Pointee: args[2],
Version: uint32(version),
Pointer: pointer,
}

msg, err := govtypes.NewMsgSubmitProposal(&content, deposit, from)
if err != nil {
return err
}

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

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func NewAddCWERC721PointerProposalTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add-cw-erc721-pointer title description erc721address version deposit [pointer address]",
Args: cobra.RangeArgs(5, 6),
Short: "Submit an add CW-ERC721 pointer proposal",
Long: strings.TrimSpace(`
Submit a proposal to register an CW pointer contract address for a ERC721 token.
Not specifying the pointer address means a proposal that deletes the existing pointer.
`),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

version, err := strconv.ParseUint(args[3], 10, 16)
if err != nil {
return err
}
deposit, err := sdk.ParseCoinsNormalized(args[4])
if err != nil {
return err
}
var pointer string
if len(args) == 6 {
pointer = args[5]
}

// Convert proposal to RegisterPairsProposal Type
from := clientCtx.GetFromAddress()

content := types.AddCWERC721PointerProposal{
Title: args[0],
Description: args[1],
Pointee: args[2],
Version: uint32(version),
Pointer: pointer,
}

msg, err := govtypes.NewMsgSubmitProposal(&content, deposit, from)
if err != nil {
return err
}

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

flags.AddTxFlagsToCmd(cmd)

return cmd
}
2 changes: 2 additions & 0 deletions x/evm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func GetTxCmd() *cobra.Command {
cmd.AddCommand(NewAddERCNativePointerProposalTxCmd())
cmd.AddCommand(NewAddERCCW20PointerProposalTxCmd())
cmd.AddCommand(NewAddERCCW721PointerProposalTxCmd())
cmd.AddCommand(NewAddCWERC20PointerProposalTxCmd())
cmd.AddCommand(NewAddCWERC721PointerProposalTxCmd())
cmd.AddCommand(AssociateContractAddressCmd())

return cmd
Expand Down
24 changes: 24 additions & 0 deletions x/evm/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,27 @@ func HandleAddERCCW721PointerProposal(ctx sdk.Context, k *keeper.Keeper, p *type
}
return k.SetERC721CW721PointerWithVersion(ctx, p.Pointee, common.HexToAddress(p.Pointer), uint16(p.Version))
}

func HandleAddCWERC20PointerProposal(ctx sdk.Context, k *keeper.Keeper, p *types.AddCWERC20PointerProposal) error {
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypePointerRegistered, sdk.NewAttribute(types.AttributeKeyPointerType, "erc20"),
sdk.NewAttribute(types.AttributeKeyPointerAddress, p.Pointer), sdk.NewAttribute(types.AttributeKeyPointee, p.Pointee),
sdk.NewAttribute(types.AttributeKeyPointerVersion, fmt.Sprintf("%d", p.Version))))
if p.Pointer == "" {
k.DeleteCW20ERC20Pointer(ctx, common.HexToAddress(p.Pointee), uint16(p.Version))
return nil
}
return k.SetCW20ERC20PointerWithVersion(ctx, common.HexToAddress(p.Pointee), p.Pointer, uint16(p.Version))
}

func HandleAddCWERC721PointerProposal(ctx sdk.Context, k *keeper.Keeper, p *types.AddCWERC721PointerProposal) error {
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypePointerRegistered, sdk.NewAttribute(types.AttributeKeyPointerType, "erc721"),
sdk.NewAttribute(types.AttributeKeyPointerAddress, p.Pointer), sdk.NewAttribute(types.AttributeKeyPointee, p.Pointee),
sdk.NewAttribute(types.AttributeKeyPointerVersion, fmt.Sprintf("%d", p.Version))))
if p.Pointer == "" {
k.DeleteCW721ERC721Pointer(ctx, common.HexToAddress(p.Pointee), uint16(p.Version))
return nil
}
return k.SetCW721ERC721PointerWithVersion(ctx, common.HexToAddress(p.Pointee), p.Pointer, uint16(p.Version))
}
66 changes: 66 additions & 0 deletions x/evm/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,69 @@ func TestAddERCCW721PointerProposals(t *testing.T) {
require.Equal(t, uint16(1), ver)
require.Equal(t, addr, pointer1)
}

func TestAddCWERC20PointerProposals(t *testing.T) {
k, ctx := testkeeper.MockEVMKeeper()
_, pointee1 := testkeeper.MockAddressPair()
pointer1, _ := testkeeper.MockAddressPair()
pointer2, _ := testkeeper.MockAddressPair()
require.Nil(t, evm.HandleAddCWERC20PointerProposal(ctx, k, &types.AddCWERC20PointerProposal{
Pointee: pointee1.Hex(),
Version: 1,
Pointer: pointer1.String(),
}))
addr, ver, exists := k.GetCW20ERC20Pointer(ctx, pointee1)
require.True(t, exists)
require.Equal(t, uint16(1), ver)
require.Equal(t, addr, pointer1)
require.Nil(t, evm.HandleAddCWERC20PointerProposal(ctx, k, &types.AddCWERC20PointerProposal{
Pointee: pointee1.Hex(),
Version: 2,
Pointer: pointer2.String(),
}))
addr, ver, exists = k.GetCW20ERC20Pointer(ctx, pointee1)
require.True(t, exists)
require.Equal(t, uint16(2), ver)
require.Equal(t, addr, pointer2)
require.Nil(t, evm.HandleAddCWERC20PointerProposal(ctx, k, &types.AddCWERC20PointerProposal{
Pointee: pointee1.Hex(),
Version: 2,
}))
addr, ver, exists = k.GetCW20ERC20Pointer(ctx, pointee1)
require.True(t, exists)
require.Equal(t, uint16(1), ver)
require.Equal(t, addr, pointer1)
}

func TestAddCWERC721PointerProposals(t *testing.T) {
k, ctx := testkeeper.MockEVMKeeper()
_, pointee1 := testkeeper.MockAddressPair()
pointer1, _ := testkeeper.MockAddressPair()
pointer2, _ := testkeeper.MockAddressPair()
require.Nil(t, evm.HandleAddCWERC721PointerProposal(ctx, k, &types.AddCWERC721PointerProposal{
Pointee: pointee1.Hex(),
Version: 1,
Pointer: pointer1.String(),
}))
addr, ver, exists := k.GetCW721ERC721Pointer(ctx, pointee1)
require.True(t, exists)
require.Equal(t, uint16(1), ver)
require.Equal(t, addr, pointer1)
require.Nil(t, evm.HandleAddCWERC721PointerProposal(ctx, k, &types.AddCWERC721PointerProposal{
Pointee: pointee1.Hex(),
Version: 2,
Pointer: pointer2.String(),
}))
addr, ver, exists = k.GetCW721ERC721Pointer(ctx, pointee1)
require.True(t, exists)
require.Equal(t, uint16(2), ver)
require.Equal(t, addr, pointer2)
require.Nil(t, evm.HandleAddCWERC721PointerProposal(ctx, k, &types.AddCWERC721PointerProposal{
Pointee: pointee1.Hex(),
Version: 2,
}))
addr, ver, exists = k.GetCW721ERC721Pointer(ctx, pointee1)
require.True(t, exists)
require.Equal(t, uint16(1), ver)
require.Equal(t, addr, pointer1)
}
4 changes: 4 additions & 0 deletions x/evm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func NewProposalHandler(k keeper.Keeper) govtypes.Handler {
return HandleAddERCCW20PointerProposal(ctx, &k, c)
case *types.AddERCCW721PointerProposal:
return HandleAddERCCW721PointerProposal(ctx, &k, c)
case *types.AddCWERC20PointerProposal:
return HandleAddCWERC20PointerProposal(ctx, &k, c)
case *types.AddCWERC721PointerProposal:
return HandleAddCWERC721PointerProposal(ctx, &k, c)
default:
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized evm proposal content type: %T", c)
}
Expand Down
Loading

0 comments on commit 015816a

Please sign in to comment.