From cdb3016716cc8612cbf0b40d7c6291ff4714dbff Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Fri, 13 Sep 2024 17:07:25 +0700 Subject: [PATCH] commit --- .../asset/priviledges/mint/messages.proto | 17 + x/asset/keeper/keeper_test.go | 2 +- x/asset/keeper/msg_server.go | 2 +- x/asset/priviledges/freeze/msg_server.go | 1 + x/asset/priviledges/mint/messages.pb.go | 467 ++++++++++++++++++ x/asset/priviledges/mint/msg_server.go | 38 ++ x/asset/priviledges/mint/priv.go | 11 + x/asset/priviledges/mint/types.go | 10 + x/asset/types/privilege.go | 5 +- 9 files changed, 549 insertions(+), 4 deletions(-) create mode 100644 proto/realionetwork/asset/priviledges/mint/messages.proto create mode 100644 x/asset/priviledges/freeze/msg_server.go create mode 100644 x/asset/priviledges/mint/messages.pb.go create mode 100644 x/asset/priviledges/mint/msg_server.go create mode 100644 x/asset/priviledges/mint/priv.go create mode 100644 x/asset/priviledges/mint/types.go diff --git a/proto/realionetwork/asset/priviledges/mint/messages.proto b/proto/realionetwork/asset/priviledges/mint/messages.proto new file mode 100644 index 00000000..4ed6b9d6 --- /dev/null +++ b/proto/realionetwork/asset/priviledges/mint/messages.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; +package realionetwork.asset.v1; + +option go_package = "github.com/realiotech/realio-network/x/asset/priviledges/mint"; + +import "gogoproto/gogo.proto"; +import "cosmos_proto/cosmos.proto"; +import "google/protobuf/any.proto"; +import "realionetwork/asset/v1/token.proto"; + + +message MsgMintToken { + string account = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ]; + string token_id = 2; + string to_account = 3; + uint64 amount = 4; +} \ No newline at end of file diff --git a/x/asset/keeper/keeper_test.go b/x/asset/keeper/keeper_test.go index 86ead128..0fcea79f 100644 --- a/x/asset/keeper/keeper_test.go +++ b/x/asset/keeper/keeper_test.go @@ -106,7 +106,7 @@ func (m MockPrivilegeI) RegisterInterfaces(registry cdctypes.InterfaceRegistry) } func (m MockPrivilegeI) MsgHandler() types.MsgHandler { - return func(context context.Context, privMsg sdk.Msg) (proto.Message, error) { + return func(context context.Context, privMsg sdk.Msg, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) { typeUrl := sdk.MsgTypeURL(privMsg) if typeUrl != sdk.MsgTypeURL(&types.MsgMock{}) { return nil, errors.New("unsupport msg type") diff --git a/x/asset/keeper/msg_server.go b/x/asset/keeper/msg_server.go index 1bc53aad..1398e94e 100644 --- a/x/asset/keeper/msg_server.go +++ b/x/asset/keeper/msg_server.go @@ -236,7 +236,7 @@ func (k msgServer) ExecutePrivilege(goCtx context.Context, msg *types.MsgExecute } msgHandler := privImplementation.MsgHandler() - _, err = msgHandler(ctx, sdkMsg) + _, err = msgHandler(ctx, sdkMsg, msg.TokenId, userAcc) if err != nil { return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "fail to execute privilege message") } diff --git a/x/asset/priviledges/freeze/msg_server.go b/x/asset/priviledges/freeze/msg_server.go new file mode 100644 index 00000000..f9a3c5ff --- /dev/null +++ b/x/asset/priviledges/freeze/msg_server.go @@ -0,0 +1 @@ +package freeze diff --git a/x/asset/priviledges/mint/messages.pb.go b/x/asset/priviledges/mint/messages.pb.go new file mode 100644 index 00000000..d8f013ba --- /dev/null +++ b/x/asset/priviledges/mint/messages.pb.go @@ -0,0 +1,467 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: realionetwork/asset/priviledges/mint/messages.proto + +package mint + +import ( + fmt "fmt" + _ "github.com/cosmos/cosmos-proto" + _ "github.com/cosmos/cosmos-sdk/codec/types" + _ "github.com/cosmos/gogoproto/gogoproto" + proto "github.com/gogo/protobuf/proto" + _ "github.com/realiotech/realio-network/x/asset/types" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +type MsgMintToken struct { + Account string `protobuf:"bytes,1,opt,name=account,proto3" json:"account,omitempty"` + TokenId string `protobuf:"bytes,2,opt,name=token_id,json=tokenId,proto3" json:"token_id,omitempty"` + ToAccount string `protobuf:"bytes,3,opt,name=to_account,json=toAccount,proto3" json:"to_account,omitempty"` + Amount uint64 `protobuf:"varint,4,opt,name=amount,proto3" json:"amount,omitempty"` +} + +func (m *MsgMintToken) Reset() { *m = MsgMintToken{} } +func (m *MsgMintToken) String() string { return proto.CompactTextString(m) } +func (*MsgMintToken) ProtoMessage() {} +func (*MsgMintToken) Descriptor() ([]byte, []int) { + return fileDescriptor_815603282167548b, []int{0} +} +func (m *MsgMintToken) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MsgMintToken) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MsgMintToken.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MsgMintToken) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgMintToken.Merge(m, src) +} +func (m *MsgMintToken) XXX_Size() int { + return m.Size() +} +func (m *MsgMintToken) XXX_DiscardUnknown() { + xxx_messageInfo_MsgMintToken.DiscardUnknown(m) +} + +var xxx_messageInfo_MsgMintToken proto.InternalMessageInfo + +func (m *MsgMintToken) GetAccount() string { + if m != nil { + return m.Account + } + return "" +} + +func (m *MsgMintToken) GetTokenId() string { + if m != nil { + return m.TokenId + } + return "" +} + +func (m *MsgMintToken) GetToAccount() string { + if m != nil { + return m.ToAccount + } + return "" +} + +func (m *MsgMintToken) GetAmount() uint64 { + if m != nil { + return m.Amount + } + return 0 +} + +func init() { + proto.RegisterType((*MsgMintToken)(nil), "realionetwork.asset.v1.MsgMintToken") +} + +func init() { + proto.RegisterFile("realionetwork/asset/priviledges/mint/messages.proto", fileDescriptor_815603282167548b) +} + +var fileDescriptor_815603282167548b = []byte{ + // 309 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x91, 0xb1, 0x6e, 0xf2, 0x30, + 0x14, 0x85, 0xf1, 0xff, 0x23, 0x28, 0x56, 0xa7, 0x08, 0x21, 0x40, 0xaa, 0x85, 0x98, 0x58, 0x88, + 0x45, 0x99, 0x3b, 0xc0, 0xd6, 0x81, 0x85, 0x56, 0xaa, 0xd4, 0x05, 0x99, 0xc4, 0x35, 0x16, 0xc4, + 0x17, 0xd9, 0x17, 0xda, 0xbe, 0x45, 0x87, 0x3e, 0x4a, 0x1f, 0xa2, 0x23, 0xea, 0xd4, 0xb1, 0x4a, + 0x5e, 0xa4, 0x8a, 0x93, 0x0c, 0x95, 0xda, 0xed, 0x1e, 0x7f, 0xe7, 0x5c, 0xfb, 0xc8, 0x74, 0x6a, + 0xa5, 0xd8, 0x69, 0x30, 0x12, 0x1f, 0xc1, 0x6e, 0xb9, 0x70, 0x4e, 0x22, 0xdf, 0x5b, 0x7d, 0xd4, + 0x3b, 0x19, 0x2b, 0xe9, 0x78, 0xa2, 0x0d, 0xf2, 0x44, 0x3a, 0x27, 0x94, 0x74, 0xe1, 0xde, 0x02, + 0x42, 0xd0, 0xf9, 0x11, 0x0a, 0x7d, 0x28, 0x3c, 0x4e, 0xfa, 0x6d, 0x05, 0x0a, 0xbc, 0x85, 0xe7, + 0x53, 0xe1, 0xee, 0xf7, 0x22, 0x70, 0x09, 0xb8, 0x55, 0x01, 0x0a, 0x51, 0x21, 0x05, 0xa0, 0x76, + 0x92, 0x7b, 0xb5, 0x3e, 0x3c, 0x70, 0x61, 0x9e, 0x4b, 0x34, 0xfc, 0xed, 0x61, 0xc7, 0x09, 0x47, + 0xd8, 0x4a, 0x53, 0x78, 0x86, 0xaf, 0x84, 0x9e, 0x2f, 0x9c, 0x5a, 0x68, 0x83, 0xb7, 0xf9, 0x71, + 0x70, 0x49, 0x9b, 0x22, 0x8a, 0xe0, 0x60, 0xb0, 0x4b, 0x06, 0x64, 0xd4, 0x9a, 0x77, 0x3f, 0xde, + 0xc6, 0xed, 0xf2, 0xca, 0x59, 0x1c, 0x5b, 0xe9, 0xdc, 0x0d, 0x5a, 0x6d, 0xd4, 0xb2, 0x32, 0x06, + 0x3d, 0x7a, 0xe6, 0x77, 0xae, 0x74, 0xdc, 0xfd, 0x97, 0x87, 0x96, 0x4d, 0xaf, 0xaf, 0xe3, 0xe0, + 0x82, 0x52, 0x84, 0x55, 0xb5, 0xf1, 0xbf, 0x87, 0x2d, 0x84, 0x59, 0x99, 0xec, 0xd0, 0x86, 0x48, + 0x3c, 0xaa, 0x0f, 0xc8, 0xa8, 0xbe, 0x2c, 0xd5, 0xfc, 0xee, 0x3d, 0x65, 0xe4, 0x94, 0x32, 0xf2, + 0x95, 0x32, 0xf2, 0x92, 0xb1, 0xda, 0x29, 0x63, 0xb5, 0xcf, 0x8c, 0xd5, 0xee, 0xaf, 0x94, 0xc6, + 0xcd, 0x61, 0x1d, 0x46, 0x90, 0xf0, 0xa2, 0x1f, 0xca, 0x68, 0x53, 0x8e, 0xe3, 0xaa, 0xeb, 0xd3, + 0x1f, 0xdf, 0xb0, 0x6e, 0xf8, 0xda, 0xd3, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0x49, 0x01, 0x8d, + 0xa0, 0xb5, 0x01, 0x00, 0x00, +} + +func (m *MsgMintToken) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *MsgMintToken) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MsgMintToken) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Amount != 0 { + i = encodeVarintMessages(dAtA, i, uint64(m.Amount)) + i-- + dAtA[i] = 0x20 + } + if len(m.ToAccount) > 0 { + i -= len(m.ToAccount) + copy(dAtA[i:], m.ToAccount) + i = encodeVarintMessages(dAtA, i, uint64(len(m.ToAccount))) + i-- + dAtA[i] = 0x1a + } + if len(m.TokenId) > 0 { + i -= len(m.TokenId) + copy(dAtA[i:], m.TokenId) + i = encodeVarintMessages(dAtA, i, uint64(len(m.TokenId))) + i-- + dAtA[i] = 0x12 + } + if len(m.Account) > 0 { + i -= len(m.Account) + copy(dAtA[i:], m.Account) + i = encodeVarintMessages(dAtA, i, uint64(len(m.Account))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintMessages(dAtA []byte, offset int, v uint64) int { + offset -= sovMessages(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *MsgMintToken) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Account) + if l > 0 { + n += 1 + l + sovMessages(uint64(l)) + } + l = len(m.TokenId) + if l > 0 { + n += 1 + l + sovMessages(uint64(l)) + } + l = len(m.ToAccount) + if l > 0 { + n += 1 + l + sovMessages(uint64(l)) + } + if m.Amount != 0 { + n += 1 + sovMessages(uint64(m.Amount)) + } + return n +} + +func sovMessages(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozMessages(x uint64) (n int) { + return sovMessages(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *MsgMintToken) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMessages + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MsgMintToken: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MsgMintToken: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Account", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMessages + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthMessages + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthMessages + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Account = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field TokenId", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMessages + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthMessages + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthMessages + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.TokenId = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ToAccount", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMessages + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthMessages + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthMessages + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ToAccount = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 4: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Amount", wireType) + } + m.Amount = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMessages + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Amount |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := skipMessages(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthMessages + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipMessages(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowMessages + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowMessages + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowMessages + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthMessages + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupMessages + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthMessages + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthMessages = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowMessages = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupMessages = fmt.Errorf("proto: unexpected end of group") +) diff --git a/x/asset/priviledges/mint/msg_server.go b/x/asset/priviledges/mint/msg_server.go new file mode 100644 index 00000000..d1391f81 --- /dev/null +++ b/x/asset/priviledges/mint/msg_server.go @@ -0,0 +1,38 @@ +package mint + +import ( + "context" + fmt "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/gogo/protobuf/proto" + "github.com/pkg/errors" + assettypes "github.com/realiotech/realio-network/x/asset/types" + // "github.com/cosmos/cosmos-sdk/store/types" +) + +func (mp MintPriviledge) MintToken(ctx sdk.Context, msg *MsgMintToken, tokenID string) error { + + mintedCoins := sdk.NewCoin(tokenID, sdk.NewIntFromUint64(msg.Amount)) + + err := mp.bk.MintCoins(ctx, assettypes.ModuleName, sdk.NewCoins(mintedCoins)) + if err != nil { + return err + } + + err = mp.bk.SendCoinsFromModuleToAccount(ctx, assettypes.ModuleName, sdk.MustAccAddressFromBech32(msg.ToAccount), sdk.NewCoins(mintedCoins)) + + return err +} + +func (mp MintPriviledge) MsgHandler(context context.Context, msg proto.Message, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) { + ctx := sdk.UnwrapSDKContext(context) + + switch msg := msg.(type) { + case *MsgMintToken: + return nil, mp.MintToken(ctx, msg, tokenID) + default: + errMsg := fmt.Sprintf("unrecognized message type: %T for Mint priviledge", msg) + return nil, errors.Errorf(errMsg) + } +} diff --git a/x/asset/priviledges/mint/priv.go b/x/asset/priviledges/mint/priv.go new file mode 100644 index 00000000..e0b6ecfc --- /dev/null +++ b/x/asset/priviledges/mint/priv.go @@ -0,0 +1,11 @@ +package mint + +type MintPriviledge struct { + bk BankKeeper +} + +func NewMintPriviledge(bk BankKeeper) MintPriviledge { + return MintPriviledge{ + bk: bk, + } +} diff --git a/x/asset/priviledges/mint/types.go b/x/asset/priviledges/mint/types.go new file mode 100644 index 00000000..f2dafe99 --- /dev/null +++ b/x/asset/priviledges/mint/types.go @@ -0,0 +1,10 @@ +package mint + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type BankKeeper interface { + SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error + MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error +} diff --git a/x/asset/types/privilege.go b/x/asset/types/privilege.go index 41643253..08387b38 100644 --- a/x/asset/types/privilege.go +++ b/x/asset/types/privilege.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/gogo/protobuf/proto" "github.com/spf13/cobra" + // "github.com/cosmos/cosmos-sdk/store/types" ) type PrivilegeMsgI interface { @@ -21,6 +22,6 @@ type PrivilegeI interface { CLI() *cobra.Command } -type MsgHandler func(context context.Context, privMsg sdk.Msg) (proto.Message, error) +type MsgHandler func(context context.Context, msg proto.Message, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) -type QueryHandler func(context context.Context, privQuery sdk.Msg) (proto.Message, error) +type QueryHandler func(context context.Context, privQuery proto.Message, tokenID string) (proto.Message, error)