From c4ff578bbdc744f10e06f4ae16ed6d57b4269a55 Mon Sep 17 00:00:00 2001 From: 170210 Date: Fri, 26 Apr 2024 17:28:18 +0900 Subject: [PATCH 01/47] refactor: fswap module's proto Signed-off-by: 170210 --- docs/core/proto-docs.md | 41 +- proto/lbm/fswap/v1/fswap.proto | 28 +- proto/lbm/fswap/v1/genesis.proto | 5 +- proto/lbm/fswap/v1/params.proto | 12 - x/fswap/types/fswap.pb.go | 695 ++++++++++++++++++++++++++++++- x/fswap/types/genesis.pb.go | 42 +- x/fswap/types/params.pb.go | 317 -------------- 7 files changed, 749 insertions(+), 391 deletions(-) delete mode 100644 proto/lbm/fswap/v1/params.proto delete mode 100644 x/fswap/types/params.pb.go diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index 7d6e8b010d..ad1362c73b 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -860,11 +860,10 @@ - [EventSwapCoins](#lbm.fswap.v1.EventSwapCoins) - [lbm/fswap/v1/fswap.proto](#lbm/fswap/v1/fswap.proto) + - [FswapInit](#lbm.fswap.v1.FswapInit) + - [FswapInitProposal](#lbm.fswap.v1.FswapInitProposal) - [Swapped](#lbm.fswap.v1.Swapped) -- [lbm/fswap/v1/params.proto](#lbm/fswap/v1/params.proto) - - [Params](#lbm.fswap.v1.Params) - - [lbm/fswap/v1/genesis.proto](#lbm/fswap/v1/genesis.proto) - [GenesisState](#lbm.fswap.v1.GenesisState) @@ -12751,47 +12750,51 @@ Msg defines the foundation Msg service. - + -### Swapped +### FswapInit | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `old_coin_amount` | [string](#string) | | | -| `new_coin_amount` | [string](#string) | | | +| `fromDenom` | [string](#string) | | | +| `toDenom` | [string](#string) | | | +| `AmountLimit` | [string](#string) | | | +| `SwapRate` | [string](#string) | | | - - + - +### FswapInitProposal +From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 - + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `title` | [string](#string) | | | +| `description` | [string](#string) | | | +| `fswap_init` | [FswapInit](#lbm.fswap.v1.FswapInit) | | | - -

Top

-## lbm/fswap/v1/params.proto + - +### Swapped -### Params -Params defines the parameters for the module. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `swappable_new_coin_amount` | [string](#string) | | | +| `old_coin_amount` | [string](#string) | | | +| `new_coin_amount` | [string](#string) | | | @@ -12822,7 +12825,7 @@ GenesisState defines the fswap module's genesis state. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `params` | [Params](#lbm.fswap.v1.Params) | | | +| `fswap_init` | [FswapInit](#lbm.fswap.v1.FswapInit) | | | | `swapped` | [Swapped](#lbm.fswap.v1.Swapped) | | | diff --git a/proto/lbm/fswap/v1/fswap.proto b/proto/lbm/fswap/v1/fswap.proto index 14315e32d0..27ad27dba4 100644 --- a/proto/lbm/fswap/v1/fswap.proto +++ b/proto/lbm/fswap/v1/fswap.proto @@ -5,8 +5,32 @@ option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types"; import "gogoproto/gogo.proto"; +message FswapInit { + option (gogoproto.equal) = true; + option (gogoproto.goproto_stringer) = false; + + string fromDenom = 1; + string toDenom = 2; + string AmountLimit = 3 + [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; + string SwapRate = 4 + [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; +} + +// From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 +message FswapInitProposal { + option (gogoproto.equal) = true; + option (gogoproto.goproto_stringer) = false; + + string title = 1; + string description = 2; + FswapInit fswap_init = 3 [(gogoproto.nullable) = false]; +} + message Swapped { option (gogoproto.goproto_stringer) = false; - string old_coin_amount = 1 [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; - string new_coin_amount = 2 [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; + string old_coin_amount = 1 + [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; + string new_coin_amount = 2 + [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; } diff --git a/proto/lbm/fswap/v1/genesis.proto b/proto/lbm/fswap/v1/genesis.proto index 43b639a341..74739b80c8 100644 --- a/proto/lbm/fswap/v1/genesis.proto +++ b/proto/lbm/fswap/v1/genesis.proto @@ -5,10 +5,9 @@ option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types"; import "gogoproto/gogo.proto"; import "lbm/fswap/v1/fswap.proto"; -import "lbm/fswap/v1/params.proto"; // GenesisState defines the fswap module's genesis state. message GenesisState { - Params params = 1 [(gogoproto.nullable) = false]; - Swapped swapped = 2 [(gogoproto.nullable) = false]; + FswapInit fswap_init = 1 [(gogoproto.nullable) = false]; + Swapped swapped = 2 [(gogoproto.nullable) = false]; } diff --git a/proto/lbm/fswap/v1/params.proto b/proto/lbm/fswap/v1/params.proto deleted file mode 100644 index bc5535afb4..0000000000 --- a/proto/lbm/fswap/v1/params.proto +++ /dev/null @@ -1,12 +0,0 @@ -syntax = "proto3"; -package lbm.fswap.v1; - -option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types"; - -import "gogoproto/gogo.proto"; - -// Params defines the parameters for the module. -message Params { - option (gogoproto.goproto_stringer) = false; - string swappable_new_coin_amount = 1 [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; -} \ No newline at end of file diff --git a/x/fswap/types/fswap.pb.go b/x/fswap/types/fswap.pb.go index 1923c31269..f29cdde09d 100644 --- a/x/fswap/types/fswap.pb.go +++ b/x/fswap/types/fswap.pb.go @@ -24,6 +24,119 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package +type FswapInit struct { + FromDenom string `protobuf:"bytes,1,opt,name=fromDenom,proto3" json:"fromDenom,omitempty"` + ToDenom string `protobuf:"bytes,2,opt,name=toDenom,proto3" json:"toDenom,omitempty"` + AmountLimit github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,3,opt,name=AmountLimit,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"AmountLimit"` + SwapRate github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,4,opt,name=SwapRate,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"SwapRate"` +} + +func (m *FswapInit) Reset() { *m = FswapInit{} } +func (*FswapInit) ProtoMessage() {} +func (*FswapInit) Descriptor() ([]byte, []int) { + return fileDescriptor_42ca60eaf37a2b67, []int{0} +} +func (m *FswapInit) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *FswapInit) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_FswapInit.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 *FswapInit) XXX_Merge(src proto.Message) { + xxx_messageInfo_FswapInit.Merge(m, src) +} +func (m *FswapInit) XXX_Size() int { + return m.Size() +} +func (m *FswapInit) XXX_DiscardUnknown() { + xxx_messageInfo_FswapInit.DiscardUnknown(m) +} + +var xxx_messageInfo_FswapInit proto.InternalMessageInfo + +func (m *FswapInit) GetFromDenom() string { + if m != nil { + return m.FromDenom + } + return "" +} + +func (m *FswapInit) GetToDenom() string { + if m != nil { + return m.ToDenom + } + return "" +} + +// From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 +type FswapInitProposal struct { + Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"` + Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` + FswapInit FswapInit `protobuf:"bytes,3,opt,name=fswap_init,json=fswapInit,proto3" json:"fswap_init"` +} + +func (m *FswapInitProposal) Reset() { *m = FswapInitProposal{} } +func (*FswapInitProposal) ProtoMessage() {} +func (*FswapInitProposal) Descriptor() ([]byte, []int) { + return fileDescriptor_42ca60eaf37a2b67, []int{1} +} +func (m *FswapInitProposal) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *FswapInitProposal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_FswapInitProposal.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 *FswapInitProposal) XXX_Merge(src proto.Message) { + xxx_messageInfo_FswapInitProposal.Merge(m, src) +} +func (m *FswapInitProposal) XXX_Size() int { + return m.Size() +} +func (m *FswapInitProposal) XXX_DiscardUnknown() { + xxx_messageInfo_FswapInitProposal.DiscardUnknown(m) +} + +var xxx_messageInfo_FswapInitProposal proto.InternalMessageInfo + +func (m *FswapInitProposal) GetTitle() string { + if m != nil { + return m.Title + } + return "" +} + +func (m *FswapInitProposal) GetDescription() string { + if m != nil { + return m.Description + } + return "" +} + +func (m *FswapInitProposal) GetFswapInit() FswapInit { + if m != nil { + return m.FswapInit + } + return FswapInit{} +} + type Swapped struct { OldCoinAmount github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,1,opt,name=old_coin_amount,json=oldCoinAmount,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"old_coin_amount"` NewCoinAmount github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,2,opt,name=new_coin_amount,json=newCoinAmount,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"new_coin_amount"` @@ -32,7 +145,7 @@ type Swapped struct { func (m *Swapped) Reset() { *m = Swapped{} } func (*Swapped) ProtoMessage() {} func (*Swapped) Descriptor() ([]byte, []int) { - return fileDescriptor_42ca60eaf37a2b67, []int{0} + return fileDescriptor_42ca60eaf37a2b67, []int{2} } func (m *Swapped) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -62,28 +175,207 @@ func (m *Swapped) XXX_DiscardUnknown() { var xxx_messageInfo_Swapped proto.InternalMessageInfo func init() { + proto.RegisterType((*FswapInit)(nil), "lbm.fswap.v1.FswapInit") + proto.RegisterType((*FswapInitProposal)(nil), "lbm.fswap.v1.FswapInitProposal") proto.RegisterType((*Swapped)(nil), "lbm.fswap.v1.Swapped") } func init() { proto.RegisterFile("lbm/fswap/v1/fswap.proto", fileDescriptor_42ca60eaf37a2b67) } var fileDescriptor_42ca60eaf37a2b67 = []byte{ - // 230 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xc8, 0x49, 0xca, 0xd5, - 0x4f, 0x2b, 0x2e, 0x4f, 0x2c, 0xd0, 0x2f, 0x33, 0x84, 0x30, 0xf4, 0x0a, 0x8a, 0xf2, 0x4b, 0xf2, - 0x85, 0x78, 0x72, 0x92, 0x72, 0xf5, 0x20, 0x02, 0x65, 0x86, 0x52, 0x22, 0xe9, 0xf9, 0xe9, 0xf9, - 0x60, 0x09, 0x7d, 0x10, 0x0b, 0xa2, 0x46, 0xe9, 0x38, 0x23, 0x17, 0x7b, 0x70, 0x79, 0x62, 0x41, - 0x41, 0x6a, 0x8a, 0x50, 0x14, 0x17, 0x7f, 0x7e, 0x4e, 0x4a, 0x7c, 0x72, 0x7e, 0x66, 0x5e, 0x7c, - 0x62, 0x6e, 0x7e, 0x69, 0x5e, 0x89, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0xa7, 0x93, 0xd1, 0x89, 0x7b, - 0xf2, 0x0c, 0xb7, 0xee, 0xc9, 0x6b, 0xa5, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, - 0xea, 0xbb, 0x65, 0xe6, 0x15, 0x27, 0x67, 0x64, 0x26, 0xea, 0xa7, 0x41, 0x19, 0xba, 0xc5, 0x29, - 0xd9, 0xfa, 0x25, 0x95, 0x05, 0xa9, 0xc5, 0x7a, 0x9e, 0x79, 0x25, 0x41, 0xbc, 0xf9, 0x39, 0x29, - 0xce, 0xf9, 0x99, 0x79, 0x8e, 0x60, 0x83, 0x40, 0x66, 0xe7, 0xa5, 0x96, 0xa3, 0x98, 0xcd, 0x44, - 0xbe, 0xd9, 0x79, 0xa9, 0xe5, 0x08, 0xb3, 0xad, 0x58, 0x66, 0x2c, 0x90, 0x67, 0x70, 0xf2, 0x38, - 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23, 0x39, 0xc6, 0x07, 0x8f, 0xe4, 0x18, 0x27, 0x3c, 0x96, 0x63, - 0xb8, 0xf0, 0x58, 0x8e, 0xe1, 0xc6, 0x63, 0x39, 0x86, 0x28, 0x3d, 0x82, 0x46, 0x57, 0x40, 0x03, - 0x10, 0x6c, 0x45, 0x12, 0x1b, 0x38, 0x68, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x82, 0xb7, - 0x84, 0x0e, 0x5a, 0x01, 0x00, 0x00, + // 397 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x92, 0x3f, 0x6b, 0xdb, 0x40, + 0x18, 0xc6, 0x75, 0xae, 0x5b, 0x5b, 0xe7, 0x96, 0xd2, 0xc3, 0x50, 0x51, 0x8a, 0x64, 0x3c, 0x95, + 0x42, 0x25, 0xec, 0x6e, 0xa6, 0x4b, 0xdd, 0x62, 0x6a, 0x28, 0xa5, 0x28, 0x99, 0xbc, 0x18, 0xfd, + 0x39, 0xd9, 0x47, 0xa4, 0x7b, 0x85, 0x74, 0xb6, 0x92, 0x6f, 0x11, 0xc8, 0x92, 0xd1, 0xdf, 0x26, + 0x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 0x69, 0xe2, 0xd7, 0x82, 0x09, 0x9f, 0x16, 0xc9, + 0xf3, 0x03, 0x69, 0xe1, 0x86, 0x4b, 0x63, 0x27, 0x62, 0xa1, 0x60, 0xc0, 0x8b, 0xe4, 0x65, 0x89, + 0xfc, 0xc0, 0x38, 0x6b, 0x79, 0xcc, 0x78, 0x11, 0xbe, 0xd1, 0xfd, 0xa8, 0x97, 0xcb, 0xd7, 0xf7, + 0x8f, 0xf5, 0xab, 0x69, 0x04, 0x53, 0xf6, 0x76, 0x42, 0x69, 0xab, 0x1b, 0x84, 0x6b, 0xe9, 0xb2, + 0x21, 0x75, 0xc9, 0x08, 0xbf, 0x07, 0xdf, 0x1d, 0x3b, 0xc0, 0xf8, 0xd8, 0xca, 0x3a, 0xc9, 0xb7, + 0x3a, 0xaa, 0x82, 0x77, 0xe0, 0xbb, 0xbf, 0x80, 0xf1, 0xbc, 0xdc, 0x94, 0xcd, 0x69, 0xf2, 0x8c, + 0x5d, 0x39, 0x9e, 0xcd, 0x69, 0x72, 0x60, 0xf7, 0xaa, 0x69, 0x9a, 0xfe, 0x9f, 0xe5, 0x46, 0x45, + 0xab, 0x8d, 0x8a, 0xee, 0x37, 0x2a, 0xba, 0xdc, 0xaa, 0xd2, 0x6a, 0xab, 0x4a, 0xb7, 0x5b, 0x55, + 0x1a, 0xe9, 0x2f, 0xa2, 0xcf, 0x8b, 0xcf, 0x9c, 0x3d, 0x61, 0xbf, 0xc9, 0xbe, 0xe9, 0xf7, 0xa7, + 0x00, 0x00, 0x00, 0xff, 0xff, 0x25, 0x80, 0x1e, 0xd7, 0xe6, 0x02, 0x00, 0x00, +} + +func (this *FswapInit) Equal(that interface{}) bool { + if that == nil { + return this == nil + } + + that1, ok := that.(*FswapInit) + if !ok { + that2, ok := that.(FswapInit) + if ok { + that1 = &that2 + } else { + return false + } + } + if that1 == nil { + return this == nil + } else if this == nil { + return false + } + if this.FromDenom != that1.FromDenom { + return false + } + if this.ToDenom != that1.ToDenom { + return false + } + if !this.AmountLimit.Equal(that1.AmountLimit) { + return false + } + if !this.SwapRate.Equal(that1.SwapRate) { + return false + } + return true +} +func (this *FswapInitProposal) Equal(that interface{}) bool { + if that == nil { + return this == nil + } + + that1, ok := that.(*FswapInitProposal) + if !ok { + that2, ok := that.(FswapInitProposal) + if ok { + that1 = &that2 + } else { + return false + } + } + if that1 == nil { + return this == nil + } else if this == nil { + return false + } + if this.Title != that1.Title { + return false + } + if this.Description != that1.Description { + return false + } + if !this.FswapInit.Equal(&that1.FswapInit) { + return false + } + return true +} +func (m *FswapInit) 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 *FswapInit) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *FswapInit) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size := m.SwapRate.Size() + i -= size + if _, err := m.SwapRate.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintFswap(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x22 + { + size := m.AmountLimit.Size() + i -= size + if _, err := m.AmountLimit.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintFswap(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + if len(m.ToDenom) > 0 { + i -= len(m.ToDenom) + copy(dAtA[i:], m.ToDenom) + i = encodeVarintFswap(dAtA, i, uint64(len(m.ToDenom))) + i-- + dAtA[i] = 0x12 + } + if len(m.FromDenom) > 0 { + i -= len(m.FromDenom) + copy(dAtA[i:], m.FromDenom) + i = encodeVarintFswap(dAtA, i, uint64(len(m.FromDenom))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *FswapInitProposal) 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 *FswapInitProposal) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *FswapInitProposal) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size, err := m.FswapInit.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintFswap(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + if len(m.Description) > 0 { + i -= len(m.Description) + copy(dAtA[i:], m.Description) + i = encodeVarintFswap(dAtA, i, uint64(len(m.Description))) + i-- + dAtA[i] = 0x12 + } + if len(m.Title) > 0 { + i -= len(m.Title) + copy(dAtA[i:], m.Title) + i = encodeVarintFswap(dAtA, i, uint64(len(m.Title))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil } func (m *Swapped) Marshal() (dAtA []byte, err error) { @@ -140,6 +432,46 @@ func encodeVarintFswap(dAtA []byte, offset int, v uint64) int { dAtA[offset] = uint8(v) return base } +func (m *FswapInit) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.FromDenom) + if l > 0 { + n += 1 + l + sovFswap(uint64(l)) + } + l = len(m.ToDenom) + if l > 0 { + n += 1 + l + sovFswap(uint64(l)) + } + l = m.AmountLimit.Size() + n += 1 + l + sovFswap(uint64(l)) + l = m.SwapRate.Size() + n += 1 + l + sovFswap(uint64(l)) + return n +} + +func (m *FswapInitProposal) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Title) + if l > 0 { + n += 1 + l + sovFswap(uint64(l)) + } + l = len(m.Description) + if l > 0 { + n += 1 + l + sovFswap(uint64(l)) + } + l = m.FswapInit.Size() + n += 1 + l + sovFswap(uint64(l)) + return n +} + func (m *Swapped) Size() (n int) { if m == nil { return 0 @@ -159,6 +491,335 @@ func sovFswap(x uint64) (n int) { func sozFswap(x uint64) (n int) { return sovFswap(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } +func (m *FswapInit) 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 ErrIntOverflowFswap + } + 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: FswapInit: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: FswapInit: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field FromDenom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFswap + } + 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 ErrInvalidLengthFswap + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthFswap + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.FromDenom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ToDenom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFswap + } + 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 ErrInvalidLengthFswap + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthFswap + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ToDenom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field AmountLimit", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFswap + } + 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 ErrInvalidLengthFswap + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthFswap + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.AmountLimit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field SwapRate", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFswap + } + 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 ErrInvalidLengthFswap + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthFswap + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.SwapRate.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipFswap(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthFswap + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *FswapInitProposal) 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 ErrIntOverflowFswap + } + 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: FswapInitProposal: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: FswapInitProposal: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Title", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFswap + } + 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 ErrInvalidLengthFswap + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthFswap + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Title = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Description", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFswap + } + 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 ErrInvalidLengthFswap + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthFswap + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Description = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field FswapInit", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFswap + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthFswap + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthFswap + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.FswapInit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipFswap(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthFswap + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func (m *Swapped) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 diff --git a/x/fswap/types/genesis.pb.go b/x/fswap/types/genesis.pb.go index 5b76f03a14..bc4a73a69f 100644 --- a/x/fswap/types/genesis.pb.go +++ b/x/fswap/types/genesis.pb.go @@ -25,8 +25,8 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // GenesisState defines the fswap module's genesis state. type GenesisState struct { - Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params"` - Swapped Swapped `protobuf:"bytes,2,opt,name=swapped,proto3" json:"swapped"` + FswapInit FswapInit `protobuf:"bytes,1,opt,name=fswap_init,json=fswapInit,proto3" json:"fswap_init"` + Swapped Swapped `protobuf:"bytes,2,opt,name=swapped,proto3" json:"swapped"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -62,11 +62,11 @@ func (m *GenesisState) XXX_DiscardUnknown() { var xxx_messageInfo_GenesisState proto.InternalMessageInfo -func (m *GenesisState) GetParams() Params { +func (m *GenesisState) GetFswapInit() FswapInit { if m != nil { - return m.Params + return m.FswapInit } - return Params{} + return FswapInit{} } func (m *GenesisState) GetSwapped() Swapped { @@ -83,22 +83,22 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/genesis.proto", fileDescriptor_94e309cb1db27661) } var fileDescriptor_94e309cb1db27661 = []byte{ - // 230 bytes of a gzipped FileDescriptorProto + // 231 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xca, 0x49, 0xca, 0xd5, 0x4f, 0x2b, 0x2e, 0x4f, 0x2c, 0xd0, 0x2f, 0x33, 0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0xc9, 0x49, 0xca, 0xd5, 0x03, 0xcb, 0xe9, 0x95, 0x19, 0x4a, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0x25, 0xf4, 0x41, 0x2c, 0x88, 0x1a, 0x29, 0x09, - 0x14, 0xfd, 0x10, 0xc5, 0x10, 0x19, 0x49, 0x14, 0x99, 0x82, 0xc4, 0xa2, 0xc4, 0x5c, 0xa8, 0xc1, - 0x4a, 0x95, 0x5c, 0x3c, 0xee, 0x10, 0x9b, 0x82, 0x4b, 0x12, 0x4b, 0x52, 0x85, 0x8c, 0xb8, 0xd8, - 0x20, 0xf2, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0xdc, 0x46, 0x22, 0x7a, 0xc8, 0x36, 0xeb, 0x05, 0x80, - 0xe5, 0x9c, 0x58, 0x4e, 0xdc, 0x93, 0x67, 0x08, 0x82, 0xaa, 0x14, 0x32, 0xe5, 0x62, 0x07, 0xc9, - 0x17, 0xa4, 0xa6, 0x48, 0x30, 0x81, 0x35, 0x89, 0xa2, 0x6a, 0x0a, 0x86, 0x48, 0x42, 0x75, 0xc1, - 0xd4, 0x3a, 0x79, 0x9c, 0x78, 0x24, 0xc7, 0x78, 0xe1, 0x91, 0x1c, 0xe3, 0x83, 0x47, 0x72, 0x8c, - 0x13, 0x1e, 0xcb, 0x31, 0x5c, 0x78, 0x2c, 0xc7, 0x70, 0xe3, 0xb1, 0x1c, 0x43, 0x94, 0x5e, 0x7a, - 0x66, 0x49, 0x46, 0x69, 0x92, 0x5e, 0x72, 0x7e, 0xae, 0xbe, 0x5b, 0x66, 0x5e, 0x71, 0x72, 0x46, - 0x66, 0xa2, 0x7e, 0x1a, 0x94, 0xa1, 0x5b, 0x9c, 0x92, 0xad, 0x5f, 0x01, 0xf5, 0x4e, 0x49, 0x65, - 0x41, 0x6a, 0x71, 0x12, 0x1b, 0xd8, 0x2f, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xcb, 0xd0, - 0xcc, 0x0f, 0x42, 0x01, 0x00, 0x00, + 0x14, 0xfd, 0x10, 0xc5, 0x60, 0x19, 0xa5, 0x66, 0x46, 0x2e, 0x1e, 0x77, 0x88, 0x79, 0xc1, 0x25, + 0x89, 0x25, 0xa9, 0x42, 0x36, 0x5c, 0x5c, 0x60, 0xf9, 0xf8, 0xcc, 0xbc, 0xcc, 0x12, 0x09, 0x46, + 0x05, 0x46, 0x0d, 0x6e, 0x23, 0x71, 0x3d, 0x64, 0x3b, 0xf4, 0xdc, 0x40, 0x0c, 0xcf, 0xbc, 0xcc, + 0x12, 0x27, 0x96, 0x13, 0xf7, 0xe4, 0x19, 0x82, 0x38, 0xd3, 0x60, 0x02, 0x42, 0xa6, 0x5c, 0xec, + 0x20, 0x76, 0x41, 0x6a, 0x8a, 0x04, 0x13, 0x58, 0xab, 0x28, 0xaa, 0xd6, 0x60, 0x88, 0x24, 0x54, + 0x23, 0x4c, 0xad, 0x93, 0xc7, 0x89, 0x47, 0x72, 0x8c, 0x17, 0x1e, 0xc9, 0x31, 0x3e, 0x78, 0x24, + 0xc7, 0x38, 0xe1, 0xb1, 0x1c, 0xc3, 0x85, 0xc7, 0x72, 0x0c, 0x37, 0x1e, 0xcb, 0x31, 0x44, 0xe9, + 0xa5, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0xbb, 0x65, 0xe6, 0x15, 0x27, + 0x67, 0x64, 0x26, 0xea, 0xa7, 0x41, 0x19, 0xba, 0xc5, 0x29, 0xd9, 0xfa, 0x15, 0x50, 0x8f, 0x95, + 0x54, 0x16, 0xa4, 0x16, 0x27, 0xb1, 0x81, 0xbd, 0x65, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x41, + 0x8c, 0x3c, 0xe3, 0x32, 0x01, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -132,7 +132,7 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x12 { - size, err := m.Params.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.FswapInit.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -161,7 +161,7 @@ func (m *GenesisState) Size() (n int) { } var l int _ = l - l = m.Params.Size() + l = m.FswapInit.Size() n += 1 + l + sovGenesis(uint64(l)) l = m.Swapped.Size() n += 1 + l + sovGenesis(uint64(l)) @@ -205,7 +205,7 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Params", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field FswapInit", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -232,7 +232,7 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.Params.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.FswapInit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/fswap/types/params.pb.go b/x/fswap/types/params.pb.go deleted file mode 100644 index 8e9887228b..0000000000 --- a/x/fswap/types/params.pb.go +++ /dev/null @@ -1,317 +0,0 @@ -// Code generated by protoc-gen-gogo. DO NOT EDIT. -// source: lbm/fswap/v1/params.proto - -package types - -import ( - fmt "fmt" - github_com_Finschia_finschia_sdk_types "github.com/Finschia/finschia-sdk/types" - _ "github.com/gogo/protobuf/gogoproto" - proto "github.com/gogo/protobuf/proto" - 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 - -// Params defines the parameters for the module. -type Params struct { - SwappableNewCoinAmount github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,1,opt,name=swappable_new_coin_amount,json=swappableNewCoinAmount,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"swappable_new_coin_amount"` -} - -func (m *Params) Reset() { *m = Params{} } -func (*Params) ProtoMessage() {} -func (*Params) Descriptor() ([]byte, []int) { - return fileDescriptor_15e620b81032c44d, []int{0} -} -func (m *Params) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *Params) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_Params.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 *Params) XXX_Merge(src proto.Message) { - xxx_messageInfo_Params.Merge(m, src) -} -func (m *Params) XXX_Size() int { - return m.Size() -} -func (m *Params) XXX_DiscardUnknown() { - xxx_messageInfo_Params.DiscardUnknown(m) -} - -var xxx_messageInfo_Params proto.InternalMessageInfo - -func init() { - proto.RegisterType((*Params)(nil), "lbm.fswap.v1.Params") -} - -func init() { proto.RegisterFile("lbm/fswap/v1/params.proto", fileDescriptor_15e620b81032c44d) } - -var fileDescriptor_15e620b81032c44d = []byte{ - // 225 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xcc, 0x49, 0xca, 0xd5, - 0x4f, 0x2b, 0x2e, 0x4f, 0x2c, 0xd0, 0x2f, 0x33, 0xd4, 0x2f, 0x48, 0x2c, 0x4a, 0xcc, 0x2d, 0xd6, - 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0xc9, 0x49, 0xca, 0xd5, 0x03, 0x4b, 0xe9, 0x95, 0x19, - 0x4a, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0x25, 0xf4, 0x41, 0x2c, 0x88, 0x1a, 0xa5, 0x5a, 0x2e, - 0xb6, 0x00, 0xb0, 0x1e, 0xa1, 0x5c, 0x2e, 0x49, 0x90, 0xd2, 0x82, 0xc4, 0xa4, 0x9c, 0xd4, 0xf8, - 0xbc, 0xd4, 0xf2, 0xf8, 0xe4, 0xfc, 0xcc, 0xbc, 0xf8, 0xc4, 0xdc, 0xfc, 0xd2, 0xbc, 0x12, 0x09, - 0x46, 0x05, 0x46, 0x0d, 0x4e, 0x27, 0xa3, 0x13, 0xf7, 0xe4, 0x19, 0x6e, 0xdd, 0x93, 0xd7, 0x4a, - 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x77, 0xcb, 0xcc, 0x2b, 0x4e, 0xce, - 0xc8, 0x4c, 0xd4, 0x4f, 0x83, 0x32, 0x74, 0x8b, 0x53, 0xb2, 0xf5, 0x4b, 0x2a, 0x0b, 0x52, 0x8b, - 0xf5, 0x3c, 0xf3, 0x4a, 0x82, 0xc4, 0xe0, 0x86, 0xfa, 0xa5, 0x96, 0x3b, 0xe7, 0x67, 0xe6, 0x39, - 0x82, 0x4d, 0xb4, 0x62, 0x99, 0xb1, 0x40, 0x9e, 0xc1, 0xc9, 0xe3, 0xc4, 0x23, 0x39, 0xc6, 0x0b, - 0x8f, 0xe4, 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x9c, 0xf0, 0x58, 0x8e, 0xe1, 0xc2, 0x63, 0x39, 0x86, - 0x1b, 0x8f, 0xe5, 0x18, 0xa2, 0xf4, 0x08, 0xda, 0x51, 0x01, 0xf5, 0x36, 0xd8, 0xae, 0x24, 0x36, - 0xb0, 0x7f, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x53, 0xb6, 0x13, 0xee, 0x10, 0x01, 0x00, - 0x00, -} - -func (m *Params) 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 *Params) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - { - size := m.SwappableNewCoinAmount.Size() - i -= size - if _, err := m.SwappableNewCoinAmount.MarshalTo(dAtA[i:]); err != nil { - return 0, err - } - i = encodeVarintParams(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0xa - return len(dAtA) - i, nil -} - -func encodeVarintParams(dAtA []byte, offset int, v uint64) int { - offset -= sovParams(v) - base := offset - for v >= 1<<7 { - dAtA[offset] = uint8(v&0x7f | 0x80) - v >>= 7 - offset++ - } - dAtA[offset] = uint8(v) - return base -} -func (m *Params) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = m.SwappableNewCoinAmount.Size() - n += 1 + l + sovParams(uint64(l)) - return n -} - -func sovParams(x uint64) (n int) { - return (math_bits.Len64(x|1) + 6) / 7 -} -func sozParams(x uint64) (n int) { - return sovParams(uint64((x << 1) ^ uint64((int64(x) >> 63)))) -} -func (m *Params) 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 ErrIntOverflowParams - } - 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: Params: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: Params: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field SwappableNewCoinAmount", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowParams - } - 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 ErrInvalidLengthParams - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthParams - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if err := m.SwappableNewCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipParams(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthParams - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func skipParams(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, ErrIntOverflowParams - } - 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, ErrIntOverflowParams - } - 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, ErrIntOverflowParams - } - 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, ErrInvalidLengthParams - } - iNdEx += length - case 3: - depth++ - case 4: - if depth == 0 { - return 0, ErrUnexpectedEndOfGroupParams - } - depth-- - case 5: - iNdEx += 4 - default: - return 0, fmt.Errorf("proto: illegal wireType %d", wireType) - } - if iNdEx < 0 { - return 0, ErrInvalidLengthParams - } - if depth == 0 { - return iNdEx, nil - } - } - return 0, io.ErrUnexpectedEOF -} - -var ( - ErrInvalidLengthParams = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowParams = fmt.Errorf("proto: integer overflow") - ErrUnexpectedEndOfGroupParams = fmt.Errorf("proto: unexpected end of group") -) From 93a97a6d2d2dc182bd8d601d781ffeabad1f94e4 Mon Sep 17 00:00:00 2001 From: 170210 Date: Fri, 26 Apr 2024 17:34:34 +0900 Subject: [PATCH 02/47] refactor: remove config¶ms and modify genesis Signed-off-by: 170210 --- x/ERRORS.md | 2 +- x/fswap/keeper/fswap.go | 23 ++++++++++++ x/fswap/keeper/genesis.go | 6 +-- x/fswap/keeper/genesis_test.go | 2 +- x/fswap/keeper/params.go | 35 ----------------- x/fswap/types/config.go | 24 ------------ x/fswap/types/errors.go | 6 +-- x/fswap/types/fswapInit.go | 38 +++++++++++++++++++ x/fswap/types/genesis.go | 16 +++++--- x/fswap/types/genesis_test.go | 68 +--------------------------------- x/fswap/types/keys.go | 4 +- x/fswap/types/params.go | 35 ----------------- 12 files changed, 82 insertions(+), 177 deletions(-) delete mode 100644 x/fswap/keeper/params.go delete mode 100644 x/fswap/types/config.go create mode 100644 x/fswap/types/fswapInit.go delete mode 100644 x/fswap/types/params.go diff --git a/x/ERRORS.md b/x/ERRORS.md index 8ed09149f9..85d489f849 100644 --- a/x/ERRORS.md +++ b/x/ERRORS.md @@ -177,7 +177,7 @@ |Error Name|Codespace|Code|Description| |:-|:-|:-|:-| -|ErrParamsNotFound|fswap|1100|params does not exist| +|ErrFswapInitNotFound|fswap|1100|fswap init does not exist| |ErrSwappedNotFound|fswap|1101|swapped does not exist| |ErrExceedSwappable|fswap|1102|exceed swappable coin amount| diff --git a/x/fswap/keeper/fswap.go b/x/fswap/keeper/fswap.go index 6100dfa111..f673b73c41 100644 --- a/x/fswap/keeper/fswap.go +++ b/x/fswap/keeper/fswap.go @@ -28,3 +28,26 @@ func (k Keeper) SetSwapped(ctx sdk.Context, swapped types.Swapped) error { store.Set([]byte{types.SwappedKey}, bz) return nil } + +// GetFswapInit get all parameters as types.FswapInit +func (k Keeper) GetFswapInit(ctx sdk.Context) types.FswapInit { + store := ctx.KVStore(k.storeKey) + bz := store.Get([]byte{types.FswapInitKey}) + var fswapInit types.FswapInit + if bz == nil { + panic(types.ErrFswapInitNotFound) + } + k.cdc.MustUnmarshal(bz, &fswapInit) + return fswapInit +} + +// SetfswapInit set the fswapInit +func (k Keeper) SetFswapInit(ctx sdk.Context, fswapInit types.FswapInit) error { + store := ctx.KVStore(k.storeKey) + bz, err := k.cdc.Marshal(&fswapInit) + if err != nil { + return err + } + store.Set([]byte{types.FswapInitKey}, bz) + return nil +} diff --git a/x/fswap/keeper/genesis.go b/x/fswap/keeper/genesis.go index 4cb0edd5e6..82a346a671 100644 --- a/x/fswap/keeper/genesis.go +++ b/x/fswap/keeper/genesis.go @@ -8,7 +8,7 @@ import ( // InitGenesis initializes the module's state from a provided genesis // state. func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { - if err := k.SetParams(ctx, genState.Params); err != nil { + if err := k.SetFswapInit(ctx, genState.FswapInit); err != nil { panic(err) } if err := k.SetSwapped(ctx, genState.Swapped); err != nil { @@ -19,7 +19,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { // ExportGenesis returns the capability module's exported genesis. func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { return &types.GenesisState{ - Params: k.GetParams(ctx), - Swapped: k.GetSwapped(ctx), + FswapInit: k.GetFswapInit(ctx), + Swapped: k.GetSwapped(ctx), } } diff --git a/x/fswap/keeper/genesis_test.go b/x/fswap/keeper/genesis_test.go index ae0e07e275..26f6779af6 100644 --- a/x/fswap/keeper/genesis_test.go +++ b/x/fswap/keeper/genesis_test.go @@ -8,6 +8,6 @@ func (s *KeeperTestSuite) TestInitAndExportGenesis() { s.keeper.InitGenesis(s.sdkCtx, *types.DefaultGenesis()) got := s.keeper.ExportGenesis(s.sdkCtx) s.Require().NotNil(got) - s.Require().Equal(types.DefaultParams(), got.Params) + s.Require().Equal(types.DefaultFswapInit(), got.FswapInit) s.Require().Equal(types.DefaultSwapped(), got.Swapped) } diff --git a/x/fswap/keeper/params.go b/x/fswap/keeper/params.go deleted file mode 100644 index 5578a6d760..0000000000 --- a/x/fswap/keeper/params.go +++ /dev/null @@ -1,35 +0,0 @@ -package keeper - -import ( - sdk "github.com/Finschia/finschia-sdk/types" - sdkerrors "github.com/Finschia/finschia-sdk/types/errors" - "github.com/Finschia/finschia-sdk/x/fswap/types" -) - -// GetParams get all parameters as types.Params -func (k Keeper) GetParams(ctx sdk.Context) types.Params { - store := ctx.KVStore(k.storeKey) - bz := store.Get([]byte{types.ParamsKey}) - var params types.Params - if bz == nil { - panic(sdkerrors.ErrNotFound) - } - k.cdc.MustUnmarshal(bz, ¶ms) - return params -} - -// SetParams set the params -func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { - store := ctx.KVStore(k.storeKey) - bz, err := k.cdc.Marshal(¶ms) - if err != nil { - return err - } - store.Set([]byte{types.ParamsKey}, bz) - return nil -} - -func (k Keeper) SwappableNewCoinAmount(ctx sdk.Context) sdk.Int { - params := k.GetParams(ctx) - return params.SwappableNewCoinAmount -} diff --git a/x/fswap/types/config.go b/x/fswap/types/config.go deleted file mode 100644 index d2e57a05aa..0000000000 --- a/x/fswap/types/config.go +++ /dev/null @@ -1,24 +0,0 @@ -package types - -import ( - sdk "github.com/Finschia/finschia-sdk/types" -) - -// Config is a config struct used for intialising the fswap module to avoid using globals. -type Config struct { - // OldCoinDenom defines the old coin denom. - OldCoinDenom string - // NewCoinDenom defines the new coin denom. - NewCoinDenom string - // SwapRate defines the swap rate. - SwapRate sdk.Int -} - -// DefaultConfig returns the default config for fswap. -func DefaultConfig() Config { - return Config{ - OldCoinDenom: "cony", - NewCoinDenom: "PDT", - SwapRate: sdk.NewInt(148079656000000), - } -} diff --git a/x/fswap/types/errors.go b/x/fswap/types/errors.go index 3a18f66f20..0d718293c8 100644 --- a/x/fswap/types/errors.go +++ b/x/fswap/types/errors.go @@ -8,7 +8,7 @@ import ( // x/fswap module sentinel errors var ( - ErrParamsNotFound = sdkerrors.Register(ModuleName, 1100, "params does not exist") - ErrSwappedNotFound = sdkerrors.Register(ModuleName, 1101, "swapped does not exist") - ErrExceedSwappable = sdkerrors.Register(ModuleName, 1102, "exceed swappable coin amount") + ErrFswapInitNotFound = sdkerrors.Register(ModuleName, 1100, "fswap init does not exist") + ErrSwappedNotFound = sdkerrors.Register(ModuleName, 1101, "swapped does not exist") + ErrExceedSwappable = sdkerrors.Register(ModuleName, 1102, "exceed swappable coin amount") ) diff --git a/x/fswap/types/fswapInit.go b/x/fswap/types/fswapInit.go new file mode 100644 index 0000000000..7bf9438249 --- /dev/null +++ b/x/fswap/types/fswapInit.go @@ -0,0 +1,38 @@ +package types + +import ( + sdk "github.com/Finschia/finschia-sdk/types" + "gopkg.in/yaml.v2" +) + +// Validate validates the set of swapped +func (fi FswapInit) ValidateBasic() error { + // todo vadalidate + return nil +} + +// String implements the Stringer interface. +func (fi FswapInit) String() string { + out, _ := yaml.Marshal(fi) + return string(out) +} + +// NewFswapInit creates a new FswapInit instance +func NewFswapInit( + fromDenom string, + toDenom string, + amountLimit sdk.Int, + swapRate sdk.Int, +) FswapInit { + return FswapInit{ + FromDenom: fromDenom, + ToDenom: toDenom, + AmountLimit: amountLimit, + SwapRate: swapRate, + } +} + +// DefaultFswapInit returns an initial FswapInit object +func DefaultFswapInit() FswapInit { + return NewFswapInit("", "", sdk.ZeroInt(), sdk.ZeroInt()) +} diff --git a/x/fswap/types/genesis.go b/x/fswap/types/genesis.go index 816ef241d0..a94daaf040 100644 --- a/x/fswap/types/genesis.go +++ b/x/fswap/types/genesis.go @@ -1,28 +1,32 @@ package types // NewGenesis creates a new genesis state -func NewGenesisState(params Params, swapped Swapped) *GenesisState { +func NewGenesisState(fswapInit FswapInit, swapped Swapped) *GenesisState { return &GenesisState{ - Params: params, - Swapped: swapped, + FswapInit: fswapInit, + Swapped: swapped, } } // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { - return NewGenesisState(DefaultParams(), DefaultSwapped()) + return &GenesisState{DefaultFswapInit(), DefaultSwapped()} } // Validate performs basic genesis state validation returning an error upon any // failure. +// need confirm: should we validate? Since it may nil func (gs GenesisState) Validate() error { - if err := gs.Params.Validate(); err != nil { + if gs == *DefaultGenesis() { + return nil + } + if err := gs.FswapInit.ValidateBasic(); err != nil { return err } if err := gs.Swapped.Validate(); err != nil { return err } - if gs.Params.SwappableNewCoinAmount.LT(gs.Swapped.NewCoinAmount) { + if gs.FswapInit.AmountLimit.LT(gs.Swapped.NewCoinAmount) { return ErrExceedSwappable } return nil diff --git a/x/fswap/types/genesis_test.go b/x/fswap/types/genesis_test.go index 15394d1ac0..29308ea519 100644 --- a/x/fswap/types/genesis_test.go +++ b/x/fswap/types/genesis_test.go @@ -5,10 +5,10 @@ import ( "github.com/stretchr/testify/require" - sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/fswap/types" ) +// todo: add tests func TestGenesisStateValidate(t *testing.T) { for _, tc := range []struct { desc string @@ -20,72 +20,6 @@ func TestGenesisStateValidate(t *testing.T) { genState: types.DefaultGenesis(), valid: true, }, - { - desc: "empty genesisState", - genState: &types.GenesisState{}, - valid: false, - }, - { - desc: "empty params", - genState: &types.GenesisState{ - Swapped: types.DefaultSwapped(), - }, - valid: false, - }, - { - desc: "empty swapped", - genState: &types.GenesisState{ - Params: types.DefaultParams(), - }, - valid: false, - }, - { - desc: "empty swappableNewCoinAmount in params", - genState: &types.GenesisState{ - Params: types.Params{}, - Swapped: types.DefaultSwapped(), - }, - valid: false, - }, - { - desc: "empty oldCoin in Swapped", - genState: &types.GenesisState{ - Params: types.DefaultParams(), - Swapped: types.Swapped{ - NewCoinAmount: sdk.ZeroInt(), - }, - }, - valid: false, - }, - { - desc: "empty newCoin in Swapped", - genState: &types.GenesisState{ - Params: types.DefaultParams(), - Swapped: types.Swapped{ - OldCoinAmount: sdk.ZeroInt(), - }, - }, - valid: false, - }, - { - desc: "coinAmount is negative", - genState: &types.GenesisState{ - Params: types.DefaultParams(), - Swapped: types.Swapped{ - NewCoinAmount: sdk.ZeroInt(), - OldCoinAmount: sdk.NewInt(-1), - }, - }, - valid: false, - }, - { - desc: "swappable coin exceed", - genState: &types.GenesisState{ - Params: types.NewParams(sdk.NewInt(1000)), - Swapped: types.NewSwapped(sdk.NewInt(1000), sdk.NewInt(100000)), - }, - valid: false, - }, } { t.Run(tc.desc, func(t *testing.T) { err := tc.genState.Validate() diff --git a/x/fswap/types/keys.go b/x/fswap/types/keys.go index a13045fad7..2a56414057 100644 --- a/x/fswap/types/keys.go +++ b/x/fswap/types/keys.go @@ -18,8 +18,8 @@ const ( ) var ( - ParamsKey = byte(0x00) - SwappedKey = byte(0x01) + FswapInitKey = byte(0x00) + SwappedKey = byte(0x01) ) func KeyPrefix(p string) []byte { diff --git a/x/fswap/types/params.go b/x/fswap/types/params.go deleted file mode 100644 index fbfedf3534..0000000000 --- a/x/fswap/types/params.go +++ /dev/null @@ -1,35 +0,0 @@ -package types - -import ( - "gopkg.in/yaml.v2" - - sdk "github.com/Finschia/finschia-sdk/types" -) - -// NewParams creates a new Params instance -func NewParams( - swappableNewCoinAmount sdk.Int, -) Params { - return Params{ - SwappableNewCoinAmount: swappableNewCoinAmount, - } -} - -// DefaultParams returns a default set of parameters -func DefaultParams() Params { - return NewParams(sdk.ZeroInt()) -} - -// Validate validates the set of params -func (p Params) Validate() error { - if err := validateCoinAmount(p.SwappableNewCoinAmount); err != nil { - return err - } - return nil -} - -// String implements the Stringer interface. -func (p Params) String() string { - out, _ := yaml.Marshal(p) - return string(out) -} From 5a8dd654c29331342a7fa444d03fd966728b2f3e Mon Sep 17 00:00:00 2001 From: 170210 Date: Fri, 26 Apr 2024 17:38:01 +0900 Subject: [PATCH 03/47] refactor: use gov/proposal to change fswapInit Signed-off-by: 170210 --- simapp/app.go | 12 ++-- x/fswap/client/cli/tx.go | 100 +++++++++++++++++++++++++++++ x/fswap/client/proposal_handler.go | 10 +++ x/fswap/handler.go | 19 ++++++ x/fswap/keeper/keeper.go | 18 +++++- x/fswap/proposal_handler_test.go | 31 +++++++++ x/fswap/types/codec.go | 36 +++++++++-- x/fswap/types/proposal.go | 44 +++++++++++++ 8 files changed, 257 insertions(+), 13 deletions(-) create mode 100644 x/fswap/client/proposal_handler.go create mode 100644 x/fswap/proposal_handler_test.go create mode 100644 x/fswap/types/proposal.go diff --git a/simapp/app.go b/simapp/app.go index 148cdbc50c..5cfa422693 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -73,6 +73,7 @@ import ( foundationkeeper "github.com/Finschia/finschia-sdk/x/foundation/keeper" foundationmodule "github.com/Finschia/finschia-sdk/x/foundation/module" "github.com/Finschia/finschia-sdk/x/fswap" + fswapclient "github.com/Finschia/finschia-sdk/x/fswap/client" fswapkeeper "github.com/Finschia/finschia-sdk/x/fswap/keeper" fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types" "github.com/Finschia/finschia-sdk/x/genutil" @@ -130,6 +131,7 @@ var ( upgradeclient.ProposalHandler, upgradeclient.CancelProposalHandler, foundationclient.ProposalHandler, + fswapclient.FswapInitHandler, ), params.AppModuleBasic{}, crisis.AppModuleBasic{}, @@ -324,6 +326,9 @@ func NewSimApp( app.TokenKeeper = tokenkeeper.NewKeeper(appCodec, keys[token.StoreKey], app.ClassKeeper) app.CollectionKeeper = collectionkeeper.NewKeeper(appCodec, keys[collection.StoreKey], app.ClassKeeper) + /**** Phase 1 ****/ + app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], app.AccountKeeper, app.BankKeeper) + // register the staking hooks // NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks app.StakingKeeper = *stakingKeeper.SetHooks( @@ -338,7 +343,8 @@ func NewSimApp( AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)). AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)). AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)). - AddRoute(foundation.RouterKey, foundationkeeper.NewFoundationProposalsHandler(app.FoundationKeeper)) + AddRoute(foundation.RouterKey, foundationkeeper.NewFoundationProposalsHandler(app.FoundationKeeper)). + AddRoute(fswaptypes.RouterKey, fswap.NewFswapInitHandler(app.FswapKeeper)) govKeeper := govkeeper.NewKeeper( appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper, @@ -358,10 +364,6 @@ func NewSimApp( // If evidence needs to be handled for the app, set routes in router here and seal app.EvidenceKeeper = *evidenceKeeper - /**** Phase 1 ****/ - fswapConfig := fswaptypes.DefaultConfig() - app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], app.AccountKeeper, app.BankKeeper, fswapConfig) - /**** Module Options ****/ // NOTE: we may consider parsing `appOpts` inside module constructors. For the moment diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index 9b2f915ab7..ee1c5cb9d1 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -11,6 +11,15 @@ import ( "github.com/Finschia/finschia-sdk/client/tx" sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/fswap/types" + "github.com/Finschia/finschia-sdk/x/gov/client/cli" + gov "github.com/Finschia/finschia-sdk/x/gov/types" +) + +const ( + FlagFromDenom = "from-denom" + FlagToDenom = "to-denom" + FlagAmountLimit = "amount-limit" + FlagSwapRate = "swap-rate" ) var DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds()) @@ -93,3 +102,94 @@ func CmdTxMsgSwapAll() *cobra.Command { flags.AddTxFlagsToCmd(cmd) return cmd } + +// NewCmdFswapInitProposal implements a command handler for submitting a fswap init proposal transaction. +func NewCmdFswapInitProposal() *cobra.Command { + cmd := &cobra.Command{ + Use: "fswap-init [flags]", + Args: cobra.ExactArgs(0), + Short: "todo", + Long: "todo", + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + // need confirm parse with flags or parse with args + contents, err := parseArgsToContent(cmd) + if err != nil { + return err + } + + from := clientCtx.GetFromAddress() + + depositStr, err := cmd.Flags().GetString(cli.FlagDeposit) + if err != nil { + return err + } + deposit, err := sdk.ParseCoinsNormalized(depositStr) + if err != nil { + return err + } + + msg, err := gov.NewMsgSubmitProposal(contents, deposit, from) + if err != nil { + return err + } + + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + cmd.Flags().String(cli.FlagTitle, "", "title of proposal") + cmd.Flags().String(cli.FlagDescription, "", "description of proposal") + cmd.Flags().String(cli.FlagDeposit, "", "deposit of proposal") + cmd.Flags().String(FlagFromDenom, "", "cony") + cmd.Flags().String(FlagToDenom, "", "PDT") + cmd.Flags().Int64(FlagAmountLimit, 0, "tbd") + cmd.Flags().Int64(FlagSwapRate, 0, "tbd") + + return cmd +} + +func parseArgsToContent(cmd *cobra.Command) (gov.Content, error) { + title, err := cmd.Flags().GetString(cli.FlagTitle) + if err != nil { + return nil, err + } + + description, err := cmd.Flags().GetString(cli.FlagDescription) + if err != nil { + return nil, err + } + + from_denom, err := cmd.Flags().GetString(FlagFromDenom) + if err != nil { + return nil, err + } + + to_denom, err := cmd.Flags().GetString(FlagToDenom) + if err != nil { + return nil, err + } + + amount_limit, err := cmd.Flags().GetInt64(FlagAmountLimit) + if err != nil { + return nil, err + } + + swap_rate, err := cmd.Flags().GetInt64(FlagSwapRate) + if err != nil { + return nil, err + } + + fswapInit := types.FswapInit{ + FromDenom: from_denom, + ToDenom: to_denom, + AmountLimit: sdk.NewInt(amount_limit), + SwapRate: sdk.NewInt(swap_rate), + } + + content := types.NewFswapInitProposal(title, description, fswapInit) + return content, nil +} diff --git a/x/fswap/client/proposal_handler.go b/x/fswap/client/proposal_handler.go new file mode 100644 index 0000000000..1a66debf26 --- /dev/null +++ b/x/fswap/client/proposal_handler.go @@ -0,0 +1,10 @@ +package client + +import ( + "github.com/Finschia/finschia-sdk/x/fswap/client/cli" + govclient "github.com/Finschia/finschia-sdk/x/gov/client" +) + +var ( + FswapInitHandler = govclient.NewProposalHandler(cli.NewCmdFswapInitProposal) +) diff --git a/x/fswap/handler.go b/x/fswap/handler.go index 2e3dae3f45..278dfee6a9 100644 --- a/x/fswap/handler.go +++ b/x/fswap/handler.go @@ -7,6 +7,7 @@ import ( sdkerrors "github.com/Finschia/finschia-sdk/types/errors" "github.com/Finschia/finschia-sdk/x/fswap/keeper" "github.com/Finschia/finschia-sdk/x/fswap/types" + govtypes "github.com/Finschia/finschia-sdk/x/gov/types" ) // NewHandler ... @@ -24,3 +25,21 @@ func NewHandler(k keeper.Keeper) sdk.Handler { // } } } + +// NewFswapInitHandler creates a governance handler to manage new proposal types. +// It enables FswapInit to propose an fswap init +func NewFswapInitHandler(k keeper.Keeper) govtypes.Handler { + return func(ctx sdk.Context, content govtypes.Content) error { + switch c := content.(type) { + case *types.FswapInitProposal: + return handleFswapInit(ctx, k, c) + + default: + return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized fsawp proposal content type: %T", c) + } + } +} + +func handleFswapInit(ctx sdk.Context, k keeper.Keeper, p *types.FswapInitProposal) error { + return k.FswapInit(ctx, p.FswapInit) +} diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 3036a079e2..6528bc3cd0 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -17,7 +17,6 @@ type ( storeKey storetypes.StoreKey accountKeeper types.AccountKeeper bankKeeper types.BankKeeper - config types.Config } ) @@ -26,17 +25,30 @@ func NewKeeper( storeKey storetypes.StoreKey, ak types.AccountKeeper, bk types.BankKeeper, - config types.Config, ) Keeper { return Keeper{ cdc: cdc, storeKey: storeKey, accountKeeper: ak, bankKeeper: bk, - config: config, } } func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) } + +func (k Keeper) FswapInit(ctx sdk.Context, fswapInit types.FswapInit) error { + // todo validate & check the first time (or not will reject the proposal) + // todo add test for this keeper in keeper test + if err := fswapInit.ValidateBasic(); err != nil { + return err + } + + // need confirm: Is ibcState necessary? (please ref upgrade keeper) + k.SetFswapInit(ctx, fswapInit) + // need confirm: Is Swapped use sdk.Coin or sdk.Int + swapped := types.NewSwapped(sdk.ZeroInt(), sdk.ZeroInt()) + k.SetSwapped(ctx, swapped) + return nil +} diff --git a/x/fswap/proposal_handler_test.go b/x/fswap/proposal_handler_test.go new file mode 100644 index 0000000000..6c86f9efe5 --- /dev/null +++ b/x/fswap/proposal_handler_test.go @@ -0,0 +1,31 @@ +package fswap_test + +import ( + "testing" + + "github.com/Finschia/finschia-sdk/simapp" + sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/x/fswap" + "github.com/Finschia/finschia-sdk/x/fswap/types" + "github.com/stretchr/testify/require" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" +) + +func testProposal(fswapInit types.FswapInit) *types.FswapInitProposal { + return types.NewFswapInitProposal("Test", "description", fswapInit) +} + +func TestProposalHandlerPassed(t *testing.T) { + app := simapp.Setup(false) + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + + fswapInit := types.NewFswapInit("aaa", "bbb", sdk.NewInt(1000), sdk.NewInt(100000)) + tp := testProposal(fswapInit) + hdlr := fswap.NewFswapInitHandler(app.FswapKeeper) + require.NoError(t, hdlr(ctx, tp)) + + // todo check contents +} + +// todo check failed +// func TestProposalHandlerFailed(t *testing.T) {} diff --git a/x/fswap/types/codec.go b/x/fswap/types/codec.go index 72003233c5..054c7a7cbf 100644 --- a/x/fswap/types/codec.go +++ b/x/fswap/types/codec.go @@ -2,22 +2,48 @@ package types import ( "github.com/Finschia/finschia-sdk/codec" - cdctypes "github.com/Finschia/finschia-sdk/codec/types" - // this line is used by starport scaffolding # 1 + "github.com/Finschia/finschia-sdk/codec/types" + cryptocodec "github.com/Finschia/finschia-sdk/crypto/codec" + sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/types/msgservice" + authzcodec "github.com/Finschia/finschia-sdk/x/authz/codec" + fdncodec "github.com/Finschia/finschia-sdk/x/foundation/codec" + govcodec "github.com/Finschia/finschia-sdk/x/gov/codec" + govtypes "github.com/Finschia/finschia-sdk/x/gov/types" ) func RegisterCodec(cdc *codec.LegacyAmino) { // this line is used by starport scaffolding # 2 } -func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { +func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + cdc.RegisterConcrete(&FswapInitProposal{}, "finschia-sdk/FswapInitProposal", nil) +} + +func RegisterInterfaces(registry types.InterfaceRegistry) { // this line is used by starport scaffolding # 3 msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) + registry.RegisterImplementations( + (*govtypes.Content)(nil), + &FswapInitProposal{}, + ) } var ( - Amino = codec.NewLegacyAmino() - ModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry()) + amino = codec.NewLegacyAmino() + ModuleCdc = codec.NewProtoCodec(types.NewInterfaceRegistry()) ) + +func init() { + RegisterLegacyAminoCodec(amino) + cryptocodec.RegisterCrypto(amino) + sdk.RegisterLegacyAminoCodec(amino) + + // Register all Amino interfaces and concrete types on the authz and gov Amino codec + // so that this can later be used to properly serialize MsgGrant and MsgExec + // instances. + RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) + RegisterLegacyAminoCodec(fdncodec.Amino) +} diff --git a/x/fswap/types/proposal.go b/x/fswap/types/proposal.go new file mode 100644 index 0000000000..c728d11339 --- /dev/null +++ b/x/fswap/types/proposal.go @@ -0,0 +1,44 @@ +package types + +import ( + gov "github.com/Finschia/finschia-sdk/x/gov/types" + "gopkg.in/yaml.v2" +) + +const ( + ProposalTypeFswapInit string = "FswapInit" +) + +// NewFswapInitProposal creates a new FswapInitProposal instance. +// Deprecated: this proposal is considered legacy and is deprecated in favor of +// Msg-based gov proposals. See MsgFswapInit. +func NewFswapInitProposal(title string, description string, fswapInit FswapInit) *FswapInitProposal { + return &FswapInitProposal{title, description, fswapInit} +} + +// Implements Proposal Interface +var _ gov.Content = &FswapInitProposal{} + +func init() { + gov.RegisterProposalType(ProposalTypeFswapInit) +} + +// ProposalRoute gets the proposal's router key +func (fip *FswapInitProposal) ProposalRoute() string { return RouterKey } + +// ProposalType is "FswapInit" +func (fip *FswapInitProposal) ProposalType() string { return ProposalTypeFswapInit } + +// String implements the Stringer interface. +func (fip *FswapInitProposal) String() string { + out, _ := yaml.Marshal(fip) + return string(out) +} + +// ValidateBasic validates the proposal +func (fip *FswapInitProposal) ValidateBasic() error { + if err := fip.FswapInit.ValidateBasic(); err != nil { + return err + } + return gov.ValidateAbstract(fip) +} From df7355dd6e4cd8b30bf59b0b63e2a61d21182000 Mon Sep 17 00:00:00 2001 From: 170210 Date: Fri, 26 Apr 2024 17:39:28 +0900 Subject: [PATCH 04/47] chore: fix simulation Signed-off-by: 170210 --- x/fswap/module_simulation.go | 5 +- x/fswap/simulation/genesis.go | 64 ------------------------- x/fswap/simulation/genesis_test.go | 77 ------------------------------ 3 files changed, 3 insertions(+), 143 deletions(-) diff --git a/x/fswap/module_simulation.go b/x/fswap/module_simulation.go index 4748931d47..1a4d90d6be 100644 --- a/x/fswap/module_simulation.go +++ b/x/fswap/module_simulation.go @@ -6,14 +6,15 @@ import ( sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/types/module" simtypes "github.com/Finschia/finschia-sdk/types/simulation" - "github.com/Finschia/finschia-sdk/x/fswap/simulation" + "github.com/Finschia/finschia-sdk/x/fswap/types" ) var _ module.AppModuleSimulation = AppModule{} // GenerateGenesisState creates a randomized GenState of the module. func (AppModule) GenerateGenesisState(simState *module.SimulationState) { - simulation.RandomizedGenState(simState) + fswapGenesis := types.DefaultGenesis() + simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(fswapGenesis) } // RegisterStoreDecoder registers a decoder. diff --git a/x/fswap/simulation/genesis.go b/x/fswap/simulation/genesis.go index f84ff0b243..02eddda2ba 100644 --- a/x/fswap/simulation/genesis.go +++ b/x/fswap/simulation/genesis.go @@ -1,65 +1 @@ package simulation - -import ( - "encoding/json" - "fmt" - "math/rand" - - sdk "github.com/Finschia/finschia-sdk/types" - "github.com/Finschia/finschia-sdk/types/module" - "github.com/Finschia/finschia-sdk/x/fswap/types" -) - -// Simulation parameter constants -const ( - OldCoinAmount = "old_coin_amount" - NewCoinAmount = "new_coin_amount" - SwappableNewCoinAmount = "swappable_new_coin_amount" -) - -// GenOldCoinAmount randomized oldCoinAmount -func GenOldCoinAmount(r *rand.Rand) sdk.Int { - return sdk.NewInt(int64(r.Intn(1001) + 1000)) -} - -// GenNewCoinAmount randomized oldCoinAmount -func GenNewCoinAmount(r *rand.Rand) sdk.Int { - return sdk.NewInt(int64(r.Intn(100001) + 100000)) -} - -// GenSwappableNewCoinAmount randomized swappableNewCoinAmount -func GenSwappableNewCoinAmount(r *rand.Rand) sdk.Int { - return sdk.NewInt(int64(r.Intn(100001) + 200000)) -} - -// RandomizedGenState generates a random GenesisState for fswap -func RandomizedGenState(simState *module.SimulationState) { - var oldCoinAmount sdk.Int - simState.AppParams.GetOrGenerate( - simState.Cdc, OldCoinAmount, &oldCoinAmount, simState.Rand, - func(r *rand.Rand) { oldCoinAmount = GenOldCoinAmount(r) }, - ) - - var newCoinAmount sdk.Int - simState.AppParams.GetOrGenerate( - simState.Cdc, NewCoinAmount, &newCoinAmount, simState.Rand, - func(r *rand.Rand) { newCoinAmount = GenNewCoinAmount(r) }, - ) - - var swappableNewCoinAmount sdk.Int - simState.AppParams.GetOrGenerate( - simState.Cdc, SwappableNewCoinAmount, &swappableNewCoinAmount, simState.Rand, - func(r *rand.Rand) { swappableNewCoinAmount = GenSwappableNewCoinAmount(r) }, - ) - - fswapParams := types.NewParams(swappableNewCoinAmount) - fswapSwapped := types.NewSwapped(oldCoinAmount, newCoinAmount) - fswapGenesis := types.NewGenesisState(fswapParams, fswapSwapped) - - bz, err := json.MarshalIndent(&fswapGenesis, "", " ") - if err != nil { - panic(err) - } - fmt.Printf("Selected randomly generated fswap parameters:\n%s\n", bz) - simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(fswapGenesis) -} diff --git a/x/fswap/simulation/genesis_test.go b/x/fswap/simulation/genesis_test.go index 00ac1852d7..5c6452f9a0 100644 --- a/x/fswap/simulation/genesis_test.go +++ b/x/fswap/simulation/genesis_test.go @@ -1,78 +1 @@ package simulation_test - -import ( - "encoding/json" - "math/rand" - "testing" - - "github.com/stretchr/testify/require" - - "github.com/Finschia/finschia-sdk/codec" - codectypes "github.com/Finschia/finschia-sdk/codec/types" - sdk "github.com/Finschia/finschia-sdk/types" - "github.com/Finschia/finschia-sdk/types/module" - simtypes "github.com/Finschia/finschia-sdk/types/simulation" - "github.com/Finschia/finschia-sdk/x/fswap/simulation" - "github.com/Finschia/finschia-sdk/x/fswap/types" -) - -// TestRandomizedGenState tests the normal scenario of applying RandomizedGenState. -// Abonormal scenarios are not tested here. -func TestRandomizedGenState(t *testing.T) { - interfaceRegistry := codectypes.NewInterfaceRegistry() - cdc := codec.NewProtoCodec(interfaceRegistry) - - s := rand.NewSource(1) - r := rand.New(s) - - simState := module.SimulationState{ - AppParams: make(simtypes.AppParams), - Cdc: cdc, - Rand: r, - NumBonded: 3, - Accounts: simtypes.RandomAccounts(r, 3), - InitialStake: 1000, - GenState: make(map[string]json.RawMessage), - } - - simulation.RandomizedGenState(&simState) - - var fswapGenesis types.GenesisState - - int1 := sdk.NewInt(296839) - int2 := sdk.NewInt(1754) - int3 := sdk.NewInt(138374) - simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &fswapGenesis) - require.Equal(t, int1, fswapGenesis.Params.SwappableNewCoinAmount) - require.Equal(t, int2, fswapGenesis.Swapped.OldCoinAmount) - require.Equal(t, int3, fswapGenesis.Swapped.NewCoinAmount) -} - -// TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState. -func TestRandomizedGenState1(t *testing.T) { - interfaceRegistry := codectypes.NewInterfaceRegistry() - cdc := codec.NewProtoCodec(interfaceRegistry) - - s := rand.NewSource(1) - r := rand.New(s) - // all these tests will panic - tests := []struct { - simState module.SimulationState - panicMsg string - }{ - { // panic => reason: incomplete initialization of the simState - module.SimulationState{}, "invalid memory address or nil pointer dereference"}, - { // panic => reason: incomplete initialization of the simState - module.SimulationState{ - AppParams: make(simtypes.AppParams), - Cdc: cdc, - Rand: r, - }, "assignment to entry in nil map"}, - } - - for _, tt := range tests { - tt := tt - - require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg) - } -} From 4c8a928c2e0904e82958120f5f42c45e2e4204a0 Mon Sep 17 00:00:00 2001 From: 170210 Date: Fri, 26 Apr 2024 17:44:23 +0900 Subject: [PATCH 05/47] chore: correct query cli Signed-off-by: 170210 --- x/fswap/client/cli/query.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/fswap/client/cli/query.go b/x/fswap/client/cli/query.go index 55ec1d2bd1..e3d5c8ea37 100644 --- a/x/fswap/client/cli/query.go +++ b/x/fswap/client/cli/query.go @@ -30,7 +30,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command { func CmdQuerySwapped() *cobra.Command { cmd := &cobra.Command{ - Use: "fswap swapped", + Use: "swapped", Short: "shows the current swap status, including both old and new coin amount", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { @@ -55,7 +55,7 @@ func CmdQuerySwapped() *cobra.Command { func CmdQueryTotalSwappableAmount() *cobra.Command { cmd := &cobra.Command{ - Use: "fswap total-swappable-amount", + Use: "total-swappable-amount", Short: "shows the current total amount of new coin that're swappable", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { From 015c224d83a2b6ac55002e1616a69182928c030d Mon Sep 17 00:00:00 2001 From: 170210 Date: Fri, 26 Apr 2024 17:44:52 +0900 Subject: [PATCH 06/47] chore: add query for test Signed-off-by: 170210 --- x/fswap/keeper/grpc_query.go | 19 +++++++++++++++++-- x/fswap/spec/README.md | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 x/fswap/spec/README.md diff --git a/x/fswap/keeper/grpc_query.go b/x/fswap/keeper/grpc_query.go index b55b7c1155..b7ba56a746 100644 --- a/x/fswap/keeper/grpc_query.go +++ b/x/fswap/keeper/grpc_query.go @@ -3,17 +3,32 @@ package keeper import ( "context" + sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/fswap/types" ) var _ types.QueryServer = Keeper{} +// todo this files is just for test // Swapped implements types.QueryServer. -func (k Keeper) Swapped(context.Context, *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) { - panic("unimplemented") +func (k Keeper) Swapped(ctx context.Context, req *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + check := k.GetFswapInit(sdkCtx) + + return &types.QuerySwappedResponse{ + OldCoin: sdk.NewCoin(check.FromDenom, sdk.ZeroInt()), + NewCoin: sdk.NewCoin(check.ToDenom, sdk.ZeroInt()), + }, nil } // TotalNewCurrencySwapLimit implements types.QueryServer. func (k Keeper) TotalNewCurrencySwapLimit(context.Context, *types.QueryTotalSwappableAmountRequest) (*types.QueryTotalSwappableAmountResponse, error) { panic("unimplemented") + // sdkCtx := sdk.UnwrapSDKContext(ctx) + // totalSwappableAmount := k.GetParams(sdkCtx).SwappableNewCoinAmount + // swapped := k.GetSwapped(sdkCtx) + // swappableNewCoin := sdk.NewCoin(k.config.NewCoinDenom, totalSwappableAmount.Sub(swapped.NewCoinAmount)) + // return &types.QueryTotalSwappableAmountResponse{ + // SwappableNewCoin: swappableNewCoin, + // }, nil } diff --git a/x/fswap/spec/README.md b/x/fswap/spec/README.md new file mode 100644 index 0000000000..6802e251ac --- /dev/null +++ b/x/fswap/spec/README.md @@ -0,0 +1,16 @@ +``` +make build +make install +zsh init_node.sh sim 1 +``` + +open the `./.simapp/simapp0/config/genesis.json` change the value of `voting_params.voting_period` to `10s` +``` +simd start --home ~/.simapp/simapp0 +simd tx gov submit-proposal fswap-init --title "test" --description "test" --from link146asaycmtydq45kxc8evntqfgepagygelel00h --from-denom "cony" --to-denom "PDT" --swap-rate 123 --amount-limit 1000 --deposit 10000000stake --chain-id=sim --keyring-backend=test --gas-prices 1000stake --gas 10000000 --gas-adjustment 1.5 --home ~/.simapp/simapp0 -b block -y +simd tx gov vote 1 yes --from link146asaycmtydq45kxc8evntqfgepagygelel00h --chain-id=sim --keyring-backend=test --home ~/.simapp/simapp0 -b block -y +``` + +``` +simd query fswap swapped --chain-id=sim +``` \ No newline at end of file From 4701f3e5277aadb6ffbfef197fa8633a2be81195 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 26 Apr 2024 19:16:50 +0900 Subject: [PATCH 07/47] chore: lint fix --- x/fswap/client/proposal_handler.go | 4 +--- x/fswap/keeper/keeper.go | 8 ++++++-- x/fswap/proposal_handler_test.go | 5 +++-- x/fswap/types/fswapInit.go | 3 ++- x/fswap/types/proposal.go | 5 +++-- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/x/fswap/client/proposal_handler.go b/x/fswap/client/proposal_handler.go index 1a66debf26..c104b2a153 100644 --- a/x/fswap/client/proposal_handler.go +++ b/x/fswap/client/proposal_handler.go @@ -5,6 +5,4 @@ import ( govclient "github.com/Finschia/finschia-sdk/x/gov/client" ) -var ( - FswapInitHandler = govclient.NewProposalHandler(cli.NewCmdFswapInitProposal) -) +var FswapInitHandler = govclient.NewProposalHandler(cli.NewCmdFswapInitProposal) diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 6528bc3cd0..85c4c34269 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -46,9 +46,13 @@ func (k Keeper) FswapInit(ctx sdk.Context, fswapInit types.FswapInit) error { } // need confirm: Is ibcState necessary? (please ref upgrade keeper) - k.SetFswapInit(ctx, fswapInit) + if err := k.SetFswapInit(ctx, fswapInit); err != nil { + return err + } // need confirm: Is Swapped use sdk.Coin or sdk.Int swapped := types.NewSwapped(sdk.ZeroInt(), sdk.ZeroInt()) - k.SetSwapped(ctx, swapped) + if err := k.SetSwapped(ctx, swapped); err != nil { + return err + } return nil } diff --git a/x/fswap/proposal_handler_test.go b/x/fswap/proposal_handler_test.go index 6c86f9efe5..d69b329fdc 100644 --- a/x/fswap/proposal_handler_test.go +++ b/x/fswap/proposal_handler_test.go @@ -3,12 +3,13 @@ package fswap_test import ( "testing" + "github.com/stretchr/testify/require" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + "github.com/Finschia/finschia-sdk/simapp" sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/fswap" "github.com/Finschia/finschia-sdk/x/fswap/types" - "github.com/stretchr/testify/require" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" ) func testProposal(fswapInit types.FswapInit) *types.FswapInitProposal { diff --git a/x/fswap/types/fswapInit.go b/x/fswap/types/fswapInit.go index 7bf9438249..5a259a5821 100644 --- a/x/fswap/types/fswapInit.go +++ b/x/fswap/types/fswapInit.go @@ -1,8 +1,9 @@ package types import ( - sdk "github.com/Finschia/finschia-sdk/types" "gopkg.in/yaml.v2" + + sdk "github.com/Finschia/finschia-sdk/types" ) // Validate validates the set of swapped diff --git a/x/fswap/types/proposal.go b/x/fswap/types/proposal.go index c728d11339..fcd15e444f 100644 --- a/x/fswap/types/proposal.go +++ b/x/fswap/types/proposal.go @@ -1,8 +1,9 @@ package types import ( - gov "github.com/Finschia/finschia-sdk/x/gov/types" "gopkg.in/yaml.v2" + + gov "github.com/Finschia/finschia-sdk/x/gov/types" ) const ( @@ -12,7 +13,7 @@ const ( // NewFswapInitProposal creates a new FswapInitProposal instance. // Deprecated: this proposal is considered legacy and is deprecated in favor of // Msg-based gov proposals. See MsgFswapInit. -func NewFswapInitProposal(title string, description string, fswapInit FswapInit) *FswapInitProposal { +func NewFswapInitProposal(title, description string, fswapInit FswapInit) *FswapInitProposal { return &FswapInitProposal{title, description, fswapInit} } From a139b67ff191ca137dfa279e9046eb22f15c5323 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Mon, 29 Apr 2024 08:55:36 +0900 Subject: [PATCH 08/47] chore: fix for tx cli --- x/fswap/client/cli/tx.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index ee1c5cb9d1..0e83601fa1 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -43,7 +43,7 @@ func GetTxCmd() *cobra.Command { func CmdTxMsgSwap() *cobra.Command { cmd := &cobra.Command{ - Use: "fswap swap [address] [fnsa_amount]", + Use: "swap [address] [fnsa_amount]", Short: "swap amounts of old coin to new coin. Note, the'--from' flag is ignored as it is implied from [address].", Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { @@ -77,7 +77,7 @@ func CmdTxMsgSwap() *cobra.Command { func CmdTxMsgSwapAll() *cobra.Command { cmd := &cobra.Command{ - Use: "fswap swap-all [address]", + Use: "swap-all [address]", Short: "swap all the old coins. Note, the'--from' flag is ignored as it is implied from [address].", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { From 60d0112a37f7d97c15248fb8581282a9f4430265 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Mon, 29 Apr 2024 23:00:27 +0900 Subject: [PATCH 09/47] chore: update proto --- docs/core/proto-docs.md | 28 +++---- proto/lbm/fswap/v1/event.proto | 4 +- proto/lbm/fswap/v1/fswap.proto | 19 +++-- proto/lbm/fswap/v1/genesis.proto | 4 +- proto/lbm/fswap/v1/query.proto | 9 +- x/fswap/client/cli/query.go | 2 +- x/fswap/client/cli/tx.go | 8 +- x/fswap/types/event.pb.go | 66 +++++++-------- x/fswap/types/fswap.go | 43 +++++++--- x/fswap/types/fswap.pb.go | 138 +++++++++++++++++-------------- x/fswap/types/fswapInit.go | 39 --------- x/fswap/types/genesis.go | 35 +++++--- x/fswap/types/genesis.pb.go | 80 +++++++++++------- x/fswap/types/keys.go | 12 --- x/fswap/types/msgs.go | 24 +++--- x/fswap/types/proposal.go | 14 ++-- x/fswap/types/query.pb.go | 117 +++++++++++++------------- x/fswap/types/query.pb.gw.go | 24 +++--- x/fswap/types/types.go | 1 - 19 files changed, 347 insertions(+), 320 deletions(-) delete mode 100644 x/fswap/types/fswapInit.go delete mode 100644 x/fswap/types/types.go diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index ad1362c73b..00f451c881 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -12726,8 +12726,8 @@ Msg defines the foundation Msg service. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | | `address` | [string](#string) | | holder's address | -| `old_coin` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | amount of the old currency | -| `new_coin` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | amount of the new currency | +| `old_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | amount of the old currency | +| `new_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | amount of the new currency | @@ -12758,10 +12758,10 @@ Msg defines the foundation Msg service. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `fromDenom` | [string](#string) | | | -| `toDenom` | [string](#string) | | | -| `AmountLimit` | [string](#string) | | | -| `SwapRate` | [string](#string) | | | +| `from_denom` | [string](#string) | | | +| `to_denom` | [string](#string) | | | +| `amount_cap_for_to_denom` | [string](#string) | | | +| `swap_multiple` | [string](#string) | | | @@ -12793,8 +12793,8 @@ From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk base | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `old_coin_amount` | [string](#string) | | | -| `new_coin_amount` | [string](#string) | | | +| `old_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | +| `new_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | @@ -12825,8 +12825,8 @@ GenesisState defines the fswap module's genesis state. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `fswap_init` | [FswapInit](#lbm.fswap.v1.FswapInit) | | | -| `swapped` | [Swapped](#lbm.fswap.v1.Swapped) | | | +| `fswap_init` | [FswapInit](#lbm.fswap.v1.FswapInit) | repeated | | +| `swapped` | [Swapped](#lbm.fswap.v1.Swapped) | repeated | | @@ -12867,8 +12867,8 @@ GenesisState defines the fswap module's genesis state. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `old_coin` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | -| `new_coin` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | +| `old_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | +| `new_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | @@ -12913,8 +12913,8 @@ GenesisState defines the fswap module's genesis state. | Method Name | Request Type | Response Type | Description | HTTP Verb | Endpoint | | ----------- | ------------ | ------------- | ------------| ------- | -------- | -| `Swapped` | [QuerySwappedRequest](#lbm.fswap.v1.QuerySwappedRequest) | [QuerySwappedResponse](#lbm.fswap.v1.QuerySwappedResponse) | | GET|/lbm/fswap/v1/swapped| -| `TotalNewCurrencySwapLimit` | [QueryTotalSwappableAmountRequest](#lbm.fswap.v1.QueryTotalSwappableAmountRequest) | [QueryTotalSwappableAmountResponse](#lbm.fswap.v1.QueryTotalSwappableAmountResponse) | | GET|/lbm/fswap/v1/swappable_new_coin_amount| +| `Swapped` | [QuerySwappedRequest](#lbm.fswap.v1.QuerySwappedRequest) | [QuerySwappedResponse](#lbm.fswap.v1.QuerySwappedResponse) | Swapped queries the current swapped status that includes a burnt amount of old coin and a minted amount of new coin. | GET|/lbm/fswap/v1/swapped| +| `TotalSwappableAmount` | [QueryTotalSwappableAmountRequest](#lbm.fswap.v1.QueryTotalSwappableAmountRequest) | [QueryTotalSwappableAmountResponse](#lbm.fswap.v1.QueryTotalSwappableAmountResponse) | TotalSwappableAmount queries the current swappable amount for new coin. | GET|/lbm/fswap/v1/swappable_new_coin_amount| diff --git a/proto/lbm/fswap/v1/event.proto b/proto/lbm/fswap/v1/event.proto index 2fea28e9d0..374447f7c3 100644 --- a/proto/lbm/fswap/v1/event.proto +++ b/proto/lbm/fswap/v1/event.proto @@ -10,9 +10,9 @@ message EventSwapCoins { // holder's address string address = 1; // amount of the old currency - cosmos.base.v1beta1.Coin old_coin = 2 + cosmos.base.v1beta1.Coin old_coin_amount = 2 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; // amount of the new currency - cosmos.base.v1beta1.Coin new_coin = 3 + cosmos.base.v1beta1.Coin new_coin_amount = 3 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; } diff --git a/proto/lbm/fswap/v1/fswap.proto b/proto/lbm/fswap/v1/fswap.proto index 27ad27dba4..0a7be86ac4 100644 --- a/proto/lbm/fswap/v1/fswap.proto +++ b/proto/lbm/fswap/v1/fswap.proto @@ -4,16 +4,17 @@ package lbm.fswap.v1; option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types"; import "gogoproto/gogo.proto"; +import "cosmos/base/v1beta1/coin.proto"; message FswapInit { option (gogoproto.equal) = true; option (gogoproto.goproto_stringer) = false; - string fromDenom = 1; - string toDenom = 2; - string AmountLimit = 3 + string from_denom = 1; + string to_denom = 2; + string amount_cap_for_to_denom = 3 [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; - string SwapRate = 4 + string swap_multiple = 4 [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; } @@ -28,9 +29,9 @@ message FswapInitProposal { } message Swapped { - option (gogoproto.goproto_stringer) = false; - string old_coin_amount = 1 - [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; - string new_coin_amount = 2 - [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; + option (gogoproto.goproto_stringer) = false; + cosmos.base.v1beta1.Coin old_coin_amount = 1 + [(gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin", (gogoproto.nullable) = false]; + cosmos.base.v1beta1.Coin new_coin_amount = 2 + [(gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin", (gogoproto.nullable) = false]; } diff --git a/proto/lbm/fswap/v1/genesis.proto b/proto/lbm/fswap/v1/genesis.proto index 74739b80c8..8f73e05399 100644 --- a/proto/lbm/fswap/v1/genesis.proto +++ b/proto/lbm/fswap/v1/genesis.proto @@ -8,6 +8,6 @@ import "lbm/fswap/v1/fswap.proto"; // GenesisState defines the fswap module's genesis state. message GenesisState { - FswapInit fswap_init = 1 [(gogoproto.nullable) = false]; - Swapped swapped = 2 [(gogoproto.nullable) = false]; + repeated FswapInit fswap_init = 1 [(gogoproto.nullable) = false]; + repeated Swapped swapped = 2 [(gogoproto.nullable) = false]; } diff --git a/proto/lbm/fswap/v1/query.proto b/proto/lbm/fswap/v1/query.proto index 5b2e0d02e9..2ef950bd79 100644 --- a/proto/lbm/fswap/v1/query.proto +++ b/proto/lbm/fswap/v1/query.proto @@ -9,19 +9,22 @@ import "cosmos/base/v1beta1/coin.proto"; import "lbm/fswap/v1/fswap.proto"; service Query { + // Swapped queries the current swapped status that includes a burnt amount of old coin and a minted amount of new + // coin. rpc Swapped(QuerySwappedRequest) returns (QuerySwappedResponse) { option (google.api.http).get = "/lbm/fswap/v1/swapped"; } - rpc TotalNewCurrencySwapLimit(QueryTotalSwappableAmountRequest) returns (QueryTotalSwappableAmountResponse) { + // TotalSwappableAmount queries the current swappable amount for new coin. + rpc TotalSwappableAmount(QueryTotalSwappableAmountRequest) returns (QueryTotalSwappableAmountResponse) { option (google.api.http).get = "/lbm/fswap/v1/swappable_new_coin_amount"; } } message QuerySwappedRequest {} message QuerySwappedResponse { - cosmos.base.v1beta1.Coin old_coin = 1 + cosmos.base.v1beta1.Coin old_coin_amount = 1 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; - cosmos.base.v1beta1.Coin new_coin = 2 + cosmos.base.v1beta1.Coin new_coin_amount = 2 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; } diff --git a/x/fswap/client/cli/query.go b/x/fswap/client/cli/query.go index e3d5c8ea37..f92118cfe3 100644 --- a/x/fswap/client/cli/query.go +++ b/x/fswap/client/cli/query.go @@ -65,7 +65,7 @@ func CmdQueryTotalSwappableAmount() *cobra.Command { } queryClient := types.NewQueryClient(clientCtx) - res, err := queryClient.TotalNewCurrencySwapLimit(cmd.Context(), &types.QueryTotalSwappableAmountRequest{}) + res, err := queryClient.TotalSwappableAmount(cmd.Context(), &types.QueryTotalSwappableAmountRequest{}) if err != nil { return err } diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index 0e83601fa1..6afe904501 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -184,10 +184,10 @@ func parseArgsToContent(cmd *cobra.Command) (gov.Content, error) { } fswapInit := types.FswapInit{ - FromDenom: from_denom, - ToDenom: to_denom, - AmountLimit: sdk.NewInt(amount_limit), - SwapRate: sdk.NewInt(swap_rate), + FromDenom: from_denom, + ToDenom: to_denom, + AmountCapForToDenom: sdk.NewInt(amount_limit), + SwapMultiple: sdk.NewInt(swap_rate), } content := types.NewFswapInitProposal(title, description, fswapInit) diff --git a/x/fswap/types/event.pb.go b/x/fswap/types/event.pb.go index d3e5284230..5808cc6e7f 100644 --- a/x/fswap/types/event.pb.go +++ b/x/fswap/types/event.pb.go @@ -28,9 +28,9 @@ type EventSwapCoins struct { // holder's address Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` // amount of the old currency - OldCoin types.Coin `protobuf:"bytes,2,opt,name=old_coin,json=oldCoin,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"old_coin"` + OldCoinAmount types.Coin `protobuf:"bytes,2,opt,name=old_coin_amount,json=oldCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"old_coin_amount"` // amount of the new currency - NewCoin types.Coin `protobuf:"bytes,3,opt,name=new_coin,json=newCoin,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"new_coin"` + NewCoinAmount types.Coin `protobuf:"bytes,3,opt,name=new_coin_amount,json=newCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"new_coin_amount"` } func (m *EventSwapCoins) Reset() { *m = EventSwapCoins{} } @@ -73,16 +73,16 @@ func (m *EventSwapCoins) GetAddress() string { return "" } -func (m *EventSwapCoins) GetOldCoin() types.Coin { +func (m *EventSwapCoins) GetOldCoinAmount() types.Coin { if m != nil { - return m.OldCoin + return m.OldCoinAmount } return types.Coin{} } -func (m *EventSwapCoins) GetNewCoin() types.Coin { +func (m *EventSwapCoins) GetNewCoinAmount() types.Coin { if m != nil { - return m.NewCoin + return m.NewCoinAmount } return types.Coin{} } @@ -94,25 +94,25 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/event.proto", fileDescriptor_92d5edbd64a725af) } var fileDescriptor_92d5edbd64a725af = []byte{ - // 278 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x91, 0xb1, 0x4e, 0xc3, 0x30, - 0x10, 0x86, 0xe3, 0x22, 0x51, 0x08, 0x88, 0xa1, 0x62, 0x08, 0x1d, 0xdc, 0x8a, 0xa9, 0x12, 0xc2, - 0x56, 0xe8, 0x1b, 0x80, 0x40, 0xcc, 0x65, 0x63, 0x41, 0x76, 0xe2, 0xa6, 0x16, 0x89, 0x2f, 0xea, - 0x99, 0x04, 0x36, 0x1e, 0x81, 0xe7, 0xe0, 0x49, 0x3a, 0x76, 0x64, 0x02, 0x94, 0xbc, 0x08, 0xb2, - 0x13, 0x66, 0x26, 0xb6, 0xff, 0xf4, 0xdf, 0xff, 0xfd, 0xd2, 0x5d, 0x18, 0xe5, 0xb2, 0xe0, 0x4b, - 0xac, 0x45, 0xc9, 0xab, 0x98, 0xab, 0x4a, 0x19, 0xcb, 0xca, 0x35, 0x58, 0x18, 0x1d, 0xe6, 0xb2, - 0x60, 0xde, 0x61, 0x55, 0x3c, 0x3e, 0xce, 0x20, 0x03, 0x6f, 0x70, 0xa7, 0xba, 0x9d, 0x31, 0x4d, - 0x00, 0x0b, 0x40, 0x2e, 0x05, 0x2a, 0x5e, 0xc5, 0x52, 0x59, 0x11, 0xf3, 0x04, 0xb4, 0xe9, 0xfc, - 0xd3, 0xd7, 0x41, 0x78, 0x74, 0xed, 0x98, 0x77, 0xb5, 0x28, 0xaf, 0x40, 0x1b, 0x1c, 0x45, 0xe1, - 0x50, 0xa4, 0xe9, 0x5a, 0x21, 0x46, 0x64, 0x4a, 0x66, 0xfb, 0x8b, 0xdf, 0x71, 0xa4, 0xc3, 0x3d, - 0xc8, 0xd3, 0x07, 0x17, 0x8f, 0x06, 0x53, 0x32, 0x3b, 0xb8, 0x38, 0x61, 0x1d, 0x9f, 0x39, 0x3e, - 0xeb, 0xf9, 0xcc, 0x71, 0x2e, 0xe7, 0x9b, 0xcf, 0x49, 0xf0, 0xfe, 0x35, 0x39, 0xcb, 0xb4, 0x5d, - 0x3d, 0x49, 0x96, 0x40, 0xc1, 0x6f, 0xb4, 0xc1, 0x64, 0xa5, 0x05, 0x5f, 0xf6, 0xe2, 0x1c, 0xd3, - 0x47, 0x6e, 0x5f, 0x4a, 0x85, 0x3e, 0xb4, 0x18, 0x42, 0x9e, 0x3a, 0xe1, 0xaa, 0x8c, 0xaa, 0xbb, - 0xaa, 0x9d, 0xff, 0xa9, 0x32, 0xaa, 0xf6, 0xe9, 0xdb, 0x4d, 0x43, 0xc9, 0xb6, 0xa1, 0xe4, 0xbb, - 0xa1, 0xe4, 0xad, 0xa5, 0xc1, 0xb6, 0xa5, 0xc1, 0x47, 0x4b, 0x83, 0x7b, 0xf6, 0x27, 0xef, 0xb9, - 0xff, 0x8c, 0xe7, 0xca, 0x5d, 0x7f, 0xd3, 0xf9, 0x4f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xb7, 0x1e, - 0x90, 0x67, 0xb3, 0x01, 0x00, 0x00, + // 288 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x91, 0x3f, 0x4e, 0xc3, 0x30, + 0x1c, 0x85, 0xe3, 0x22, 0x81, 0x08, 0xff, 0xa4, 0x88, 0x21, 0x74, 0x70, 0x2b, 0xa6, 0x4a, 0x08, + 0x5b, 0xa1, 0x27, 0xa0, 0x08, 0xc4, 0x5c, 0x36, 0x96, 0xca, 0x4e, 0xdc, 0x34, 0x22, 0xf1, 0x2f, + 0xaa, 0x5d, 0x07, 0x6e, 0xc1, 0xcc, 0x11, 0x38, 0x49, 0xc7, 0x8e, 0x4c, 0x80, 0x92, 0x8b, 0x20, + 0x3b, 0x41, 0x88, 0x89, 0x89, 0xed, 0x59, 0xcf, 0xef, 0xfb, 0x24, 0xdb, 0x0f, 0x73, 0x5e, 0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4f, 0xfe, 0xa4, 0x3e, 0x76, 0x7f, 0xe6, 0xe8, 0x7c, 0xdb, + 0xbd, 0xf6, 0xf8, 0x2b, 0x00, 0x00, 0xff, 0xff, 0x2b, 0x08, 0x6b, 0x7c, 0xcd, 0x01, 0x00, 0x00, } func (m *EventSwapCoins) Marshal() (dAtA []byte, err error) { @@ -136,7 +136,7 @@ func (m *EventSwapCoins) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size, err := m.NewCoin.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.NewCoinAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -146,7 +146,7 @@ func (m *EventSwapCoins) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x1a { - size, err := m.OldCoin.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.OldCoinAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -186,9 +186,9 @@ func (m *EventSwapCoins) Size() (n int) { if l > 0 { n += 1 + l + sovEvent(uint64(l)) } - l = m.OldCoin.Size() + l = m.OldCoinAmount.Size() n += 1 + l + sovEvent(uint64(l)) - l = m.NewCoin.Size() + l = m.NewCoinAmount.Size() n += 1 + l + sovEvent(uint64(l)) return n } @@ -262,7 +262,7 @@ func (m *EventSwapCoins) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field OldCoin", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field OldCoinAmount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -289,13 +289,13 @@ func (m *EventSwapCoins) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.OldCoin.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.OldCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 3: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field NewCoin", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field NewCoinAmount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -322,7 +322,7 @@ func (m *EventSwapCoins) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.NewCoin.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.NewCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/fswap/types/fswap.go b/x/fswap/types/fswap.go index 76803ed5f3..bd01e4b0ec 100644 --- a/x/fswap/types/fswap.go +++ b/x/fswap/types/fswap.go @@ -1,7 +1,7 @@ package types import ( - fmt "fmt" + "fmt" "gopkg.in/yaml.v2" @@ -10,8 +10,8 @@ import ( // NewSwapped creates a new Swapped instance func NewSwapped( - oldCoinAmount sdk.Int, - newCoinAmount sdk.Int, + oldCoinAmount sdk.Coin, + newCoinAmount sdk.Coin, ) Swapped { return Swapped{ OldCoinAmount: oldCoinAmount, @@ -21,23 +21,20 @@ func NewSwapped( // DefaultSwapped returns an initial Swapped object func DefaultSwapped() Swapped { - return NewSwapped(sdk.ZeroInt(), sdk.ZeroInt()) + return NewSwapped(sdk.Coin{}, sdk.Coin{}) } func validateCoinAmount(i interface{}) error { - v, ok := i.(sdk.Int) + v, ok := i.(sdk.Coin) if !ok { return fmt.Errorf("invalid coin amount: %T", i) } - if v.IsNil() { return fmt.Errorf("coin amount must be not nil") } - - if v.LT(sdk.ZeroInt()) { - return fmt.Errorf("coin amount cannot be lower than 0") + if err := v.Validate(); err != nil { + return err } - return nil } @@ -57,3 +54,29 @@ func (s Swapped) String() string { out, _ := yaml.Marshal(s) return string(out) } + +// Validate validates the set of swapped +func (f FswapInit) ValidateBasic() error { + if f.FromDenom == "" { + return fmt.Errorf("from denomination cannot be empty") + } + if f.ToDenom == "" { + return fmt.Errorf("to denomination cannot be empty") + } + if f.FromDenom == f.ToDenom { + return fmt.Errorf("from denomination cannot be equal to to denomination") + } + if f.AmountCapForToDenom.LT(sdk.ZeroInt()) { + return fmt.Errorf("amount cannot be less than zero") + } + if f.SwapMultiple.LT(sdk.ZeroInt()) { + return fmt.Errorf("swap multiple cannot be less than zero") + } + return nil +} + +// String implements the Stringer interface. +func (f FswapInit) String() string { + out, _ := yaml.Marshal(f) + return string(out) +} diff --git a/x/fswap/types/fswap.pb.go b/x/fswap/types/fswap.pb.go index f29cdde09d..d25ba4bcd4 100644 --- a/x/fswap/types/fswap.pb.go +++ b/x/fswap/types/fswap.pb.go @@ -6,6 +6,7 @@ package types import ( fmt "fmt" github_com_Finschia_finschia_sdk_types "github.com/Finschia/finschia-sdk/types" + types "github.com/Finschia/finschia-sdk/types" _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" io "io" @@ -25,10 +26,10 @@ var _ = math.Inf const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type FswapInit struct { - FromDenom string `protobuf:"bytes,1,opt,name=fromDenom,proto3" json:"fromDenom,omitempty"` - ToDenom string `protobuf:"bytes,2,opt,name=toDenom,proto3" json:"toDenom,omitempty"` - AmountLimit github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,3,opt,name=AmountLimit,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"AmountLimit"` - SwapRate github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,4,opt,name=SwapRate,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"SwapRate"` + FromDenom string `protobuf:"bytes,1,opt,name=from_denom,json=fromDenom,proto3" json:"from_denom,omitempty"` + ToDenom string `protobuf:"bytes,2,opt,name=to_denom,json=toDenom,proto3" json:"to_denom,omitempty"` + AmountCapForToDenom github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,3,opt,name=amount_cap_for_to_denom,json=amountCapForToDenom,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"amount_cap_for_to_denom"` + SwapMultiple github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,4,opt,name=swap_multiple,json=swapMultiple,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"swap_multiple"` } func (m *FswapInit) Reset() { *m = FswapInit{} } @@ -138,8 +139,8 @@ func (m *FswapInitProposal) GetFswapInit() FswapInit { } type Swapped struct { - OldCoinAmount github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,1,opt,name=old_coin_amount,json=oldCoinAmount,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"old_coin_amount"` - NewCoinAmount github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,2,opt,name=new_coin_amount,json=newCoinAmount,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"new_coin_amount"` + OldCoinAmount types.Coin `protobuf:"bytes,1,opt,name=old_coin_amount,json=oldCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"old_coin_amount"` + NewCoinAmount types.Coin `protobuf:"bytes,2,opt,name=new_coin_amount,json=newCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"new_coin_amount"` } func (m *Swapped) Reset() { *m = Swapped{} } @@ -174,6 +175,20 @@ func (m *Swapped) XXX_DiscardUnknown() { var xxx_messageInfo_Swapped proto.InternalMessageInfo +func (m *Swapped) GetOldCoinAmount() types.Coin { + if m != nil { + return m.OldCoinAmount + } + return types.Coin{} +} + +func (m *Swapped) GetNewCoinAmount() types.Coin { + if m != nil { + return m.NewCoinAmount + } + return types.Coin{} +} + func init() { proto.RegisterType((*FswapInit)(nil), "lbm.fswap.v1.FswapInit") proto.RegisterType((*FswapInitProposal)(nil), "lbm.fswap.v1.FswapInitProposal") @@ -183,32 +198,37 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/fswap.proto", fileDescriptor_42ca60eaf37a2b67) } var fileDescriptor_42ca60eaf37a2b67 = []byte{ - // 397 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x92, 0x3f, 0x6b, 0xdb, 0x40, - 0x18, 0xc6, 0x75, 0xae, 0x5b, 0x5b, 0xe7, 0x96, 0xd2, 0xc3, 0x50, 0x51, 0x8a, 0x64, 0x3c, 0x95, - 0x42, 0x25, 0xec, 0x6e, 0xa6, 0x4b, 0xdd, 0x62, 0x6a, 0x28, 0xa5, 0x28, 0x99, 0xbc, 0x18, 0xfd, - 0x39, 0xd9, 0x47, 0xa4, 0x7b, 0x85, 0x74, 0xb6, 0x92, 0x6f, 0x11, 0xc8, 0x92, 0xd1, 0xdf, 0x26, - 0x1e, 0x3d, 0x86, 0x0c, 0x26, 0xd8, 0x4b, 0xf6, 0x7c, 0x81, 0xa0, 0x3f, 0xb6, 0x95, 0x29, 0xe0, - 0xed, 0xbd, 0xe7, 0x39, 0x7e, 0xf7, 0x3e, 0x0f, 0x87, 0x15, 0xdf, 0x0e, 0x0c, 0x2f, 0x4e, 0xac, - 0xd0, 0x98, 0x77, 0xf2, 0x41, 0x0f, 0x23, 0x10, 0x40, 0xde, 0xfa, 0x76, 0xa0, 0xe7, 0xc2, 0xbc, - 0xf3, 0xa9, 0x39, 0x81, 0x09, 0x64, 0x86, 0x91, 0x4e, 0xf9, 0x9d, 0xf6, 0x23, 0xc2, 0xf2, 0x20, - 0xbd, 0x32, 0xe4, 0x4c, 0x90, 0xcf, 0x58, 0xf6, 0x22, 0x08, 0x7e, 0x53, 0x0e, 0x81, 0x82, 0x5a, - 0xe8, 0x8b, 0x6c, 0x1e, 0x04, 0xa2, 0xe0, 0x9a, 0x80, 0xdc, 0xab, 0x64, 0xde, 0xee, 0x48, 0x4e, - 0x71, 0xe3, 0x67, 0x00, 0x33, 0x2e, 0xfe, 0xb2, 0x80, 0x09, 0xe5, 0x55, 0xea, 0xf6, 0xbb, 0xcb, - 0xb5, 0x26, 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xd2, 0x6a, 0xab, 0x4a, 0xb7, 0x5b, 0x55, - 0x1a, 0xe9, 0x2f, 0xa2, 0xcf, 0x8b, 0xcf, 0x9c, 0x3d, 0x61, 0xbf, 0xc9, 0xbe, 0xe9, 0xf7, 0xa7, - 0x00, 0x00, 0x00, 0xff, 0xff, 0x25, 0x80, 0x1e, 0xd7, 0xe6, 0x02, 0x00, 0x00, + // 467 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 0x31, 0x6f, 0xd3, 0x40, + 0x18, 0xb5, 0x43, 0xa0, 0xcd, 0xa5, 0x15, 0xc2, 0x54, 0x6a, 0x5a, 0x09, 0xbb, 0xea, 0x84, 0x40, + 0xdc, 0x29, 0xe9, 0x56, 0xb1, 0x90, 0xa2, 0x88, 0x0e, 0x48, 0x28, 0x20, 0x21, 0xb1, 0x58, 0x67, + 0xfb, 0x9c, 0x9c, 0xb0, 0xef, 0x3b, 0xf9, 0x2e, 0x31, 0xfc, 0x0b, 0x04, 0x0b, 0x63, 0x67, 0x7e, + 0x49, 0xc7, 0x8e, 0x88, 0xa1, 0xa0, 0x64, 0xe1, 0x5f, 0x80, 0xce, 0x77, 0x84, 0x30, 0x81, 0x90, + 0xba, 0x7d, 0x79, 0xef, 0xcb, 0x7b, 0x9f, 0xdf, 0xb3, 0x51, 0xaf, 0x48, 0x4a, 0x92, 0xab, 0x9a, + 0x4a, 0x32, 0xef, 0xdb, 0x01, 0xcb, 0x0a, 0x34, 0x04, 0x5b, 0x45, 0x52, 0x62, 0x0b, 0xcc, 0xfb, + 0xfb, 0x3b, 0x13, 0x98, 0x40, 0x43, 0x10, 0x33, 0xd9, 0x9d, 0xfd, 0x30, 0x05, 0x55, 0x82, 0x22, + 0x09, 0x55, 0x8c, 0xcc, 0xfb, 0x09, 0xd3, 0xb4, 0x4f, 0x52, 0xe0, 0xc2, 0xf2, 0x87, 0xef, 0x5b, +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b0, 0x8b, 0xd7, 0xbb, 0xc3, 0x2b, 0xb3, 0x61, 0xdb, 0x3c, + 0xc6, 0xb8, 0x93, 0xff, 0x02, 0xd6, 0xae, 0xfa, 0xe1, 0xa3, 0x8d, 0xe7, 0x35, 0x95, 0x92, 0x65, + 0xc1, 0x1c, 0xdd, 0x84, 0x22, 0x8b, 0x4d, 0x91, 0xb1, 0x0d, 0xa9, 0xb9, 0xaa, 0x3b, 0xd8, 0xc3, + 0xb6, 0x70, 0x6c, 0x0a, 0xc7, 0xae, 0x70, 0x7c, 0x02, 0x5c, 0x0c, 0x8f, 0x8c, 0xf4, 0xa7, 0xaf, + 0xd1, 0xfd, 0x7f, 0x4c, 0xc8, 0xfc, 0x69, 0xbc, 0x0d, 0x45, 0x66, 0x86, 0x47, 0x8d, 0x89, 0xf1, + 0x15, 0xac, 0xfe, 0xc3, 0xb7, 0x75, 0x35, 0xbe, 0x82, 0xd5, 0xbf, 0x7d, 0x8f, 0xdb, 0x26, 0x85, + 0xe1, 0x93, 0xf3, 0x45, 0xe8, 0x5f, 0x2c, 0x42, 0xff, 0xdb, 0x22, 0xf4, 0xdf, 0x2d, 0x43, 0xef, + 0x62, 0x19, 0x7a, 0x9f, 0x97, 0xa1, 0xf7, 0x0a, 0xff, 0x55, 0xfb, 0x8d, 0xfb, 0x86, 0x1a, 0x8f, + 0xe4, 0x46, 0xf3, 0xf6, 0x1f, 0xfd, 0x0c, 0x00, 0x00, 0xff, 0xff, 0xd3, 0xe3, 0x53, 0x7f, 0x5d, + 0x03, 0x00, 0x00, } func (this *FswapInit) Equal(that interface{}) bool { @@ -236,10 +256,10 @@ func (this *FswapInit) Equal(that interface{}) bool { if this.ToDenom != that1.ToDenom { return false } - if !this.AmountLimit.Equal(that1.AmountLimit) { + if !this.AmountCapForToDenom.Equal(that1.AmountCapForToDenom) { return false } - if !this.SwapRate.Equal(that1.SwapRate) { + if !this.SwapMultiple.Equal(that1.SwapMultiple) { return false } return true @@ -295,9 +315,9 @@ func (m *FswapInit) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size := m.SwapRate.Size() + size := m.SwapMultiple.Size() i -= size - if _, err := m.SwapRate.MarshalTo(dAtA[i:]); err != nil { + if _, err := m.SwapMultiple.MarshalTo(dAtA[i:]); err != nil { return 0, err } i = encodeVarintFswap(dAtA, i, uint64(size)) @@ -305,9 +325,9 @@ func (m *FswapInit) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x22 { - size := m.AmountLimit.Size() + size := m.AmountCapForToDenom.Size() i -= size - if _, err := m.AmountLimit.MarshalTo(dAtA[i:]); err != nil { + if _, err := m.AmountCapForToDenom.MarshalTo(dAtA[i:]); err != nil { return 0, err } i = encodeVarintFswap(dAtA, i, uint64(size)) @@ -399,21 +419,21 @@ func (m *Swapped) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size := m.NewCoinAmount.Size() - i -= size - if _, err := m.NewCoinAmount.MarshalTo(dAtA[i:]); err != nil { + size, err := m.NewCoinAmount.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { return 0, err } + i -= size i = encodeVarintFswap(dAtA, i, uint64(size)) } i-- dAtA[i] = 0x12 { - size := m.OldCoinAmount.Size() - i -= size - if _, err := m.OldCoinAmount.MarshalTo(dAtA[i:]); err != nil { + size, err := m.OldCoinAmount.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { return 0, err } + i -= size i = encodeVarintFswap(dAtA, i, uint64(size)) } i-- @@ -446,9 +466,9 @@ func (m *FswapInit) Size() (n int) { if l > 0 { n += 1 + l + sovFswap(uint64(l)) } - l = m.AmountLimit.Size() + l = m.AmountCapForToDenom.Size() n += 1 + l + sovFswap(uint64(l)) - l = m.SwapRate.Size() + l = m.SwapMultiple.Size() n += 1 + l + sovFswap(uint64(l)) return n } @@ -586,7 +606,7 @@ func (m *FswapInit) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 3: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field AmountLimit", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field AmountCapForToDenom", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -614,13 +634,13 @@ func (m *FswapInit) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.AmountLimit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.AmountCapForToDenom.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 4: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field SwapRate", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field SwapMultiple", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -648,7 +668,7 @@ func (m *FswapInit) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.SwapRate.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.SwapMultiple.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex @@ -853,7 +873,7 @@ func (m *Swapped) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field OldCoinAmount", wireType) } - var stringLen uint64 + var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowFswap @@ -863,16 +883,15 @@ func (m *Swapped) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - stringLen |= uint64(b&0x7F) << shift + msglen |= int(b&0x7F) << shift if b < 0x80 { break } } - intStringLen := int(stringLen) - if intStringLen < 0 { + if msglen < 0 { return ErrInvalidLengthFswap } - postIndex := iNdEx + intStringLen + postIndex := iNdEx + msglen if postIndex < 0 { return ErrInvalidLengthFswap } @@ -887,7 +906,7 @@ func (m *Swapped) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field NewCoinAmount", wireType) } - var stringLen uint64 + var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowFswap @@ -897,16 +916,15 @@ func (m *Swapped) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - stringLen |= uint64(b&0x7F) << shift + msglen |= int(b&0x7F) << shift if b < 0x80 { break } } - intStringLen := int(stringLen) - if intStringLen < 0 { + if msglen < 0 { return ErrInvalidLengthFswap } - postIndex := iNdEx + intStringLen + postIndex := iNdEx + msglen if postIndex < 0 { return ErrInvalidLengthFswap } diff --git a/x/fswap/types/fswapInit.go b/x/fswap/types/fswapInit.go deleted file mode 100644 index 5a259a5821..0000000000 --- a/x/fswap/types/fswapInit.go +++ /dev/null @@ -1,39 +0,0 @@ -package types - -import ( - "gopkg.in/yaml.v2" - - sdk "github.com/Finschia/finschia-sdk/types" -) - -// Validate validates the set of swapped -func (fi FswapInit) ValidateBasic() error { - // todo vadalidate - return nil -} - -// String implements the Stringer interface. -func (fi FswapInit) String() string { - out, _ := yaml.Marshal(fi) - return string(out) -} - -// NewFswapInit creates a new FswapInit instance -func NewFswapInit( - fromDenom string, - toDenom string, - amountLimit sdk.Int, - swapRate sdk.Int, -) FswapInit { - return FswapInit{ - FromDenom: fromDenom, - ToDenom: toDenom, - AmountLimit: amountLimit, - SwapRate: swapRate, - } -} - -// DefaultFswapInit returns an initial FswapInit object -func DefaultFswapInit() FswapInit { - return NewFswapInit("", "", sdk.ZeroInt(), sdk.ZeroInt()) -} diff --git a/x/fswap/types/genesis.go b/x/fswap/types/genesis.go index a94daaf040..c93d81e5d2 100644 --- a/x/fswap/types/genesis.go +++ b/x/fswap/types/genesis.go @@ -1,32 +1,43 @@ package types -// NewGenesis creates a new genesis state -func NewGenesisState(fswapInit FswapInit, swapped Swapped) *GenesisState { - return &GenesisState{ - FswapInit: fswapInit, - Swapped: swapped, - } -} +import ( + "errors" +) // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { - return &GenesisState{DefaultFswapInit(), DefaultSwapped()} + return &GenesisState{ + FswapInit: []FswapInit{}, + Swapped: []Swapped{}, + } } // Validate performs basic genesis state validation returning an error upon any // failure. // need confirm: should we validate? Since it may nil func (gs GenesisState) Validate() error { - if gs == *DefaultGenesis() { + if len(gs.GetFswapInit()) == 0 { return nil } - if err := gs.FswapInit.ValidateBasic(); err != nil { + if len(gs.GetSwapped()) == 0 { + return nil + } + if len(gs.GetFswapInit()) > 1 { + return errors.New("cannot have more than one fswap") // TODO(bjs) to sentinel + } + if len(gs.GetSwapped()) > 1 { + return errors.New("cannot have more than one swapped") // TODO(bjs) to sentinel + } + fswap := gs.GetFswapInit()[0] + if err := fswap.ValidateBasic(); err != nil { return err } - if err := gs.Swapped.Validate(); err != nil { + swapped := gs.GetSwapped()[0] + if err := swapped.Validate(); err != nil { return err } - if gs.FswapInit.AmountLimit.LT(gs.Swapped.NewCoinAmount) { + + if fswap.AmountCapForToDenom.LT(swapped.GetNewCoinAmount().Amount) { return ErrExceedSwappable } return nil diff --git a/x/fswap/types/genesis.pb.go b/x/fswap/types/genesis.pb.go index bc4a73a69f..0dbfc653e0 100644 --- a/x/fswap/types/genesis.pb.go +++ b/x/fswap/types/genesis.pb.go @@ -25,8 +25,8 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // GenesisState defines the fswap module's genesis state. type GenesisState struct { - FswapInit FswapInit `protobuf:"bytes,1,opt,name=fswap_init,json=fswapInit,proto3" json:"fswap_init"` - Swapped Swapped `protobuf:"bytes,2,opt,name=swapped,proto3" json:"swapped"` + FswapInit []FswapInit `protobuf:"bytes,1,rep,name=fswap_init,json=fswapInit,proto3" json:"fswap_init"` + Swapped []Swapped `protobuf:"bytes,2,rep,name=swapped,proto3" json:"swapped"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -62,18 +62,18 @@ func (m *GenesisState) XXX_DiscardUnknown() { var xxx_messageInfo_GenesisState proto.InternalMessageInfo -func (m *GenesisState) GetFswapInit() FswapInit { +func (m *GenesisState) GetFswapInit() []FswapInit { if m != nil { return m.FswapInit } - return FswapInit{} + return nil } -func (m *GenesisState) GetSwapped() Swapped { +func (m *GenesisState) GetSwapped() []Swapped { if m != nil { return m.Swapped } - return Swapped{} + return nil } func init() { @@ -90,15 +90,15 @@ var fileDescriptor_94e309cb1db27661 = []byte{ 0x19, 0x4a, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0x25, 0xf4, 0x41, 0x2c, 0x88, 0x1a, 0x29, 0x09, 0x14, 0xfd, 0x10, 0xc5, 0x60, 0x19, 0xa5, 0x66, 0x46, 0x2e, 0x1e, 0x77, 0x88, 0x79, 0xc1, 0x25, 0x89, 0x25, 0xa9, 0x42, 0x36, 0x5c, 0x5c, 0x60, 0xf9, 0xf8, 0xcc, 0xbc, 0xcc, 0x12, 0x09, 0x46, - 0x05, 0x46, 0x0d, 0x6e, 0x23, 0x71, 0x3d, 0x64, 0x3b, 0xf4, 0xdc, 0x40, 0x0c, 0xcf, 0xbc, 0xcc, + 0x05, 0x66, 0x0d, 0x6e, 0x23, 0x71, 0x3d, 0x64, 0x3b, 0xf4, 0xdc, 0x40, 0x0c, 0xcf, 0xbc, 0xcc, 0x12, 0x27, 0x96, 0x13, 0xf7, 0xe4, 0x19, 0x82, 0x38, 0xd3, 0x60, 0x02, 0x42, 0xa6, 0x5c, 0xec, 0x20, 0x76, 0x41, 0x6a, 0x8a, 0x04, 0x13, 0x58, 0xab, 0x28, 0xaa, 0xd6, 0x60, 0x88, 0x24, 0x54, 0x23, 0x4c, 0xad, 0x93, 0xc7, 0x89, 0x47, 0x72, 0x8c, 0x17, 0x1e, 0xc9, 0x31, 0x3e, 0x78, 0x24, 0xc7, 0x38, 0xe1, 0xb1, 0x1c, 0xc3, 0x85, 0xc7, 0x72, 0x0c, 0x37, 0x1e, 0xcb, 0x31, 0x44, 0xe9, 0xa5, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0xbb, 0x65, 0xe6, 0x15, 0x27, 0x67, 0x64, 0x26, 0xea, 0xa7, 0x41, 0x19, 0xba, 0xc5, 0x29, 0xd9, 0xfa, 0x15, 0x50, 0x8f, 0x95, - 0x54, 0x16, 0xa4, 0x16, 0x27, 0xb1, 0x81, 0xbd, 0x65, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x41, - 0x8c, 0x3c, 0xe3, 0x32, 0x01, 0x00, 0x00, + 0x54, 0x16, 0xa4, 0x16, 0x27, 0xb1, 0x81, 0xbd, 0x65, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x8c, + 0x44, 0xd5, 0xdb, 0x32, 0x01, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -121,26 +121,34 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - { - size, err := m.Swapped.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err + if len(m.Swapped) > 0 { + for iNdEx := len(m.Swapped) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Swapped[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 } - i -= size - i = encodeVarintGenesis(dAtA, i, uint64(size)) } - i-- - dAtA[i] = 0x12 - { - size, err := m.FswapInit.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err + if len(m.FswapInit) > 0 { + for iNdEx := len(m.FswapInit) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.FswapInit[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa } - i -= size - i = encodeVarintGenesis(dAtA, i, uint64(size)) } - i-- - dAtA[i] = 0xa return len(dAtA) - i, nil } @@ -161,10 +169,18 @@ func (m *GenesisState) Size() (n int) { } var l int _ = l - l = m.FswapInit.Size() - n += 1 + l + sovGenesis(uint64(l)) - l = m.Swapped.Size() - n += 1 + l + sovGenesis(uint64(l)) + if len(m.FswapInit) > 0 { + for _, e := range m.FswapInit { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } + if len(m.Swapped) > 0 { + for _, e := range m.Swapped { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } return n } @@ -232,7 +248,8 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.FswapInit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + m.FswapInit = append(m.FswapInit, FswapInit{}) + if err := m.FswapInit[len(m.FswapInit)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex @@ -265,7 +282,8 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.Swapped.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + m.Swapped = append(m.Swapped, Swapped{}) + if err := m.Swapped[len(m.Swapped)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/fswap/types/keys.go b/x/fswap/types/keys.go index 2a56414057..d69f08c9e0 100644 --- a/x/fswap/types/keys.go +++ b/x/fswap/types/keys.go @@ -12,16 +12,4 @@ const ( // QuerierRoute defines the module's query routing key QuerierRoute = ModuleName - - // MemStoreKey defines the in-memory store key - MemStoreKey = "mem_fswap" -) - -var ( - FswapInitKey = byte(0x00) - SwappedKey = byte(0x01) ) - -func KeyPrefix(p string) []byte { - return []byte(p) -} diff --git a/x/fswap/types/msgs.go b/x/fswap/types/msgs.go index 4f4ad81cb5..c3b51da05f 100644 --- a/x/fswap/types/msgs.go +++ b/x/fswap/types/msgs.go @@ -15,26 +15,26 @@ func NewMsgSwapRequest(fromAddr, toAddr sdk.AccAddress, amount sdk.Coin) *MsgSwa } // ValidateBasic Implements Msg. -func (msg MsgSwapRequest) ValidateBasic() error { - _, err := sdk.AccAddressFromBech32(msg.FromAddress) +func (m *MsgSwapRequest) ValidateBasic() error { + _, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid address (%s)", err) } - if !msg.Amount.IsValid() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String()) + if !m.Amount.IsValid() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.Amount.String()) } - if !msg.Amount.IsPositive() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String()) + if !m.Amount.IsPositive() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.Amount.String()) } return nil } // GetSigners Implements Msg. -func (msg MsgSwapRequest) GetSigners() []sdk.AccAddress { - from, err := sdk.AccAddressFromBech32(msg.FromAddress) +func (m *MsgSwapRequest) GetSigners() []sdk.AccAddress { + from, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { panic(err) } @@ -51,8 +51,8 @@ func NewMsgSwapAllRequest(fromAddr, toAddr sdk.AccAddress) *MsgSwapAllRequest { } // ValidateBasic Implements Msg. -func (msg MsgSwapAllRequest) ValidateBasic() error { - _, err := sdk.AccAddressFromBech32(msg.FromAddress) +func (m *MsgSwapAllRequest) ValidateBasic() error { + _, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid address (%s)", err) } @@ -61,8 +61,8 @@ func (msg MsgSwapAllRequest) ValidateBasic() error { } // GetSigners Implements Msg. -func (msg MsgSwapAllRequest) GetSigners() []sdk.AccAddress { - from, err := sdk.AccAddressFromBech32(msg.FromAddress) +func (m *MsgSwapAllRequest) GetSigners() []sdk.AccAddress { + from, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { panic(err) } diff --git a/x/fswap/types/proposal.go b/x/fswap/types/proposal.go index fcd15e444f..1a0cfca990 100644 --- a/x/fswap/types/proposal.go +++ b/x/fswap/types/proposal.go @@ -25,21 +25,21 @@ func init() { } // ProposalRoute gets the proposal's router key -func (fip *FswapInitProposal) ProposalRoute() string { return RouterKey } +func (m *FswapInitProposal) ProposalRoute() string { return RouterKey } // ProposalType is "FswapInit" -func (fip *FswapInitProposal) ProposalType() string { return ProposalTypeFswapInit } +func (m *FswapInitProposal) ProposalType() string { return ProposalTypeFswapInit } // String implements the Stringer interface. -func (fip *FswapInitProposal) String() string { - out, _ := yaml.Marshal(fip) +func (m *FswapInitProposal) String() string { + out, _ := yaml.Marshal(m) return string(out) } // ValidateBasic validates the proposal -func (fip *FswapInitProposal) ValidateBasic() error { - if err := fip.FswapInit.ValidateBasic(); err != nil { +func (m *FswapInitProposal) ValidateBasic() error { + if err := m.FswapInit.ValidateBasic(); err != nil { return err } - return gov.ValidateAbstract(fip) + return gov.ValidateAbstract(m) } diff --git a/x/fswap/types/query.pb.go b/x/fswap/types/query.pb.go index 67643e704b..8a69ac7609 100644 --- a/x/fswap/types/query.pb.go +++ b/x/fswap/types/query.pb.go @@ -67,8 +67,8 @@ func (m *QuerySwappedRequest) XXX_DiscardUnknown() { var xxx_messageInfo_QuerySwappedRequest proto.InternalMessageInfo type QuerySwappedResponse struct { - OldCoin types.Coin `protobuf:"bytes,1,opt,name=old_coin,json=oldCoin,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"old_coin"` - NewCoin types.Coin `protobuf:"bytes,2,opt,name=new_coin,json=newCoin,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"new_coin"` + OldCoinAmount types.Coin `protobuf:"bytes,1,opt,name=old_coin_amount,json=oldCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"old_coin_amount"` + NewCoinAmount types.Coin `protobuf:"bytes,2,opt,name=new_coin_amount,json=newCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"new_coin_amount"` } func (m *QuerySwappedResponse) Reset() { *m = QuerySwappedResponse{} } @@ -104,16 +104,16 @@ func (m *QuerySwappedResponse) XXX_DiscardUnknown() { var xxx_messageInfo_QuerySwappedResponse proto.InternalMessageInfo -func (m *QuerySwappedResponse) GetOldCoin() types.Coin { +func (m *QuerySwappedResponse) GetOldCoinAmount() types.Coin { if m != nil { - return m.OldCoin + return m.OldCoinAmount } return types.Coin{} } -func (m *QuerySwappedResponse) GetNewCoin() types.Coin { +func (m *QuerySwappedResponse) GetNewCoinAmount() types.Coin { if m != nil { - return m.NewCoin + return m.NewCoinAmount } return types.Coin{} } @@ -208,36 +208,35 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/query.proto", fileDescriptor_01deae9da7816d6a) } var fileDescriptor_01deae9da7816d6a = []byte{ - // 449 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0xc1, 0x6a, 0xd4, 0x40, - 0x18, 0xc7, 0x33, 0x0b, 0x5a, 0x19, 0x3d, 0xc8, 0xd8, 0x62, 0x1b, 0x34, 0x6d, 0x73, 0x51, 0x11, - 0x67, 0x48, 0xfb, 0x04, 0x56, 0x10, 0x0f, 0x22, 0x58, 0x3d, 0x79, 0x59, 0x26, 0xc9, 0x34, 0x1d, - 0x4c, 0xe6, 0x4b, 0x33, 0x93, 0x8d, 0x05, 0x4f, 0x3e, 0x81, 0xe0, 0x13, 0x78, 0x13, 0xc1, 0xf7, - 0xe8, 0xb1, 0xe0, 0xc5, 0x93, 0xca, 0xae, 0x47, 0x1f, 0x42, 0x66, 0x32, 0x5d, 0x0c, 0x2c, 0xab, - 0x97, 0xbd, 0x7d, 0xe4, 0xfb, 0x66, 0x7e, 0xff, 0xef, 0xff, 0x9f, 0xe0, 0xcd, 0x32, 0xad, 0xd8, - 0x91, 0xee, 0x78, 0xcd, 0x26, 0x09, 0x3b, 0x69, 0x45, 0x73, 0x4a, 0xeb, 0x06, 0x0c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0x55, 0xe6, 0xc2, 0x92, 0x8f, 0x08, 0xef, 0x2e, 0x19, 0xf2, 0xfe, 0xbc, 0xc5, 0x44, - 0x5f, 0xb4, 0xc6, 0x73, 0xf9, 0xab, 0x71, 0xea, 0xfa, 0x9c, 0xf4, 0xac, 0xdf, 0x63, 0xef, 0xd3, - 0x08, 0x5f, 0x72, 0x1a, 0x09, 0xe0, 0x35, 0x1f, 0x1d, 0xd9, 0xa5, 0x7f, 0xbf, 0x2f, 0xba, 0x20, - 0xee, 0x30, 0x5e, 0x36, 0xd2, 0x6f, 0x16, 0xdf, 0x7e, 0xf7, 0xf5, 0xd7, 0x87, 0xd1, 0x4d, 0xb2, - 0xc1, 0x06, 0x8f, 0x49, 0x7b, 0xca, 0x17, 0x84, 0xb7, 0x9c, 0x33, 0x56, 0x4b, 0xdb, 0x34, 0x42, - 0x65, 0xee, 0x8a, 0xa7, 0xb2, 0x92, 0x86, 0xd0, 0x05, 0x80, 0x25, 0x66, 0x87, 0xec, 0xbf, 0xe7, - 0xbd, 0x3a, 0xe6, 0xd4, 0xdd, 0x23, 0x77, 0x16, 0xa8, 0x1b, 0x64, 0x31, 0xe6, 0xee, 0xe0, 0xc1, - 0x93, 0xb3, 0x69, 0x84, 0xce, 0xa7, 0x11, 0xfa, 0x39, 0x8d, 0xd0, 0xfb, 0x59, 0x14, 0x9c, 0xcf, - 0xa2, 0xe0, 0xdb, 0x2c, 0x0a, 0x5e, 0xd1, 0x7f, 0x66, 0xf0, 0xc6, 0x03, 0x5c, 0x16, 0xe9, 0x65, - 0xf7, 0x27, 0xed, 0xff, 0x09, 0x00, 0x00, 0xff, 0xff, 0x91, 0x66, 0x98, 0xa4, 0xe1, 0x03, 0x00, - 0x00, + // 441 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0xc1, 0xaa, 0xd3, 0x40, + 0x14, 0x86, 0x93, 0x80, 0x0a, 0xa3, 0xa2, 0x8c, 0xbd, 0x78, 0x0d, 0x9a, 0x7b, 0x6f, 0x36, 0x2a, + 0xe2, 0x0c, 0xb9, 0xf7, 0x09, 0xac, 0x20, 0xae, 0x04, 0xab, 0x2b, 0x37, 0x65, 0x92, 0x4c, 0xd3, + 0x60, 0x32, 0x27, 0xed, 0x4c, 0x12, 0x0b, 0xae, 0x5c, 0xb8, 0x16, 0x7c, 0x02, 0x71, 0x23, 0x3e, + 0x49, 0x97, 0x05, 0x37, 0xae, 0x54, 0x5a, 0x1f, 0x44, 0x32, 0x99, 0xaa, 0x81, 0x50, 0xdd, 0x74, + 0x77, 0x98, 0xff, 0x64, 0xbe, 0x7f, 0xfe, 0x73, 0x82, 0x0e, 0xb3, 0x30, 0xa7, 0x13, 0x59, 0xb3, + 0x82, 0x56, 0x01, 0x9d, 0x95, 0x7c, 0xbe, 0x20, 0xc5, 0x1c, 0x14, 0xe0, 0x4b, 0x59, 0x98, 0x13, + 0xad, 0x90, 0x2a, 0x70, 0x6f, 0x26, 0x00, 0x49, 0xc6, 0x29, 0x2b, 0x52, 0xca, 0x84, 0x00, 0xc5, + 0x54, 0x0a, 0x42, 0xb6, 0xbd, 0xee, 0x20, 0x81, 0x04, 0x74, 0x49, 0x9b, 0xca, 0x9c, 0x7a, 0x11, + 0xc8, 0x1c, 0x24, 0x0d, 0x99, 0xe4, 0xb4, 0x0a, 0x42, 0xae, 0x58, 0x40, 0x23, 0x48, 0x85, 0xd1, + 0xbb, 0xec, 0x16, 0xa5, 0x15, 0xff, 0x00, 0x5d, 0x7b, 0xda, 0x58, 0x79, 0x56, 0xb3, 0xa2, 0xe0, + 0xf1, 0x88, 0xcf, 0x4a, 0x2e, 0x95, 0xff, 0xd6, 0x41, 0x83, 0xee, 0xb9, 0x2c, 0x40, 0x48, 0x8e, + 0x2b, 0x74, 0x05, 0xb2, 0x78, 0xdc, 0xdc, 0x3d, 0x66, 0x39, 0x94, 0x42, 0x1d, 0xda, 0xc7, 0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0c, 0x66, 0xa5, 0x86, 0x8f, 0x97, 0x6b, 0xcf, 0x5e, 0xad, 0x3d, 0xfb, 0xc7, + 0xda, 0xb3, 0xdf, 0x6d, 0x3c, 0x6b, 0xb5, 0xf1, 0xac, 0xaf, 0x1b, 0xcf, 0x7a, 0x41, 0xfe, 0x19, + 0xff, 0x2b, 0x03, 0xd0, 0x63, 0x08, 0xcf, 0xeb, 0xdf, 0xeb, 0xec, 0x57, 0x00, 0x00, 0x00, 0xff, + 0xff, 0x63, 0x79, 0x56, 0x3c, 0xf6, 0x03, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -252,8 +251,11 @@ const _ = grpc.SupportPackageIsVersion4 // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. type QueryClient interface { + // Swapped queries the current swapped status that includes a burnt amount of old coin and a minted amount of new + // coin. Swapped(ctx context.Context, in *QuerySwappedRequest, opts ...grpc.CallOption) (*QuerySwappedResponse, error) - TotalNewCurrencySwapLimit(ctx context.Context, in *QueryTotalSwappableAmountRequest, opts ...grpc.CallOption) (*QueryTotalSwappableAmountResponse, error) + // TotalSwappableAmount queries the current swappable amount for new coin. + TotalSwappableAmount(ctx context.Context, in *QueryTotalSwappableAmountRequest, opts ...grpc.CallOption) (*QueryTotalSwappableAmountResponse, error) } type queryClient struct { @@ -273,9 +275,9 @@ func (c *queryClient) Swapped(ctx context.Context, in *QuerySwappedRequest, opts return out, nil } -func (c *queryClient) TotalNewCurrencySwapLimit(ctx context.Context, in *QueryTotalSwappableAmountRequest, opts ...grpc.CallOption) (*QueryTotalSwappableAmountResponse, error) { +func (c *queryClient) TotalSwappableAmount(ctx context.Context, in *QueryTotalSwappableAmountRequest, opts ...grpc.CallOption) (*QueryTotalSwappableAmountResponse, error) { out := new(QueryTotalSwappableAmountResponse) - err := c.cc.Invoke(ctx, "/lbm.fswap.v1.Query/TotalNewCurrencySwapLimit", in, out, opts...) + err := c.cc.Invoke(ctx, "/lbm.fswap.v1.Query/TotalSwappableAmount", in, out, opts...) if err != nil { return nil, err } @@ -284,8 +286,11 @@ func (c *queryClient) TotalNewCurrencySwapLimit(ctx context.Context, in *QueryTo // QueryServer is the server API for Query service. type QueryServer interface { + // Swapped queries the current swapped status that includes a burnt amount of old coin and a minted amount of new + // coin. Swapped(context.Context, *QuerySwappedRequest) (*QuerySwappedResponse, error) - TotalNewCurrencySwapLimit(context.Context, *QueryTotalSwappableAmountRequest) (*QueryTotalSwappableAmountResponse, error) + // TotalSwappableAmount queries the current swappable amount for new coin. + TotalSwappableAmount(context.Context, *QueryTotalSwappableAmountRequest) (*QueryTotalSwappableAmountResponse, error) } // UnimplementedQueryServer can be embedded to have forward compatible implementations. @@ -295,8 +300,8 @@ type UnimplementedQueryServer struct { func (*UnimplementedQueryServer) Swapped(ctx context.Context, req *QuerySwappedRequest) (*QuerySwappedResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method Swapped not implemented") } -func (*UnimplementedQueryServer) TotalNewCurrencySwapLimit(ctx context.Context, req *QueryTotalSwappableAmountRequest) (*QueryTotalSwappableAmountResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method TotalNewCurrencySwapLimit not implemented") +func (*UnimplementedQueryServer) TotalSwappableAmount(ctx context.Context, req *QueryTotalSwappableAmountRequest) (*QueryTotalSwappableAmountResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method TotalSwappableAmount not implemented") } func RegisterQueryServer(s grpc1.Server, srv QueryServer) { @@ -321,20 +326,20 @@ func _Query_Swapped_Handler(srv interface{}, ctx context.Context, dec func(inter return interceptor(ctx, in, info, handler) } -func _Query_TotalNewCurrencySwapLimit_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { +func _Query_TotalSwappableAmount_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(QueryTotalSwappableAmountRequest) if err := dec(in); err != nil { return nil, err } if interceptor == nil { - return srv.(QueryServer).TotalNewCurrencySwapLimit(ctx, in) + return srv.(QueryServer).TotalSwappableAmount(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, - FullMethod: "/lbm.fswap.v1.Query/TotalNewCurrencySwapLimit", + FullMethod: "/lbm.fswap.v1.Query/TotalSwappableAmount", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(QueryServer).TotalNewCurrencySwapLimit(ctx, req.(*QueryTotalSwappableAmountRequest)) + return srv.(QueryServer).TotalSwappableAmount(ctx, req.(*QueryTotalSwappableAmountRequest)) } return interceptor(ctx, in, info, handler) } @@ -348,8 +353,8 @@ var _Query_serviceDesc = grpc.ServiceDesc{ Handler: _Query_Swapped_Handler, }, { - MethodName: "TotalNewCurrencySwapLimit", - Handler: _Query_TotalNewCurrencySwapLimit_Handler, + MethodName: "TotalSwappableAmount", + Handler: _Query_TotalSwappableAmount_Handler, }, }, Streams: []grpc.StreamDesc{}, @@ -400,7 +405,7 @@ func (m *QuerySwappedResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size, err := m.NewCoin.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.NewCoinAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -410,7 +415,7 @@ func (m *QuerySwappedResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x12 { - size, err := m.OldCoin.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.OldCoinAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -504,9 +509,9 @@ func (m *QuerySwappedResponse) Size() (n int) { } var l int _ = l - l = m.OldCoin.Size() + l = m.OldCoinAmount.Size() n += 1 + l + sovQuery(uint64(l)) - l = m.NewCoin.Size() + l = m.NewCoinAmount.Size() n += 1 + l + sovQuery(uint64(l)) return n } @@ -618,7 +623,7 @@ func (m *QuerySwappedResponse) Unmarshal(dAtA []byte) error { switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field OldCoin", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field OldCoinAmount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -645,13 +650,13 @@ func (m *QuerySwappedResponse) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.OldCoin.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.OldCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field NewCoin", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field NewCoinAmount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -678,7 +683,7 @@ func (m *QuerySwappedResponse) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.NewCoin.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.NewCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/fswap/types/query.pb.gw.go b/x/fswap/types/query.pb.gw.go index 5a63817f6a..fb7cadac5d 100644 --- a/x/fswap/types/query.pb.gw.go +++ b/x/fswap/types/query.pb.gw.go @@ -49,20 +49,20 @@ func local_request_Query_Swapped_0(ctx context.Context, marshaler runtime.Marsha } -func request_Query_TotalNewCurrencySwapLimit_0(ctx context.Context, marshaler runtime.Marshaler, client QueryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { +func request_Query_TotalSwappableAmount_0(ctx context.Context, marshaler runtime.Marshaler, client QueryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq QueryTotalSwappableAmountRequest var metadata runtime.ServerMetadata - msg, err := client.TotalNewCurrencySwapLimit(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + msg, err := client.TotalSwappableAmount(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) return msg, metadata, err } -func local_request_Query_TotalNewCurrencySwapLimit_0(ctx context.Context, marshaler runtime.Marshaler, server QueryServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { +func local_request_Query_TotalSwappableAmount_0(ctx context.Context, marshaler runtime.Marshaler, server QueryServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq QueryTotalSwappableAmountRequest var metadata runtime.ServerMetadata - msg, err := server.TotalNewCurrencySwapLimit(ctx, &protoReq) + msg, err := server.TotalSwappableAmount(ctx, &protoReq) return msg, metadata, err } @@ -93,7 +93,7 @@ func RegisterQueryHandlerServer(ctx context.Context, mux *runtime.ServeMux, serv }) - mux.Handle("GET", pattern_Query_TotalNewCurrencySwapLimit_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + mux.Handle("GET", pattern_Query_TotalSwappableAmount_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { ctx, cancel := context.WithCancel(req.Context()) defer cancel() inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) @@ -102,14 +102,14 @@ func RegisterQueryHandlerServer(ctx context.Context, mux *runtime.ServeMux, serv runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) return } - resp, md, err := local_request_Query_TotalNewCurrencySwapLimit_0(rctx, inboundMarshaler, server, req, pathParams) + resp, md, err := local_request_Query_TotalSwappableAmount_0(rctx, inboundMarshaler, server, req, pathParams) ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) return } - forward_Query_TotalNewCurrencySwapLimit_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + forward_Query_TotalSwappableAmount_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) }) @@ -174,7 +174,7 @@ func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, clie }) - mux.Handle("GET", pattern_Query_TotalNewCurrencySwapLimit_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + mux.Handle("GET", pattern_Query_TotalSwappableAmount_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { ctx, cancel := context.WithCancel(req.Context()) defer cancel() inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) @@ -183,14 +183,14 @@ func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, clie runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) return } - resp, md, err := request_Query_TotalNewCurrencySwapLimit_0(rctx, inboundMarshaler, client, req, pathParams) + resp, md, err := request_Query_TotalSwappableAmount_0(rctx, inboundMarshaler, client, req, pathParams) ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) return } - forward_Query_TotalNewCurrencySwapLimit_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + forward_Query_TotalSwappableAmount_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) }) @@ -200,11 +200,11 @@ func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, clie var ( pattern_Query_Swapped_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"lbm", "fswap", "v1", "swapped"}, "", runtime.AssumeColonVerbOpt(false))) - pattern_Query_TotalNewCurrencySwapLimit_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"lbm", "fswap", "v1", "swappable_new_coin_amount"}, "", runtime.AssumeColonVerbOpt(false))) + pattern_Query_TotalSwappableAmount_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"lbm", "fswap", "v1", "swappable_new_coin_amount"}, "", runtime.AssumeColonVerbOpt(false))) ) var ( forward_Query_Swapped_0 = runtime.ForwardResponseMessage - forward_Query_TotalNewCurrencySwapLimit_0 = runtime.ForwardResponseMessage + forward_Query_TotalSwappableAmount_0 = runtime.ForwardResponseMessage ) diff --git a/x/fswap/types/types.go b/x/fswap/types/types.go deleted file mode 100644 index ab1254f4c2..0000000000 --- a/x/fswap/types/types.go +++ /dev/null @@ -1 +0,0 @@ -package types From f1e68111faaa705b06d6ead288f5119f96442159 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Mon, 29 Apr 2024 23:01:35 +0900 Subject: [PATCH 10/47] chore: update genesis logic --- x/fswap/keeper/fswap.go | 94 +++++++++++++++++++++++++++------- x/fswap/keeper/genesis.go | 32 ++++++++---- x/fswap/keeper/genesis_test.go | 14 +++-- x/fswap/keeper/grpc_query.go | 67 +++++++++++++++++------- x/fswap/keeper/keeper.go | 11 +++- x/fswap/keeper/keeper_test.go | 2 +- x/fswap/keeper/keys.go | 15 ++++++ x/fswap/keeper/msg_server.go | 47 ++++++++++++----- x/fswap/module.go | 13 ++--- 9 files changed, 221 insertions(+), 74 deletions(-) create mode 100644 x/fswap/keeper/keys.go diff --git a/x/fswap/keeper/fswap.go b/x/fswap/keeper/fswap.go index f673b73c41..a3cdb16b49 100644 --- a/x/fswap/keeper/fswap.go +++ b/x/fswap/keeper/fswap.go @@ -1,53 +1,109 @@ package keeper import ( + "github.com/Finschia/finschia-sdk/store/prefix" sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/fswap/types" ) -// GetSwapped get all parameters as types.Swapped -func (k Keeper) GetSwapped(ctx sdk.Context) types.Swapped { +func (k Keeper) GetSwapped(ctx sdk.Context, toDenom string) (types.Swapped, error) { store := ctx.KVStore(k.storeKey) - bz := store.Get([]byte{types.SwappedKey}) - var swapped types.Swapped - + key := swappedKey(toDenom) + bz := store.Get(key) if bz == nil { - panic(types.ErrSwappedNotFound) + return types.Swapped{ + OldCoinAmount: sdk.Coin{Denom: "", Amount: sdk.ZeroInt()}, + NewCoinAmount: sdk.Coin{Denom: "", Amount: sdk.ZeroInt()}, + }, nil + } + swapped := types.Swapped{} + if err := k.cdc.Unmarshal(bz, &swapped); err != nil { + return types.Swapped{}, err + } + return swapped, nil +} + +func (k Keeper) GetAllSwapped(ctx sdk.Context) []types.Swapped { + swappedSlice := make([]types.Swapped, 0) + k.IterateAllSwapped(ctx, func(fswapInit types.Swapped) bool { + swappedSlice = append(swappedSlice, fswapInit) + return false + }) + return swappedSlice +} + +func (k Keeper) IterateAllSwapped(ctx sdk.Context, cb func(swapped types.Swapped) (stop bool)) { + store := ctx.KVStore(k.storeKey) + swappedDataStore := prefix.NewStore(store, swappedKeyPrefix) + + iterator := swappedDataStore.Iterator(nil, nil) + defer iterator.Close() + + for ; iterator.Valid(); iterator.Next() { + swapped := types.Swapped{} + k.cdc.MustUnmarshal(iterator.Value(), &swapped) + if cb(swapped) { + break + } } - k.cdc.MustUnmarshal(bz, &swapped) - return swapped } -// SetSwapped set the types.Swapped func (k Keeper) SetSwapped(ctx sdk.Context, swapped types.Swapped) error { store := ctx.KVStore(k.storeKey) + key := swappedKey(swapped.NewCoinAmount.Denom) bz, err := k.cdc.Marshal(&swapped) if err != nil { return err } - store.Set([]byte{types.SwappedKey}, bz) + store.Set(key, bz) return nil } -// GetFswapInit get all parameters as types.FswapInit -func (k Keeper) GetFswapInit(ctx sdk.Context) types.FswapInit { +func (k Keeper) GetFswapInit(ctx sdk.Context, toDenom string) (types.FswapInit, error) { store := ctx.KVStore(k.storeKey) - bz := store.Get([]byte{types.FswapInitKey}) - var fswapInit types.FswapInit + bz := store.Get(fswapInitKey(toDenom)) if bz == nil { - panic(types.ErrFswapInitNotFound) + return types.FswapInit{}, types.ErrFswapInitNotFound + } + fswapInit := types.FswapInit{} + err := k.cdc.Unmarshal(bz, &fswapInit) + if err != nil { + return types.FswapInit{}, err + } + return fswapInit, nil +} + +func (k Keeper) GetAllFswapInits(ctx sdk.Context) []types.FswapInit { + fswapInits := make([]types.FswapInit, 0) + k.IterateAllFswapInits(ctx, func(fswapInit types.FswapInit) bool { + fswapInits = append(fswapInits, fswapInit) + return false + }) + return fswapInits +} + +func (k Keeper) IterateAllFswapInits(ctx sdk.Context, cb func(swapped types.FswapInit) (stop bool)) { + store := ctx.KVStore(k.storeKey) + fswapInitDataStore := prefix.NewStore(store, fswapInitPrefix) + + iterator := fswapInitDataStore.Iterator(nil, nil) + defer iterator.Close() + + for ; iterator.Valid(); iterator.Next() { + fswapInit := types.FswapInit{} + k.cdc.MustUnmarshal(iterator.Value(), &fswapInit) + if cb(fswapInit) { + break + } } - k.cdc.MustUnmarshal(bz, &fswapInit) - return fswapInit } -// SetfswapInit set the fswapInit func (k Keeper) SetFswapInit(ctx sdk.Context, fswapInit types.FswapInit) error { store := ctx.KVStore(k.storeKey) bz, err := k.cdc.Marshal(&fswapInit) if err != nil { return err } - store.Set([]byte{types.FswapInitKey}, bz) + store.Set(fswapInitKey(fswapInit.ToDenom), bz) return nil } diff --git a/x/fswap/keeper/genesis.go b/x/fswap/keeper/genesis.go index 82a346a671..1fb6929dea 100644 --- a/x/fswap/keeper/genesis.go +++ b/x/fswap/keeper/genesis.go @@ -1,25 +1,37 @@ package keeper import ( + "fmt" + sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/fswap/types" ) -// InitGenesis initializes the module's state from a provided genesis -// state. -func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { - if err := k.SetFswapInit(ctx, genState.FswapInit); err != nil { - panic(err) +// InitGenesis initializes the module's state from a provided genesis state. +func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error { + if len(genState.GetFswapInit()) > 1 { + return fmt.Errorf("cannot initialize genesis state, there are more than 1 fswapInit") + } + if len(genState.GetSwapped()) > 1 { + return fmt.Errorf("cannot initialize genesis state, there are more than 1 swapped") + } + for _, fswapInit := range genState.GetFswapInit() { + if err := k.SetFswapInit(ctx, fswapInit); err != nil { + panic(err) + } } - if err := k.SetSwapped(ctx, genState.Swapped); err != nil { - panic(err) + for _, swapped := range genState.GetSwapped() { + if err := swapped.Validate(); err != nil { + panic(err) + } } + return nil } -// ExportGenesis returns the capability module's exported genesis. +// ExportGenesis returns the module's exported genesis. func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { return &types.GenesisState{ - FswapInit: k.GetFswapInit(ctx), - Swapped: k.GetSwapped(ctx), + FswapInit: k.GetAllFswapInits(ctx), + Swapped: k.GetAllSwapped(ctx), } } diff --git a/x/fswap/keeper/genesis_test.go b/x/fswap/keeper/genesis_test.go index 26f6779af6..483078a433 100644 --- a/x/fswap/keeper/genesis_test.go +++ b/x/fswap/keeper/genesis_test.go @@ -5,9 +5,13 @@ import ( ) func (s *KeeperTestSuite) TestInitAndExportGenesis() { - s.keeper.InitGenesis(s.sdkCtx, *types.DefaultGenesis()) - got := s.keeper.ExportGenesis(s.sdkCtx) - s.Require().NotNil(got) - s.Require().Equal(types.DefaultFswapInit(), got.FswapInit) - s.Require().Equal(types.DefaultSwapped(), got.Swapped) + ctx, _ := s.sdkCtx.CacheContext() + defaultGenesis := types.DefaultGenesis() + err := s.keeper.InitGenesis(ctx, defaultGenesis) + s.Require().NoError(err) + + exportGenesis := s.keeper.ExportGenesis(ctx) + s.Require().Equal(defaultGenesis, exportGenesis) + s.Require().Equal(defaultGenesis.GetFswapInit(), exportGenesis.GetFswapInit()) + s.Require().Equal(defaultGenesis.GetSwapped(), exportGenesis.GetSwapped()) } diff --git a/x/fswap/keeper/grpc_query.go b/x/fswap/keeper/grpc_query.go index b7ba56a746..ed416a1f4d 100644 --- a/x/fswap/keeper/grpc_query.go +++ b/x/fswap/keeper/grpc_query.go @@ -7,28 +7,59 @@ import ( "github.com/Finschia/finschia-sdk/x/fswap/types" ) -var _ types.QueryServer = Keeper{} +//var _ types.QueryServer = Keeper{} +// +//// todo this files is just for test +//// Swapped implements types.QueryServer. +//func (k Keeper) Swapped(ctx context.Context, req *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) { +// sdkCtx := sdk.UnwrapSDKContext(ctx) +// check := k.GetFswap(sdkCtx) +// +// return &types.QuerySwappedResponse{ +// OldCoinAmount: sdk.NewCoin(check.FromDenom, sdk.ZeroInt()), +// NewCoinAmount: sdk.NewCoin(check.ToDenom, sdk.ZeroInt()), +// }, nil +//} +// +//// TotalSwappableAmount implements types.QueryServer. +//func (k Keeper) TotalSwappableAmount(context.Context, *types.QueryTotalSwappableAmountRequest) (*types.QueryTotalSwappableAmountResponse, error) { +// //TODO implement me +// panic("unimplemented") +// // sdkCtx := sdk.UnwrapSDKContext(ctx) +// // totalSwappableAmount := k.GetParams(sdkCtx).SwappableNewCoinAmount +// // swapped := k.GetSwapped(sdkCtx) +// // swappableNewCoin := sdk.NewCoin(k.config.NewCoinDenom, totalSwappableAmount.Sub(swapped.NewCoinAmount)) +// // return &types.QueryTotalSwappableAmountResponse{ +// // SwappableNewCoin: swappableNewCoin, +// // }, nil +//} -// todo this files is just for test -// Swapped implements types.QueryServer. -func (k Keeper) Swapped(ctx context.Context, req *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) { - sdkCtx := sdk.UnwrapSDKContext(ctx) - check := k.GetFswapInit(sdkCtx) +var _ types.QueryServer = QueryServer{} +type QueryServer struct { + Keeper +} + +func NewQueryServer(keeper Keeper) *QueryServer { + return &QueryServer{ + keeper, + } +} + +func (s QueryServer) Swapped(ctx context.Context, _ *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) { + c := sdk.UnwrapSDKContext(ctx) + allSwapped := s.Keeper.GetAllSwapped(c) + if len(allSwapped) == 0 { + return nil, types.ErrSwappedNotFound + } + swapped := allSwapped[0] return &types.QuerySwappedResponse{ - OldCoin: sdk.NewCoin(check.FromDenom, sdk.ZeroInt()), - NewCoin: sdk.NewCoin(check.ToDenom, sdk.ZeroInt()), + OldCoinAmount: swapped.GetOldCoinAmount(), + NewCoinAmount: swapped.GetNewCoinAmount(), }, nil } -// TotalNewCurrencySwapLimit implements types.QueryServer. -func (k Keeper) TotalNewCurrencySwapLimit(context.Context, *types.QueryTotalSwappableAmountRequest) (*types.QueryTotalSwappableAmountResponse, error) { - panic("unimplemented") - // sdkCtx := sdk.UnwrapSDKContext(ctx) - // totalSwappableAmount := k.GetParams(sdkCtx).SwappableNewCoinAmount - // swapped := k.GetSwapped(sdkCtx) - // swappableNewCoin := sdk.NewCoin(k.config.NewCoinDenom, totalSwappableAmount.Sub(swapped.NewCoinAmount)) - // return &types.QueryTotalSwappableAmountResponse{ - // SwappableNewCoin: swappableNewCoin, - // }, nil +func (s QueryServer) TotalSwappableAmount(_ context.Context, _ *types.QueryTotalSwappableAmountRequest) (*types.QueryTotalSwappableAmountResponse, error) { + // s.Keeper.GetSwappableNewCoinAmount() + return nil, nil } diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 85c4c34269..753244c0f2 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -50,7 +50,16 @@ func (k Keeper) FswapInit(ctx sdk.Context, fswapInit types.FswapInit) error { return err } // need confirm: Is Swapped use sdk.Coin or sdk.Int - swapped := types.NewSwapped(sdk.ZeroInt(), sdk.ZeroInt()) + swapped := types.Swapped{ + OldCoinAmount: sdk.Coin{ + Denom: "", + Amount: sdk.ZeroInt(), + }, + NewCoinAmount: sdk.Coin{ + Denom: "", + Amount: sdk.ZeroInt(), + }, + } if err := k.SetSwapped(ctx, swapped); err != nil { return err } diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index dc87a7e267..47ef1bc592 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -39,5 +39,5 @@ func (s *KeeperTestSuite) SetupTest() { s.goCtx = sdk.WrapSDKContext(s.sdkCtx) s.keeper = app.FswapKeeper - s.msgServer = keeper.NewMsgServerImpl(s.keeper) + s.msgServer = keeper.NewMsgServer(s.keeper) } diff --git a/x/fswap/keeper/keys.go b/x/fswap/keeper/keys.go new file mode 100644 index 0000000000..39cedce5d2 --- /dev/null +++ b/x/fswap/keeper/keys.go @@ -0,0 +1,15 @@ +package keeper + +var ( + fswapInitPrefix = []byte{0x01} + swappedKeyPrefix = []byte{0x02} +) + +// fswapInitKey key(prefix + toDenom) +func fswapInitKey(toDenom string) []byte { + return append(fswapInitPrefix, toDenom...) +} + +func swappedKey(toDenom string) []byte { + return append(swappedKeyPrefix, toDenom...) +} diff --git a/x/fswap/keeper/msg_server.go b/x/fswap/keeper/msg_server.go index 41b76b832a..1a1703012b 100644 --- a/x/fswap/keeper/msg_server.go +++ b/x/fswap/keeper/msg_server.go @@ -6,24 +6,43 @@ import ( "github.com/Finschia/finschia-sdk/x/fswap/types" ) -type msgServer struct { - Keeper -} +var _ types.MsgServer = MsgServer{} -// NewMsgServerImpl returns an implementation of the MsgServer interface -// for the provided Keeper. -func NewMsgServerImpl(keeper Keeper) types.MsgServer { - return &msgServer{Keeper: keeper} +type MsgServer struct { + keeper Keeper } -var _ types.MsgServer = msgServer{} +func NewMsgServer(keeper Keeper) *MsgServer { + return &MsgServer{keeper} +} -// Swap implements types.MsgServer. -func (m msgServer) Swap(context.Context, *types.MsgSwapRequest) (*types.MsgSwapResponse, error) { - panic("unimplemented") +func (s MsgServer) Swap(ctx context.Context, req *types.MsgSwapRequest) (*types.MsgSwapResponse, error) { + //c := sdk.UnwrapSDKContext(ctx) + //swapped, err := s.keeper.GetSwapped(c) + //if err != nil { + // return nil, err + //} + //if req.GetAmount().Denom != swapped.OldCoinAmount.Denom { + // return nil, sdkerrors.ErrInvalidCoins + //} + //from, err := sdk.AccAddressFromBech32(req.FromAddress) + //if err != nil { + // return nil, err + //} + //if err := s.keeper.Swap(c, from, req.GetAmount()); err != nil { + // return nil, err + //} + return &types.MsgSwapResponse{}, nil } -// SwapAll implements types.MsgServer. -func (m msgServer) SwapAll(context.Context, *types.MsgSwapAllRequest) (*types.MsgSwapAllResponse, error) { - panic("unimplemented") +func (s MsgServer) SwapAll(ctx context.Context, req *types.MsgSwapAllRequest) (*types.MsgSwapAllResponse, error) { + //c := sdk.UnwrapSDKContext(ctx) + //from, err := sdk.AccAddressFromBech32(req.FromAddress) + //if err != nil { + // return nil, err + //} + //if err := s.keeper.SwapAll(c, from); err != nil { + // return nil, err + //} + return &types.MsgSwapAllResponse{}, nil } diff --git a/x/fswap/module.go b/x/fswap/module.go index 724b883a9e..db1f2415f0 100644 --- a/x/fswap/module.go +++ b/x/fswap/module.go @@ -61,7 +61,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { } // ValidateGenesis performs genesis state validation for the capability module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error { var genState types.GenesisState if err := cdc.UnmarshalJSON(bz, &genState); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) @@ -132,7 +132,8 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd // RegisterServices registers a GRPC query service to respond to the // module-specific GRPC queries. func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterQueryServer(cfg.QueryServer(), am.keeper) + types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper)) + types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(am.keeper)) } // RegisterInvariants registers the capability module's invariants. @@ -141,12 +142,12 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // InitGenesis performs the capability module's genesis initialization It returns // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) []abci.ValidatorUpdate { - var genState types.GenesisState + genState := types.GenesisState{} // Initialize global index to index in genesis state cdc.MustUnmarshalJSON(gs, &genState) - - am.keeper.InitGenesis(ctx, genState) - + if err := am.keeper.InitGenesis(ctx, &genState); err != nil { + panic(err) + } return []abci.ValidatorUpdate{} } From 427da10acbbffac6f90a461a5823b57c4816764d Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Tue, 30 Apr 2024 14:03:10 +0900 Subject: [PATCH 11/47] chore: add basic tx and queries --- simapp/app.go | 1 + x/fswap/keeper/expected_keepers.go | 19 ++ x/fswap/keeper/fswap.go | 109 ------- x/fswap/keeper/genesis.go | 6 +- x/fswap/keeper/genesis_test.go | 2 +- x/fswap/keeper/grpc_query.go | 46 +-- x/fswap/keeper/grpc_query_params_test.go | 1 - x/fswap/keeper/keeper.go | 328 +++++++++++++++++++-- x/fswap/keeper/keeper_test.go | 176 ++++++++++- x/fswap/keeper/keys.go | 10 +- x/fswap/keeper/msg_server.go | 51 ++-- x/fswap/module.go | 10 +- x/fswap/testutil/expected_keepers_mocks.go | 85 ++++-- x/fswap/types/errors.go | 7 +- x/fswap/types/expected_keepers.go | 19 -- 15 files changed, 608 insertions(+), 262 deletions(-) create mode 100644 x/fswap/keeper/expected_keepers.go delete mode 100644 x/fswap/keeper/fswap.go delete mode 100644 x/fswap/keeper/grpc_query_params_test.go delete mode 100644 x/fswap/types/expected_keepers.go diff --git a/simapp/app.go b/simapp/app.go index 5cfa422693..915258f4b3 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -156,6 +156,7 @@ var ( stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking}, stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking}, govtypes.ModuleName: {authtypes.Burner}, + fswaptypes.ModuleName: {authtypes.Burner, authtypes.Minter}, } // module accounts that are allowed to receive tokens diff --git a/x/fswap/keeper/expected_keepers.go b/x/fswap/keeper/expected_keepers.go new file mode 100644 index 0000000000..f1a8330f1f --- /dev/null +++ b/x/fswap/keeper/expected_keepers.go @@ -0,0 +1,19 @@ +package keeper + +import ( + sdk "github.com/Finschia/finschia-sdk/types" +) + +type ( + AccountKeeper interface { + GetModuleAddress(name string) sdk.AccAddress + } + + BankKeeper interface { + HasBalance(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coin) bool + GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin + SendCoins(ctx sdk.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error + MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error + BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error + } +) diff --git a/x/fswap/keeper/fswap.go b/x/fswap/keeper/fswap.go deleted file mode 100644 index a3cdb16b49..0000000000 --- a/x/fswap/keeper/fswap.go +++ /dev/null @@ -1,109 +0,0 @@ -package keeper - -import ( - "github.com/Finschia/finschia-sdk/store/prefix" - sdk "github.com/Finschia/finschia-sdk/types" - "github.com/Finschia/finschia-sdk/x/fswap/types" -) - -func (k Keeper) GetSwapped(ctx sdk.Context, toDenom string) (types.Swapped, error) { - store := ctx.KVStore(k.storeKey) - key := swappedKey(toDenom) - bz := store.Get(key) - if bz == nil { - return types.Swapped{ - OldCoinAmount: sdk.Coin{Denom: "", Amount: sdk.ZeroInt()}, - NewCoinAmount: sdk.Coin{Denom: "", Amount: sdk.ZeroInt()}, - }, nil - } - swapped := types.Swapped{} - if err := k.cdc.Unmarshal(bz, &swapped); err != nil { - return types.Swapped{}, err - } - return swapped, nil -} - -func (k Keeper) GetAllSwapped(ctx sdk.Context) []types.Swapped { - swappedSlice := make([]types.Swapped, 0) - k.IterateAllSwapped(ctx, func(fswapInit types.Swapped) bool { - swappedSlice = append(swappedSlice, fswapInit) - return false - }) - return swappedSlice -} - -func (k Keeper) IterateAllSwapped(ctx sdk.Context, cb func(swapped types.Swapped) (stop bool)) { - store := ctx.KVStore(k.storeKey) - swappedDataStore := prefix.NewStore(store, swappedKeyPrefix) - - iterator := swappedDataStore.Iterator(nil, nil) - defer iterator.Close() - - for ; iterator.Valid(); iterator.Next() { - swapped := types.Swapped{} - k.cdc.MustUnmarshal(iterator.Value(), &swapped) - if cb(swapped) { - break - } - } -} - -func (k Keeper) SetSwapped(ctx sdk.Context, swapped types.Swapped) error { - store := ctx.KVStore(k.storeKey) - key := swappedKey(swapped.NewCoinAmount.Denom) - bz, err := k.cdc.Marshal(&swapped) - if err != nil { - return err - } - store.Set(key, bz) - return nil -} - -func (k Keeper) GetFswapInit(ctx sdk.Context, toDenom string) (types.FswapInit, error) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(fswapInitKey(toDenom)) - if bz == nil { - return types.FswapInit{}, types.ErrFswapInitNotFound - } - fswapInit := types.FswapInit{} - err := k.cdc.Unmarshal(bz, &fswapInit) - if err != nil { - return types.FswapInit{}, err - } - return fswapInit, nil -} - -func (k Keeper) GetAllFswapInits(ctx sdk.Context) []types.FswapInit { - fswapInits := make([]types.FswapInit, 0) - k.IterateAllFswapInits(ctx, func(fswapInit types.FswapInit) bool { - fswapInits = append(fswapInits, fswapInit) - return false - }) - return fswapInits -} - -func (k Keeper) IterateAllFswapInits(ctx sdk.Context, cb func(swapped types.FswapInit) (stop bool)) { - store := ctx.KVStore(k.storeKey) - fswapInitDataStore := prefix.NewStore(store, fswapInitPrefix) - - iterator := fswapInitDataStore.Iterator(nil, nil) - defer iterator.Close() - - for ; iterator.Valid(); iterator.Next() { - fswapInit := types.FswapInit{} - k.cdc.MustUnmarshal(iterator.Value(), &fswapInit) - if cb(fswapInit) { - break - } - } -} - -func (k Keeper) SetFswapInit(ctx sdk.Context, fswapInit types.FswapInit) error { - store := ctx.KVStore(k.storeKey) - bz, err := k.cdc.Marshal(&fswapInit) - if err != nil { - return err - } - store.Set(fswapInitKey(fswapInit.ToDenom), bz) - return nil -} diff --git a/x/fswap/keeper/genesis.go b/x/fswap/keeper/genesis.go index 1fb6929dea..08a263a460 100644 --- a/x/fswap/keeper/genesis.go +++ b/x/fswap/keeper/genesis.go @@ -16,7 +16,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error return fmt.Errorf("cannot initialize genesis state, there are more than 1 swapped") } for _, fswapInit := range genState.GetFswapInit() { - if err := k.SetFswapInit(ctx, fswapInit); err != nil { + if err := k.setFswapInit(ctx, fswapInit); err != nil { panic(err) } } @@ -31,7 +31,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error // ExportGenesis returns the module's exported genesis. func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { return &types.GenesisState{ - FswapInit: k.GetAllFswapInits(ctx), - Swapped: k.GetAllSwapped(ctx), + FswapInit: k.getAllFswapInits(ctx), + Swapped: k.getAllSwapped(ctx), } } diff --git a/x/fswap/keeper/genesis_test.go b/x/fswap/keeper/genesis_test.go index 483078a433..86c011b284 100644 --- a/x/fswap/keeper/genesis_test.go +++ b/x/fswap/keeper/genesis_test.go @@ -5,7 +5,7 @@ import ( ) func (s *KeeperTestSuite) TestInitAndExportGenesis() { - ctx, _ := s.sdkCtx.CacheContext() + ctx, _ := s.ctx.CacheContext() defaultGenesis := types.DefaultGenesis() err := s.keeper.InitGenesis(ctx, defaultGenesis) s.Require().NoError(err) diff --git a/x/fswap/keeper/grpc_query.go b/x/fswap/keeper/grpc_query.go index ed416a1f4d..5f8952f8aa 100644 --- a/x/fswap/keeper/grpc_query.go +++ b/x/fswap/keeper/grpc_query.go @@ -7,33 +7,6 @@ import ( "github.com/Finschia/finschia-sdk/x/fswap/types" ) -//var _ types.QueryServer = Keeper{} -// -//// todo this files is just for test -//// Swapped implements types.QueryServer. -//func (k Keeper) Swapped(ctx context.Context, req *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) { -// sdkCtx := sdk.UnwrapSDKContext(ctx) -// check := k.GetFswap(sdkCtx) -// -// return &types.QuerySwappedResponse{ -// OldCoinAmount: sdk.NewCoin(check.FromDenom, sdk.ZeroInt()), -// NewCoinAmount: sdk.NewCoin(check.ToDenom, sdk.ZeroInt()), -// }, nil -//} -// -//// TotalSwappableAmount implements types.QueryServer. -//func (k Keeper) TotalSwappableAmount(context.Context, *types.QueryTotalSwappableAmountRequest) (*types.QueryTotalSwappableAmountResponse, error) { -// //TODO implement me -// panic("unimplemented") -// // sdkCtx := sdk.UnwrapSDKContext(ctx) -// // totalSwappableAmount := k.GetParams(sdkCtx).SwappableNewCoinAmount -// // swapped := k.GetSwapped(sdkCtx) -// // swappableNewCoin := sdk.NewCoin(k.config.NewCoinDenom, totalSwappableAmount.Sub(swapped.NewCoinAmount)) -// // return &types.QueryTotalSwappableAmountResponse{ -// // SwappableNewCoin: swappableNewCoin, -// // }, nil -//} - var _ types.QueryServer = QueryServer{} type QueryServer struct { @@ -48,18 +21,23 @@ func NewQueryServer(keeper Keeper) *QueryServer { func (s QueryServer) Swapped(ctx context.Context, _ *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) { c := sdk.UnwrapSDKContext(ctx) - allSwapped := s.Keeper.GetAllSwapped(c) - if len(allSwapped) == 0 { - return nil, types.ErrSwappedNotFound + swapped, err := s.Keeper.getSwapped(c) + if err != nil { + return nil, err } - swapped := allSwapped[0] return &types.QuerySwappedResponse{ OldCoinAmount: swapped.GetOldCoinAmount(), NewCoinAmount: swapped.GetNewCoinAmount(), }, nil } -func (s QueryServer) TotalSwappableAmount(_ context.Context, _ *types.QueryTotalSwappableAmountRequest) (*types.QueryTotalSwappableAmountResponse, error) { - // s.Keeper.GetSwappableNewCoinAmount() - return nil, nil +func (s QueryServer) TotalSwappableAmount(ctx context.Context, _ *types.QueryTotalSwappableAmountRequest) (*types.QueryTotalSwappableAmountResponse, error) { + c := sdk.UnwrapSDKContext(ctx) + amount, err := s.Keeper.getSwappableNewCoinAmount(c) + if err != nil { + return &types.QueryTotalSwappableAmountResponse{}, err + } + return &types.QueryTotalSwappableAmountResponse{ + SwappableNewCoin: amount, + }, nil } diff --git a/x/fswap/keeper/grpc_query_params_test.go b/x/fswap/keeper/grpc_query_params_test.go deleted file mode 100644 index 9429264902..0000000000 --- a/x/fswap/keeper/grpc_query_params_test.go +++ /dev/null @@ -1 +0,0 @@ -package keeper_test diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 753244c0f2..54e6c106c3 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -1,36 +1,42 @@ package keeper import ( + "errors" "fmt" "github.com/tendermint/tendermint/libs/log" "github.com/Finschia/finschia-sdk/codec" + "github.com/Finschia/finschia-sdk/store/prefix" storetypes "github.com/Finschia/finschia-sdk/store/types" sdk "github.com/Finschia/finschia-sdk/types" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" "github.com/Finschia/finschia-sdk/x/fswap/types" ) -type ( - Keeper struct { - cdc codec.BinaryCodec - storeKey storetypes.StoreKey - accountKeeper types.AccountKeeper - bankKeeper types.BankKeeper - } -) +type Keeper struct { + cdc codec.BinaryCodec + storeKey storetypes.StoreKey + + AccountKeeper + BankKeeper + + fromDenom string + toDenom string + swapMultiple sdk.Int + swapCap sdk.Int +} -func NewKeeper( - cdc codec.BinaryCodec, - storeKey storetypes.StoreKey, - ak types.AccountKeeper, - bk types.BankKeeper, -) Keeper { +func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, ak AccountKeeper, bk BankKeeper) Keeper { return Keeper{ cdc: cdc, storeKey: storeKey, - accountKeeper: ak, - bankKeeper: bk, + AccountKeeper: ak, + BankKeeper: bk, + fromDenom: "", + toDenom: "", + swapMultiple: sdk.ZeroInt(), + swapCap: sdk.ZeroInt(), } } @@ -39,29 +45,301 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { } func (k Keeper) FswapInit(ctx sdk.Context, fswapInit types.FswapInit) error { - // todo validate & check the first time (or not will reject the proposal) - // todo add test for this keeper in keeper test if err := fswapInit.ValidateBasic(); err != nil { return err } - - // need confirm: Is ibcState necessary? (please ref upgrade keeper) - if err := k.SetFswapInit(ctx, fswapInit); err != nil { + if k.hasBeenInitialized(ctx) { + return errors.New("already initialized") + } + if err := k.setFswapInit(ctx, fswapInit); err != nil { return err } - // need confirm: Is Swapped use sdk.Coin or sdk.Int swapped := types.Swapped{ OldCoinAmount: sdk.Coin{ - Denom: "", + Denom: fswapInit.GetFromDenom(), Amount: sdk.ZeroInt(), }, NewCoinAmount: sdk.Coin{ - Denom: "", + Denom: fswapInit.GetToDenom(), Amount: sdk.ZeroInt(), }, } - if err := k.SetSwapped(ctx, swapped); err != nil { + if err := k.setSwapped(ctx, swapped); err != nil { + return err + } + return nil +} + +func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, oldCoinAmount sdk.Coin) error { + if ok := k.HasBalance(ctx, addr, oldCoinAmount); !ok { + return sdkerrors.ErrInsufficientFunds + } + fswapInit, err := k.getFswapInit(ctx) + if err != nil { + return err + } + if oldCoinAmount.GetDenom() != fswapInit.GetFromDenom() { + return errors.New("denom mismatch") + } + + newAmount := oldCoinAmount.Amount.Mul(fswapInit.SwapMultiple) + newCoinAmount := sdk.NewCoin(fswapInit.ToDenom, newAmount) + if err := k.checkSwapCap(ctx, newCoinAmount); err != nil { + return err + } + + moduleAddr := k.GetModuleAddress(types.ModuleName) + if err := k.SendCoins(ctx, addr, moduleAddr, sdk.NewCoins(oldCoinAmount)); err != nil { + return err + } + + if err := k.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(oldCoinAmount)); err != nil { + return err + } + + if err := k.MintCoins(ctx, types.ModuleName, sdk.NewCoins(newCoinAmount)); err != nil { + return err + } + + if err := k.SendCoins(ctx, moduleAddr, addr, sdk.NewCoins(newCoinAmount)); err != nil { + return err + } + + if err := k.updateSwapped(ctx, oldCoinAmount, newCoinAmount); err != nil { + return err + } + + if err := ctx.EventManager().EmitTypedEvent(&types.EventSwapCoins{ + Address: addr.String(), + OldCoinAmount: oldCoinAmount, + NewCoinAmount: newCoinAmount, + }); err != nil { + return err + } + return nil +} + +func (k Keeper) SwapAll(ctx sdk.Context, addr sdk.AccAddress) error { + oldDenom, err := k.getFromDenom(ctx) + if err != nil { + return err + } + balance := k.GetBalance(ctx, addr, oldDenom) + if balance.IsZero() { + return sdkerrors.ErrInsufficientFunds + } + + if err := k.Swap(ctx, addr, balance); err != nil { + return err + } + return nil +} + +func (k Keeper) setFswapInit(ctx sdk.Context, fswapInit types.FswapInit) error { + store := ctx.KVStore(k.storeKey) + //if store.Has(allowFswapInitOnceKey()) { + // return errors.New("fswap already initialized, allow only one init") + //} + //store.Set(allowFswapInitOnceKey(), []byte{}) + bz, err := k.cdc.Marshal(&fswapInit) + if err != nil { return err } + store.Set(fswapInitKey(fswapInit.ToDenom), bz) return nil } + +func (k Keeper) getAllSwapped(ctx sdk.Context) []types.Swapped { + swappedSlice := make([]types.Swapped, 0) + k.iterateAllSwapped(ctx, func(swapped types.Swapped) bool { + swappedSlice = append(swappedSlice, swapped) + return false + }) + return swappedSlice +} + +func (k Keeper) iterateAllSwapped(ctx sdk.Context, cb func(swapped types.Swapped) (stop bool)) { + store := ctx.KVStore(k.storeKey) + swappedDataStore := prefix.NewStore(store, swappedKeyPrefix) + + iterator := swappedDataStore.Iterator(nil, nil) + defer iterator.Close() + + for ; iterator.Valid(); iterator.Next() { + swapped := types.Swapped{} + k.cdc.MustUnmarshal(iterator.Value(), &swapped) + if cb(swapped) { + break + } + } +} + +func (k Keeper) getSwapped(ctx sdk.Context) (types.Swapped, error) { + newDenom, err := k.getToDenom(ctx) + if err != nil { + return types.Swapped{}, err + } + store := ctx.KVStore(k.storeKey) + key := swappedKey(newDenom) + bz := store.Get(key) + if bz == nil { + return types.Swapped{}, types.ErrSwappedNotFound + } + swapped := types.Swapped{} + if err := k.cdc.Unmarshal(bz, &swapped); err != nil { + return types.Swapped{}, err + } + return swapped, nil +} + +func (k Keeper) setSwapped(ctx sdk.Context, swapped types.Swapped) error { + store := ctx.KVStore(k.storeKey) + key := swappedKey(swapped.NewCoinAmount.Denom) + bz, err := k.cdc.Marshal(&swapped) + if err != nil { + return err + } + store.Set(key, bz) + return nil +} + +func (k Keeper) getAllFswapInits(ctx sdk.Context) []types.FswapInit { + fswapInits := make([]types.FswapInit, 0) + k.iterateAllFswapInits(ctx, func(fswapInit types.FswapInit) bool { + fswapInits = append(fswapInits, fswapInit) + return false + }) + return fswapInits +} + +func (k Keeper) iterateAllFswapInits(ctx sdk.Context, cb func(swapped types.FswapInit) (stop bool)) { + store := ctx.KVStore(k.storeKey) + fswapInitDataStore := prefix.NewStore(store, fswapInitPrefix) + + iterator := fswapInitDataStore.Iterator(nil, nil) + defer iterator.Close() + + for ; iterator.Valid(); iterator.Next() { + fswapInit := types.FswapInit{} + k.cdc.MustUnmarshal(iterator.Value(), &fswapInit) + if cb(fswapInit) { + break + } + } +} + +func (k Keeper) getSwappableNewCoinAmount(ctx sdk.Context) (sdk.Coin, error) { + swapCap, err := k.getSwapCap(ctx) + if err != nil { + return sdk.Coin{}, err + } + swapped, err := k.getSwapped(ctx) + if err != nil { + return sdk.Coin{}, err + } + denom, err := k.getToDenom(ctx) + if err != nil { + return sdk.Coin{}, err + } + + remainingAmount := swapCap.Sub(swapped.GetNewCoinAmount().Amount) + return sdk.NewCoin(denom, remainingAmount), nil +} + +func (k Keeper) getFromDenom(ctx sdk.Context) (string, error) { + if len(k.fromDenom) > 0 { + return k.fromDenom, nil + } + fswapInit, err := k.getFswapInit(ctx) + if err != nil { + return "", err + } + k.fromDenom = fswapInit.GetFromDenom() + return k.fromDenom, nil +} + +func (k Keeper) getToDenom(ctx sdk.Context) (string, error) { + if len(k.toDenom) > 0 { + return k.toDenom, nil + } + fswapInit, err := k.getFswapInit(ctx) + if err != nil { + return "", err + } + k.toDenom = fswapInit.GetToDenom() + return k.toDenom, nil +} + +func (k Keeper) getSwapMultiple(ctx sdk.Context) (sdk.Int, error) { + if k.swapMultiple.IsPositive() { + return k.swapMultiple, nil + } + fswapInit, err := k.getFswapInit(ctx) + if err != nil { + return sdk.ZeroInt(), err + } + k.swapMultiple = fswapInit.SwapMultiple + return k.swapMultiple, nil +} + +func (k Keeper) getSwapCap(ctx sdk.Context) (sdk.Int, error) { + if k.swapCap.IsPositive() { + return k.swapCap, nil + } + fswapInit, err := k.getFswapInit(ctx) + if err != nil { + return sdk.ZeroInt(), err + } + k.swapCap = fswapInit.AmountCapForToDenom + return k.swapCap, nil +} + +func (k Keeper) getFswapInit(ctx sdk.Context) (types.FswapInit, error) { + fswapInits := k.getAllFswapInits(ctx) + if len(fswapInits) == 0 { + return types.FswapInit{}, types.ErrFswapNotInitilized + } + return fswapInits[0], nil +} + +func (k Keeper) updateSwapped(ctx sdk.Context, oldAmount, newAmount sdk.Coin) error { + prevSwapped, err := k.getSwapped(ctx) + if err != nil { + return err + } + updatedSwapped := &types.Swapped{ + OldCoinAmount: oldAmount.Add(prevSwapped.OldCoinAmount), + NewCoinAmount: newAmount.Add(prevSwapped.NewCoinAmount), + } + + store := ctx.KVStore(k.storeKey) + key := swappedKey(newAmount.Denom) + bz, err := k.cdc.Marshal(updatedSwapped) + if err != nil { + return err + } + store.Set(key, bz) + return nil +} + +func (k Keeper) checkSwapCap(ctx sdk.Context, newCoinAmount sdk.Coin) error { + swapped, err := k.getSwapped(ctx) + if err != nil { + return err + } + swapCap, err := k.getSwapCap(ctx) + if err != nil { + return err + } + + if swapCap.LT(swapped.NewCoinAmount.Add(newCoinAmount).Amount) { + return fmt.Errorf("cann't swap more because of swapCap limit, amount=%s", newCoinAmount.String()) + } + + return nil +} + +func (k Keeper) hasBeenInitialized(ctx sdk.Context) bool { + inits := k.getAllFswapInits(ctx) + return len(inits) > 0 +} diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 47ef1bc592..f1c9aa87b5 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -4,40 +4,186 @@ import ( "context" "testing" - "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + "github.com/Finschia/finschia-sdk/crypto/keys/secp256k1" "github.com/Finschia/finschia-sdk/simapp" + "github.com/Finschia/finschia-sdk/testutil/testdata" sdk "github.com/Finschia/finschia-sdk/types" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" "github.com/Finschia/finschia-sdk/x/fswap/keeper" - "github.com/Finschia/finschia-sdk/x/fswap/testutil" "github.com/Finschia/finschia-sdk/x/fswap/types" + minttypes "github.com/Finschia/finschia-sdk/x/mint/types" ) type KeeperTestSuite struct { suite.Suite - sdkCtx sdk.Context - goCtx context.Context - keeper keeper.Keeper - bankKeeper types.BankKeeper - msgServer types.MsgServer + ctx sdk.Context + goCtx context.Context + keeper keeper.Keeper + queryServer types.QueryServer + msgServer types.MsgServer + + accWithOldCoin sdk.AccAddress + accWithNewCoin sdk.AccAddress + initBalance sdk.Int + + fswapInit types.FswapInit + oldDenom string + newDenom string + swapMultiple sdk.Int + swapCap sdk.Int } -func TestKeeperTestSuite(t *testing.T) { - suite.Run(t, new(KeeperTestSuite)) +func (s *KeeperTestSuite) createRandomAccounts(n int) []sdk.AccAddress { + seenAddresses := make(map[string]bool, n) + addresses := make([]sdk.AccAddress, n) + for i := range addresses { + var address sdk.AccAddress + for { + pk := secp256k1.GenPrivKey().PubKey() + address = sdk.AccAddress(pk.Address()) + if !seenAddresses[address.String()] { + seenAddresses[address.String()] = true + break + } + } + addresses[i] = address + } + return addresses } func (s *KeeperTestSuite) SetupTest() { - ctrl := gomock.NewController(s.T()) - bankKeeper := testutil.NewMockBankKeeper(ctrl) - s.bankKeeper = bankKeeper checkTx := false app := simapp.Setup(checkTx) - s.sdkCtx = app.BaseApp.NewContext(checkTx, tmproto.Header{}) - s.goCtx = sdk.WrapSDKContext(s.sdkCtx) + testdata.RegisterInterfaces(app.InterfaceRegistry()) + testdata.RegisterMsgServer(app.MsgServiceRouter(), testdata.MsgServerImpl{}) + s.ctx = app.BaseApp.NewContext(checkTx, tmproto.Header{}) + s.goCtx = sdk.WrapSDKContext(s.ctx) s.keeper = app.FswapKeeper - + s.queryServer = keeper.NewQueryServer(s.keeper) s.msgServer = keeper.NewMsgServer(s.keeper) + + s.oldDenom = "old" + s.newDenom = "new" + s.swapMultiple = sdk.NewInt(1000) + s.initBalance = sdk.NewInt(123456789) + numAcc := int64(2) + s.swapCap = s.initBalance.Mul(s.swapMultiple.Mul(sdk.NewInt(numAcc))) + s.fswapInit = types.FswapInit{ + FromDenom: s.oldDenom, + ToDenom: s.newDenom, + AmountCapForToDenom: s.swapCap, + SwapMultiple: s.swapMultiple, + } + s.createAccountsWithInitBalance(app) + app.AccountKeeper.GetModuleAccount(s.ctx, types.ModuleName) +} + +func (s *KeeperTestSuite) createAccountsWithInitBalance(app *simapp.SimApp) { + addresses := []*sdk.AccAddress{ + &s.accWithOldCoin, + &s.accWithNewCoin, + } + for i, address := range s.createRandomAccounts(len(addresses)) { + *addresses[i] = address + } + minter := app.AccountKeeper.GetModuleAccount(s.ctx, minttypes.ModuleName).GetAddress() + oldAmount := sdk.NewCoins(sdk.NewCoin(s.oldDenom, s.initBalance)) + s.Require().NoError(app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, oldAmount)) + s.Require().NoError(app.BankKeeper.SendCoins(s.ctx, minter, s.accWithOldCoin, oldAmount)) + + newAmount := sdk.NewCoins(sdk.NewCoin(s.newDenom, s.initBalance)) + s.Require().NoError(app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, newAmount)) + s.Require().NoError(app.BankKeeper.SendCoins(s.ctx, minter, s.accWithNewCoin, newAmount)) +} + +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, &KeeperTestSuite{}) +} + +func (s *KeeperTestSuite) TestSwap() { + err := s.keeper.FswapInit(s.ctx, s.fswapInit) + s.Require().NoError(err) + testCases := map[string]struct { + from sdk.AccAddress + amountToSwap sdk.Int + expectedBalanceWithoutMultiply sdk.Int + shouldThrowError bool + expectedError error + }{ + "swap some": { + s.accWithOldCoin, + sdk.NewInt(100), + sdk.NewInt(100), + false, + nil, + }, + "swap all the balance": { + s.accWithOldCoin, + s.initBalance, + s.initBalance, + false, + nil, + }, + "account holding new coin only": { + s.accWithNewCoin, + sdk.NewInt(100), + s.initBalance, + true, + sdkerrors.ErrInsufficientFunds, + }, + } + for name, tc := range testCases { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() + err := s.keeper.Swap(ctx, tc.from, sdk.NewCoin(s.oldDenom, tc.amountToSwap)) + if tc.shouldThrowError { + s.Require().ErrorIs(err, tc.expectedError) + return + } + s.Require().NoError(err) + actualAmount := s.keeper.GetBalance(ctx, tc.from, s.newDenom).Amount + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapMultiple) + s.Require().Equal(expectedAmount, actualAmount) + }) + } +} + +func (s *KeeperTestSuite) TestSwapAll() { + testCases := map[string]struct { + from sdk.AccAddress + expectedBalanceWithoutMultiply sdk.Int + shouldThrowError bool + expectedError error + }{ + "account holding old coin": { + s.accWithOldCoin, + s.initBalance, + false, + nil, + }, + "account holding new coin only": { + s.accWithNewCoin, + s.initBalance, + true, + sdkerrors.ErrInsufficientFunds, + }, + } + for name, tc := range testCases { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() + err := s.keeper.SwapAll(ctx, tc.from) + if tc.shouldThrowError { + s.Require().ErrorIs(err, tc.expectedError) + return + } + s.Require().NoError(err) + actualAmount := s.keeper.GetBalance(ctx, tc.from, s.newDenom).Amount + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapMultiple) + s.Require().Equal(expectedAmount, actualAmount) + }) + } } diff --git a/x/fswap/keeper/keys.go b/x/fswap/keeper/keys.go index 39cedce5d2..874b526e55 100644 --- a/x/fswap/keeper/keys.go +++ b/x/fswap/keeper/keys.go @@ -1,8 +1,9 @@ package keeper var ( - fswapInitPrefix = []byte{0x01} - swappedKeyPrefix = []byte{0x02} + fswapInitPrefix = []byte{0x01} + swappedKeyPrefix = []byte{0x02} + allowFswapInitOnceValue = []byte{0x03} ) // fswapInitKey key(prefix + toDenom) @@ -10,6 +11,11 @@ func fswapInitKey(toDenom string) []byte { return append(fswapInitPrefix, toDenom...) } +// swappedKey key(prefix + toDenom) func swappedKey(toDenom string) []byte { return append(swappedKeyPrefix, toDenom...) } + +func allowFswapInitOnceKey() []byte { + return allowFswapInitOnceValue +} diff --git a/x/fswap/keeper/msg_server.go b/x/fswap/keeper/msg_server.go index 1a1703012b..e3310afb6d 100644 --- a/x/fswap/keeper/msg_server.go +++ b/x/fswap/keeper/msg_server.go @@ -3,6 +3,8 @@ package keeper import ( "context" + sdk "github.com/Finschia/finschia-sdk/types" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" "github.com/Finschia/finschia-sdk/x/fswap/types" ) @@ -17,32 +19,35 @@ func NewMsgServer(keeper Keeper) *MsgServer { } func (s MsgServer) Swap(ctx context.Context, req *types.MsgSwapRequest) (*types.MsgSwapResponse, error) { - //c := sdk.UnwrapSDKContext(ctx) - //swapped, err := s.keeper.GetSwapped(c) - //if err != nil { - // return nil, err - //} - //if req.GetAmount().Denom != swapped.OldCoinAmount.Denom { - // return nil, sdkerrors.ErrInvalidCoins - //} - //from, err := sdk.AccAddressFromBech32(req.FromAddress) - //if err != nil { - // return nil, err - //} - //if err := s.keeper.Swap(c, from, req.GetAmount()); err != nil { - // return nil, err - //} + c := sdk.UnwrapSDKContext(ctx) + fswapInit, err := s.keeper.getFswapInit(c) + if err != nil { + return &types.MsgSwapResponse{}, err + } + if req.GetAmount().Denom != fswapInit.GetFromDenom() { + return nil, sdkerrors.ErrInvalidCoins + } + from, err := sdk.AccAddressFromBech32(req.FromAddress) + if err != nil { + return nil, err + } + if err := s.keeper.Swap(c, from, req.GetAmount()); err != nil { + return nil, err + } return &types.MsgSwapResponse{}, nil } func (s MsgServer) SwapAll(ctx context.Context, req *types.MsgSwapAllRequest) (*types.MsgSwapAllResponse, error) { - //c := sdk.UnwrapSDKContext(ctx) - //from, err := sdk.AccAddressFromBech32(req.FromAddress) - //if err != nil { - // return nil, err - //} - //if err := s.keeper.SwapAll(c, from); err != nil { - // return nil, err - //} + c := sdk.UnwrapSDKContext(ctx) + if !s.keeper.hasBeenInitialized(c) { + return &types.MsgSwapAllResponse{}, types.ErrFswapNotInitilized + } + from, err := sdk.AccAddressFromBech32(req.FromAddress) + if err != nil { + return nil, err + } + if err := s.keeper.SwapAll(c, from); err != nil { + return nil, err + } return &types.MsgSwapAllResponse{}, nil } diff --git a/x/fswap/module.go b/x/fswap/module.go index db1f2415f0..280ba8f091 100644 --- a/x/fswap/module.go +++ b/x/fswap/module.go @@ -93,15 +93,15 @@ type AppModule struct { AppModuleBasic keeper keeper.Keeper - accountKeeper types.AccountKeeper - bankKeeper types.BankKeeper + accountKeeper keeper.AccountKeeper + bankKeeper keeper.BankKeeper } func NewAppModule( cdc codec.Codec, keeper keeper.Keeper, - accountKeeper types.AccountKeeper, - bankKeeper types.BankKeeper, + accountKeeper keeper.AccountKeeper, + bankKeeper keeper.BankKeeper, ) AppModule { return AppModule{ AppModuleBasic: NewAppModuleBasic(cdc), @@ -125,7 +125,7 @@ func (am AppModule) Route() sdk.Route { func (AppModule) QuerierRoute() string { return types.QuerierRoute } // LegacyQuerierHandler returns the capability module's Querier. -func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier { +func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier { return nil } diff --git a/x/fswap/testutil/expected_keepers_mocks.go b/x/fswap/testutil/expected_keepers_mocks.go index 21bb92feaf..bae4f2df62 100644 --- a/x/fswap/testutil/expected_keepers_mocks.go +++ b/x/fswap/testutil/expected_keepers_mocks.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: x/fswap/types/expected_keepers.go +// Source: x/fswap/keeper/expected_keepers.go // Package testutil is a generated GoMock package. package testutil @@ -8,7 +8,6 @@ import ( reflect "reflect" types "github.com/Finschia/finschia-sdk/types" - types0 "github.com/Finschia/finschia-sdk/x/auth/types" gomock "github.com/golang/mock/gomock" ) @@ -35,18 +34,18 @@ func (m *MockAccountKeeper) EXPECT() *MockAccountKeeperMockRecorder { return m.recorder } -// GetAccount mocks base method. -func (m *MockAccountKeeper) GetAccount(ctx types.Context, addr types.AccAddress) types0.AccountI { +// GetModuleAddress mocks base method. +func (m *MockAccountKeeper) GetModuleAddress(name string) types.AccAddress { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetAccount", ctx, addr) - ret0, _ := ret[0].(types0.AccountI) + ret := m.ctrl.Call(m, "GetModuleAddress", name) + ret0, _ := ret[0].(types.AccAddress) return ret0 } -// GetAccount indicates an expected call of GetAccount. -func (mr *MockAccountKeeperMockRecorder) GetAccount(ctx, addr interface{}) *gomock.Call { +// GetModuleAddress indicates an expected call of GetModuleAddress. +func (mr *MockAccountKeeperMockRecorder) GetModuleAddress(name interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAccount", reflect.TypeOf((*MockAccountKeeper)(nil).GetAccount), ctx, addr) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAddress", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAddress), name) } // MockBankKeeper is a mock of BankKeeper interface. @@ -72,30 +71,72 @@ func (m *MockBankKeeper) EXPECT() *MockBankKeeperMockRecorder { return m.recorder } -// GetSupply mocks base method. -func (m *MockBankKeeper) GetSupply(ctx types.Context, denom string) types.Coin { +// BurnCoins mocks base method. +func (m *MockBankKeeper) BurnCoins(ctx types.Context, moduleName string, amt types.Coins) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetSupply", ctx, denom) + ret := m.ctrl.Call(m, "BurnCoins", ctx, moduleName, amt) + ret0, _ := ret[0].(error) + return ret0 +} + +// BurnCoins indicates an expected call of BurnCoins. +func (mr *MockBankKeeperMockRecorder) BurnCoins(ctx, moduleName, amt interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BurnCoins", reflect.TypeOf((*MockBankKeeper)(nil).BurnCoins), ctx, moduleName, amt) +} + +// GetBalance mocks base method. +func (m *MockBankKeeper) GetBalance(ctx types.Context, addr types.AccAddress, denom string) types.Coin { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBalance", ctx, addr, denom) ret0, _ := ret[0].(types.Coin) return ret0 } -// GetSupply indicates an expected call of GetSupply. -func (mr *MockBankKeeperMockRecorder) GetSupply(ctx, denom interface{}) *gomock.Call { +// GetBalance indicates an expected call of GetBalance. +func (mr *MockBankKeeperMockRecorder) GetBalance(ctx, addr, denom interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBalance", reflect.TypeOf((*MockBankKeeper)(nil).GetBalance), ctx, addr, denom) +} + +// HasBalance mocks base method. +func (m *MockBankKeeper) HasBalance(ctx types.Context, addr types.AccAddress, amt types.Coin) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "HasBalance", ctx, addr, amt) + ret0, _ := ret[0].(bool) + return ret0 +} + +// HasBalance indicates an expected call of HasBalance. +func (mr *MockBankKeeperMockRecorder) HasBalance(ctx, addr, amt interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HasBalance", reflect.TypeOf((*MockBankKeeper)(nil).HasBalance), ctx, addr, amt) +} + +// MintCoins mocks base method. +func (m *MockBankKeeper) MintCoins(ctx types.Context, moduleName string, amt types.Coins) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "MintCoins", ctx, moduleName, amt) + ret0, _ := ret[0].(error) + return ret0 +} + +// MintCoins indicates an expected call of MintCoins. +func (mr *MockBankKeeperMockRecorder) MintCoins(ctx, moduleName, amt interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSupply", reflect.TypeOf((*MockBankKeeper)(nil).GetSupply), ctx, denom) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MintCoins", reflect.TypeOf((*MockBankKeeper)(nil).MintCoins), ctx, moduleName, amt) } -// SpendableCoins mocks base method. -func (m *MockBankKeeper) SpendableCoins(ctx types.Context, addr types.AccAddress) types.Coins { +// SendCoins mocks base method. +func (m *MockBankKeeper) SendCoins(ctx types.Context, fromAddr, toAddr types.AccAddress, amt types.Coins) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SpendableCoins", ctx, addr) - ret0, _ := ret[0].(types.Coins) + ret := m.ctrl.Call(m, "SendCoins", ctx, fromAddr, toAddr, amt) + ret0, _ := ret[0].(error) return ret0 } -// SpendableCoins indicates an expected call of SpendableCoins. -func (mr *MockBankKeeperMockRecorder) SpendableCoins(ctx, addr interface{}) *gomock.Call { +// SendCoins indicates an expected call of SendCoins. +func (mr *MockBankKeeperMockRecorder) SendCoins(ctx, fromAddr, toAddr, amt interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SpendableCoins", reflect.TypeOf((*MockBankKeeper)(nil).SpendableCoins), ctx, addr) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendCoins", reflect.TypeOf((*MockBankKeeper)(nil).SendCoins), ctx, fromAddr, toAddr, amt) } diff --git a/x/fswap/types/errors.go b/x/fswap/types/errors.go index 0d718293c8..0ca61b9916 100644 --- a/x/fswap/types/errors.go +++ b/x/fswap/types/errors.go @@ -8,7 +8,8 @@ import ( // x/fswap module sentinel errors var ( - ErrFswapInitNotFound = sdkerrors.Register(ModuleName, 1100, "fswap init does not exist") - ErrSwappedNotFound = sdkerrors.Register(ModuleName, 1101, "swapped does not exist") - ErrExceedSwappable = sdkerrors.Register(ModuleName, 1102, "exceed swappable coin amount") + ErrFswapInitNotFound = sdkerrors.Register(ModuleName, 1100, "fswap init does not exist") + ErrSwappedNotFound = sdkerrors.Register(ModuleName, 1101, "swapped does not exist") + ErrExceedSwappable = sdkerrors.Register(ModuleName, 1102, "exceed swappable coin amount") + ErrFswapNotInitilized = sdkerrors.Register(ModuleName, 1103, "fswap not initilized") ) diff --git a/x/fswap/types/expected_keepers.go b/x/fswap/types/expected_keepers.go deleted file mode 100644 index 195cd89bdc..0000000000 --- a/x/fswap/types/expected_keepers.go +++ /dev/null @@ -1,19 +0,0 @@ -package types - -import ( - sdk "github.com/Finschia/finschia-sdk/types" - "github.com/Finschia/finschia-sdk/x/auth/types" -) - -// AccountKeeper defines the expected account keeper used for simulations (noalias) -type AccountKeeper interface { - GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI - // Methods imported from account should be defined here -} - -// BankKeeper defines the expected interface needed to retrieve account balances. -type BankKeeper interface { - SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins - // Methods imported from bank should be defined here - GetSupply(ctx sdk.Context, denom string) sdk.Coin -} From 9b26d2f89790247d7f92392dd8ee459df11f1032 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Tue, 30 Apr 2024 15:11:22 +0900 Subject: [PATCH 12/47] chore: rename fswap to swap --- docs/core/proto-docs.md | 16 +-- proto/lbm/fswap/v1/fswap.proto | 10 +- proto/lbm/fswap/v1/genesis.proto | 4 +- simapp/app.go | 4 +- x/fswap/client/cli/tx.go | 10 +- x/fswap/client/proposal_handler.go | 2 +- x/fswap/handler.go | 16 +-- x/fswap/keeper/genesis.go | 12 +- x/fswap/keeper/genesis_test.go | 2 +- x/fswap/keeper/keeper.go | 78 ++++++------ x/fswap/keeper/keeper_test.go | 6 +- x/fswap/keeper/keys.go | 15 +-- x/fswap/keeper/msg_server.go | 6 +- x/fswap/proposal_handler_test.go | 24 ++-- x/fswap/spec/README.md | 2 +- x/fswap/types/codec.go | 4 +- x/fswap/types/errors.go | 8 +- x/fswap/types/fswap.go | 16 +-- x/fswap/types/fswap.pb.go | 188 ++++++++++++++--------------- x/fswap/types/genesis.go | 16 +-- x/fswap/types/genesis.pb.go | 48 ++++---- x/fswap/types/proposal.go | 26 ++-- 22 files changed, 249 insertions(+), 264 deletions(-) diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index 00f451c881..7a5798c5c3 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -860,8 +860,8 @@ - [EventSwapCoins](#lbm.fswap.v1.EventSwapCoins) - [lbm/fswap/v1/fswap.proto](#lbm/fswap/v1/fswap.proto) - - [FswapInit](#lbm.fswap.v1.FswapInit) - - [FswapInitProposal](#lbm.fswap.v1.FswapInitProposal) + - [SwapInit](#lbm.fswap.v1.SwapInit) + - [SwapInitProposal](#lbm.fswap.v1.SwapInitProposal) - [Swapped](#lbm.fswap.v1.Swapped) - [lbm/fswap/v1/genesis.proto](#lbm/fswap/v1/genesis.proto) @@ -12750,9 +12750,9 @@ Msg defines the foundation Msg service. - + -### FswapInit +### SwapInit @@ -12768,9 +12768,9 @@ Msg defines the foundation Msg service. - + -### FswapInitProposal +### SwapInitProposal From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 @@ -12778,7 +12778,7 @@ From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk base | ----- | ---- | ----- | ----------- | | `title` | [string](#string) | | | | `description` | [string](#string) | | | -| `fswap_init` | [FswapInit](#lbm.fswap.v1.FswapInit) | | | +| `swap_init` | [SwapInit](#lbm.fswap.v1.SwapInit) | | | @@ -12825,7 +12825,7 @@ GenesisState defines the fswap module's genesis state. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `fswap_init` | [FswapInit](#lbm.fswap.v1.FswapInit) | repeated | | +| `swap_init` | [SwapInit](#lbm.fswap.v1.SwapInit) | repeated | | | `swapped` | [Swapped](#lbm.fswap.v1.Swapped) | repeated | | diff --git a/proto/lbm/fswap/v1/fswap.proto b/proto/lbm/fswap/v1/fswap.proto index 0a7be86ac4..cfc2826a6f 100644 --- a/proto/lbm/fswap/v1/fswap.proto +++ b/proto/lbm/fswap/v1/fswap.proto @@ -6,7 +6,7 @@ option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types"; import "gogoproto/gogo.proto"; import "cosmos/base/v1beta1/coin.proto"; -message FswapInit { +message SwapInit { option (gogoproto.equal) = true; option (gogoproto.goproto_stringer) = false; @@ -19,13 +19,13 @@ message FswapInit { } // From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 -message FswapInitProposal { +message SwapInitProposal { option (gogoproto.equal) = true; option (gogoproto.goproto_stringer) = false; - string title = 1; - string description = 2; - FswapInit fswap_init = 3 [(gogoproto.nullable) = false]; + string title = 1; + string description = 2; + SwapInit swap_init = 3 [(gogoproto.nullable) = false]; } message Swapped { diff --git a/proto/lbm/fswap/v1/genesis.proto b/proto/lbm/fswap/v1/genesis.proto index 8f73e05399..539454bc2c 100644 --- a/proto/lbm/fswap/v1/genesis.proto +++ b/proto/lbm/fswap/v1/genesis.proto @@ -8,6 +8,6 @@ import "lbm/fswap/v1/fswap.proto"; // GenesisState defines the fswap module's genesis state. message GenesisState { - repeated FswapInit fswap_init = 1 [(gogoproto.nullable) = false]; - repeated Swapped swapped = 2 [(gogoproto.nullable) = false]; + repeated SwapInit swap_init = 1 [(gogoproto.nullable) = false]; + repeated Swapped swapped = 2 [(gogoproto.nullable) = false]; } diff --git a/simapp/app.go b/simapp/app.go index 915258f4b3..5072f33759 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -131,7 +131,7 @@ var ( upgradeclient.ProposalHandler, upgradeclient.CancelProposalHandler, foundationclient.ProposalHandler, - fswapclient.FswapInitHandler, + fswapclient.SwapInitHandler, ), params.AppModuleBasic{}, crisis.AppModuleBasic{}, @@ -345,7 +345,7 @@ func NewSimApp( AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)). AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)). AddRoute(foundation.RouterKey, foundationkeeper.NewFoundationProposalsHandler(app.FoundationKeeper)). - AddRoute(fswaptypes.RouterKey, fswap.NewFswapInitHandler(app.FswapKeeper)) + AddRoute(fswaptypes.RouterKey, fswap.NewSwapInitHandler(app.FswapKeeper)) govKeeper := govkeeper.NewKeeper( appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper, diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index 6afe904501..6905ffa8b6 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -103,10 +103,10 @@ func CmdTxMsgSwapAll() *cobra.Command { return cmd } -// NewCmdFswapInitProposal implements a command handler for submitting a fswap init proposal transaction. -func NewCmdFswapInitProposal() *cobra.Command { +// NewCmdSwapInitProposal implements a command handler for submitting a swap init proposal transaction. +func NewCmdSwapInitProposal() *cobra.Command { cmd := &cobra.Command{ - Use: "fswap-init [flags]", + Use: "swap-init [flags]", Args: cobra.ExactArgs(0), Short: "todo", Long: "todo", @@ -183,13 +183,13 @@ func parseArgsToContent(cmd *cobra.Command) (gov.Content, error) { return nil, err } - fswapInit := types.FswapInit{ + swapInit := types.SwapInit{ FromDenom: from_denom, ToDenom: to_denom, AmountCapForToDenom: sdk.NewInt(amount_limit), SwapMultiple: sdk.NewInt(swap_rate), } - content := types.NewFswapInitProposal(title, description, fswapInit) + content := types.NewSwapInitProposal(title, description, swapInit) return content, nil } diff --git a/x/fswap/client/proposal_handler.go b/x/fswap/client/proposal_handler.go index c104b2a153..17a3b40eb3 100644 --- a/x/fswap/client/proposal_handler.go +++ b/x/fswap/client/proposal_handler.go @@ -5,4 +5,4 @@ import ( govclient "github.com/Finschia/finschia-sdk/x/gov/client" ) -var FswapInitHandler = govclient.NewProposalHandler(cli.NewCmdFswapInitProposal) +var SwapInitHandler = govclient.NewProposalHandler(cli.NewCmdSwapInitProposal) diff --git a/x/fswap/handler.go b/x/fswap/handler.go index 278dfee6a9..d8dfaac2f6 100644 --- a/x/fswap/handler.go +++ b/x/fswap/handler.go @@ -26,20 +26,20 @@ func NewHandler(k keeper.Keeper) sdk.Handler { } } -// NewFswapInitHandler creates a governance handler to manage new proposal types. -// It enables FswapInit to propose an fswap init -func NewFswapInitHandler(k keeper.Keeper) govtypes.Handler { +// NewSwapInitHandler creates a governance handler to manage new proposal types. +// It enables SwapInit to propose an fswap init +func NewSwapInitHandler(k keeper.Keeper) govtypes.Handler { return func(ctx sdk.Context, content govtypes.Content) error { switch c := content.(type) { - case *types.FswapInitProposal: - return handleFswapInit(ctx, k, c) + case *types.SwapInitProposal: + return handleSwapInit(ctx, k, c) default: - return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized fsawp proposal content type: %T", c) + return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized sawp proposal content type: %T", c) } } } -func handleFswapInit(ctx sdk.Context, k keeper.Keeper, p *types.FswapInitProposal) error { - return k.FswapInit(ctx, p.FswapInit) +func handleSwapInit(ctx sdk.Context, k keeper.Keeper, p *types.SwapInitProposal) error { + return k.SwapInit(ctx, p.SwapInit) } diff --git a/x/fswap/keeper/genesis.go b/x/fswap/keeper/genesis.go index 08a263a460..5d63779245 100644 --- a/x/fswap/keeper/genesis.go +++ b/x/fswap/keeper/genesis.go @@ -9,14 +9,14 @@ import ( // InitGenesis initializes the module's state from a provided genesis state. func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error { - if len(genState.GetFswapInit()) > 1 { - return fmt.Errorf("cannot initialize genesis state, there are more than 1 fswapInit") + if len(genState.GetSwapInit()) > 1 { + return fmt.Errorf("cannot initialize genesis state, there are more than 1 swapInit") } if len(genState.GetSwapped()) > 1 { return fmt.Errorf("cannot initialize genesis state, there are more than 1 swapped") } - for _, fswapInit := range genState.GetFswapInit() { - if err := k.setFswapInit(ctx, fswapInit); err != nil { + for _, swapInit := range genState.GetSwapInit() { + if err := k.setSwapInit(ctx, swapInit); err != nil { panic(err) } } @@ -31,7 +31,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error // ExportGenesis returns the module's exported genesis. func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { return &types.GenesisState{ - FswapInit: k.getAllFswapInits(ctx), - Swapped: k.getAllSwapped(ctx), + SwapInit: k.getAllSwapInits(ctx), + Swapped: k.getAllSwapped(ctx), } } diff --git a/x/fswap/keeper/genesis_test.go b/x/fswap/keeper/genesis_test.go index 86c011b284..d57f9a24f8 100644 --- a/x/fswap/keeper/genesis_test.go +++ b/x/fswap/keeper/genesis_test.go @@ -12,6 +12,6 @@ func (s *KeeperTestSuite) TestInitAndExportGenesis() { exportGenesis := s.keeper.ExportGenesis(ctx) s.Require().Equal(defaultGenesis, exportGenesis) - s.Require().Equal(defaultGenesis.GetFswapInit(), exportGenesis.GetFswapInit()) + s.Require().Equal(defaultGenesis.GetSwapInit(), exportGenesis.GetSwapInit()) s.Require().Equal(defaultGenesis.GetSwapped(), exportGenesis.GetSwapped()) } diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 54e6c106c3..b164bd2fb4 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -44,23 +44,23 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) } -func (k Keeper) FswapInit(ctx sdk.Context, fswapInit types.FswapInit) error { - if err := fswapInit.ValidateBasic(); err != nil { +func (k Keeper) SwapInit(ctx sdk.Context, swapInit types.SwapInit) error { + if err := swapInit.ValidateBasic(); err != nil { return err } if k.hasBeenInitialized(ctx) { return errors.New("already initialized") } - if err := k.setFswapInit(ctx, fswapInit); err != nil { + if err := k.setSwapInit(ctx, swapInit); err != nil { return err } swapped := types.Swapped{ OldCoinAmount: sdk.Coin{ - Denom: fswapInit.GetFromDenom(), + Denom: swapInit.GetFromDenom(), Amount: sdk.ZeroInt(), }, NewCoinAmount: sdk.Coin{ - Denom: fswapInit.GetToDenom(), + Denom: swapInit.GetToDenom(), Amount: sdk.ZeroInt(), }, } @@ -74,16 +74,16 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, oldCoinAmount sdk.Coi if ok := k.HasBalance(ctx, addr, oldCoinAmount); !ok { return sdkerrors.ErrInsufficientFunds } - fswapInit, err := k.getFswapInit(ctx) + swapInit, err := k.getSwapInit(ctx) if err != nil { return err } - if oldCoinAmount.GetDenom() != fswapInit.GetFromDenom() { + if oldCoinAmount.GetDenom() != swapInit.GetFromDenom() { return errors.New("denom mismatch") } - newAmount := oldCoinAmount.Amount.Mul(fswapInit.SwapMultiple) - newCoinAmount := sdk.NewCoin(fswapInit.ToDenom, newAmount) + newAmount := oldCoinAmount.Amount.Mul(swapInit.SwapMultiple) + newCoinAmount := sdk.NewCoin(swapInit.ToDenom, newAmount) if err := k.checkSwapCap(ctx, newCoinAmount); err != nil { return err } @@ -135,17 +135,13 @@ func (k Keeper) SwapAll(ctx sdk.Context, addr sdk.AccAddress) error { return nil } -func (k Keeper) setFswapInit(ctx sdk.Context, fswapInit types.FswapInit) error { +func (k Keeper) setSwapInit(ctx sdk.Context, swapInit types.SwapInit) error { store := ctx.KVStore(k.storeKey) - //if store.Has(allowFswapInitOnceKey()) { - // return errors.New("fswap already initialized, allow only one init") - //} - //store.Set(allowFswapInitOnceKey(), []byte{}) - bz, err := k.cdc.Marshal(&fswapInit) + bz, err := k.cdc.Marshal(&swapInit) if err != nil { return err } - store.Set(fswapInitKey(fswapInit.ToDenom), bz) + store.Set(swapInitKey(swapInit.ToDenom), bz) return nil } @@ -203,26 +199,26 @@ func (k Keeper) setSwapped(ctx sdk.Context, swapped types.Swapped) error { return nil } -func (k Keeper) getAllFswapInits(ctx sdk.Context) []types.FswapInit { - fswapInits := make([]types.FswapInit, 0) - k.iterateAllFswapInits(ctx, func(fswapInit types.FswapInit) bool { - fswapInits = append(fswapInits, fswapInit) +func (k Keeper) getAllSwapInits(ctx sdk.Context) []types.SwapInit { + swapInits := make([]types.SwapInit, 0) + k.iterateAllSwapInits(ctx, func(swapInit types.SwapInit) bool { + swapInits = append(swapInits, swapInit) return false }) - return fswapInits + return swapInits } -func (k Keeper) iterateAllFswapInits(ctx sdk.Context, cb func(swapped types.FswapInit) (stop bool)) { +func (k Keeper) iterateAllSwapInits(ctx sdk.Context, cb func(swapped types.SwapInit) (stop bool)) { store := ctx.KVStore(k.storeKey) - fswapInitDataStore := prefix.NewStore(store, fswapInitPrefix) + swapInitDataStore := prefix.NewStore(store, swapInitPrefix) - iterator := fswapInitDataStore.Iterator(nil, nil) + iterator := swapInitDataStore.Iterator(nil, nil) defer iterator.Close() for ; iterator.Valid(); iterator.Next() { - fswapInit := types.FswapInit{} - k.cdc.MustUnmarshal(iterator.Value(), &fswapInit) - if cb(fswapInit) { + swapInit := types.SwapInit{} + k.cdc.MustUnmarshal(iterator.Value(), &swapInit) + if cb(swapInit) { break } } @@ -250,11 +246,11 @@ func (k Keeper) getFromDenom(ctx sdk.Context) (string, error) { if len(k.fromDenom) > 0 { return k.fromDenom, nil } - fswapInit, err := k.getFswapInit(ctx) + swapInit, err := k.getSwapInit(ctx) if err != nil { return "", err } - k.fromDenom = fswapInit.GetFromDenom() + k.fromDenom = swapInit.GetFromDenom() return k.fromDenom, nil } @@ -262,11 +258,11 @@ func (k Keeper) getToDenom(ctx sdk.Context) (string, error) { if len(k.toDenom) > 0 { return k.toDenom, nil } - fswapInit, err := k.getFswapInit(ctx) + swapInit, err := k.getSwapInit(ctx) if err != nil { return "", err } - k.toDenom = fswapInit.GetToDenom() + k.toDenom = swapInit.GetToDenom() return k.toDenom, nil } @@ -274,11 +270,11 @@ func (k Keeper) getSwapMultiple(ctx sdk.Context) (sdk.Int, error) { if k.swapMultiple.IsPositive() { return k.swapMultiple, nil } - fswapInit, err := k.getFswapInit(ctx) + swapInit, err := k.getSwapInit(ctx) if err != nil { return sdk.ZeroInt(), err } - k.swapMultiple = fswapInit.SwapMultiple + k.swapMultiple = swapInit.SwapMultiple return k.swapMultiple, nil } @@ -286,20 +282,20 @@ func (k Keeper) getSwapCap(ctx sdk.Context) (sdk.Int, error) { if k.swapCap.IsPositive() { return k.swapCap, nil } - fswapInit, err := k.getFswapInit(ctx) + swapInit, err := k.getSwapInit(ctx) if err != nil { return sdk.ZeroInt(), err } - k.swapCap = fswapInit.AmountCapForToDenom + k.swapCap = swapInit.AmountCapForToDenom return k.swapCap, nil } -func (k Keeper) getFswapInit(ctx sdk.Context) (types.FswapInit, error) { - fswapInits := k.getAllFswapInits(ctx) - if len(fswapInits) == 0 { - return types.FswapInit{}, types.ErrFswapNotInitilized +func (k Keeper) getSwapInit(ctx sdk.Context) (types.SwapInit, error) { + swapInits := k.getAllSwapInits(ctx) + if len(swapInits) == 0 { + return types.SwapInit{}, types.ErrSwapNotInitilized } - return fswapInits[0], nil + return swapInits[0], nil } func (k Keeper) updateSwapped(ctx sdk.Context, oldAmount, newAmount sdk.Coin) error { @@ -340,6 +336,6 @@ func (k Keeper) checkSwapCap(ctx sdk.Context, newCoinAmount sdk.Coin) error { } func (k Keeper) hasBeenInitialized(ctx sdk.Context) bool { - inits := k.getAllFswapInits(ctx) + inits := k.getAllSwapInits(ctx) return len(inits) > 0 } diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index f1c9aa87b5..0e624c38b4 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -30,7 +30,7 @@ type KeeperTestSuite struct { accWithNewCoin sdk.AccAddress initBalance sdk.Int - fswapInit types.FswapInit + swapInit types.SwapInit oldDenom string newDenom string swapMultiple sdk.Int @@ -72,7 +72,7 @@ func (s *KeeperTestSuite) SetupTest() { s.initBalance = sdk.NewInt(123456789) numAcc := int64(2) s.swapCap = s.initBalance.Mul(s.swapMultiple.Mul(sdk.NewInt(numAcc))) - s.fswapInit = types.FswapInit{ + s.swapInit = types.SwapInit{ FromDenom: s.oldDenom, ToDenom: s.newDenom, AmountCapForToDenom: s.swapCap, @@ -105,7 +105,7 @@ func TestKeeperTestSuite(t *testing.T) { } func (s *KeeperTestSuite) TestSwap() { - err := s.keeper.FswapInit(s.ctx, s.fswapInit) + err := s.keeper.SwapInit(s.ctx, s.swapInit) s.Require().NoError(err) testCases := map[string]struct { from sdk.AccAddress diff --git a/x/fswap/keeper/keys.go b/x/fswap/keeper/keys.go index 874b526e55..b3b652723f 100644 --- a/x/fswap/keeper/keys.go +++ b/x/fswap/keeper/keys.go @@ -1,21 +1,16 @@ package keeper var ( - fswapInitPrefix = []byte{0x01} - swappedKeyPrefix = []byte{0x02} - allowFswapInitOnceValue = []byte{0x03} + swapInitPrefix = []byte{0x01} + swappedKeyPrefix = []byte{0x02} ) -// fswapInitKey key(prefix + toDenom) -func fswapInitKey(toDenom string) []byte { - return append(fswapInitPrefix, toDenom...) +// swapInitKey key(prefix + toDenom) +func swapInitKey(toDenom string) []byte { + return append(swapInitPrefix, toDenom...) } // swappedKey key(prefix + toDenom) func swappedKey(toDenom string) []byte { return append(swappedKeyPrefix, toDenom...) } - -func allowFswapInitOnceKey() []byte { - return allowFswapInitOnceValue -} diff --git a/x/fswap/keeper/msg_server.go b/x/fswap/keeper/msg_server.go index e3310afb6d..849037fb6b 100644 --- a/x/fswap/keeper/msg_server.go +++ b/x/fswap/keeper/msg_server.go @@ -20,11 +20,11 @@ func NewMsgServer(keeper Keeper) *MsgServer { func (s MsgServer) Swap(ctx context.Context, req *types.MsgSwapRequest) (*types.MsgSwapResponse, error) { c := sdk.UnwrapSDKContext(ctx) - fswapInit, err := s.keeper.getFswapInit(c) + swapInit, err := s.keeper.getSwapInit(c) if err != nil { return &types.MsgSwapResponse{}, err } - if req.GetAmount().Denom != fswapInit.GetFromDenom() { + if req.GetAmount().Denom != swapInit.GetFromDenom() { return nil, sdkerrors.ErrInvalidCoins } from, err := sdk.AccAddressFromBech32(req.FromAddress) @@ -40,7 +40,7 @@ func (s MsgServer) Swap(ctx context.Context, req *types.MsgSwapRequest) (*types. func (s MsgServer) SwapAll(ctx context.Context, req *types.MsgSwapAllRequest) (*types.MsgSwapAllResponse, error) { c := sdk.UnwrapSDKContext(ctx) if !s.keeper.hasBeenInitialized(c) { - return &types.MsgSwapAllResponse{}, types.ErrFswapNotInitilized + return &types.MsgSwapAllResponse{}, types.ErrSwapNotInitilized } from, err := sdk.AccAddressFromBech32(req.FromAddress) if err != nil { diff --git a/x/fswap/proposal_handler_test.go b/x/fswap/proposal_handler_test.go index d69b329fdc..bd7598862a 100644 --- a/x/fswap/proposal_handler_test.go +++ b/x/fswap/proposal_handler_test.go @@ -3,27 +3,21 @@ package fswap_test import ( "testing" - "github.com/stretchr/testify/require" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - - "github.com/Finschia/finschia-sdk/simapp" - sdk "github.com/Finschia/finschia-sdk/types" - "github.com/Finschia/finschia-sdk/x/fswap" "github.com/Finschia/finschia-sdk/x/fswap/types" ) -func testProposal(fswapInit types.FswapInit) *types.FswapInitProposal { - return types.NewFswapInitProposal("Test", "description", fswapInit) +func testProposal(swapInit types.SwapInit) *types.SwapInitProposal { + return types.NewSwapInitProposal("Test", "description", swapInit) } func TestProposalHandlerPassed(t *testing.T) { - app := simapp.Setup(false) - ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - - fswapInit := types.NewFswapInit("aaa", "bbb", sdk.NewInt(1000), sdk.NewInt(100000)) - tp := testProposal(fswapInit) - hdlr := fswap.NewFswapInitHandler(app.FswapKeeper) - require.NoError(t, hdlr(ctx, tp)) + //app := simapp.Setup(false) + //ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + // + //fswapInit := types.NewSwapInit("aaa", "bbb", sdk.NewInt(1000), sdk.NewInt(100000)) + //tp := testProposal(fswapInit) + //hdlr := fswap.NewSwapInitHandler(app.FswapKeeper) + //require.NoError(t, hdlr(ctx, tp)) // todo check contents } diff --git a/x/fswap/spec/README.md b/x/fswap/spec/README.md index 6802e251ac..ee7423f27c 100644 --- a/x/fswap/spec/README.md +++ b/x/fswap/spec/README.md @@ -7,7 +7,7 @@ zsh init_node.sh sim 1 open the `./.simapp/simapp0/config/genesis.json` change the value of `voting_params.voting_period` to `10s` ``` simd start --home ~/.simapp/simapp0 -simd tx gov submit-proposal fswap-init --title "test" --description "test" --from link146asaycmtydq45kxc8evntqfgepagygelel00h --from-denom "cony" --to-denom "PDT" --swap-rate 123 --amount-limit 1000 --deposit 10000000stake --chain-id=sim --keyring-backend=test --gas-prices 1000stake --gas 10000000 --gas-adjustment 1.5 --home ~/.simapp/simapp0 -b block -y +simd tx gov submit-proposal swap-init --title "test" --description "test" --from link146asaycmtydq45kxc8evntqfgepagygelel00h --from-denom "cony" --to-denom "PDT" --swap-rate 123 --amount-limit 1000 --deposit 10000000stake --chain-id=sim --keyring-backend=test --gas-prices 1000stake --gas 10000000 --gas-adjustment 1.5 --home ~/.simapp/simapp0 -b block -y simd tx gov vote 1 yes --from link146asaycmtydq45kxc8evntqfgepagygelel00h --chain-id=sim --keyring-backend=test --home ~/.simapp/simapp0 -b block -y ``` diff --git a/x/fswap/types/codec.go b/x/fswap/types/codec.go index 054c7a7cbf..bcf91a9869 100644 --- a/x/fswap/types/codec.go +++ b/x/fswap/types/codec.go @@ -17,7 +17,7 @@ func RegisterCodec(cdc *codec.LegacyAmino) { } func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&FswapInitProposal{}, "finschia-sdk/FswapInitProposal", nil) + cdc.RegisterConcrete(&SwapInitProposal{}, "finschia-sdk/FswapInitProposal", nil) } func RegisterInterfaces(registry types.InterfaceRegistry) { @@ -26,7 +26,7 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) registry.RegisterImplementations( (*govtypes.Content)(nil), - &FswapInitProposal{}, + &SwapInitProposal{}, ) } diff --git a/x/fswap/types/errors.go b/x/fswap/types/errors.go index 0ca61b9916..baf70fc391 100644 --- a/x/fswap/types/errors.go +++ b/x/fswap/types/errors.go @@ -8,8 +8,8 @@ import ( // x/fswap module sentinel errors var ( - ErrFswapInitNotFound = sdkerrors.Register(ModuleName, 1100, "fswap init does not exist") - ErrSwappedNotFound = sdkerrors.Register(ModuleName, 1101, "swapped does not exist") - ErrExceedSwappable = sdkerrors.Register(ModuleName, 1102, "exceed swappable coin amount") - ErrFswapNotInitilized = sdkerrors.Register(ModuleName, 1103, "fswap not initilized") + ErrSwapInitNotFound = sdkerrors.Register(ModuleName, 1100, "swap init does not exist") + ErrSwappedNotFound = sdkerrors.Register(ModuleName, 1101, "swapped does not exist") + ErrExceedSwappable = sdkerrors.Register(ModuleName, 1102, "exceed swappable coin amount") + ErrSwapNotInitilized = sdkerrors.Register(ModuleName, 1103, "swap not initilized") ) diff --git a/x/fswap/types/fswap.go b/x/fswap/types/fswap.go index bd01e4b0ec..722e8c46a5 100644 --- a/x/fswap/types/fswap.go +++ b/x/fswap/types/fswap.go @@ -56,27 +56,27 @@ func (s Swapped) String() string { } // Validate validates the set of swapped -func (f FswapInit) ValidateBasic() error { - if f.FromDenom == "" { +func (s SwapInit) ValidateBasic() error { + if s.FromDenom == "" { return fmt.Errorf("from denomination cannot be empty") } - if f.ToDenom == "" { + if s.ToDenom == "" { return fmt.Errorf("to denomination cannot be empty") } - if f.FromDenom == f.ToDenom { + if s.FromDenom == s.ToDenom { return fmt.Errorf("from denomination cannot be equal to to denomination") } - if f.AmountCapForToDenom.LT(sdk.ZeroInt()) { + if s.AmountCapForToDenom.LT(sdk.ZeroInt()) { return fmt.Errorf("amount cannot be less than zero") } - if f.SwapMultiple.LT(sdk.ZeroInt()) { + if s.SwapMultiple.LT(sdk.ZeroInt()) { return fmt.Errorf("swap multiple cannot be less than zero") } return nil } // String implements the Stringer interface. -func (f FswapInit) String() string { - out, _ := yaml.Marshal(f) +func (s SwapInit) String() string { + out, _ := yaml.Marshal(s) return string(out) } diff --git a/x/fswap/types/fswap.pb.go b/x/fswap/types/fswap.pb.go index d25ba4bcd4..9b50fa5406 100644 --- a/x/fswap/types/fswap.pb.go +++ b/x/fswap/types/fswap.pb.go @@ -25,24 +25,24 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package -type FswapInit struct { +type SwapInit struct { FromDenom string `protobuf:"bytes,1,opt,name=from_denom,json=fromDenom,proto3" json:"from_denom,omitempty"` ToDenom string `protobuf:"bytes,2,opt,name=to_denom,json=toDenom,proto3" json:"to_denom,omitempty"` AmountCapForToDenom github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,3,opt,name=amount_cap_for_to_denom,json=amountCapForToDenom,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"amount_cap_for_to_denom"` SwapMultiple github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,4,opt,name=swap_multiple,json=swapMultiple,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"swap_multiple"` } -func (m *FswapInit) Reset() { *m = FswapInit{} } -func (*FswapInit) ProtoMessage() {} -func (*FswapInit) Descriptor() ([]byte, []int) { +func (m *SwapInit) Reset() { *m = SwapInit{} } +func (*SwapInit) ProtoMessage() {} +func (*SwapInit) Descriptor() ([]byte, []int) { return fileDescriptor_42ca60eaf37a2b67, []int{0} } -func (m *FswapInit) XXX_Unmarshal(b []byte) error { +func (m *SwapInit) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) } -func (m *FswapInit) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { +func (m *SwapInit) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { if deterministic { - return xxx_messageInfo_FswapInit.Marshal(b, m, deterministic) + return xxx_messageInfo_SwapInit.Marshal(b, m, deterministic) } else { b = b[:cap(b)] n, err := m.MarshalToSizedBuffer(b) @@ -52,26 +52,26 @@ func (m *FswapInit) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { return b[:n], nil } } -func (m *FswapInit) XXX_Merge(src proto.Message) { - xxx_messageInfo_FswapInit.Merge(m, src) +func (m *SwapInit) XXX_Merge(src proto.Message) { + xxx_messageInfo_SwapInit.Merge(m, src) } -func (m *FswapInit) XXX_Size() int { +func (m *SwapInit) XXX_Size() int { return m.Size() } -func (m *FswapInit) XXX_DiscardUnknown() { - xxx_messageInfo_FswapInit.DiscardUnknown(m) +func (m *SwapInit) XXX_DiscardUnknown() { + xxx_messageInfo_SwapInit.DiscardUnknown(m) } -var xxx_messageInfo_FswapInit proto.InternalMessageInfo +var xxx_messageInfo_SwapInit proto.InternalMessageInfo -func (m *FswapInit) GetFromDenom() string { +func (m *SwapInit) GetFromDenom() string { if m != nil { return m.FromDenom } return "" } -func (m *FswapInit) GetToDenom() string { +func (m *SwapInit) GetToDenom() string { if m != nil { return m.ToDenom } @@ -79,23 +79,23 @@ func (m *FswapInit) GetToDenom() string { } // From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 -type FswapInitProposal struct { - Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"` - Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` - FswapInit FswapInit `protobuf:"bytes,3,opt,name=fswap_init,json=fswapInit,proto3" json:"fswap_init"` +type SwapInitProposal struct { + Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"` + Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` + SwapInit SwapInit `protobuf:"bytes,3,opt,name=swap_init,json=swapInit,proto3" json:"swap_init"` } -func (m *FswapInitProposal) Reset() { *m = FswapInitProposal{} } -func (*FswapInitProposal) ProtoMessage() {} -func (*FswapInitProposal) Descriptor() ([]byte, []int) { +func (m *SwapInitProposal) Reset() { *m = SwapInitProposal{} } +func (*SwapInitProposal) ProtoMessage() {} +func (*SwapInitProposal) Descriptor() ([]byte, []int) { return fileDescriptor_42ca60eaf37a2b67, []int{1} } -func (m *FswapInitProposal) XXX_Unmarshal(b []byte) error { +func (m *SwapInitProposal) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) } -func (m *FswapInitProposal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { +func (m *SwapInitProposal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { if deterministic { - return xxx_messageInfo_FswapInitProposal.Marshal(b, m, deterministic) + return xxx_messageInfo_SwapInitProposal.Marshal(b, m, deterministic) } else { b = b[:cap(b)] n, err := m.MarshalToSizedBuffer(b) @@ -105,37 +105,37 @@ func (m *FswapInitProposal) XXX_Marshal(b []byte, deterministic bool) ([]byte, e return b[:n], nil } } -func (m *FswapInitProposal) XXX_Merge(src proto.Message) { - xxx_messageInfo_FswapInitProposal.Merge(m, src) +func (m *SwapInitProposal) XXX_Merge(src proto.Message) { + xxx_messageInfo_SwapInitProposal.Merge(m, src) } -func (m *FswapInitProposal) XXX_Size() int { +func (m *SwapInitProposal) XXX_Size() int { return m.Size() } -func (m *FswapInitProposal) XXX_DiscardUnknown() { - xxx_messageInfo_FswapInitProposal.DiscardUnknown(m) +func (m *SwapInitProposal) XXX_DiscardUnknown() { + xxx_messageInfo_SwapInitProposal.DiscardUnknown(m) } -var xxx_messageInfo_FswapInitProposal proto.InternalMessageInfo +var xxx_messageInfo_SwapInitProposal proto.InternalMessageInfo -func (m *FswapInitProposal) GetTitle() string { +func (m *SwapInitProposal) GetTitle() string { if m != nil { return m.Title } return "" } -func (m *FswapInitProposal) GetDescription() string { +func (m *SwapInitProposal) GetDescription() string { if m != nil { return m.Description } return "" } -func (m *FswapInitProposal) GetFswapInit() FswapInit { +func (m *SwapInitProposal) GetSwapInit() SwapInit { if m != nil { - return m.FswapInit + return m.SwapInit } - return FswapInit{} + return SwapInit{} } type Swapped struct { @@ -190,55 +190,55 @@ func (m *Swapped) GetNewCoinAmount() types.Coin { } func init() { - proto.RegisterType((*FswapInit)(nil), "lbm.fswap.v1.FswapInit") - proto.RegisterType((*FswapInitProposal)(nil), "lbm.fswap.v1.FswapInitProposal") + proto.RegisterType((*SwapInit)(nil), "lbm.fswap.v1.SwapInit") + proto.RegisterType((*SwapInitProposal)(nil), "lbm.fswap.v1.SwapInitProposal") proto.RegisterType((*Swapped)(nil), "lbm.fswap.v1.Swapped") } func init() { proto.RegisterFile("lbm/fswap/v1/fswap.proto", fileDescriptor_42ca60eaf37a2b67) } var fileDescriptor_42ca60eaf37a2b67 = []byte{ - // 467 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 0x31, 0x6f, 0xd3, 0x40, - 0x18, 0xb5, 0x43, 0xa0, 0xcd, 0xa5, 0x15, 0xc2, 0x54, 0x6a, 0x5a, 0x09, 0xbb, 0xea, 0x84, 0x40, - 0xdc, 0x29, 0xe9, 0x56, 0xb1, 0x90, 0xa2, 0x88, 0x0e, 0x48, 0x28, 0x20, 0x21, 0xb1, 0x58, 0x67, - 0xfb, 0x9c, 0x9c, 0xb0, 0xef, 0x3b, 0xf9, 0x2e, 0x31, 0xfc, 0x0b, 0x04, 0x0b, 0x63, 0x67, 0x7e, - 0x49, 0xc7, 0x8e, 0x88, 0xa1, 0xa0, 0x64, 0xe1, 0x5f, 0x80, 0xce, 0x77, 0x84, 0x30, 0x81, 0x90, - 0xba, 0x7d, 0x79, 0xef, 0xcb, 0x7b, 0x9f, 0xdf, 0xb3, 0x51, 0xaf, 0x48, 0x4a, 0x92, 0xab, 0x9a, - 0x4a, 0x32, 0xef, 0xdb, 0x01, 0xcb, 0x0a, 0x34, 0x04, 0x5b, 0x45, 0x52, 0x62, 0x0b, 0xcc, 0xfb, - 0xfb, 0x3b, 0x13, 0x98, 0x40, 0x43, 0x10, 0x33,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 0x50, 0x37, - 0x63, 0x2a, 0xad, 0xb8, 0xd4, 0x1c, 0x84, 0x8b, 0x65, 0x1d, 0x0a, 0x1e, 0x22, 0xd4, 0x94, 0x14, - 0x73, 0xc1, 0x75, 0x93, 0x46, 0x77, 0xb0, 0x8b, 0xd7, 0xbb, 0xc3, 0x2b, 0xb3, 0x61, 0xdb, 0x3c, - 0xc6, 0xb8, 0x93, 0xff, 0x02, 0xd6, 0xae, 0xfa, 0xe1, 0xa3, 0x8d, 0xe7, 0x35, 0x95, 0x92, 0x65, - 0xc1, 0x1c, 0xdd, 0x84, 0x22, 0x8b, 0x4d, 0x91, 0xb1, 0x0d, 0xa9, 0xb9, 0xaa, 0x3b, 0xd8, 0xc3, - 0xb6, 0x70, 0x6c, 0x0a, 0xc7, 0xae, 0x70, 0x7c, 0x02, 0x5c, 0x0c, 0x8f, 0x8c, 0xf4, 0xa7, 0xaf, - 0xd1, 0xfd, 0x7f, 0x4c, 0xc8, 0xfc, 0x69, 0xbc, 0x0d, 0x45, 0x66, 0x86, 0x47, 0x8d, 0x89, 0xf1, - 0x15, 0xac, 0xfe, 0xc3, 0xb7, 0x75, 0x35, 0xbe, 0x82, 0xd5, 0xbf, 0x7d, 0x8f, 0xdb, 0x26, 0x85, - 0xe1, 0x93, 0xf3, 0x45, 0xe8, 0x5f, 0x2c, 0x42, 0xff, 0xdb, 0x22, 0xf4, 0xdf, 0x2d, 0x43, 0xef, - 0x62, 0x19, 0x7a, 0x9f, 0x97, 0xa1, 0xf7, 0x0a, 0xff, 0x55, 0xfb, 0x8d, 0xfb, 0x86, 0x1a, 0x8f, - 0xe4, 0x46, 0xf3, 0xf6, 0x1f, 0xfd, 0x0c, 0x00, 0x00, 0xff, 0xff, 0xd3, 0xe3, 0x53, 0x7f, 0x5d, - 0x03, 0x00, 0x00, + // 465 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 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 0x80, 0x8b, 0xc1, 0x81, 0x61, 0xfe, 0xf4, 0x35, 0xb9, + 0xff, 0x8f, 0xee, 0x98, 0x8f, 0x86, 0x5b, 0x50, 0x16, 0xa6, 0x78, 0x64, 0x45, 0x8c, 0xae, 0x60, + 0xf3, 0x3f, 0x74, 0x1b, 0x97, 0xa3, 0x2b, 0xd8, 0xfc, 0xb7, 0xee, 0x61, 0xd3, 0xb8, 0x30, 0x78, + 0x72, 0xb6, 0x88, 0xc3, 0xf3, 0x45, 0x1c, 0x7e, 0x5b, 0xc4, 0xe1, 0xfb, 0x65, 0x1c, 0x9c, 0x2f, + 0xe3, 0xe0, 0xf3, 0x32, 0x0e, 0x5e, 0xe1, 0xbf, 0x72, 0xbf, 0xf1, 0xcf, 0xc7, 0x6a, 0x64, 0xd7, + 0xec, 0x8f, 0x7f, 0xf0, 0x33, 0x00, 0x00, 0xff, 0xff, 0x28, 0x05, 0xbc, 0x8c, 0x58, 0x03, 0x00, + 0x00, } -func (this *FswapInit) Equal(that interface{}) bool { +func (this *SwapInit) Equal(that interface{}) bool { if that == nil { return this == nil } - that1, ok := that.(*FswapInit) + that1, ok := that.(*SwapInit) if !ok { - that2, ok := that.(FswapInit) + that2, ok := that.(SwapInit) if ok { that1 = &that2 } else { @@ -264,14 +264,14 @@ func (this *FswapInit) Equal(that interface{}) bool { } return true } -func (this *FswapInitProposal) Equal(that interface{}) bool { +func (this *SwapInitProposal) Equal(that interface{}) bool { if that == nil { return this == nil } - that1, ok := that.(*FswapInitProposal) + that1, ok := that.(*SwapInitProposal) if !ok { - that2, ok := that.(FswapInitProposal) + that2, ok := that.(SwapInitProposal) if ok { that1 = &that2 } else { @@ -289,12 +289,12 @@ func (this *FswapInitProposal) Equal(that interface{}) bool { if this.Description != that1.Description { return false } - if !this.FswapInit.Equal(&that1.FswapInit) { + if !this.SwapInit.Equal(&that1.SwapInit) { return false } return true } -func (m *FswapInit) Marshal() (dAtA []byte, err error) { +func (m *SwapInit) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) n, err := m.MarshalToSizedBuffer(dAtA[:size]) @@ -304,12 +304,12 @@ func (m *FswapInit) Marshal() (dAtA []byte, err error) { return dAtA[:n], nil } -func (m *FswapInit) MarshalTo(dAtA []byte) (int, error) { +func (m *SwapInit) MarshalTo(dAtA []byte) (int, error) { size := m.Size() return m.MarshalToSizedBuffer(dAtA[:size]) } -func (m *FswapInit) MarshalToSizedBuffer(dAtA []byte) (int, error) { +func (m *SwapInit) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) _ = i var l int @@ -351,7 +351,7 @@ func (m *FswapInit) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } -func (m *FswapInitProposal) Marshal() (dAtA []byte, err error) { +func (m *SwapInitProposal) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) n, err := m.MarshalToSizedBuffer(dAtA[:size]) @@ -361,18 +361,18 @@ func (m *FswapInitProposal) Marshal() (dAtA []byte, err error) { return dAtA[:n], nil } -func (m *FswapInitProposal) MarshalTo(dAtA []byte) (int, error) { +func (m *SwapInitProposal) MarshalTo(dAtA []byte) (int, error) { size := m.Size() return m.MarshalToSizedBuffer(dAtA[:size]) } -func (m *FswapInitProposal) MarshalToSizedBuffer(dAtA []byte) (int, error) { +func (m *SwapInitProposal) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) _ = i var l int _ = l { - size, err := m.FswapInit.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.SwapInit.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -452,7 +452,7 @@ func encodeVarintFswap(dAtA []byte, offset int, v uint64) int { dAtA[offset] = uint8(v) return base } -func (m *FswapInit) Size() (n int) { +func (m *SwapInit) Size() (n int) { if m == nil { return 0 } @@ -473,7 +473,7 @@ func (m *FswapInit) Size() (n int) { return n } -func (m *FswapInitProposal) Size() (n int) { +func (m *SwapInitProposal) Size() (n int) { if m == nil { return 0 } @@ -487,7 +487,7 @@ func (m *FswapInitProposal) Size() (n int) { if l > 0 { n += 1 + l + sovFswap(uint64(l)) } - l = m.FswapInit.Size() + l = m.SwapInit.Size() n += 1 + l + sovFswap(uint64(l)) return n } @@ -511,7 +511,7 @@ func sovFswap(x uint64) (n int) { func sozFswap(x uint64) (n int) { return sovFswap(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } -func (m *FswapInit) Unmarshal(dAtA []byte) error { +func (m *SwapInit) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { @@ -534,10 +534,10 @@ func (m *FswapInit) Unmarshal(dAtA []byte) error { fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { - return fmt.Errorf("proto: FswapInit: wiretype end group for non-group") + return fmt.Errorf("proto: SwapInit: wiretype end group for non-group") } if fieldNum <= 0 { - return fmt.Errorf("proto: FswapInit: illegal tag %d (wire type %d)", fieldNum, wire) + return fmt.Errorf("proto: SwapInit: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: @@ -693,7 +693,7 @@ func (m *FswapInit) Unmarshal(dAtA []byte) error { } return nil } -func (m *FswapInitProposal) Unmarshal(dAtA []byte) error { +func (m *SwapInitProposal) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { @@ -716,10 +716,10 @@ func (m *FswapInitProposal) Unmarshal(dAtA []byte) error { fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { - return fmt.Errorf("proto: FswapInitProposal: wiretype end group for non-group") + return fmt.Errorf("proto: SwapInitProposal: wiretype end group for non-group") } if fieldNum <= 0 { - return fmt.Errorf("proto: FswapInitProposal: illegal tag %d (wire type %d)", fieldNum, wire) + return fmt.Errorf("proto: SwapInitProposal: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: @@ -788,7 +788,7 @@ func (m *FswapInitProposal) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 3: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field FswapInit", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field SwapInit", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -815,7 +815,7 @@ func (m *FswapInitProposal) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.FswapInit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.SwapInit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/fswap/types/genesis.go b/x/fswap/types/genesis.go index c93d81e5d2..9304fa6732 100644 --- a/x/fswap/types/genesis.go +++ b/x/fswap/types/genesis.go @@ -7,8 +7,8 @@ import ( // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { return &GenesisState{ - FswapInit: []FswapInit{}, - Swapped: []Swapped{}, + SwapInit: []SwapInit{}, + Swapped: []Swapped{}, } } @@ -16,20 +16,20 @@ func DefaultGenesis() *GenesisState { // failure. // need confirm: should we validate? Since it may nil func (gs GenesisState) Validate() error { - if len(gs.GetFswapInit()) == 0 { + if len(gs.GetSwapInit()) == 0 { return nil } if len(gs.GetSwapped()) == 0 { return nil } - if len(gs.GetFswapInit()) > 1 { - return errors.New("cannot have more than one fswap") // TODO(bjs) to sentinel + if len(gs.GetSwapInit()) > 1 { + return errors.New("cannot have more than one swapInit") // TODO(bjs) to sentinel } if len(gs.GetSwapped()) > 1 { return errors.New("cannot have more than one swapped") // TODO(bjs) to sentinel } - fswap := gs.GetFswapInit()[0] - if err := fswap.ValidateBasic(); err != nil { + swapInit := gs.GetSwapInit()[0] + if err := swapInit.ValidateBasic(); err != nil { return err } swapped := gs.GetSwapped()[0] @@ -37,7 +37,7 @@ func (gs GenesisState) Validate() error { return err } - if fswap.AmountCapForToDenom.LT(swapped.GetNewCoinAmount().Amount) { + if swapInit.AmountCapForToDenom.LT(swapped.GetNewCoinAmount().Amount) { return ErrExceedSwappable } return nil diff --git a/x/fswap/types/genesis.pb.go b/x/fswap/types/genesis.pb.go index 0dbfc653e0..9846807217 100644 --- a/x/fswap/types/genesis.pb.go +++ b/x/fswap/types/genesis.pb.go @@ -25,8 +25,8 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // GenesisState defines the fswap module's genesis state. type GenesisState struct { - FswapInit []FswapInit `protobuf:"bytes,1,rep,name=fswap_init,json=fswapInit,proto3" json:"fswap_init"` - Swapped []Swapped `protobuf:"bytes,2,rep,name=swapped,proto3" json:"swapped"` + SwapInit []SwapInit `protobuf:"bytes,1,rep,name=swap_init,json=swapInit,proto3" json:"swap_init"` + Swapped []Swapped `protobuf:"bytes,2,rep,name=swapped,proto3" json:"swapped"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -62,9 +62,9 @@ func (m *GenesisState) XXX_DiscardUnknown() { var xxx_messageInfo_GenesisState proto.InternalMessageInfo -func (m *GenesisState) GetFswapInit() []FswapInit { +func (m *GenesisState) GetSwapInit() []SwapInit { if m != nil { - return m.FswapInit + return m.SwapInit } return nil } @@ -83,22 +83,22 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/genesis.proto", fileDescriptor_94e309cb1db27661) } var fileDescriptor_94e309cb1db27661 = []byte{ - // 231 bytes of a gzipped FileDescriptorProto + // 232 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xca, 0x49, 0xca, 0xd5, 0x4f, 0x2b, 0x2e, 0x4f, 0x2c, 0xd0, 0x2f, 0x33, 0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0xc9, 0x49, 0xca, 0xd5, 0x03, 0xcb, 0xe9, 0x95, 0x19, 0x4a, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0x25, 0xf4, 0x41, 0x2c, 0x88, 0x1a, 0x29, 0x09, - 0x14, 0xfd, 0x10, 0xc5, 0x60, 0x19, 0xa5, 0x66, 0x46, 0x2e, 0x1e, 0x77, 0x88, 0x79, 0xc1, 0x25, - 0x89, 0x25, 0xa9, 0x42, 0x36, 0x5c, 0x5c, 0x60, 0xf9, 0xf8, 0xcc, 0xbc, 0xcc, 0x12, 0x09, 0x46, - 0x05, 0x66, 0x0d, 0x6e, 0x23, 0x71, 0x3d, 0x64, 0x3b, 0xf4, 0xdc, 0x40, 0x0c, 0xcf, 0xbc, 0xcc, - 0x12, 0x27, 0x96, 0x13, 0xf7, 0xe4, 0x19, 0x82, 0x38, 0xd3, 0x60, 0x02, 0x42, 0xa6, 0x5c, 0xec, - 0x20, 0x76, 0x41, 0x6a, 0x8a, 0x04, 0x13, 0x58, 0xab, 0x28, 0xaa, 0xd6, 0x60, 0x88, 0x24, 0x54, - 0x23, 0x4c, 0xad, 0x93, 0xc7, 0x89, 0x47, 0x72, 0x8c, 0x17, 0x1e, 0xc9, 0x31, 0x3e, 0x78, 0x24, - 0xc7, 0x38, 0xe1, 0xb1, 0x1c, 0xc3, 0x85, 0xc7, 0x72, 0x0c, 0x37, 0x1e, 0xcb, 0x31, 0x44, 0xe9, - 0xa5, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0xbb, 0x65, 0xe6, 0x15, 0x27, - 0x67, 0x64, 0x26, 0xea, 0xa7, 0x41, 0x19, 0xba, 0xc5, 0x29, 0xd9, 0xfa, 0x15, 0x50, 0x8f, 0x95, - 0x54, 0x16, 0xa4, 0x16, 0x27, 0xb1, 0x81, 0xbd, 0x65, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x8c, - 0x44, 0xd5, 0xdb, 0x32, 0x01, 0x00, 0x00, + 0x14, 0xfd, 0x10, 0xc5, 0x60, 0x19, 0xa5, 0x06, 0x46, 0x2e, 0x1e, 0x77, 0x88, 0x79, 0xc1, 0x25, + 0x89, 0x25, 0xa9, 0x42, 0x96, 0x5c, 0x9c, 0x20, 0xe9, 0xf8, 0xcc, 0xbc, 0xcc, 0x12, 0x09, 0x46, + 0x05, 0x66, 0x0d, 0x6e, 0x23, 0x31, 0x3d, 0x64, 0x2b, 0xf4, 0x82, 0xcb, 0x13, 0x0b, 0x3c, 0xf3, + 0x32, 0x4b, 0x9c, 0x58, 0x4e, 0xdc, 0x93, 0x67, 0x08, 0xe2, 0x28, 0x86, 0xf2, 0x85, 0x4c, 0xb9, + 0xd8, 0x41, 0xec, 0x82, 0xd4, 0x14, 0x09, 0x26, 0xb0, 0x46, 0x51, 0x4c, 0x8d, 0x05, 0xa9, 0x29, + 0x50, 0x7d, 0x30, 0xb5, 0x4e, 0x1e, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, + 0x91, 0x1c, 0xe3, 0x84, 0xc7, 0x72, 0x0c, 0x17, 0x1e, 0xcb, 0x31, 0xdc, 0x78, 0x2c, 0xc7, 0x10, + 0xa5, 0x97, 0x9e, 0x59, 0x92, 0x51, 0x9a, 0xa4, 0x97, 0x9c, 0x9f, 0xab, 0xef, 0x96, 0x99, 0x57, + 0x9c, 0x9c, 0x91, 0x99, 0xa8, 0x9f, 0x06, 0x65, 0xe8, 0x16, 0xa7, 0x64, 0xeb, 0x57, 0x40, 0x7d, + 0x55, 0x52, 0x59, 0x90, 0x5a, 0x9c, 0xc4, 0x06, 0xf6, 0x93, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, + 0x23, 0x10, 0xb5, 0x38, 0x2f, 0x01, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -135,10 +135,10 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { dAtA[i] = 0x12 } } - if len(m.FswapInit) > 0 { - for iNdEx := len(m.FswapInit) - 1; iNdEx >= 0; iNdEx-- { + if len(m.SwapInit) > 0 { + for iNdEx := len(m.SwapInit) - 1; iNdEx >= 0; iNdEx-- { { - size, err := m.FswapInit[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + size, err := m.SwapInit[iNdEx].MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -169,8 +169,8 @@ func (m *GenesisState) Size() (n int) { } var l int _ = l - if len(m.FswapInit) > 0 { - for _, e := range m.FswapInit { + if len(m.SwapInit) > 0 { + for _, e := range m.SwapInit { l = e.Size() n += 1 + l + sovGenesis(uint64(l)) } @@ -221,7 +221,7 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field FswapInit", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field SwapInit", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -248,8 +248,8 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.FswapInit = append(m.FswapInit, FswapInit{}) - if err := m.FswapInit[len(m.FswapInit)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + m.SwapInit = append(m.SwapInit, SwapInit{}) + if err := m.SwapInit[len(m.SwapInit)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/fswap/types/proposal.go b/x/fswap/types/proposal.go index 1a0cfca990..c90c190d36 100644 --- a/x/fswap/types/proposal.go +++ b/x/fswap/types/proposal.go @@ -7,38 +7,38 @@ import ( ) const ( - ProposalTypeFswapInit string = "FswapInit" + ProposalTypeSwapInit string = "SwapInit" ) -// NewFswapInitProposal creates a new FswapInitProposal instance. +// NewSwapInitProposal creates a new SwapInitProposal instance. // Deprecated: this proposal is considered legacy and is deprecated in favor of -// Msg-based gov proposals. See MsgFswapInit. -func NewFswapInitProposal(title, description string, fswapInit FswapInit) *FswapInitProposal { - return &FswapInitProposal{title, description, fswapInit} +// Msg-based gov proposals. See MsgSwapInit. +func NewSwapInitProposal(title, description string, swapInit SwapInit) *SwapInitProposal { + return &SwapInitProposal{title, description, swapInit} } // Implements Proposal Interface -var _ gov.Content = &FswapInitProposal{} +var _ gov.Content = &SwapInitProposal{} func init() { - gov.RegisterProposalType(ProposalTypeFswapInit) + gov.RegisterProposalType(ProposalTypeSwapInit) } // ProposalRoute gets the proposal's router key -func (m *FswapInitProposal) ProposalRoute() string { return RouterKey } +func (m *SwapInitProposal) ProposalRoute() string { return RouterKey } -// ProposalType is "FswapInit" -func (m *FswapInitProposal) ProposalType() string { return ProposalTypeFswapInit } +// ProposalType is "SwapInit" +func (m *SwapInitProposal) ProposalType() string { return ProposalTypeSwapInit } // String implements the Stringer interface. -func (m *FswapInitProposal) String() string { +func (m *SwapInitProposal) String() string { out, _ := yaml.Marshal(m) return string(out) } // ValidateBasic validates the proposal -func (m *FswapInitProposal) ValidateBasic() error { - if err := m.FswapInit.ValidateBasic(); err != nil { +func (m *SwapInitProposal) ValidateBasic() error { + if err := m.SwapInit.ValidateBasic(); err != nil { return err } return gov.ValidateAbstract(m) From 6aecac652ff99a9511591db90fe41a8fea65f850 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Tue, 30 Apr 2024 16:17:40 +0900 Subject: [PATCH 13/47] chore: change old/new to from/to --- docs/core/proto-docs.md | 32 ++--- proto/lbm/fswap/v1/event.proto | 8 +- proto/lbm/fswap/v1/fswap.proto | 6 +- proto/lbm/fswap/v1/query.proto | 19 +-- proto/lbm/fswap/v1/tx.proto | 2 +- x/fswap/client/cli/query.go | 2 +- x/fswap/keeper/grpc_query.go | 12 +- x/fswap/keeper/keeper.go | 20 +-- x/fswap/keeper/keeper_test.go | 66 +++++----- x/fswap/types/event.pb.go | 71 +++++----- x/fswap/types/fswap.go | 8 +- x/fswap/types/fswap.pb.go | 86 ++++++------ x/fswap/types/genesis.go | 2 +- x/fswap/types/query.pb.go | 232 +++++++++++++++++---------------- x/fswap/types/query.pb.gw.go | 28 ++-- x/fswap/types/tx.pb.go | 2 +- 16 files changed, 304 insertions(+), 292 deletions(-) diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index 7a5798c5c3..684b66a19e 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -870,8 +870,8 @@ - [lbm/fswap/v1/query.proto](#lbm/fswap/v1/query.proto) - [QuerySwappedRequest](#lbm.fswap.v1.QuerySwappedRequest) - [QuerySwappedResponse](#lbm.fswap.v1.QuerySwappedResponse) - - [QueryTotalSwappableAmountRequest](#lbm.fswap.v1.QueryTotalSwappableAmountRequest) - - [QueryTotalSwappableAmountResponse](#lbm.fswap.v1.QueryTotalSwappableAmountResponse) + - [QueryTotalSwappableToCoinAmountRequest](#lbm.fswap.v1.QueryTotalSwappableToCoinAmountRequest) + - [QueryTotalSwappableToCoinAmountResponse](#lbm.fswap.v1.QueryTotalSwappableToCoinAmountResponse) - [Query](#lbm.fswap.v1.Query) @@ -12726,8 +12726,8 @@ Msg defines the foundation Msg service. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | | `address` | [string](#string) | | holder's address | -| `old_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | amount of the old currency | -| `new_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | amount of the new currency | +| `from_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | from-coin amount | +| `to_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | to-coin amount | @@ -12793,8 +12793,8 @@ From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk base | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `old_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | -| `new_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | +| `from_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | +| `to_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | @@ -12867,17 +12867,17 @@ GenesisState defines the fswap module's genesis state. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `old_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | -| `new_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | +| `from_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | +| `to_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | - + -### QueryTotalSwappableAmountRequest +### QueryTotalSwappableToCoinAmountRequest @@ -12885,15 +12885,15 @@ GenesisState defines the fswap module's genesis state. - + -### QueryTotalSwappableAmountResponse +### QueryTotalSwappableToCoinAmountResponse | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `swappable_new_coin` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | +| `swappable_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | | @@ -12913,8 +12913,8 @@ GenesisState defines the fswap module's genesis state. | Method Name | Request Type | Response Type | Description | HTTP Verb | Endpoint | | ----------- | ------------ | ------------- | ------------| ------- | -------- | -| `Swapped` | [QuerySwappedRequest](#lbm.fswap.v1.QuerySwappedRequest) | [QuerySwappedResponse](#lbm.fswap.v1.QuerySwappedResponse) | Swapped queries the current swapped status that includes a burnt amount of old coin and a minted amount of new coin. | GET|/lbm/fswap/v1/swapped| -| `TotalSwappableAmount` | [QueryTotalSwappableAmountRequest](#lbm.fswap.v1.QueryTotalSwappableAmountRequest) | [QueryTotalSwappableAmountResponse](#lbm.fswap.v1.QueryTotalSwappableAmountResponse) | TotalSwappableAmount queries the current swappable amount for new coin. | GET|/lbm/fswap/v1/swappable_new_coin_amount| +| `Swapped` | [QuerySwappedRequest](#lbm.fswap.v1.QuerySwappedRequest) | [QuerySwappedResponse](#lbm.fswap.v1.QuerySwappedResponse) | Swapped queries the current swapped status that includes a burnt amount of from-coin and a minted amount of to-coin. | GET|/lbm/fswap/v1/swapped| +| `TotalSwappableToCoinAmount` | [QueryTotalSwappableToCoinAmountRequest](#lbm.fswap.v1.QueryTotalSwappableToCoinAmountRequest) | [QueryTotalSwappableToCoinAmountResponse](#lbm.fswap.v1.QueryTotalSwappableToCoinAmountResponse) | TotalSwappableToCoinAmount queries the current swappable amount for to-coin. | GET|/lbm/fswap/v1/swappable_new_coin_amount| @@ -12961,7 +12961,7 @@ GenesisState defines the fswap module's genesis state. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | | `from_address` | [string](#string) | | holder's address | -| `amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | amount of old currency | +| `amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | from-coin amount | diff --git a/proto/lbm/fswap/v1/event.proto b/proto/lbm/fswap/v1/event.proto index 374447f7c3..6834da7654 100644 --- a/proto/lbm/fswap/v1/event.proto +++ b/proto/lbm/fswap/v1/event.proto @@ -9,10 +9,10 @@ import "cosmos/base/v1beta1/coin.proto"; message EventSwapCoins { // holder's address string address = 1; - // amount of the old currency - cosmos.base.v1beta1.Coin old_coin_amount = 2 + // from-coin amount + cosmos.base.v1beta1.Coin from_coin_amount = 2 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; - // amount of the new currency - cosmos.base.v1beta1.Coin new_coin_amount = 3 + // to-coin amount + cosmos.base.v1beta1.Coin to_coin_amount = 3 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; } diff --git a/proto/lbm/fswap/v1/fswap.proto b/proto/lbm/fswap/v1/fswap.proto index cfc2826a6f..1216ceca57 100644 --- a/proto/lbm/fswap/v1/fswap.proto +++ b/proto/lbm/fswap/v1/fswap.proto @@ -29,9 +29,9 @@ message SwapInitProposal { } message Swapped { - option (gogoproto.goproto_stringer) = false; - cosmos.base.v1beta1.Coin old_coin_amount = 1 + option (gogoproto.goproto_stringer) = false; + cosmos.base.v1beta1.Coin from_coin_amount = 1 [(gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin", (gogoproto.nullable) = false]; - cosmos.base.v1beta1.Coin new_coin_amount = 2 + cosmos.base.v1beta1.Coin to_coin_amount = 2 [(gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin", (gogoproto.nullable) = false]; } diff --git a/proto/lbm/fswap/v1/query.proto b/proto/lbm/fswap/v1/query.proto index 2ef950bd79..09ca077261 100644 --- a/proto/lbm/fswap/v1/query.proto +++ b/proto/lbm/fswap/v1/query.proto @@ -9,27 +9,28 @@ import "cosmos/base/v1beta1/coin.proto"; import "lbm/fswap/v1/fswap.proto"; service Query { - // Swapped queries the current swapped status that includes a burnt amount of old coin and a minted amount of new - // coin. + // Swapped queries the current swapped status that includes a burnt amount of from-coin and a minted amount of + // to-coin. rpc Swapped(QuerySwappedRequest) returns (QuerySwappedResponse) { option (google.api.http).get = "/lbm/fswap/v1/swapped"; } - // TotalSwappableAmount queries the current swappable amount for new coin. - rpc TotalSwappableAmount(QueryTotalSwappableAmountRequest) returns (QueryTotalSwappableAmountResponse) { + // TotalSwappableToCoinAmount queries the current swappable amount for to-coin. + rpc TotalSwappableToCoinAmount(QueryTotalSwappableToCoinAmountRequest) + returns (QueryTotalSwappableToCoinAmountResponse) { option (google.api.http).get = "/lbm/fswap/v1/swappable_new_coin_amount"; } } message QuerySwappedRequest {} message QuerySwappedResponse { - cosmos.base.v1beta1.Coin old_coin_amount = 1 + cosmos.base.v1beta1.Coin from_coin_amount = 1 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; - cosmos.base.v1beta1.Coin new_coin_amount = 2 + cosmos.base.v1beta1.Coin to_coin_amount = 2 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; } -message QueryTotalSwappableAmountRequest {} -message QueryTotalSwappableAmountResponse { - cosmos.base.v1beta1.Coin swappable_new_coin = 1 +message QueryTotalSwappableToCoinAmountRequest {} +message QueryTotalSwappableToCoinAmountResponse { + cosmos.base.v1beta1.Coin swappable_amount = 1 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; } diff --git a/proto/lbm/fswap/v1/tx.proto b/proto/lbm/fswap/v1/tx.proto index c9b3c171bc..149abf400a 100644 --- a/proto/lbm/fswap/v1/tx.proto +++ b/proto/lbm/fswap/v1/tx.proto @@ -14,7 +14,7 @@ service Msg { message MsgSwapRequest { // holder's address string from_address = 1; - // amount of old currency + // from-coin amount cosmos.base.v1beta1.Coin amount = 2 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; } diff --git a/x/fswap/client/cli/query.go b/x/fswap/client/cli/query.go index f92118cfe3..feddf7dc1f 100644 --- a/x/fswap/client/cli/query.go +++ b/x/fswap/client/cli/query.go @@ -65,7 +65,7 @@ func CmdQueryTotalSwappableAmount() *cobra.Command { } queryClient := types.NewQueryClient(clientCtx) - res, err := queryClient.TotalSwappableAmount(cmd.Context(), &types.QueryTotalSwappableAmountRequest{}) + res, err := queryClient.TotalSwappableToCoinAmount(cmd.Context(), &types.QueryTotalSwappableToCoinAmountRequest{}) if err != nil { return err } diff --git a/x/fswap/keeper/grpc_query.go b/x/fswap/keeper/grpc_query.go index 5f8952f8aa..e94c1c78f5 100644 --- a/x/fswap/keeper/grpc_query.go +++ b/x/fswap/keeper/grpc_query.go @@ -26,18 +26,18 @@ func (s QueryServer) Swapped(ctx context.Context, _ *types.QuerySwappedRequest) return nil, err } return &types.QuerySwappedResponse{ - OldCoinAmount: swapped.GetOldCoinAmount(), - NewCoinAmount: swapped.GetNewCoinAmount(), + FromCoinAmount: swapped.GetFromCoinAmount(), + ToCoinAmount: swapped.GetToCoinAmount(), }, nil } -func (s QueryServer) TotalSwappableAmount(ctx context.Context, _ *types.QueryTotalSwappableAmountRequest) (*types.QueryTotalSwappableAmountResponse, error) { +func (s QueryServer) TotalSwappableToCoinAmount(ctx context.Context, _ *types.QueryTotalSwappableToCoinAmountRequest) (*types.QueryTotalSwappableToCoinAmountResponse, error) { c := sdk.UnwrapSDKContext(ctx) amount, err := s.Keeper.getSwappableNewCoinAmount(c) if err != nil { - return &types.QueryTotalSwappableAmountResponse{}, err + return &types.QueryTotalSwappableToCoinAmountResponse{}, err } - return &types.QueryTotalSwappableAmountResponse{ - SwappableNewCoin: amount, + return &types.QueryTotalSwappableToCoinAmountResponse{ + SwappableAmount: amount, }, nil } diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index b164bd2fb4..91dbcc98c4 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -55,11 +55,11 @@ func (k Keeper) SwapInit(ctx sdk.Context, swapInit types.SwapInit) error { return err } swapped := types.Swapped{ - OldCoinAmount: sdk.Coin{ + FromCoinAmount: sdk.Coin{ Denom: swapInit.GetFromDenom(), Amount: sdk.ZeroInt(), }, - NewCoinAmount: sdk.Coin{ + ToCoinAmount: sdk.Coin{ Denom: swapInit.GetToDenom(), Amount: sdk.ZeroInt(), }, @@ -110,9 +110,9 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, oldCoinAmount sdk.Coi } if err := ctx.EventManager().EmitTypedEvent(&types.EventSwapCoins{ - Address: addr.String(), - OldCoinAmount: oldCoinAmount, - NewCoinAmount: newCoinAmount, + Address: addr.String(), + FromCoinAmount: oldCoinAmount, + ToCoinAmount: newCoinAmount, }); err != nil { return err } @@ -190,7 +190,7 @@ func (k Keeper) getSwapped(ctx sdk.Context) (types.Swapped, error) { func (k Keeper) setSwapped(ctx sdk.Context, swapped types.Swapped) error { store := ctx.KVStore(k.storeKey) - key := swappedKey(swapped.NewCoinAmount.Denom) + key := swappedKey(swapped.ToCoinAmount.Denom) bz, err := k.cdc.Marshal(&swapped) if err != nil { return err @@ -238,7 +238,7 @@ func (k Keeper) getSwappableNewCoinAmount(ctx sdk.Context) (sdk.Coin, error) { return sdk.Coin{}, err } - remainingAmount := swapCap.Sub(swapped.GetNewCoinAmount().Amount) + remainingAmount := swapCap.Sub(swapped.GetToCoinAmount().Amount) return sdk.NewCoin(denom, remainingAmount), nil } @@ -304,8 +304,8 @@ func (k Keeper) updateSwapped(ctx sdk.Context, oldAmount, newAmount sdk.Coin) er return err } updatedSwapped := &types.Swapped{ - OldCoinAmount: oldAmount.Add(prevSwapped.OldCoinAmount), - NewCoinAmount: newAmount.Add(prevSwapped.NewCoinAmount), + FromCoinAmount: oldAmount.Add(prevSwapped.FromCoinAmount), + ToCoinAmount: newAmount.Add(prevSwapped.ToCoinAmount), } store := ctx.KVStore(k.storeKey) @@ -328,7 +328,7 @@ func (k Keeper) checkSwapCap(ctx sdk.Context, newCoinAmount sdk.Coin) error { return err } - if swapCap.LT(swapped.NewCoinAmount.Add(newCoinAmount).Amount) { + if swapCap.LT(swapped.ToCoinAmount.Add(newCoinAmount).Amount) { return fmt.Errorf("cann't swap more because of swapCap limit, amount=%s", newCoinAmount.String()) } diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 0e624c38b4..86e2bca4c8 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -26,13 +26,13 @@ type KeeperTestSuite struct { queryServer types.QueryServer msgServer types.MsgServer - accWithOldCoin sdk.AccAddress - accWithNewCoin sdk.AccAddress - initBalance sdk.Int + accWithFromCoin sdk.AccAddress + accWithToCoin sdk.AccAddress + initBalance sdk.Int swapInit types.SwapInit - oldDenom string - newDenom string + fromDenom string + toDenom string swapMultiple sdk.Int swapCap sdk.Int } @@ -66,15 +66,15 @@ func (s *KeeperTestSuite) SetupTest() { s.queryServer = keeper.NewQueryServer(s.keeper) s.msgServer = keeper.NewMsgServer(s.keeper) - s.oldDenom = "old" - s.newDenom = "new" + s.fromDenom = "fromdenom" + s.toDenom = "todenom" s.swapMultiple = sdk.NewInt(1000) s.initBalance = sdk.NewInt(123456789) numAcc := int64(2) s.swapCap = s.initBalance.Mul(s.swapMultiple.Mul(sdk.NewInt(numAcc))) s.swapInit = types.SwapInit{ - FromDenom: s.oldDenom, - ToDenom: s.newDenom, + FromDenom: s.fromDenom, + ToDenom: s.toDenom, AmountCapForToDenom: s.swapCap, SwapMultiple: s.swapMultiple, } @@ -84,20 +84,20 @@ func (s *KeeperTestSuite) SetupTest() { func (s *KeeperTestSuite) createAccountsWithInitBalance(app *simapp.SimApp) { addresses := []*sdk.AccAddress{ - &s.accWithOldCoin, - &s.accWithNewCoin, + &s.accWithFromCoin, + &s.accWithToCoin, } for i, address := range s.createRandomAccounts(len(addresses)) { *addresses[i] = address } minter := app.AccountKeeper.GetModuleAccount(s.ctx, minttypes.ModuleName).GetAddress() - oldAmount := sdk.NewCoins(sdk.NewCoin(s.oldDenom, s.initBalance)) - s.Require().NoError(app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, oldAmount)) - s.Require().NoError(app.BankKeeper.SendCoins(s.ctx, minter, s.accWithOldCoin, oldAmount)) + fromAmount := sdk.NewCoins(sdk.NewCoin(s.fromDenom, s.initBalance)) + s.Require().NoError(app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, fromAmount)) + s.Require().NoError(app.BankKeeper.SendCoins(s.ctx, minter, s.accWithFromCoin, fromAmount)) - newAmount := sdk.NewCoins(sdk.NewCoin(s.newDenom, s.initBalance)) - s.Require().NoError(app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, newAmount)) - s.Require().NoError(app.BankKeeper.SendCoins(s.ctx, minter, s.accWithNewCoin, newAmount)) + toAmount := sdk.NewCoins(sdk.NewCoin(s.toDenom, s.initBalance)) + s.Require().NoError(app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, toAmount)) + s.Require().NoError(app.BankKeeper.SendCoins(s.ctx, minter, s.accWithToCoin, toAmount)) } func TestKeeperTestSuite(t *testing.T) { @@ -105,8 +105,6 @@ func TestKeeperTestSuite(t *testing.T) { } func (s *KeeperTestSuite) TestSwap() { - err := s.keeper.SwapInit(s.ctx, s.swapInit) - s.Require().NoError(err) testCases := map[string]struct { from sdk.AccAddress amountToSwap sdk.Int @@ -115,21 +113,21 @@ func (s *KeeperTestSuite) TestSwap() { expectedError error }{ "swap some": { - s.accWithOldCoin, + s.accWithFromCoin, sdk.NewInt(100), sdk.NewInt(100), false, nil, }, "swap all the balance": { - s.accWithOldCoin, + s.accWithFromCoin, s.initBalance, s.initBalance, false, nil, }, "account holding new coin only": { - s.accWithNewCoin, + s.accWithToCoin, sdk.NewInt(100), s.initBalance, true, @@ -139,13 +137,17 @@ func (s *KeeperTestSuite) TestSwap() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.Swap(ctx, tc.from, sdk.NewCoin(s.oldDenom, tc.amountToSwap)) + err := s.keeper.SwapInit(ctx, s.swapInit) + s.Require().NoError(err) + + err = s.keeper.Swap(ctx, tc.from, sdk.NewCoin(s.fromDenom, tc.amountToSwap)) if tc.shouldThrowError { s.Require().ErrorIs(err, tc.expectedError) return } s.Require().NoError(err) - actualAmount := s.keeper.GetBalance(ctx, tc.from, s.newDenom).Amount + + actualAmount := s.keeper.GetBalance(ctx, tc.from, s.toDenom).Amount expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapMultiple) s.Require().Equal(expectedAmount, actualAmount) }) @@ -159,14 +161,14 @@ func (s *KeeperTestSuite) TestSwapAll() { shouldThrowError bool expectedError error }{ - "account holding old coin": { - s.accWithOldCoin, + "account holding from coin": { + s.accWithFromCoin, s.initBalance, false, nil, }, - "account holding new coin only": { - s.accWithNewCoin, + "account holding to coin only": { + s.accWithToCoin, s.initBalance, true, sdkerrors.ErrInsufficientFunds, @@ -175,13 +177,17 @@ func (s *KeeperTestSuite) TestSwapAll() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.SwapAll(ctx, tc.from) + err := s.keeper.SwapInit(ctx, s.swapInit) + s.Require().NoError(err) + + err = s.keeper.SwapAll(ctx, tc.from) if tc.shouldThrowError { s.Require().ErrorIs(err, tc.expectedError) return } s.Require().NoError(err) - actualAmount := s.keeper.GetBalance(ctx, tc.from, s.newDenom).Amount + + actualAmount := s.keeper.GetBalance(ctx, tc.from, s.toDenom).Amount expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapMultiple) s.Require().Equal(expectedAmount, actualAmount) }) diff --git a/x/fswap/types/event.pb.go b/x/fswap/types/event.pb.go index 5808cc6e7f..6461787a83 100644 --- a/x/fswap/types/event.pb.go +++ b/x/fswap/types/event.pb.go @@ -27,10 +27,10 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type EventSwapCoins struct { // holder's address Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` - // amount of the old currency - OldCoinAmount types.Coin `protobuf:"bytes,2,opt,name=old_coin_amount,json=oldCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"old_coin_amount"` - // amount of the new currency - NewCoinAmount types.Coin `protobuf:"bytes,3,opt,name=new_coin_amount,json=newCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"new_coin_amount"` + // from-coin amount + FromCoinAmount types.Coin `protobuf:"bytes,2,opt,name=from_coin_amount,json=fromCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"from_coin_amount"` + // to-coin amount + ToCoinAmount types.Coin `protobuf:"bytes,3,opt,name=to_coin_amount,json=toCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"to_coin_amount"` } func (m *EventSwapCoins) Reset() { *m = EventSwapCoins{} } @@ -73,16 +73,16 @@ func (m *EventSwapCoins) GetAddress() string { return "" } -func (m *EventSwapCoins) GetOldCoinAmount() types.Coin { +func (m *EventSwapCoins) GetFromCoinAmount() types.Coin { if m != nil { - return m.OldCoinAmount + return m.FromCoinAmount } return types.Coin{} } -func (m *EventSwapCoins) GetNewCoinAmount() types.Coin { +func (m *EventSwapCoins) GetToCoinAmount() types.Coin { if m != nil { - return m.NewCoinAmount + return m.ToCoinAmount } return types.Coin{} } @@ -94,25 +94,26 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/event.proto", fileDescriptor_92d5edbd64a725af) } var fileDescriptor_92d5edbd64a725af = []byte{ - // 288 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x91, 0x3f, 0x4e, 0xc3, 0x30, - 0x1c, 0x85, 0xe3, 0x22, 0x81, 0x08, 0xff, 0xa4, 0x88, 0x21, 0x74, 0x70, 0x2b, 0xa6, 0x4a, 0x08, - 0x5b, 0xa1, 0x27, 0xa0, 0x08, 0xc4, 0x5c, 0x36, 0x96, 0xca, 0x4e, 0xdc, 0x34, 0x22, 0xf1, 0x2f, - 0xaa, 0x5d, 0x07, 0x6e, 0xc1, 0xcc, 0x11, 0x38, 0x49, 0xc7, 0x8e, 0x4c, 0x80, 0x92, 0x8b, 0x20, - 0x3b, 0x41, 0x88, 0x89, 0x89, 0xed, 0x59, 0xcf, 0xef, 0xfb, 0x24, 0xdb, 0x0f, 0x73, 0x5e, 0xd0, - 0xb9, 0xaa, 0x58, 0x49, 0x4d, 0x44, 0x85, 0x11, 0x52, 0x93, 0x72, 0x09, 0x1a, 0x82, 0xfd, 0x9c, - 0x17, 0xc4, 0x35, 0xc4, 0x44, 0xfd, 0xe3, 0x14, 0x52, 0x70, 0x05, 0xb5, 0xa9, 0xbd, 0xd3, 0xc7, - 0x31, 0xa8, 0x02, 0x14, 0xe5, 0x4c, 0x09, 0x6a, 0x22, 0x2e, 0x34, 0x8b, 0x68, 0x0c, 0x99, 0x6c, - 0xfb, 0xd3, 0x97, 0x9e, 0x7f, 0x78, 0x6d, 0x99, 0x77, 0x15, 0x2b, 0xaf, 0x20, 0x93, 0x2a, 0x08, - 0xfd, 0x1d, 0x96, 0x24, 0x4b, 0xa1, 0x54, 0x88, 0x86, 0x68, 0xb4, 0x3b, 0xfd, 0x3e, 0x06, 0xc6, - 0x3f, 0x82, 0x3c, 0x99, 0xd9, 0xf9, 0x8c, 0x15, 0xb0, 0x92, 0x3a, 0xec, 0x0d, 0xd1, 0x68, 0xef, - 0xe2, 0x84, 0xb4, 0x1a, 0x62, 0x35, 0xa4, 0xd3, 0x10, 0x8b, 0x9b, 0x8c, 0xd7, 0xef, 0x03, 0xef, - 0xf5, 0x63, 0x70, 0x96, 0x66, 0x7a, 0xb1, 0xe2, 0x24, 0x86, 0x82, 0xde, 0x64, 0x52, 0xc5, 0x8b, - 0x8c, 0xd1, 0x79, 0x17, 0xce, 0x55, 0xf2, 0x40, 0xf5, 0x53, 0x29, 0x94, 0x1b, 0x4d, 0x0f, 0x20, - 0x4f, 0x6c, 0xb8, 0x74, 0x12, 0xeb, 0x95, 0xa2, 0xfa, 0xe5, 0xdd, 0xfa, 0x1f, 0xaf, 0x14, 0xd5, - 0x8f, 0x77, 0x72, 0xbb, 0xae, 0x31, 0xda, 0xd4, 0x18, 0x7d, 0xd6, 0x18, 0x3d, 0x37, 0xd8, 0xdb, - 0x34, 0xd8, 0x7b, 0x6b, 0xb0, 0x77, 0x4f, 0xfe, 0xa4, 0x3e, 0x76, 0x7f, 0xe6, 0xe8, 0x7c, 0xdb, - 0xbd, 0xf6, 0xf8, 0x2b, 0x00, 0x00, 0xff, 0xff, 0x2b, 0x08, 0x6b, 0x7c, 0xcd, 0x01, 0x00, 0x00, + // 289 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x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3a, 0xaf, 0x1f, 0xc3, 0xf3, 0x34, 0xd3, 0x8b, 0x15, 0x27, 0x11, 0x48, 0x7a, 0x9b, 0xe5, 0x2a, + 0x5a, 0x64, 0x8c, 0x26, 0x5d, 0xb8, 0x50, 0xf1, 0x23, 0xd5, 0x4f, 0x85, 0x50, 0x76, 0x34, 0xf3, + 0x8c, 0xc7, 0xa4, 0x2b, 0x6b, 0xf1, 0xb5, 0xeb, 0x69, 0xf8, 0xe5, 0xdd, 0xfb, 0x17, 0x6f, 0x5f, + 0xc3, 0x8f, 0x75, 0x7a, 0xb7, 0xad, 0x31, 0xda, 0xd5, 0x18, 0x7d, 0xd6, 0x18, 0x3d, 0x37, 0xd8, + 0xd9, 0x35, 0xd8, 0x79, 0x6b, 0xb0, 0xf3, 0x40, 0xfe, 0x84, 0x6e, 0xba, 0x3f, 0xb3, 0x70, 0xbe, + 0x6f, 0x5f, 0x7b, 0xf2, 0x15, 0x00, 0x00, 0xff, 0xff, 0xe2, 0xc5, 0x6e, 0x30, 0xcd, 0x01, 0x00, + 0x00, } func (m *EventSwapCoins) Marshal() (dAtA []byte, err error) { @@ -136,7 +137,7 @@ func (m *EventSwapCoins) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size, err := m.NewCoinAmount.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.ToCoinAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -146,7 +147,7 @@ func (m *EventSwapCoins) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x1a { - size, err := m.OldCoinAmount.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.FromCoinAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -186,9 +187,9 @@ func (m *EventSwapCoins) Size() (n int) { if l > 0 { n += 1 + l + sovEvent(uint64(l)) } - l = m.OldCoinAmount.Size() + l = m.FromCoinAmount.Size() n += 1 + l + sovEvent(uint64(l)) - l = m.NewCoinAmount.Size() + l = m.ToCoinAmount.Size() n += 1 + l + sovEvent(uint64(l)) return n } @@ -262,7 +263,7 @@ func (m *EventSwapCoins) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field OldCoinAmount", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field FromCoinAmount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -289,13 +290,13 @@ func (m *EventSwapCoins) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.OldCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.FromCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 3: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field NewCoinAmount", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field ToCoinAmount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -322,7 +323,7 @@ func (m *EventSwapCoins) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.NewCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.ToCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/fswap/types/fswap.go b/x/fswap/types/fswap.go index 722e8c46a5..8c226e0425 100644 --- a/x/fswap/types/fswap.go +++ b/x/fswap/types/fswap.go @@ -14,8 +14,8 @@ func NewSwapped( newCoinAmount sdk.Coin, ) Swapped { return Swapped{ - OldCoinAmount: oldCoinAmount, - NewCoinAmount: newCoinAmount, + FromCoinAmount: oldCoinAmount, + ToCoinAmount: newCoinAmount, } } @@ -40,10 +40,10 @@ func validateCoinAmount(i interface{}) error { // Validate validates the set of swapped func (s Swapped) Validate() error { - if err := validateCoinAmount(s.OldCoinAmount); err != nil { + if err := validateCoinAmount(s.FromCoinAmount); err != nil { return err } - if err := validateCoinAmount(s.NewCoinAmount); err != nil { + if err := validateCoinAmount(s.ToCoinAmount); err != nil { return err } return nil diff --git a/x/fswap/types/fswap.pb.go b/x/fswap/types/fswap.pb.go index 9b50fa5406..f406f4dc63 100644 --- a/x/fswap/types/fswap.pb.go +++ b/x/fswap/types/fswap.pb.go @@ -139,8 +139,8 @@ func (m *SwapInitProposal) GetSwapInit() SwapInit { } type Swapped struct { - OldCoinAmount types.Coin `protobuf:"bytes,1,opt,name=old_coin_amount,json=oldCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"old_coin_amount"` - NewCoinAmount types.Coin `protobuf:"bytes,2,opt,name=new_coin_amount,json=newCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"new_coin_amount"` + FromCoinAmount types.Coin `protobuf:"bytes,1,opt,name=from_coin_amount,json=fromCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"from_coin_amount"` + ToCoinAmount types.Coin `protobuf:"bytes,2,opt,name=to_coin_amount,json=toCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"to_coin_amount"` } func (m *Swapped) Reset() { *m = Swapped{} } @@ -175,16 +175,16 @@ func (m *Swapped) XXX_DiscardUnknown() { var xxx_messageInfo_Swapped proto.InternalMessageInfo -func (m *Swapped) GetOldCoinAmount() types.Coin { +func (m *Swapped) GetFromCoinAmount() types.Coin { if m != nil { - return m.OldCoinAmount + return m.FromCoinAmount } return types.Coin{} } -func (m *Swapped) GetNewCoinAmount() types.Coin { +func (m *Swapped) GetToCoinAmount() types.Coin { if m != nil { - return m.NewCoinAmount + return m.ToCoinAmount } return types.Coin{} } @@ -199,35 +199,35 @@ func init() { proto.RegisterFile("lbm/fswap/v1/fswap.proto", fileDescriptor_42ca var fileDescriptor_42ca60eaf37a2b67 = []byte{ // 465 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0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 0x4d, 0xd6, 0xd8, 0x67, 0x8d, 0x8f, 0x80, 0x8b, 0xc1, 0x81, 0x61, 0xfe, 0xf4, 0x35, 0xb9, - 0xff, 0x8f, 0xee, 0x98, 0x8f, 0x86, 0x5b, 0x50, 0x16, 0xa6, 0x78, 0x64, 0x45, 0x8c, 0xae, 0x60, - 0xf3, 0x3f, 0x74, 0x1b, 0x97, 0xa3, 0x2b, 0xd8, 0xfc, 0xb7, 0xee, 0x61, 0xd3, 0xb8, 0x30, 0x78, - 0x72, 0xb6, 0x88, 0xc3, 0xf3, 0x45, 0x1c, 0x7e, 0x5b, 0xc4, 0xe1, 0xfb, 0x65, 0x1c, 0x9c, 0x2f, - 0xe3, 0xe0, 0xf3, 0x32, 0x0e, 0x5e, 0xe1, 0xbf, 0x72, 0xbf, 0xf1, 0xcf, 0xc7, 0x6a, 0x64, 0xd7, - 0xec, 0x8f, 0x7f, 0xf0, 0x33, 0x00, 0x00, 0xff, 0xff, 0x28, 0x05, 0xbc, 0x8c, 0x58, 0x03, 0x00, + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 0x3d, 0x6f, 0x13, 0x41, + 0x10, 0xf5, 0x19, 0x43, 0xec, 0xb5, 0x89, 0xa2, 0x23, 0x02, 0x27, 0x12, 0x77, 0x51, 0x2a, 0x04, + 0x62, 0x57, 0x76, 0x2a, 0xd2, 0xe1, 0xa0, 0x88, 0x14, 0x48, 0xc8, 0x20, 0x21, 0xd1, 0x9c, 0xf6, + 0xce, 0x67, 0x7b, 0xc5, 0xdd, 0xce, 0xea, 0x76, 0xec, 0x84, 0x5f, 0x01, 0x74, 0x94, 0xa9, 0xf9, + 0x25, 0x29, 0x53, 0x22, 0x8a, 0x80, 0xec, 0x86, 0x7f, 0x01, 0xda, 0x8f, 0x80, 0xa9, 0x40, 0x48, + 0xe9, 0x66, 0xe7, 0xcd, 0xbd, 0x37, 0xfb, 0xde, 0x2d, 0xe9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0x00, 0x00, 0xff, 0xff, 0x7f, 0x63, 0xad, 0x91, 0x58, 0x03, 0x00, 0x00, } @@ -419,7 +419,7 @@ func (m *Swapped) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size, err := m.NewCoinAmount.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.ToCoinAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -429,7 +429,7 @@ func (m *Swapped) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x12 { - size, err := m.OldCoinAmount.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.FromCoinAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -498,9 +498,9 @@ func (m *Swapped) Size() (n int) { } var l int _ = l - l = m.OldCoinAmount.Size() + l = m.FromCoinAmount.Size() n += 1 + l + sovFswap(uint64(l)) - l = m.NewCoinAmount.Size() + l = m.ToCoinAmount.Size() n += 1 + l + sovFswap(uint64(l)) return n } @@ -871,7 +871,7 @@ func (m *Swapped) Unmarshal(dAtA []byte) error { switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field OldCoinAmount", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field FromCoinAmount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -898,13 +898,13 @@ func (m *Swapped) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.OldCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.FromCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field NewCoinAmount", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field ToCoinAmount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -931,7 +931,7 @@ func (m *Swapped) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.NewCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.ToCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/fswap/types/genesis.go b/x/fswap/types/genesis.go index 9304fa6732..505efab380 100644 --- a/x/fswap/types/genesis.go +++ b/x/fswap/types/genesis.go @@ -37,7 +37,7 @@ func (gs GenesisState) Validate() error { return err } - if swapInit.AmountCapForToDenom.LT(swapped.GetNewCoinAmount().Amount) { + if swapInit.AmountCapForToDenom.LT(swapped.GetToCoinAmount().Amount) { return ErrExceedSwappable } return nil diff --git a/x/fswap/types/query.pb.go b/x/fswap/types/query.pb.go index 8a69ac7609..3478a1db06 100644 --- a/x/fswap/types/query.pb.go +++ b/x/fswap/types/query.pb.go @@ -67,8 +67,8 @@ func (m *QuerySwappedRequest) XXX_DiscardUnknown() { var xxx_messageInfo_QuerySwappedRequest proto.InternalMessageInfo type QuerySwappedResponse struct { - OldCoinAmount types.Coin `protobuf:"bytes,1,opt,name=old_coin_amount,json=oldCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"old_coin_amount"` - NewCoinAmount types.Coin `protobuf:"bytes,2,opt,name=new_coin_amount,json=newCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"new_coin_amount"` + FromCoinAmount types.Coin `protobuf:"bytes,1,opt,name=from_coin_amount,json=fromCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"from_coin_amount"` + ToCoinAmount types.Coin `protobuf:"bytes,2,opt,name=to_coin_amount,json=toCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"to_coin_amount"` } func (m *QuerySwappedResponse) Reset() { *m = QuerySwappedResponse{} } @@ -104,35 +104,37 @@ func (m *QuerySwappedResponse) XXX_DiscardUnknown() { var xxx_messageInfo_QuerySwappedResponse proto.InternalMessageInfo -func (m *QuerySwappedResponse) GetOldCoinAmount() types.Coin { +func (m *QuerySwappedResponse) GetFromCoinAmount() types.Coin { if m != nil { - return m.OldCoinAmount + return m.FromCoinAmount } return types.Coin{} } -func (m *QuerySwappedResponse) GetNewCoinAmount() types.Coin { +func (m *QuerySwappedResponse) GetToCoinAmount() types.Coin { if m != nil { - return m.NewCoinAmount + return m.ToCoinAmount } return types.Coin{} } -type QueryTotalSwappableAmountRequest struct { +type QueryTotalSwappableToCoinAmountRequest struct { } -func (m *QueryTotalSwappableAmountRequest) Reset() { *m = QueryTotalSwappableAmountRequest{} } -func (m *QueryTotalSwappableAmountRequest) String() string { return proto.CompactTextString(m) } -func (*QueryTotalSwappableAmountRequest) ProtoMessage() {} -func (*QueryTotalSwappableAmountRequest) Descriptor() ([]byte, []int) { +func (m *QueryTotalSwappableToCoinAmountRequest) Reset() { + *m = QueryTotalSwappableToCoinAmountRequest{} +} +func (m *QueryTotalSwappableToCoinAmountRequest) String() string { return proto.CompactTextString(m) } +func (*QueryTotalSwappableToCoinAmountRequest) ProtoMessage() {} +func (*QueryTotalSwappableToCoinAmountRequest) Descriptor() ([]byte, []int) { return fileDescriptor_01deae9da7816d6a, []int{2} } -func (m *QueryTotalSwappableAmountRequest) XXX_Unmarshal(b []byte) error { +func (m *QueryTotalSwappableToCoinAmountRequest) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) } -func (m *QueryTotalSwappableAmountRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { +func (m *QueryTotalSwappableToCoinAmountRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { if deterministic { - return xxx_messageInfo_QueryTotalSwappableAmountRequest.Marshal(b, m, deterministic) + return xxx_messageInfo_QueryTotalSwappableToCoinAmountRequest.Marshal(b, m, deterministic) } else { b = b[:cap(b)] n, err := m.MarshalToSizedBuffer(b) @@ -142,34 +144,36 @@ func (m *QueryTotalSwappableAmountRequest) XXX_Marshal(b []byte, deterministic b return b[:n], nil } } -func (m *QueryTotalSwappableAmountRequest) XXX_Merge(src proto.Message) { - xxx_messageInfo_QueryTotalSwappableAmountRequest.Merge(m, src) +func (m *QueryTotalSwappableToCoinAmountRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_QueryTotalSwappableToCoinAmountRequest.Merge(m, src) } -func (m *QueryTotalSwappableAmountRequest) XXX_Size() int { +func (m *QueryTotalSwappableToCoinAmountRequest) XXX_Size() int { return m.Size() } -func (m *QueryTotalSwappableAmountRequest) XXX_DiscardUnknown() { - xxx_messageInfo_QueryTotalSwappableAmountRequest.DiscardUnknown(m) +func (m *QueryTotalSwappableToCoinAmountRequest) XXX_DiscardUnknown() { + xxx_messageInfo_QueryTotalSwappableToCoinAmountRequest.DiscardUnknown(m) } -var xxx_messageInfo_QueryTotalSwappableAmountRequest proto.InternalMessageInfo +var xxx_messageInfo_QueryTotalSwappableToCoinAmountRequest proto.InternalMessageInfo -type QueryTotalSwappableAmountResponse struct { - SwappableNewCoin types.Coin `protobuf:"bytes,1,opt,name=swappable_new_coin,json=swappableNewCoin,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"swappable_new_coin"` +type QueryTotalSwappableToCoinAmountResponse struct { + SwappableAmount types.Coin `protobuf:"bytes,1,opt,name=swappable_amount,json=swappableAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"swappable_amount"` } -func (m *QueryTotalSwappableAmountResponse) Reset() { *m = QueryTotalSwappableAmountResponse{} } -func (m *QueryTotalSwappableAmountResponse) String() string { return proto.CompactTextString(m) } -func (*QueryTotalSwappableAmountResponse) ProtoMessage() {} -func (*QueryTotalSwappableAmountResponse) Descriptor() ([]byte, []int) { +func (m *QueryTotalSwappableToCoinAmountResponse) Reset() { + *m = QueryTotalSwappableToCoinAmountResponse{} +} +func (m *QueryTotalSwappableToCoinAmountResponse) String() string { return proto.CompactTextString(m) } +func (*QueryTotalSwappableToCoinAmountResponse) ProtoMessage() {} +func (*QueryTotalSwappableToCoinAmountResponse) Descriptor() ([]byte, []int) { return fileDescriptor_01deae9da7816d6a, []int{3} } -func (m *QueryTotalSwappableAmountResponse) XXX_Unmarshal(b []byte) error { +func (m *QueryTotalSwappableToCoinAmountResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) } -func (m *QueryTotalSwappableAmountResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { +func (m *QueryTotalSwappableToCoinAmountResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { if deterministic { - return xxx_messageInfo_QueryTotalSwappableAmountResponse.Marshal(b, m, deterministic) + return xxx_messageInfo_QueryTotalSwappableToCoinAmountResponse.Marshal(b, m, deterministic) } else { b = b[:cap(b)] n, err := m.MarshalToSizedBuffer(b) @@ -179,21 +183,21 @@ func (m *QueryTotalSwappableAmountResponse) XXX_Marshal(b []byte, deterministic return b[:n], nil } } -func (m *QueryTotalSwappableAmountResponse) XXX_Merge(src proto.Message) { - xxx_messageInfo_QueryTotalSwappableAmountResponse.Merge(m, src) +func (m *QueryTotalSwappableToCoinAmountResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_QueryTotalSwappableToCoinAmountResponse.Merge(m, src) } -func (m *QueryTotalSwappableAmountResponse) XXX_Size() int { +func (m *QueryTotalSwappableToCoinAmountResponse) XXX_Size() int { return m.Size() } -func (m *QueryTotalSwappableAmountResponse) XXX_DiscardUnknown() { - xxx_messageInfo_QueryTotalSwappableAmountResponse.DiscardUnknown(m) +func (m *QueryTotalSwappableToCoinAmountResponse) XXX_DiscardUnknown() { + xxx_messageInfo_QueryTotalSwappableToCoinAmountResponse.DiscardUnknown(m) } -var xxx_messageInfo_QueryTotalSwappableAmountResponse proto.InternalMessageInfo +var xxx_messageInfo_QueryTotalSwappableToCoinAmountResponse proto.InternalMessageInfo -func (m *QueryTotalSwappableAmountResponse) GetSwappableNewCoin() types.Coin { +func (m *QueryTotalSwappableToCoinAmountResponse) GetSwappableAmount() types.Coin { if m != nil { - return m.SwappableNewCoin + return m.SwappableAmount } return types.Coin{} } @@ -201,42 +205,42 @@ func (m *QueryTotalSwappableAmountResponse) GetSwappableNewCoin() types.Coin { func init() { proto.RegisterType((*QuerySwappedRequest)(nil), "lbm.fswap.v1.QuerySwappedRequest") proto.RegisterType((*QuerySwappedResponse)(nil), "lbm.fswap.v1.QuerySwappedResponse") - proto.RegisterType((*QueryTotalSwappableAmountRequest)(nil), "lbm.fswap.v1.QueryTotalSwappableAmountRequest") - proto.RegisterType((*QueryTotalSwappableAmountResponse)(nil), "lbm.fswap.v1.QueryTotalSwappableAmountResponse") + proto.RegisterType((*QueryTotalSwappableToCoinAmountRequest)(nil), "lbm.fswap.v1.QueryTotalSwappableToCoinAmountRequest") + proto.RegisterType((*QueryTotalSwappableToCoinAmountResponse)(nil), "lbm.fswap.v1.QueryTotalSwappableToCoinAmountResponse") } func init() { proto.RegisterFile("lbm/fswap/v1/query.proto", fileDescriptor_01deae9da7816d6a) } var fileDescriptor_01deae9da7816d6a = []byte{ - // 441 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0xc1, 0xaa, 0xd3, 0x40, - 0x14, 0x86, 0x93, 0x80, 0x0a, 0xa3, 0xa2, 0x8c, 0xbd, 0x78, 0x0d, 0x9a, 0x7b, 0x6f, 0x36, 0x2a, - 0xe2, 0x0c, 0xb9, 0xf7, 0x09, 0xac, 0x20, 0xae, 0x04, 0xab, 0x2b, 0x37, 0x65, 0x92, 0x4c, 0xd3, - 0x60, 0x32, 0x27, 0xed, 0x4c, 0x12, 0x0b, 0xae, 0x5c, 0xb8, 0x16, 0x7c, 0x02, 0x71, 0x23, 0x3e, - 0x49, 0x97, 0x05, 0x37, 0xae, 0x54, 0x5a, 0x1f, 0x44, 0x32, 0x99, 0xaa, 0x81, 0x50, 0xdd, 0x74, - 0x77, 0x98, 0xff, 0x64, 0xbe, 0x7f, 0xfe, 0x73, 0x82, 0x0e, 0xb3, 0x30, 0xa7, 0x13, 0x59, 0xb3, - 0x82, 0x56, 0x01, 0x9d, 0x95, 0x7c, 0xbe, 0x20, 0xc5, 0x1c, 0x14, 0xe0, 0x4b, 0x59, 0x98, 0x13, - 0xad, 0x90, 0x2a, 0x70, 0x6f, 0x26, 0x00, 0x49, 0xc6, 0x29, 0x2b, 0x52, 0xca, 0x84, 0x00, 0xc5, - 0x54, 0x0a, 0x42, 0xb6, 0xbd, 0xee, 0x20, 0x81, 0x04, 0x74, 0x49, 0x9b, 0xca, 0x9c, 0x7a, 0x11, - 0xc8, 0x1c, 0x24, 0x0d, 0x99, 0xe4, 0xb4, 0x0a, 0x42, 0xae, 0x58, 0x40, 0x23, 0x48, 0x85, 0xd1, - 0xbb, 0xec, 0x16, 0xa5, 0x15, 0xff, 0x00, 0x5d, 0x7b, 0xda, 0x58, 0x79, 0x56, 0xb3, 0xa2, 0xe0, - 0xf1, 0x88, 0xcf, 0x4a, 0x2e, 0x95, 0xff, 0xd6, 0x41, 0x83, 0xee, 0xb9, 0x2c, 0x40, 0x48, 0x8e, - 0x2b, 0x74, 0x05, 0xb2, 0x78, 0xdc, 0xdc, 0x3d, 0x66, 0x39, 0x94, 0x42, 0x1d, 0xda, 0xc7, 0xf6, - 0x9d, 0x8b, 0xa7, 0x37, 0x48, 0xeb, 0x81, 0x34, 0x1e, 0x88, 0xf1, 0x40, 0x1e, 0x42, 0x2a, 0x86, - 0x67, 0xcb, 0x6f, 0x47, 0xd6, 0xe7, 0xef, 0x47, 0xf7, 0x92, 0x54, 0x4d, 0xcb, 0x90, 0x44, 0x90, - 0xd3, 0x47, 0xa9, 0x90, 0xd1, 0x34, 0x65, 0x74, 0x62, 0x8a, 0xfb, 0x32, 0x7e, 0x49, 0xd5, 0xa2, - 0xe0, 0x52, 0x7f, 0x34, 0xba, 0x0c, 0x59, 0xdc, 0x14, 0x0f, 0x34, 0xa4, 0xe1, 0x0a, 0x5e, 0x77, - 0xb8, 0xce, 0x7e, 0xb8, 0x82, 0xd7, 0x7f, 0xb8, 0xbe, 0x8f, 0x8e, 0x75, 0x0e, 0xcf, 0x41, 0xb1, - 0x4c, 0x87, 0xc1, 0xc2, 0x8c, 0xb7, 0xe2, 0x36, 0xac, 0x0f, 0x36, 0x3a, 0xd9, 0xd1, 0x64, 0x92, - 0x7b, 0x8d, 0xb0, 0xdc, 0x4a, 0xe3, 0xed, 0x5b, 0xf6, 0x14, 0xde, 0xd5, 0xdf, 0xa4, 0x27, 0xed, - 0x6b, 0x4e, 0x3f, 0x3a, 0xe8, 0x9c, 0xf6, 0x88, 0x01, 0x5d, 0x30, 0x43, 0xc5, 0x27, 0xe4, 0xef, - 0xcd, 0x23, 0x3d, 0x8b, 0xe0, 0xfa, 0xbb, 0x5a, 0xda, 0x97, 0xf9, 0xb7, 0xde, 0x7c, 0xf9, 0xf9, - 0xde, 0xb9, 0x8e, 0x0f, 0x68, 0x67, 0xcd, 0xa4, 0xa1, 0x7c, 0xb2, 0xd1, 0xa0, 0x2f, 0x19, 0x4c, - 0x7a, 0xee, 0xde, 0x91, 0xb3, 0x4b, 0xff, 0xbb, 0xdf, 0x18, 0xa3, 0xda, 0xd8, 0x5d, 0x7c, 0xbb, - 0xc7, 0x58, 0x67, 0x0c, 0x66, 0xa5, 0x86, 0x8f, 0x97, 0x6b, 0xcf, 0x5e, 0xad, 0x3d, 0xfb, 0xc7, - 0xda, 0xb3, 0xdf, 0x6d, 0x3c, 0x6b, 0xb5, 0xf1, 0xac, 0xaf, 0x1b, 0xcf, 0x7a, 0x41, 0xfe, 0x19, - 0xff, 0x2b, 0x03, 0xd0, 0x63, 0x08, 0xcf, 0xeb, 0xdf, 0xeb, 0xec, 0x57, 0x00, 0x00, 0x00, 0xff, - 0xff, 0x63, 0x79, 0x56, 0x3c, 0xf6, 0x03, 0x00, 0x00, + // 444 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0x3f, 0x8f, 0xd3, 0x30, + 0x18, 0xc6, 0x93, 0x48, 0x80, 0x64, 0x4e, 0xc7, 0xc9, 0xdc, 0x89, 0x23, 0x82, 0x1c, 0x64, 0xe0, + 0x0e, 0x21, 0x6c, 0xa5, 0x85, 0x0f, 0x40, 0x91, 0x10, 0x2b, 0xa5, 0x13, 0x4b, 0xe5, 0xa4, 0x6e, + 0x1a, 0x91, 0xf8, 0x4d, 0x6b, 0xa7, 0x7f, 0x56, 0x06, 0x66, 0x24, 0xbe, 0x02, 0x13, 0x3b, 0x0b, + 0x9f, 0xa0, 0x63, 0x25, 0x16, 0x26, 0x40, 0x2d, 0x1f, 0x04, 0xc5, 0x71, 0xa1, 0x91, 0x0a, 0x15, + 0x43, 0xb7, 0x57, 0x7e, 0x1f, 0xe7, 0xf7, 0xe4, 0x79, 0x5f, 0xa3, 0xd3, 0x34, 0xcc, 0x68, 0x5f, + 0x4e, 0x58, 0x4e, 0xc7, 0x01, 0x1d, 0x16, 0x7c, 0x34, 0x23, 0xf9, 0x08, 0x14, 0xe0, 0x83, 0x34, + 0xcc, 0x88, 0xee, 0x90, 0x71, 0xe0, 0xde, 0x8a, 0x01, 0xe2, 0x94, 0x53, 0x96, 0x27, 0x94, 0x09, + 0x01, 0x8a, 0xa9, 0x04, 0x84, 0xac, 0xb4, 0xee, 0x71, 0x0c, 0x31, 0xe8, 0x92, 0x96, 0x95, 0x39, + 0xf5, 0x22, 0x90, 0x19, 0x48, 0x1a, 0x32, 0xc9, 0xe9, 0x38, 0x08, 0xb9, 0x62, 0x01, 0x8d, 0x20, + 0x11, 0xa6, 0x5f, 0x67, 0x57, 0x28, 0xdd, 0xf1, 0x4f, 0xd0, 0xf5, 0x17, 0xa5, 0x95, 0x97, 0x13, + 0x96, 0xe7, 0xbc, 0xd7, 0xe6, 0xc3, 0x82, 0x4b, 0xe5, 0xbf, 0x75, 0xd0, 0x71, 0xfd, 0x5c, 0xe6, + 0x20,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c7, 0xff, 0x79, 0xcb, 0x58, 0xa5, 0xda, 0xea, 0x7d, + 0x7c, 0xbe, 0xc5, 0xaa, 0x9e, 0x8a, 0xe0, 0x93, 0xcd, 0x65, 0x6b, 0x3d, 0x9f, 0x2f, 0x3d, 0x7b, + 0xb1, 0xf4, 0xec, 0x1f, 0x4b, 0xcf, 0x7e, 0xb7, 0xf2, 0xac, 0xc5, 0xca, 0xb3, 0xbe, 0xae, 0x3c, + 0xeb, 0x15, 0xd9, 0x39, 0x8f, 0xa9, 0x01, 0xe8, 0xb9, 0x84, 0x97, 0xf5, 0xcb, 0x6b, 0xfe, 0x0a, + 0x00, 0x00, 0xff, 0xff, 0x8c, 0x3e, 0xc5, 0x8e, 0x11, 0x04, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -251,11 +255,11 @@ const _ = grpc.SupportPackageIsVersion4 // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. type QueryClient interface { - // Swapped queries the current swapped status that includes a burnt amount of old coin and a minted amount of new - // coin. + // Swapped queries the current swapped status that includes a burnt amount of from-coin and a minted amount of + // to-coin. Swapped(ctx context.Context, in *QuerySwappedRequest, opts ...grpc.CallOption) (*QuerySwappedResponse, error) - // TotalSwappableAmount queries the current swappable amount for new coin. - TotalSwappableAmount(ctx context.Context, in *QueryTotalSwappableAmountRequest, opts ...grpc.CallOption) (*QueryTotalSwappableAmountResponse, error) + // TotalSwappableToCoinAmount queries the current swappable amount for to-coin. + TotalSwappableToCoinAmount(ctx context.Context, in *QueryTotalSwappableToCoinAmountRequest, opts ...grpc.CallOption) (*QueryTotalSwappableToCoinAmountResponse, error) } type queryClient struct { @@ -275,9 +279,9 @@ func (c *queryClient) Swapped(ctx context.Context, in *QuerySwappedRequest, opts return out, nil } -func (c *queryClient) TotalSwappableAmount(ctx context.Context, in *QueryTotalSwappableAmountRequest, opts ...grpc.CallOption) (*QueryTotalSwappableAmountResponse, error) { - out := new(QueryTotalSwappableAmountResponse) - err := c.cc.Invoke(ctx, "/lbm.fswap.v1.Query/TotalSwappableAmount", in, out, opts...) +func (c *queryClient) TotalSwappableToCoinAmount(ctx context.Context, in *QueryTotalSwappableToCoinAmountRequest, opts ...grpc.CallOption) (*QueryTotalSwappableToCoinAmountResponse, error) { + out := new(QueryTotalSwappableToCoinAmountResponse) + err := c.cc.Invoke(ctx, "/lbm.fswap.v1.Query/TotalSwappableToCoinAmount", in, out, opts...) if err != nil { return nil, err } @@ -286,11 +290,11 @@ func (c *queryClient) TotalSwappableAmount(ctx context.Context, in *QueryTotalSw // QueryServer is the server API for Query service. type QueryServer interface { - // Swapped queries the current swapped status that includes a burnt amount of old coin and a minted amount of new - // coin. + // Swapped queries the current swapped status that includes a burnt amount of from-coin and a minted amount of + // to-coin. Swapped(context.Context, *QuerySwappedRequest) (*QuerySwappedResponse, error) - // TotalSwappableAmount queries the current swappable amount for new coin. - TotalSwappableAmount(context.Context, *QueryTotalSwappableAmountRequest) (*QueryTotalSwappableAmountResponse, error) + // TotalSwappableToCoinAmount queries the current swappable amount for to-coin. + TotalSwappableToCoinAmount(context.Context, *QueryTotalSwappableToCoinAmountRequest) (*QueryTotalSwappableToCoinAmountResponse, error) } // UnimplementedQueryServer can be embedded to have forward compatible implementations. @@ -300,8 +304,8 @@ type UnimplementedQueryServer struct { func (*UnimplementedQueryServer) Swapped(ctx context.Context, req *QuerySwappedRequest) (*QuerySwappedResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method Swapped not implemented") } -func (*UnimplementedQueryServer) TotalSwappableAmount(ctx context.Context, req *QueryTotalSwappableAmountRequest) (*QueryTotalSwappableAmountResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method TotalSwappableAmount not implemented") +func (*UnimplementedQueryServer) TotalSwappableToCoinAmount(ctx context.Context, req *QueryTotalSwappableToCoinAmountRequest) (*QueryTotalSwappableToCoinAmountResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method TotalSwappableToCoinAmount not implemented") } func RegisterQueryServer(s grpc1.Server, srv QueryServer) { @@ -326,20 +330,20 @@ func _Query_Swapped_Handler(srv interface{}, ctx context.Context, dec func(inter return interceptor(ctx, in, info, handler) } -func _Query_TotalSwappableAmount_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(QueryTotalSwappableAmountRequest) +func _Query_TotalSwappableToCoinAmount_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(QueryTotalSwappableToCoinAmountRequest) if err := dec(in); err != nil { return nil, err } if interceptor == nil { - return srv.(QueryServer).TotalSwappableAmount(ctx, in) + return srv.(QueryServer).TotalSwappableToCoinAmount(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, - FullMethod: "/lbm.fswap.v1.Query/TotalSwappableAmount", + FullMethod: "/lbm.fswap.v1.Query/TotalSwappableToCoinAmount", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(QueryServer).TotalSwappableAmount(ctx, req.(*QueryTotalSwappableAmountRequest)) + return srv.(QueryServer).TotalSwappableToCoinAmount(ctx, req.(*QueryTotalSwappableToCoinAmountRequest)) } return interceptor(ctx, in, info, handler) } @@ -353,8 +357,8 @@ var _Query_serviceDesc = grpc.ServiceDesc{ Handler: _Query_Swapped_Handler, }, { - MethodName: "TotalSwappableAmount", - Handler: _Query_TotalSwappableAmount_Handler, + MethodName: "TotalSwappableToCoinAmount", + Handler: _Query_TotalSwappableToCoinAmount_Handler, }, }, Streams: []grpc.StreamDesc{}, @@ -405,7 +409,7 @@ func (m *QuerySwappedResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size, err := m.NewCoinAmount.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.ToCoinAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -415,7 +419,7 @@ func (m *QuerySwappedResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x12 { - size, err := m.OldCoinAmount.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.FromCoinAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -427,7 +431,7 @@ func (m *QuerySwappedResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } -func (m *QueryTotalSwappableAmountRequest) Marshal() (dAtA []byte, err error) { +func (m *QueryTotalSwappableToCoinAmountRequest) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) n, err := m.MarshalToSizedBuffer(dAtA[:size]) @@ -437,12 +441,12 @@ func (m *QueryTotalSwappableAmountRequest) Marshal() (dAtA []byte, err error) { return dAtA[:n], nil } -func (m *QueryTotalSwappableAmountRequest) MarshalTo(dAtA []byte) (int, error) { +func (m *QueryTotalSwappableToCoinAmountRequest) MarshalTo(dAtA []byte) (int, error) { size := m.Size() return m.MarshalToSizedBuffer(dAtA[:size]) } -func (m *QueryTotalSwappableAmountRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { +func (m *QueryTotalSwappableToCoinAmountRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) _ = i var l int @@ -450,7 +454,7 @@ func (m *QueryTotalSwappableAmountRequest) MarshalToSizedBuffer(dAtA []byte) (in return len(dAtA) - i, nil } -func (m *QueryTotalSwappableAmountResponse) Marshal() (dAtA []byte, err error) { +func (m *QueryTotalSwappableToCoinAmountResponse) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) n, err := m.MarshalToSizedBuffer(dAtA[:size]) @@ -460,18 +464,18 @@ func (m *QueryTotalSwappableAmountResponse) Marshal() (dAtA []byte, err error) { return dAtA[:n], nil } -func (m *QueryTotalSwappableAmountResponse) MarshalTo(dAtA []byte) (int, error) { +func (m *QueryTotalSwappableToCoinAmountResponse) MarshalTo(dAtA []byte) (int, error) { size := m.Size() return m.MarshalToSizedBuffer(dAtA[:size]) } -func (m *QueryTotalSwappableAmountResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { +func (m *QueryTotalSwappableToCoinAmountResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) _ = i var l int _ = l { - size, err := m.SwappableNewCoin.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.SwappableAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -509,14 +513,14 @@ func (m *QuerySwappedResponse) Size() (n int) { } var l int _ = l - l = m.OldCoinAmount.Size() + l = m.FromCoinAmount.Size() n += 1 + l + sovQuery(uint64(l)) - l = m.NewCoinAmount.Size() + l = m.ToCoinAmount.Size() n += 1 + l + sovQuery(uint64(l)) return n } -func (m *QueryTotalSwappableAmountRequest) Size() (n int) { +func (m *QueryTotalSwappableToCoinAmountRequest) Size() (n int) { if m == nil { return 0 } @@ -525,13 +529,13 @@ func (m *QueryTotalSwappableAmountRequest) Size() (n int) { return n } -func (m *QueryTotalSwappableAmountResponse) Size() (n int) { +func (m *QueryTotalSwappableToCoinAmountResponse) Size() (n int) { if m == nil { return 0 } var l int _ = l - l = m.SwappableNewCoin.Size() + l = m.SwappableAmount.Size() n += 1 + l + sovQuery(uint64(l)) return n } @@ -623,7 +627,7 @@ func (m *QuerySwappedResponse) Unmarshal(dAtA []byte) error { switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field OldCoinAmount", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field FromCoinAmount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -650,13 +654,13 @@ func (m *QuerySwappedResponse) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.OldCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.FromCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field NewCoinAmount", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field ToCoinAmount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -683,7 +687,7 @@ func (m *QuerySwappedResponse) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.NewCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.ToCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex @@ -708,7 +712,7 @@ func (m *QuerySwappedResponse) Unmarshal(dAtA []byte) error { } return nil } -func (m *QueryTotalSwappableAmountRequest) Unmarshal(dAtA []byte) error { +func (m *QueryTotalSwappableToCoinAmountRequest) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { @@ -731,10 +735,10 @@ func (m *QueryTotalSwappableAmountRequest) Unmarshal(dAtA []byte) error { fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { - return fmt.Errorf("proto: QueryTotalSwappableAmountRequest: wiretype end group for non-group") + return fmt.Errorf("proto: QueryTotalSwappableToCoinAmountRequest: wiretype end group for non-group") } if fieldNum <= 0 { - return fmt.Errorf("proto: QueryTotalSwappableAmountRequest: illegal tag %d (wire type %d)", fieldNum, wire) + return fmt.Errorf("proto: QueryTotalSwappableToCoinAmountRequest: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { default: @@ -758,7 +762,7 @@ func (m *QueryTotalSwappableAmountRequest) Unmarshal(dAtA []byte) error { } return nil } -func (m *QueryTotalSwappableAmountResponse) Unmarshal(dAtA []byte) error { +func (m *QueryTotalSwappableToCoinAmountResponse) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { @@ -781,15 +785,15 @@ func (m *QueryTotalSwappableAmountResponse) Unmarshal(dAtA []byte) error { fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { - return fmt.Errorf("proto: QueryTotalSwappableAmountResponse: wiretype end group for non-group") + return fmt.Errorf("proto: QueryTotalSwappableToCoinAmountResponse: wiretype end group for non-group") } if fieldNum <= 0 { - return fmt.Errorf("proto: QueryTotalSwappableAmountResponse: illegal tag %d (wire type %d)", fieldNum, wire) + return fmt.Errorf("proto: QueryTotalSwappableToCoinAmountResponse: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field SwappableNewCoin", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field SwappableAmount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -816,7 +820,7 @@ func (m *QueryTotalSwappableAmountResponse) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.SwappableNewCoin.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.SwappableAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/fswap/types/query.pb.gw.go b/x/fswap/types/query.pb.gw.go index fb7cadac5d..b657b521fd 100644 --- a/x/fswap/types/query.pb.gw.go +++ b/x/fswap/types/query.pb.gw.go @@ -49,20 +49,20 @@ func local_request_Query_Swapped_0(ctx context.Context, marshaler runtime.Marsha } -func request_Query_TotalSwappableAmount_0(ctx context.Context, marshaler runtime.Marshaler, client QueryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { - var protoReq QueryTotalSwappableAmountRequest +func request_Query_TotalSwappableToCoinAmount_0(ctx context.Context, marshaler runtime.Marshaler, client QueryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq QueryTotalSwappableToCoinAmountRequest var metadata runtime.ServerMetadata - msg, err := client.TotalSwappableAmount(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + msg, err := client.TotalSwappableToCoinAmount(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) return msg, metadata, err } -func local_request_Query_TotalSwappableAmount_0(ctx context.Context, marshaler runtime.Marshaler, server QueryServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { - var protoReq QueryTotalSwappableAmountRequest +func local_request_Query_TotalSwappableToCoinAmount_0(ctx context.Context, marshaler runtime.Marshaler, server QueryServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq QueryTotalSwappableToCoinAmountRequest var metadata runtime.ServerMetadata - msg, err := server.TotalSwappableAmount(ctx, &protoReq) + msg, err := server.TotalSwappableToCoinAmount(ctx, &protoReq) return msg, metadata, err } @@ -93,7 +93,7 @@ func RegisterQueryHandlerServer(ctx context.Context, mux *runtime.ServeMux, serv }) - mux.Handle("GET", pattern_Query_TotalSwappableAmount_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + mux.Handle("GET", pattern_Query_TotalSwappableToCoinAmount_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { ctx, cancel := context.WithCancel(req.Context()) defer cancel() inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) @@ -102,14 +102,14 @@ func RegisterQueryHandlerServer(ctx context.Context, mux *runtime.ServeMux, serv runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) return } - resp, md, err := local_request_Query_TotalSwappableAmount_0(rctx, inboundMarshaler, server, req, pathParams) + resp, md, err := local_request_Query_TotalSwappableToCoinAmount_0(rctx, inboundMarshaler, server, req, pathParams) ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) return } - forward_Query_TotalSwappableAmount_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + forward_Query_TotalSwappableToCoinAmount_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) }) @@ -174,7 +174,7 @@ func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, clie }) - mux.Handle("GET", pattern_Query_TotalSwappableAmount_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + mux.Handle("GET", pattern_Query_TotalSwappableToCoinAmount_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { ctx, cancel := context.WithCancel(req.Context()) defer cancel() inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) @@ -183,14 +183,14 @@ func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, clie runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) return } - resp, md, err := request_Query_TotalSwappableAmount_0(rctx, inboundMarshaler, client, req, pathParams) + resp, md, err := request_Query_TotalSwappableToCoinAmount_0(rctx, inboundMarshaler, client, req, pathParams) ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) return } - forward_Query_TotalSwappableAmount_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + forward_Query_TotalSwappableToCoinAmount_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) }) @@ -200,11 +200,11 @@ func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, clie var ( pattern_Query_Swapped_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"lbm", "fswap", "v1", "swapped"}, "", runtime.AssumeColonVerbOpt(false))) - pattern_Query_TotalSwappableAmount_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"lbm", "fswap", "v1", "swappable_new_coin_amount"}, "", runtime.AssumeColonVerbOpt(false))) + pattern_Query_TotalSwappableToCoinAmount_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"lbm", "fswap", "v1", "swappable_new_coin_amount"}, "", runtime.AssumeColonVerbOpt(false))) ) var ( forward_Query_Swapped_0 = runtime.ForwardResponseMessage - forward_Query_TotalSwappableAmount_0 = runtime.ForwardResponseMessage + forward_Query_TotalSwappableToCoinAmount_0 = runtime.ForwardResponseMessage ) diff --git a/x/fswap/types/tx.pb.go b/x/fswap/types/tx.pb.go index 682ec7260b..2738e7aabb 100644 --- a/x/fswap/types/tx.pb.go +++ b/x/fswap/types/tx.pb.go @@ -32,7 +32,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type MsgSwapRequest struct { // holder's address FromAddress string `protobuf:"bytes,1,opt,name=from_address,json=fromAddress,proto3" json:"from_address,omitempty"` - // amount of old currency + // from-coin amount Amount types.Coin `protobuf:"bytes,2,opt,name=amount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"amount"` } From 5195f88ab3a90eed18c43e1b855720de4c9afeb5 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Tue, 30 Apr 2024 16:43:32 +0900 Subject: [PATCH 14/47] chore: clean-up --- simapp/app.go | 2 +- x/fswap/client/proposal_handler.go | 2 +- x/fswap/keeper/grpc_query.go | 8 +++++--- x/fswap/keeper/msg_server.go | 9 +++++++++ 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 5072f33759..237c7e049e 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -131,7 +131,7 @@ var ( upgradeclient.ProposalHandler, upgradeclient.CancelProposalHandler, foundationclient.ProposalHandler, - fswapclient.SwapInitHandler, + fswapclient.ProposalHandler, ), params.AppModuleBasic{}, crisis.AppModuleBasic{}, diff --git a/x/fswap/client/proposal_handler.go b/x/fswap/client/proposal_handler.go index 17a3b40eb3..78ef57b56d 100644 --- a/x/fswap/client/proposal_handler.go +++ b/x/fswap/client/proposal_handler.go @@ -5,4 +5,4 @@ import ( govclient "github.com/Finschia/finschia-sdk/x/gov/client" ) -var SwapInitHandler = govclient.NewProposalHandler(cli.NewCmdSwapInitProposal) +var ProposalHandler = govclient.NewProposalHandler(cli.NewCmdSwapInitProposal) diff --git a/x/fswap/keeper/grpc_query.go b/x/fswap/keeper/grpc_query.go index e94c1c78f5..44ce360b82 100644 --- a/x/fswap/keeper/grpc_query.go +++ b/x/fswap/keeper/grpc_query.go @@ -21,10 +21,12 @@ func NewQueryServer(keeper Keeper) *QueryServer { func (s QueryServer) Swapped(ctx context.Context, _ *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) { c := sdk.UnwrapSDKContext(ctx) + swapped, err := s.Keeper.getSwapped(c) if err != nil { return nil, err } + return &types.QuerySwappedResponse{ FromCoinAmount: swapped.GetFromCoinAmount(), ToCoinAmount: swapped.GetToCoinAmount(), @@ -33,11 +35,11 @@ func (s QueryServer) Swapped(ctx context.Context, _ *types.QuerySwappedRequest) func (s QueryServer) TotalSwappableToCoinAmount(ctx context.Context, _ *types.QueryTotalSwappableToCoinAmountRequest) (*types.QueryTotalSwappableToCoinAmountResponse, error) { c := sdk.UnwrapSDKContext(ctx) + amount, err := s.Keeper.getSwappableNewCoinAmount(c) if err != nil { return &types.QueryTotalSwappableToCoinAmountResponse{}, err } - return &types.QueryTotalSwappableToCoinAmountResponse{ - SwappableAmount: amount, - }, nil + + return &types.QueryTotalSwappableToCoinAmountResponse{SwappableAmount: amount}, nil } diff --git a/x/fswap/keeper/msg_server.go b/x/fswap/keeper/msg_server.go index 849037fb6b..7735574919 100644 --- a/x/fswap/keeper/msg_server.go +++ b/x/fswap/keeper/msg_server.go @@ -20,34 +20,43 @@ func NewMsgServer(keeper Keeper) *MsgServer { func (s MsgServer) Swap(ctx context.Context, req *types.MsgSwapRequest) (*types.MsgSwapResponse, error) { c := sdk.UnwrapSDKContext(ctx) + swapInit, err := s.keeper.getSwapInit(c) if err != nil { return &types.MsgSwapResponse{}, err } + if req.GetAmount().Denom != swapInit.GetFromDenom() { return nil, sdkerrors.ErrInvalidCoins } + from, err := sdk.AccAddressFromBech32(req.FromAddress) if err != nil { return nil, err } + if err := s.keeper.Swap(c, from, req.GetAmount()); err != nil { return nil, err } + return &types.MsgSwapResponse{}, nil } func (s MsgServer) SwapAll(ctx context.Context, req *types.MsgSwapAllRequest) (*types.MsgSwapAllResponse, error) { c := sdk.UnwrapSDKContext(ctx) + if !s.keeper.hasBeenInitialized(c) { return &types.MsgSwapAllResponse{}, types.ErrSwapNotInitilized } + from, err := sdk.AccAddressFromBech32(req.FromAddress) if err != nil { return nil, err } + if err := s.keeper.SwapAll(c, from); err != nil { return nil, err } + return &types.MsgSwapAllResponse{}, nil } From 6151a0cc760cec66ad106992915d9388a4ba6ed8 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Tue, 30 Apr 2024 19:38:09 +0900 Subject: [PATCH 15/47] chore: clean errors and rename --- docs/core/proto-docs.md | 2 +- proto/lbm/fswap/v1/tx.proto | 2 +- x/fswap/client/cli/tx.go | 4 +- x/fswap/keeper/expected_keepers.go | 3 +- x/fswap/keeper/genesis.go | 6 +- x/fswap/keeper/keeper.go | 74 ++++++++------ x/fswap/keeper/keeper_test.go | 106 +++++++++++++++++++-- x/fswap/keeper/msg_server.go | 6 +- x/fswap/testutil/expected_keepers_mocks.go | 12 +-- x/fswap/types/errors.go | 8 +- x/fswap/types/fswap.go | 51 ++++++---- x/fswap/types/genesis.go | 13 +-- x/fswap/types/msgs.go | 15 +-- x/fswap/types/tx.pb.go | 61 ++++++------ 14 files changed, 239 insertions(+), 124 deletions(-) diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index 684b66a19e..833f112287 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -12961,7 +12961,7 @@ GenesisState defines the fswap module's genesis state. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | | `from_address` | [string](#string) | | holder's address | -| `amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | from-coin amount | +| `from_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | from-coin amount | diff --git a/proto/lbm/fswap/v1/tx.proto b/proto/lbm/fswap/v1/tx.proto index 149abf400a..004819fa46 100644 --- a/proto/lbm/fswap/v1/tx.proto +++ b/proto/lbm/fswap/v1/tx.proto @@ -15,7 +15,7 @@ message MsgSwapRequest { // holder's address string from_address = 1; // from-coin amount - cosmos.base.v1beta1.Coin amount = 2 + cosmos.base.v1beta1.Coin from_coin_amount = 2 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; } diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index 6905ffa8b6..047071181c 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -61,8 +61,8 @@ func CmdTxMsgSwap() *cobra.Command { } msg := &types.MsgSwapRequest{ - FromAddress: clientCtx.GetFromAddress().String(), - Amount: coin, + FromAddress: clientCtx.GetFromAddress().String(), + FromCoinAmount: coin, } if err := msg.ValidateBasic(); err != nil { return err diff --git a/x/fswap/keeper/expected_keepers.go b/x/fswap/keeper/expected_keepers.go index f1a8330f1f..5b45bc1963 100644 --- a/x/fswap/keeper/expected_keepers.go +++ b/x/fswap/keeper/expected_keepers.go @@ -12,7 +12,8 @@ type ( BankKeeper interface { HasBalance(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coin) bool GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin - SendCoins(ctx sdk.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error + SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error + SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error } diff --git a/x/fswap/keeper/genesis.go b/x/fswap/keeper/genesis.go index 5d63779245..39ef32bf68 100644 --- a/x/fswap/keeper/genesis.go +++ b/x/fswap/keeper/genesis.go @@ -1,8 +1,6 @@ package keeper import ( - "fmt" - sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/fswap/types" ) @@ -10,10 +8,10 @@ import ( // InitGenesis initializes the module's state from a provided genesis state. func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error { if len(genState.GetSwapInit()) > 1 { - return fmt.Errorf("cannot initialize genesis state, there are more than 1 swapInit") + return types.ErrSwapCanNotBeInitializedTwice.Wrap("cannot initialize genesis state, there are more than 1 swapInit") } if len(genState.GetSwapped()) > 1 { - return fmt.Errorf("cannot initialize genesis state, there are more than 1 swapped") + return types.ErrSwapCanNotBeInitializedTwice.Wrap("cannot initialize genesis state, there are more than 1 swapped") } for _, swapInit := range genState.GetSwapInit() { if err := k.setSwapInit(ctx, swapInit); err != nil { diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 91dbcc98c4..2634a68b1b 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -1,7 +1,6 @@ package keeper import ( - "errors" "fmt" "github.com/tendermint/tendermint/libs/log" @@ -23,6 +22,7 @@ type Keeper struct { fromDenom string toDenom string + swapInit types.SwapInit swapMultiple sdk.Int swapCap sdk.Int } @@ -35,6 +35,7 @@ func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, ak AccountKe BankKeeper: bk, fromDenom: "", toDenom: "", + swapInit: types.SwapInit{}, swapMultiple: sdk.ZeroInt(), swapCap: sdk.ZeroInt(), } @@ -49,7 +50,7 @@ func (k Keeper) SwapInit(ctx sdk.Context, swapInit types.SwapInit) error { return err } if k.hasBeenInitialized(ctx) { - return errors.New("already initialized") + return types.ErrSwapCanNotBeInitializedTwice } if err := k.setSwapInit(ctx, swapInit); err != nil { return err @@ -70,30 +71,27 @@ func (k Keeper) SwapInit(ctx sdk.Context, swapInit types.SwapInit) error { return nil } -func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, oldCoinAmount sdk.Coin) error { - if ok := k.HasBalance(ctx, addr, oldCoinAmount); !ok { - return sdkerrors.ErrInsufficientFunds - } +func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Coin) error { swapInit, err := k.getSwapInit(ctx) if err != nil { return err } - if oldCoinAmount.GetDenom() != swapInit.GetFromDenom() { - return errors.New("denom mismatch") + + if fromCoinAmount.GetDenom() != swapInit.GetFromDenom() { + return sdkerrors.ErrInvalidRequest.Wrapf("denom mismatch, expected %s, got %s", swapInit.GetFromDenom(), fromCoinAmount.Denom) } - newAmount := oldCoinAmount.Amount.Mul(swapInit.SwapMultiple) + newAmount := fromCoinAmount.Amount.Mul(swapInit.SwapMultiple) newCoinAmount := sdk.NewCoin(swapInit.ToDenom, newAmount) if err := k.checkSwapCap(ctx, newCoinAmount); err != nil { return err } - moduleAddr := k.GetModuleAddress(types.ModuleName) - if err := k.SendCoins(ctx, addr, moduleAddr, sdk.NewCoins(oldCoinAmount)); err != nil { + if err := k.SendCoinsFromAccountToModule(ctx, addr, types.ModuleName, sdk.NewCoins(fromCoinAmount)); err != nil { return err } - if err := k.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(oldCoinAmount)); err != nil { + if err := k.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(fromCoinAmount)); err != nil { return err } @@ -101,17 +99,17 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, oldCoinAmount sdk.Coi return err } - if err := k.SendCoins(ctx, moduleAddr, addr, sdk.NewCoins(newCoinAmount)); err != nil { + if err := k.SendCoinsFromModuleToAccount(ctx, types.ModuleName, addr, sdk.NewCoins(newCoinAmount)); err != nil { return err } - if err := k.updateSwapped(ctx, oldCoinAmount, newCoinAmount); err != nil { + if err := k.updateSwapped(ctx, fromCoinAmount, newCoinAmount); err != nil { return err } if err := ctx.EventManager().EmitTypedEvent(&types.EventSwapCoins{ Address: addr.String(), - FromCoinAmount: oldCoinAmount, + FromCoinAmount: fromCoinAmount, ToCoinAmount: newCoinAmount, }); err != nil { return err @@ -171,16 +169,18 @@ func (k Keeper) iterateAllSwapped(ctx sdk.Context, cb func(swapped types.Swapped } func (k Keeper) getSwapped(ctx sdk.Context) (types.Swapped, error) { - newDenom, err := k.getToDenom(ctx) + toDenom, err := k.getToDenom(ctx) if err != nil { return types.Swapped{}, err } + store := ctx.KVStore(k.storeKey) - key := swappedKey(newDenom) + key := swappedKey(toDenom) bz := store.Get(key) if bz == nil { return types.Swapped{}, types.ErrSwappedNotFound } + swapped := types.Swapped{} if err := k.cdc.Unmarshal(bz, &swapped); err != nil { return types.Swapped{}, err @@ -195,6 +195,7 @@ func (k Keeper) setSwapped(ctx sdk.Context, swapped types.Swapped) error { if err != nil { return err } + store.Set(key, bz) return nil } @@ -214,7 +215,6 @@ func (k Keeper) iterateAllSwapInits(ctx sdk.Context, cb func(swapped types.SwapI iterator := swapInitDataStore.Iterator(nil, nil) defer iterator.Close() - for ; iterator.Valid(); iterator.Next() { swapInit := types.SwapInit{} k.cdc.MustUnmarshal(iterator.Value(), &swapInit) @@ -239,6 +239,7 @@ func (k Keeper) getSwappableNewCoinAmount(ctx sdk.Context) (sdk.Coin, error) { } remainingAmount := swapCap.Sub(swapped.GetToCoinAmount().Amount) + return sdk.NewCoin(denom, remainingAmount), nil } @@ -246,10 +247,12 @@ func (k Keeper) getFromDenom(ctx sdk.Context) (string, error) { if len(k.fromDenom) > 0 { return k.fromDenom, nil } + swapInit, err := k.getSwapInit(ctx) if err != nil { return "", err } + k.fromDenom = swapInit.GetFromDenom() return k.fromDenom, nil } @@ -258,10 +261,12 @@ func (k Keeper) getToDenom(ctx sdk.Context) (string, error) { if len(k.toDenom) > 0 { return k.toDenom, nil } + swapInit, err := k.getSwapInit(ctx) if err != nil { return "", err } + k.toDenom = swapInit.GetToDenom() return k.toDenom, nil } @@ -270,10 +275,12 @@ func (k Keeper) getSwapMultiple(ctx sdk.Context) (sdk.Int, error) { if k.swapMultiple.IsPositive() { return k.swapMultiple, nil } + swapInit, err := k.getSwapInit(ctx) if err != nil { - return sdk.ZeroInt(), err + return sdk.Int{}, err } + k.swapMultiple = swapInit.SwapMultiple return k.swapMultiple, nil } @@ -282,38 +289,47 @@ func (k Keeper) getSwapCap(ctx sdk.Context) (sdk.Int, error) { if k.swapCap.IsPositive() { return k.swapCap, nil } + swapInit, err := k.getSwapInit(ctx) if err != nil { - return sdk.ZeroInt(), err + return sdk.Int{}, err } + k.swapCap = swapInit.AmountCapForToDenom return k.swapCap, nil } func (k Keeper) getSwapInit(ctx sdk.Context) (types.SwapInit, error) { + if !k.swapInit.IsEmpty() { + return k.swapInit, nil + } + swapInits := k.getAllSwapInits(ctx) if len(swapInits) == 0 { - return types.SwapInit{}, types.ErrSwapNotInitilized + return types.SwapInit{}, types.ErrSwapNotInitialized } - return swapInits[0], nil + + k.swapInit = swapInits[0] + return k.swapInit, nil } -func (k Keeper) updateSwapped(ctx sdk.Context, oldAmount, newAmount sdk.Coin) error { +func (k Keeper) updateSwapped(ctx sdk.Context, fromAmount, toAmount sdk.Coin) error { prevSwapped, err := k.getSwapped(ctx) if err != nil { return err } + updatedSwapped := &types.Swapped{ - FromCoinAmount: oldAmount.Add(prevSwapped.FromCoinAmount), - ToCoinAmount: newAmount.Add(prevSwapped.ToCoinAmount), + FromCoinAmount: fromAmount.Add(prevSwapped.FromCoinAmount), + ToCoinAmount: toAmount.Add(prevSwapped.ToCoinAmount), } - store := ctx.KVStore(k.storeKey) - key := swappedKey(newAmount.Denom) bz, err := k.cdc.Marshal(updatedSwapped) if err != nil { return err } + + key := swappedKey(toAmount.Denom) store.Set(key, bz) return nil } @@ -323,15 +339,15 @@ func (k Keeper) checkSwapCap(ctx sdk.Context, newCoinAmount sdk.Coin) error { if err != nil { return err } + swapCap, err := k.getSwapCap(ctx) if err != nil { return err } if swapCap.LT(swapped.ToCoinAmount.Add(newCoinAmount).Amount) { - return fmt.Errorf("cann't swap more because of swapCap limit, amount=%s", newCoinAmount.String()) + return types.ErrExceedSwappableToCoinAmount } - return nil } diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 86e2bca4c8..f0cb62a5fe 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -107,32 +107,46 @@ func TestKeeperTestSuite(t *testing.T) { func (s *KeeperTestSuite) TestSwap() { testCases := map[string]struct { from sdk.AccAddress - amountToSwap sdk.Int + amountToSwap sdk.Coin expectedBalanceWithoutMultiply sdk.Int shouldThrowError bool expectedError error }{ "swap some": { s.accWithFromCoin, - sdk.NewInt(100), + sdk.NewCoin(s.fromDenom, sdk.NewInt(100)), sdk.NewInt(100), false, nil, }, "swap all the balance": { s.accWithFromCoin, - s.initBalance, + sdk.NewCoin(s.fromDenom, s.initBalance), s.initBalance, false, nil, }, + "swap without holding enough balance": { + s.accWithFromCoin, + sdk.NewCoin(s.fromDenom, sdk.OneInt().Add(s.initBalance)), + sdk.ZeroInt(), + true, + sdkerrors.ErrInsufficientFunds, + }, "account holding new coin only": { s.accWithToCoin, - sdk.NewInt(100), - s.initBalance, + sdk.NewCoin(s.fromDenom, sdk.NewInt(100)), + sdk.ZeroInt(), true, sdkerrors.ErrInsufficientFunds, }, + "swap with the same from-denom and to-denom": { + s.accWithFromCoin, + sdk.NewCoin(s.toDenom, s.initBalance), + sdk.ZeroInt(), + true, + sdkerrors.ErrInvalidRequest, + }, } for name, tc := range testCases { s.Run(name, func() { @@ -140,7 +154,7 @@ func (s *KeeperTestSuite) TestSwap() { err := s.keeper.SwapInit(ctx, s.swapInit) s.Require().NoError(err) - err = s.keeper.Swap(ctx, tc.from, sdk.NewCoin(s.fromDenom, tc.amountToSwap)) + err = s.keeper.Swap(ctx, tc.from, tc.amountToSwap) if tc.shouldThrowError { s.Require().ErrorIs(err, tc.expectedError) return @@ -193,3 +207,83 @@ func (s *KeeperTestSuite) TestSwapAll() { }) } } + +func (s *KeeperTestSuite) TestSwapInit() { + testCases := map[string]struct { + req types.SwapInit + shouldThrowError bool + expectedError error + }{ + "valid swapInit": { + types.SwapInit{ + FromDenom: "fromD", + ToDenom: "toD", + AmountCapForToDenom: sdk.OneInt(), + SwapMultiple: sdk.OneInt(), + }, + false, + nil, + }, + "invalid empty from-denom": { + types.SwapInit{ + FromDenom: "", + ToDenom: "toD", + AmountCapForToDenom: sdk.OneInt(), + SwapMultiple: sdk.OneInt(), + }, + true, + sdkerrors.ErrInvalidRequest, + }, + "invalid empty to-denom": { + types.SwapInit{ + FromDenom: "fromD", + ToDenom: "", + AmountCapForToDenom: sdk.OneInt(), + SwapMultiple: sdk.OneInt(), + }, + true, + sdkerrors.ErrInvalidRequest, + }, + "invalid zero amount cap for to-denom": { + types.SwapInit{ + FromDenom: "fromD", + ToDenom: "toD", + AmountCapForToDenom: sdk.ZeroInt(), + SwapMultiple: sdk.OneInt(), + }, + true, + sdkerrors.ErrInvalidRequest, + }, + "invalid zero swap-rate": { + types.SwapInit{ + FromDenom: "fromD", + ToDenom: "toD", + AmountCapForToDenom: sdk.OneInt(), + SwapMultiple: sdk.ZeroInt(), + }, + true, + sdkerrors.ErrInvalidRequest, + }, + "invalid the same from-denom and to-denom": { + types.SwapInit{ + FromDenom: "same", + ToDenom: "same", + AmountCapForToDenom: sdk.OneInt(), + SwapMultiple: sdk.OneInt(), + }, + true, + sdkerrors.ErrInvalidRequest, + }, + } + for name, tc := range testCases { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() + err := s.keeper.SwapInit(ctx, tc.req) + if tc.shouldThrowError { + s.Require().ErrorIs(err, tc.expectedError) + return + } + s.Require().NoError(err) + }) + } +} diff --git a/x/fswap/keeper/msg_server.go b/x/fswap/keeper/msg_server.go index 7735574919..f1892a2575 100644 --- a/x/fswap/keeper/msg_server.go +++ b/x/fswap/keeper/msg_server.go @@ -26,7 +26,7 @@ func (s MsgServer) Swap(ctx context.Context, req *types.MsgSwapRequest) (*types. return &types.MsgSwapResponse{}, err } - if req.GetAmount().Denom != swapInit.GetFromDenom() { + if req.GetFromCoinAmount().Denom != swapInit.GetFromDenom() { return nil, sdkerrors.ErrInvalidCoins } @@ -35,7 +35,7 @@ func (s MsgServer) Swap(ctx context.Context, req *types.MsgSwapRequest) (*types. return nil, err } - if err := s.keeper.Swap(c, from, req.GetAmount()); err != nil { + if err := s.keeper.Swap(c, from, req.GetFromCoinAmount()); err != nil { return nil, err } @@ -46,7 +46,7 @@ func (s MsgServer) SwapAll(ctx context.Context, req *types.MsgSwapAllRequest) (* c := sdk.UnwrapSDKContext(ctx) if !s.keeper.hasBeenInitialized(c) { - return &types.MsgSwapAllResponse{}, types.ErrSwapNotInitilized + return &types.MsgSwapAllResponse{}, types.ErrSwapNotInitialized } from, err := sdk.AccAddressFromBech32(req.FromAddress) diff --git a/x/fswap/testutil/expected_keepers_mocks.go b/x/fswap/testutil/expected_keepers_mocks.go index bae4f2df62..a97e70920c 100644 --- a/x/fswap/testutil/expected_keepers_mocks.go +++ b/x/fswap/testutil/expected_keepers_mocks.go @@ -127,16 +127,16 @@ func (mr *MockBankKeeperMockRecorder) MintCoins(ctx, moduleName, amt interface{} return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MintCoins", reflect.TypeOf((*MockBankKeeper)(nil).MintCoins), ctx, moduleName, amt) } -// SendCoins mocks base method. -func (m *MockBankKeeper) SendCoins(ctx types.Context, fromAddr, toAddr types.AccAddress, amt types.Coins) error { +// SendCoinsFromModuleToAccount mocks base method. +func (m *MockBankKeeper) SendCoinsFromModuleToAccount(ctx types.Context, senderModule string, recipientAddr types.AccAddress, amt types.Coins) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SendCoins", ctx, fromAddr, toAddr, amt) + ret := m.ctrl.Call(m, "SendCoinsFromModuleToAccount", ctx, senderModule, recipientAddr, amt) ret0, _ := ret[0].(error) return ret0 } -// SendCoins indicates an expected call of SendCoins. -func (mr *MockBankKeeperMockRecorder) SendCoins(ctx, fromAddr, toAddr, amt interface{}) *gomock.Call { +// SendCoinsFromModuleToAccount indicates an expected call of SendCoinsFromModuleToAccount. +func (mr *MockBankKeeperMockRecorder) SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, amt interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendCoins", reflect.TypeOf((*MockBankKeeper)(nil).SendCoins), ctx, fromAddr, toAddr, amt) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendCoinsFromModuleToAccount", reflect.TypeOf((*MockBankKeeper)(nil).SendCoinsFromModuleToAccount), ctx, senderModule, recipientAddr, amt) } diff --git a/x/fswap/types/errors.go b/x/fswap/types/errors.go index baf70fc391..ba1fe96aff 100644 --- a/x/fswap/types/errors.go +++ b/x/fswap/types/errors.go @@ -8,8 +8,8 @@ import ( // x/fswap module sentinel errors var ( - ErrSwapInitNotFound = sdkerrors.Register(ModuleName, 1100, "swap init does not exist") - ErrSwappedNotFound = sdkerrors.Register(ModuleName, 1101, "swapped does not exist") - ErrExceedSwappable = sdkerrors.Register(ModuleName, 1102, "exceed swappable coin amount") - ErrSwapNotInitilized = sdkerrors.Register(ModuleName, 1103, "swap not initilized") + ErrSwapNotInitialized = sdkerrors.Register(ModuleName, 1100, "swap not initialized") + ErrSwapCanNotBeInitializedTwice = sdkerrors.Register(ModuleName, 1101, "swap cannot be initialized twice") + ErrSwappedNotFound = sdkerrors.Register(ModuleName, 1102, "swapped does not exist") + ErrExceedSwappableToCoinAmount = sdkerrors.Register(ModuleName, 1103, "exceed swappable to-coin amount") ) diff --git a/x/fswap/types/fswap.go b/x/fswap/types/fswap.go index 8c226e0425..64f5133060 100644 --- a/x/fswap/types/fswap.go +++ b/x/fswap/types/fswap.go @@ -1,11 +1,10 @@ package types import ( - "fmt" - "gopkg.in/yaml.v2" sdk "github.com/Finschia/finschia-sdk/types" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" ) // NewSwapped creates a new Swapped instance @@ -27,19 +26,19 @@ func DefaultSwapped() Swapped { func validateCoinAmount(i interface{}) error { v, ok := i.(sdk.Coin) if !ok { - return fmt.Errorf("invalid coin amount: %T", i) + return sdkerrors.ErrInvalidCoins.Wrapf("invalid coin amount: %T", i) } if v.IsNil() { - return fmt.Errorf("coin amount must be not nil") + return sdkerrors.ErrInvalidCoins.Wrap("coin amount must be not nil") } if err := v.Validate(); err != nil { - return err + return sdkerrors.ErrInvalidCoins.Wrap(err.Error()) } return nil } -// Validate validates the set of swapped -func (s Swapped) Validate() error { +// Validate validates the set of Swapped +func (s *Swapped) Validate() error { if err := validateCoinAmount(s.FromCoinAmount); err != nil { return err } @@ -50,33 +49,49 @@ func (s Swapped) Validate() error { } // String implements the Stringer interface. -func (s Swapped) String() string { +func (s *Swapped) String() string { out, _ := yaml.Marshal(s) return string(out) } -// Validate validates the set of swapped -func (s SwapInit) ValidateBasic() error { +// ValidateBasic validates the set of SwapInit +func (s *SwapInit) ValidateBasic() error { if s.FromDenom == "" { - return fmt.Errorf("from denomination cannot be empty") + return sdkerrors.ErrInvalidRequest.Wrap("from denomination cannot be empty") } if s.ToDenom == "" { - return fmt.Errorf("to denomination cannot be empty") + return sdkerrors.ErrInvalidRequest.Wrap("to denomination cannot be empty") } if s.FromDenom == s.ToDenom { - return fmt.Errorf("from denomination cannot be equal to to denomination") + return sdkerrors.ErrInvalidRequest.Wrap("from denomination cannot be equal to to denomination") } - if s.AmountCapForToDenom.LT(sdk.ZeroInt()) { - return fmt.Errorf("amount cannot be less than zero") + if s.AmountCapForToDenom.LT(sdk.OneInt()) { + return sdkerrors.ErrInvalidRequest.Wrap("amount cannot be less than one") } - if s.SwapMultiple.LT(sdk.ZeroInt()) { - return fmt.Errorf("swap multiple cannot be less than zero") + if s.SwapMultiple.LT(sdk.OneInt()) { + return sdkerrors.ErrInvalidRequest.Wrap("swap multiple cannot be less than one") } return nil } // String implements the Stringer interface. -func (s SwapInit) String() string { +func (s *SwapInit) String() string { out, _ := yaml.Marshal(s) return string(out) } + +func (s *SwapInit) IsEmpty() bool { + if s.FromDenom == "" { + return true + } + if s.ToDenom == "" { + return true + } + if s.AmountCapForToDenom.IsZero() { + return true + } + if s.SwapMultiple.IsZero() { + return true + } + return false +} diff --git a/x/fswap/types/genesis.go b/x/fswap/types/genesis.go index 505efab380..624a54ce5d 100644 --- a/x/fswap/types/genesis.go +++ b/x/fswap/types/genesis.go @@ -1,7 +1,7 @@ package types import ( - "errors" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" ) // DefaultGenesis returns the default Capability genesis state @@ -16,17 +16,14 @@ func DefaultGenesis() *GenesisState { // failure. // need confirm: should we validate? Since it may nil func (gs GenesisState) Validate() error { - if len(gs.GetSwapInit()) == 0 { - return nil - } - if len(gs.GetSwapped()) == 0 { + if len(gs.GetSwapInit()) == 0 && len(gs.GetSwapped()) == 0 { return nil } if len(gs.GetSwapInit()) > 1 { - return errors.New("cannot have more than one swapInit") // TODO(bjs) to sentinel + return sdkerrors.Wrap(ErrSwapCanNotBeInitializedTwice, "cannot have more than one swapInit") } if len(gs.GetSwapped()) > 1 { - return errors.New("cannot have more than one swapped") // TODO(bjs) to sentinel + return sdkerrors.Wrap(ErrSwapCanNotBeInitializedTwice, "cannot have more than one swapped") } swapInit := gs.GetSwapInit()[0] if err := swapInit.ValidateBasic(); err != nil { @@ -38,7 +35,7 @@ func (gs GenesisState) Validate() error { } if swapInit.AmountCapForToDenom.LT(swapped.GetToCoinAmount().Amount) { - return ErrExceedSwappable + return ErrExceedSwappableToCoinAmount } return nil } diff --git a/x/fswap/types/msgs.go b/x/fswap/types/msgs.go index c3b51da05f..996bb7425a 100644 --- a/x/fswap/types/msgs.go +++ b/x/fswap/types/msgs.go @@ -7,13 +7,6 @@ import ( var _ sdk.Msg = &MsgSwapRequest{} -// NewMsgSwapRequest - construct a msg to swap amounts of old coin to new coin -// -//nolint:interfacer -func NewMsgSwapRequest(fromAddr, toAddr sdk.AccAddress, amount sdk.Coin) *MsgSwapRequest { - return &MsgSwapRequest{FromAddress: fromAddr.String(), Amount: amount} -} - // ValidateBasic Implements Msg. func (m *MsgSwapRequest) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(m.FromAddress) @@ -21,12 +14,12 @@ func (m *MsgSwapRequest) ValidateBasic() error { return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid address (%s)", err) } - if !m.Amount.IsValid() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.Amount.String()) + if !m.FromCoinAmount.IsValid() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.FromCoinAmount.String()) } - if !m.Amount.IsPositive() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.Amount.String()) + if !m.FromCoinAmount.IsPositive() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.FromCoinAmount.String()) } return nil diff --git a/x/fswap/types/tx.pb.go b/x/fswap/types/tx.pb.go index 2738e7aabb..0a0cb720e0 100644 --- a/x/fswap/types/tx.pb.go +++ b/x/fswap/types/tx.pb.go @@ -33,7 +33,7 @@ type MsgSwapRequest struct { // holder's address FromAddress string `protobuf:"bytes,1,opt,name=from_address,json=fromAddress,proto3" json:"from_address,omitempty"` // from-coin amount - Amount types.Coin `protobuf:"bytes,2,opt,name=amount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"amount"` + FromCoinAmount types.Coin `protobuf:"bytes,2,opt,name=from_coin_amount,json=fromCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"from_coin_amount"` } func (m *MsgSwapRequest) Reset() { *m = MsgSwapRequest{} } @@ -76,9 +76,9 @@ func (m *MsgSwapRequest) GetFromAddress() string { return "" } -func (m *MsgSwapRequest) GetAmount() types.Coin { +func (m *MsgSwapRequest) GetFromCoinAmount() types.Coin { if m != nil { - return m.Amount + return m.FromCoinAmount } return types.Coin{} } @@ -210,29 +210,30 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/tx.proto", fileDescriptor_65c77cf1d9b67323) } var fileDescriptor_65c77cf1d9b67323 = []byte{ - // 348 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x52, 0xcd, 0x4a, 0xf3, 0x40, - 0x14, 0xcd, 0x7c, 0x9f, 0x54, 0x9c, 0x16, 0xa5, 0x43, 0x85, 0x1a, 0x34, 0xad, 0x59, 0x15, 0xc4, - 0x19, 0xd2, 0x82, 0xfb, 0xb6, 0x20, 0x2e, 0xec, 0xa6, 0xee, 0xdc, 0x48, 0x92, 0x4e, 0xd2, 0x60, - 0x92, 0x89, 0xbd, 0xd3, 0x1f, 0xdf, 0xc2, 0xa5, 0xe0, 0x1b, 0xf8, 0x24, 0x5d, 0x76, 0xe9, 0x4a, - 0xa5, 0x7d, 0x11, 0x49, 0x26, 0x68, 0x05, 0x8b, 0xee, 0x2e, 0xe7, 0xdc, 0x7b, 0xce, 0xe1, 0xcc, - 0xe0, 0xfd, 0xd0, 0x89, 0x98, 0x07, 0x53, 0x3b, 0x61, 0x13, 0x8b, 0xc9, 0x19, 0x4d, 0x46, 0x42, - 0x0a, 0x52, 0x0a, 0x9d, 0x88, 0x66, 0x30, 0x9d, 0x58, 0x7a, 0xc5, 0x17, 0xbe, 0xc8, 0x08, 0x96, - 0x4e, 0x6a, 0x47, 0x37, 0x5c, 0x01, 0x91, 0x00, 0xe6, 0xd8, 0xc0, 0xd9, 0xc4, 0x72, 0xb8, 0xb4, - 0x2d, 0xe6, 0x8a, 0x20, 0x56, 0xbc, 0xf9, 0x84, 0xf0, 0x6e, 0x0f, 0xfc, 0xab, 0xa9, 0x9d, 0xf4, - 0xf9, 0xdd, 0x98, 0x83, 0x24, 0xc7, 0xb8, 0xe4, 0x8d, 0x44, 0x74, 0x63, 0x0f, 0x06, 0x23, 0x0e, - 0x50, 0x45, 0x75, 0xd4, 0xd8, 0xe9, 0x17, 0x53, 0xac, 0xad, 0x20, 0xe2, 0xe1, 0x82, 0x1d, 0x89, - 0x71, 0x2c, 0xab, 0xff, 0xea, 0xa8, 0x51, 0x6c, 0x1e, 0x50, 0x65, 0x43, 0x53, 0x1b, 0x9a, 0xdb, - 0xd0, 0xae, 0x08, 0xe2, 0x4e, 0x6b, 0xfe, 0x5a, 0xd3, 0x9e, 0xdf, 0x6a, 0x27, 0x7e, 0x20, 0x87, - 0x63, 0x87, 0xba, 0x22, 0x62, 0xe7, 0x41, 0x0c, 0xee, 0x30, 0xb0, 0x99, 0x97, 0x0f, 0xa7, 0x30, - 0xb8, 0x65, 0xf2, 0x3e, 0xe1, 0x90, 0x1d, 0xf5, 0x73, 0x75, 0xb3, 0x8c, 0xf7, 0x3e, 0xc3, 0x41, - 0x22, 0x62, 0xe0, 0xe6, 0x19, 0x2e, 0xe7, 0x50, 0x3b, 0x0c, 0xff, 0x1e, 0xd9, 0xac, 0x60, 0xb2, - 0x7e, 0xa7, 0xd4, 0x9a, 0x8f, 0x08, 0xff, 0xef, 0x81, 0x4f, 0xba, 0x78, 0x2b, 0xa5, 0xc8, 0x21, - 0x5d, 0xef, 0x94, 0x7e, 0x6f, 0x46, 0x3f, 0xda, 0xc0, 0x2a, 0x31, 0x72, 0x89, 0xb7, 0x73, 0x7d, - 0x52, 0xfb, 0x71, 0xf3, 0x2b, 0xb1, 0x5e, 0xdf, 0xbc, 0xa0, 0xd4, 0x3a, 0x17, 0xf3, 0xa5, 0x81, - 0x16, 0x4b, 0x03, 0xbd, 0x2f, 0x0d, 0xf4, 0xb0, 0x32, 0xb4, 0xc5, 0xca, 0xd0, 0x5e, 0x56, 0x86, - 0x76, 0x4d, 0x7f, 0xad, 0x72, 0x96, 0xff, 0x96, 0xac, 0x52, 0xa7, 0x90, 0x3d, 0x75, 0xeb, 0x23, - 0x00, 0x00, 0xff, 0xff, 0xf1, 0x37, 0x1c, 0xc2, 0x47, 0x02, 0x00, 0x00, + // 359 bytes of a gzipped FileDescriptorProto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0xe5, 0x7f, 0x55, 0x54, 0xcb, 0xd5, 0x0f, 0xa9, 0x0a, + 0xa4, 0x22, 0x90, 0x66, 0x81, 0xb4, 0xcd, 0x83, 0xb8, 0xd5, 0x98, 0xbf, 0x56, 0xb4, 0xa7, 0xb7, + 0xca, 0xa9, 0x1f, 0xa4, 0xc3, 0xb1, 0x43, 0x5d, 0x1e, 0xb1, 0x8b, 0x20, 0x06, 0x77, 0x18, 0xd8, + 0x6c, 0x90, 0x0d, 0x67, 0xd0, 0xbf, 0x65, 0xe9, 0x7d, 0xe2, 0x81, 0x5c, 0xea, 0x16, 0x44, 0x8e, + 0x98, 0x9a, 0x32, 0xc5, 0x2c, 0xe2, 0xbd, 0xcf, 0x73, 0x21, 0xe1, 0x31, 0x78, 0xe6, 0x39, 0x2e, + 0x66, 0x50, 0x33, 0x0c, 0xff, 0xfe, 0x11, 0x66, 0x09, 0x93, 0xf5, 0x3d, 0xe5, 0x56, 0x7f, 0x44, + 0xf8, 0x7f, 0x07, 0x7c, 0xd2, 0xc6, 0x5b, 0x82, 0x22, 0x47, 0x74, 0xbd, 0x65, 0xfa, 0xbd, 0x2b, + 0xfd, 0x78, 0x03, 0xab, 0xcc, 0xc8, 0x15, 0xde, 0xc9, 0xfc, 0x49, 0xe5, 0x47, 0xe5, 0xd7, 0xc5, + 0x7a, 0x75, 0xb3, 0x40, 0xb9, 0xb5, 0x2e, 0xe7, 0x4b, 0x03, 0x2d, 0x96, 0x06, 0x7a, 0x5f, 0x1a, + 0xe8, 0x61, 0x65, 0x68, 0x8b, 0x95, 0xa1, 0xbd, 0xac, 0x0c, 0xed, 0x86, 0xfe, 0x5a, 0xe9, 0x2c, + 0x7b, 0x3f, 0xb2, 0x5a, 0x67, 0x5b, 0xfe, 0xfc, 0xc6, 0x47, 0x00, 0x00, 0x00, 0xff, 0xff, 0x65, + 0xac, 0x90, 0x75, 0x59, 0x02, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -372,7 +373,7 @@ func (m *MsgSwapRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size, err := m.Amount.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.FromCoinAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -488,7 +489,7 @@ func (m *MsgSwapRequest) Size() (n int) { if l > 0 { n += 1 + l + sovTx(uint64(l)) } - l = m.Amount.Size() + l = m.FromCoinAmount.Size() n += 1 + l + sovTx(uint64(l)) return n } @@ -593,7 +594,7 @@ func (m *MsgSwapRequest) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Amount", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field FromCoinAmount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -620,7 +621,7 @@ func (m *MsgSwapRequest) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.Amount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.FromCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex From be5f72373f2d9b445681926162a67b2974fa0ba9 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Tue, 30 Apr 2024 21:37:29 +0900 Subject: [PATCH 16/47] chore: update proto --- docs/core/proto-docs.md | 33 +-- proto/lbm/fswap/v1/query.proto | 2 +- proto/lbm/fswap/v1/tx.proto | 11 +- x/fswap/client/cli/tx.go | 4 +- x/fswap/keeper/keeper.go | 22 +- x/fswap/keeper/keeper_test.go | 16 +- x/fswap/keeper/msg_server.go | 22 +- x/fswap/keeper/msg_server_test.go | 118 +++++++++++ x/fswap/types/msgs.go | 31 +-- x/fswap/types/query.pb.go | 58 +++--- x/fswap/types/query.pb.gw.go | 2 +- x/fswap/types/tx.pb.go | 321 ++++++++++++++++++++++-------- 12 files changed, 461 insertions(+), 179 deletions(-) diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index 833f112287..16831e12a1 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -876,9 +876,9 @@ - [Query](#lbm.fswap.v1.Query) - [lbm/fswap/v1/tx.proto](#lbm/fswap/v1/tx.proto) - - [MsgSwapAllRequest](#lbm.fswap.v1.MsgSwapAllRequest) + - [MsgSwap](#lbm.fswap.v1.MsgSwap) + - [MsgSwapAll](#lbm.fswap.v1.MsgSwapAll) - [MsgSwapAllResponse](#lbm.fswap.v1.MsgSwapAllResponse) - - [MsgSwapRequest](#lbm.fswap.v1.MsgSwapRequest) - [MsgSwapResponse](#lbm.fswap.v1.MsgSwapResponse) - [Msg](#lbm.fswap.v1.Msg) @@ -12914,7 +12914,7 @@ GenesisState defines the fswap module's genesis state. | Method Name | Request Type | Response Type | Description | HTTP Verb | Endpoint | | ----------- | ------------ | ------------- | ------------| ------- | -------- | | `Swapped` | [QuerySwappedRequest](#lbm.fswap.v1.QuerySwappedRequest) | [QuerySwappedResponse](#lbm.fswap.v1.QuerySwappedResponse) | Swapped queries the current swapped status that includes a burnt amount of from-coin and a minted amount of to-coin. | GET|/lbm/fswap/v1/swapped| -| `TotalSwappableToCoinAmount` | [QueryTotalSwappableToCoinAmountRequest](#lbm.fswap.v1.QueryTotalSwappableToCoinAmountRequest) | [QueryTotalSwappableToCoinAmountResponse](#lbm.fswap.v1.QueryTotalSwappableToCoinAmountResponse) | TotalSwappableToCoinAmount queries the current swappable amount for to-coin. | GET|/lbm/fswap/v1/swappable_new_coin_amount| +| `TotalSwappableToCoinAmount` | [QueryTotalSwappableToCoinAmountRequest](#lbm.fswap.v1.QueryTotalSwappableToCoinAmountRequest) | [QueryTotalSwappableToCoinAmountResponse](#lbm.fswap.v1.QueryTotalSwappableToCoinAmountResponse) | TotalSwappableToCoinAmount queries the current swappable amount for to-coin. | GET|/lbm/fswap/v1/total_swappable_to_coin_amount| @@ -12927,41 +12927,44 @@ GenesisState defines the fswap module's genesis state. - + -### MsgSwapAllRequest +### MsgSwap | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | | `from_address` | [string](#string) | | holder's address | +| `from_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | from-coin amount | +| `to_denom` | [string](#string) | | | - + -### MsgSwapAllResponse +### MsgSwapAll +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `from_address` | [string](#string) | | holder's address | +| `from_denom` | [string](#string) | | | +| `to_denom` | [string](#string) | | | - -### MsgSwapRequest + +### MsgSwapAllResponse -| Field | Type | Label | Description | -| ----- | ---- | ----- | ----------- | -| `from_address` | [string](#string) | | holder's address | -| `from_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | from-coin amount | @@ -12991,8 +12994,8 @@ GenesisState defines the fswap module's genesis state. | Method Name | Request Type | Response Type | Description | HTTP Verb | Endpoint | | ----------- | ------------ | ------------- | ------------| ------- | -------- | -| `Swap` | [MsgSwapRequest](#lbm.fswap.v1.MsgSwapRequest) | [MsgSwapResponse](#lbm.fswap.v1.MsgSwapResponse) | | | -| `SwapAll` | [MsgSwapAllRequest](#lbm.fswap.v1.MsgSwapAllRequest) | [MsgSwapAllResponse](#lbm.fswap.v1.MsgSwapAllResponse) | | | +| `Swap` | [MsgSwap](#lbm.fswap.v1.MsgSwap) | [MsgSwapResponse](#lbm.fswap.v1.MsgSwapResponse) | | | +| `SwapAll` | [MsgSwapAll](#lbm.fswap.v1.MsgSwapAll) | [MsgSwapAllResponse](#lbm.fswap.v1.MsgSwapAllResponse) | | | diff --git a/proto/lbm/fswap/v1/query.proto b/proto/lbm/fswap/v1/query.proto index 09ca077261..3502013229 100644 --- a/proto/lbm/fswap/v1/query.proto +++ b/proto/lbm/fswap/v1/query.proto @@ -17,7 +17,7 @@ service Query { // TotalSwappableToCoinAmount queries the current swappable amount for to-coin. rpc TotalSwappableToCoinAmount(QueryTotalSwappableToCoinAmountRequest) returns (QueryTotalSwappableToCoinAmountResponse) { - option (google.api.http).get = "/lbm/fswap/v1/swappable_new_coin_amount"; + option (google.api.http).get = "/lbm/fswap/v1/total_swappable_to_coin_amount"; } } diff --git a/proto/lbm/fswap/v1/tx.proto b/proto/lbm/fswap/v1/tx.proto index 004819fa46..07813f9eab 100644 --- a/proto/lbm/fswap/v1/tx.proto +++ b/proto/lbm/fswap/v1/tx.proto @@ -7,23 +7,26 @@ import "gogoproto/gogo.proto"; import "cosmos/base/v1beta1/coin.proto"; service Msg { - rpc Swap(MsgSwapRequest) returns (MsgSwapResponse); - rpc SwapAll(MsgSwapAllRequest) returns (MsgSwapAllResponse); + rpc Swap(MsgSwap) returns (MsgSwapResponse); + rpc SwapAll(MsgSwapAll) returns (MsgSwapAllResponse); } -message MsgSwapRequest { +message MsgSwap { // holder's address string from_address = 1; // from-coin amount cosmos.base.v1beta1.Coin from_coin_amount = 2 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; + string to_denom = 3; } message MsgSwapResponse {} -message MsgSwapAllRequest { +message MsgSwapAll { // holder's address string from_address = 1; + string from_denom = 2; + string to_denom = 3; } message MsgSwapAllResponse {} diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index 047071181c..0ab9c915ec 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -60,7 +60,7 @@ func CmdTxMsgSwap() *cobra.Command { return err } - msg := &types.MsgSwapRequest{ + msg := &types.MsgSwap{ FromAddress: clientCtx.GetFromAddress().String(), FromCoinAmount: coin, } @@ -89,7 +89,7 @@ func CmdTxMsgSwapAll() *cobra.Command { return err } - msg := &types.MsgSwapAllRequest{ + msg := &types.MsgSwapAll{ FromAddress: clientCtx.GetFromAddress().String(), } if err := msg.ValidateBasic(); err != nil { diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 2634a68b1b..450a43ce21 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -71,18 +71,22 @@ func (k Keeper) SwapInit(ctx sdk.Context, swapInit types.SwapInit) error { return nil } -func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Coin) error { +func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Coin, toDenom string) error { swapInit, err := k.getSwapInit(ctx) if err != nil { return err } - if fromCoinAmount.GetDenom() != swapInit.GetFromDenom() { + if swapInit.GetFromDenom() != fromCoinAmount.GetDenom() { return sdkerrors.ErrInvalidRequest.Wrapf("denom mismatch, expected %s, got %s", swapInit.GetFromDenom(), fromCoinAmount.Denom) } + if swapInit.GetToDenom() != toDenom { + return sdkerrors.ErrInvalidRequest.Wrapf("denom mismatch, expected %s, got %s", swapInit.GetToDenom(), toDenom) + } + newAmount := fromCoinAmount.Amount.Mul(swapInit.SwapMultiple) - newCoinAmount := sdk.NewCoin(swapInit.ToDenom, newAmount) + newCoinAmount := sdk.NewCoin(toDenom, newAmount) if err := k.checkSwapCap(ctx, newCoinAmount); err != nil { return err } @@ -117,17 +121,13 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Co return nil } -func (k Keeper) SwapAll(ctx sdk.Context, addr sdk.AccAddress) error { - oldDenom, err := k.getFromDenom(ctx) - if err != nil { - return err - } - balance := k.GetBalance(ctx, addr, oldDenom) +func (k Keeper) SwapAll(ctx sdk.Context, addr sdk.AccAddress, fromDenom, toDenom string) error { + balance := k.GetBalance(ctx, addr, fromDenom) if balance.IsZero() { return sdkerrors.ErrInsufficientFunds } - if err := k.Swap(ctx, addr, balance); err != nil { + if err := k.Swap(ctx, addr, balance, toDenom); err != nil { return err } return nil @@ -144,7 +144,7 @@ func (k Keeper) setSwapInit(ctx sdk.Context, swapInit types.SwapInit) error { } func (k Keeper) getAllSwapped(ctx sdk.Context) []types.Swapped { - swappedSlice := make([]types.Swapped, 0) + swappedSlice := make([]types.Swapped, 0) // TODO(bjs) k.iterateAllSwapped(ctx, func(swapped types.Swapped) bool { swappedSlice = append(swappedSlice, swapped) return false diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index f0cb62a5fe..a49b42317c 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -108,6 +108,7 @@ func (s *KeeperTestSuite) TestSwap() { testCases := map[string]struct { from sdk.AccAddress amountToSwap sdk.Coin + toDenom string expectedBalanceWithoutMultiply sdk.Int shouldThrowError bool expectedError error @@ -115,6 +116,7 @@ func (s *KeeperTestSuite) TestSwap() { "swap some": { s.accWithFromCoin, sdk.NewCoin(s.fromDenom, sdk.NewInt(100)), + s.toDenom, sdk.NewInt(100), false, nil, @@ -122,6 +124,7 @@ func (s *KeeperTestSuite) TestSwap() { "swap all the balance": { s.accWithFromCoin, sdk.NewCoin(s.fromDenom, s.initBalance), + s.toDenom, s.initBalance, false, nil, @@ -129,6 +132,7 @@ func (s *KeeperTestSuite) TestSwap() { "swap without holding enough balance": { s.accWithFromCoin, sdk.NewCoin(s.fromDenom, sdk.OneInt().Add(s.initBalance)), + s.toDenom, sdk.ZeroInt(), true, sdkerrors.ErrInsufficientFunds, @@ -136,6 +140,7 @@ func (s *KeeperTestSuite) TestSwap() { "account holding new coin only": { s.accWithToCoin, sdk.NewCoin(s.fromDenom, sdk.NewInt(100)), + s.toDenom, sdk.ZeroInt(), true, sdkerrors.ErrInsufficientFunds, @@ -143,6 +148,7 @@ func (s *KeeperTestSuite) TestSwap() { "swap with the same from-denom and to-denom": { s.accWithFromCoin, sdk.NewCoin(s.toDenom, s.initBalance), + s.toDenom, sdk.ZeroInt(), true, sdkerrors.ErrInvalidRequest, @@ -154,7 +160,7 @@ func (s *KeeperTestSuite) TestSwap() { err := s.keeper.SwapInit(ctx, s.swapInit) s.Require().NoError(err) - err = s.keeper.Swap(ctx, tc.from, tc.amountToSwap) + err = s.keeper.Swap(ctx, tc.from, tc.amountToSwap, tc.toDenom) if tc.shouldThrowError { s.Require().ErrorIs(err, tc.expectedError) return @@ -171,18 +177,24 @@ func (s *KeeperTestSuite) TestSwap() { func (s *KeeperTestSuite) TestSwapAll() { testCases := map[string]struct { from sdk.AccAddress + fromDenom string + toDenom string expectedBalanceWithoutMultiply sdk.Int shouldThrowError bool expectedError error }{ "account holding from coin": { s.accWithFromCoin, + s.fromDenom, + s.toDenom, s.initBalance, false, nil, }, "account holding to coin only": { s.accWithToCoin, + s.fromDenom, + s.toDenom, s.initBalance, true, sdkerrors.ErrInsufficientFunds, @@ -194,7 +206,7 @@ func (s *KeeperTestSuite) TestSwapAll() { err := s.keeper.SwapInit(ctx, s.swapInit) s.Require().NoError(err) - err = s.keeper.SwapAll(ctx, tc.from) + err = s.keeper.SwapAll(ctx, tc.from, tc.fromDenom, tc.toDenom) if tc.shouldThrowError { s.Require().ErrorIs(err, tc.expectedError) return diff --git a/x/fswap/keeper/msg_server.go b/x/fswap/keeper/msg_server.go index f1892a2575..b9484492e3 100644 --- a/x/fswap/keeper/msg_server.go +++ b/x/fswap/keeper/msg_server.go @@ -4,7 +4,6 @@ import ( "context" sdk "github.com/Finschia/finschia-sdk/types" - sdkerrors "github.com/Finschia/finschia-sdk/types/errors" "github.com/Finschia/finschia-sdk/x/fswap/types" ) @@ -18,43 +17,30 @@ func NewMsgServer(keeper Keeper) *MsgServer { return &MsgServer{keeper} } -func (s MsgServer) Swap(ctx context.Context, req *types.MsgSwapRequest) (*types.MsgSwapResponse, error) { +func (s MsgServer) Swap(ctx context.Context, req *types.MsgSwap) (*types.MsgSwapResponse, error) { c := sdk.UnwrapSDKContext(ctx) - swapInit, err := s.keeper.getSwapInit(c) - if err != nil { - return &types.MsgSwapResponse{}, err - } - - if req.GetFromCoinAmount().Denom != swapInit.GetFromDenom() { - return nil, sdkerrors.ErrInvalidCoins - } - from, err := sdk.AccAddressFromBech32(req.FromAddress) if err != nil { return nil, err } - if err := s.keeper.Swap(c, from, req.GetFromCoinAmount()); err != nil { + if err := s.keeper.Swap(c, from, req.GetFromCoinAmount(), req.GetToDenom()); err != nil { return nil, err } return &types.MsgSwapResponse{}, nil } -func (s MsgServer) SwapAll(ctx context.Context, req *types.MsgSwapAllRequest) (*types.MsgSwapAllResponse, error) { +func (s MsgServer) SwapAll(ctx context.Context, req *types.MsgSwapAll) (*types.MsgSwapAllResponse, error) { c := sdk.UnwrapSDKContext(ctx) - if !s.keeper.hasBeenInitialized(c) { - return &types.MsgSwapAllResponse{}, types.ErrSwapNotInitialized - } - from, err := sdk.AccAddressFromBech32(req.FromAddress) if err != nil { return nil, err } - if err := s.keeper.SwapAll(c, from); err != nil { + if err := s.keeper.SwapAll(c, from, req.GetFromDenom(), req.GetToDenom()); err != nil { return nil, err } diff --git a/x/fswap/keeper/msg_server_test.go b/x/fswap/keeper/msg_server_test.go index 9429264902..8dde4f9b4b 100644 --- a/x/fswap/keeper/msg_server_test.go +++ b/x/fswap/keeper/msg_server_test.go @@ -1 +1,119 @@ package keeper_test + +import ( + sdk "github.com/Finschia/finschia-sdk/types" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + "github.com/Finschia/finschia-sdk/x/fswap/types" +) + +func (s *KeeperTestSuite) TestMsgSwap() { + testCases := map[string]struct { + request *types.MsgSwap + expectedBalanceWithoutMultiply sdk.Int + shouldThrowError bool + expectedError error + }{ + "swap some": { + &types.MsgSwap{ + FromAddress: s.accWithFromCoin.String(), + FromCoinAmount: sdk.NewCoin(s.fromDenom, sdk.NewInt(100)), + ToDenom: s.toDenom, + }, + sdk.NewInt(100), + false, + nil, + }, + "swap all the balance": { + &types.MsgSwap{ + FromAddress: s.accWithFromCoin.String(), + FromCoinAmount: sdk.NewCoin(s.fromDenom, s.initBalance), + ToDenom: s.toDenom, + }, + s.initBalance, + false, + nil, + }, + "account holding new coin only": { + &types.MsgSwap{ + FromAddress: s.accWithToCoin.String(), + FromCoinAmount: sdk.NewCoin(s.fromDenom, sdk.NewInt(100)), + ToDenom: s.toDenom, + }, + s.initBalance, + true, + sdkerrors.ErrInsufficientFunds, + }, + } + for name, tc := range testCases { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() + err := s.keeper.SwapInit(ctx, s.swapInit) + s.Require().NoError(err) + + swapResponse, err := s.msgServer.Swap(sdk.WrapSDKContext(ctx), tc.request) + if tc.shouldThrowError { + s.Require().ErrorIs(err, tc.expectedError) + return + } + s.Require().NotNil(swapResponse) + s.Require().NoError(err) + + from, err := sdk.AccAddressFromBech32(tc.request.FromAddress) + s.Require().NoError(err) + actualAmount := s.keeper.GetBalance(ctx, from, tc.request.GetToDenom()).Amount + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapMultiple) + s.Require().Equal(expectedAmount, actualAmount) + }) + } +} + +func (s *KeeperTestSuite) TestMsgSwapAll() { + testCases := map[string]struct { + request *types.MsgSwapAll + expectedBalanceWithoutMultiply sdk.Int + shouldThrowError bool + expectedError error + }{ + "swapAll": { + &types.MsgSwapAll{ + FromAddress: s.accWithFromCoin.String(), + FromDenom: s.fromDenom, + ToDenom: s.toDenom, + }, + s.initBalance, + false, + nil, + }, + "account holding new coin only": { + &types.MsgSwapAll{ + FromAddress: s.accWithToCoin.String(), + FromDenom: s.fromDenom, + ToDenom: s.toDenom, + }, + s.initBalance, + true, + sdkerrors.ErrInsufficientFunds, + }, + } + for name, tc := range testCases { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() + err := s.keeper.SwapInit(ctx, s.swapInit) + s.Require().NoError(err) + + swapResponse, err := s.msgServer.SwapAll(sdk.WrapSDKContext(ctx), tc.request) + if tc.shouldThrowError { + s.Require().ErrorIs(err, tc.expectedError) + return + } + s.Require().NotNil(swapResponse) + s.Require().NoError(err) + + from, err := sdk.AccAddressFromBech32(tc.request.FromAddress) + s.Require().NoError(err) + actualAmount := s.keeper.GetBalance(ctx, from, tc.request.GetToDenom()).Amount + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapMultiple) + s.Require().Equal(expectedAmount, actualAmount) + }) + } +} diff --git a/x/fswap/types/msgs.go b/x/fswap/types/msgs.go index 996bb7425a..2d74d9a6e7 100644 --- a/x/fswap/types/msgs.go +++ b/x/fswap/types/msgs.go @@ -5,10 +5,10 @@ import ( sdkerrors "github.com/Finschia/finschia-sdk/types/errors" ) -var _ sdk.Msg = &MsgSwapRequest{} +var _ sdk.Msg = &MsgSwap{} // ValidateBasic Implements Msg. -func (m *MsgSwapRequest) ValidateBasic() error { +func (m *MsgSwap) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid address (%s)", err) @@ -22,11 +22,15 @@ func (m *MsgSwapRequest) ValidateBasic() error { return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.FromCoinAmount.String()) } + if len(m.GetToDenom()) == 0 { + return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.FromCoinAmount.String()) + } + return nil } // GetSigners Implements Msg. -func (m *MsgSwapRequest) GetSigners() []sdk.AccAddress { +func (m *MsgSwap) GetSigners() []sdk.AccAddress { from, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { panic(err) @@ -34,27 +38,28 @@ func (m *MsgSwapRequest) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{from} } -var _ sdk.Msg = &MsgSwapAllRequest{} - -// NewMsgSwapRequest - construct a msg to swap all old coin to new coin -// -//nolint:interfacer -func NewMsgSwapAllRequest(fromAddr, toAddr sdk.AccAddress) *MsgSwapAllRequest { - return &MsgSwapAllRequest{FromAddress: fromAddr.String()} -} +var _ sdk.Msg = &MsgSwapAll{} // ValidateBasic Implements Msg. -func (m *MsgSwapAllRequest) ValidateBasic() error { +func (m *MsgSwapAll) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid address (%s)", err) } + if len(m.GetFromDenom()) == 0 { + return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "Invalid Denom") + } + + if len(m.GetToDenom()) == 0 { + return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "Invalid Denom") + } + return nil } // GetSigners Implements Msg. -func (m *MsgSwapAllRequest) GetSigners() []sdk.AccAddress { +func (m *MsgSwapAll) GetSigners() []sdk.AccAddress { from, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { panic(err) diff --git a/x/fswap/types/query.pb.go b/x/fswap/types/query.pb.go index 3478a1db06..4a13541bf4 100644 --- a/x/fswap/types/query.pb.go +++ b/x/fswap/types/query.pb.go @@ -212,35 +212,35 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/query.proto", fileDescriptor_01deae9da7816d6a) } var fileDescriptor_01deae9da7816d6a = []byte{ - // 444 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0x3f, 0x8f, 0xd3, 0x30, - 0x18, 0xc6, 0x93, 0x48, 0x80, 0x64, 0x4e, 0xc7, 0xc9, 0xdc, 0x89, 0x23, 0x82, 0x1c, 0x64, 0xe0, - 0x0e, 0x21, 0x6c, 0xa5, 0x85, 0x0f, 0x40, 0x91, 0x10, 0x2b, 0xa5, 0x13, 0x4b, 0xe5, 0xa4, 0x6e, - 0x1a, 0x91, 0xf8, 0x4d, 0x6b, 0xa7, 0x7f, 0x56, 0x06, 0x66, 0x24, 0xbe, 0x02, 0x13, 0x3b, 0x0b, - 0x9f, 0xa0, 0x63, 0x25, 0x16, 0x26, 0x40, 0x2d, 0x1f, 0x04, 0xc5, 0x71, 0xa1, 0x91, 0x0a, 0x15, - 0x43, 0xb7, 0x57, 0x7e, 0x1f, 0xe7, 0xf7, 0xe4, 0x79, 0x5f, 0xa3, 0xd3, 0x34, 0xcc, 0x68, 0x5f, - 0x4e, 0x58, 0x4e, 0xc7, 0x01, 0x1d, 0x16, 0x7c, 0x34, 0x23, 0xf9, 0x08, 0x14, 0xe0, 0x83, 0x34, - 0xcc, 0x88, 0xee, 0x90, 0x71, 0xe0, 0xde, 0x8a, 0x01, 0xe2, 0x94, 0x53, 0x96, 0x27, 0x94, 0x09, - 0x01, 0x8a, 0xa9, 0x04, 0x84, 0xac, 0xb4, 0xee, 0x71, 0x0c, 0x31, 0xe8, 0x92, 0x96, 0x95, 0x39, - 0xf5, 0x22, 0x90, 0x19, 0x48, 0x1a, 0x32, 0xc9, 0xe9, 0x38, 0x08, 0xb9, 0x62, 0x01, 0x8d, 0x20, - 0x11, 0xa6, 0x5f, 0x67, 0x57, 0x28, 0xdd, 0xf1, 0x4f, 0xd0, 0xf5, 0x17, 0xa5, 0x95, 0x97, 0x13, - 0x96, 0xe7, 0xbc, 0xd7, 0xe6, 0xc3, 0x82, 0x4b, 0xe5, 0xbf, 0x75, 0xd0, 0x71, 0xfd, 0x5c, 0xe6, - 0x20, 0x24, 0xc7, 0x53, 0x74, 0xd4, 0x1f, 0x41, 0xd6, 0x2d, 0x3f, 0xde, 0x65, 0x19, 0x14, 0x42, - 0x9d, 0xda, 0x77, 0xec, 0x8b, 0xab, 0x8d, 0x9b, 0xa4, 0x32, 0x41, 0x4a, 0x13, 0xc4, 0x98, 0x20, - 0x4f, 0x21, 0x11, 0xad, 0xe6, 0xfc, 0xdb, 0x99, 0xf5, 0xf1, 0xfb, 0xd9, 0x83, 0x38, 0x51, 0x83, - 0x22, 0x24, 0x11, 0x64, 0xf4, 0x59, 0x22, 0x64, 0x34, 0x48, 0x18, 0xed, 0x9b, 0xe2, 0xa1, 0xec, - 0xbd, 0xa6, 0x6a, 0x96, 0x73, 0xa9, 0x2f, 0xb5, 0x0f, 0x4b, 0x4e, 0x59, 0x3d, 0xd1, 0x14, 0xac, - 0xd0, 0xa1, 0x82, 0x1a, 0xd7, 0xd9, 0x0b, 0xf7, 0x40, 0xc1, 0x1f, 0xaa, 0x7f, 0x81, 0xee, 0xe9, - 0x1c, 0x3a, 0xa0, 0x58, 0xaa, 0xc3, 0x60, 0x61, 0xca, 0x3b, 0x1b, 0x92, 0x75, 0x64, 0x1f, 0x6c, - 0x74, 0xbe, 0x53, 0x6a, 0x52, 0x9c, 0xa1, 0x23, 0xb9, 0x16, 0xec, 0x37, 0xc5, 0x6b, 0xbf, 0x39, - 0x95, 0x85, 0xc6, 0x27, 0x07, 0x5d, 0xd2, 0x36, 0x31, 0xa0, 0x2b, 0x66, 0xba, 0xf8, 0x2e, 0xd9, - 0x5c, 0x41, 0xb2, 0x65, 0x23, 0x5c, 0xff, 0x5f, 0x92, 0xea, 0xb7, 0xfc, 0xdb, 0x6f, 0xbe, 0xfc, - 0x7c, 0xef, 0xdc, 0xc0, 0x27, 0xb4, 0xb6, 0x6f, 0xd2, 0x50, 0x3e, 0xdb, 0xc8, 0xfd, 0x7b, 0x38, - 0xf8, 0xd1, 0x16, 0xc2, 0xce, 0xd8, 0xdd, 0xc7, 0xff, 0x79, 0xcb, 0x58, 0xa5, 0xda, 0xea, 0x7d, - 0x7c, 0xbe, 0xc5, 0xaa, 0x9e, 0x8a, 0xe0, 0x93, 0xcd, 0x65, 0x6b, 0x3d, 0x9f, 0x2f, 0x3d, 0x7b, - 0xb1, 0xf4, 0xec, 0x1f, 0x4b, 0xcf, 0x7e, 0xb7, 0xf2, 0xac, 0xc5, 0xca, 0xb3, 0xbe, 0xae, 0x3c, - 0xeb, 0x15, 0xd9, 0x39, 0x8f, 0xa9, 0x01, 0xe8, 0xb9, 0x84, 0x97, 0xf5, 0xcb, 0x6b, 0xfe, 0x0a, - 0x00, 0x00, 0xff, 0xff, 0x8c, 0x3e, 0xc5, 0x8e, 0x11, 0x04, 0x00, 0x00, + // 446 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0xbf, 0x8e, 0xd3, 0x40, + 0x10, 0xc6, 0x6d, 0x4b, 0x80,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, 0xc7, 0x75, 0xab, 0xaa, 0x9c, 0x6c, 0xaf, 0x76, 0x53, 0xbf, 0xb8, 0xc6, 0xcb, 0xe9, 0xdc, + 0xb3, 0x67, 0x73, 0xcf, 0xfe, 0x35, 0xf7, 0xec, 0x0f, 0x0b, 0xcf, 0x9a, 0x2d, 0x3c, 0xeb, 0xfb, + 0xc2, 0xb3, 0xde, 0x90, 0xad, 0x4b, 0x19, 0x1b, 0x8a, 0x5e, 0x4e, 0x78, 0x55, 0x7f, 0x7e, 0xe7, + 0x7f, 0x02, 0x00, 0x00, 0xff, 0xff, 0xc0, 0xa3, 0xd5, 0x06, 0x16, 0x04, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/x/fswap/types/query.pb.gw.go b/x/fswap/types/query.pb.gw.go index b657b521fd..ea06d0652c 100644 --- a/x/fswap/types/query.pb.gw.go +++ b/x/fswap/types/query.pb.gw.go @@ -200,7 +200,7 @@ func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, clie var ( pattern_Query_Swapped_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"lbm", "fswap", "v1", "swapped"}, "", runtime.AssumeColonVerbOpt(false))) - pattern_Query_TotalSwappableToCoinAmount_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"lbm", "fswap", "v1", "swappable_new_coin_amount"}, "", runtime.AssumeColonVerbOpt(false))) + pattern_Query_TotalSwappableToCoinAmount_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"lbm", "fswap", "v1", "total_swappable_to_coin_amount"}, "", runtime.AssumeColonVerbOpt(false))) ) var ( diff --git a/x/fswap/types/tx.pb.go b/x/fswap/types/tx.pb.go index 0a0cb720e0..288ada4778 100644 --- a/x/fswap/types/tx.pb.go +++ b/x/fswap/types/tx.pb.go @@ -29,25 +29,26 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package -type MsgSwapRequest struct { +type MsgSwap struct { // holder's address FromAddress string `protobuf:"bytes,1,opt,name=from_address,json=fromAddress,proto3" json:"from_address,omitempty"` // from-coin amount FromCoinAmount types.Coin `protobuf:"bytes,2,opt,name=from_coin_amount,json=fromCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"from_coin_amount"` + ToDenom string `protobuf:"bytes,3,opt,name=to_denom,json=toDenom,proto3" json:"to_denom,omitempty"` } -func (m *MsgSwapRequest) Reset() { *m = MsgSwapRequest{} } -func (m *MsgSwapRequest) String() string { return proto.CompactTextString(m) } -func (*MsgSwapRequest) ProtoMessage() {} -func (*MsgSwapRequest) Descriptor() ([]byte, []int) { +func (m *MsgSwap) Reset() { *m = MsgSwap{} } +func (m *MsgSwap) String() string { return proto.CompactTextString(m) } +func (*MsgSwap) ProtoMessage() {} +func (*MsgSwap) Descriptor() ([]byte, []int) { return fileDescriptor_65c77cf1d9b67323, []int{0} } -func (m *MsgSwapRequest) XXX_Unmarshal(b []byte) error { +func (m *MsgSwap) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) } -func (m *MsgSwapRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { +func (m *MsgSwap) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { if deterministic { - return xxx_messageInfo_MsgSwapRequest.Marshal(b, m, deterministic) + return xxx_messageInfo_MsgSwap.Marshal(b, m, deterministic) } else { b = b[:cap(b)] n, err := m.MarshalToSizedBuffer(b) @@ -57,32 +58,39 @@ func (m *MsgSwapRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, erro return b[:n], nil } } -func (m *MsgSwapRequest) XXX_Merge(src proto.Message) { - xxx_messageInfo_MsgSwapRequest.Merge(m, src) +func (m *MsgSwap) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgSwap.Merge(m, src) } -func (m *MsgSwapRequest) XXX_Size() int { +func (m *MsgSwap) XXX_Size() int { return m.Size() } -func (m *MsgSwapRequest) XXX_DiscardUnknown() { - xxx_messageInfo_MsgSwapRequest.DiscardUnknown(m) +func (m *MsgSwap) XXX_DiscardUnknown() { + xxx_messageInfo_MsgSwap.DiscardUnknown(m) } -var xxx_messageInfo_MsgSwapRequest proto.InternalMessageInfo +var xxx_messageInfo_MsgSwap proto.InternalMessageInfo -func (m *MsgSwapRequest) GetFromAddress() string { +func (m *MsgSwap) GetFromAddress() string { if m != nil { return m.FromAddress } return "" } -func (m *MsgSwapRequest) GetFromCoinAmount() types.Coin { +func (m *MsgSwap) GetFromCoinAmount() types.Coin { if m != nil { return m.FromCoinAmount } return types.Coin{} } +func (m *MsgSwap) GetToDenom() string { + if m != nil { + return m.ToDenom + } + return "" +} + type MsgSwapResponse struct { } @@ -119,23 +127,25 @@ func (m *MsgSwapResponse) XXX_DiscardUnknown() { var xxx_messageInfo_MsgSwapResponse proto.InternalMessageInfo -type MsgSwapAllRequest struct { +type MsgSwapAll struct { // holder's address FromAddress string `protobuf:"bytes,1,opt,name=from_address,json=fromAddress,proto3" json:"from_address,omitempty"` + FromDenom string `protobuf:"bytes,2,opt,name=from_denom,json=fromDenom,proto3" json:"from_denom,omitempty"` + ToDenom string `protobuf:"bytes,3,opt,name=to_denom,json=toDenom,proto3" json:"to_denom,omitempty"` } -func (m *MsgSwapAllRequest) Reset() { *m = MsgSwapAllRequest{} } -func (m *MsgSwapAllRequest) String() string { return proto.CompactTextString(m) } -func (*MsgSwapAllRequest) ProtoMessage() {} -func (*MsgSwapAllRequest) Descriptor() ([]byte, []int) { +func (m *MsgSwapAll) Reset() { *m = MsgSwapAll{} } +func (m *MsgSwapAll) String() string { return proto.CompactTextString(m) } +func (*MsgSwapAll) ProtoMessage() {} +func (*MsgSwapAll) Descriptor() ([]byte, []int) { return fileDescriptor_65c77cf1d9b67323, []int{2} } -func (m *MsgSwapAllRequest) XXX_Unmarshal(b []byte) error { +func (m *MsgSwapAll) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) } -func (m *MsgSwapAllRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { +func (m *MsgSwapAll) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { if deterministic { - return xxx_messageInfo_MsgSwapAllRequest.Marshal(b, m, deterministic) + return xxx_messageInfo_MsgSwapAll.Marshal(b, m, deterministic) } else { b = b[:cap(b)] n, err := m.MarshalToSizedBuffer(b) @@ -145,25 +155,39 @@ func (m *MsgSwapAllRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, e return b[:n], nil } } -func (m *MsgSwapAllRequest) XXX_Merge(src proto.Message) { - xxx_messageInfo_MsgSwapAllRequest.Merge(m, src) +func (m *MsgSwapAll) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgSwapAll.Merge(m, src) } -func (m *MsgSwapAllRequest) XXX_Size() int { +func (m *MsgSwapAll) XXX_Size() int { return m.Size() } -func (m *MsgSwapAllRequest) XXX_DiscardUnknown() { - xxx_messageInfo_MsgSwapAllRequest.DiscardUnknown(m) +func (m *MsgSwapAll) XXX_DiscardUnknown() { + xxx_messageInfo_MsgSwapAll.DiscardUnknown(m) } -var xxx_messageInfo_MsgSwapAllRequest proto.InternalMessageInfo +var xxx_messageInfo_MsgSwapAll proto.InternalMessageInfo -func (m *MsgSwapAllRequest) GetFromAddress() string { +func (m *MsgSwapAll) GetFromAddress() string { if m != nil { return m.FromAddress } return "" } +func (m *MsgSwapAll) GetFromDenom() string { + if m != nil { + return m.FromDenom + } + return "" +} + +func (m *MsgSwapAll) GetToDenom() string { + if m != nil { + return m.ToDenom + } + return "" +} + type MsgSwapAllResponse struct { } @@ -201,39 +225,41 @@ func (m *MsgSwapAllResponse) XXX_DiscardUnknown() { var xxx_messageInfo_MsgSwapAllResponse proto.InternalMessageInfo func init() { - proto.RegisterType((*MsgSwapRequest)(nil), "lbm.fswap.v1.MsgSwapRequest") + proto.RegisterType((*MsgSwap)(nil), "lbm.fswap.v1.MsgSwap") proto.RegisterType((*MsgSwapResponse)(nil), "lbm.fswap.v1.MsgSwapResponse") - proto.RegisterType((*MsgSwapAllRequest)(nil), "lbm.fswap.v1.MsgSwapAllRequest") + proto.RegisterType((*MsgSwapAll)(nil), "lbm.fswap.v1.MsgSwapAll") proto.RegisterType((*MsgSwapAllResponse)(nil), "lbm.fswap.v1.MsgSwapAllResponse") } func init() { proto.RegisterFile("lbm/fswap/v1/tx.proto", fileDescriptor_65c77cf1d9b67323) } var fileDescriptor_65c77cf1d9b67323 = []byte{ - // 359 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x52, 0xcd, 0x4e, 0xf2, 0x40, - 0x14, 0xed, 0x7c, 0x9f, 0xd1, 0x38, 0x10, 0x94, 0x09, 0x26, 0xd8, 0x68, 0xc1, 0xae, 0x48, 0x8c, - 0x33, 0x29, 0x24, 0xee, 0x81, 0xc4, 0xb8, 0x90, 0x0d, 0xee, 0xdc, 0x90, 0xb6, 0x94, 0xd2, 0xd8, - 0x76, 0x2a, 0xb7, 0xfc, 0xf8, 0x16, 0x2e, 0x7d, 0x06, 0x13, 0xdf, 0x83, 0x25, 0x4b, 0x57, 0x6a, - 0xe0, 0x45, 0xcc, 0xcc, 0x34, 0x8a, 0x89, 0x44, 0x77, 0x37, 0xe7, 0x9c, 0x7b, 0xce, 0xed, 0xe9, - 0xe0, 0x83, 0xd0, 0x89, 0xd8, 0x00, 0xa6, 0x76, 0xc2, 0x26, 0x16, 0x4b, 0x67, 0x34, 0x19, 0xf1, - 0x94, 0x93, 0x7c, 0xe8, 0x44, 0x54, 0xc2, 0x74, 0x62, 0xe9, 0x25, 0x9f, 0xfb, 0x5c, 0x12, 0x4c, - 0x4c, 0x4a, 0xa3, 0x1b, 0x2e, 0x87, 0x88, 0x03, 0x73, 0x6c, 0xf0, 0xd8, 0xc4, 0x72, 0xbc, 0xd4, - 0xb6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0x2e, 0xe7, 0x4b, 0x03, 0x2d, 0x96, 0x06, 0x7a, 0x5f, 0x1a, - 0xe8, 0x61, 0x65, 0x68, 0x8b, 0x95, 0xa1, 0xbd, 0xac, 0x0c, 0xed, 0x86, 0xfe, 0x5a, 0xe9, 0x2c, - 0x7b, 0x3f, 0xb2, 0x5a, 0x67, 0x5b, 0xfe, 0xfc, 0xc6, 0x47, 0x00, 0x00, 0x00, 0xff, 0xff, 0x65, - 0xac, 0x90, 0x75, 0x59, 0x02, 0x00, 0x00, + // 387 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0xbd, 0x6e, 0xe2, 0x40, + 0x10, 0xc7, 0xbd, 0x70, 0x3a, 0x8e, 0x05, 0xdd, 0x87, 0x05, 0x92, 0xb1, 0x84, 0xe1, 0xa8, 0x90, + 0x4e, 0xb7, 0x2b, 0x43, 0x7b, 0x8d, 0xb9, 0xbb, 0x28, 0x0d, 0x0d, 0xe9, 0xd2, 0x20, 0x7f, 0x61, + 0xac, 0xd8, 0x1e, 0x8b, 0x5d, 0x3e, 0xf2, 0x0a, 0xa9, 0xf2, 0x1c, 0x79, 0x8b, 0x74, 0x94, 0x94, + 0xa9, 0x92, 0x08, 0x5e, 0x24, 0xda, 0xb5, 0xad, 0xa4, 0x20, 0x1f, 0xdd, 0xf8, 0x3f, 0xff, 0x9d, + 0xdf, 0x8c, 0x67, 0x70, 0x33, 0x72, 0x62, 0x3a, 0x63, 0x6b, 0x3b, 0xa5, 0x2b, 0x93, 0xf2, 0x0d, + 0x49, 0x17, 0xc0, 0x41, 0xad, 0x47, 0x4e, 0x4c, 0xa4, 0x4c, 0x56, 0xa6, 0xde, 0x08, 0x20, 0x00, + 0x99, 0xa0, 0x22, 0xca, 0x3c, 0xba, 0xe1, 0x02, 0x8b, 0x81, 0x51, 0xc7, 0x66, 0x3e, 0x5d, 0x99, + 0x8e, 0xcf, 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f2, 0x72, 0x59, 0x24, 0x7f, 0xa1, 0xb7, 0x8f, 0xca, + 0x45, 0x15, 0xf5, 0x3f, 0xae, 0x14, 0x33, 0x68, 0x47, 0x9d, 0x56, 0x14, 0xe9, 0xdd, 0xd7, 0x32, + 0x45, 0x99, 0xd1, 0xe9, 0x76, 0x6f, 0xa0, 0xdd, 0xde, 0x40, 0x8f, 0x7b, 0x03, 0x5d, 0x1f, 0x0c, + 0x65, 0x77, 0x30, 0x94, 0xbb, 0x83, 0xa1, 0x9c, 0x93, 0x77, 0x57, 0xb2, 0xc9, 0x0f, 0x4f, 0xae, + 0xc6, 0xf9, 0x2c, 0xaf, 0x66, 0xf8, 0x14, 0x00, 0x00, 0xff, 0xff, 0xb0, 0x18, 0x17, 0xb0, 0x92, + 0x02, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -248,8 +274,8 @@ const _ = grpc.SupportPackageIsVersion4 // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. type MsgClient interface { - Swap(ctx context.Context, in *MsgSwapRequest, opts ...grpc.CallOption) (*MsgSwapResponse, error) - SwapAll(ctx context.Context, in *MsgSwapAllRequest, opts ...grpc.CallOption) (*MsgSwapAllResponse, error) + Swap(ctx context.Context, in *MsgSwap, opts ...grpc.CallOption) (*MsgSwapResponse, error) + SwapAll(ctx context.Context, in *MsgSwapAll, opts ...grpc.CallOption) (*MsgSwapAllResponse, error) } type msgClient struct { @@ -260,7 +286,7 @@ func NewMsgClient(cc grpc1.ClientConn) MsgClient { return &msgClient{cc} } -func (c *msgClient) Swap(ctx context.Context, in *MsgSwapRequest, opts ...grpc.CallOption) (*MsgSwapResponse, error) { +func (c *msgClient) Swap(ctx context.Context, in *MsgSwap, opts ...grpc.CallOption) (*MsgSwapResponse, error) { out := new(MsgSwapResponse) err := c.cc.Invoke(ctx, "/lbm.fswap.v1.Msg/Swap", in, out, opts...) if err != nil { @@ -269,7 +295,7 @@ func (c *msgClient) Swap(ctx context.Context, in *MsgSwapRequest, opts ...grpc.C return out, nil } -func (c *msgClient) SwapAll(ctx context.Context, in *MsgSwapAllRequest, opts ...grpc.CallOption) (*MsgSwapAllResponse, error) { +func (c *msgClient) SwapAll(ctx context.Context, in *MsgSwapAll, opts ...grpc.CallOption) (*MsgSwapAllResponse, error) { out := new(MsgSwapAllResponse) err := c.cc.Invoke(ctx, "/lbm.fswap.v1.Msg/SwapAll", in, out, opts...) if err != nil { @@ -280,18 +306,18 @@ func (c *msgClient) SwapAll(ctx context.Context, in *MsgSwapAllRequest, opts ... // MsgServer is the server API for Msg service. type MsgServer interface { - Swap(context.Context, *MsgSwapRequest) (*MsgSwapResponse, error) - SwapAll(context.Context, *MsgSwapAllRequest) (*MsgSwapAllResponse, error) + Swap(context.Context, *MsgSwap) (*MsgSwapResponse, error) + SwapAll(context.Context, *MsgSwapAll) (*MsgSwapAllResponse, error) } // UnimplementedMsgServer can be embedded to have forward compatible implementations. type UnimplementedMsgServer struct { } -func (*UnimplementedMsgServer) Swap(ctx context.Context, req *MsgSwapRequest) (*MsgSwapResponse, error) { +func (*UnimplementedMsgServer) Swap(ctx context.Context, req *MsgSwap) (*MsgSwapResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method Swap not implemented") } -func (*UnimplementedMsgServer) SwapAll(ctx context.Context, req *MsgSwapAllRequest) (*MsgSwapAllResponse, error) { +func (*UnimplementedMsgServer) SwapAll(ctx context.Context, req *MsgSwapAll) (*MsgSwapAllResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method SwapAll not implemented") } @@ -300,7 +326,7 @@ func RegisterMsgServer(s grpc1.Server, srv MsgServer) { } func _Msg_Swap_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(MsgSwapRequest) + in := new(MsgSwap) if err := dec(in); err != nil { return nil, err } @@ -312,13 +338,13 @@ func _Msg_Swap_Handler(srv interface{}, ctx context.Context, dec func(interface{ FullMethod: "/lbm.fswap.v1.Msg/Swap", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(MsgServer).Swap(ctx, req.(*MsgSwapRequest)) + return srv.(MsgServer).Swap(ctx, req.(*MsgSwap)) } return interceptor(ctx, in, info, handler) } func _Msg_SwapAll_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(MsgSwapAllRequest) + in := new(MsgSwapAll) if err := dec(in); err != nil { return nil, err } @@ -330,7 +356,7 @@ func _Msg_SwapAll_Handler(srv interface{}, ctx context.Context, dec func(interfa FullMethod: "/lbm.fswap.v1.Msg/SwapAll", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(MsgServer).SwapAll(ctx, req.(*MsgSwapAllRequest)) + return srv.(MsgServer).SwapAll(ctx, req.(*MsgSwapAll)) } return interceptor(ctx, in, info, handler) } @@ -352,7 +378,7 @@ var _Msg_serviceDesc = grpc.ServiceDesc{ Metadata: "lbm/fswap/v1/tx.proto", } -func (m *MsgSwapRequest) Marshal() (dAtA []byte, err error) { +func (m *MsgSwap) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) n, err := m.MarshalToSizedBuffer(dAtA[:size]) @@ -362,16 +388,23 @@ func (m *MsgSwapRequest) Marshal() (dAtA []byte, err error) { return dAtA[:n], nil } -func (m *MsgSwapRequest) MarshalTo(dAtA []byte) (int, error) { +func (m *MsgSwap) MarshalTo(dAtA []byte) (int, error) { size := m.Size() return m.MarshalToSizedBuffer(dAtA[:size]) } -func (m *MsgSwapRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { +func (m *MsgSwap) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) _ = i var l int _ = l + if len(m.ToDenom) > 0 { + i -= len(m.ToDenom) + copy(dAtA[i:], m.ToDenom) + i = encodeVarintTx(dAtA, i, uint64(len(m.ToDenom))) + i-- + dAtA[i] = 0x1a + } { size, err := m.FromCoinAmount.MarshalToSizedBuffer(dAtA[:i]) if err != nil { @@ -415,7 +448,7 @@ func (m *MsgSwapResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } -func (m *MsgSwapAllRequest) Marshal() (dAtA []byte, err error) { +func (m *MsgSwapAll) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) n, err := m.MarshalToSizedBuffer(dAtA[:size]) @@ -425,16 +458,30 @@ func (m *MsgSwapAllRequest) Marshal() (dAtA []byte, err error) { return dAtA[:n], nil } -func (m *MsgSwapAllRequest) MarshalTo(dAtA []byte) (int, error) { +func (m *MsgSwapAll) MarshalTo(dAtA []byte) (int, error) { size := m.Size() return m.MarshalToSizedBuffer(dAtA[:size]) } -func (m *MsgSwapAllRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { +func (m *MsgSwapAll) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) _ = i var l int _ = l + if len(m.ToDenom) > 0 { + i -= len(m.ToDenom) + copy(dAtA[i:], m.ToDenom) + i = encodeVarintTx(dAtA, i, uint64(len(m.ToDenom))) + i-- + dAtA[i] = 0x1a + } + if len(m.FromDenom) > 0 { + i -= len(m.FromDenom) + copy(dAtA[i:], m.FromDenom) + i = encodeVarintTx(dAtA, i, uint64(len(m.FromDenom))) + i-- + dAtA[i] = 0x12 + } if len(m.FromAddress) > 0 { i -= len(m.FromAddress) copy(dAtA[i:], m.FromAddress) @@ -479,7 +526,7 @@ func encodeVarintTx(dAtA []byte, offset int, v uint64) int { dAtA[offset] = uint8(v) return base } -func (m *MsgSwapRequest) Size() (n int) { +func (m *MsgSwap) Size() (n int) { if m == nil { return 0 } @@ -491,6 +538,10 @@ func (m *MsgSwapRequest) Size() (n int) { } l = m.FromCoinAmount.Size() n += 1 + l + sovTx(uint64(l)) + l = len(m.ToDenom) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } return n } @@ -503,7 +554,7 @@ func (m *MsgSwapResponse) Size() (n int) { return n } -func (m *MsgSwapAllRequest) Size() (n int) { +func (m *MsgSwapAll) Size() (n int) { if m == nil { return 0 } @@ -513,6 +564,14 @@ func (m *MsgSwapAllRequest) Size() (n int) { if l > 0 { n += 1 + l + sovTx(uint64(l)) } + l = len(m.FromDenom) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } + l = len(m.ToDenom) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } return n } @@ -531,7 +590,7 @@ func sovTx(x uint64) (n int) { func sozTx(x uint64) (n int) { return sovTx(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } -func (m *MsgSwapRequest) Unmarshal(dAtA []byte) error { +func (m *MsgSwap) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { @@ -554,10 +613,10 @@ func (m *MsgSwapRequest) Unmarshal(dAtA []byte) error { fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { - return fmt.Errorf("proto: MsgSwapRequest: wiretype end group for non-group") + return fmt.Errorf("proto: MsgSwap: wiretype end group for non-group") } if fieldNum <= 0 { - return fmt.Errorf("proto: MsgSwapRequest: illegal tag %d (wire type %d)", fieldNum, wire) + return fmt.Errorf("proto: MsgSwap: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: @@ -625,6 +684,38 @@ func (m *MsgSwapRequest) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ToDenom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + 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 ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ToDenom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTx(dAtA[iNdEx:]) @@ -696,7 +787,7 @@ func (m *MsgSwapResponse) Unmarshal(dAtA []byte) error { } return nil } -func (m *MsgSwapAllRequest) Unmarshal(dAtA []byte) error { +func (m *MsgSwapAll) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { @@ -719,10 +810,10 @@ func (m *MsgSwapAllRequest) Unmarshal(dAtA []byte) error { fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { - return fmt.Errorf("proto: MsgSwapAllRequest: wiretype end group for non-group") + return fmt.Errorf("proto: MsgSwapAll: wiretype end group for non-group") } if fieldNum <= 0 { - return fmt.Errorf("proto: MsgSwapAllRequest: illegal tag %d (wire type %d)", fieldNum, wire) + return fmt.Errorf("proto: MsgSwapAll: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: @@ -757,6 +848,70 @@ func (m *MsgSwapAllRequest) Unmarshal(dAtA []byte) error { } m.FromAddress = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field FromDenom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + 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 ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.FromDenom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ToDenom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + 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 ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ToDenom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTx(dAtA[iNdEx:]) From e2ac84c2c7bacec53fc989c3a027d354093f5fef Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Tue, 30 Apr 2024 21:44:50 +0900 Subject: [PATCH 17/47] chore: clean unnecessary member variables --- x/fswap/keeper/keeper.go | 74 ++++++++----------------------- x/fswap/keeper/keeper_test.go | 60 +++++++++++-------------- x/fswap/keeper/msg_server_test.go | 24 +++++----- 3 files changed, 57 insertions(+), 101 deletions(-) diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 450a43ce21..7706d5780c 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -19,12 +19,6 @@ type Keeper struct { AccountKeeper BankKeeper - - fromDenom string - toDenom string - swapInit types.SwapInit - swapMultiple sdk.Int - swapCap sdk.Int } func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, ak AccountKeeper, bk BankKeeper) Keeper { @@ -33,11 +27,6 @@ func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, ak AccountKe storeKey: storeKey, AccountKeeper: ak, BankKeeper: bk, - fromDenom: "", - toDenom: "", - swapInit: types.SwapInit{}, - swapMultiple: sdk.ZeroInt(), - swapCap: sdk.ZeroInt(), } } @@ -72,7 +61,7 @@ func (k Keeper) SwapInit(ctx sdk.Context, swapInit types.SwapInit) error { } func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Coin, toDenom string) error { - swapInit, err := k.getSwapInit(ctx) + swapInit, err := k.swapInit(ctx) if err != nil { return err } @@ -169,7 +158,7 @@ func (k Keeper) iterateAllSwapped(ctx sdk.Context, cb func(swapped types.Swapped } func (k Keeper) getSwapped(ctx sdk.Context) (types.Swapped, error) { - toDenom, err := k.getToDenom(ctx) + toDenom, err := k.toDenom(ctx) if err != nil { return types.Swapped{}, err } @@ -225,7 +214,7 @@ func (k Keeper) iterateAllSwapInits(ctx sdk.Context, cb func(swapped types.SwapI } func (k Keeper) getSwappableNewCoinAmount(ctx sdk.Context) (sdk.Coin, error) { - swapCap, err := k.getSwapCap(ctx) + swapCap, err := k.swapCap(ctx) if err != nil { return sdk.Coin{}, err } @@ -233,7 +222,7 @@ func (k Keeper) getSwappableNewCoinAmount(ctx sdk.Context) (sdk.Coin, error) { if err != nil { return sdk.Coin{}, err } - denom, err := k.getToDenom(ctx) + denom, err := k.toDenom(ctx) if err != nil { return sdk.Coin{}, err } @@ -243,74 +232,49 @@ func (k Keeper) getSwappableNewCoinAmount(ctx sdk.Context) (sdk.Coin, error) { return sdk.NewCoin(denom, remainingAmount), nil } -func (k Keeper) getFromDenom(ctx sdk.Context) (string, error) { - if len(k.fromDenom) > 0 { - return k.fromDenom, nil - } - - swapInit, err := k.getSwapInit(ctx) +func (k Keeper) fromDenom(ctx sdk.Context) (string, error) { + swapInit, err := k.swapInit(ctx) if err != nil { return "", err } - k.fromDenom = swapInit.GetFromDenom() - return k.fromDenom, nil + return swapInit.FromDenom, nil } -func (k Keeper) getToDenom(ctx sdk.Context) (string, error) { - if len(k.toDenom) > 0 { - return k.toDenom, nil - } - - swapInit, err := k.getSwapInit(ctx) +func (k Keeper) toDenom(ctx sdk.Context) (string, error) { + swapInit, err := k.swapInit(ctx) if err != nil { return "", err } - k.toDenom = swapInit.GetToDenom() - return k.toDenom, nil + return swapInit.GetToDenom(), nil } -func (k Keeper) getSwapMultiple(ctx sdk.Context) (sdk.Int, error) { - if k.swapMultiple.IsPositive() { - return k.swapMultiple, nil - } - - swapInit, err := k.getSwapInit(ctx) +func (k Keeper) swapMultiple(ctx sdk.Context) (sdk.Int, error) { + swapInit, err := k.swapInit(ctx) if err != nil { return sdk.Int{}, err } - k.swapMultiple = swapInit.SwapMultiple - return k.swapMultiple, nil + return swapInit.SwapMultiple, nil } -func (k Keeper) getSwapCap(ctx sdk.Context) (sdk.Int, error) { - if k.swapCap.IsPositive() { - return k.swapCap, nil - } - - swapInit, err := k.getSwapInit(ctx) +func (k Keeper) swapCap(ctx sdk.Context) (sdk.Int, error) { + swapInit, err := k.swapInit(ctx) if err != nil { return sdk.Int{}, err } - k.swapCap = swapInit.AmountCapForToDenom - return k.swapCap, nil + return swapInit.AmountCapForToDenom, nil } -func (k Keeper) getSwapInit(ctx sdk.Context) (types.SwapInit, error) { - if !k.swapInit.IsEmpty() { - return k.swapInit, nil - } - +func (k Keeper) swapInit(ctx sdk.Context) (types.SwapInit, error) { swapInits := k.getAllSwapInits(ctx) if len(swapInits) == 0 { return types.SwapInit{}, types.ErrSwapNotInitialized } - k.swapInit = swapInits[0] - return k.swapInit, nil + return swapInits[0], nil } func (k Keeper) updateSwapped(ctx sdk.Context, fromAmount, toAmount sdk.Coin) error { @@ -340,7 +304,7 @@ func (k Keeper) checkSwapCap(ctx sdk.Context, newCoinAmount sdk.Coin) error { return err } - swapCap, err := k.getSwapCap(ctx) + swapCap, err := k.swapCap(ctx) if err != nil { return err } diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index a49b42317c..9880b13e17 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -30,11 +30,7 @@ type KeeperTestSuite struct { accWithToCoin sdk.AccAddress initBalance sdk.Int - swapInit types.SwapInit - fromDenom string - toDenom string - swapMultiple sdk.Int - swapCap sdk.Int + swapInit types.SwapInit } func (s *KeeperTestSuite) createRandomAccounts(n int) []sdk.AccAddress { @@ -66,17 +62,13 @@ func (s *KeeperTestSuite) SetupTest() { s.queryServer = keeper.NewQueryServer(s.keeper) s.msgServer = keeper.NewMsgServer(s.keeper) - s.fromDenom = "fromdenom" - s.toDenom = "todenom" - s.swapMultiple = sdk.NewInt(1000) - s.initBalance = sdk.NewInt(123456789) numAcc := int64(2) - s.swapCap = s.initBalance.Mul(s.swapMultiple.Mul(sdk.NewInt(numAcc))) + s.initBalance = sdk.NewInt(123456789) s.swapInit = types.SwapInit{ - FromDenom: s.fromDenom, - ToDenom: s.toDenom, - AmountCapForToDenom: s.swapCap, - SwapMultiple: s.swapMultiple, + FromDenom: "fromdenom", + ToDenom: "todenom", + AmountCapForToDenom: s.initBalance.Mul(sdk.NewInt(1000).Mul(sdk.NewInt(numAcc))), + SwapMultiple: sdk.NewInt(1000), } s.createAccountsWithInitBalance(app) app.AccountKeeper.GetModuleAccount(s.ctx, types.ModuleName) @@ -91,11 +83,11 @@ func (s *KeeperTestSuite) createAccountsWithInitBalance(app *simapp.SimApp) { *addresses[i] = address } minter := app.AccountKeeper.GetModuleAccount(s.ctx, minttypes.ModuleName).GetAddress() - fromAmount := sdk.NewCoins(sdk.NewCoin(s.fromDenom, s.initBalance)) + fromAmount := sdk.NewCoins(sdk.NewCoin(s.swapInit.GetFromDenom(), s.initBalance)) s.Require().NoError(app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, fromAmount)) s.Require().NoError(app.BankKeeper.SendCoins(s.ctx, minter, s.accWithFromCoin, fromAmount)) - toAmount := sdk.NewCoins(sdk.NewCoin(s.toDenom, s.initBalance)) + toAmount := sdk.NewCoins(sdk.NewCoin(s.swapInit.GetToDenom(), s.initBalance)) s.Require().NoError(app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, toAmount)) s.Require().NoError(app.BankKeeper.SendCoins(s.ctx, minter, s.accWithToCoin, toAmount)) } @@ -115,40 +107,40 @@ func (s *KeeperTestSuite) TestSwap() { }{ "swap some": { s.accWithFromCoin, - sdk.NewCoin(s.fromDenom, sdk.NewInt(100)), - s.toDenom, + sdk.NewCoin(s.swapInit.GetFromDenom(), sdk.NewInt(100)), + s.swapInit.GetToDenom(), sdk.NewInt(100), false, nil, }, "swap all the balance": { s.accWithFromCoin, - sdk.NewCoin(s.fromDenom, s.initBalance), - s.toDenom, + sdk.NewCoin(s.swapInit.GetFromDenom(), s.initBalance), + s.swapInit.GetToDenom(), s.initBalance, false, nil, }, "swap without holding enough balance": { s.accWithFromCoin, - sdk.NewCoin(s.fromDenom, sdk.OneInt().Add(s.initBalance)), - s.toDenom, + sdk.NewCoin(s.swapInit.GetFromDenom(), sdk.OneInt().Add(s.initBalance)), + s.swapInit.GetToDenom(), sdk.ZeroInt(), true, sdkerrors.ErrInsufficientFunds, }, "account holding new coin only": { s.accWithToCoin, - sdk.NewCoin(s.fromDenom, sdk.NewInt(100)), - s.toDenom, + sdk.NewCoin(s.swapInit.GetFromDenom(), sdk.NewInt(100)), + s.swapInit.GetToDenom(), sdk.ZeroInt(), true, sdkerrors.ErrInsufficientFunds, }, "swap with the same from-denom and to-denom": { s.accWithFromCoin, - sdk.NewCoin(s.toDenom, s.initBalance), - s.toDenom, + sdk.NewCoin(s.swapInit.GetToDenom(), s.initBalance), + s.swapInit.GetToDenom(), sdk.ZeroInt(), true, sdkerrors.ErrInvalidRequest, @@ -167,8 +159,8 @@ func (s *KeeperTestSuite) TestSwap() { } s.Require().NoError(err) - actualAmount := s.keeper.GetBalance(ctx, tc.from, s.toDenom).Amount - expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapMultiple) + actualAmount := s.keeper.GetBalance(ctx, tc.from, s.swapInit.GetToDenom()).Amount + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapInit.SwapMultiple) s.Require().Equal(expectedAmount, actualAmount) }) } @@ -185,16 +177,16 @@ func (s *KeeperTestSuite) TestSwapAll() { }{ "account holding from coin": { s.accWithFromCoin, - s.fromDenom, - s.toDenom, + s.swapInit.GetFromDenom(), + s.swapInit.GetToDenom(), s.initBalance, false, nil, }, "account holding to coin only": { s.accWithToCoin, - s.fromDenom, - s.toDenom, + s.swapInit.GetFromDenom(), + s.swapInit.GetToDenom(), s.initBalance, true, sdkerrors.ErrInsufficientFunds, @@ -213,8 +205,8 @@ func (s *KeeperTestSuite) TestSwapAll() { } s.Require().NoError(err) - actualAmount := s.keeper.GetBalance(ctx, tc.from, s.toDenom).Amount - expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapMultiple) + actualAmount := s.keeper.GetBalance(ctx, tc.from, s.swapInit.GetToDenom()).Amount + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapInit.SwapMultiple) s.Require().Equal(expectedAmount, actualAmount) }) } diff --git a/x/fswap/keeper/msg_server_test.go b/x/fswap/keeper/msg_server_test.go index 8dde4f9b4b..dc0e4093f9 100644 --- a/x/fswap/keeper/msg_server_test.go +++ b/x/fswap/keeper/msg_server_test.go @@ -16,8 +16,8 @@ func (s *KeeperTestSuite) TestMsgSwap() { "swap some": { &types.MsgSwap{ FromAddress: s.accWithFromCoin.String(), - FromCoinAmount: sdk.NewCoin(s.fromDenom, sdk.NewInt(100)), - ToDenom: s.toDenom, + FromCoinAmount: sdk.NewCoin(s.swapInit.GetFromDenom(), sdk.NewInt(100)), + ToDenom: s.swapInit.GetToDenom(), }, sdk.NewInt(100), false, @@ -26,8 +26,8 @@ func (s *KeeperTestSuite) TestMsgSwap() { "swap all the balance": { &types.MsgSwap{ FromAddress: s.accWithFromCoin.String(), - FromCoinAmount: sdk.NewCoin(s.fromDenom, s.initBalance), - ToDenom: s.toDenom, + FromCoinAmount: sdk.NewCoin(s.swapInit.GetFromDenom(), s.initBalance), + ToDenom: s.swapInit.GetToDenom(), }, s.initBalance, false, @@ -36,8 +36,8 @@ func (s *KeeperTestSuite) TestMsgSwap() { "account holding new coin only": { &types.MsgSwap{ FromAddress: s.accWithToCoin.String(), - FromCoinAmount: sdk.NewCoin(s.fromDenom, sdk.NewInt(100)), - ToDenom: s.toDenom, + FromCoinAmount: sdk.NewCoin(s.swapInit.GetFromDenom(), sdk.NewInt(100)), + ToDenom: s.swapInit.GetToDenom(), }, s.initBalance, true, @@ -61,7 +61,7 @@ func (s *KeeperTestSuite) TestMsgSwap() { from, err := sdk.AccAddressFromBech32(tc.request.FromAddress) s.Require().NoError(err) actualAmount := s.keeper.GetBalance(ctx, from, tc.request.GetToDenom()).Amount - expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapMultiple) + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapInit.SwapMultiple) s.Require().Equal(expectedAmount, actualAmount) }) } @@ -77,8 +77,8 @@ func (s *KeeperTestSuite) TestMsgSwapAll() { "swapAll": { &types.MsgSwapAll{ FromAddress: s.accWithFromCoin.String(), - FromDenom: s.fromDenom, - ToDenom: s.toDenom, + FromDenom: s.swapInit.GetFromDenom(), + ToDenom: s.swapInit.GetToDenom(), }, s.initBalance, false, @@ -87,8 +87,8 @@ func (s *KeeperTestSuite) TestMsgSwapAll() { "account holding new coin only": { &types.MsgSwapAll{ FromAddress: s.accWithToCoin.String(), - FromDenom: s.fromDenom, - ToDenom: s.toDenom, + FromDenom: s.swapInit.GetFromDenom(), + ToDenom: s.swapInit.GetToDenom(), }, s.initBalance, true, @@ -112,7 +112,7 @@ func (s *KeeperTestSuite) TestMsgSwapAll() { from, err := sdk.AccAddressFromBech32(tc.request.FromAddress) s.Require().NoError(err) actualAmount := s.keeper.GetBalance(ctx, from, tc.request.GetToDenom()).Amount - expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapMultiple) + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapInit.SwapMultiple) s.Require().Equal(expectedAmount, actualAmount) }) } From 8b1afaf6943ccad5705f3b7f68f3ec02608bc01a Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 09:09:30 +0900 Subject: [PATCH 18/47] chore: update proto, introduce config, clean code --- docs/core/proto-docs.md | 47 +++- proto/lbm/fswap/v1/fswap.proto | 17 +- proto/lbm/fswap/v1/genesis.proto | 5 +- proto/lbm/fswap/v1/query.proto | 10 +- simapp/app.go | 6 +- x/fswap/client/cli/query.go | 2 +- x/fswap/client/cli/tx.go | 4 +- x/fswap/handler.go | 14 +- x/fswap/keeper/genesis.go | 36 ++- x/fswap/keeper/genesis_test.go | 8 +- x/fswap/keeper/grpc_query.go | 8 +- x/fswap/keeper/keeper.go | 267 +++++++++++---------- x/fswap/keeper/keeper_test.go | 66 +++--- x/fswap/keeper/keys.go | 19 +- x/fswap/keeper/msg_server_test.go | 28 +-- x/fswap/module.go | 32 +-- x/fswap/proposal_handler_test.go | 6 +- x/fswap/types/codec.go | 4 +- x/fswap/types/config.go | 13 ++ x/fswap/types/errors.go | 8 +- x/fswap/types/fswap.go | 99 ++++---- x/fswap/types/fswap.pb.go | 370 ++++++++++++++++++++++-------- x/fswap/types/genesis.go | 41 ++-- x/fswap/types/genesis.pb.go | 124 +++++++--- x/fswap/types/proposal.go | 26 +-- x/fswap/types/query.pb.go | 264 ++++++++++++++++++--- x/fswap/types/query.pb.gw.go | 36 +++ 27 files changed, 1054 insertions(+), 506 deletions(-) create mode 100644 x/fswap/types/config.go diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index 16831e12a1..183452dd62 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -860,8 +860,9 @@ - [EventSwapCoins](#lbm.fswap.v1.EventSwapCoins) - [lbm/fswap/v1/fswap.proto](#lbm/fswap/v1/fswap.proto) - - [SwapInit](#lbm.fswap.v1.SwapInit) - - [SwapInitProposal](#lbm.fswap.v1.SwapInitProposal) + - [Swap](#lbm.fswap.v1.Swap) + - [SwapProposal](#lbm.fswap.v1.SwapProposal) + - [SwapStats](#lbm.fswap.v1.SwapStats) - [Swapped](#lbm.fswap.v1.Swapped) - [lbm/fswap/v1/genesis.proto](#lbm/fswap/v1/genesis.proto) @@ -12750,9 +12751,9 @@ Msg defines the foundation Msg service. - + -### SwapInit +### Swap @@ -12768,9 +12769,9 @@ Msg defines the foundation Msg service. - + -### SwapInitProposal +### SwapProposal From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 @@ -12778,7 +12779,22 @@ From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk base | ----- | ---- | ----- | ----------- | | `title` | [string](#string) | | | | `description` | [string](#string) | | | -| `swap_init` | [SwapInit](#lbm.fswap.v1.SwapInit) | | | +| `swap` | [Swap](#lbm.fswap.v1.Swap) | | | + + + + + + + + +### SwapStats + + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `swap_count` | [int32](#int32) | | | @@ -12825,8 +12841,9 @@ GenesisState defines the fswap module's genesis state. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `swap_init` | [SwapInit](#lbm.fswap.v1.SwapInit) | repeated | | -| `swapped` | [Swapped](#lbm.fswap.v1.Swapped) | repeated | | +| `swaps` | [Swap](#lbm.fswap.v1.Swap) | repeated | | +| `swap_stats` | [SwapStats](#lbm.fswap.v1.SwapStats) | | | +| `swappeds` | [Swapped](#lbm.fswap.v1.Swapped) | repeated | | @@ -12855,6 +12872,12 @@ GenesisState defines the fswap module's genesis state. +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `fromDenom` | [string](#string) | | | +| `toDenom` | [string](#string) | | | + + @@ -12881,6 +12904,12 @@ GenesisState defines the fswap module's genesis state. +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `fromDenom` | [string](#string) | | | +| `toDenom` | [string](#string) | | | + + diff --git a/proto/lbm/fswap/v1/fswap.proto b/proto/lbm/fswap/v1/fswap.proto index 1216ceca57..d771b253b7 100644 --- a/proto/lbm/fswap/v1/fswap.proto +++ b/proto/lbm/fswap/v1/fswap.proto @@ -6,7 +6,7 @@ option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types"; import "gogoproto/gogo.proto"; import "cosmos/base/v1beta1/coin.proto"; -message SwapInit { +message Swap { option (gogoproto.equal) = true; option (gogoproto.goproto_stringer) = false; @@ -18,14 +18,21 @@ message SwapInit { [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; } +message SwapStats { + option (gogoproto.equal) = true; + option (gogoproto.goproto_stringer) = false; + + int32 swap_count = 1; +} + // From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 -message SwapInitProposal { +message SwapProposal { option (gogoproto.equal) = true; option (gogoproto.goproto_stringer) = false; - string title = 1; - string description = 2; - SwapInit swap_init = 3 [(gogoproto.nullable) = false]; + string title = 1; + string description = 2; + Swap swap = 3 [(gogoproto.nullable) = false]; } message Swapped { diff --git a/proto/lbm/fswap/v1/genesis.proto b/proto/lbm/fswap/v1/genesis.proto index 539454bc2c..c001741269 100644 --- a/proto/lbm/fswap/v1/genesis.proto +++ b/proto/lbm/fswap/v1/genesis.proto @@ -8,6 +8,7 @@ import "lbm/fswap/v1/fswap.proto"; // GenesisState defines the fswap module's genesis state. message GenesisState { - repeated SwapInit swap_init = 1 [(gogoproto.nullable) = false]; - repeated Swapped swapped = 2 [(gogoproto.nullable) = false]; + repeated Swap swaps = 1 [(gogoproto.nullable) = false]; + SwapStats swap_stats = 2 [(gogoproto.nullable) = false]; + repeated Swapped swappeds = 3 [(gogoproto.nullable) = false]; } diff --git a/proto/lbm/fswap/v1/query.proto b/proto/lbm/fswap/v1/query.proto index 3502013229..825d66ad30 100644 --- a/proto/lbm/fswap/v1/query.proto +++ b/proto/lbm/fswap/v1/query.proto @@ -21,7 +21,10 @@ service Query { } } -message QuerySwappedRequest {} +message QuerySwappedRequest { + string fromDenom = 1; + string toDenom = 2; +} message QuerySwappedResponse { cosmos.base.v1beta1.Coin from_coin_amount = 1 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; @@ -29,7 +32,10 @@ message QuerySwappedResponse { [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; } -message QueryTotalSwappableToCoinAmountRequest {} +message QueryTotalSwappableToCoinAmountRequest { + string fromDenom = 1; + string toDenom = 2; +} message QueryTotalSwappableToCoinAmountResponse { cosmos.base.v1beta1.Coin swappable_amount = 1 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; diff --git a/simapp/app.go b/simapp/app.go index 237c7e049e..49885f9ec3 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -326,9 +326,7 @@ func NewSimApp( app.ClassKeeper = classkeeper.NewKeeper(appCodec, keys[class.StoreKey]) app.TokenKeeper = tokenkeeper.NewKeeper(appCodec, keys[token.StoreKey], app.ClassKeeper) app.CollectionKeeper = collectionkeeper.NewKeeper(appCodec, keys[collection.StoreKey], app.ClassKeeper) - - /**** Phase 1 ****/ - app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], app.AccountKeeper, app.BankKeeper) + app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], fswaptypes.DefaultConfig(), app.AccountKeeper, app.BankKeeper) // register the staking hooks // NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks @@ -345,7 +343,7 @@ func NewSimApp( AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)). AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)). AddRoute(foundation.RouterKey, foundationkeeper.NewFoundationProposalsHandler(app.FoundationKeeper)). - AddRoute(fswaptypes.RouterKey, fswap.NewSwapInitHandler(app.FswapKeeper)) + AddRoute(fswaptypes.RouterKey, fswap.NewSwapHandler(app.FswapKeeper)) govKeeper := govkeeper.NewKeeper( appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper, diff --git a/x/fswap/client/cli/query.go b/x/fswap/client/cli/query.go index feddf7dc1f..d5b1a60778 100644 --- a/x/fswap/client/cli/query.go +++ b/x/fswap/client/cli/query.go @@ -14,7 +14,7 @@ import ( func GetQueryCmd(queryRoute string) *cobra.Command { // Group fswap queries under a subcommand cmd := &cobra.Command{ - Use: types.ModuleName, + Use: queryRoute, Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName), DisableFlagParsing: true, SuggestionsMinimumDistance: 2, diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index 0ab9c915ec..8baec3f747 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -183,13 +183,13 @@ func parseArgsToContent(cmd *cobra.Command) (gov.Content, error) { return nil, err } - swapInit := types.SwapInit{ + swapInit := types.Swap{ FromDenom: from_denom, ToDenom: to_denom, AmountCapForToDenom: sdk.NewInt(amount_limit), SwapMultiple: sdk.NewInt(swap_rate), } - content := types.NewSwapInitProposal(title, description, swapInit) + content := types.NewSwapProposal(title, description, swapInit) return content, nil } diff --git a/x/fswap/handler.go b/x/fswap/handler.go index d8dfaac2f6..8c0a4f7114 100644 --- a/x/fswap/handler.go +++ b/x/fswap/handler.go @@ -26,13 +26,13 @@ func NewHandler(k keeper.Keeper) sdk.Handler { } } -// NewSwapInitHandler creates a governance handler to manage new proposal types. -// It enables SwapInit to propose an fswap init -func NewSwapInitHandler(k keeper.Keeper) govtypes.Handler { +// NewSwapHandler creates a governance handler to manage new proposal types. +// It enables Swap to propose a swap init +func NewSwapHandler(k keeper.Keeper) govtypes.Handler { return func(ctx sdk.Context, content govtypes.Content) error { switch c := content.(type) { - case *types.SwapInitProposal: - return handleSwapInit(ctx, k, c) + case *types.SwapProposal: + return handleSwap(ctx, k, c) default: return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized sawp proposal content type: %T", c) @@ -40,6 +40,6 @@ func NewSwapInitHandler(k keeper.Keeper) govtypes.Handler { } } -func handleSwapInit(ctx sdk.Context, k keeper.Keeper, p *types.SwapInitProposal) error { - return k.SwapInit(ctx, p.SwapInit) +func handleSwap(ctx sdk.Context, k keeper.Keeper, p *types.SwapProposal) error { + return k.MakeSwap(ctx, p.Swap) } diff --git a/x/fswap/keeper/genesis.go b/x/fswap/keeper/genesis.go index 39ef32bf68..20b01e3656 100644 --- a/x/fswap/keeper/genesis.go +++ b/x/fswap/keeper/genesis.go @@ -7,29 +7,45 @@ import ( // InitGenesis initializes the module's state from a provided genesis state. func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error { - if len(genState.GetSwapInit()) > 1 { - return types.ErrSwapCanNotBeInitializedTwice.Wrap("cannot initialize genesis state, there are more than 1 swapInit") + if err := genState.Validate(); err != nil { + return err } - if len(genState.GetSwapped()) > 1 { - return types.ErrSwapCanNotBeInitializedTwice.Wrap("cannot initialize genesis state, there are more than 1 swapped") + + if len(genState.GetSwaps()) > k.config.MaxSwaps && !k.isUnlimited() { + return types.ErrCanNotHaveMoreSwap.Wrapf("cannot initialize genesis state, there are more than %d swapInits", k.config.MaxSwaps) + } + + if len(genState.GetSwappeds()) > k.config.MaxSwaps && !k.isUnlimited() { + return types.ErrCanNotHaveMoreSwap.Wrapf("cannot initialize genesis state, there are more than %d swapped", k.config.MaxSwaps) } - for _, swapInit := range genState.GetSwapInit() { - if err := k.setSwapInit(ctx, swapInit); err != nil { + + for _, swap := range genState.GetSwaps() { + if err := k.MakeSwap(ctx, swap); err != nil { panic(err) } } - for _, swapped := range genState.GetSwapped() { - if err := swapped.Validate(); err != nil { + + for _, swapped := range genState.GetSwappeds() { + if err := swapped.ValidateBasic(); err != nil { panic(err) } } return nil } +func (k Keeper) isUnlimited() bool { + return k.config.MaxSwaps == 0 +} + // ExportGenesis returns the module's exported genesis. func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { + stats, err := k.getSwapStats(ctx) + if err != nil { + panic(err) + } return &types.GenesisState{ - SwapInit: k.getAllSwapInits(ctx), - Swapped: k.getAllSwapped(ctx), + Swaps: k.getAllSwaps(ctx), + SwapStats: stats, + Swappeds: k.getAllSwapped(ctx), } } diff --git a/x/fswap/keeper/genesis_test.go b/x/fswap/keeper/genesis_test.go index d57f9a24f8..c4fa1a0e99 100644 --- a/x/fswap/keeper/genesis_test.go +++ b/x/fswap/keeper/genesis_test.go @@ -1,6 +1,8 @@ package keeper_test import ( + "fmt" + "github.com/Finschia/finschia-sdk/x/fswap/types" ) @@ -11,7 +13,9 @@ func (s *KeeperTestSuite) TestInitAndExportGenesis() { s.Require().NoError(err) exportGenesis := s.keeper.ExportGenesis(ctx) + fmt.Println(len(exportGenesis.GetSwaps())) s.Require().Equal(defaultGenesis, exportGenesis) - s.Require().Equal(defaultGenesis.GetSwapInit(), exportGenesis.GetSwapInit()) - s.Require().Equal(defaultGenesis.GetSwapped(), exportGenesis.GetSwapped()) + s.Require().Equal(defaultGenesis.GetSwaps(), exportGenesis.GetSwaps()) + s.Require().Equal(defaultGenesis.GetSwapStats(), exportGenesis.GetSwapStats()) + s.Require().Equal(defaultGenesis.GetSwappeds(), exportGenesis.GetSwappeds()) } diff --git a/x/fswap/keeper/grpc_query.go b/x/fswap/keeper/grpc_query.go index 44ce360b82..1a47467cff 100644 --- a/x/fswap/keeper/grpc_query.go +++ b/x/fswap/keeper/grpc_query.go @@ -19,10 +19,10 @@ func NewQueryServer(keeper Keeper) *QueryServer { } } -func (s QueryServer) Swapped(ctx context.Context, _ *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) { +func (s QueryServer) Swapped(ctx context.Context, req *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) { c := sdk.UnwrapSDKContext(ctx) - swapped, err := s.Keeper.getSwapped(c) + swapped, err := s.Keeper.getSwapped(c, req.GetFromDenom(), req.GetToDenom()) if err != nil { return nil, err } @@ -33,10 +33,10 @@ func (s QueryServer) Swapped(ctx context.Context, _ *types.QuerySwappedRequest) }, nil } -func (s QueryServer) TotalSwappableToCoinAmount(ctx context.Context, _ *types.QueryTotalSwappableToCoinAmountRequest) (*types.QueryTotalSwappableToCoinAmountResponse, error) { +func (s QueryServer) TotalSwappableToCoinAmount(ctx context.Context, req *types.QueryTotalSwappableToCoinAmountRequest) (*types.QueryTotalSwappableToCoinAmountResponse, error) { c := sdk.UnwrapSDKContext(ctx) - amount, err := s.Keeper.getSwappableNewCoinAmount(c) + amount, err := s.Keeper.getSwappableNewCoinAmount(c, req.GetFromDenom(), req.GetToDenom()) if err != nil { return &types.QueryTotalSwappableToCoinAmountResponse{}, err } diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 7706d5780c..06f3bee98a 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -17,16 +17,24 @@ type Keeper struct { cdc codec.BinaryCodec storeKey storetypes.StoreKey + config types.Config + AccountKeeper BankKeeper + + swaps []types.Swap + swappedSlice []types.Swapped } -func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, ak AccountKeeper, bk BankKeeper) Keeper { +func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, config types.Config, ak AccountKeeper, bk BankKeeper) Keeper { return Keeper{ - cdc: cdc, - storeKey: storeKey, - AccountKeeper: ak, - BankKeeper: bk, + cdc, + storeKey, + config, + ak, + bk, + make([]types.Swap, config.MaxSwaps), + make([]types.Swapped, config.MaxSwaps), } } @@ -34,49 +42,72 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) } -func (k Keeper) SwapInit(ctx sdk.Context, swapInit types.SwapInit) error { - if err := swapInit.ValidateBasic(); err != nil { +func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap) error { + if err := swap.ValidateBasic(); err != nil { return err } - if k.hasBeenInitialized(ctx) { - return types.ErrSwapCanNotBeInitializedTwice + + isNewSwap := true + if _, err := k.getSwap(ctx, swap.ToDenom, swap.FromDenom); err == nil { + return sdkerrors.ErrInvalidRequest.Wrap("there is already a swap in reverse way, only one way swap allowed") + } + + if _, err := k.getSwap(ctx, swap.FromDenom, swap.ToDenom); err == nil { + isNewSwap = false } - if err := k.setSwapInit(ctx, swapInit); err != nil { + + if !isNewSwap && !k.config.UpdateAllowed { + return sdkerrors.ErrInvalidRequest.Wrap("update existing swap not allowed") + } + + if err := k.increaseSwapCount(ctx); err != nil { return err } - swapped := types.Swapped{ - FromCoinAmount: sdk.Coin{ - Denom: swapInit.GetFromDenom(), - Amount: sdk.ZeroInt(), - }, - ToCoinAmount: sdk.Coin{ - Denom: swapInit.GetToDenom(), - Amount: sdk.ZeroInt(), - }, - } - if err := k.setSwapped(ctx, swapped); err != nil { + + key := swapKey(swap.FromDenom, swap.ToDenom) + bz, err := k.cdc.Marshal(&swap) + if err != nil { return err } + + store := ctx.KVStore(k.storeKey) + store.Set(key, bz) + + if isNewSwap { + swapped := types.Swapped{ + FromCoinAmount: sdk.Coin{ + Denom: swap.GetFromDenom(), + Amount: sdk.ZeroInt(), + }, + ToCoinAmount: sdk.Coin{ + Denom: swap.GetToDenom(), + Amount: sdk.ZeroInt(), + }, + } + if err := k.setSwapped(ctx, swap.GetFromDenom(), swap.GetToDenom(), swapped); err != nil { + return err + } + } return nil } func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Coin, toDenom string) error { - swapInit, err := k.swapInit(ctx) + swap, err := k.getSwap(ctx, fromCoinAmount.Denom, toDenom) if err != nil { return err } - if swapInit.GetFromDenom() != fromCoinAmount.GetDenom() { - return sdkerrors.ErrInvalidRequest.Wrapf("denom mismatch, expected %s, got %s", swapInit.GetFromDenom(), fromCoinAmount.Denom) + if swap.GetFromDenom() != fromCoinAmount.GetDenom() { + return sdkerrors.ErrInvalidRequest.Wrapf("denom mismatch, expected %s, got %s", swap.GetFromDenom(), fromCoinAmount.Denom) } - if swapInit.GetToDenom() != toDenom { - return sdkerrors.ErrInvalidRequest.Wrapf("denom mismatch, expected %s, got %s", swapInit.GetToDenom(), toDenom) + if swap.GetToDenom() != toDenom { + return sdkerrors.ErrInvalidRequest.Wrapf("denom mismatch, expected %s, got %s", swap.GetToDenom(), toDenom) } - newAmount := fromCoinAmount.Amount.Mul(swapInit.SwapMultiple) + newAmount := fromCoinAmount.Amount.Mul(swap.SwapMultiple) newCoinAmount := sdk.NewCoin(toDenom, newAmount) - if err := k.checkSwapCap(ctx, newCoinAmount); err != nil { + if err := k.checkSwapCap(ctx, swap, newCoinAmount); err != nil { return err } @@ -122,23 +153,17 @@ func (k Keeper) SwapAll(ctx sdk.Context, addr sdk.AccAddress, fromDenom, toDenom return nil } -func (k Keeper) setSwapInit(ctx sdk.Context, swapInit types.SwapInit) error { - store := ctx.KVStore(k.storeKey) - bz, err := k.cdc.Marshal(&swapInit) - if err != nil { - return err - } - store.Set(swapInitKey(swapInit.ToDenom), bz) - return nil -} - func (k Keeper) getAllSwapped(ctx sdk.Context) []types.Swapped { - swappedSlice := make([]types.Swapped, 0) // TODO(bjs) + idx := 0 k.iterateAllSwapped(ctx, func(swapped types.Swapped) bool { - swappedSlice = append(swappedSlice, swapped) + k.swappedSlice[idx] = swapped + idx++ return false }) - return swappedSlice + if idx == 0 { + return []types.Swapped{} + } + return k.swappedSlice } func (k Keeper) iterateAllSwapped(ctx sdk.Context, cb func(swapped types.Swapped) (stop bool)) { @@ -157,14 +182,9 @@ func (k Keeper) iterateAllSwapped(ctx sdk.Context, cb func(swapped types.Swapped } } -func (k Keeper) getSwapped(ctx sdk.Context) (types.Swapped, error) { - toDenom, err := k.toDenom(ctx) - if err != nil { - return types.Swapped{}, err - } - +func (k Keeper) getSwapped(ctx sdk.Context, fromDenom, toDenom string) (types.Swapped, error) { store := ctx.KVStore(k.storeKey) - key := swappedKey(toDenom) + key := swappedKey(fromDenom, toDenom) bz := store.Get(key) if bz == nil { return types.Swapped{}, types.ErrSwappedNotFound @@ -177,145 +197,154 @@ func (k Keeper) getSwapped(ctx sdk.Context) (types.Swapped, error) { return swapped, nil } -func (k Keeper) setSwapped(ctx sdk.Context, swapped types.Swapped) error { - store := ctx.KVStore(k.storeKey) - key := swappedKey(swapped.ToCoinAmount.Denom) +func (k Keeper) setSwapped(ctx sdk.Context, fromDenom, toDenom string, swapped types.Swapped) error { + key := swappedKey(fromDenom, toDenom) bz, err := k.cdc.Marshal(&swapped) if err != nil { return err } + store := ctx.KVStore(k.storeKey) store.Set(key, bz) return nil } -func (k Keeper) getAllSwapInits(ctx sdk.Context) []types.SwapInit { - swapInits := make([]types.SwapInit, 0) - k.iterateAllSwapInits(ctx, func(swapInit types.SwapInit) bool { - swapInits = append(swapInits, swapInit) +func (k Keeper) getAllSwaps(ctx sdk.Context) []types.Swap { + idx := 0 + k.iterateAllSwaps(ctx, func(swap types.Swap) bool { + k.swaps[idx] = swap + idx++ return false }) - return swapInits + if idx == 0 { + return []types.Swap{} + } + return k.swaps } -func (k Keeper) iterateAllSwapInits(ctx sdk.Context, cb func(swapped types.SwapInit) (stop bool)) { +func (k Keeper) iterateAllSwaps(ctx sdk.Context, cb func(swapped types.Swap) (stop bool)) { store := ctx.KVStore(k.storeKey) - swapInitDataStore := prefix.NewStore(store, swapInitPrefix) + swapDataStore := prefix.NewStore(store, swapPrefix) - iterator := swapInitDataStore.Iterator(nil, nil) + iterator := swapDataStore.Iterator(nil, nil) defer iterator.Close() for ; iterator.Valid(); iterator.Next() { - swapInit := types.SwapInit{} - k.cdc.MustUnmarshal(iterator.Value(), &swapInit) - if cb(swapInit) { + swap := types.Swap{} + k.cdc.MustUnmarshal(iterator.Value(), &swap) + if cb(swap) { break } } } -func (k Keeper) getSwappableNewCoinAmount(ctx sdk.Context) (sdk.Coin, error) { - swapCap, err := k.swapCap(ctx) +func (k Keeper) getSwappableNewCoinAmount(ctx sdk.Context, fromDenom, toDenom string) (sdk.Coin, error) { + swap, err := k.getSwap(ctx, fromDenom, toDenom) if err != nil { return sdk.Coin{}, err } - swapped, err := k.getSwapped(ctx) - if err != nil { - return sdk.Coin{}, err - } - denom, err := k.toDenom(ctx) + + swapped, err := k.getSwapped(ctx, fromDenom, toDenom) if err != nil { return sdk.Coin{}, err } + swapCap := swap.AmountCapForToDenom remainingAmount := swapCap.Sub(swapped.GetToCoinAmount().Amount) - return sdk.NewCoin(denom, remainingAmount), nil + return sdk.NewCoin(toDenom, remainingAmount), nil } -func (k Keeper) fromDenom(ctx sdk.Context) (string, error) { - swapInit, err := k.swapInit(ctx) - if err != nil { - return "", err +func (k Keeper) getSwap(ctx sdk.Context, fromDenom, toDenom string) (types.Swap, error) { + store := ctx.KVStore(k.storeKey) + key := swapKey(fromDenom, toDenom) + bz := store.Get(key) + if bz == nil { + return types.Swap{}, sdkerrors.ErrNotFound.Wrap("swap not found") } - return swapInit.FromDenom, nil -} - -func (k Keeper) toDenom(ctx sdk.Context) (string, error) { - swapInit, err := k.swapInit(ctx) - if err != nil { - return "", err + swap := types.Swap{} + if err := k.cdc.Unmarshal(bz, &swap); err != nil { + return types.Swap{}, err } - return swapInit.GetToDenom(), nil + return swap, nil } -func (k Keeper) swapMultiple(ctx sdk.Context) (sdk.Int, error) { - swapInit, err := k.swapInit(ctx) +func (k Keeper) updateSwapped(ctx sdk.Context, fromAmount, toAmount sdk.Coin) error { + prevSwapped, err := k.getSwapped(ctx, fromAmount.Denom, toAmount.Denom) if err != nil { - return sdk.Int{}, err + return err } - return swapInit.SwapMultiple, nil -} + updatedSwapped := &types.Swapped{ + FromCoinAmount: fromAmount.Add(prevSwapped.FromCoinAmount), + ToCoinAmount: toAmount.Add(prevSwapped.ToCoinAmount), + } -func (k Keeper) swapCap(ctx sdk.Context) (sdk.Int, error) { - swapInit, err := k.swapInit(ctx) + key := swappedKey(fromAmount.Denom, toAmount.Denom) + bz, err := k.cdc.Marshal(updatedSwapped) if err != nil { - return sdk.Int{}, err + return err } - return swapInit.AmountCapForToDenom, nil + store := ctx.KVStore(k.storeKey) + store.Set(key, bz) + return nil } -func (k Keeper) swapInit(ctx sdk.Context) (types.SwapInit, error) { - swapInits := k.getAllSwapInits(ctx) - if len(swapInits) == 0 { - return types.SwapInit{}, types.ErrSwapNotInitialized +func (k Keeper) checkSwapCap(ctx sdk.Context, swap types.Swap, newCoinAmount sdk.Coin) error { + swapped, err := k.getSwapped(ctx, swap.GetFromDenom(), swap.GetToDenom()) + if err != nil { + return err } - return swapInits[0], nil + swapCap := swap.AmountCapForToDenom + if swapCap.LT(swapped.ToCoinAmount.Add(newCoinAmount).Amount) { + return types.ErrExceedSwappableToCoinAmount + } + return nil } -func (k Keeper) updateSwapped(ctx sdk.Context, fromAmount, toAmount sdk.Coin) error { - prevSwapped, err := k.getSwapped(ctx) +func (k Keeper) increaseSwapCount(ctx sdk.Context) error { + stats, err := k.getSwapStats(ctx) if err != nil { return err } - updatedSwapped := &types.Swapped{ - FromCoinAmount: fromAmount.Add(prevSwapped.FromCoinAmount), - ToCoinAmount: toAmount.Add(prevSwapped.ToCoinAmount), + prev := stats.SwapCount + stats.SwapCount += 1 + if stats.SwapCount < prev { + return types.ErrInvalidState.Wrap("overflow detected") } - store := ctx.KVStore(k.storeKey) - bz, err := k.cdc.Marshal(updatedSwapped) - if err != nil { + + if err := k.setSwapStats(ctx, stats); err != nil { return err } - - key := swappedKey(toAmount.Denom) - store.Set(key, bz) return nil } -func (k Keeper) checkSwapCap(ctx sdk.Context, newCoinAmount sdk.Coin) error { - swapped, err := k.getSwapped(ctx) +func (k Keeper) getSwapStats(ctx sdk.Context) (types.SwapStats, error) { + store := ctx.KVStore(k.storeKey) + bz := store.Get(swapStatsKey) + if bz == nil { + return types.SwapStats{}, nil + } + + stats := types.SwapStats{} + err := k.cdc.Unmarshal(bz, &stats) if err != nil { - return err + return types.SwapStats{}, err } + return stats, nil +} - swapCap, err := k.swapCap(ctx) +func (k Keeper) setSwapStats(ctx sdk.Context, stats types.SwapStats) error { + bz, err := k.cdc.Marshal(&stats) if err != nil { return err } - if swapCap.LT(swapped.ToCoinAmount.Add(newCoinAmount).Amount) { - return types.ErrExceedSwappableToCoinAmount - } + store := ctx.KVStore(k.storeKey) + store.Set(swapStatsKey, bz) return nil } - -func (k Keeper) hasBeenInitialized(ctx sdk.Context) bool { - inits := k.getAllSwapInits(ctx) - return len(inits) > 0 -} diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 9880b13e17..8457f227ad 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -30,7 +30,7 @@ type KeeperTestSuite struct { accWithToCoin sdk.AccAddress initBalance sdk.Int - swapInit types.SwapInit + swap types.Swap } func (s *KeeperTestSuite) createRandomAccounts(n int) []sdk.AccAddress { @@ -64,7 +64,7 @@ func (s *KeeperTestSuite) SetupTest() { numAcc := int64(2) s.initBalance = sdk.NewInt(123456789) - s.swapInit = types.SwapInit{ + s.swap = types.Swap{ FromDenom: "fromdenom", ToDenom: "todenom", AmountCapForToDenom: s.initBalance.Mul(sdk.NewInt(1000).Mul(sdk.NewInt(numAcc))), @@ -83,11 +83,11 @@ func (s *KeeperTestSuite) createAccountsWithInitBalance(app *simapp.SimApp) { *addresses[i] = address } minter := app.AccountKeeper.GetModuleAccount(s.ctx, minttypes.ModuleName).GetAddress() - fromAmount := sdk.NewCoins(sdk.NewCoin(s.swapInit.GetFromDenom(), s.initBalance)) + fromAmount := sdk.NewCoins(sdk.NewCoin(s.swap.GetFromDenom(), s.initBalance)) s.Require().NoError(app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, fromAmount)) s.Require().NoError(app.BankKeeper.SendCoins(s.ctx, minter, s.accWithFromCoin, fromAmount)) - toAmount := sdk.NewCoins(sdk.NewCoin(s.swapInit.GetToDenom(), s.initBalance)) + toAmount := sdk.NewCoins(sdk.NewCoin(s.swap.GetToDenom(), s.initBalance)) s.Require().NoError(app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, toAmount)) s.Require().NoError(app.BankKeeper.SendCoins(s.ctx, minter, s.accWithToCoin, toAmount)) } @@ -107,40 +107,40 @@ func (s *KeeperTestSuite) TestSwap() { }{ "swap some": { s.accWithFromCoin, - sdk.NewCoin(s.swapInit.GetFromDenom(), sdk.NewInt(100)), - s.swapInit.GetToDenom(), + sdk.NewCoin(s.swap.GetFromDenom(), sdk.NewInt(100)), + s.swap.GetToDenom(), sdk.NewInt(100), false, nil, }, "swap all the balance": { s.accWithFromCoin, - sdk.NewCoin(s.swapInit.GetFromDenom(), s.initBalance), - s.swapInit.GetToDenom(), + sdk.NewCoin(s.swap.GetFromDenom(), s.initBalance), + s.swap.GetToDenom(), s.initBalance, false, nil, }, "swap without holding enough balance": { s.accWithFromCoin, - sdk.NewCoin(s.swapInit.GetFromDenom(), sdk.OneInt().Add(s.initBalance)), - s.swapInit.GetToDenom(), + sdk.NewCoin(s.swap.GetFromDenom(), sdk.OneInt().Add(s.initBalance)), + s.swap.GetToDenom(), sdk.ZeroInt(), true, sdkerrors.ErrInsufficientFunds, }, "account holding new coin only": { s.accWithToCoin, - sdk.NewCoin(s.swapInit.GetFromDenom(), sdk.NewInt(100)), - s.swapInit.GetToDenom(), + sdk.NewCoin(s.swap.GetFromDenom(), sdk.NewInt(100)), + s.swap.GetToDenom(), sdk.ZeroInt(), true, sdkerrors.ErrInsufficientFunds, }, "swap with the same from-denom and to-denom": { s.accWithFromCoin, - sdk.NewCoin(s.swapInit.GetToDenom(), s.initBalance), - s.swapInit.GetToDenom(), + sdk.NewCoin(s.swap.GetToDenom(), s.initBalance), + s.swap.GetToDenom(), sdk.ZeroInt(), true, sdkerrors.ErrInvalidRequest, @@ -149,7 +149,7 @@ func (s *KeeperTestSuite) TestSwap() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.SwapInit(ctx, s.swapInit) + err := s.keeper.MakeSwap(ctx, s.swap) s.Require().NoError(err) err = s.keeper.Swap(ctx, tc.from, tc.amountToSwap, tc.toDenom) @@ -159,8 +159,8 @@ func (s *KeeperTestSuite) TestSwap() { } s.Require().NoError(err) - actualAmount := s.keeper.GetBalance(ctx, tc.from, s.swapInit.GetToDenom()).Amount - expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapInit.SwapMultiple) + actualAmount := s.keeper.GetBalance(ctx, tc.from, s.swap.GetToDenom()).Amount + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swap.SwapMultiple) s.Require().Equal(expectedAmount, actualAmount) }) } @@ -177,16 +177,16 @@ func (s *KeeperTestSuite) TestSwapAll() { }{ "account holding from coin": { s.accWithFromCoin, - s.swapInit.GetFromDenom(), - s.swapInit.GetToDenom(), + s.swap.GetFromDenom(), + s.swap.GetToDenom(), s.initBalance, false, nil, }, "account holding to coin only": { s.accWithToCoin, - s.swapInit.GetFromDenom(), - s.swapInit.GetToDenom(), + s.swap.GetFromDenom(), + s.swap.GetToDenom(), s.initBalance, true, sdkerrors.ErrInsufficientFunds, @@ -195,7 +195,7 @@ func (s *KeeperTestSuite) TestSwapAll() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.SwapInit(ctx, s.swapInit) + err := s.keeper.MakeSwap(ctx, s.swap) s.Require().NoError(err) err = s.keeper.SwapAll(ctx, tc.from, tc.fromDenom, tc.toDenom) @@ -205,8 +205,8 @@ func (s *KeeperTestSuite) TestSwapAll() { } s.Require().NoError(err) - actualAmount := s.keeper.GetBalance(ctx, tc.from, s.swapInit.GetToDenom()).Amount - expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapInit.SwapMultiple) + actualAmount := s.keeper.GetBalance(ctx, tc.from, s.swap.GetToDenom()).Amount + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swap.SwapMultiple) s.Require().Equal(expectedAmount, actualAmount) }) } @@ -214,12 +214,12 @@ func (s *KeeperTestSuite) TestSwapAll() { func (s *KeeperTestSuite) TestSwapInit() { testCases := map[string]struct { - req types.SwapInit + req types.Swap shouldThrowError bool expectedError error }{ - "valid swapInit": { - types.SwapInit{ + "valid getSwapInit": { + types.Swap{ FromDenom: "fromD", ToDenom: "toD", AmountCapForToDenom: sdk.OneInt(), @@ -229,7 +229,7 @@ func (s *KeeperTestSuite) TestSwapInit() { nil, }, "invalid empty from-denom": { - types.SwapInit{ + types.Swap{ FromDenom: "", ToDenom: "toD", AmountCapForToDenom: sdk.OneInt(), @@ -239,7 +239,7 @@ func (s *KeeperTestSuite) TestSwapInit() { sdkerrors.ErrInvalidRequest, }, "invalid empty to-denom": { - types.SwapInit{ + types.Swap{ FromDenom: "fromD", ToDenom: "", AmountCapForToDenom: sdk.OneInt(), @@ -249,7 +249,7 @@ func (s *KeeperTestSuite) TestSwapInit() { sdkerrors.ErrInvalidRequest, }, "invalid zero amount cap for to-denom": { - types.SwapInit{ + types.Swap{ FromDenom: "fromD", ToDenom: "toD", AmountCapForToDenom: sdk.ZeroInt(), @@ -259,7 +259,7 @@ func (s *KeeperTestSuite) TestSwapInit() { sdkerrors.ErrInvalidRequest, }, "invalid zero swap-rate": { - types.SwapInit{ + types.Swap{ FromDenom: "fromD", ToDenom: "toD", AmountCapForToDenom: sdk.OneInt(), @@ -269,7 +269,7 @@ func (s *KeeperTestSuite) TestSwapInit() { sdkerrors.ErrInvalidRequest, }, "invalid the same from-denom and to-denom": { - types.SwapInit{ + types.Swap{ FromDenom: "same", ToDenom: "same", AmountCapForToDenom: sdk.OneInt(), @@ -282,7 +282,7 @@ func (s *KeeperTestSuite) TestSwapInit() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.SwapInit(ctx, tc.req) + err := s.keeper.MakeSwap(ctx, tc.req) if tc.shouldThrowError { s.Require().ErrorIs(err, tc.expectedError) return diff --git a/x/fswap/keeper/keys.go b/x/fswap/keeper/keys.go index b3b652723f..04e36660f8 100644 --- a/x/fswap/keeper/keys.go +++ b/x/fswap/keeper/keys.go @@ -1,16 +1,19 @@ package keeper var ( - swapInitPrefix = []byte{0x01} - swappedKeyPrefix = []byte{0x02} + swapPrefix = []byte{0x01} + swapStatsKey = []byte{0x02} + swappedKeyPrefix = []byte{0x03} ) -// swapInitKey key(prefix + toDenom) -func swapInitKey(toDenom string) []byte { - return append(swapInitPrefix, toDenom...) +// swapKey key(prefix + fromDenom + toDenom) +func swapKey(fromDenom, toDenom string) []byte { + key := append(swapPrefix, fromDenom...) + return append(key, toDenom...) } -// swappedKey key(prefix + toDenom) -func swappedKey(toDenom string) []byte { - return append(swappedKeyPrefix, toDenom...) +// swappedKey key(prefix + fromDenom + toDenom) +func swappedKey(fromDenom, toDenom string) []byte { + key := append(swappedKeyPrefix, fromDenom...) + return append(key, toDenom...) } diff --git a/x/fswap/keeper/msg_server_test.go b/x/fswap/keeper/msg_server_test.go index dc0e4093f9..499ccff16f 100644 --- a/x/fswap/keeper/msg_server_test.go +++ b/x/fswap/keeper/msg_server_test.go @@ -16,8 +16,8 @@ func (s *KeeperTestSuite) TestMsgSwap() { "swap some": { &types.MsgSwap{ FromAddress: s.accWithFromCoin.String(), - FromCoinAmount: sdk.NewCoin(s.swapInit.GetFromDenom(), sdk.NewInt(100)), - ToDenom: s.swapInit.GetToDenom(), + FromCoinAmount: sdk.NewCoin(s.swap.GetFromDenom(), sdk.NewInt(100)), + ToDenom: s.swap.GetToDenom(), }, sdk.NewInt(100), false, @@ -26,8 +26,8 @@ func (s *KeeperTestSuite) TestMsgSwap() { "swap all the balance": { &types.MsgSwap{ FromAddress: s.accWithFromCoin.String(), - FromCoinAmount: sdk.NewCoin(s.swapInit.GetFromDenom(), s.initBalance), - ToDenom: s.swapInit.GetToDenom(), + FromCoinAmount: sdk.NewCoin(s.swap.GetFromDenom(), s.initBalance), + ToDenom: s.swap.GetToDenom(), }, s.initBalance, false, @@ -36,8 +36,8 @@ func (s *KeeperTestSuite) TestMsgSwap() { "account holding new coin only": { &types.MsgSwap{ FromAddress: s.accWithToCoin.String(), - FromCoinAmount: sdk.NewCoin(s.swapInit.GetFromDenom(), sdk.NewInt(100)), - ToDenom: s.swapInit.GetToDenom(), + FromCoinAmount: sdk.NewCoin(s.swap.GetFromDenom(), sdk.NewInt(100)), + ToDenom: s.swap.GetToDenom(), }, s.initBalance, true, @@ -47,7 +47,7 @@ func (s *KeeperTestSuite) TestMsgSwap() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.SwapInit(ctx, s.swapInit) + err := s.keeper.MakeSwap(ctx, s.swap) s.Require().NoError(err) swapResponse, err := s.msgServer.Swap(sdk.WrapSDKContext(ctx), tc.request) @@ -61,7 +61,7 @@ func (s *KeeperTestSuite) TestMsgSwap() { from, err := sdk.AccAddressFromBech32(tc.request.FromAddress) s.Require().NoError(err) actualAmount := s.keeper.GetBalance(ctx, from, tc.request.GetToDenom()).Amount - expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapInit.SwapMultiple) + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swap.SwapMultiple) s.Require().Equal(expectedAmount, actualAmount) }) } @@ -77,8 +77,8 @@ func (s *KeeperTestSuite) TestMsgSwapAll() { "swapAll": { &types.MsgSwapAll{ FromAddress: s.accWithFromCoin.String(), - FromDenom: s.swapInit.GetFromDenom(), - ToDenom: s.swapInit.GetToDenom(), + FromDenom: s.swap.GetFromDenom(), + ToDenom: s.swap.GetToDenom(), }, s.initBalance, false, @@ -87,8 +87,8 @@ func (s *KeeperTestSuite) TestMsgSwapAll() { "account holding new coin only": { &types.MsgSwapAll{ FromAddress: s.accWithToCoin.String(), - FromDenom: s.swapInit.GetFromDenom(), - ToDenom: s.swapInit.GetToDenom(), + FromDenom: s.swap.GetFromDenom(), + ToDenom: s.swap.GetToDenom(), }, s.initBalance, true, @@ -98,7 +98,7 @@ func (s *KeeperTestSuite) TestMsgSwapAll() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.SwapInit(ctx, s.swapInit) + err := s.keeper.MakeSwap(ctx, s.swap) s.Require().NoError(err) swapResponse, err := s.msgServer.SwapAll(sdk.WrapSDKContext(ctx), tc.request) @@ -112,7 +112,7 @@ func (s *KeeperTestSuite) TestMsgSwapAll() { from, err := sdk.AccAddressFromBech32(tc.request.FromAddress) s.Require().NoError(err) actualAmount := s.keeper.GetBalance(ctx, from, tc.request.GetToDenom()).Amount - expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swapInit.SwapMultiple) + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swap.SwapMultiple) s.Require().Equal(expectedAmount, actualAmount) }) } diff --git a/x/fswap/module.go b/x/fswap/module.go index 280ba8f091..9ee61a12cb 100644 --- a/x/fswap/module.go +++ b/x/fswap/module.go @@ -28,7 +28,7 @@ var ( // AppModuleBasic // ---------------------------------------------------------------------------- -// AppModuleBasic implements the AppModuleBasic interface for the capability module. +// AppModuleBasic implements the AppModuleBasic interface for the fswap module. type AppModuleBasic struct { cdc codec.BinaryCodec } @@ -37,7 +37,7 @@ func NewAppModuleBasic(cdc codec.BinaryCodec) AppModuleBasic { return AppModuleBasic{cdc: cdc} } -// Name returns the capability module's name. +// Name returns the fswap module's name. func (AppModuleBasic) Name() string { return types.ModuleName } @@ -55,12 +55,12 @@ func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) { types.RegisterInterfaces(reg) } -// DefaultGenesis returns the capability module's default genesis state. +// DefaultGenesis returns the fswap module's default genesis state. func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { return cdc.MustMarshalJSON(types.DefaultGenesis()) } -// ValidateGenesis performs genesis state validation for the capability module. +// ValidateGenesis performs genesis state validation for the fswap module. func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error { var genState types.GenesisState if err := cdc.UnmarshalJSON(bz, &genState); err != nil { @@ -74,12 +74,12 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *r // this line is used by starport scaffolding # 2 } -// GetTxCmd returns the capability module's root tx command. +// GetTxCmd returns the fswap module's root tx command. func (a AppModuleBasic) GetTxCmd() *cobra.Command { return cli.GetTxCmd() } -// GetQueryCmd returns the capability module's root query command. +// GetQueryCmd returns the fswap module's root query command. func (AppModuleBasic) GetQueryCmd() *cobra.Command { return cli.GetQueryCmd(types.StoreKey) } @@ -88,7 +88,7 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { // AppModule // ---------------------------------------------------------------------------- -// AppModule implements the AppModule interface for the capability module. +// AppModule implements the AppModule interface for the fswap module. type AppModule struct { AppModuleBasic @@ -111,20 +111,20 @@ func NewAppModule( } } -// Name returns the capability module's name. +// Name returns the fswap module's name. func (am AppModule) Name() string { return am.AppModuleBasic.Name() } -// Route returns the capability module's message routing key. +// Route returns the fswap module's message routing key. func (am AppModule) Route() sdk.Route { return sdk.NewRoute(types.RouterKey, NewHandler(am.keeper)) } -// QuerierRoute returns the capability module's query routing key. +// QuerierRoute returns the fswap module's query routing key. func (AppModule) QuerierRoute() string { return types.QuerierRoute } -// LegacyQuerierHandler returns the capability module's Querier. +// LegacyQuerierHandler returns the fswap module's Querier. func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier { return nil } @@ -136,10 +136,10 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(am.keeper)) } -// RegisterInvariants registers the capability module's invariants. +// RegisterInvariants registers the fswap module's invariants. func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} -// InitGenesis performs the capability module's genesis initialization It returns +// InitGenesis performs the fswap module's genesis initialization It returns // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) []abci.ValidatorUpdate { genState := types.GenesisState{} @@ -151,7 +151,7 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.Ra return []abci.ValidatorUpdate{} } -// ExportGenesis returns the capability module's exported genesis state as raw JSON bytes. +// ExportGenesis returns the fswap module's exported genesis state as raw JSON bytes. func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { genState := am.keeper.ExportGenesis(ctx) return cdc.MustMarshalJSON(genState) @@ -160,10 +160,10 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements ConsensusVersion. func (AppModule) ConsensusVersion() uint64 { return 1 } -// BeginBlock executes all ABCI BeginBlock logic respective to the capability module. +// BeginBlock executes all ABCI BeginBlock logic respective to the fswap module. func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} -// EndBlock executes all ABCI EndBlock logic respective to the capability module. It +// EndBlock executes all ABCI EndBlock logic respective to the fswap module. It // returns no validator updates. func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} diff --git a/x/fswap/proposal_handler_test.go b/x/fswap/proposal_handler_test.go index bd7598862a..2790520cae 100644 --- a/x/fswap/proposal_handler_test.go +++ b/x/fswap/proposal_handler_test.go @@ -6,12 +6,12 @@ import ( "github.com/Finschia/finschia-sdk/x/fswap/types" ) -func testProposal(swapInit types.SwapInit) *types.SwapInitProposal { - return types.NewSwapInitProposal("Test", "description", swapInit) +func testProposal(swapInit types.Swap) *types.SwapProposal { + return types.NewSwapProposal("Test", "description", swapInit) } func TestProposalHandlerPassed(t *testing.T) { - //app := simapp.Setup(false) + // app := simapp.Setup(false) //ctx := app.BaseApp.NewContext(false, tmproto.Header{}) // //fswapInit := types.NewSwapInit("aaa", "bbb", sdk.NewInt(1000), sdk.NewInt(100000)) diff --git a/x/fswap/types/codec.go b/x/fswap/types/codec.go index bcf91a9869..09ded16639 100644 --- a/x/fswap/types/codec.go +++ b/x/fswap/types/codec.go @@ -17,7 +17,7 @@ func RegisterCodec(cdc *codec.LegacyAmino) { } func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&SwapInitProposal{}, "finschia-sdk/FswapInitProposal", nil) + cdc.RegisterConcrete(&SwapProposal{}, "finschia-sdk/SwapProposal", nil) } func RegisterInterfaces(registry types.InterfaceRegistry) { @@ -26,7 +26,7 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) registry.RegisterImplementations( (*govtypes.Content)(nil), - &SwapInitProposal{}, + &SwapProposal{}, ) } diff --git a/x/fswap/types/config.go b/x/fswap/types/config.go new file mode 100644 index 0000000000..071a0cab1f --- /dev/null +++ b/x/fswap/types/config.go @@ -0,0 +1,13 @@ +package types + +type Config struct { + MaxSwaps int + UpdateAllowed bool +} + +func DefaultConfig() Config { + return Config{ + MaxSwaps: 1, + UpdateAllowed: false, + } +} diff --git a/x/fswap/types/errors.go b/x/fswap/types/errors.go index ba1fe96aff..ee4e52bf1a 100644 --- a/x/fswap/types/errors.go +++ b/x/fswap/types/errors.go @@ -8,8 +8,8 @@ import ( // x/fswap module sentinel errors var ( - ErrSwapNotInitialized = sdkerrors.Register(ModuleName, 1100, "swap not initialized") - ErrSwapCanNotBeInitializedTwice = sdkerrors.Register(ModuleName, 1101, "swap cannot be initialized twice") - ErrSwappedNotFound = sdkerrors.Register(ModuleName, 1102, "swapped does not exist") - ErrExceedSwappableToCoinAmount = sdkerrors.Register(ModuleName, 1103, "exceed swappable to-coin amount") + ErrInvalidState = sdkerrors.Register(ModuleName, 1100, "swap module invalid state") + ErrCanNotHaveMoreSwap = sdkerrors.Register(ModuleName, 1101, "no more swap allowed") + ErrSwappedNotFound = sdkerrors.Register(ModuleName, 1102, "swapped does not exist") + ErrExceedSwappableToCoinAmount = sdkerrors.Register(ModuleName, 1103, "exceed swappable to-coin amount") ) diff --git a/x/fswap/types/fswap.go b/x/fswap/types/fswap.go index 64f5133060..7692f09af3 100644 --- a/x/fswap/types/fswap.go +++ b/x/fswap/types/fswap.go @@ -7,38 +7,45 @@ import ( sdkerrors "github.com/Finschia/finschia-sdk/types/errors" ) -// NewSwapped creates a new Swapped instance -func NewSwapped( - oldCoinAmount sdk.Coin, - newCoinAmount sdk.Coin, -) Swapped { - return Swapped{ - FromCoinAmount: oldCoinAmount, - ToCoinAmount: newCoinAmount, +// ValidateBasic validates the set of Swap +func (s *Swap) ValidateBasic() error { + if s.FromDenom == "" { + return sdkerrors.ErrInvalidRequest.Wrap("from denomination cannot be empty") + } + if s.ToDenom == "" { + return sdkerrors.ErrInvalidRequest.Wrap("to denomination cannot be empty") } + if s.FromDenom == s.ToDenom { + return sdkerrors.ErrInvalidRequest.Wrap("from denomination cannot be equal to to denomination") + } + if s.AmountCapForToDenom.LT(sdk.OneInt()) { + return sdkerrors.ErrInvalidRequest.Wrap("amount cannot be less than one") + } + if s.SwapMultiple.LT(sdk.OneInt()) { + return sdkerrors.ErrInvalidRequest.Wrap("swap multiple cannot be less than one") + } + return nil } -// DefaultSwapped returns an initial Swapped object -func DefaultSwapped() Swapped { - return NewSwapped(sdk.Coin{}, sdk.Coin{}) +func (s *Swap) String() string { + out, _ := yaml.Marshal(s) + return string(out) } -func validateCoinAmount(i interface{}) error { - v, ok := i.(sdk.Coin) - if !ok { - return sdkerrors.ErrInvalidCoins.Wrapf("invalid coin amount: %T", i) - } - if v.IsNil() { - return sdkerrors.ErrInvalidCoins.Wrap("coin amount must be not nil") - } - if err := v.Validate(); err != nil { - return sdkerrors.ErrInvalidCoins.Wrap(err.Error()) +func (s *SwapStats) ValidateBasic() error { + if s.SwapCount < 0 { + return ErrInvalidState.Wrap("swap count cannot be negative") } return nil } -// Validate validates the set of Swapped -func (s *Swapped) Validate() error { +func (s *SwapStats) String() string { + out, _ := yaml.Marshal(s) + return string(out) +} + +// ValidateBasic validates the set of Swapped +func (s *Swapped) ValidateBasic() error { if err := validateCoinAmount(s.FromCoinAmount); err != nil { return err } @@ -48,50 +55,22 @@ func (s *Swapped) Validate() error { return nil } -// String implements the Stringer interface. -func (s *Swapped) String() string { - out, _ := yaml.Marshal(s) - return string(out) -} - -// ValidateBasic validates the set of SwapInit -func (s *SwapInit) ValidateBasic() error { - if s.FromDenom == "" { - return sdkerrors.ErrInvalidRequest.Wrap("from denomination cannot be empty") - } - if s.ToDenom == "" { - return sdkerrors.ErrInvalidRequest.Wrap("to denomination cannot be empty") - } - if s.FromDenom == s.ToDenom { - return sdkerrors.ErrInvalidRequest.Wrap("from denomination cannot be equal to to denomination") +func validateCoinAmount(i interface{}) error { + v, ok := i.(sdk.Coin) + if !ok { + return sdkerrors.ErrInvalidCoins.Wrapf("invalid coin amount: %T", i) } - if s.AmountCapForToDenom.LT(sdk.OneInt()) { - return sdkerrors.ErrInvalidRequest.Wrap("amount cannot be less than one") + if v.IsNil() { + return sdkerrors.ErrInvalidCoins.Wrap("coin amount must be not nil") } - if s.SwapMultiple.LT(sdk.OneInt()) { - return sdkerrors.ErrInvalidRequest.Wrap("swap multiple cannot be less than one") + if err := v.Validate(); err != nil { + return sdkerrors.ErrInvalidCoins.Wrap(err.Error()) } return nil } // String implements the Stringer interface. -func (s *SwapInit) String() string { +func (s *Swapped) String() string { out, _ := yaml.Marshal(s) return string(out) } - -func (s *SwapInit) IsEmpty() bool { - if s.FromDenom == "" { - return true - } - if s.ToDenom == "" { - return true - } - if s.AmountCapForToDenom.IsZero() { - return true - } - if s.SwapMultiple.IsZero() { - return true - } - return false -} diff --git a/x/fswap/types/fswap.pb.go b/x/fswap/types/fswap.pb.go index f406f4dc63..edf25acb93 100644 --- a/x/fswap/types/fswap.pb.go +++ b/x/fswap/types/fswap.pb.go @@ -25,24 +25,24 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package -type SwapInit struct { +type Swap struct { FromDenom string `protobuf:"bytes,1,opt,name=from_denom,json=fromDenom,proto3" json:"from_denom,omitempty"` ToDenom string `protobuf:"bytes,2,opt,name=to_denom,json=toDenom,proto3" json:"to_denom,omitempty"` AmountCapForToDenom github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,3,opt,name=amount_cap_for_to_denom,json=amountCapForToDenom,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"amount_cap_for_to_denom"` SwapMultiple github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,4,opt,name=swap_multiple,json=swapMultiple,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"swap_multiple"` } -func (m *SwapInit) Reset() { *m = SwapInit{} } -func (*SwapInit) ProtoMessage() {} -func (*SwapInit) Descriptor() ([]byte, []int) { +func (m *Swap) Reset() { *m = Swap{} } +func (*Swap) ProtoMessage() {} +func (*Swap) Descriptor() ([]byte, []int) { return fileDescriptor_42ca60eaf37a2b67, []int{0} } -func (m *SwapInit) XXX_Unmarshal(b []byte) error { +func (m *Swap) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) } -func (m *SwapInit) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { +func (m *Swap) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { if deterministic { - return xxx_messageInfo_SwapInit.Marshal(b, m, deterministic) + return xxx_messageInfo_Swap.Marshal(b, m, deterministic) } else { b = b[:cap(b)] n, err := m.MarshalToSizedBuffer(b) @@ -52,50 +52,93 @@ func (m *SwapInit) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { return b[:n], nil } } -func (m *SwapInit) XXX_Merge(src proto.Message) { - xxx_messageInfo_SwapInit.Merge(m, src) +func (m *Swap) XXX_Merge(src proto.Message) { + xxx_messageInfo_Swap.Merge(m, src) } -func (m *SwapInit) XXX_Size() int { +func (m *Swap) XXX_Size() int { return m.Size() } -func (m *SwapInit) XXX_DiscardUnknown() { - xxx_messageInfo_SwapInit.DiscardUnknown(m) +func (m *Swap) XXX_DiscardUnknown() { + xxx_messageInfo_Swap.DiscardUnknown(m) } -var xxx_messageInfo_SwapInit proto.InternalMessageInfo +var xxx_messageInfo_Swap proto.InternalMessageInfo -func (m *SwapInit) GetFromDenom() string { +func (m *Swap) GetFromDenom() string { if m != nil { return m.FromDenom } return "" } -func (m *SwapInit) GetToDenom() string { +func (m *Swap) GetToDenom() string { if m != nil { return m.ToDenom } return "" } -// From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 -type SwapInitProposal struct { - Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"` - Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` - SwapInit SwapInit `protobuf:"bytes,3,opt,name=swap_init,json=swapInit,proto3" json:"swap_init"` +type SwapStats struct { + SwapCount int32 `protobuf:"varint,1,opt,name=swap_count,json=swapCount,proto3" json:"swap_count,omitempty"` } -func (m *SwapInitProposal) Reset() { *m = SwapInitProposal{} } -func (*SwapInitProposal) ProtoMessage() {} -func (*SwapInitProposal) Descriptor() ([]byte, []int) { +func (m *SwapStats) Reset() { *m = SwapStats{} } +func (*SwapStats) ProtoMessage() {} +func (*SwapStats) Descriptor() ([]byte, []int) { return fileDescriptor_42ca60eaf37a2b67, []int{1} } -func (m *SwapInitProposal) XXX_Unmarshal(b []byte) error { +func (m *SwapStats) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *SwapStats) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_SwapStats.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 *SwapStats) XXX_Merge(src proto.Message) { + xxx_messageInfo_SwapStats.Merge(m, src) +} +func (m *SwapStats) XXX_Size() int { + return m.Size() +} +func (m *SwapStats) XXX_DiscardUnknown() { + xxx_messageInfo_SwapStats.DiscardUnknown(m) +} + +var xxx_messageInfo_SwapStats proto.InternalMessageInfo + +func (m *SwapStats) GetSwapCount() int32 { + if m != nil { + return m.SwapCount + } + return 0 +} + +// From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 +type SwapProposal struct { + Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"` + Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` + Swap Swap `protobuf:"bytes,3,opt,name=swap,proto3" json:"swap"` +} + +func (m *SwapProposal) Reset() { *m = SwapProposal{} } +func (*SwapProposal) ProtoMessage() {} +func (*SwapProposal) Descriptor() ([]byte, []int) { + return fileDescriptor_42ca60eaf37a2b67, []int{2} +} +func (m *SwapProposal) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) } -func (m *SwapInitProposal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { +func (m *SwapProposal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { if deterministic { - return xxx_messageInfo_SwapInitProposal.Marshal(b, m, deterministic) + return xxx_messageInfo_SwapProposal.Marshal(b, m, deterministic) } else { b = b[:cap(b)] n, err := m.MarshalToSizedBuffer(b) @@ -105,37 +148,37 @@ func (m *SwapInitProposal) XXX_Marshal(b []byte, deterministic bool) ([]byte, er return b[:n], nil } } -func (m *SwapInitProposal) XXX_Merge(src proto.Message) { - xxx_messageInfo_SwapInitProposal.Merge(m, src) +func (m *SwapProposal) XXX_Merge(src proto.Message) { + xxx_messageInfo_SwapProposal.Merge(m, src) } -func (m *SwapInitProposal) XXX_Size() int { +func (m *SwapProposal) XXX_Size() int { return m.Size() } -func (m *SwapInitProposal) XXX_DiscardUnknown() { - xxx_messageInfo_SwapInitProposal.DiscardUnknown(m) +func (m *SwapProposal) XXX_DiscardUnknown() { + xxx_messageInfo_SwapProposal.DiscardUnknown(m) } -var xxx_messageInfo_SwapInitProposal proto.InternalMessageInfo +var xxx_messageInfo_SwapProposal proto.InternalMessageInfo -func (m *SwapInitProposal) GetTitle() string { +func (m *SwapProposal) GetTitle() string { if m != nil { return m.Title } return "" } -func (m *SwapInitProposal) GetDescription() string { +func (m *SwapProposal) GetDescription() string { if m != nil { return m.Description } return "" } -func (m *SwapInitProposal) GetSwapInit() SwapInit { +func (m *SwapProposal) GetSwap() Swap { if m != nil { - return m.SwapInit + return m.Swap } - return SwapInit{} + return Swap{} } type Swapped struct { @@ -146,7 +189,7 @@ type Swapped struct { func (m *Swapped) Reset() { *m = Swapped{} } func (*Swapped) ProtoMessage() {} func (*Swapped) Descriptor() ([]byte, []int) { - return fileDescriptor_42ca60eaf37a2b67, []int{2} + return fileDescriptor_42ca60eaf37a2b67, []int{3} } func (m *Swapped) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -190,55 +233,57 @@ func (m *Swapped) GetToCoinAmount() types.Coin { } func init() { - proto.RegisterType((*SwapInit)(nil), "lbm.fswap.v1.SwapInit") - proto.RegisterType((*SwapInitProposal)(nil), "lbm.fswap.v1.SwapInitProposal") + proto.RegisterType((*Swap)(nil), "lbm.fswap.v1.Swap") + proto.RegisterType((*SwapStats)(nil), "lbm.fswap.v1.SwapStats") + proto.RegisterType((*SwapProposal)(nil), "lbm.fswap.v1.SwapProposal") proto.RegisterType((*Swapped)(nil), "lbm.fswap.v1.Swapped") } func init() { proto.RegisterFile("lbm/fswap/v1/fswap.proto", fileDescriptor_42ca60eaf37a2b67) } var fileDescriptor_42ca60eaf37a2b67 = []byte{ - // 465 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 0x3d, 0x6f, 0x13, 0x41, - 0x10, 0xf5, 0x19, 0x43, 0xec, 0xb5, 0x89, 0xa2, 0x23, 0x02, 0x27, 0x12, 0x77, 0x51, 0x2a, 0x04, - 0x62, 0x57, 0x76, 0x2a, 0xd2, 0xe1, 0xa0, 0x88, 0x14, 0x48, 0xc8, 0x20, 0x21, 0xd1, 0x9c, 0xf6, - 0xce, 0x67, 0x7b, 0xc5, 0xdd, 0xce, 0xea, 0x76, 0xec, 0x84, 0x5f, 0x01, 0x74, 0x94, 0xa9, 0xf9, - 0x25, 0x29, 0x53, 0x22, 0x8a, 0x80, 0xec, 0x86, 0x7f, 0x01, 0xda, 0x8f, 0x80, 0xa9, 0x40, 0x48, - 0xe9, 0x66, 0xe7, 0xcd, 0xbd, 0x37, 0xfb, 0xde, 0x2d, 0xe9, 0x16, 0x69, 0xc9, 0xc6, 0xfa, 0x98, - 0x2b, 0x36, 0xef, 0xb9, 0x82, 0xaa, 0x0a, 0x10, 0xc2, 0x4e, 0x91, 0x96, 0xd4, 0x35, 0xe6, 0xbd, - 0xed, 0xcd, 0x09, 0x4c, 0xc0, 0x02, 0xcc, 0x54, 0x6e, 0x66, 0x3b, 0xca, 0x40, 0x97, 0xa0, 0x59, - 0xca, 0x75, 0xce, 0xe6, 0xbd, 0x34, 0x47, 0xde, 0x63, 0x19, 0x08, 0xe9, 0xf0, 0xdd, 0x0f, 0x75, - 0xd2, 0x7c, 0x71, 0xcc, 0xd5, 0x91, 0x14, 0x18, 0xde, 0x25, 0x64, 0x5c, 0x41, 0x99, 0x8c, 0x72, - 0x09, 0x65, 0x37, 0xd8, 0x09, 0xee, 0xb5, 0x86, 0x2d, 0xd3, 0x79, 0x62, 0x1a, 0xe1, 0x16, 0x69, - 0x22, 0x78, 0xb0, 0x6e, 0xc1, 0x35, 0x04, 0x07, 0x4d, 0xc9, 0x1d, 0x5e, 0xc2, 0x4c, 0x62, 0x92, - 0x71, 0x95, 0x8c, 0xa1, 0x4a, 0x7e, 0x4d, 0x5e, 0x33, 0x93, 0x83, 0xfe, 0xd9, 0x45, 0x5c, 0xfb, - 0x72, 0x11, 0xdf, 0x9f, 0x08, 0x9c, 0xce, 0x52, 0x9a, 0x41, 0xc9, 0x0e, 0x85, 0xd4, 0xd9, 0x54, - 0x70, 0x36, 0xf6, 0xc5, 0x43, 0x3d, 0x7a, 0xc3, 0xf0, 0xad, 0xca, 0x35, 0x3d, 0x92, 0x38, 0xbc, - 0xe5, 0x28, 0x0f, 0xb8, 0x3a, 0x84, 0xea, 0xa5, 0x57, 0x7a, 0x45, 0x6e, 0x9a, 0x1b, 0x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, 0x65, 0x54, 0x7b, 0x4d, 0xff, 0x4a, 0x7d, 0xe2, 0x9f, 0x8f, 0x95, 0x48, 0x6f, 0xd8, - 0x1f, 0x7f, 0xef, 0x67, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x63, 0xad, 0x91, 0x58, 0x03, 0x00, - 0x00, + // 483 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 0x4f, 0x6f, 0xd3, 0x4e, + 0x10, 0xb5, 0xf3, 0x73, 0x7f, 0x6d, 0x36, 0xa1, 0x42, 0x4b, 0x25, 0xd2, 0x4a, 0xd8, 0x55, 0x4f, + 0x88, 0x3f, 0xbb, 0x4a, 0xca, 0xa9, 0x37, 0x12, 0x54, 0xc1, 0x01, 0x09, 0xb9, 0x48, 0x48, 0x5c, + 0xac, 0xb5, 0xe3, 0x24, 0x2b, 0x6c, 0xcf, 0xca, 0xbb, 0x49, 0xcb, 0x85, 0x6f, 0x80, 0xc4, 0x91, + 0x63, 0xcf, 0x7c, 0x92, 0x1e, 0x7b, 0x44, 0x1c, 0x0a, 0x4a, 0x2e, 0x7c, 0x0b, 0xd0, 0xec, 0x5a, + 0x10, 0x4e, 0x20, 0x24, 0x6e, 0xe3, 0xf7, 0x66, 0xdf, 0xf3, 0xbe, 0x99, 0x25, 0xbd, 0x22, 0x2d, + 0xf9, 0x44, 0x9f, 0x0a, 0xc5, 0x17, 0x7d, 0x57, 0x30, 0x55, 0x83, 0x01, 0xda, 0x2d, 0xd2, 0x92, + 0x39, 0x60, 0xd1, 0xdf, 0xdb, 0x99, 0xc2, 0x14, 0x2c, 0xc1, 0xb1, 0x72, 0x3d, 0x7b, 0x61, 0x06, + 0xba, 0x04, 0xcd, 0x53, 0xa1, 0x73, 0xbe, 0xe8, 0xa7, 0xb9, 0x11, 0x7d, 0x9e, 0x81, 0xac, 0x1c, + 0x7f, 0xf0, 0xb6, 0x45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b, 0xac, 0x96, 0xca, 0x48, 0xa8, + 0x9a, 0x18, 0xd7, 0x21, 0x7a, 0x8f, 0x04, 0x28, 0x6f, 0x73, 0xeb, 0x0c, 0x28, 0x5b, 0x1f, 0x30, + 0x43, 0x87, 0x61, 0x80, 0x77, 0x8d, 0x6d, 0xd7, 0x9a, 0xff, 0x37, 0x9f, 0x6c, 0x22, 0xad, 0xf2, + 0x31, 0x3d, 0x23, 0xd7, 0xed, 0x20, 0x71, 0xc8, 0x89, 0x4b, 0xd1, 0xfe, 0x46, 0x67, 0xb0, 0xcb, + 0xdc, 0x32, 0x30, 0x5c, 0x06, 0xd6, 0x2c, 0x03, 0x1b, 0x81, 0xac, 0x86, 0x87, 0x28, 0xfb, 0xe1, + 0x73, 0x74, 0xf7, 0x0f, 0x23, 0xc4, 0x43, 0xf1, 0x36, 0xfa, 0x60, 0xf5, 0xd0, 0xba, 0x50, 0x43, + 0xb6, 0x0d, 0xfc, 0xe2, 0xdb, 0xfa, 0x27, 0xbe, 0x5d, 0x03, 0x3f, 0x5d, 0x8f, 0x02, 0x4c, 0x61, + 0xf8, 0xf8, 0x62, 0x19, 0xfa, 0x97, 0xcb, 0xd0, 0xff, 0xb2, 0x0c, 0xfd, 0x77, 0xab, 0xd0, 0xbb, + 0x5c, 0x85, 0xde, 0xc7, 0x55, 0xe8, 0xbd, 0x64, 0xbf, 0x95, 0x3e, 0x6b, 0x9e, 0x97, 0xb5, 0x48, + 0xff, 0xb7, 0x0f, 0xe3, 0xf0, 0x7b, 0x00, 0x00, 0x00, 0xff, 0xff, 0x9a, 0x24, 0xaa, 0x02, 0x78, + 0x03, 0x00, 0x00, } -func (this *SwapInit) Equal(that interface{}) bool { +func (this *Swap) Equal(that interface{}) bool { if that == nil { return this == nil } - that1, ok := that.(*SwapInit) + that1, ok := that.(*Swap) if !ok { - that2, ok := that.(SwapInit) + that2, ok := that.(Swap) if ok { that1 = &that2 } else { @@ -264,14 +309,38 @@ func (this *SwapInit) Equal(that interface{}) bool { } return true } -func (this *SwapInitProposal) Equal(that interface{}) bool { +func (this *SwapStats) Equal(that interface{}) bool { if that == nil { return this == nil } - that1, ok := that.(*SwapInitProposal) + that1, ok := that.(*SwapStats) if !ok { - that2, ok := that.(SwapInitProposal) + that2, ok := that.(SwapStats) + if ok { + that1 = &that2 + } else { + return false + } + } + if that1 == nil { + return this == nil + } else if this == nil { + return false + } + if this.SwapCount != that1.SwapCount { + return false + } + return true +} +func (this *SwapProposal) Equal(that interface{}) bool { + if that == nil { + return this == nil + } + + that1, ok := that.(*SwapProposal) + if !ok { + that2, ok := that.(SwapProposal) if ok { that1 = &that2 } else { @@ -289,12 +358,12 @@ func (this *SwapInitProposal) Equal(that interface{}) bool { if this.Description != that1.Description { return false } - if !this.SwapInit.Equal(&that1.SwapInit) { + if !this.Swap.Equal(&that1.Swap) { return false } return true } -func (m *SwapInit) Marshal() (dAtA []byte, err error) { +func (m *Swap) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) n, err := m.MarshalToSizedBuffer(dAtA[:size]) @@ -304,12 +373,12 @@ func (m *SwapInit) Marshal() (dAtA []byte, err error) { return dAtA[:n], nil } -func (m *SwapInit) MarshalTo(dAtA []byte) (int, error) { +func (m *Swap) MarshalTo(dAtA []byte) (int, error) { size := m.Size() return m.MarshalToSizedBuffer(dAtA[:size]) } -func (m *SwapInit) MarshalToSizedBuffer(dAtA []byte) (int, error) { +func (m *Swap) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) _ = i var l int @@ -351,7 +420,35 @@ func (m *SwapInit) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } -func (m *SwapInitProposal) Marshal() (dAtA []byte, err error) { +func (m *SwapStats) 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 *SwapStats) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *SwapStats) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.SwapCount != 0 { + i = encodeVarintFswap(dAtA, i, uint64(m.SwapCount)) + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil +} + +func (m *SwapProposal) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) n, err := m.MarshalToSizedBuffer(dAtA[:size]) @@ -361,18 +458,18 @@ func (m *SwapInitProposal) Marshal() (dAtA []byte, err error) { return dAtA[:n], nil } -func (m *SwapInitProposal) MarshalTo(dAtA []byte) (int, error) { +func (m *SwapProposal) MarshalTo(dAtA []byte) (int, error) { size := m.Size() return m.MarshalToSizedBuffer(dAtA[:size]) } -func (m *SwapInitProposal) MarshalToSizedBuffer(dAtA []byte) (int, error) { +func (m *SwapProposal) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) _ = i var l int _ = l { - size, err := m.SwapInit.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.Swap.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -452,7 +549,7 @@ func encodeVarintFswap(dAtA []byte, offset int, v uint64) int { dAtA[offset] = uint8(v) return base } -func (m *SwapInit) Size() (n int) { +func (m *Swap) Size() (n int) { if m == nil { return 0 } @@ -473,7 +570,19 @@ func (m *SwapInit) Size() (n int) { return n } -func (m *SwapInitProposal) Size() (n int) { +func (m *SwapStats) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.SwapCount != 0 { + n += 1 + sovFswap(uint64(m.SwapCount)) + } + return n +} + +func (m *SwapProposal) Size() (n int) { if m == nil { return 0 } @@ -487,7 +596,7 @@ func (m *SwapInitProposal) Size() (n int) { if l > 0 { n += 1 + l + sovFswap(uint64(l)) } - l = m.SwapInit.Size() + l = m.Swap.Size() n += 1 + l + sovFswap(uint64(l)) return n } @@ -511,7 +620,7 @@ func sovFswap(x uint64) (n int) { func sozFswap(x uint64) (n int) { return sovFswap(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } -func (m *SwapInit) Unmarshal(dAtA []byte) error { +func (m *Swap) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { @@ -534,10 +643,10 @@ func (m *SwapInit) Unmarshal(dAtA []byte) error { fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { - return fmt.Errorf("proto: SwapInit: wiretype end group for non-group") + return fmt.Errorf("proto: Swap: wiretype end group for non-group") } if fieldNum <= 0 { - return fmt.Errorf("proto: SwapInit: illegal tag %d (wire type %d)", fieldNum, wire) + return fmt.Errorf("proto: Swap: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: @@ -693,7 +802,76 @@ func (m *SwapInit) Unmarshal(dAtA []byte) error { } return nil } -func (m *SwapInitProposal) Unmarshal(dAtA []byte) error { +func (m *SwapStats) 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 ErrIntOverflowFswap + } + 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: SwapStats: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: SwapStats: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field SwapCount", wireType) + } + m.SwapCount = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFswap + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.SwapCount |= int32(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := skipFswap(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthFswap + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *SwapProposal) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { @@ -716,10 +894,10 @@ func (m *SwapInitProposal) Unmarshal(dAtA []byte) error { fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { - return fmt.Errorf("proto: SwapInitProposal: wiretype end group for non-group") + return fmt.Errorf("proto: SwapProposal: wiretype end group for non-group") } if fieldNum <= 0 { - return fmt.Errorf("proto: SwapInitProposal: illegal tag %d (wire type %d)", fieldNum, wire) + return fmt.Errorf("proto: SwapProposal: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: @@ -788,7 +966,7 @@ func (m *SwapInitProposal) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 3: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field SwapInit", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field Swap", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -815,7 +993,7 @@ func (m *SwapInitProposal) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.SwapInit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.Swap.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/fswap/types/genesis.go b/x/fswap/types/genesis.go index 624a54ce5d..2c2d4c2ab0 100644 --- a/x/fswap/types/genesis.go +++ b/x/fswap/types/genesis.go @@ -1,41 +1,30 @@ package types -import ( - sdkerrors "github.com/Finschia/finschia-sdk/types/errors" -) - // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { return &GenesisState{ - SwapInit: []SwapInit{}, - Swapped: []Swapped{}, + Swaps: []Swap{}, + SwapStats: SwapStats{}, + Swappeds: []Swapped{}, } } -// Validate performs basic genesis state validation returning an error upon any -// failure. -// need confirm: should we validate? Since it may nil -func (gs GenesisState) Validate() error { - if len(gs.GetSwapInit()) == 0 && len(gs.GetSwapped()) == 0 { - return nil - } - if len(gs.GetSwapInit()) > 1 { - return sdkerrors.Wrap(ErrSwapCanNotBeInitializedTwice, "cannot have more than one swapInit") - } - if len(gs.GetSwapped()) > 1 { - return sdkerrors.Wrap(ErrSwapCanNotBeInitializedTwice, "cannot have more than one swapped") +// Validate performs basic genesis state validation returning an error upon any failure. +func (gs *GenesisState) Validate() error { + for _, swap := range gs.GetSwaps() { + if err := swap.ValidateBasic(); err != nil { + return err + } } - swapInit := gs.GetSwapInit()[0] - if err := swapInit.ValidateBasic(); err != nil { - return err - } - swapped := gs.GetSwapped()[0] - if err := swapped.Validate(); err != nil { + + if err := gs.SwapStats.ValidateBasic(); err != nil { return err } - if swapInit.AmountCapForToDenom.LT(swapped.GetToCoinAmount().Amount) { - return ErrExceedSwappableToCoinAmount + for _, swapped := range gs.GetSwappeds() { + if err := swapped.ValidateBasic(); err != nil { + return err + } } return nil } diff --git a/x/fswap/types/genesis.pb.go b/x/fswap/types/genesis.pb.go index 9846807217..89415e9484 100644 --- a/x/fswap/types/genesis.pb.go +++ b/x/fswap/types/genesis.pb.go @@ -25,8 +25,9 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // GenesisState defines the fswap module's genesis state. type GenesisState struct { - SwapInit []SwapInit `protobuf:"bytes,1,rep,name=swap_init,json=swapInit,proto3" json:"swap_init"` - Swapped []Swapped `protobuf:"bytes,2,rep,name=swapped,proto3" json:"swapped"` + Swaps []Swap `protobuf:"bytes,1,rep,name=swaps,proto3" json:"swaps"` + SwapStats SwapStats `protobuf:"bytes,2,opt,name=swap_stats,json=swapStats,proto3" json:"swap_stats"` + Swappeds []Swapped `protobuf:"bytes,3,rep,name=swappeds,proto3" json:"swappeds"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -62,16 +63,23 @@ func (m *GenesisState) XXX_DiscardUnknown() { var xxx_messageInfo_GenesisState proto.InternalMessageInfo -func (m *GenesisState) GetSwapInit() []SwapInit { +func (m *GenesisState) GetSwaps() []Swap { if m != nil { - return m.SwapInit + return m.Swaps } return nil } -func (m *GenesisState) GetSwapped() []Swapped { +func (m *GenesisState) GetSwapStats() SwapStats { if m != nil { - return m.Swapped + return m.SwapStats + } + return SwapStats{} +} + +func (m *GenesisState) GetSwappeds() []Swapped { + if m != nil { + return m.Swappeds } return nil } @@ -83,22 +91,23 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/genesis.proto", fileDescriptor_94e309cb1db27661) } var fileDescriptor_94e309cb1db27661 = []byte{ - // 232 bytes of a gzipped FileDescriptorProto + // 255 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xca, 0x49, 0xca, 0xd5, 0x4f, 0x2b, 0x2e, 0x4f, 0x2c, 0xd0, 0x2f, 0x33, 0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0xc9, 0x49, 0xca, 0xd5, 0x03, 0xcb, 0xe9, 0x95, 0x19, 0x4a, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0x25, 0xf4, 0x41, 0x2c, 0x88, 0x1a, 0x29, 0x09, - 0x14, 0xfd, 0x10, 0xc5, 0x60, 0x19, 0xa5, 0x06, 0x46, 0x2e, 0x1e, 0x77, 0x88, 0x79, 0xc1, 0x25, - 0x89, 0x25, 0xa9, 0x42, 0x96, 0x5c, 0x9c, 0x20, 0xe9, 0xf8, 0xcc, 0xbc, 0xcc, 0x12, 0x09, 0x46, - 0x05, 0x66, 0x0d, 0x6e, 0x23, 0x31, 0x3d, 0x64, 0x2b, 0xf4, 0x82, 0xcb, 0x13, 0x0b, 0x3c, 0xf3, - 0x32, 0x4b, 0x9c, 0x58, 0x4e, 0xdc, 0x93, 0x67, 0x08, 0xe2, 0x28, 0x86, 0xf2, 0x85, 0x4c, 0xb9, - 0xd8, 0x41, 0xec, 0x82, 0xd4, 0x14, 0x09, 0x26, 0xb0, 0x46, 0x51, 0x4c, 0x8d, 0x05, 0xa9, 0x29, - 0x50, 0x7d, 0x30, 0xb5, 0x4e, 0x1e, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, - 0x91, 0x1c, 0xe3, 0x84, 0xc7, 0x72, 0x0c, 0x17, 0x1e, 0xcb, 0x31, 0xdc, 0x78, 0x2c, 0xc7, 0x10, - 0xa5, 0x97, 0x9e, 0x59, 0x92, 0x51, 0x9a, 0xa4, 0x97, 0x9c, 0x9f, 0xab, 0xef, 0x96, 0x99, 0x57, - 0x9c, 0x9c, 0x91, 0x99, 0xa8, 0x9f, 0x06, 0x65, 0xe8, 0x16, 0xa7, 0x64, 0xeb, 0x57, 0x40, 0x7d, - 0x55, 0x52, 0x59, 0x90, 0x5a, 0x9c, 0xc4, 0x06, 0xf6, 0x93, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, - 0x23, 0x10, 0xb5, 0x38, 0x2f, 0x01, 0x00, 0x00, + 0x14, 0xfd, 0x10, 0xc5, 0x60, 0x19, 0xa5, 0xad, 0x8c, 0x5c, 0x3c, 0xee, 0x10, 0xf3, 0x82, 0x4b, + 0x12, 0x4b, 0x52, 0x85, 0xf4, 0xb8, 0x58, 0x41, 0xd2, 0xc5, 0x12, 0x8c, 0x0a, 0xcc, 0x1a, 0xdc, + 0x46, 0x42, 0x7a, 0xc8, 0xc6, 0xeb, 0x05, 0x97, 0x27, 0x16, 0x38, 0xb1, 0x9c, 0xb8, 0x27, 0xcf, + 0x10, 0x04, 0x51, 0x26, 0x64, 0xc3, 0xc5, 0x05, 0x62, 0xc4, 0x17, 0x97, 0x24, 0x96, 0x14, 0x4b, + 0x30, 0x29, 0x30, 0x6a, 0x70, 0x1b, 0x89, 0x63, 0x6a, 0x02, 0x19, 0x5e, 0x0c, 0xd5, 0xc9, 0x59, + 0x0c, 0x13, 0x10, 0x32, 0xe7, 0xe2, 0x00, 0x71, 0x0a, 0x52, 0x53, 0x8a, 0x25, 0x98, 0xc1, 0x16, + 0x8a, 0x62, 0xea, 0x2d, 0x48, 0x4d, 0x81, 0xea, 0x84, 0x2b, 0x76, 0xf2, 0x38, 0xf1, 0x48, 0x8e, + 0xf1, 0xc2, 0x23, 0x39, 0xc6, 0x07, 0x8f, 0xe4, 0x18, 0x27, 0x3c, 0x96, 0x63, 0xb8, 0xf0, 0x58, + 0x8e, 0xe1, 0xc6, 0x63, 0x39, 0x86, 0x28, 0xbd, 0xf4, 0xcc, 0x92, 0x8c, 0xd2, 0x24, 0xbd, 0xe4, + 0xfc, 0x5c, 0x7d, 0xb7, 0xcc, 0xbc, 0xe2, 0xe4, 0x8c, 0xcc, 0x44, 0xfd, 0x34, 0x28, 0x43, 0xb7, + 0x38, 0x25, 0x5b, 0xbf, 0x02, 0x1a, 0x14, 0x25, 0x95, 0x05, 0xa9, 0xc5, 0x49, 0x6c, 0xe0, 0x80, + 0x30, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x4f, 0x1a, 0xa2, 0xb3, 0x64, 0x01, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -121,10 +130,10 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - if len(m.Swapped) > 0 { - for iNdEx := len(m.Swapped) - 1; iNdEx >= 0; iNdEx-- { + if len(m.Swappeds) > 0 { + for iNdEx := len(m.Swappeds) - 1; iNdEx >= 0; iNdEx-- { { - size, err := m.Swapped[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + size, err := m.Swappeds[iNdEx].MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -132,13 +141,23 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { i = encodeVarintGenesis(dAtA, i, uint64(size)) } i-- - dAtA[i] = 0x12 + dAtA[i] = 0x1a + } + } + { + size, err := m.SwapStats.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) } - if len(m.SwapInit) > 0 { - for iNdEx := len(m.SwapInit) - 1; iNdEx >= 0; iNdEx-- { + i-- + dAtA[i] = 0x12 + if len(m.Swaps) > 0 { + for iNdEx := len(m.Swaps) - 1; iNdEx >= 0; iNdEx-- { { - size, err := m.SwapInit[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + size, err := m.Swaps[iNdEx].MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -169,14 +188,16 @@ func (m *GenesisState) Size() (n int) { } var l int _ = l - if len(m.SwapInit) > 0 { - for _, e := range m.SwapInit { + if len(m.Swaps) > 0 { + for _, e := range m.Swaps { l = e.Size() n += 1 + l + sovGenesis(uint64(l)) } } - if len(m.Swapped) > 0 { - for _, e := range m.Swapped { + l = m.SwapStats.Size() + n += 1 + l + sovGenesis(uint64(l)) + if len(m.Swappeds) > 0 { + for _, e := range m.Swappeds { l = e.Size() n += 1 + l + sovGenesis(uint64(l)) } @@ -221,7 +242,7 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field SwapInit", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field Swaps", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -248,14 +269,47 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.SwapInit = append(m.SwapInit, SwapInit{}) - if err := m.SwapInit[len(m.SwapInit)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + m.Swaps = append(m.Swaps, Swap{}) + if err := m.Swaps[len(m.Swaps)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Swapped", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field SwapStats", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.SwapStats.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Swappeds", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -282,8 +336,8 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Swapped = append(m.Swapped, Swapped{}) - if err := m.Swapped[len(m.Swapped)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + m.Swappeds = append(m.Swappeds, Swapped{}) + if err := m.Swappeds[len(m.Swappeds)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/fswap/types/proposal.go b/x/fswap/types/proposal.go index c90c190d36..302d7e98c2 100644 --- a/x/fswap/types/proposal.go +++ b/x/fswap/types/proposal.go @@ -7,38 +7,38 @@ import ( ) const ( - ProposalTypeSwapInit string = "SwapInit" + ProposalTypeSwap string = "Swap" ) -// NewSwapInitProposal creates a new SwapInitProposal instance. +// NewSwapProposal creates a new SwapProposal instance. // Deprecated: this proposal is considered legacy and is deprecated in favor of -// Msg-based gov proposals. See MsgSwapInit. -func NewSwapInitProposal(title, description string, swapInit SwapInit) *SwapInitProposal { - return &SwapInitProposal{title, description, swapInit} +// Msg-based gov proposals. See MsgSwap. +func NewSwapProposal(title, description string, swap Swap) *SwapProposal { + return &SwapProposal{title, description, swap} } // Implements Proposal Interface -var _ gov.Content = &SwapInitProposal{} +var _ gov.Content = &SwapProposal{} func init() { - gov.RegisterProposalType(ProposalTypeSwapInit) + gov.RegisterProposalType(ProposalTypeSwap) } // ProposalRoute gets the proposal's router key -func (m *SwapInitProposal) ProposalRoute() string { return RouterKey } +func (m *SwapProposal) ProposalRoute() string { return RouterKey } -// ProposalType is "SwapInit" -func (m *SwapInitProposal) ProposalType() string { return ProposalTypeSwapInit } +// ProposalType is "Swap" +func (m *SwapProposal) ProposalType() string { return ProposalTypeSwap } // String implements the Stringer interface. -func (m *SwapInitProposal) String() string { +func (m *SwapProposal) String() string { out, _ := yaml.Marshal(m) return string(out) } // ValidateBasic validates the proposal -func (m *SwapInitProposal) ValidateBasic() error { - if err := m.SwapInit.ValidateBasic(); err != nil { +func (m *SwapProposal) ValidateBasic() error { + if err := m.Swap.ValidateBasic(); err != nil { return err } return gov.ValidateAbstract(m) diff --git a/x/fswap/types/query.pb.go b/x/fswap/types/query.pb.go index 4a13541bf4..286ad93a32 100644 --- a/x/fswap/types/query.pb.go +++ b/x/fswap/types/query.pb.go @@ -31,6 +31,8 @@ var _ = math.Inf const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type QuerySwappedRequest struct { + FromDenom string `protobuf:"bytes,1,opt,name=fromDenom,proto3" json:"fromDenom,omitempty"` + ToDenom string `protobuf:"bytes,2,opt,name=toDenom,proto3" json:"toDenom,omitempty"` } func (m *QuerySwappedRequest) Reset() { *m = QuerySwappedRequest{} } @@ -66,6 +68,20 @@ func (m *QuerySwappedRequest) XXX_DiscardUnknown() { var xxx_messageInfo_QuerySwappedRequest proto.InternalMessageInfo +func (m *QuerySwappedRequest) GetFromDenom() string { + if m != nil { + return m.FromDenom + } + return "" +} + +func (m *QuerySwappedRequest) GetToDenom() string { + if m != nil { + return m.ToDenom + } + return "" +} + type QuerySwappedResponse struct { FromCoinAmount types.Coin `protobuf:"bytes,1,opt,name=from_coin_amount,json=fromCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"from_coin_amount"` ToCoinAmount types.Coin `protobuf:"bytes,2,opt,name=to_coin_amount,json=toCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"to_coin_amount"` @@ -119,6 +135,8 @@ func (m *QuerySwappedResponse) GetToCoinAmount() types.Coin { } type QueryTotalSwappableToCoinAmountRequest struct { + FromDenom string `protobuf:"bytes,1,opt,name=fromDenom,proto3" json:"fromDenom,omitempty"` + ToDenom string `protobuf:"bytes,2,opt,name=toDenom,proto3" json:"toDenom,omitempty"` } func (m *QueryTotalSwappableToCoinAmountRequest) Reset() { @@ -156,6 +174,20 @@ func (m *QueryTotalSwappableToCoinAmountRequest) XXX_DiscardUnknown() { var xxx_messageInfo_QueryTotalSwappableToCoinAmountRequest proto.InternalMessageInfo +func (m *QueryTotalSwappableToCoinAmountRequest) GetFromDenom() string { + if m != nil { + return m.FromDenom + } + return "" +} + +func (m *QueryTotalSwappableToCoinAmountRequest) GetToDenom() string { + if m != nil { + return m.ToDenom + } + return "" +} + type QueryTotalSwappableToCoinAmountResponse struct { SwappableAmount types.Coin `protobuf:"bytes,1,opt,name=swappable_amount,json=swappableAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"swappable_amount"` } @@ -212,35 +244,37 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/query.proto", fileDescriptor_01deae9da7816d6a) } var fileDescriptor_01deae9da7816d6a = []byte{ - // 446 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 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xf7, 0xe9, 0x25, 0xa7, 0x8c, 0xd5, 0x0b, 0x6d, 0x95, - 0xe0, 0xc7, 0x75, 0xab, 0xaa, 0x9c, 0x6c, 0xaf, 0x76, 0x53, 0xbf, 0xb8, 0xc6, 0xcb, 0xe9, 0xdc, - 0xb3, 0x67, 0x73, 0xcf, 0xfe, 0x35, 0xf7, 0xec, 0x0f, 0x0b, 0xcf, 0x9a, 0x2d, 0x3c, 0xeb, 0xfb, - 0xc2, 0xb3, 0xde, 0x90, 0xad, 0x4b, 0x19, 0x1b, 0x8a, 0x5e, 0x4e, 0x78, 0x55, 0x7f, 0x7e, 0xe7, - 0x7f, 0x02, 0x00, 0x00, 0xff, 0xff, 0xc0, 0xa3, 0xd5, 0x06, 0x16, 0x04, 0x00, 0x00, + // 473 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0x31, 0x6f, 0xd3, 0x40, + 0x14, 0xc7, 0x6d, 0x4b, 0x50, 0xf5, 0xa8, 0x4a, 0x75, 0x14, 0x11, 0xac, 0xe2, 0x82, 0x07, 0x40, + 0x02, 0xee, 0xe4, 0xb6, 0x7c, 0x00, 0x0a, 0x42, 0x2c, 0x0c, 0x84, 0x4e, 0x2c, 0xe1, 0xec, 0x5e, + 0x5c, 0x0b, 0xfb, 0x9e, 0x9b, 0x3b, 0xa7, 0xcd, 0xca, 0xc0, 0x8c, 0xc4, 0x57, 0x60, 0xe2, 0x23, + 0xb0, 0xb0, 0x76, 0x8c, 0xc4, 0xc2, 0x04, 0x28, 0xe1, 0x83, 0xa0, 0x3b, 0x5f, 0x48, 0x2c, 0x05, + 0x22, 0x54, 0x65, 0x7b, 0xbe, 0xf7, 0xde, 0xfd, 0xfe, 0xfe, 0xbf, 0x77, 0xa8, 0x95, 0xc7, 0x05, + 0xed, 0xca, 0x13, 0x56, 0xd2, 0x7e, 0x44, 0x8f, 0x2b, 0xde, 0x1b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x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -385,6 +419,20 @@ func (m *QuerySwappedRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.ToDenom) > 0 { + i -= len(m.ToDenom) + copy(dAtA[i:], m.ToDenom) + i = encodeVarintQuery(dAtA, i, uint64(len(m.ToDenom))) + i-- + dAtA[i] = 0x12 + } + if len(m.FromDenom) > 0 { + i -= len(m.FromDenom) + copy(dAtA[i:], m.FromDenom) + i = encodeVarintQuery(dAtA, i, uint64(len(m.FromDenom))) + i-- + dAtA[i] = 0xa + } return len(dAtA) - i, nil } @@ -451,6 +499,20 @@ func (m *QueryTotalSwappableToCoinAmountRequest) MarshalToSizedBuffer(dAtA []byt _ = i var l int _ = l + if len(m.ToDenom) > 0 { + i -= len(m.ToDenom) + copy(dAtA[i:], m.ToDenom) + i = encodeVarintQuery(dAtA, i, uint64(len(m.ToDenom))) + i-- + dAtA[i] = 0x12 + } + if len(m.FromDenom) > 0 { + i -= len(m.FromDenom) + copy(dAtA[i:], m.FromDenom) + i = encodeVarintQuery(dAtA, i, uint64(len(m.FromDenom))) + i-- + dAtA[i] = 0xa + } return len(dAtA) - i, nil } @@ -504,6 +566,14 @@ func (m *QuerySwappedRequest) Size() (n int) { } var l int _ = l + l = len(m.FromDenom) + if l > 0 { + n += 1 + l + sovQuery(uint64(l)) + } + l = len(m.ToDenom) + if l > 0 { + n += 1 + l + sovQuery(uint64(l)) + } return n } @@ -526,6 +596,14 @@ func (m *QueryTotalSwappableToCoinAmountRequest) Size() (n int) { } var l int _ = l + l = len(m.FromDenom) + if l > 0 { + n += 1 + l + sovQuery(uint64(l)) + } + l = len(m.ToDenom) + if l > 0 { + n += 1 + l + sovQuery(uint64(l)) + } return n } @@ -575,6 +653,70 @@ func (m *QuerySwappedRequest) Unmarshal(dAtA []byte) error { return fmt.Errorf("proto: QuerySwappedRequest: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field FromDenom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + 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 ErrInvalidLengthQuery + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.FromDenom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ToDenom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + 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 ErrInvalidLengthQuery + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ToDenom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipQuery(dAtA[iNdEx:]) @@ -741,6 +883,70 @@ func (m *QueryTotalSwappableToCoinAmountRequest) Unmarshal(dAtA []byte) error { return fmt.Errorf("proto: QueryTotalSwappableToCoinAmountRequest: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field FromDenom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + 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 ErrInvalidLengthQuery + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.FromDenom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ToDenom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + 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 ErrInvalidLengthQuery + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ToDenom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipQuery(dAtA[iNdEx:]) diff --git a/x/fswap/types/query.pb.gw.go b/x/fswap/types/query.pb.gw.go index ea06d0652c..0ff5b16232 100644 --- a/x/fswap/types/query.pb.gw.go +++ b/x/fswap/types/query.pb.gw.go @@ -31,10 +31,21 @@ var _ = runtime.String var _ = utilities.NewDoubleArray var _ = descriptor.ForMessage +var ( + filter_Query_Swapped_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} +) + func request_Query_Swapped_0(ctx context.Context, marshaler runtime.Marshaler, client QueryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq QuerySwappedRequest var metadata runtime.ServerMetadata + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Query_Swapped_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + msg, err := client.Swapped(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) return msg, metadata, err @@ -44,15 +55,33 @@ func local_request_Query_Swapped_0(ctx context.Context, marshaler runtime.Marsha var protoReq QuerySwappedRequest var metadata runtime.ServerMetadata + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Query_Swapped_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + msg, err := server.Swapped(ctx, &protoReq) return msg, metadata, err } +var ( + filter_Query_TotalSwappableToCoinAmount_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} +) + func request_Query_TotalSwappableToCoinAmount_0(ctx context.Context, marshaler runtime.Marshaler, client QueryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq QueryTotalSwappableToCoinAmountRequest var metadata runtime.ServerMetadata + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Query_TotalSwappableToCoinAmount_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + msg, err := client.TotalSwappableToCoinAmount(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) return msg, metadata, err @@ -62,6 +91,13 @@ func local_request_Query_TotalSwappableToCoinAmount_0(ctx context.Context, marsh var protoReq QueryTotalSwappableToCoinAmountRequest var metadata runtime.ServerMetadata + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Query_TotalSwappableToCoinAmount_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + msg, err := server.TotalSwappableToCoinAmount(ctx, &protoReq) return msg, metadata, err From edc1df6d12e81b1440ae89611010bcd8411135c8 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 09:19:42 +0900 Subject: [PATCH 19/47] chore: remove redundant denom validation --- x/fswap/keeper/keeper.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 06f3bee98a..44d7081903 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -97,14 +97,6 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Co return err } - if swap.GetFromDenom() != fromCoinAmount.GetDenom() { - return sdkerrors.ErrInvalidRequest.Wrapf("denom mismatch, expected %s, got %s", swap.GetFromDenom(), fromCoinAmount.Denom) - } - - if swap.GetToDenom() != toDenom { - return sdkerrors.ErrInvalidRequest.Wrapf("denom mismatch, expected %s, got %s", swap.GetToDenom(), toDenom) - } - newAmount := fromCoinAmount.Amount.Mul(swap.SwapMultiple) newCoinAmount := sdk.NewCoin(toDenom, newAmount) if err := k.checkSwapCap(ctx, swap, newCoinAmount); err != nil { From f59a2c33adf167c5257f737b6036b8ada3acf4bc Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 11:45:55 +0900 Subject: [PATCH 20/47] chore: remove cache-like operation, rename swap proposal in proto, errors start with number 2 --- docs/core/proto-docs.md | 28 +++---- proto/lbm/fswap/v1/fswap.proto | 2 +- x/fswap/client/cli/tx.go | 2 +- x/fswap/handler.go | 24 +----- x/fswap/keeper/keeper.go | 126 +------------------------------ x/fswap/keeper/keeper_test.go | 88 --------------------- x/fswap/keeper/proposal.go | 115 ++++++++++++++++++++++++++++ x/fswap/keeper/proposal_test.go | 87 +++++++++++++++++++++ x/fswap/module.go | 4 +- x/fswap/proposal_handler_test.go | 4 +- x/fswap/types/codec.go | 4 +- x/fswap/types/errors.go | 8 +- x/fswap/types/fswap.pb.go | 118 ++++++++++++++--------------- x/fswap/types/proposal.go | 16 ++-- 14 files changed, 300 insertions(+), 326 deletions(-) create mode 100644 x/fswap/keeper/proposal.go create mode 100644 x/fswap/keeper/proposal_test.go diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index 183452dd62..0d5be963be 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -860,8 +860,8 @@ - [EventSwapCoins](#lbm.fswap.v1.EventSwapCoins) - [lbm/fswap/v1/fswap.proto](#lbm/fswap/v1/fswap.proto) + - [MakeSwapProposal](#lbm.fswap.v1.MakeSwapProposal) - [Swap](#lbm.fswap.v1.Swap) - - [SwapProposal](#lbm.fswap.v1.SwapProposal) - [SwapStats](#lbm.fswap.v1.SwapStats) - [Swapped](#lbm.fswap.v1.Swapped) @@ -12751,35 +12751,35 @@ Msg defines the foundation Msg service. - - -### Swap + +### MakeSwapProposal +From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `from_denom` | [string](#string) | | | -| `to_denom` | [string](#string) | | | -| `amount_cap_for_to_denom` | [string](#string) | | | -| `swap_multiple` | [string](#string) | | | +| `title` | [string](#string) | | | +| `description` | [string](#string) | | | +| `swap` | [Swap](#lbm.fswap.v1.Swap) | | | - + + +### Swap -### SwapProposal -From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `title` | [string](#string) | | | -| `description` | [string](#string) | | | -| `swap` | [Swap](#lbm.fswap.v1.Swap) | | | +| `from_denom` | [string](#string) | | | +| `to_denom` | [string](#string) | | | +| `amount_cap_for_to_denom` | [string](#string) | | | +| `swap_multiple` | [string](#string) | | | diff --git a/proto/lbm/fswap/v1/fswap.proto b/proto/lbm/fswap/v1/fswap.proto index d771b253b7..06b8ee3e83 100644 --- a/proto/lbm/fswap/v1/fswap.proto +++ b/proto/lbm/fswap/v1/fswap.proto @@ -26,7 +26,7 @@ message SwapStats { } // From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 -message SwapProposal { +message MakeSwapProposal { option (gogoproto.equal) = true; option (gogoproto.goproto_stringer) = false; diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index 8baec3f747..bd0f2915b1 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -190,6 +190,6 @@ func parseArgsToContent(cmd *cobra.Command) (gov.Content, error) { SwapMultiple: sdk.NewInt(swap_rate), } - content := types.NewSwapProposal(title, description, swapInit) + content := types.NewMakeSwapProposal(title, description, swapInit) return content, nil } diff --git a/x/fswap/handler.go b/x/fswap/handler.go index 8c0a4f7114..41ca61f973 100644 --- a/x/fswap/handler.go +++ b/x/fswap/handler.go @@ -1,8 +1,6 @@ package fswap import ( - "fmt" - sdk "github.com/Finschia/finschia-sdk/types" sdkerrors "github.com/Finschia/finschia-sdk/types/errors" "github.com/Finschia/finschia-sdk/x/fswap/keeper" @@ -10,29 +8,13 @@ import ( govtypes "github.com/Finschia/finschia-sdk/x/gov/types" ) -// NewHandler ... -func NewHandler(k keeper.Keeper) sdk.Handler { - // this line is used by starport scaffolding # handler/msgServer - - return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { - // ctx = ctx.WithEventManager(sdk.NewEventManager()) - - // switch msg := msg.(type) { - // this line is used by starport scaffolding # 1 - // default: - errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg) - return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg) - // } - } -} - // NewSwapHandler creates a governance handler to manage new proposal types. // It enables Swap to propose a swap init func NewSwapHandler(k keeper.Keeper) govtypes.Handler { return func(ctx sdk.Context, content govtypes.Content) error { switch c := content.(type) { - case *types.SwapProposal: - return handleSwap(ctx, k, c) + case *types.MakeSwapProposal: + return handleMakeSwapProposal(ctx, k, c) default: return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized sawp proposal content type: %T", c) @@ -40,6 +22,6 @@ func NewSwapHandler(k keeper.Keeper) govtypes.Handler { } } -func handleSwap(ctx sdk.Context, k keeper.Keeper, p *types.SwapProposal) error { +func handleMakeSwapProposal(ctx sdk.Context, k keeper.Keeper, p *types.MakeSwapProposal) error { return k.MakeSwap(ctx, p.Swap) } diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 44d7081903..d86ff2d62f 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -21,9 +21,6 @@ type Keeper struct { AccountKeeper BankKeeper - - swaps []types.Swap - swappedSlice []types.Swapped } func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, config types.Config, ak AccountKeeper, bk BankKeeper) Keeper { @@ -33,8 +30,6 @@ func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, config types config, ak, bk, - make([]types.Swap, config.MaxSwaps), - make([]types.Swapped, config.MaxSwaps), } } @@ -42,55 +37,6 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) } -func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap) error { - if err := swap.ValidateBasic(); err != nil { - return err - } - - isNewSwap := true - if _, err := k.getSwap(ctx, swap.ToDenom, swap.FromDenom); err == nil { - return sdkerrors.ErrInvalidRequest.Wrap("there is already a swap in reverse way, only one way swap allowed") - } - - if _, err := k.getSwap(ctx, swap.FromDenom, swap.ToDenom); err == nil { - isNewSwap = false - } - - if !isNewSwap && !k.config.UpdateAllowed { - return sdkerrors.ErrInvalidRequest.Wrap("update existing swap not allowed") - } - - if err := k.increaseSwapCount(ctx); err != nil { - return err - } - - key := swapKey(swap.FromDenom, swap.ToDenom) - bz, err := k.cdc.Marshal(&swap) - if err != nil { - return err - } - - store := ctx.KVStore(k.storeKey) - store.Set(key, bz) - - if isNewSwap { - swapped := types.Swapped{ - FromCoinAmount: sdk.Coin{ - Denom: swap.GetFromDenom(), - Amount: sdk.ZeroInt(), - }, - ToCoinAmount: sdk.Coin{ - Denom: swap.GetToDenom(), - Amount: sdk.ZeroInt(), - }, - } - if err := k.setSwapped(ctx, swap.GetFromDenom(), swap.GetToDenom(), swapped); err != nil { - return err - } - } - return nil -} - func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Coin, toDenom string) error { swap, err := k.getSwap(ctx, fromCoinAmount.Denom, toDenom) if err != nil { @@ -146,16 +92,12 @@ func (k Keeper) SwapAll(ctx sdk.Context, addr sdk.AccAddress, fromDenom, toDenom } func (k Keeper) getAllSwapped(ctx sdk.Context) []types.Swapped { - idx := 0 + swappedSlice := []types.Swapped{} k.iterateAllSwapped(ctx, func(swapped types.Swapped) bool { - k.swappedSlice[idx] = swapped - idx++ + swappedSlice = append(swappedSlice, swapped) return false }) - if idx == 0 { - return []types.Swapped{} - } - return k.swappedSlice + return swappedSlice } func (k Keeper) iterateAllSwapped(ctx sdk.Context, cb func(swapped types.Swapped) (stop bool)) { @@ -201,34 +143,6 @@ func (k Keeper) setSwapped(ctx sdk.Context, fromDenom, toDenom string, swapped t return nil } -func (k Keeper) getAllSwaps(ctx sdk.Context) []types.Swap { - idx := 0 - k.iterateAllSwaps(ctx, func(swap types.Swap) bool { - k.swaps[idx] = swap - idx++ - return false - }) - if idx == 0 { - return []types.Swap{} - } - return k.swaps -} - -func (k Keeper) iterateAllSwaps(ctx sdk.Context, cb func(swapped types.Swap) (stop bool)) { - store := ctx.KVStore(k.storeKey) - swapDataStore := prefix.NewStore(store, swapPrefix) - - iterator := swapDataStore.Iterator(nil, nil) - defer iterator.Close() - for ; iterator.Valid(); iterator.Next() { - swap := types.Swap{} - k.cdc.MustUnmarshal(iterator.Value(), &swap) - if cb(swap) { - break - } - } -} - func (k Keeper) getSwappableNewCoinAmount(ctx sdk.Context, fromDenom, toDenom string) (sdk.Coin, error) { swap, err := k.getSwap(ctx, fromDenom, toDenom) if err != nil { @@ -246,22 +160,6 @@ func (k Keeper) getSwappableNewCoinAmount(ctx sdk.Context, fromDenom, toDenom st return sdk.NewCoin(toDenom, remainingAmount), nil } -func (k Keeper) getSwap(ctx sdk.Context, fromDenom, toDenom string) (types.Swap, error) { - store := ctx.KVStore(k.storeKey) - key := swapKey(fromDenom, toDenom) - bz := store.Get(key) - if bz == nil { - return types.Swap{}, sdkerrors.ErrNotFound.Wrap("swap not found") - } - - swap := types.Swap{} - if err := k.cdc.Unmarshal(bz, &swap); err != nil { - return types.Swap{}, err - } - - return swap, nil -} - func (k Keeper) updateSwapped(ctx sdk.Context, fromAmount, toAmount sdk.Coin) error { prevSwapped, err := k.getSwapped(ctx, fromAmount.Denom, toAmount.Denom) if err != nil { @@ -297,24 +195,6 @@ func (k Keeper) checkSwapCap(ctx sdk.Context, swap types.Swap, newCoinAmount sdk return nil } -func (k Keeper) increaseSwapCount(ctx sdk.Context) error { - stats, err := k.getSwapStats(ctx) - if err != nil { - return err - } - - prev := stats.SwapCount - stats.SwapCount += 1 - if stats.SwapCount < prev { - return types.ErrInvalidState.Wrap("overflow detected") - } - - if err := k.setSwapStats(ctx, stats); err != nil { - return err - } - return nil -} - func (k Keeper) getSwapStats(ctx sdk.Context) (types.SwapStats, error) { store := ctx.KVStore(k.storeKey) bz := store.Get(swapStatsKey) diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 8457f227ad..3af3352dd2 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -137,14 +137,6 @@ func (s *KeeperTestSuite) TestSwap() { true, sdkerrors.ErrInsufficientFunds, }, - "swap with the same from-denom and to-denom": { - s.accWithFromCoin, - sdk.NewCoin(s.swap.GetToDenom(), s.initBalance), - s.swap.GetToDenom(), - sdk.ZeroInt(), - true, - sdkerrors.ErrInvalidRequest, - }, } for name, tc := range testCases { s.Run(name, func() { @@ -211,83 +203,3 @@ func (s *KeeperTestSuite) TestSwapAll() { }) } } - -func (s *KeeperTestSuite) TestSwapInit() { - testCases := map[string]struct { - req types.Swap - shouldThrowError bool - expectedError error - }{ - "valid getSwapInit": { - types.Swap{ - FromDenom: "fromD", - ToDenom: "toD", - AmountCapForToDenom: sdk.OneInt(), - SwapMultiple: sdk.OneInt(), - }, - false, - nil, - }, - "invalid empty from-denom": { - types.Swap{ - FromDenom: "", - ToDenom: "toD", - AmountCapForToDenom: sdk.OneInt(), - SwapMultiple: sdk.OneInt(), - }, - true, - sdkerrors.ErrInvalidRequest, - }, - "invalid empty to-denom": { - types.Swap{ - FromDenom: "fromD", - ToDenom: "", - AmountCapForToDenom: sdk.OneInt(), - SwapMultiple: sdk.OneInt(), - }, - true, - sdkerrors.ErrInvalidRequest, - }, - "invalid zero amount cap for to-denom": { - types.Swap{ - FromDenom: "fromD", - ToDenom: "toD", - AmountCapForToDenom: sdk.ZeroInt(), - SwapMultiple: sdk.OneInt(), - }, - true, - sdkerrors.ErrInvalidRequest, - }, - "invalid zero swap-rate": { - types.Swap{ - FromDenom: "fromD", - ToDenom: "toD", - AmountCapForToDenom: sdk.OneInt(), - SwapMultiple: sdk.ZeroInt(), - }, - true, - sdkerrors.ErrInvalidRequest, - }, - "invalid the same from-denom and to-denom": { - types.Swap{ - FromDenom: "same", - ToDenom: "same", - AmountCapForToDenom: sdk.OneInt(), - SwapMultiple: sdk.OneInt(), - }, - true, - sdkerrors.ErrInvalidRequest, - }, - } - for name, tc := range testCases { - s.Run(name, func() { - ctx, _ := s.ctx.CacheContext() - err := s.keeper.MakeSwap(ctx, tc.req) - if tc.shouldThrowError { - s.Require().ErrorIs(err, tc.expectedError) - return - } - s.Require().NoError(err) - }) - } -} diff --git a/x/fswap/keeper/proposal.go b/x/fswap/keeper/proposal.go new file mode 100644 index 0000000000..fefbc85966 --- /dev/null +++ b/x/fswap/keeper/proposal.go @@ -0,0 +1,115 @@ +package keeper + +import ( + "github.com/Finschia/finschia-sdk/store/prefix" + sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/types/errors" + "github.com/Finschia/finschia-sdk/x/fswap/types" +) + +func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap) error { + if err := swap.ValidateBasic(); err != nil { + return err + } + + isNewSwap := true + if _, err := k.getSwap(ctx, swap.ToDenom, swap.FromDenom); err == nil { + return errors.ErrInvalidRequest.Wrap("there is already a swap in reverse way, only one way swap allowed") + } + + if _, err := k.getSwap(ctx, swap.FromDenom, swap.ToDenom); err == nil { + isNewSwap = false + } + + if !isNewSwap && !k.config.UpdateAllowed { + return errors.ErrInvalidRequest.Wrap("update existing swap not allowed") + } + + if err := k.increaseSwapCount(ctx); err != nil { + return err + } + + key := swapKey(swap.FromDenom, swap.ToDenom) + bz, err := k.cdc.Marshal(&swap) + if err != nil { + return err + } + + store := ctx.KVStore(k.storeKey) + store.Set(key, bz) + + if isNewSwap { + swapped := types.Swapped{ + FromCoinAmount: sdk.Coin{ + Denom: swap.GetFromDenom(), + Amount: sdk.ZeroInt(), + }, + ToCoinAmount: sdk.Coin{ + Denom: swap.GetToDenom(), + Amount: sdk.ZeroInt(), + }, + } + if err := k.setSwapped(ctx, swap.GetFromDenom(), swap.GetToDenom(), swapped); err != nil { + return err + } + } + return nil +} + +func (k Keeper) increaseSwapCount(ctx sdk.Context) error { + stats, err := k.getSwapStats(ctx) + if err != nil { + return err + } + + prev := stats.SwapCount + stats.SwapCount += 1 + if stats.SwapCount < prev { + return types.ErrInvalidState.Wrap("overflow detected") + } + + if err := k.setSwapStats(ctx, stats); err != nil { + return err + } + return nil +} + +func (k Keeper) getSwap(ctx sdk.Context, fromDenom, toDenom string) (types.Swap, error) { + store := ctx.KVStore(k.storeKey) + key := swapKey(fromDenom, toDenom) + bz := store.Get(key) + if bz == nil { + return types.Swap{}, errors.ErrNotFound.Wrap("swap not found") + } + + swap := types.Swap{} + if err := k.cdc.Unmarshal(bz, &swap); err != nil { + return types.Swap{}, err + } + + return swap, nil +} + +func (k Keeper) getAllSwaps(ctx sdk.Context) []types.Swap { + swaps := []types.Swap{} + k.iterateAllSwaps(ctx, func(swap types.Swap) bool { + swaps = append(swaps, swap) + return false + }) + return swaps +} + +func (k Keeper) iterateAllSwaps(ctx sdk.Context, cb func(swapped types.Swap) (stop bool)) { + store := ctx.KVStore(k.storeKey) + swapDataStore := prefix.NewStore(store, swapPrefix) + + iterator := swapDataStore.Iterator(nil, nil) + defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { + swap := types.Swap{} + k.cdc.MustUnmarshal(iterator.Value(), &swap) + if cb(swap) { + break + } + } +} diff --git a/x/fswap/keeper/proposal_test.go b/x/fswap/keeper/proposal_test.go new file mode 100644 index 0000000000..9dbe91d0e0 --- /dev/null +++ b/x/fswap/keeper/proposal_test.go @@ -0,0 +1,87 @@ +package keeper_test + +import ( + sdk "github.com/Finschia/finschia-sdk/types" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + "github.com/Finschia/finschia-sdk/x/fswap/types" +) + +func (s *KeeperTestSuite) TestMakeSwapProposal() { + testCases := map[string]struct { + req types.Swap + shouldThrowError bool + expectedError error + }{ + "valid getSwap": { + types.Swap{ + FromDenom: "fromD", + ToDenom: "toD", + AmountCapForToDenom: sdk.OneInt(), + SwapMultiple: sdk.OneInt(), + }, + false, + nil, + }, + "invalid empty from-denom": { + types.Swap{ + FromDenom: "", + ToDenom: "toD", + AmountCapForToDenom: sdk.OneInt(), + SwapMultiple: sdk.OneInt(), + }, + true, + sdkerrors.ErrInvalidRequest, + }, + "invalid empty to-denom": { + types.Swap{ + FromDenom: "fromD", + ToDenom: "", + AmountCapForToDenom: sdk.OneInt(), + SwapMultiple: sdk.OneInt(), + }, + true, + sdkerrors.ErrInvalidRequest, + }, + "invalid zero amount cap for to-denom": { + types.Swap{ + FromDenom: "fromD", + ToDenom: "toD", + AmountCapForToDenom: sdk.ZeroInt(), + SwapMultiple: sdk.OneInt(), + }, + true, + sdkerrors.ErrInvalidRequest, + }, + "invalid zero swap-rate": { + types.Swap{ + FromDenom: "fromD", + ToDenom: "toD", + AmountCapForToDenom: sdk.OneInt(), + SwapMultiple: sdk.ZeroInt(), + }, + true, + sdkerrors.ErrInvalidRequest, + }, + "invalid the same from-denom and to-denom": { + types.Swap{ + FromDenom: "same", + ToDenom: "same", + AmountCapForToDenom: sdk.OneInt(), + SwapMultiple: sdk.OneInt(), + }, + true, + sdkerrors.ErrInvalidRequest, + }, + } + for name, tc := range testCases { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() + err := s.keeper.MakeSwap(ctx, tc.req) + if tc.shouldThrowError { + s.Require().ErrorIs(err, tc.expectedError) + return + } + s.Require().NoError(err) + }) + } +} diff --git a/x/fswap/module.go b/x/fswap/module.go index 9ee61a12cb..ff1d97e9d0 100644 --- a/x/fswap/module.go +++ b/x/fswap/module.go @@ -117,9 +117,7 @@ func (am AppModule) Name() string { } // Route returns the fswap module's message routing key. -func (am AppModule) Route() sdk.Route { - return sdk.NewRoute(types.RouterKey, NewHandler(am.keeper)) -} +func (AppModule) Route() sdk.Route { return sdk.Route{} } // QuerierRoute returns the fswap module's query routing key. func (AppModule) QuerierRoute() string { return types.QuerierRoute } diff --git a/x/fswap/proposal_handler_test.go b/x/fswap/proposal_handler_test.go index 2790520cae..974c81e0c4 100644 --- a/x/fswap/proposal_handler_test.go +++ b/x/fswap/proposal_handler_test.go @@ -6,8 +6,8 @@ import ( "github.com/Finschia/finschia-sdk/x/fswap/types" ) -func testProposal(swapInit types.Swap) *types.SwapProposal { - return types.NewSwapProposal("Test", "description", swapInit) +func testProposal(swapInit types.Swap) *types.MakeSwapProposal { + return types.NewMakeSwapProposal("Test", "description", swapInit) } func TestProposalHandlerPassed(t *testing.T) { diff --git a/x/fswap/types/codec.go b/x/fswap/types/codec.go index 09ded16639..e0419b7755 100644 --- a/x/fswap/types/codec.go +++ b/x/fswap/types/codec.go @@ -17,7 +17,7 @@ func RegisterCodec(cdc *codec.LegacyAmino) { } func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&SwapProposal{}, "finschia-sdk/SwapProposal", nil) + cdc.RegisterConcrete(&MakeSwapProposal{}, "finschia-sdk/MakeSwapProposal", nil) } func RegisterInterfaces(registry types.InterfaceRegistry) { @@ -26,7 +26,7 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) registry.RegisterImplementations( (*govtypes.Content)(nil), - &SwapProposal{}, + &MakeSwapProposal{}, ) } diff --git a/x/fswap/types/errors.go b/x/fswap/types/errors.go index ee4e52bf1a..24e0fa6c11 100644 --- a/x/fswap/types/errors.go +++ b/x/fswap/types/errors.go @@ -8,8 +8,8 @@ import ( // x/fswap module sentinel errors var ( - ErrInvalidState = sdkerrors.Register(ModuleName, 1100, "swap module invalid state") - ErrCanNotHaveMoreSwap = sdkerrors.Register(ModuleName, 1101, "no more swap allowed") - ErrSwappedNotFound = sdkerrors.Register(ModuleName, 1102, "swapped does not exist") - ErrExceedSwappableToCoinAmount = sdkerrors.Register(ModuleName, 1103, "exceed swappable to-coin amount") + ErrInvalidState = sdkerrors.Register(ModuleName, 2, "swap module invalid state") + ErrCanNotHaveMoreSwap = sdkerrors.Register(ModuleName, 3, "no more swap allowed") + ErrSwappedNotFound = sdkerrors.Register(ModuleName, 4, "swapped does not exist") + ErrExceedSwappableToCoinAmount = sdkerrors.Register(ModuleName, 5, "exceed swappable to-coin amount") ) diff --git a/x/fswap/types/fswap.pb.go b/x/fswap/types/fswap.pb.go index edf25acb93..333ccc3a27 100644 --- a/x/fswap/types/fswap.pb.go +++ b/x/fswap/types/fswap.pb.go @@ -122,23 +122,23 @@ func (m *SwapStats) GetSwapCount() int32 { } // From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 -type SwapProposal struct { +type MakeSwapProposal struct { Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"` Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` Swap Swap `protobuf:"bytes,3,opt,name=swap,proto3" json:"swap"` } -func (m *SwapProposal) Reset() { *m = SwapProposal{} } -func (*SwapProposal) ProtoMessage() {} -func (*SwapProposal) Descriptor() ([]byte, []int) { +func (m *MakeSwapProposal) Reset() { *m = MakeSwapProposal{} } +func (*MakeSwapProposal) ProtoMessage() {} +func (*MakeSwapProposal) Descriptor() ([]byte, []int) { return fileDescriptor_42ca60eaf37a2b67, []int{2} } -func (m *SwapProposal) XXX_Unmarshal(b []byte) error { +func (m *MakeSwapProposal) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) } -func (m *SwapProposal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { +func (m *MakeSwapProposal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { if deterministic { - return xxx_messageInfo_SwapProposal.Marshal(b, m, deterministic) + return xxx_messageInfo_MakeSwapProposal.Marshal(b, m, deterministic) } else { b = b[:cap(b)] n, err := m.MarshalToSizedBuffer(b) @@ -148,33 +148,33 @@ func (m *SwapProposal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) return b[:n], nil } } -func (m *SwapProposal) XXX_Merge(src proto.Message) { - xxx_messageInfo_SwapProposal.Merge(m, src) +func (m *MakeSwapProposal) XXX_Merge(src proto.Message) { + xxx_messageInfo_MakeSwapProposal.Merge(m, src) } -func (m *SwapProposal) XXX_Size() int { +func (m *MakeSwapProposal) XXX_Size() int { return m.Size() } -func (m *SwapProposal) XXX_DiscardUnknown() { - xxx_messageInfo_SwapProposal.DiscardUnknown(m) +func (m *MakeSwapProposal) XXX_DiscardUnknown() { + xxx_messageInfo_MakeSwapProposal.DiscardUnknown(m) } -var xxx_messageInfo_SwapProposal proto.InternalMessageInfo +var xxx_messageInfo_MakeSwapProposal proto.InternalMessageInfo -func (m *SwapProposal) GetTitle() string { +func (m *MakeSwapProposal) GetTitle() string { if m != nil { return m.Title } return "" } -func (m *SwapProposal) GetDescription() string { +func (m *MakeSwapProposal) GetDescription() string { if m != nil { return m.Description } return "" } -func (m *SwapProposal) GetSwap() Swap { +func (m *MakeSwapProposal) GetSwap() Swap { if m != nil { return m.Swap } @@ -235,45 +235,45 @@ func (m *Swapped) GetToCoinAmount() types.Coin { func init() { proto.RegisterType((*Swap)(nil), "lbm.fswap.v1.Swap") proto.RegisterType((*SwapStats)(nil), "lbm.fswap.v1.SwapStats") - proto.RegisterType((*SwapProposal)(nil), "lbm.fswap.v1.SwapProposal") + proto.RegisterType((*MakeSwapProposal)(nil), "lbm.fswap.v1.MakeSwapProposal") proto.RegisterType((*Swapped)(nil), "lbm.fswap.v1.Swapped") } func init() { proto.RegisterFile("lbm/fswap/v1/fswap.proto", fileDescriptor_42ca60eaf37a2b67) } var fileDescriptor_42ca60eaf37a2b67 = []byte{ - // 483 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 0x4f, 0x6f, 0xd3, 0x4e, - 0x10, 0xb5, 0xf3, 0x73, 0x7f, 0x6d, 0x36, 0xa1, 0x42, 0x4b, 0x25, 0xd2, 0x4a, 0xd8, 0x55, 0x4f, - 0x88, 0x3f, 0xbb, 0x4a, 0xca, 0xa9, 0x37, 0x12, 0x54, 0xc1, 0x01, 0x09, 0xb9, 0x48, 0x48, 0x5c, - 0xac, 0xb5, 0xe3, 0x24, 0x2b, 0x6c, 0xcf, 0xca, 0xbb, 0x49, 0xcb, 0x85, 0x6f, 0x80, 0xc4, 0x91, - 0x63, 0xcf, 0x7c, 0x92, 0x1e, 0x7b, 0x44, 0x1c, 0x0a, 0x4a, 0x2e, 0x7c, 0x0b, 0xd0, 0xec, 0x5a, - 0x10, 0x4e, 0x20, 0x24, 0x6e, 0xe3, 0xf7, 0x66, 0xdf, 0xf3, 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0xbf, 0x95, 0x3e, 0x6b, 0x9e, 0x97, 0xb5, 0x48, - 0xff, 0xb7, 0x0f, 0xe3, 0xf0, 0x7b, 0x00, 0x00, 0x00, 0xff, 0xff, 0x9a, 0x24, 0xaa, 0x02, 0x78, - 0x03, 0x00, 0x00, + // 485 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 0xbf, 0x6f, 0xd3, 0x40, + 0x14, 0x8e, 0x83, 0x4b, 0x9b, 0x4b, 0xa8, 0x2a, 0x53, 0x89, 0xb4, 0x12, 0x76, 0xd5, 0x09, 0xf1, + 0xe3, 0x4e, 0x49, 0x99, 0xba, 0x91, 0xa0, 0x0a, 0x86, 0x4a, 0xc8, 0x45, 0x42, 0x62, 0xb1, 0xce, + 0xce, 0x25, 0x39, 0xd5, 0xf6, 0x3b, 0xf9, 0x2e, 0x69, 0x59, 0xd9, 0x91, 0x18, 0x19, 0x3b, 0xf3, + 0x97, 0x74, 0xec, 0x88, 0x18, 0x0a, 0x4a, 0x16, 0xfe, 0x0b, 0xd0, 0xbb, 0xb3, 0x20, 0x4c, 0x20, + 0x24, 0xb6, 0xe7, 0xef, 0x7b, 0xf7, 0x7d, 0xe7, 0xef, 0xbd, 0x23, 0xdd, 0x3c, 0x2d, 0xd8, 0x58, + 0x9f, 0x71, 0xc5, 0xe6, 0x3d, 0x57, 0x50, 0x55, 0x81, 0x81, 0xa0, 0x93, 0xa7, 0x05, 0x75, 0xc0, + 0xbc, 0xb7, 0xbb, 0x3d, 0x81, 0x09, 0x58, 0x82, 0x61, 0xe5, 0x7a, 0x76, 0xc3, 0x0c, 0x74, 0x01, + 0x9a, 0xa5, 0x5c, 0x0b, 0x36, 0xef, 0xa5, 0xc2, 0xf0, 0x1e, 0xcb, 0x40, 0x96, 0x8e, 0xdf, 0x7f, + 0xd7, 0x24, 0xfe, 0xc9, 0x19, 0x57, 0xc1, 0x5d, 0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4, 0x32, + 0x12, 0xca, 0x3a, 0xcb, 0x55, 0x28, 0x78, 0x48, 0x7c, 0xf4, 0xb0, 0xe1, 0xb5, 0xfb, 0x01, 0x5d, + 0x9d, 0x32, 0x45, 0x87, 0x81, 0x8f, 0x3f, 0x1c, 0xdb, 0xae, 0x95, 0x4b, 0x7c, 0xf7, 0xc8, 0x3a, + 0xd2, 0x4a, 0x8c, 0x82, 0x73, 0xb2, 0x65, 0xa7, 0x89, 0x93, 0x4e, 0x5c, 0x94, 0xf6, 0x1a, 0xed, + 0xfe, 0x0e, 0x75, 0x1b, 0x41, 0x71, 0x23, 0x68, 0xbd, 0x11, 0x74, 0x08, 0xb2, 0x1c, 0x1c, 0xa0, + 0xec, 0xc7, 0x2f, 0xd1, 0x83, 0xbf, 0xcc, 0x11, 0x0f, 0xc5, 0x9b, 0xe8, 0x83, 0xd5, 0x13, 0xeb, + 0x12, 0x18, 0xb2, 0x69, 0xe0, 0x37, 0xdf, 0xe6, 0x7f, 0xf1, 0xed, 0x18, 0xf8, 0xe5, 0x7a, 0xe8, + 0x63, 0x0a, 0x83, 0x67, 0x97, 0x8b, 0xd0, 0xbb, 0x5a, 0x84, 0xde, 0xd7, 0x45, 0xe8, 0xbd, 0x5f, + 0x86, 0x8d, 0xab, 0x65, 0xd8, 0xf8, 0xb4, 0x0c, 0x1b, 0xaf, 0xe9, 0x1f, 0xa5, 0xcf, 0xeb, 0x37, + 0x66, 0x2d, 0xd2, 0x9b, 0xf6, 0x75, 0x1c, 0xfc, 0x08, 0x00, 0x00, 0xff, 0xff, 0xcd, 0x7d, 0xbf, + 0x68, 0x7d, 0x03, 0x00, 0x00, } func (this *Swap) Equal(that interface{}) bool { @@ -333,14 +333,14 @@ func (this *SwapStats) Equal(that interface{}) bool { } return true } -func (this *SwapProposal) Equal(that interface{}) bool { +func (this *MakeSwapProposal) Equal(that interface{}) bool { if that == nil { return this == nil } - that1, ok := that.(*SwapProposal) + that1, ok := that.(*MakeSwapProposal) if !ok { - that2, ok := that.(SwapProposal) + that2, ok := that.(MakeSwapProposal) if ok { that1 = &that2 } else { @@ -448,7 +448,7 @@ func (m *SwapStats) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } -func (m *SwapProposal) Marshal() (dAtA []byte, err error) { +func (m *MakeSwapProposal) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) n, err := m.MarshalToSizedBuffer(dAtA[:size]) @@ -458,12 +458,12 @@ func (m *SwapProposal) Marshal() (dAtA []byte, err error) { return dAtA[:n], nil } -func (m *SwapProposal) MarshalTo(dAtA []byte) (int, error) { +func (m *MakeSwapProposal) MarshalTo(dAtA []byte) (int, error) { size := m.Size() return m.MarshalToSizedBuffer(dAtA[:size]) } -func (m *SwapProposal) MarshalToSizedBuffer(dAtA []byte) (int, error) { +func (m *MakeSwapProposal) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) _ = i var l int @@ -582,7 +582,7 @@ func (m *SwapStats) Size() (n int) { return n } -func (m *SwapProposal) Size() (n int) { +func (m *MakeSwapProposal) Size() (n int) { if m == nil { return 0 } @@ -871,7 +871,7 @@ func (m *SwapStats) Unmarshal(dAtA []byte) error { } return nil } -func (m *SwapProposal) Unmarshal(dAtA []byte) error { +func (m *MakeSwapProposal) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { @@ -894,10 +894,10 @@ func (m *SwapProposal) Unmarshal(dAtA []byte) error { fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { - return fmt.Errorf("proto: SwapProposal: wiretype end group for non-group") + return fmt.Errorf("proto: MakeSwapProposal: wiretype end group for non-group") } if fieldNum <= 0 { - return fmt.Errorf("proto: SwapProposal: illegal tag %d (wire type %d)", fieldNum, wire) + return fmt.Errorf("proto: MakeSwapProposal: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: diff --git a/x/fswap/types/proposal.go b/x/fswap/types/proposal.go index 302d7e98c2..ac2ab862cf 100644 --- a/x/fswap/types/proposal.go +++ b/x/fswap/types/proposal.go @@ -10,34 +10,34 @@ const ( ProposalTypeSwap string = "Swap" ) -// NewSwapProposal creates a new SwapProposal instance. +// NewMakeSwapProposal creates a new SwapProposal instance. // Deprecated: this proposal is considered legacy and is deprecated in favor of // Msg-based gov proposals. See MsgSwap. -func NewSwapProposal(title, description string, swap Swap) *SwapProposal { - return &SwapProposal{title, description, swap} +func NewMakeSwapProposal(title, description string, swap Swap) *MakeSwapProposal { + return &MakeSwapProposal{title, description, swap} } // Implements Proposal Interface -var _ gov.Content = &SwapProposal{} +var _ gov.Content = &MakeSwapProposal{} func init() { gov.RegisterProposalType(ProposalTypeSwap) } // ProposalRoute gets the proposal's router key -func (m *SwapProposal) ProposalRoute() string { return RouterKey } +func (m *MakeSwapProposal) ProposalRoute() string { return RouterKey } // ProposalType is "Swap" -func (m *SwapProposal) ProposalType() string { return ProposalTypeSwap } +func (m *MakeSwapProposal) ProposalType() string { return ProposalTypeSwap } // String implements the Stringer interface. -func (m *SwapProposal) String() string { +func (m *MakeSwapProposal) String() string { out, _ := yaml.Marshal(m) return string(out) } // ValidateBasic validates the proposal -func (m *SwapProposal) ValidateBasic() error { +func (m *MakeSwapProposal) ValidateBasic() error { if err := m.Swap.ValidateBasic(); err != nil { return err } From 8e3e634a8794379dfc8a8e815f691b53491a4b72 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 14:03:50 +0900 Subject: [PATCH 21/47] chore: apply feedbacks --- simapp/app.go | 4 +- x/fswap/handler.go | 2 +- x/fswap/keeper/expected_keepers.go | 5 -- x/fswap/keeper/genesis.go | 14 +---- x/fswap/keeper/keeper.go | 48 ++++++++--------- x/fswap/module.go | 7 +-- x/fswap/proposal_handler_test.go | 29 ++++++---- x/fswap/testutil/expected_keepers_mocks.go | 63 +++++----------------- x/fswap/types/msgs.go | 14 ++--- 9 files changed, 67 insertions(+), 119 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 49885f9ec3..43d01afb7e 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -326,7 +326,7 @@ func NewSimApp( app.ClassKeeper = classkeeper.NewKeeper(appCodec, keys[class.StoreKey]) app.TokenKeeper = tokenkeeper.NewKeeper(appCodec, keys[token.StoreKey], app.ClassKeeper) app.CollectionKeeper = collectionkeeper.NewKeeper(appCodec, keys[collection.StoreKey], app.ClassKeeper) - app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], fswaptypes.DefaultConfig(), app.AccountKeeper, app.BankKeeper) + app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], fswaptypes.DefaultConfig(), app.BankKeeper) // register the staking hooks // NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks @@ -394,7 +394,7 @@ func NewSimApp( tokenmodule.NewAppModule(appCodec, app.TokenKeeper), collectionmodule.NewAppModule(appCodec, app.CollectionKeeper), authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), - fswap.NewAppModule(appCodec, app.FswapKeeper, app.AccountKeeper, app.BankKeeper), + fswap.NewAppModule(appCodec, app.FswapKeeper, app.BankKeeper), ) // During begin block slashing happens after distr.BeginBlocker so that diff --git a/x/fswap/handler.go b/x/fswap/handler.go index 41ca61f973..401fe29332 100644 --- a/x/fswap/handler.go +++ b/x/fswap/handler.go @@ -17,7 +17,7 @@ func NewSwapHandler(k keeper.Keeper) govtypes.Handler { return handleMakeSwapProposal(ctx, k, c) default: - return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized sawp proposal content type: %T", c) + return sdkerrors.ErrUnknownRequest.Wrapf("unrecognized sawp proposal content type: %T", c) } } } diff --git a/x/fswap/keeper/expected_keepers.go b/x/fswap/keeper/expected_keepers.go index 5b45bc1963..17466c69cd 100644 --- a/x/fswap/keeper/expected_keepers.go +++ b/x/fswap/keeper/expected_keepers.go @@ -5,12 +5,7 @@ import ( ) type ( - AccountKeeper interface { - GetModuleAddress(name string) sdk.AccAddress - } - BankKeeper interface { - HasBalance(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coin) bool GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error diff --git a/x/fswap/keeper/genesis.go b/x/fswap/keeper/genesis.go index 20b01e3656..1630e4b667 100644 --- a/x/fswap/keeper/genesis.go +++ b/x/fswap/keeper/genesis.go @@ -6,7 +6,7 @@ import ( ) // InitGenesis initializes the module's state from a provided genesis state. -func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error { +func (k Keeper) InitGenesis(_ sdk.Context, genState *types.GenesisState) error { if err := genState.Validate(); err != nil { return err } @@ -18,18 +18,6 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error if len(genState.GetSwappeds()) > k.config.MaxSwaps && !k.isUnlimited() { return types.ErrCanNotHaveMoreSwap.Wrapf("cannot initialize genesis state, there are more than %d swapped", k.config.MaxSwaps) } - - for _, swap := range genState.GetSwaps() { - if err := k.MakeSwap(ctx, swap); err != nil { - panic(err) - } - } - - for _, swapped := range genState.GetSwappeds() { - if err := swapped.ValidateBasic(); err != nil { - panic(err) - } - } return nil } diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index d86ff2d62f..5b51363e4f 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -19,16 +19,14 @@ type Keeper struct { config types.Config - AccountKeeper BankKeeper } -func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, config types.Config, ak AccountKeeper, bk BankKeeper) Keeper { +func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, config types.Config, bk BankKeeper) Keeper { return Keeper{ cdc, storeKey, config, - ak, bk, } } @@ -45,7 +43,17 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Co newAmount := fromCoinAmount.Amount.Mul(swap.SwapMultiple) newCoinAmount := sdk.NewCoin(toDenom, newAmount) - if err := k.checkSwapCap(ctx, swap, newCoinAmount); err != nil { + swapped, err := k.getSwapped(ctx, swap.GetFromDenom(), swap.GetToDenom()) + if err != nil { + return err + } + + updateSwapped, err := k.updateSwapped(ctx, swapped, fromCoinAmount, newCoinAmount) + if err != nil { + return err + } + + if err := k.checkSwapCap(swap, updateSwapped); err != nil { return err } @@ -65,10 +73,6 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Co return err } - if err := k.updateSwapped(ctx, fromCoinAmount, newCoinAmount); err != nil { - return err - } - if err := ctx.EventManager().EmitTypedEvent(&types.EventSwapCoins{ Address: addr.String(), FromCoinAmount: fromCoinAmount, @@ -160,36 +164,26 @@ func (k Keeper) getSwappableNewCoinAmount(ctx sdk.Context, fromDenom, toDenom st return sdk.NewCoin(toDenom, remainingAmount), nil } -func (k Keeper) updateSwapped(ctx sdk.Context, fromAmount, toAmount sdk.Coin) error { - prevSwapped, err := k.getSwapped(ctx, fromAmount.Denom, toAmount.Denom) - if err != nil { - return err - } - - updatedSwapped := &types.Swapped{ - FromCoinAmount: fromAmount.Add(prevSwapped.FromCoinAmount), - ToCoinAmount: toAmount.Add(prevSwapped.ToCoinAmount), +func (k Keeper) updateSwapped(ctx sdk.Context, curSwapped types.Swapped, fromAmount, toAmount sdk.Coin) (types.Swapped, error) { + updatedSwapped := types.Swapped{ + FromCoinAmount: fromAmount.Add(curSwapped.FromCoinAmount), + ToCoinAmount: toAmount.Add(curSwapped.ToCoinAmount), } key := swappedKey(fromAmount.Denom, toAmount.Denom) - bz, err := k.cdc.Marshal(updatedSwapped) + bz, err := k.cdc.Marshal(&updatedSwapped) if err != nil { - return err + return types.Swapped{}, err } store := ctx.KVStore(k.storeKey) store.Set(key, bz) - return nil + return updatedSwapped, nil } -func (k Keeper) checkSwapCap(ctx sdk.Context, swap types.Swap, newCoinAmount sdk.Coin) error { - swapped, err := k.getSwapped(ctx, swap.GetFromDenom(), swap.GetToDenom()) - if err != nil { - return err - } - +func (k Keeper) checkSwapCap(swap types.Swap, swapped types.Swapped) error { swapCap := swap.AmountCapForToDenom - if swapCap.LT(swapped.ToCoinAmount.Add(newCoinAmount).Amount) { + if swapCap.LT(swapped.ToCoinAmount.Amount) { return types.ErrExceedSwappableToCoinAmount } return nil diff --git a/x/fswap/module.go b/x/fswap/module.go index ff1d97e9d0..9687b4cff8 100644 --- a/x/fswap/module.go +++ b/x/fswap/module.go @@ -92,21 +92,18 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { type AppModule struct { AppModuleBasic - keeper keeper.Keeper - accountKeeper keeper.AccountKeeper - bankKeeper keeper.BankKeeper + keeper keeper.Keeper + bankKeeper keeper.BankKeeper } func NewAppModule( cdc codec.Codec, keeper keeper.Keeper, - accountKeeper keeper.AccountKeeper, bankKeeper keeper.BankKeeper, ) AppModule { return AppModule{ AppModuleBasic: NewAppModuleBasic(cdc), keeper: keeper, - accountKeeper: accountKeeper, bankKeeper: bankKeeper, } } diff --git a/x/fswap/proposal_handler_test.go b/x/fswap/proposal_handler_test.go index 974c81e0c4..5697d4575f 100644 --- a/x/fswap/proposal_handler_test.go +++ b/x/fswap/proposal_handler_test.go @@ -3,21 +3,32 @@ package fswap_test import ( "testing" + "github.com/stretchr/testify/require" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + + "github.com/Finschia/finschia-sdk/simapp" + sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/x/fswap" "github.com/Finschia/finschia-sdk/x/fswap/types" ) -func testProposal(swapInit types.Swap) *types.MakeSwapProposal { - return types.NewMakeSwapProposal("Test", "description", swapInit) +func testProposal(swap types.Swap) *types.MakeSwapProposal { + return types.NewMakeSwapProposal("Test", "description", swap) } func TestProposalHandlerPassed(t *testing.T) { - // app := simapp.Setup(false) - //ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - // - //fswapInit := types.NewSwapInit("aaa", "bbb", sdk.NewInt(1000), sdk.NewInt(100000)) - //tp := testProposal(fswapInit) - //hdlr := fswap.NewSwapInitHandler(app.FswapKeeper) - //require.NoError(t, hdlr(ctx, tp)) + app := simapp.Setup(false) + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + + swap := types.Swap{ + FromDenom: "aaa", + ToDenom: "bbb", + AmountCapForToDenom: sdk.NewInt(100), + SwapMultiple: sdk.NewInt(10), + } + tp := testProposal(swap) + hdlr := fswap.NewSwapHandler(app.FswapKeeper) + require.NoError(t, hdlr(ctx, tp)) // todo check contents } diff --git a/x/fswap/testutil/expected_keepers_mocks.go b/x/fswap/testutil/expected_keepers_mocks.go index a97e70920c..25acd349a0 100644 --- a/x/fswap/testutil/expected_keepers_mocks.go +++ b/x/fswap/testutil/expected_keepers_mocks.go @@ -11,43 +11,6 @@ import ( gomock "github.com/golang/mock/gomock" ) -// MockAccountKeeper is a mock of AccountKeeper interface. -type MockAccountKeeper struct { - ctrl *gomock.Controller - recorder *MockAccountKeeperMockRecorder -} - -// MockAccountKeeperMockRecorder is the mock recorder for MockAccountKeeper. -type MockAccountKeeperMockRecorder struct { - mock *MockAccountKeeper -} - -// NewMockAccountKeeper creates a new mock instance. -func NewMockAccountKeeper(ctrl *gomock.Controller) *MockAccountKeeper { - mock := &MockAccountKeeper{ctrl: ctrl} - mock.recorder = &MockAccountKeeperMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockAccountKeeper) EXPECT() *MockAccountKeeperMockRecorder { - return m.recorder -} - -// GetModuleAddress mocks base method. -func (m *MockAccountKeeper) GetModuleAddress(name string) types.AccAddress { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetModuleAddress", name) - ret0, _ := ret[0].(types.AccAddress) - return ret0 -} - -// GetModuleAddress indicates an expected call of GetModuleAddress. -func (mr *MockAccountKeeperMockRecorder) GetModuleAddress(name interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAddress", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAddress), name) -} - // MockBankKeeper is a mock of BankKeeper interface. type MockBankKeeper struct { ctrl *gomock.Controller @@ -99,32 +62,32 @@ func (mr *MockBankKeeperMockRecorder) GetBalance(ctx, addr, denom interface{}) * return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBalance", reflect.TypeOf((*MockBankKeeper)(nil).GetBalance), ctx, addr, denom) } -// HasBalance mocks base method. -func (m *MockBankKeeper) HasBalance(ctx types.Context, addr types.AccAddress, amt types.Coin) bool { +// MintCoins mocks base method. +func (m *MockBankKeeper) MintCoins(ctx types.Context, moduleName string, amt types.Coins) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "HasBalance", ctx, addr, amt) - ret0, _ := ret[0].(bool) + ret := m.ctrl.Call(m, "MintCoins", ctx, moduleName, amt) + ret0, _ := ret[0].(error) return ret0 } -// HasBalance indicates an expected call of HasBalance. -func (mr *MockBankKeeperMockRecorder) HasBalance(ctx, addr, amt interface{}) *gomock.Call { +// MintCoins indicates an expected call of MintCoins. +func (mr *MockBankKeeperMockRecorder) MintCoins(ctx, moduleName, amt interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HasBalance", reflect.TypeOf((*MockBankKeeper)(nil).HasBalance), ctx, addr, amt) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MintCoins", reflect.TypeOf((*MockBankKeeper)(nil).MintCoins), ctx, moduleName, amt) } -// MintCoins mocks base method. -func (m *MockBankKeeper) MintCoins(ctx types.Context, moduleName string, amt types.Coins) error { +// SendCoinsFromAccountToModule mocks base method. +func (m *MockBankKeeper) SendCoinsFromAccountToModule(ctx types.Context, senderAddr types.AccAddress, recipientModule string, amt types.Coins) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "MintCoins", ctx, moduleName, amt) + ret := m.ctrl.Call(m, "SendCoinsFromAccountToModule", ctx, senderAddr, recipientModule, amt) ret0, _ := ret[0].(error) return ret0 } -// MintCoins indicates an expected call of MintCoins. -func (mr *MockBankKeeperMockRecorder) MintCoins(ctx, moduleName, amt interface{}) *gomock.Call { +// SendCoinsFromAccountToModule indicates an expected call of SendCoinsFromAccountToModule. +func (mr *MockBankKeeperMockRecorder) SendCoinsFromAccountToModule(ctx, senderAddr, recipientModule, amt interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MintCoins", reflect.TypeOf((*MockBankKeeper)(nil).MintCoins), ctx, moduleName, amt) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendCoinsFromAccountToModule", reflect.TypeOf((*MockBankKeeper)(nil).SendCoinsFromAccountToModule), ctx, senderAddr, recipientModule, amt) } // SendCoinsFromModuleToAccount mocks base method. diff --git a/x/fswap/types/msgs.go b/x/fswap/types/msgs.go index 2d74d9a6e7..631f42ceb0 100644 --- a/x/fswap/types/msgs.go +++ b/x/fswap/types/msgs.go @@ -11,19 +11,19 @@ var _ sdk.Msg = &MsgSwap{} func (m *MsgSwap) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid address (%s)", err) + return sdkerrors.ErrInvalidAddress.Wrapf("Invalid address (%s)", err) } if !m.FromCoinAmount.IsValid() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.FromCoinAmount.String()) + return sdkerrors.ErrInvalidCoins.Wrap(m.FromCoinAmount.String()) } if !m.FromCoinAmount.IsPositive() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.FromCoinAmount.String()) + return sdkerrors.ErrInvalidCoins.Wrap(m.FromCoinAmount.String()) } if len(m.GetToDenom()) == 0 { - return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.FromCoinAmount.String()) + return sdkerrors.ErrInvalidRequest.Wrapf("invalid denom (%s)", m.GetToDenom()) } return nil @@ -44,15 +44,15 @@ var _ sdk.Msg = &MsgSwapAll{} func (m *MsgSwapAll) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid address (%s)", err) + return sdkerrors.ErrInvalidAddress.Wrapf("Invalid address (%s)", err) } if len(m.GetFromDenom()) == 0 { - return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "Invalid Denom") + return sdkerrors.ErrInvalidRequest.Wrapf("invalid denom (%s)", m.GetFromDenom()) } if len(m.GetToDenom()) == 0 { - return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "Invalid Denom") + return sdkerrors.ErrInvalidRequest.Wrapf("invalid denom (%s)", m.GetFromDenom()) } return nil From 17eeffd74233d1a5e7ba7eb93458799bafeefd1b Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 14:56:05 +0900 Subject: [PATCH 22/47] chore: fix some --- x/fswap/keeper/expected_keepers.go | 1 + x/fswap/keeper/genesis.go | 19 ++++++++++++++++++- x/fswap/keeper/keeper.go | 4 ++++ x/fswap/keeper/msg_server.go | 4 ++++ x/fswap/keeper/proposal.go | 10 ++++------ x/fswap/testutil/expected_keepers_mocks.go | 19 +++++++++++++++++++ 6 files changed, 50 insertions(+), 7 deletions(-) diff --git a/x/fswap/keeper/expected_keepers.go b/x/fswap/keeper/expected_keepers.go index 17466c69cd..b17177d0a7 100644 --- a/x/fswap/keeper/expected_keepers.go +++ b/x/fswap/keeper/expected_keepers.go @@ -11,5 +11,6 @@ type ( SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error + IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error } ) diff --git a/x/fswap/keeper/genesis.go b/x/fswap/keeper/genesis.go index 1630e4b667..8744e5098b 100644 --- a/x/fswap/keeper/genesis.go +++ b/x/fswap/keeper/genesis.go @@ -6,7 +6,7 @@ import ( ) // InitGenesis initializes the module's state from a provided genesis state. -func (k Keeper) InitGenesis(_ sdk.Context, genState *types.GenesisState) error { +func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error { if err := genState.Validate(); err != nil { return err } @@ -18,6 +18,23 @@ func (k Keeper) InitGenesis(_ sdk.Context, genState *types.GenesisState) error { if len(genState.GetSwappeds()) > k.config.MaxSwaps && !k.isUnlimited() { return types.ErrCanNotHaveMoreSwap.Wrapf("cannot initialize genesis state, there are more than %d swapped", k.config.MaxSwaps) } + + // SwapCount starts from 0, and get increased inside k.MakeSwap(ctx, swap) + if err := k.setSwapStats(ctx, types.SwapStats{SwapCount: 0}); err != nil { + return err + } + + for _, swap := range genState.GetSwaps() { + if err := k.MakeSwap(ctx, swap); err != nil { + panic(err) + } + } + + for _, swapped := range genState.GetSwappeds() { + if err := swapped.ValidateBasic(); err != nil { + panic(err) + } + } return nil } diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 5b51363e4f..064f562945 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -89,6 +89,10 @@ func (k Keeper) SwapAll(ctx sdk.Context, addr sdk.AccAddress, fromDenom, toDenom return sdkerrors.ErrInsufficientFunds } + if err := k.IsSendEnabledCoins(ctx, balance); err != nil { + return err + } + if err := k.Swap(ctx, addr, balance, toDenom); err != nil { return err } diff --git a/x/fswap/keeper/msg_server.go b/x/fswap/keeper/msg_server.go index b9484492e3..535ae7cf32 100644 --- a/x/fswap/keeper/msg_server.go +++ b/x/fswap/keeper/msg_server.go @@ -25,6 +25,10 @@ func (s MsgServer) Swap(ctx context.Context, req *types.MsgSwap) (*types.MsgSwap return nil, err } + if err := s.keeper.IsSendEnabledCoins(c, req.GetFromCoinAmount()); err != nil { + return &types.MsgSwapResponse{}, err + } + if err := s.keeper.Swap(c, from, req.GetFromCoinAmount(), req.GetToDenom()); err != nil { return nil, err } diff --git a/x/fswap/keeper/proposal.go b/x/fswap/keeper/proposal.go index fefbc85966..ec962014cd 100644 --- a/x/fswap/keeper/proposal.go +++ b/x/fswap/keeper/proposal.go @@ -13,10 +13,6 @@ func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap) error { } isNewSwap := true - if _, err := k.getSwap(ctx, swap.ToDenom, swap.FromDenom); err == nil { - return errors.ErrInvalidRequest.Wrap("there is already a swap in reverse way, only one way swap allowed") - } - if _, err := k.getSwap(ctx, swap.FromDenom, swap.ToDenom); err == nil { isNewSwap = false } @@ -25,8 +21,10 @@ func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap) error { return errors.ErrInvalidRequest.Wrap("update existing swap not allowed") } - if err := k.increaseSwapCount(ctx); err != nil { - return err + if isNewSwap { + if err := k.increaseSwapCount(ctx); err != nil { + return err + } } key := swapKey(swap.FromDenom, swap.ToDenom) diff --git a/x/fswap/testutil/expected_keepers_mocks.go b/x/fswap/testutil/expected_keepers_mocks.go index 25acd349a0..6d6b45df2c 100644 --- a/x/fswap/testutil/expected_keepers_mocks.go +++ b/x/fswap/testutil/expected_keepers_mocks.go @@ -62,6 +62,25 @@ func (mr *MockBankKeeperMockRecorder) GetBalance(ctx, addr, denom interface{}) * return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBalance", reflect.TypeOf((*MockBankKeeper)(nil).GetBalance), ctx, addr, denom) } +// IsSendEnabledCoins mocks base method. +func (m *MockBankKeeper) IsSendEnabledCoins(ctx types.Context, coins ...types.Coin) error { + m.ctrl.T.Helper() + varargs := []interface{}{ctx} + for _, a := range coins { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "IsSendEnabledCoins", varargs...) + ret0, _ := ret[0].(error) + return ret0 +} + +// IsSendEnabledCoins indicates an expected call of IsSendEnabledCoins. +func (mr *MockBankKeeperMockRecorder) IsSendEnabledCoins(ctx interface{}, coins ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{ctx}, coins...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsSendEnabledCoins", reflect.TypeOf((*MockBankKeeper)(nil).IsSendEnabledCoins), varargs...) +} + // MintCoins mocks base method. func (m *MockBankKeeper) MintCoins(ctx types.Context, moduleName string, amt types.Coins) error { m.ctrl.T.Helper() From cb736addf8048d72dc620a339232d2b38e0df057 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 15:28:50 +0900 Subject: [PATCH 23/47] chore: fix for initGenesis --- x/fswap/keeper/genesis.go | 11 ++++++----- x/fswap/keeper/keeper.go | 4 ++-- x/fswap/keeper/proposal.go | 28 ++++++++++++++++++---------- x/fswap/types/genesis.go | 9 +++++++++ 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/x/fswap/keeper/genesis.go b/x/fswap/keeper/genesis.go index 8744e5098b..c444785f99 100644 --- a/x/fswap/keeper/genesis.go +++ b/x/fswap/keeper/genesis.go @@ -19,22 +19,23 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error return types.ErrCanNotHaveMoreSwap.Wrapf("cannot initialize genesis state, there are more than %d swapped", k.config.MaxSwaps) } - // SwapCount starts from 0, and get increased inside k.MakeSwap(ctx, swap) - if err := k.setSwapStats(ctx, types.SwapStats{SwapCount: 0}); err != nil { + if err := k.setSwapStats(ctx, genState.GetSwapStats()); err != nil { return err } for _, swap := range genState.GetSwaps() { - if err := k.MakeSwap(ctx, swap); err != nil { + if err := k.setSwap(ctx, swap); err != nil { panic(err) } } for _, swapped := range genState.GetSwappeds() { - if err := swapped.ValidateBasic(); err != nil { - panic(err) + err := k.setSwapped(ctx, swapped) + if err != nil { + return err } } + return nil } diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 064f562945..8f3c7785ae 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -139,8 +139,8 @@ func (k Keeper) getSwapped(ctx sdk.Context, fromDenom, toDenom string) (types.Sw return swapped, nil } -func (k Keeper) setSwapped(ctx sdk.Context, fromDenom, toDenom string, swapped types.Swapped) error { - key := swappedKey(fromDenom, toDenom) +func (k Keeper) setSwapped(ctx sdk.Context, swapped types.Swapped) error { + key := swappedKey(swapped.FromCoinAmount.Denom, swapped.ToCoinAmount.Denom) bz, err := k.cdc.Marshal(&swapped) if err != nil { return err diff --git a/x/fswap/keeper/proposal.go b/x/fswap/keeper/proposal.go index ec962014cd..89a1733062 100644 --- a/x/fswap/keeper/proposal.go +++ b/x/fswap/keeper/proposal.go @@ -27,15 +27,6 @@ func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap) error { } } - key := swapKey(swap.FromDenom, swap.ToDenom) - bz, err := k.cdc.Marshal(&swap) - if err != nil { - return err - } - - store := ctx.KVStore(k.storeKey) - store.Set(key, bz) - if isNewSwap { swapped := types.Swapped{ FromCoinAmount: sdk.Coin{ @@ -47,10 +38,15 @@ func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap) error { Amount: sdk.ZeroInt(), }, } - if err := k.setSwapped(ctx, swap.GetFromDenom(), swap.GetToDenom(), swapped); err != nil { + if err := k.setSwapped(ctx, swapped); err != nil { return err } } + + err := k.setSwap(ctx, swap) + if err != nil { + return err + } return nil } @@ -72,6 +68,18 @@ func (k Keeper) increaseSwapCount(ctx sdk.Context) error { return nil } +func (k Keeper) setSwap(ctx sdk.Context, swap types.Swap) error { + key := swapKey(swap.FromDenom, swap.ToDenom) + bz, err := k.cdc.Marshal(&swap) + if err != nil { + return err + } + + store := ctx.KVStore(k.storeKey) + store.Set(key, bz) + return nil +} + func (k Keeper) getSwap(ctx sdk.Context, fromDenom, toDenom string) (types.Swap, error) { store := ctx.KVStore(k.storeKey) key := swapKey(fromDenom, toDenom) diff --git a/x/fswap/types/genesis.go b/x/fswap/types/genesis.go index 2c2d4c2ab0..08c1d79338 100644 --- a/x/fswap/types/genesis.go +++ b/x/fswap/types/genesis.go @@ -26,5 +26,14 @@ func (gs *GenesisState) Validate() error { return err } } + + if len(gs.GetSwaps()) != len(gs.GetSwappeds()) { + return ErrInvalidState.Wrap("number of swaps does not match number of Swappeds") + } + + if len(gs.GetSwaps()) != int(gs.GetSwapStats().SwapCount) { + return ErrInvalidState.Wrap("number of swaps does not match swap count in SwapStats") + } + return nil } From f48ffbc99e74542634bb0467375ac86f28156e53 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 16:09:40 +0900 Subject: [PATCH 24/47] chore: apply feedbacks --- x/fswap/keeper/genesis.go | 2 +- x/fswap/keeper/keeper.go | 17 ------------ x/fswap/keeper/keeper_test.go | 46 ------------------------------- x/fswap/keeper/msg_server.go | 12 +++++++- x/fswap/keeper/msg_server_test.go | 4 +-- x/fswap/types/msgs.go | 6 ++-- 6 files changed, 17 insertions(+), 70 deletions(-) diff --git a/x/fswap/keeper/genesis.go b/x/fswap/keeper/genesis.go index c444785f99..d27c80785a 100644 --- a/x/fswap/keeper/genesis.go +++ b/x/fswap/keeper/genesis.go @@ -12,7 +12,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error } if len(genState.GetSwaps()) > k.config.MaxSwaps && !k.isUnlimited() { - return types.ErrCanNotHaveMoreSwap.Wrapf("cannot initialize genesis state, there are more than %d swapInits", k.config.MaxSwaps) + return types.ErrCanNotHaveMoreSwap.Wrapf("cannot initialize genesis state, there are more than %d swaps", k.config.MaxSwaps) } if len(genState.GetSwappeds()) > k.config.MaxSwaps && !k.isUnlimited() { diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 8f3c7785ae..9fe4258a28 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -9,7 +9,6 @@ import ( "github.com/Finschia/finschia-sdk/store/prefix" storetypes "github.com/Finschia/finschia-sdk/store/types" sdk "github.com/Finschia/finschia-sdk/types" - sdkerrors "github.com/Finschia/finschia-sdk/types/errors" "github.com/Finschia/finschia-sdk/x/fswap/types" ) @@ -83,22 +82,6 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Co return nil } -func (k Keeper) SwapAll(ctx sdk.Context, addr sdk.AccAddress, fromDenom, toDenom string) error { - balance := k.GetBalance(ctx, addr, fromDenom) - if balance.IsZero() { - return sdkerrors.ErrInsufficientFunds - } - - if err := k.IsSendEnabledCoins(ctx, balance); err != nil { - return err - } - - if err := k.Swap(ctx, addr, balance, toDenom); err != nil { - return err - } - return nil -} - func (k Keeper) getAllSwapped(ctx sdk.Context) []types.Swapped { swappedSlice := []types.Swapped{} k.iterateAllSwapped(ctx, func(swapped types.Swapped) bool { diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 3af3352dd2..a0484170c2 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -157,49 +157,3 @@ func (s *KeeperTestSuite) TestSwap() { }) } } - -func (s *KeeperTestSuite) TestSwapAll() { - testCases := map[string]struct { - from sdk.AccAddress - fromDenom string - toDenom string - expectedBalanceWithoutMultiply sdk.Int - shouldThrowError bool - expectedError error - }{ - "account holding from coin": { - s.accWithFromCoin, - s.swap.GetFromDenom(), - s.swap.GetToDenom(), - s.initBalance, - false, - nil, - }, - "account holding to coin only": { - s.accWithToCoin, - s.swap.GetFromDenom(), - s.swap.GetToDenom(), - s.initBalance, - true, - sdkerrors.ErrInsufficientFunds, - }, - } - for name, tc := range testCases { - s.Run(name, func() { - ctx, _ := s.ctx.CacheContext() - err := s.keeper.MakeSwap(ctx, s.swap) - s.Require().NoError(err) - - err = s.keeper.SwapAll(ctx, tc.from, tc.fromDenom, tc.toDenom) - if tc.shouldThrowError { - s.Require().ErrorIs(err, tc.expectedError) - return - } - s.Require().NoError(err) - - actualAmount := s.keeper.GetBalance(ctx, tc.from, s.swap.GetToDenom()).Amount - expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swap.SwapMultiple) - s.Require().Equal(expectedAmount, actualAmount) - }) - } -} diff --git a/x/fswap/keeper/msg_server.go b/x/fswap/keeper/msg_server.go index 535ae7cf32..1f3086fc4f 100644 --- a/x/fswap/keeper/msg_server.go +++ b/x/fswap/keeper/msg_server.go @@ -4,6 +4,7 @@ import ( "context" sdk "github.com/Finschia/finschia-sdk/types" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" "github.com/Finschia/finschia-sdk/x/fswap/types" ) @@ -44,7 +45,16 @@ func (s MsgServer) SwapAll(ctx context.Context, req *types.MsgSwapAll) (*types.M return nil, err } - if err := s.keeper.SwapAll(c, from, req.GetFromDenom(), req.GetToDenom()); err != nil { + balance := s.keeper.GetBalance(c, from, req.FromDenom) + if balance.IsZero() { + return nil, sdkerrors.ErrInsufficientFunds + } + + if err := s.keeper.IsSendEnabledCoins(c, balance); err != nil { + return nil, err + } + + if err := s.keeper.Swap(c, from, balance, req.GetToDenom()); err != nil { return nil, err } diff --git a/x/fswap/keeper/msg_server_test.go b/x/fswap/keeper/msg_server_test.go index 499ccff16f..bb4aa7a528 100644 --- a/x/fswap/keeper/msg_server_test.go +++ b/x/fswap/keeper/msg_server_test.go @@ -33,7 +33,7 @@ func (s *KeeperTestSuite) TestMsgSwap() { false, nil, }, - "account holding new coin only": { + "account holding to-coin only": { &types.MsgSwap{ FromAddress: s.accWithToCoin.String(), FromCoinAmount: sdk.NewCoin(s.swap.GetFromDenom(), sdk.NewInt(100)), @@ -84,7 +84,7 @@ func (s *KeeperTestSuite) TestMsgSwapAll() { false, nil, }, - "account holding new coin only": { + "account holding to-coin only": { &types.MsgSwapAll{ FromAddress: s.accWithToCoin.String(), FromDenom: s.swap.GetFromDenom(), diff --git a/x/fswap/types/msgs.go b/x/fswap/types/msgs.go index 631f42ceb0..038d8baebd 100644 --- a/x/fswap/types/msgs.go +++ b/x/fswap/types/msgs.go @@ -23,7 +23,7 @@ func (m *MsgSwap) ValidateBasic() error { } if len(m.GetToDenom()) == 0 { - return sdkerrors.ErrInvalidRequest.Wrapf("invalid denom (%s)", m.GetToDenom()) + return sdkerrors.ErrInvalidRequest.Wrap("invalid denom (empty denom)") } return nil @@ -48,11 +48,11 @@ func (m *MsgSwapAll) ValidateBasic() error { } if len(m.GetFromDenom()) == 0 { - return sdkerrors.ErrInvalidRequest.Wrapf("invalid denom (%s)", m.GetFromDenom()) + return sdkerrors.ErrInvalidRequest.Wrap("invalid denom (empty denom)") } if len(m.GetToDenom()) == 0 { - return sdkerrors.ErrInvalidRequest.Wrapf("invalid denom (%s)", m.GetFromDenom()) + return sdkerrors.ErrInvalidRequest.Wrap("invalid denom (empty denom)") } return nil From 44caeee5e39f08d5effff8ee5436887ae161d5a0 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 16:24:29 +0900 Subject: [PATCH 25/47] chore: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fe176f455..823a3d7f3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/fswap) [\#1339](https://github.com/Finschia/finschia-sdk/pull/1339) Implement fswap module's genesis * (x/fbridge) [\#1340](https://github.com/Finschia/finschia-sdk/pull/1340) Initialize fbridge module * (x/fbridge) [\#1347](https://github.com/Finschia/finschia-sdk/pull/1347) Implement bridge transfer feature (sending side) +* (x/fswap) [\#1345](https://github.com/Finschia/finschia-sdk/pull/1345) Implement fswap's basic functionality(MsgSwap, MsgSwapAll, Query, Proposal) ### Improvements * (docs) [\#1120](https://github.com/Finschia/finschia-sdk/pull/1120) Update links in x/foundation README.md From 15ed1c9b15f349e0806d1d26f225cadd6adffa85 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 16:30:38 +0900 Subject: [PATCH 26/47] chore: remove redundant logic --- x/fswap/keeper/proposal.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/x/fswap/keeper/proposal.go b/x/fswap/keeper/proposal.go index 89a1733062..13c6bcf5d8 100644 --- a/x/fswap/keeper/proposal.go +++ b/x/fswap/keeper/proposal.go @@ -8,10 +8,6 @@ import ( ) func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap) error { - if err := swap.ValidateBasic(); err != nil { - return err - } - isNewSwap := true if _, err := k.getSwap(ctx, swap.FromDenom, swap.ToDenom); err == nil { isNewSwap = false From 607d94a2b27c7aa0604f132e8c69d8e9896424e1 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 16:38:09 +0900 Subject: [PATCH 27/47] chore: update error doc --- x/ERRORS.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/x/ERRORS.md b/x/ERRORS.md index 85d489f849..dcf56afb65 100644 --- a/x/ERRORS.md +++ b/x/ERRORS.md @@ -177,9 +177,10 @@ |Error Name|Codespace|Code|Description| |:-|:-|:-|:-| -|ErrFswapInitNotFound|fswap|1100|fswap init does not exist| -|ErrSwappedNotFound|fswap|1101|swapped does not exist| -|ErrExceedSwappable|fswap|1102|exceed swappable coin amount| +|ErrInvalidState|fswap|2|swap module invalid state| +|ErrCanNotHaveMoreSwap|fswap|3|no more swap allowed| +|ErrSwappedNotFound|fswap|4|swapped does not exist| +|ErrExceedSwappableToCoinAmount|fswap|5|exceed swappable to-coin amount| >You can also find detailed information in the following Errors.go files: * [fswap/types/errors.go](fswap/types/errors.go) From a0ca4d3c449a8b8286add0efa11d1a0077403e4d Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 18:41:42 +0900 Subject: [PATCH 28/47] chore: update proto and logic to accept bank.Metadata --- docs/core/proto-docs.md | 1 + proto/lbm/fswap/v1/fswap.proto | 10 +- x/fswap/client/cli/tx.go | 192 +++++++++++++-------- x/fswap/client/proposal_handler.go | 2 +- x/fswap/handler.go | 2 +- x/fswap/keeper/expected_keepers.go | 4 +- x/fswap/keeper/keeper_test.go | 17 +- x/fswap/keeper/msg_server_test.go | 6 +- x/fswap/keeper/proposal.go | 5 +- x/fswap/keeper/proposal_test.go | 39 ++++- x/fswap/proposal_handler_test.go | 3 +- x/fswap/testutil/expected_keepers_mocks.go | 13 ++ x/fswap/types/fswap.pb.go | 171 ++++++++++-------- x/fswap/types/proposal.go | 5 +- 14 files changed, 308 insertions(+), 162 deletions(-) diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index cf629720ff..f43431498f 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -13930,6 +13930,7 @@ From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk base | `title` | [string](#string) | | | | `description` | [string](#string) | | | | `swap` | [Swap](#lbm.fswap.v1.Swap) | | | +| `to_denom_metadata` | [cosmos.bank.v1beta1.Metadata](#cosmos.bank.v1beta1.Metadata) | | | diff --git a/proto/lbm/fswap/v1/fswap.proto b/proto/lbm/fswap/v1/fswap.proto index 06b8ee3e83..26de0e21c2 100644 --- a/proto/lbm/fswap/v1/fswap.proto +++ b/proto/lbm/fswap/v1/fswap.proto @@ -5,6 +5,7 @@ option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types"; import "gogoproto/gogo.proto"; import "cosmos/base/v1beta1/coin.proto"; +import "cosmos/bank/v1beta1/bank.proto"; message Swap { option (gogoproto.equal) = true; @@ -27,12 +28,13 @@ message SwapStats { // From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 message MakeSwapProposal { - option (gogoproto.equal) = true; option (gogoproto.goproto_stringer) = false; - string title = 1; - string description = 2; - Swap swap = 3 [(gogoproto.nullable) = false]; + string title = 1; + string description = 2; + Swap swap = 3 [(gogoproto.nullable) = false]; + cosmos.bank.v1beta1.Metadata to_denom_metadata = 4 + [(gogoproto.moretags) = "yaml:\"denom_metadata\"", (gogoproto.nullable) = false]; } message Swapped { diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index bd0f2915b1..cbaafc5147 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -1,29 +1,29 @@ package cli import ( + "encoding/json" "fmt" - "time" "github.com/spf13/cobra" "github.com/Finschia/finschia-sdk/client" "github.com/Finschia/finschia-sdk/client/flags" "github.com/Finschia/finschia-sdk/client/tx" + "github.com/Finschia/finschia-sdk/codec" sdk "github.com/Finschia/finschia-sdk/types" + bank "github.com/Finschia/finschia-sdk/x/bank/types" "github.com/Finschia/finschia-sdk/x/fswap/types" - "github.com/Finschia/finschia-sdk/x/gov/client/cli" + govcli "github.com/Finschia/finschia-sdk/x/gov/client/cli" gov "github.com/Finschia/finschia-sdk/x/gov/types" ) const ( - FlagFromDenom = "from-denom" - FlagToDenom = "to-denom" - FlagAmountLimit = "amount-limit" - FlagSwapRate = "swap-rate" + FlagFromDenom = "from-denom" + FlagToDenom = "to-denom" + FlagAmountCapForToDenom = "to-coin-amount-cap" + FlagSwapMultiple = "swap-multiple" ) -var DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds()) - // GetTxCmd returns the transaction commands for this module func GetTxCmd() *cobra.Command { cmd := &cobra.Command{ @@ -43,26 +43,31 @@ func GetTxCmd() *cobra.Command { func CmdTxMsgSwap() *cobra.Command { cmd := &cobra.Command{ - Use: "swap [address] [fnsa_amount]", - Short: "swap amounts of old coin to new coin. Note, the'--from' flag is ignored as it is implied from [address].", - Args: cobra.ExactArgs(2), + Use: "swap [from] [from_coin_amount] [to_denom]", + Short: "swap amount of from-coin to to-coin", + Args: cobra.ExactArgs(3), RunE: func(cmd *cobra.Command, args []string) error { - if err := cmd.Flags().Set(flags.FlagFrom, args[0]); err != nil { + from := args[0] + if err := cmd.Flags().Set(flags.FlagFrom, from); err != nil { return err } + clientCtx, err := client.GetClientTxContext(cmd) if err != nil { return err } - coin, err := sdk.ParseCoinNormalized(args[2]) + amount, err := sdk.ParseCoinNormalized(args[1]) if err != nil { return err } + toDenom := args[2] + msg := &types.MsgSwap{ - FromAddress: clientCtx.GetFromAddress().String(), - FromCoinAmount: coin, + FromAddress: from, + FromCoinAmount: amount, + ToDenom: toDenom, } if err := msg.ValidateBasic(); err != nil { return err @@ -77,24 +82,31 @@ func CmdTxMsgSwap() *cobra.Command { func CmdTxMsgSwapAll() *cobra.Command { cmd := &cobra.Command{ - Use: "swap-all [address]", - Short: "swap all the old coins. Note, the'--from' flag is ignored as it is implied from [address].", - Args: cobra.ExactArgs(1), + Use: "swap-all [from_address] [from_denom] [to_denom]", + Short: "swap all the from-coin to to-coin", + Args: cobra.ExactArgs(3), RunE: func(cmd *cobra.Command, args []string) error { - if err := cmd.Flags().Set(flags.FlagFrom, args[0]); err != nil { + from := args[0] + if err := cmd.Flags().Set(flags.FlagFrom, from); err != nil { return err } + clientCtx, err := client.GetClientTxContext(cmd) if err != nil { return err } + fromDenom := args[1] + toDenom := args[2] msg := &types.MsgSwapAll{ FromAddress: clientCtx.GetFromAddress().String(), + FromDenom: fromDenom, + ToDenom: toDenom, } if err := msg.ValidateBasic(); err != nil { return err } + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) }, } @@ -103,27 +115,95 @@ func CmdTxMsgSwapAll() *cobra.Command { return cmd } -// NewCmdSwapInitProposal implements a command handler for submitting a swap init proposal transaction. -func NewCmdSwapInitProposal() *cobra.Command { +// NewCmdMakeSwapProposal implements a command handler for submitting a swap init proposal transaction. +func NewCmdMakeSwapProposal() *cobra.Command { cmd := &cobra.Command{ - Use: "swap-init [flags]", - Args: cobra.ExactArgs(0), + Use: "make-swap [messages-json]", + Args: cobra.ExactArgs(1), Short: "todo", - Long: "todo", + Long: ` +Parameters: + messages-json: messages in json format that will be executed if the proposal is accepted. + +Example of the content of messages-json: + +{ + "metadata": { + "description": "the base coin of Finschia mainnet", + "denom_units": [ + { + "denom": "cony", + "exponent": 0, + "aliases": [ + "microfinschia" + ] + }, + { + "denom": "finschia", + "exponent": 6, + "aliases": [] + } + ], + "base": "cony", + "display": "finschia", + "name": "FINSCHIA", + "symbol": "FNSA" + } +} +`, RunE: func(cmd *cobra.Command, args []string) error { clientCtx, err := client.GetClientTxContext(cmd) if err != nil { return err } - // need confirm parse with flags or parse with args - contents, err := parseArgsToContent(cmd) + + from := clientCtx.GetFromAddress() + + title, err := cmd.Flags().GetString(govcli.FlagTitle) if err != nil { return err } - from := clientCtx.GetFromAddress() + description, err := cmd.Flags().GetString(govcli.FlagDescription) + if err != nil { + return err + } + + fromDenom, err := cmd.Flags().GetString(FlagFromDenom) + if err != nil { + return err + } + + toDenom, err := cmd.Flags().GetString(FlagToDenom) + if err != nil { + return err + } + + amountCap, err := cmd.Flags().GetInt64(FlagAmountCapForToDenom) + if err != nil { + return err + } - depositStr, err := cmd.Flags().GetString(cli.FlagDeposit) + swapMultiple, err := cmd.Flags().GetInt64(FlagSwapMultiple) + if err != nil { + return err + } + + swap := types.Swap{ + FromDenom: fromDenom, + ToDenom: toDenom, + AmountCapForToDenom: sdk.NewInt(amountCap), + SwapMultiple: sdk.NewInt(swapMultiple), + } + + toDenomMetadata, err := parseToDenomMetadata(clientCtx.Codec, args[0]) + if err != nil { + return err + } + + content := types.NewMakeSwapProposal(title, description, swap, toDenomMetadata) + + depositStr, err := cmd.Flags().GetString(govcli.FlagDeposit) if err != nil { return err } @@ -132,7 +212,7 @@ func NewCmdSwapInitProposal() *cobra.Command { return err } - msg, err := gov.NewMsgSubmitProposal(contents, deposit, from) + msg, err := gov.NewMsgSubmitProposal(content, deposit, from) if err != nil { return err } @@ -141,55 +221,27 @@ func NewCmdSwapInitProposal() *cobra.Command { }, } - cmd.Flags().String(cli.FlagTitle, "", "title of proposal") - cmd.Flags().String(cli.FlagDescription, "", "description of proposal") - cmd.Flags().String(cli.FlagDeposit, "", "deposit of proposal") + cmd.Flags().String(govcli.FlagTitle, "", "title of proposal") + cmd.Flags().String(govcli.FlagDescription, "", "description of proposal") + cmd.Flags().String(govcli.FlagDeposit, "", "deposit of proposal") cmd.Flags().String(FlagFromDenom, "", "cony") cmd.Flags().String(FlagToDenom, "", "PDT") - cmd.Flags().Int64(FlagAmountLimit, 0, "tbd") - cmd.Flags().Int64(FlagSwapRate, 0, "tbd") + cmd.Flags().Int64(FlagAmountCapForToDenom, 0, "tbd") + cmd.Flags().Int64(FlagSwapMultiple, 0, "tbd") return cmd } -func parseArgsToContent(cmd *cobra.Command) (gov.Content, error) { - title, err := cmd.Flags().GetString(cli.FlagTitle) - if err != nil { - return nil, err +func parseToDenomMetadata(cdc codec.Codec, jsonDenomMetadata string) (bank.Metadata, error) { + cliMsg := json.RawMessage{} + if err := json.Unmarshal([]byte(jsonDenomMetadata), &cliMsg); err != nil { + return bank.Metadata{}, err } - description, err := cmd.Flags().GetString(cli.FlagDescription) + metadata := bank.Metadata{} + err := cdc.UnmarshalInterfaceJSON(cliMsg, &metadata) if err != nil { - return nil, err + return bank.Metadata{}, err } - - from_denom, err := cmd.Flags().GetString(FlagFromDenom) - if err != nil { - return nil, err - } - - to_denom, err := cmd.Flags().GetString(FlagToDenom) - if err != nil { - return nil, err - } - - amount_limit, err := cmd.Flags().GetInt64(FlagAmountLimit) - if err != nil { - return nil, err - } - - swap_rate, err := cmd.Flags().GetInt64(FlagSwapRate) - if err != nil { - return nil, err - } - - swapInit := types.Swap{ - FromDenom: from_denom, - ToDenom: to_denom, - AmountCapForToDenom: sdk.NewInt(amount_limit), - SwapMultiple: sdk.NewInt(swap_rate), - } - - content := types.NewMakeSwapProposal(title, description, swapInit) - return content, nil + return metadata, nil } diff --git a/x/fswap/client/proposal_handler.go b/x/fswap/client/proposal_handler.go index 78ef57b56d..72c073b733 100644 --- a/x/fswap/client/proposal_handler.go +++ b/x/fswap/client/proposal_handler.go @@ -5,4 +5,4 @@ import ( govclient "github.com/Finschia/finschia-sdk/x/gov/client" ) -var ProposalHandler = govclient.NewProposalHandler(cli.NewCmdSwapInitProposal) +var ProposalHandler = govclient.NewProposalHandler(cli.NewCmdMakeSwapProposal) diff --git a/x/fswap/handler.go b/x/fswap/handler.go index 401fe29332..7bae485c7e 100644 --- a/x/fswap/handler.go +++ b/x/fswap/handler.go @@ -23,5 +23,5 @@ func NewSwapHandler(k keeper.Keeper) govtypes.Handler { } func handleMakeSwapProposal(ctx sdk.Context, k keeper.Keeper, p *types.MakeSwapProposal) error { - return k.MakeSwap(ctx, p.Swap) + return k.MakeSwap(ctx, p.Swap, p.ToDenomMetadata) } diff --git a/x/fswap/keeper/expected_keepers.go b/x/fswap/keeper/expected_keepers.go index b17177d0a7..aea3330379 100644 --- a/x/fswap/keeper/expected_keepers.go +++ b/x/fswap/keeper/expected_keepers.go @@ -2,6 +2,7 @@ package keeper import ( sdk "github.com/Finschia/finschia-sdk/types" + banktypes "github.com/Finschia/finschia-sdk/x/bank/types" ) type ( @@ -9,8 +10,9 @@ type ( GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error + IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error + SetDenomMetaData(ctx sdk.Context, denomMetaData banktypes.Metadata) MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error - IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error } ) diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index a0484170c2..ba9956ed84 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -12,6 +12,7 @@ import ( "github.com/Finschia/finschia-sdk/testutil/testdata" sdk "github.com/Finschia/finschia-sdk/types" sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + bank "github.com/Finschia/finschia-sdk/x/bank/types" "github.com/Finschia/finschia-sdk/x/fswap/keeper" "github.com/Finschia/finschia-sdk/x/fswap/types" minttypes "github.com/Finschia/finschia-sdk/x/mint/types" @@ -30,7 +31,8 @@ type KeeperTestSuite struct { accWithToCoin sdk.AccAddress initBalance sdk.Int - swap types.Swap + swap types.Swap + toDenomMetadata bank.Metadata } func (s *KeeperTestSuite) createRandomAccounts(n int) []sdk.AccAddress { @@ -70,6 +72,16 @@ func (s *KeeperTestSuite) SetupTest() { AmountCapForToDenom: s.initBalance.Mul(sdk.NewInt(1000).Mul(sdk.NewInt(numAcc))), SwapMultiple: sdk.NewInt(1000), } + s.toDenomMetadata = bank.Metadata{ + Description: "This is metadata for to-coin", + DenomUnits: []*bank.DenomUnit{ + {Denom: s.swap.ToDenom, Exponent: 0}, + }, + Base: "dummy", + Display: "dummycoin", + Name: "DUMMY", + Symbol: "DUM", + } s.createAccountsWithInitBalance(app) app.AccountKeeper.GetModuleAccount(s.ctx, types.ModuleName) } @@ -141,7 +153,8 @@ func (s *KeeperTestSuite) TestSwap() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.MakeSwap(ctx, s.swap) + dontCareForThisTest := bank.Metadata{Base: "dummy"} + err := s.keeper.MakeSwap(ctx, s.swap, dontCareForThisTest) s.Require().NoError(err) err = s.keeper.Swap(ctx, tc.from, tc.amountToSwap, tc.toDenom) diff --git a/x/fswap/keeper/msg_server_test.go b/x/fswap/keeper/msg_server_test.go index bb4aa7a528..b035bb53da 100644 --- a/x/fswap/keeper/msg_server_test.go +++ b/x/fswap/keeper/msg_server_test.go @@ -3,6 +3,7 @@ package keeper_test import ( sdk "github.com/Finschia/finschia-sdk/types" sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + bank "github.com/Finschia/finschia-sdk/x/bank/types" "github.com/Finschia/finschia-sdk/x/fswap/types" ) @@ -47,7 +48,8 @@ func (s *KeeperTestSuite) TestMsgSwap() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.MakeSwap(ctx, s.swap) + dontCareForThisTestCase := bank.Metadata{Base: "dummy"} + err := s.keeper.MakeSwap(ctx, s.swap, dontCareForThisTestCase) s.Require().NoError(err) swapResponse, err := s.msgServer.Swap(sdk.WrapSDKContext(ctx), tc.request) @@ -98,7 +100,7 @@ func (s *KeeperTestSuite) TestMsgSwapAll() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.MakeSwap(ctx, s.swap) + err := s.keeper.MakeSwap(ctx, s.swap, s.toDenomMetadata) s.Require().NoError(err) swapResponse, err := s.msgServer.SwapAll(sdk.WrapSDKContext(ctx), tc.request) diff --git a/x/fswap/keeper/proposal.go b/x/fswap/keeper/proposal.go index 13c6bcf5d8..67f95af47f 100644 --- a/x/fswap/keeper/proposal.go +++ b/x/fswap/keeper/proposal.go @@ -4,10 +4,11 @@ import ( "github.com/Finschia/finschia-sdk/store/prefix" sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/types/errors" + bank "github.com/Finschia/finschia-sdk/x/bank/types" "github.com/Finschia/finschia-sdk/x/fswap/types" ) -func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap) error { +func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank.Metadata) error { isNewSwap := true if _, err := k.getSwap(ctx, swap.FromDenom, swap.ToDenom); err == nil { isNewSwap = false @@ -43,6 +44,8 @@ func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap) error { if err != nil { return err } + + k.SetDenomMetaData(ctx, toDenomMetadata) return nil } diff --git a/x/fswap/keeper/proposal_test.go b/x/fswap/keeper/proposal_test.go index 9dbe91d0e0..84f11c3555 100644 --- a/x/fswap/keeper/proposal_test.go +++ b/x/fswap/keeper/proposal_test.go @@ -8,11 +8,41 @@ import ( func (s *KeeperTestSuite) TestMakeSwapProposal() { testCases := map[string]struct { - req types.Swap + swap types.Swap shouldThrowError bool expectedError error }{ - "valid getSwap": { + "valid": { + types.Swap{ + FromDenom: "fromD", + ToDenom: "toD", + AmountCapForToDenom: sdk.OneInt(), + SwapMultiple: sdk.OneInt(), + }, + false, + nil, + }, + } + for name, tc := range testCases { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() + err := s.keeper.MakeSwap(ctx, tc.swap, s.toDenomMetadata) + if tc.shouldThrowError { + s.Require().ErrorIs(err, tc.expectedError) + return + } + s.Require().NoError(err) + }) + } +} + +func (s *KeeperTestSuite) TestSwapValidateBasic() { + testCases := map[string]struct { + swap types.Swap + shouldThrowError bool + expectedError error + }{ + "valid": { types.Swap{ FromDenom: "fromD", ToDenom: "toD", @@ -52,7 +82,7 @@ func (s *KeeperTestSuite) TestMakeSwapProposal() { true, sdkerrors.ErrInvalidRequest, }, - "invalid zero swap-rate": { + "invalid zero swap-multiple": { types.Swap{ FromDenom: "fromD", ToDenom: "toD", @@ -75,8 +105,7 @@ func (s *KeeperTestSuite) TestMakeSwapProposal() { } for name, tc := range testCases { s.Run(name, func() { - ctx, _ := s.ctx.CacheContext() - err := s.keeper.MakeSwap(ctx, tc.req) + err := tc.swap.ValidateBasic() if tc.shouldThrowError { s.Require().ErrorIs(err, tc.expectedError) return diff --git a/x/fswap/proposal_handler_test.go b/x/fswap/proposal_handler_test.go index 5697d4575f..bcb7dec32f 100644 --- a/x/fswap/proposal_handler_test.go +++ b/x/fswap/proposal_handler_test.go @@ -8,12 +8,13 @@ import ( "github.com/Finschia/finschia-sdk/simapp" sdk "github.com/Finschia/finschia-sdk/types" + bank "github.com/Finschia/finschia-sdk/x/bank/types" "github.com/Finschia/finschia-sdk/x/fswap" "github.com/Finschia/finschia-sdk/x/fswap/types" ) func testProposal(swap types.Swap) *types.MakeSwapProposal { - return types.NewMakeSwapProposal("Test", "description", swap) + return types.NewMakeSwapProposal("Test", "description", swap, bank.Metadata{Base: "DUM"}) } func TestProposalHandlerPassed(t *testing.T) { diff --git a/x/fswap/testutil/expected_keepers_mocks.go b/x/fswap/testutil/expected_keepers_mocks.go index 6d6b45df2c..29059fb0db 100644 --- a/x/fswap/testutil/expected_keepers_mocks.go +++ b/x/fswap/testutil/expected_keepers_mocks.go @@ -8,6 +8,7 @@ import ( reflect "reflect" types "github.com/Finschia/finschia-sdk/types" + types0 "github.com/Finschia/finschia-sdk/x/bank/types" gomock "github.com/golang/mock/gomock" ) @@ -122,3 +123,15 @@ func (mr *MockBankKeeperMockRecorder) SendCoinsFromModuleToAccount(ctx, senderMo mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendCoinsFromModuleToAccount", reflect.TypeOf((*MockBankKeeper)(nil).SendCoinsFromModuleToAccount), ctx, senderModule, recipientAddr, amt) } + +// SetDenomMetaData mocks base method. +func (m *MockBankKeeper) SetDenomMetaData(ctx types.Context, denomMetaData types0.Metadata) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetDenomMetaData", ctx, denomMetaData) +} + +// SetDenomMetaData indicates an expected call of SetDenomMetaData. +func (mr *MockBankKeeperMockRecorder) SetDenomMetaData(ctx, denomMetaData interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetDenomMetaData", reflect.TypeOf((*MockBankKeeper)(nil).SetDenomMetaData), ctx, denomMetaData) +} diff --git a/x/fswap/types/fswap.pb.go b/x/fswap/types/fswap.pb.go index 333ccc3a27..f4c77c37bd 100644 --- a/x/fswap/types/fswap.pb.go +++ b/x/fswap/types/fswap.pb.go @@ -6,7 +6,8 @@ package types import ( fmt "fmt" github_com_Finschia_finschia_sdk_types "github.com/Finschia/finschia-sdk/types" - types "github.com/Finschia/finschia-sdk/types" + types1 "github.com/Finschia/finschia-sdk/types" + types "github.com/Finschia/finschia-sdk/x/bank/types" _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" io "io" @@ -123,9 +124,10 @@ func (m *SwapStats) GetSwapCount() int32 { // From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10 type MakeSwapProposal struct { - Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"` - Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` - Swap Swap `protobuf:"bytes,3,opt,name=swap,proto3" json:"swap"` + Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"` + Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` + Swap Swap `protobuf:"bytes,3,opt,name=swap,proto3" json:"swap"` + ToDenomMetadata types.Metadata `protobuf:"bytes,4,opt,name=to_denom_metadata,json=toDenomMetadata,proto3" json:"to_denom_metadata" yaml:"denom_metadata"` } func (m *MakeSwapProposal) Reset() { *m = MakeSwapProposal{} } @@ -181,9 +183,16 @@ func (m *MakeSwapProposal) GetSwap() Swap { return Swap{} } +func (m *MakeSwapProposal) GetToDenomMetadata() types.Metadata { + if m != nil { + return m.ToDenomMetadata + } + return types.Metadata{} +} + type Swapped struct { - FromCoinAmount types.Coin `protobuf:"bytes,1,opt,name=from_coin_amount,json=fromCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"from_coin_amount"` - ToCoinAmount types.Coin `protobuf:"bytes,2,opt,name=to_coin_amount,json=toCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"to_coin_amount"` + FromCoinAmount types1.Coin `protobuf:"bytes,1,opt,name=from_coin_amount,json=fromCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"from_coin_amount"` + ToCoinAmount types1.Coin `protobuf:"bytes,2,opt,name=to_coin_amount,json=toCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"to_coin_amount"` } func (m *Swapped) Reset() { *m = Swapped{} } @@ -218,18 +227,18 @@ func (m *Swapped) XXX_DiscardUnknown() { var xxx_messageInfo_Swapped proto.InternalMessageInfo -func (m *Swapped) GetFromCoinAmount() types.Coin { +func (m *Swapped) GetFromCoinAmount() types1.Coin { if m != nil { return m.FromCoinAmount } - return types.Coin{} + return types1.Coin{} } -func (m *Swapped) GetToCoinAmount() types.Coin { +func (m *Swapped) GetToCoinAmount() types1.Coin { if m != nil { return m.ToCoinAmount } - return types.Coin{} + return types1.Coin{} } func init() { @@ -242,38 +251,41 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/fswap.proto", fileDescriptor_42ca60eaf37a2b67) } var fileDescriptor_42ca60eaf37a2b67 = []byte{ - // 485 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 0xbf, 0x6f, 0xd3, 0x40, - 0x14, 0x8e, 0x83, 0x4b, 0x9b, 0x4b, 0xa8, 0x2a, 0x53, 0x89, 0xb4, 0x12, 0x76, 0xd5, 0x09, 0xf1, - 0xe3, 0x4e, 0x49, 0x99, 0xba, 0x91, 0xa0, 0x0a, 0x86, 0x4a, 0xc8, 0x45, 0x42, 0x62, 0xb1, 0xce, - 0x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83, 0xd5, 0x13, 0xeb, - 0x12, 0x18, 0xb2, 0x69, 0xe0, 0x37, 0xdf, 0xe6, 0x7f, 0xf1, 0xed, 0x18, 0xf8, 0xe5, 0x7a, 0xe8, - 0x63, 0x0a, 0x83, 0x67, 0x97, 0x8b, 0xd0, 0xbb, 0x5a, 0x84, 0xde, 0xd7, 0x45, 0xe8, 0xbd, 0x5f, - 0x86, 0x8d, 0xab, 0x65, 0xd8, 0xf8, 0xb4, 0x0c, 0x1b, 0xaf, 0xe9, 0x1f, 0xa5, 0xcf, 0xeb, 0x37, - 0x66, 0x2d, 0xd2, 0x9b, 0xf6, 0x75, 0x1c, 0xfc, 0x08, 0x00, 0x00, 0xff, 0xff, 0xcd, 0x7d, 0xbf, - 0x68, 0x7d, 0x03, 0x00, 0x00, + // 542 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0xbf, 0x6f, 0xd3, 0x40, + 0x14, 0xc7, 0xe3, 0x92, 0xd2, 0xe6, 0x12, 0x4a, 0x31, 0x45, 0xa4, 0x95, 0x62, 0x57, 0x99, 0x10, + 0x3f, 0x6c, 0x25, 0x65, 0xca, 0x46, 0x82, 0x2a, 0x18, 0x22, 0x21, 0x17, 0x09, 0x89, 0xc5, 0x3a, + 0xdb, 0x97, 0xc4, 0x8a, 0xed, 0x67, 0xf9, 0x2e, 0x69, 0xfb, 0x47, 0x20, 0x31, 0x32, 0x76, 0xe6, + 0x2f, 0xe9, 0xd8, 0x11, 0x31, 0x04, 0x94, 0x2c, 0x9d, 0xf9, 0x07, 0x40, 0xef, 0xee, 0x9a, 0xa6, + 0x62, 0x00, 0x21, 0xb1, 0xbd, 0x7b, 0xef, 0xdd, 0xf7, 0x73, 0xf7, 0x7d, 0x77, 0xa4, 0x9e, 0x04, + 0xa9, 0x3b, 0xe0, 0xc7, 0x34, 0x77, 0xa7, 0x2d, 0x15, 0x38, 0x79, 0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, 0xc8, 0xc1, 0xe8, 0x85, 0xa4, 0x98, 0x82, 0x6c, 0x09, 0xb8, + 0xc1, 0x5d, 0xfb, 0x2f, 0xdc, 0x9a, 0x80, 0x6b, 0xaa, 0x72, 0xa0, 0xfb, 0xea, 0x7c, 0x6e, 0x19, + 0x17, 0x73, 0xcb, 0xf8, 0x3e, 0xb7, 0x8c, 0x8f, 0x0b, 0xab, 0x74, 0xb1, 0xb0, 0x4a, 0x5f, 0x16, + 0x56, 0xe9, 0xbd, 0xf3, 0x47, 0xe9, 0x13, 0xfd, 0xd3, 0x25, 0x22, 0xb8, 0x2d, 0xff, 0xe0, 0xc1, + 0xaf, 0x00, 0x00, 0x00, 0xff, 0xff, 0x28, 0x1c, 0x9a, 0x78, 0x03, 0x04, 0x00, 0x00, } func (this *Swap) Equal(that interface{}) bool { @@ -333,36 +345,6 @@ func (this *SwapStats) Equal(that interface{}) bool { } return true } -func (this *MakeSwapProposal) Equal(that interface{}) bool { - if that == nil { - return this == nil - } - - that1, ok := that.(*MakeSwapProposal) - if !ok { - that2, ok := that.(MakeSwapProposal) - if ok { - that1 = &that2 - } else { - return false - } - } - if that1 == nil { - return this == nil - } else if this == nil { - return false - } - if this.Title != that1.Title { - return false - } - if this.Description != that1.Description { - return false - } - if !this.Swap.Equal(&that1.Swap) { - return false - } - return true -} func (m *Swap) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -468,6 +450,16 @@ func (m *MakeSwapProposal) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + { + size, err := m.ToDenomMetadata.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintFswap(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x22 { size, err := m.Swap.MarshalToSizedBuffer(dAtA[:i]) if err != nil { @@ -598,6 +590,8 @@ func (m *MakeSwapProposal) Size() (n int) { } l = m.Swap.Size() n += 1 + l + sovFswap(uint64(l)) + l = m.ToDenomMetadata.Size() + n += 1 + l + sovFswap(uint64(l)) return n } @@ -997,6 +991,39 @@ func (m *MakeSwapProposal) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ToDenomMetadata", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFswap + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthFswap + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthFswap + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.ToDenomMetadata.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipFswap(dAtA[iNdEx:]) diff --git a/x/fswap/types/proposal.go b/x/fswap/types/proposal.go index ac2ab862cf..91b2e3413f 100644 --- a/x/fswap/types/proposal.go +++ b/x/fswap/types/proposal.go @@ -3,6 +3,7 @@ package types import ( "gopkg.in/yaml.v2" + bank "github.com/Finschia/finschia-sdk/x/bank/types" gov "github.com/Finschia/finschia-sdk/x/gov/types" ) @@ -13,8 +14,8 @@ const ( // NewMakeSwapProposal creates a new SwapProposal instance. // Deprecated: this proposal is considered legacy and is deprecated in favor of // Msg-based gov proposals. See MsgSwap. -func NewMakeSwapProposal(title, description string, swap Swap) *MakeSwapProposal { - return &MakeSwapProposal{title, description, swap} +func NewMakeSwapProposal(title, description string, swap Swap, toDenomMetadata bank.Metadata) *MakeSwapProposal { + return &MakeSwapProposal{title, description, swap, toDenomMetadata} } // Implements Proposal Interface From 0f5de191ff4fead6aa8142ba9e1544203076291d Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 19:53:48 +0900 Subject: [PATCH 29/47] chore: add query to get all the swaps --- docs/core/proto-docs.md | 34 +++ proto/lbm/fswap/v1/query.proto | 13 + x/fswap/keeper/grpc_query.go | 25 ++ x/fswap/types/query.pb.go | 528 +++++++++++++++++++++++++++++++-- x/fswap/types/query.pb.gw.go | 80 +++++ 5 files changed, 649 insertions(+), 31 deletions(-) diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index f43431498f..ca874f98a0 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -952,6 +952,8 @@ - [lbm/fswap/v1/query.proto](#lbm/fswap/v1/query.proto) - [QuerySwappedRequest](#lbm.fswap.v1.QuerySwappedRequest) - [QuerySwappedResponse](#lbm.fswap.v1.QuerySwappedResponse) + - [QuerySwapsRequest](#lbm.fswap.v1.QuerySwapsRequest) + - [QuerySwapsResponse](#lbm.fswap.v1.QuerySwapsResponse) - [QueryTotalSwappableToCoinAmountRequest](#lbm.fswap.v1.QueryTotalSwappableToCoinAmountRequest) - [QueryTotalSwappableToCoinAmountResponse](#lbm.fswap.v1.QueryTotalSwappableToCoinAmountResponse) @@ -14067,6 +14069,37 @@ GenesisState defines the fswap module's genesis state. + + +### QuerySwapsRequest + + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `pagination` | [cosmos.base.query.v1beta1.PageRequest](#cosmos.base.query.v1beta1.PageRequest) | | | + + + + + + + + +### QuerySwapsResponse + + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `swaps` | [Swap](#lbm.fswap.v1.Swap) | repeated | | +| `pagination` | [cosmos.base.query.v1beta1.PageResponse](#cosmos.base.query.v1beta1.PageResponse) | | | + + + + + + ### QueryTotalSwappableToCoinAmountRequest @@ -14113,6 +14146,7 @@ GenesisState defines the fswap module's genesis state. | ----------- | ------------ | ------------- | ------------| ------- | -------- | | `Swapped` | [QuerySwappedRequest](#lbm.fswap.v1.QuerySwappedRequest) | [QuerySwappedResponse](#lbm.fswap.v1.QuerySwappedResponse) | Swapped queries the current swapped status that includes a burnt amount of from-coin and a minted amount of to-coin. | GET|/lbm/fswap/v1/swapped| | `TotalSwappableToCoinAmount` | [QueryTotalSwappableToCoinAmountRequest](#lbm.fswap.v1.QueryTotalSwappableToCoinAmountRequest) | [QueryTotalSwappableToCoinAmountResponse](#lbm.fswap.v1.QueryTotalSwappableToCoinAmountResponse) | TotalSwappableToCoinAmount queries the current swappable amount for to-coin. | GET|/lbm/fswap/v1/total_swappable_to_coin_amount| +| `Swaps` | [QuerySwapsRequest](#lbm.fswap.v1.QuerySwapsRequest) | [QuerySwapsResponse](#lbm.fswap.v1.QuerySwapsResponse) | Swaps queries all the swap that registered | GET|/lbm/fswap/v1/swaps| diff --git a/proto/lbm/fswap/v1/query.proto b/proto/lbm/fswap/v1/query.proto index 825d66ad30..a2a88b7093 100644 --- a/proto/lbm/fswap/v1/query.proto +++ b/proto/lbm/fswap/v1/query.proto @@ -5,6 +5,7 @@ option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types"; import "google/api/annotations.proto"; import "gogoproto/gogo.proto"; +import "cosmos/base/query/v1beta1/pagination.proto"; import "cosmos/base/v1beta1/coin.proto"; import "lbm/fswap/v1/fswap.proto"; @@ -19,6 +20,10 @@ service Query { returns (QueryTotalSwappableToCoinAmountResponse) { option (google.api.http).get = "/lbm/fswap/v1/total_swappable_to_coin_amount"; } + // Swaps queries all the swap that registered + rpc Swaps(QuerySwapsRequest) returns (QuerySwapsResponse) { + option (google.api.http).get = "/lbm/fswap/v1/swaps"; + } } message QuerySwappedRequest { @@ -40,3 +45,11 @@ message QueryTotalSwappableToCoinAmountResponse { cosmos.base.v1beta1.Coin swappable_amount = 1 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; } + +message QuerySwapsRequest { + cosmos.base.query.v1beta1.PageRequest pagination = 1; +} +message QuerySwapsResponse { + repeated Swap swaps = 1 [(gogoproto.nullable) = false]; + cosmos.base.query.v1beta1.PageResponse pagination = 2; +} \ No newline at end of file diff --git a/x/fswap/keeper/grpc_query.go b/x/fswap/keeper/grpc_query.go index 1a47467cff..def3ee5ffb 100644 --- a/x/fswap/keeper/grpc_query.go +++ b/x/fswap/keeper/grpc_query.go @@ -3,7 +3,9 @@ package keeper import ( "context" + "github.com/Finschia/finschia-sdk/store/prefix" sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/types/query" "github.com/Finschia/finschia-sdk/x/fswap/types" ) @@ -43,3 +45,26 @@ func (s QueryServer) TotalSwappableToCoinAmount(ctx context.Context, req *types. return &types.QueryTotalSwappableToCoinAmountResponse{SwappableAmount: amount}, nil } + +func (s QueryServer) Swaps(ctx context.Context, req *types.QuerySwapsRequest) (*types.QuerySwapsResponse, error) { + c := sdk.UnwrapSDKContext(ctx) + + swaps := []types.Swap{} + store := c.KVStore(s.storeKey) + swapStore := prefix.NewStore(store, swapPrefix) + pageResponse, err := query.Paginate(swapStore, req.Pagination, func(key, value []byte) error { + swap := types.Swap{} + if err := s.Keeper.cdc.Unmarshal(value, &swap); err != nil { + return err + } + swaps = append(swaps, swap) + return nil + }) + if err != nil { + return nil, err + } + return &types.QuerySwapsResponse{ + Swaps: swaps, + Pagination: pageResponse, + }, nil +} diff --git a/x/fswap/types/query.pb.go b/x/fswap/types/query.pb.go index 286ad93a32..80beb5195a 100644 --- a/x/fswap/types/query.pb.go +++ b/x/fswap/types/query.pb.go @@ -7,6 +7,7 @@ import ( context "context" fmt "fmt" types "github.com/Finschia/finschia-sdk/types" + query "github.com/Finschia/finschia-sdk/types/query" _ "github.com/gogo/protobuf/gogoproto" grpc1 "github.com/gogo/protobuf/grpc" proto "github.com/gogo/protobuf/proto" @@ -234,47 +235,152 @@ func (m *QueryTotalSwappableToCoinAmountResponse) GetSwappableAmount() types.Coi return types.Coin{} } +type QuerySwapsRequest struct { + Pagination *query.PageRequest `protobuf:"bytes,1,opt,name=pagination,proto3" json:"pagination,omitempty"` +} + +func (m *QuerySwapsRequest) Reset() { *m = QuerySwapsRequest{} } +func (m *QuerySwapsRequest) String() string { return proto.CompactTextString(m) } +func (*QuerySwapsRequest) ProtoMessage() {} +func (*QuerySwapsRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_01deae9da7816d6a, []int{4} +} +func (m *QuerySwapsRequest) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *QuerySwapsRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_QuerySwapsRequest.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 *QuerySwapsRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_QuerySwapsRequest.Merge(m, src) +} +func (m *QuerySwapsRequest) XXX_Size() int { + return m.Size() +} +func (m *QuerySwapsRequest) XXX_DiscardUnknown() { + xxx_messageInfo_QuerySwapsRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_QuerySwapsRequest proto.InternalMessageInfo + +func (m *QuerySwapsRequest) GetPagination() *query.PageRequest { + if m != nil { + return m.Pagination + } + return nil +} + +type QuerySwapsResponse struct { + Swaps []Swap `protobuf:"bytes,1,rep,name=swaps,proto3" json:"swaps"` + Pagination *query.PageResponse `protobuf:"bytes,2,opt,name=pagination,proto3" json:"pagination,omitempty"` +} + +func (m *QuerySwapsResponse) Reset() { *m = QuerySwapsResponse{} } +func (m *QuerySwapsResponse) String() string { return proto.CompactTextString(m) } +func (*QuerySwapsResponse) ProtoMessage() {} +func (*QuerySwapsResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_01deae9da7816d6a, []int{5} +} +func (m *QuerySwapsResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *QuerySwapsResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_QuerySwapsResponse.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 *QuerySwapsResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_QuerySwapsResponse.Merge(m, src) +} +func (m *QuerySwapsResponse) XXX_Size() int { + return m.Size() +} +func (m *QuerySwapsResponse) XXX_DiscardUnknown() { + xxx_messageInfo_QuerySwapsResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_QuerySwapsResponse proto.InternalMessageInfo + +func (m *QuerySwapsResponse) GetSwaps() []Swap { + if m != nil { + return m.Swaps + } + return nil +} + +func (m *QuerySwapsResponse) GetPagination() *query.PageResponse { + if m != nil { + return m.Pagination + } + return nil +} + func init() { proto.RegisterType((*QuerySwappedRequest)(nil), "lbm.fswap.v1.QuerySwappedRequest") proto.RegisterType((*QuerySwappedResponse)(nil), "lbm.fswap.v1.QuerySwappedResponse") proto.RegisterType((*QueryTotalSwappableToCoinAmountRequest)(nil), "lbm.fswap.v1.QueryTotalSwappableToCoinAmountRequest") proto.RegisterType((*QueryTotalSwappableToCoinAmountResponse)(nil), "lbm.fswap.v1.QueryTotalSwappableToCoinAmountResponse") + proto.RegisterType((*QuerySwapsRequest)(nil), "lbm.fswap.v1.QuerySwapsRequest") + proto.RegisterType((*QuerySwapsResponse)(nil), "lbm.fswap.v1.QuerySwapsResponse") } func init() { proto.RegisterFile("lbm/fswap/v1/query.proto", fileDescriptor_01deae9da7816d6a) } var fileDescriptor_01deae9da7816d6a = []byte{ - // 473 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0x31, 0x6f, 0xd3, 0x40, - 0x14, 0xc7, 0x6d, 0x4b, 0x50, 0xf5, 0xa8, 0x4a, 0x75, 0x14, 0x11, 0xac, 0xe2, 0x82, 0x07, 0x40, - 0x02, 0xee, 0xe4, 0xb6, 0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 0xe1, 0xb8, 0xfc, 0x87, 0xff, 0xd9, 0x65, 0xa5, 0xee, 0x19, 0xa9, 0x04, 0xdf, 0x6f, 0x4a, 0x55, - 0xba, 0xb3, 0x33, 0x9d, 0x4d, 0x73, 0x53, 0xf7, 0x9f, 0x9d, 0x8d, 0x02, 0x77, 0x38, 0x0a, 0xdc, - 0x9f, 0xa3, 0xc0, 0x7d, 0x3f, 0x0e, 0x9c, 0xe1, 0x38, 0x70, 0xbe, 0x8d, 0x03, 0xe7, 0x15, 0x59, - 0x38, 0x94, 0x53, 0x4b, 0x31, 0xc3, 0x89, 0x2f, 0x9a, 0xe7, 0xbe, 0xfb, 0x3b, 0x00, 0x00, 0xff, - 0xff, 0x4e, 0x71, 0x07, 0xdd, 0x86, 0x04, 0x00, 0x00, + // 590 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x94, 0xcf, 0x6e, 0xd3, 0x4e, + 0x10, 0xc7, 0xe3, 0xf4, 0xd7, 0x5f, 0xd5, 0x6d, 0x55, 0xca, 0xb6, 0x15, 0xc1, 0x14, 0x27, 0xf8, + 0xd0, 0x56, 0xfc, 0xd9, 0x55, 0xd2, 0xf2, 0x00, 0x04, 0x54, 0xb8, 0x20, 0x41, 0xe8, 0x09, 0x0e, + 0x61, 0x9d, 0x6e, 0x5c, 0x8b, 0xd8, 0xe3, 0x66, 0x37, 0x69, 0x73, 0xe5, 0xc0, 0x0d, 0x09, 0x89, + 0x47, 0x80, 0x13, 0x2f, 0xc1, 0xb5, 0xc7, 0x4a, 0x5c, 0x38, 0x01, 0x4a, 0x78, 0x10, 0xb4, 0xeb, + 0x75, 0x63, 0x8b, 0x86, 0x08, 0xa1, 0xde, 0x36, 0x3b, 0xdf, 0x99, 0xef, 0x67, 0x27, 0x33, 0x46, + 0xa5, 0x8e, 0x17, 0xd2, 0xb6, 0x38, 0x62, 0x31, 0xed, 0x57, 0xe9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e2, 0x15, 0xbc, 0x46, 0x73, 0xcb, 0x2c, 0x8c, 0xcb, + 0x67, 0x0b, 0xd9, 0x93, 0x27, 0x01, 0xef, 0x9c, 0xe3, 0x30, 0x75, 0x36, 0xed, 0xbb, 0x7f, 0x99, + 0x65, 0x50, 0x77, 0x34, 0x2a, 0xc1, 0xb7, 0xf3, 0xa8, 0x52, 0x65, 0x36, 0xc7, 0x83, 0x98, 0x5f, + 0x4b, 0xec, 0xa3, 0x59, 0xfd, 0x37, 0xe2, 0xf2, 0x84, 0x6e, 0xa4, 0xe3, 0x63, 0x57, 0x26, 0x0b, + 0x0c, 0xc1, 0x35, 0x4d, 0xb0, 0x86, 0x57, 0x7e, 0x6f, 0x96, 0xa8, 0x3f, 0x3a, 0x19, 0x3a, 0xd6, + 0xe9, 0xd0, 0xb1, 0x7e, 0x0c, 0x1d, 0xeb, 0xdd, 0xc8, 0x29, 0x9c, 0x8e, 0x9c, 0xc2, 0xd7, 0x91, + 0x53, 0x78, 0x4e, 0xa6, 0x8e, 0xfa, 0xb1, 0x29, 0xa6, 0x47, 0xde, 0xfb, 0x5f, 0x7f, 0x44, 0xb7, + 0x7f, 0x05, 0x00, 0x00, 0xff, 0xff, 0x4e, 0x87, 0x53, 0x1d, 0x08, 0x06, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -294,6 +400,8 @@ type QueryClient interface { Swapped(ctx context.Context, in *QuerySwappedRequest, opts ...grpc.CallOption) (*QuerySwappedResponse, error) // TotalSwappableToCoinAmount queries the current swappable amount for to-coin. TotalSwappableToCoinAmount(ctx context.Context, in *QueryTotalSwappableToCoinAmountRequest, opts ...grpc.CallOption) (*QueryTotalSwappableToCoinAmountResponse, error) + // Swaps queries all the swap that registered + Swaps(ctx context.Context, in *QuerySwapsRequest, opts ...grpc.CallOption) (*QuerySwapsResponse, error) } type queryClient struct { @@ -322,6 +430,15 @@ func (c *queryClient) TotalSwappableToCoinAmount(ctx context.Context, in *QueryT return out, nil } +func (c *queryClient) Swaps(ctx context.Context, in *QuerySwapsRequest, opts ...grpc.CallOption) (*QuerySwapsResponse, error) { + out := new(QuerySwapsResponse) + err := c.cc.Invoke(ctx, "/lbm.fswap.v1.Query/Swaps", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // QueryServer is the server API for Query service. type QueryServer interface { // Swapped queries the current swapped status that includes a burnt amount of from-coin and a minted amount of @@ -329,6 +446,8 @@ type QueryServer interface { Swapped(context.Context, *QuerySwappedRequest) (*QuerySwappedResponse, error) // TotalSwappableToCoinAmount queries the current swappable amount for to-coin. TotalSwappableToCoinAmount(context.Context, *QueryTotalSwappableToCoinAmountRequest) (*QueryTotalSwappableToCoinAmountResponse, error) + // Swaps queries all the swap that registered + Swaps(context.Context, *QuerySwapsRequest) (*QuerySwapsResponse, error) } // UnimplementedQueryServer can be embedded to have forward compatible implementations. @@ -341,6 +460,9 @@ func (*UnimplementedQueryServer) Swapped(ctx context.Context, req *QuerySwappedR func (*UnimplementedQueryServer) TotalSwappableToCoinAmount(ctx context.Context, req *QueryTotalSwappableToCoinAmountRequest) (*QueryTotalSwappableToCoinAmountResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method TotalSwappableToCoinAmount not implemented") } +func (*UnimplementedQueryServer) Swaps(ctx context.Context, req *QuerySwapsRequest) (*QuerySwapsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Swaps not implemented") +} func RegisterQueryServer(s grpc1.Server, srv QueryServer) { s.RegisterService(&_Query_serviceDesc, srv) @@ -382,6 +504,24 @@ func _Query_TotalSwappableToCoinAmount_Handler(srv interface{}, ctx context.Cont return interceptor(ctx, in, info, handler) } +func _Query_Swaps_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(QuerySwapsRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(QueryServer).Swaps(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/lbm.fswap.v1.Query/Swaps", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(QueryServer).Swaps(ctx, req.(*QuerySwapsRequest)) + } + return interceptor(ctx, in, info, handler) +} + var _Query_serviceDesc = grpc.ServiceDesc{ ServiceName: "lbm.fswap.v1.Query", HandlerType: (*QueryServer)(nil), @@ -394,6 +534,10 @@ var _Query_serviceDesc = grpc.ServiceDesc{ MethodName: "TotalSwappableToCoinAmount", Handler: _Query_TotalSwappableToCoinAmount_Handler, }, + { + MethodName: "Swaps", + Handler: _Query_Swaps_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "lbm/fswap/v1/query.proto", @@ -549,6 +693,90 @@ func (m *QueryTotalSwappableToCoinAmountResponse) MarshalToSizedBuffer(dAtA []by return len(dAtA) - i, nil } +func (m *QuerySwapsRequest) 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 *QuerySwapsRequest) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *QuerySwapsRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Pagination != nil { + { + size, err := m.Pagination.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintQuery(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *QuerySwapsResponse) 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 *QuerySwapsResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *QuerySwapsResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Pagination != nil { + { + size, err := m.Pagination.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintQuery(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + if len(m.Swaps) > 0 { + for iNdEx := len(m.Swaps) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Swaps[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintQuery(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + func encodeVarintQuery(dAtA []byte, offset int, v uint64) int { offset -= sovQuery(v) base := offset @@ -618,6 +846,38 @@ func (m *QueryTotalSwappableToCoinAmountResponse) Size() (n int) { return n } +func (m *QuerySwapsRequest) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Pagination != nil { + l = m.Pagination.Size() + n += 1 + l + sovQuery(uint64(l)) + } + return n +} + +func (m *QuerySwapsResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.Swaps) > 0 { + for _, e := range m.Swaps { + l = e.Size() + n += 1 + l + sovQuery(uint64(l)) + } + } + if m.Pagination != nil { + l = m.Pagination.Size() + n += 1 + l + sovQuery(uint64(l)) + } + return n +} + func sovQuery(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -1051,6 +1311,212 @@ func (m *QueryTotalSwappableToCoinAmountResponse) Unmarshal(dAtA []byte) error { } return nil } +func (m *QuerySwapsRequest) 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 ErrIntOverflowQuery + } + 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: QuerySwapsRequest: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: QuerySwapsRequest: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Pagination", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Pagination == nil { + m.Pagination = &query.PageRequest{} + } + if err := m.Pagination.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipQuery(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthQuery + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *QuerySwapsResponse) 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 ErrIntOverflowQuery + } + 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: QuerySwapsResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: QuerySwapsResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Swaps", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Swaps = append(m.Swaps, Swap{}) + if err := m.Swaps[len(m.Swaps)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Pagination", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Pagination == nil { + m.Pagination = &query.PageResponse{} + } + if err := m.Pagination.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipQuery(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthQuery + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipQuery(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/x/fswap/types/query.pb.gw.go b/x/fswap/types/query.pb.gw.go index 0ff5b16232..00786290bd 100644 --- a/x/fswap/types/query.pb.gw.go +++ b/x/fswap/types/query.pb.gw.go @@ -103,6 +103,42 @@ func local_request_Query_TotalSwappableToCoinAmount_0(ctx context.Context, marsh } +var ( + filter_Query_Swaps_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} +) + +func request_Query_Swaps_0(ctx context.Context, marshaler runtime.Marshaler, client QueryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq QuerySwapsRequest + var metadata runtime.ServerMetadata + + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Query_Swaps_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.Swaps(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_Query_Swaps_0(ctx context.Context, marshaler runtime.Marshaler, server QueryServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq QuerySwapsRequest + var metadata runtime.ServerMetadata + + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Query_Swaps_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := server.Swaps(ctx, &protoReq) + return msg, metadata, err + +} + // RegisterQueryHandlerServer registers the http handlers for service Query to "mux". // UnaryRPC :call QueryServer directly. // StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906. @@ -149,6 +185,26 @@ func RegisterQueryHandlerServer(ctx context.Context, mux *runtime.ServeMux, serv }) + mux.Handle("GET", pattern_Query_Swaps_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_Query_Swaps_0(rctx, inboundMarshaler, server, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Query_Swaps_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + return nil } @@ -230,6 +286,26 @@ func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, clie }) + mux.Handle("GET", pattern_Query_Swaps_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Query_Swaps_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Query_Swaps_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + return nil } @@ -237,10 +313,14 @@ var ( pattern_Query_Swapped_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"lbm", "fswap", "v1", "swapped"}, "", runtime.AssumeColonVerbOpt(false))) pattern_Query_TotalSwappableToCoinAmount_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"lbm", "fswap", "v1", "total_swappable_to_coin_amount"}, "", runtime.AssumeColonVerbOpt(false))) + + pattern_Query_Swaps_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"lbm", "fswap", "v1", "swaps"}, "", runtime.AssumeColonVerbOpt(false))) ) var ( forward_Query_Swapped_0 = runtime.ForwardResponseMessage forward_Query_TotalSwappableToCoinAmount_0 = runtime.ForwardResponseMessage + + forward_Query_Swaps_0 = runtime.ForwardResponseMessage ) From 484774693dcb1dd61a429b799680510b7b69306c Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 20:53:33 +0900 Subject: [PATCH 30/47] chore: change swap_multiple to swap_rate --- docs/core/proto-docs.md | 2 +- proto/lbm/fswap/v1/fswap.proto | 4 +- x/fswap/client/cli/tx.go | 13 +++-- x/fswap/keeper/keeper.go | 5 +- x/fswap/keeper/keeper_test.go | 15 ++++-- x/fswap/keeper/msg_server_test.go | 6 ++- x/fswap/keeper/proposal_test.go | 16 +++--- x/fswap/keeper/query_params_test.go | 1 - x/fswap/proposal_handler_test.go | 4 +- x/fswap/types/fswap.go | 4 +- x/fswap/types/fswap.pb.go | 82 ++++++++++++++--------------- 11 files changed, 82 insertions(+), 70 deletions(-) delete mode 100644 x/fswap/keeper/query_params_test.go diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index ca874f98a0..f0917f3d54 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -13950,7 +13950,7 @@ From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk base | `from_denom` | [string](#string) | | | | `to_denom` | [string](#string) | | | | `amount_cap_for_to_denom` | [string](#string) | | | -| `swap_multiple` | [string](#string) | | | +| `swap_rate` | [string](#string) | | | diff --git a/proto/lbm/fswap/v1/fswap.proto b/proto/lbm/fswap/v1/fswap.proto index 26de0e21c2..13a5c2a006 100644 --- a/proto/lbm/fswap/v1/fswap.proto +++ b/proto/lbm/fswap/v1/fswap.proto @@ -15,8 +15,8 @@ message Swap { string to_denom = 2; string amount_cap_for_to_denom = 3 [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; - string swap_multiple = 4 - [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; + string swap_rate = 4 + [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Dec", (gogoproto.nullable) = false]; } message SwapStats { diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index cbaafc5147..98f6f41e48 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -21,7 +21,7 @@ const ( FlagFromDenom = "from-denom" FlagToDenom = "to-denom" FlagAmountCapForToDenom = "to-coin-amount-cap" - FlagSwapMultiple = "swap-multiple" + FlagSwapRate = "swap-rate" ) // GetTxCmd returns the transaction commands for this module @@ -184,16 +184,19 @@ Example of the content of messages-json: return err } - swapMultiple, err := cmd.Flags().GetInt64(FlagSwapMultiple) + swapRate, err := cmd.Flags().GetString(FlagSwapRate) + if err != nil { + return err + } + swapRateDec, err := sdk.NewDecFromStr(swapRate) if err != nil { return err } - swap := types.Swap{ FromDenom: fromDenom, ToDenom: toDenom, AmountCapForToDenom: sdk.NewInt(amountCap), - SwapMultiple: sdk.NewInt(swapMultiple), + SwapRate: swapRateDec, } toDenomMetadata, err := parseToDenomMetadata(clientCtx.Codec, args[0]) @@ -227,7 +230,7 @@ Example of the content of messages-json: cmd.Flags().String(FlagFromDenom, "", "cony") cmd.Flags().String(FlagToDenom, "", "PDT") cmd.Flags().Int64(FlagAmountCapForToDenom, 0, "tbd") - cmd.Flags().Int64(FlagSwapMultiple, 0, "tbd") + cmd.Flags().Int64(FlagSwapRate, 0, "tbd") return cmd } diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 9fe4258a28..586da1fa70 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -40,8 +40,9 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Co return err } - newAmount := fromCoinAmount.Amount.Mul(swap.SwapMultiple) - newCoinAmount := sdk.NewCoin(toDenom, newAmount) + multipliedAmountDec := swap.SwapRate.Mul(sdk.NewDecFromBigInt(fromCoinAmount.Amount.BigInt())) + multipliedAmountInt := sdk.NewIntFromBigInt(multipliedAmountDec.BigInt()) + newCoinAmount := sdk.NewCoin(toDenom, multipliedAmountInt) swapped, err := k.getSwapped(ctx, swap.GetFromDenom(), swap.GetToDenom()) if err != nil { return err diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index ba9956ed84..fae078e0fb 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -66,11 +66,16 @@ func (s *KeeperTestSuite) SetupTest() { numAcc := int64(2) s.initBalance = sdk.NewInt(123456789) + swapRateForCony, err := sdk.NewDecFromStr("148079656000000") + s.Require().NoError(err) + swapCap := sdk.NewIntFromBigInt(swapRateForCony.Mul(s.initBalance.ToDec()).BigInt()) + swapCap = swapCap.Mul(sdk.NewInt(numAcc)) + s.Require().NoError(err) s.swap = types.Swap{ FromDenom: "fromdenom", ToDenom: "todenom", - AmountCapForToDenom: s.initBalance.Mul(sdk.NewInt(1000).Mul(sdk.NewInt(numAcc))), - SwapMultiple: sdk.NewInt(1000), + AmountCapForToDenom: swapCap, + SwapRate: swapRateForCony, } s.toDenomMetadata = bank.Metadata{ Description: "This is metadata for to-coin", @@ -153,8 +158,7 @@ func (s *KeeperTestSuite) TestSwap() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - dontCareForThisTest := bank.Metadata{Base: "dummy"} - err := s.keeper.MakeSwap(ctx, s.swap, dontCareForThisTest) + err := s.keeper.MakeSwap(ctx, s.swap, s.toDenomMetadata) s.Require().NoError(err) err = s.keeper.Swap(ctx, tc.from, tc.amountToSwap, tc.toDenom) @@ -165,7 +169,8 @@ func (s *KeeperTestSuite) TestSwap() { s.Require().NoError(err) actualAmount := s.keeper.GetBalance(ctx, tc.from, s.swap.GetToDenom()).Amount - expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swap.SwapMultiple) + multipliedAmountDec := s.swap.SwapRate.Mul(sdk.NewDecFromBigInt(tc.expectedBalanceWithoutMultiply.BigInt())) + expectedAmount := sdk.NewIntFromBigInt(multipliedAmountDec.BigInt()) s.Require().Equal(expectedAmount, actualAmount) }) } diff --git a/x/fswap/keeper/msg_server_test.go b/x/fswap/keeper/msg_server_test.go index b035bb53da..14e0658759 100644 --- a/x/fswap/keeper/msg_server_test.go +++ b/x/fswap/keeper/msg_server_test.go @@ -63,7 +63,8 @@ func (s *KeeperTestSuite) TestMsgSwap() { from, err := sdk.AccAddressFromBech32(tc.request.FromAddress) s.Require().NoError(err) actualAmount := s.keeper.GetBalance(ctx, from, tc.request.GetToDenom()).Amount - expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swap.SwapMultiple) + multipliedAmountDec := s.swap.SwapRate.Mul(sdk.NewDecFromBigInt(tc.expectedBalanceWithoutMultiply.BigInt())) + expectedAmount := sdk.NewIntFromBigInt(multipliedAmountDec.BigInt()) s.Require().Equal(expectedAmount, actualAmount) }) } @@ -114,7 +115,8 @@ func (s *KeeperTestSuite) TestMsgSwapAll() { from, err := sdk.AccAddressFromBech32(tc.request.FromAddress) s.Require().NoError(err) actualAmount := s.keeper.GetBalance(ctx, from, tc.request.GetToDenom()).Amount - expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.swap.SwapMultiple) + multipliedAmountDec := s.swap.SwapRate.Mul(sdk.NewDecFromBigInt(tc.expectedBalanceWithoutMultiply.BigInt())) + expectedAmount := sdk.NewIntFromBigInt(multipliedAmountDec.BigInt()) s.Require().Equal(expectedAmount, actualAmount) }) } diff --git a/x/fswap/keeper/proposal_test.go b/x/fswap/keeper/proposal_test.go index 84f11c3555..45247ee658 100644 --- a/x/fswap/keeper/proposal_test.go +++ b/x/fswap/keeper/proposal_test.go @@ -17,7 +17,7 @@ func (s *KeeperTestSuite) TestMakeSwapProposal() { FromDenom: "fromD", ToDenom: "toD", AmountCapForToDenom: sdk.OneInt(), - SwapMultiple: sdk.OneInt(), + SwapRate: sdk.OneDec(), }, false, nil, @@ -47,7 +47,7 @@ func (s *KeeperTestSuite) TestSwapValidateBasic() { FromDenom: "fromD", ToDenom: "toD", AmountCapForToDenom: sdk.OneInt(), - SwapMultiple: sdk.OneInt(), + SwapRate: sdk.OneDec(), }, false, nil, @@ -57,7 +57,7 @@ func (s *KeeperTestSuite) TestSwapValidateBasic() { FromDenom: "", ToDenom: "toD", AmountCapForToDenom: sdk.OneInt(), - SwapMultiple: sdk.OneInt(), + SwapRate: sdk.OneDec(), }, true, sdkerrors.ErrInvalidRequest, @@ -67,7 +67,7 @@ func (s *KeeperTestSuite) TestSwapValidateBasic() { FromDenom: "fromD", ToDenom: "", AmountCapForToDenom: sdk.OneInt(), - SwapMultiple: sdk.OneInt(), + SwapRate: sdk.OneDec(), }, true, sdkerrors.ErrInvalidRequest, @@ -77,17 +77,17 @@ func (s *KeeperTestSuite) TestSwapValidateBasic() { FromDenom: "fromD", ToDenom: "toD", AmountCapForToDenom: sdk.ZeroInt(), - SwapMultiple: sdk.OneInt(), + SwapRate: sdk.OneDec(), }, true, sdkerrors.ErrInvalidRequest, }, - "invalid zero swap-multiple": { + "invalid zero swap-rate": { types.Swap{ FromDenom: "fromD", ToDenom: "toD", AmountCapForToDenom: sdk.OneInt(), - SwapMultiple: sdk.ZeroInt(), + SwapRate: sdk.ZeroDec(), }, true, sdkerrors.ErrInvalidRequest, @@ -97,7 +97,7 @@ func (s *KeeperTestSuite) TestSwapValidateBasic() { FromDenom: "same", ToDenom: "same", AmountCapForToDenom: sdk.OneInt(), - SwapMultiple: sdk.OneInt(), + SwapRate: sdk.OneDec(), }, true, sdkerrors.ErrInvalidRequest, diff --git a/x/fswap/keeper/query_params_test.go b/x/fswap/keeper/query_params_test.go deleted file mode 100644 index 9429264902..0000000000 --- a/x/fswap/keeper/query_params_test.go +++ /dev/null @@ -1 +0,0 @@ -package keeper_test diff --git a/x/fswap/proposal_handler_test.go b/x/fswap/proposal_handler_test.go index bcb7dec32f..c55bd5a2e4 100644 --- a/x/fswap/proposal_handler_test.go +++ b/x/fswap/proposal_handler_test.go @@ -21,11 +21,13 @@ func TestProposalHandlerPassed(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + forConyRate, err := sdk.NewDecFromStr("148079656000000") + require.NoError(t, err) swap := types.Swap{ FromDenom: "aaa", ToDenom: "bbb", AmountCapForToDenom: sdk.NewInt(100), - SwapMultiple: sdk.NewInt(10), + SwapRate: forConyRate, } tp := testProposal(swap) hdlr := fswap.NewSwapHandler(app.FswapKeeper) diff --git a/x/fswap/types/fswap.go b/x/fswap/types/fswap.go index 7692f09af3..c307c826b3 100644 --- a/x/fswap/types/fswap.go +++ b/x/fswap/types/fswap.go @@ -21,8 +21,8 @@ func (s *Swap) ValidateBasic() error { if s.AmountCapForToDenom.LT(sdk.OneInt()) { return sdkerrors.ErrInvalidRequest.Wrap("amount cannot be less than one") } - if s.SwapMultiple.LT(sdk.OneInt()) { - return sdkerrors.ErrInvalidRequest.Wrap("swap multiple cannot be less than one") + if s.SwapRate.IsZero() { + return sdkerrors.ErrInvalidRequest.Wrap("swap rate cannot be zero") } return nil } diff --git a/x/fswap/types/fswap.pb.go b/x/fswap/types/fswap.pb.go index f4c77c37bd..4449b20bec 100644 --- a/x/fswap/types/fswap.pb.go +++ b/x/fswap/types/fswap.pb.go @@ -30,7 +30,7 @@ type Swap struct { FromDenom string `protobuf:"bytes,1,opt,name=from_denom,json=fromDenom,proto3" json:"from_denom,omitempty"` ToDenom string `protobuf:"bytes,2,opt,name=to_denom,json=toDenom,proto3" json:"to_denom,omitempty"` AmountCapForToDenom github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,3,opt,name=amount_cap_for_to_denom,json=amountCapForToDenom,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"amount_cap_for_to_denom"` - SwapMultiple github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,4,opt,name=swap_multiple,json=swapMultiple,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"swap_multiple"` + SwapRate github_com_Finschia_finschia_sdk_types.Dec `protobuf:"bytes,4,opt,name=swap_rate,json=swapRate,proto3,customtype=github.com/Finschia/finschia-sdk/types.Dec" json:"swap_rate"` } func (m *Swap) Reset() { *m = Swap{} } @@ -251,41 +251,41 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/fswap.proto", fileDescriptor_42ca60eaf37a2b67) } var fileDescriptor_42ca60eaf37a2b67 = []byte{ - // 542 bytes of a gzipped FileDescriptorProto + // 539 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0xbf, 0x6f, 0xd3, 0x40, - 0x14, 0xc7, 0xe3, 0x92, 0xd2, 0xe6, 0x12, 0x4a, 0x31, 0x45, 0xa4, 0x95, 0x62, 0x57, 0x99, 0x10, - 0x3f, 0x6c, 0x25, 0x65, 0xca, 0x46, 0x82, 0x2a, 0x18, 0x22, 0x21, 0x17, 0x09, 0x89, 0xc5, 0x3a, - 0xdb, 0x97, 0xc4, 0x8a, 0xed, 0x67, 0xf9, 0x2e, 0x69, 0xfb, 0x47, 0x20, 0x31, 0x32, 0x76, 0xe6, - 0x2f, 0xe9, 0xd8, 0x11, 0x31, 0x04, 0x94, 0x2c, 0x9d, 0xf9, 0x07, 0x40, 0xef, 0xee, 0x9a, 0xa6, - 0x62, 0x00, 0x21, 0xb1, 0xbd, 0x7b, 0xef, 0xdd, 0xf7, 0x73, 0xf7, 0x7d, 0x77, 0xa4, 0x9e, 0x04, - 0xa9, 0x3b, 0xe0, 0xc7, 0x34, 0x77, 0xa7, 0x2d, 0x15, 0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xd2, 0x3d, 0x3b, 0x56, + 0x08, 0x32, 0x01, 0xe9, 0x06, 0x5c, 0x0a, 0x77, 0xd2, 0x0a, 0x04, 0xf2, 0x96, 0x1b, 0x42, 0x94, + 0xfe, 0x56, 0x4f, 0xc7, 0x8b, 0x3a, 0x2d, 0x74, 0xbd, 0xf9, 0x61, 0x85, 0x95, 0x8f, 0x4e, 0x78, + 0x66, 0x36, 0x18, 0x1b, 0xe4, 0x90, 0xf8, 0x7d, 0x91, 0x42, 0x52, 0x37, 0x76, 0x8d, 0xc7, 0x55, + 0xaf, 0x4a, 0x99, 0x7d, 0x4a, 0x98, 0xdb, 0xac, 0x82, 0x50, 0x14, 0x57, 0x54, 0x71, 0x0d, 0x41, + 0x97, 0x46, 0xec, 0x11, 0x4f, 0xe0, 0x38, 0x45, 0x3f, 0xe4, 0x99, 0x3f, 0x80, 0xdc, 0x5f, 0x74, + 0xde, 0xa1, 0xce, 0x6e, 0xfb, 0x62, 0x6a, 0x97, 0xbe, 0x4e, 0xed, 0x27, 0xc3, 0x08, 0x47, 0xc7, + 0x81, 0x13, 0x42, 0xe2, 0x1e, 0x44, 0xa9, 0x0c, 0x47, 0x11, 0x77, 0x07, 0x45, 0xf0, 0x5c, 0xf6, + 0xc7, 0x2e, 0x9e, 0x65, 0x42, 0x3a, 0xaf, 0x53, 0xf4, 0x1e, 0xe8, 0x91, 0x3d, 0x9e, 0x1d, 0x40, + 0xfe, 0xae, 0x20, 0xbd, 0x61, 0x55, 0x52, 0xc3, 0xcf, 0x39, 0x8a, 0x7a, 0xf9, 0x9f, 0x66, 0xef, + 0x8b, 0xd0, 0xab, 0xd0, 0x10, 0x8f, 0xa3, 0xe8, 0x54, 0x3e, 0x9d, 0xdb, 0xa5, 0xab, 0x73, 0xdb, + 0x68, 0xbe, 0x60, 0x55, 0x92, 0xe1, 0x08, 0x39, 0x4a, 0xd2, 0x42, 0x71, 0x42, 0x3a, 0x82, 0xd2, + 0x62, 0xd5, 0x53, 0xe4, 0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c5, 0xcc, 0x32, 0x2e, 0x67, 0x96, + 0xf1, 0x7d, 0x66, 0x19, 0x1f, 0xe7, 0x56, 0xe9, 0x72, 0x6e, 0x95, 0xbe, 0xcc, 0xad, 0xd2, 0x7b, + 0xe7, 0x8f, 0xa3, 0x4f, 0x8b, 0x1f, 0xae, 0x10, 0xc1, 0x5d, 0xf5, 0xf7, 0xf6, 0x7e, 0x05, 0x00, + 0x00, 0xff, 0xff, 0x9d, 0x2f, 0x11, 0xda, 0xfb, 0x03, 0x00, 0x00, } func (this *Swap) Equal(that interface{}) bool { @@ -316,7 +316,7 @@ func (this *Swap) Equal(that interface{}) bool { if !this.AmountCapForToDenom.Equal(that1.AmountCapForToDenom) { return false } - if !this.SwapMultiple.Equal(that1.SwapMultiple) { + if !this.SwapRate.Equal(that1.SwapRate) { return false } return true @@ -366,9 +366,9 @@ func (m *Swap) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size := m.SwapMultiple.Size() + size := m.SwapRate.Size() i -= size - if _, err := m.SwapMultiple.MarshalTo(dAtA[i:]); err != nil { + if _, err := m.SwapRate.MarshalTo(dAtA[i:]); err != nil { return 0, err } i = encodeVarintFswap(dAtA, i, uint64(size)) @@ -557,7 +557,7 @@ func (m *Swap) Size() (n int) { } l = m.AmountCapForToDenom.Size() n += 1 + l + sovFswap(uint64(l)) - l = m.SwapMultiple.Size() + l = m.SwapRate.Size() n += 1 + l + sovFswap(uint64(l)) return n } @@ -743,7 +743,7 @@ func (m *Swap) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 4: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field SwapMultiple", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field SwapRate", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -771,7 +771,7 @@ func (m *Swap) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.SwapMultiple.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.SwapRate.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex From badefa5d5a87f6a6a3f8084ddad7dab11637ee75 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Thu, 2 May 2024 21:49:24 +0900 Subject: [PATCH 31/47] chore: disallow updating existing denom metadata with different values --- x/fswap/keeper/expected_keepers.go | 1 + x/fswap/keeper/proposal.go | 37 +++++++++++++++++++++- x/fswap/keeper/proposal_test.go | 30 +++++++++++++++--- x/fswap/testutil/expected_keepers_mocks.go | 36 +++++++++++++++------ 4 files changed, 89 insertions(+), 15 deletions(-) diff --git a/x/fswap/keeper/expected_keepers.go b/x/fswap/keeper/expected_keepers.go index aea3330379..08934e1c3c 100644 --- a/x/fswap/keeper/expected_keepers.go +++ b/x/fswap/keeper/expected_keepers.go @@ -11,6 +11,7 @@ type ( SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error + GetDenomMetaData(ctx sdk.Context, denom string) (banktypes.Metadata, bool) SetDenomMetaData(ctx sdk.Context, denomMetaData banktypes.Metadata) MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error diff --git a/x/fswap/keeper/proposal.go b/x/fswap/keeper/proposal.go index 67f95af47f..f163e0fb9c 100644 --- a/x/fswap/keeper/proposal.go +++ b/x/fswap/keeper/proposal.go @@ -45,10 +45,45 @@ func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank. return err } - k.SetDenomMetaData(ctx, toDenomMetadata) + existingMetadata, ok := k.GetDenomMetaData(ctx, swap.ToDenom) + if !ok { + k.SetDenomMetaData(ctx, toDenomMetadata) + return nil + } + if !denomMetadataEqual(existingMetadata, toDenomMetadata) { + return errors.ErrInvalidRequest.Wrap("changing existing metadata not allowed") + } + return nil } +func denomMetadataEqual(metadata bank.Metadata, otherMetadata bank.Metadata) bool { + if metadata.Description != otherMetadata.Description { + return false + } + if len(metadata.DenomUnits) != len(otherMetadata.DenomUnits) { + return false + } + for i, unit := range metadata.DenomUnits { + if unit.Denom != otherMetadata.DenomUnits[i].Denom { + return false + } + } + if metadata.Base != otherMetadata.Base { + return false + } + if metadata.Display != otherMetadata.Display { + return false + } + if metadata.Name != otherMetadata.Name { + return false + } + if metadata.Symbol != otherMetadata.Symbol { + return false + } + return true +} + func (k Keeper) increaseSwapCount(ctx sdk.Context) error { stats, err := k.getSwapStats(ctx) if err != nil { diff --git a/x/fswap/keeper/proposal_test.go b/x/fswap/keeper/proposal_test.go index 45247ee658..4dbe0e63b2 100644 --- a/x/fswap/keeper/proposal_test.go +++ b/x/fswap/keeper/proposal_test.go @@ -3,13 +3,15 @@ package keeper_test import ( sdk "github.com/Finschia/finschia-sdk/types" sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + bank "github.com/Finschia/finschia-sdk/x/bank/types" "github.com/Finschia/finschia-sdk/x/fswap/types" ) func (s *KeeperTestSuite) TestMakeSwapProposal() { testCases := map[string]struct { swap types.Swap - shouldThrowError bool + toDenomMeta bank.Metadata + existingMetadata bool expectedError error }{ "valid": { @@ -19,19 +21,39 @@ func (s *KeeperTestSuite) TestMakeSwapProposal() { AmountCapForToDenom: sdk.OneInt(), SwapRate: sdk.OneDec(), }, + s.toDenomMetadata, false, nil, }, + "to-denom metadata change not allowed": { + types.Swap{ + FromDenom: "fromD", + ToDenom: "toD", + AmountCapForToDenom: sdk.OneInt(), + SwapRate: sdk.OneDec(), + }, + bank.Metadata{ + Description: s.toDenomMetadata.Description, + DenomUnits: s.toDenomMetadata.DenomUnits, + Base: "change", + Display: s.toDenomMetadata.Display, + Name: s.toDenomMetadata.Name, + Symbol: s.toDenomMetadata.Symbol, + }, + true, + sdkerrors.ErrInvalidRequest, + }, } for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() err := s.keeper.MakeSwap(ctx, tc.swap, s.toDenomMetadata) - if tc.shouldThrowError { + if tc.existingMetadata { + err := s.keeper.MakeSwap(ctx, tc.swap, s.toDenomMetadata) + s.Require().ErrorIs(err, tc.expectedError) + } else { s.Require().ErrorIs(err, tc.expectedError) - return } - s.Require().NoError(err) }) } } diff --git a/x/fswap/testutil/expected_keepers_mocks.go b/x/fswap/testutil/expected_keepers_mocks.go index 29059fb0db..8718d93937 100644 --- a/x/fswap/testutil/expected_keepers_mocks.go +++ b/x/fswap/testutil/expected_keepers_mocks.go @@ -7,8 +7,9 @@ package testutil import ( reflect "reflect" - types "github.com/Finschia/finschia-sdk/types" - types0 "github.com/Finschia/finschia-sdk/x/bank/types" + types "github.com/Finschia/finschia-sdk/snapshots/types" + types0 "github.com/Finschia/finschia-sdk/types" + types1 "github.com/Finschia/finschia-sdk/x/bank/types" gomock "github.com/golang/mock/gomock" ) @@ -36,7 +37,7 @@ func (m *MockBankKeeper) EXPECT() *MockBankKeeperMockRecorder { } // BurnCoins mocks base method. -func (m *MockBankKeeper) BurnCoins(ctx types.Context, moduleName string, amt types.Coins) error { +func (m *MockBankKeeper) BurnCoins(ctx types0.Context, moduleName string, amt types0.Coins) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "BurnCoins", ctx, moduleName, amt) ret0, _ := ret[0].(error) @@ -50,10 +51,10 @@ func (mr *MockBankKeeperMockRecorder) BurnCoins(ctx, moduleName, amt interface{} } // GetBalance mocks base method. -func (m *MockBankKeeper) GetBalance(ctx types.Context, addr types.AccAddress, denom string) types.Coin { +func (m *MockBankKeeper) GetBalance(ctx types0.Context, addr types0.AccAddress, denom string) types0.Coin { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetBalance", ctx, addr, denom) - ret0, _ := ret[0].(types.Coin) + ret0, _ := ret[0].(types0.Coin) return ret0 } @@ -63,8 +64,23 @@ func (mr *MockBankKeeperMockRecorder) GetBalance(ctx, addr, denom interface{}) * return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBalance", reflect.TypeOf((*MockBankKeeper)(nil).GetBalance), ctx, addr, denom) } +// GetDenomMetaData mocks base method. +func (m *MockBankKeeper) GetDenomMetaData(ctx types0.Context, denom string) (types.Metadata, bool) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetDenomMetaData", ctx, denom) + ret0, _ := ret[0].(types.Metadata) + ret1, _ := ret[1].(bool) + return ret0, ret1 +} + +// GetDenomMetaData indicates an expected call of GetDenomMetaData. +func (mr *MockBankKeeperMockRecorder) GetDenomMetaData(ctx, denom interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDenomMetaData", reflect.TypeOf((*MockBankKeeper)(nil).GetDenomMetaData), ctx, denom) +} + // IsSendEnabledCoins mocks base method. -func (m *MockBankKeeper) IsSendEnabledCoins(ctx types.Context, coins ...types.Coin) error { +func (m *MockBankKeeper) IsSendEnabledCoins(ctx types0.Context, coins ...types0.Coin) error { m.ctrl.T.Helper() varargs := []interface{}{ctx} for _, a := range coins { @@ -83,7 +99,7 @@ func (mr *MockBankKeeperMockRecorder) IsSendEnabledCoins(ctx interface{}, coins } // MintCoins mocks base method. -func (m *MockBankKeeper) MintCoins(ctx types.Context, moduleName string, amt types.Coins) error { +func (m *MockBankKeeper) MintCoins(ctx types0.Context, moduleName string, amt types0.Coins) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "MintCoins", ctx, moduleName, amt) ret0, _ := ret[0].(error) @@ -97,7 +113,7 @@ func (mr *MockBankKeeperMockRecorder) MintCoins(ctx, moduleName, amt interface{} } // SendCoinsFromAccountToModule mocks base method. -func (m *MockBankKeeper) SendCoinsFromAccountToModule(ctx types.Context, senderAddr types.AccAddress, recipientModule string, amt types.Coins) error { +func (m *MockBankKeeper) SendCoinsFromAccountToModule(ctx types0.Context, senderAddr types0.AccAddress, recipientModule string, amt types0.Coins) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SendCoinsFromAccountToModule", ctx, senderAddr, recipientModule, amt) ret0, _ := ret[0].(error) @@ -111,7 +127,7 @@ func (mr *MockBankKeeperMockRecorder) SendCoinsFromAccountToModule(ctx, senderAd } // SendCoinsFromModuleToAccount mocks base method. -func (m *MockBankKeeper) SendCoinsFromModuleToAccount(ctx types.Context, senderModule string, recipientAddr types.AccAddress, amt types.Coins) error { +func (m *MockBankKeeper) SendCoinsFromModuleToAccount(ctx types0.Context, senderModule string, recipientAddr types0.AccAddress, amt types0.Coins) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SendCoinsFromModuleToAccount", ctx, senderModule, recipientAddr, amt) ret0, _ := ret[0].(error) @@ -125,7 +141,7 @@ func (mr *MockBankKeeperMockRecorder) SendCoinsFromModuleToAccount(ctx, senderMo } // SetDenomMetaData mocks base method. -func (m *MockBankKeeper) SetDenomMetaData(ctx types.Context, denomMetaData types0.Metadata) { +func (m *MockBankKeeper) SetDenomMetaData(ctx types0.Context, denomMetaData types1.Metadata) { m.ctrl.T.Helper() m.ctrl.Call(m, "SetDenomMetaData", ctx, denomMetaData) } From 1d1a0ce7bd1ffc1d179c418a89374f4a16ff65ac Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 11:03:25 +0900 Subject: [PATCH 32/47] chore: fix for calcSwap logic --- x/fswap/keeper/keeper.go | 11 +++- x/fswap/keeper/keeper_test.go | 96 ++++++++++++++++++++++++++----- x/fswap/keeper/msg_server_test.go | 54 ++++++++++------- x/fswap/keeper/proposal.go | 2 +- 4 files changed, 124 insertions(+), 39 deletions(-) diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 586da1fa70..a637c3a5e1 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -40,9 +40,8 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Co return err } - multipliedAmountDec := swap.SwapRate.Mul(sdk.NewDecFromBigInt(fromCoinAmount.Amount.BigInt())) - multipliedAmountInt := sdk.NewIntFromBigInt(multipliedAmountDec.BigInt()) - newCoinAmount := sdk.NewCoin(toDenom, multipliedAmountInt) + newCoinAmountInt := k.CalcSwap(swap.SwapRate, fromCoinAmount.Amount) + newCoinAmount := sdk.NewCoin(toDenom, newCoinAmountInt) swapped, err := k.getSwapped(ctx, swap.GetFromDenom(), swap.GetToDenom()) if err != nil { return err @@ -83,6 +82,12 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Co return nil } +func (k Keeper) CalcSwap(swapRate sdk.Dec, fromCoinAmount sdk.Int) sdk.Int { + multipliedAmountDec := swapRate.Mul(sdk.NewDecFromBigInt(fromCoinAmount.BigInt())).TruncateInt() + multipliedAmountInt := sdk.NewIntFromBigInt(multipliedAmountDec.BigInt()) + return multipliedAmountInt +} + func (k Keeper) getAllSwapped(ctx sdk.Context) []types.Swapped { swappedSlice := []types.Swapped{} k.iterateAllSwapped(ctx, func(swapped types.Swapped) bool { diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index fae078e0fb..172a282be8 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -2,6 +2,7 @@ package keeper_test import ( "context" + "fmt" "testing" "github.com/stretchr/testify/suite" @@ -66,16 +67,16 @@ func (s *KeeperTestSuite) SetupTest() { numAcc := int64(2) s.initBalance = sdk.NewInt(123456789) - swapRateForCony, err := sdk.NewDecFromStr("148079656000000") + pebSwapRateForCony, err := sdk.NewDecFromStr("148079656000000") s.Require().NoError(err) - swapCap := sdk.NewIntFromBigInt(swapRateForCony.Mul(s.initBalance.ToDec()).BigInt()) + swapCap := sdk.NewIntFromBigInt(pebSwapRateForCony.Mul(s.initBalance.ToDec()).BigInt()) swapCap = swapCap.Mul(sdk.NewInt(numAcc)) s.Require().NoError(err) s.swap = types.Swap{ FromDenom: "fromdenom", ToDenom: "todenom", AmountCapForToDenom: swapCap, - SwapRate: swapRateForCony, + SwapRate: pebSwapRateForCony, } s.toDenomMetadata = bank.Metadata{ Description: "This is metadata for to-coin", @@ -114,19 +115,33 @@ func TestKeeperTestSuite(t *testing.T) { } func (s *KeeperTestSuite) TestSwap() { + swap2ExpectedAmount, ok := sdk.NewIntFromString("296159312000000") + s.Require().True(ok) + swap100ExpectedAmount, ok := sdk.NewIntFromString("14807965600000000") + s.Require().True(ok) + swapAllExpectedBalance, ok := sdk.NewIntFromString("18281438845984584000000") + s.Require().True(ok) testCases := map[string]struct { - from sdk.AccAddress - amountToSwap sdk.Coin - toDenom string - expectedBalanceWithoutMultiply sdk.Int - shouldThrowError bool - expectedError error + from sdk.AccAddress + amountToSwap sdk.Coin + toDenom string + expectedAmount sdk.Int + shouldThrowError bool + expectedError error }{ + "swap 2 from-denom": { + s.accWithFromCoin, + sdk.NewCoin(s.swap.GetFromDenom(), sdk.NewInt(2)), + s.swap.GetToDenom(), + swap2ExpectedAmount, + false, + nil, + }, "swap some": { s.accWithFromCoin, sdk.NewCoin(s.swap.GetFromDenom(), sdk.NewInt(100)), s.swap.GetToDenom(), - sdk.NewInt(100), + swap100ExpectedAmount, false, nil, }, @@ -134,7 +149,7 @@ func (s *KeeperTestSuite) TestSwap() { s.accWithFromCoin, sdk.NewCoin(s.swap.GetFromDenom(), s.initBalance), s.swap.GetToDenom(), - s.initBalance, + swapAllExpectedBalance, false, nil, }, @@ -169,9 +184,62 @@ func (s *KeeperTestSuite) TestSwap() { s.Require().NoError(err) actualAmount := s.keeper.GetBalance(ctx, tc.from, s.swap.GetToDenom()).Amount - multipliedAmountDec := s.swap.SwapRate.Mul(sdk.NewDecFromBigInt(tc.expectedBalanceWithoutMultiply.BigInt())) - expectedAmount := sdk.NewIntFromBigInt(multipliedAmountDec.BigInt()) - s.Require().Equal(expectedAmount, actualAmount) + s.Require().Equal(tc.expectedAmount, actualAmount) + }) + } +} + +func (s *KeeperTestSuite) TestCalcSwap() { + rateDot5, err := sdk.NewDecFromStr("0.5") + s.Require().NoError(err) + rateDot3maxPrecision, err := sdk.NewDecFromStr("0.333333333333333333") + s.Require().NoError(err) + + finschiaSwapRate, err := sdk.NewDecFromStr("148.079656") + s.Require().NoError(err) + conySwapRate := finschiaSwapRate.Mul(sdk.NewDec(1000000)) + pebSwapRateForCony, err := sdk.NewDecFromStr("148079656000000") + s.Require().NoError(err) + testCases := map[string]struct { + fromAmount sdk.Int + expectedAmount sdk.Int + swapRate sdk.Dec + }{ + "swapRate 0.5": { + fromAmount: sdk.ZeroInt(), + swapRate: rateDot5, + expectedAmount: sdk.ZeroInt(), + }, + "swapRate 0.333333333333333333": { + fromAmount: sdk.NewInt(3), + swapRate: rateDot3maxPrecision, + expectedAmount: sdk.ZeroInt(), + }, + "swapRate conySwapRate(148.079656 * 10^6) fromAmount(1)": { + fromAmount: sdk.NewInt(1), + swapRate: conySwapRate, + expectedAmount: sdk.NewInt(148079656), + }, + "swapRate conySwapRate(148.079656 * 10^6) fromAmount(3)": { + fromAmount: sdk.NewInt(3), + swapRate: conySwapRate, + expectedAmount: sdk.NewInt(444238968), + }, + "pebSwapRateForCony pebSwapRateForCony(148.079656 * 10^12) fromAmount(1)": { + fromAmount: sdk.NewInt(1), + swapRate: pebSwapRateForCony, + expectedAmount: sdk.NewInt(148079656000000), + }, + "pebSwapRateForCony pebSwapRateForCony(148.079656 * 10^12) fromAmount(3)": { + fromAmount: sdk.NewInt(3), + swapRate: pebSwapRateForCony, + expectedAmount: sdk.NewInt(444238968000000), + }, + } + for name, tc := range testCases { + s.Run(name, func() { + actualAmount := s.keeper.CalcSwap(tc.swapRate, tc.fromAmount) + s.Require().Equal(tc.expectedAmount, actualAmount, fmt.Sprintf("tc.expectedAmount = %v, actualAmount = %v", tc.expectedAmount, actualAmount)) }) } } diff --git a/x/fswap/keeper/msg_server_test.go b/x/fswap/keeper/msg_server_test.go index 14e0658759..6e7953ac64 100644 --- a/x/fswap/keeper/msg_server_test.go +++ b/x/fswap/keeper/msg_server_test.go @@ -3,24 +3,39 @@ package keeper_test import ( sdk "github.com/Finschia/finschia-sdk/types" sdkerrors "github.com/Finschia/finschia-sdk/types/errors" - bank "github.com/Finschia/finschia-sdk/x/bank/types" "github.com/Finschia/finschia-sdk/x/fswap/types" ) func (s *KeeperTestSuite) TestMsgSwap() { + swap2ExpectedAmount, ok := sdk.NewIntFromString("296159312000000") + s.Require().True(ok) + swap100ExpectedAmount, ok := sdk.NewIntFromString("14807965600000000") + s.Require().True(ok) + swapAllExpectedBalance, ok := sdk.NewIntFromString("18281438845984584000000") + s.Require().True(ok) testCases := map[string]struct { - request *types.MsgSwap - expectedBalanceWithoutMultiply sdk.Int - shouldThrowError bool - expectedError error + request *types.MsgSwap + expectedAmount sdk.Int + shouldThrowError bool + expectedError error }{ + "swap 2 from-denom": { + &types.MsgSwap{ + FromAddress: s.accWithFromCoin.String(), + FromCoinAmount: sdk.NewCoin(s.swap.GetFromDenom(), sdk.NewInt(2)), + ToDenom: s.swap.GetToDenom(), + }, + swap2ExpectedAmount, + false, + nil, + }, "swap some": { &types.MsgSwap{ FromAddress: s.accWithFromCoin.String(), FromCoinAmount: sdk.NewCoin(s.swap.GetFromDenom(), sdk.NewInt(100)), ToDenom: s.swap.GetToDenom(), }, - sdk.NewInt(100), + swap100ExpectedAmount, false, nil, }, @@ -30,7 +45,7 @@ func (s *KeeperTestSuite) TestMsgSwap() { FromCoinAmount: sdk.NewCoin(s.swap.GetFromDenom(), s.initBalance), ToDenom: s.swap.GetToDenom(), }, - s.initBalance, + swapAllExpectedBalance, false, nil, }, @@ -40,7 +55,7 @@ func (s *KeeperTestSuite) TestMsgSwap() { FromCoinAmount: sdk.NewCoin(s.swap.GetFromDenom(), sdk.NewInt(100)), ToDenom: s.swap.GetToDenom(), }, - s.initBalance, + sdk.ZeroInt(), true, sdkerrors.ErrInsufficientFunds, }, @@ -48,8 +63,7 @@ func (s *KeeperTestSuite) TestMsgSwap() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - dontCareForThisTestCase := bank.Metadata{Base: "dummy"} - err := s.keeper.MakeSwap(ctx, s.swap, dontCareForThisTestCase) + err := s.keeper.MakeSwap(ctx, s.swap, s.toDenomMetadata) s.Require().NoError(err) swapResponse, err := s.msgServer.Swap(sdk.WrapSDKContext(ctx), tc.request) @@ -63,19 +77,19 @@ func (s *KeeperTestSuite) TestMsgSwap() { from, err := sdk.AccAddressFromBech32(tc.request.FromAddress) s.Require().NoError(err) actualAmount := s.keeper.GetBalance(ctx, from, tc.request.GetToDenom()).Amount - multipliedAmountDec := s.swap.SwapRate.Mul(sdk.NewDecFromBigInt(tc.expectedBalanceWithoutMultiply.BigInt())) - expectedAmount := sdk.NewIntFromBigInt(multipliedAmountDec.BigInt()) - s.Require().Equal(expectedAmount, actualAmount) + s.Require().Equal(tc.expectedAmount, actualAmount) }) } } func (s *KeeperTestSuite) TestMsgSwapAll() { + swapAllExpectedBalance, ok := sdk.NewIntFromString("18281438845984584000000") + s.Require().True(ok) testCases := map[string]struct { - request *types.MsgSwapAll - expectedBalanceWithoutMultiply sdk.Int - shouldThrowError bool - expectedError error + request *types.MsgSwapAll + expectedAmount sdk.Int + shouldThrowError bool + expectedError error }{ "swapAll": { &types.MsgSwapAll{ @@ -83,7 +97,7 @@ func (s *KeeperTestSuite) TestMsgSwapAll() { FromDenom: s.swap.GetFromDenom(), ToDenom: s.swap.GetToDenom(), }, - s.initBalance, + swapAllExpectedBalance, false, nil, }, @@ -115,9 +129,7 @@ func (s *KeeperTestSuite) TestMsgSwapAll() { from, err := sdk.AccAddressFromBech32(tc.request.FromAddress) s.Require().NoError(err) actualAmount := s.keeper.GetBalance(ctx, from, tc.request.GetToDenom()).Amount - multipliedAmountDec := s.swap.SwapRate.Mul(sdk.NewDecFromBigInt(tc.expectedBalanceWithoutMultiply.BigInt())) - expectedAmount := sdk.NewIntFromBigInt(multipliedAmountDec.BigInt()) - s.Require().Equal(expectedAmount, actualAmount) + s.Require().Equal(tc.expectedAmount, actualAmount) }) } } diff --git a/x/fswap/keeper/proposal.go b/x/fswap/keeper/proposal.go index f163e0fb9c..7f202de690 100644 --- a/x/fswap/keeper/proposal.go +++ b/x/fswap/keeper/proposal.go @@ -57,7 +57,7 @@ func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank. return nil } -func denomMetadataEqual(metadata bank.Metadata, otherMetadata bank.Metadata) bool { +func denomMetadataEqual(metadata, otherMetadata bank.Metadata) bool { if metadata.Description != otherMetadata.Description { return false } From c4996b30fc5096f9ce00745782a56486c6b23e49 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 11:33:04 +0900 Subject: [PATCH 33/47] chore: extract CalcSwap to function --- x/fswap/keeper/calc.go | 7 ++++ x/fswap/keeper/calc_test.go | 65 +++++++++++++++++++++++++++++++++++ x/fswap/keeper/keeper.go | 8 +---- x/fswap/keeper/keeper_test.go | 56 ------------------------------ 4 files changed, 73 insertions(+), 63 deletions(-) create mode 100644 x/fswap/keeper/calc.go create mode 100644 x/fswap/keeper/calc_test.go diff --git a/x/fswap/keeper/calc.go b/x/fswap/keeper/calc.go new file mode 100644 index 0000000000..930bfdbf43 --- /dev/null +++ b/x/fswap/keeper/calc.go @@ -0,0 +1,7 @@ +package keeper + +import "github.com/Finschia/finschia-sdk/types" + +func CalcSwap(swapRate types.Dec, fromCoinAmount types.Int) types.Int { + return swapRate.MulTruncate(types.NewDecFromBigInt(fromCoinAmount.BigInt())).TruncateInt() +} diff --git a/x/fswap/keeper/calc_test.go b/x/fswap/keeper/calc_test.go new file mode 100644 index 0000000000..ed8cece9f5 --- /dev/null +++ b/x/fswap/keeper/calc_test.go @@ -0,0 +1,65 @@ +package keeper + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/require" + + sdk "github.com/Finschia/finschia-sdk/types" +) + +func TestCalcSwap(t *testing.T) { + rateDot5, err := sdk.NewDecFromStr("0.5") + require.NoError(t, err) + rateDot3maxPrecision, err := sdk.NewDecFromStr("0.333333333333333333") + require.NoError(t, err) + + finschiaSwapRate, err := sdk.NewDecFromStr("148.079656") + require.NoError(t, err) + conySwapRate := finschiaSwapRate.Mul(sdk.NewDec(1000000)) + pebSwapRateForCony, err := sdk.NewDecFromStr("148079656000000") + require.NoError(t, err) + testCases := map[string]struct { + fromAmount sdk.Int + expectedAmount sdk.Int + swapRate sdk.Dec + }{ + "swapRate 0.5": { + fromAmount: sdk.ZeroInt(), + swapRate: rateDot5, + expectedAmount: sdk.ZeroInt(), + }, + "swapRate 0.333333333333333333": { + fromAmount: sdk.NewInt(3), + swapRate: rateDot3maxPrecision, + expectedAmount: sdk.ZeroInt(), + }, + "swapRate conySwapRate(148.079656 * 10^6) fromAmount(1)": { + fromAmount: sdk.NewInt(1), + swapRate: conySwapRate, + expectedAmount: sdk.NewInt(148079656), + }, + "swapRate conySwapRate(148.079656 * 10^6) fromAmount(3)": { + fromAmount: sdk.NewInt(3), + swapRate: conySwapRate, + expectedAmount: sdk.NewInt(444238968), + }, + "pebSwapRateForCony pebSwapRateForCony(148.079656 * 10^12) fromAmount(1)": { + fromAmount: sdk.NewInt(1), + swapRate: pebSwapRateForCony, + expectedAmount: sdk.NewInt(148079656000000), + }, + "pebSwapRateForCony pebSwapRateForCony(148.079656 * 10^12) fromAmount(3)": { + fromAmount: sdk.NewInt(3), + swapRate: pebSwapRateForCony, + expectedAmount: sdk.NewInt(444238968000000), + }, + } + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + actualAmount := CalcSwap(tc.swapRate, tc.fromAmount) + require.Equal(t, tc.expectedAmount, actualAmount, fmt.Sprintf("tc.expectedAmount = %v, actualAmount = %v", tc.expectedAmount, actualAmount)) + }) + } +} diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index a637c3a5e1..f21a246cea 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -40,7 +40,7 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Co return err } - newCoinAmountInt := k.CalcSwap(swap.SwapRate, fromCoinAmount.Amount) + newCoinAmountInt := CalcSwap(swap.SwapRate, fromCoinAmount.Amount) newCoinAmount := sdk.NewCoin(toDenom, newCoinAmountInt) swapped, err := k.getSwapped(ctx, swap.GetFromDenom(), swap.GetToDenom()) if err != nil { @@ -82,12 +82,6 @@ func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, fromCoinAmount sdk.Co return nil } -func (k Keeper) CalcSwap(swapRate sdk.Dec, fromCoinAmount sdk.Int) sdk.Int { - multipliedAmountDec := swapRate.Mul(sdk.NewDecFromBigInt(fromCoinAmount.BigInt())).TruncateInt() - multipliedAmountInt := sdk.NewIntFromBigInt(multipliedAmountDec.BigInt()) - return multipliedAmountInt -} - func (k Keeper) getAllSwapped(ctx sdk.Context) []types.Swapped { swappedSlice := []types.Swapped{} k.iterateAllSwapped(ctx, func(swapped types.Swapped) bool { diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 172a282be8..fdf9e95d25 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -2,7 +2,6 @@ package keeper_test import ( "context" - "fmt" "testing" "github.com/stretchr/testify/suite" @@ -188,58 +187,3 @@ func (s *KeeperTestSuite) TestSwap() { }) } } - -func (s *KeeperTestSuite) TestCalcSwap() { - rateDot5, err := sdk.NewDecFromStr("0.5") - s.Require().NoError(err) - rateDot3maxPrecision, err := sdk.NewDecFromStr("0.333333333333333333") - s.Require().NoError(err) - - finschiaSwapRate, err := sdk.NewDecFromStr("148.079656") - s.Require().NoError(err) - conySwapRate := finschiaSwapRate.Mul(sdk.NewDec(1000000)) - pebSwapRateForCony, err := sdk.NewDecFromStr("148079656000000") - s.Require().NoError(err) - testCases := map[string]struct { - fromAmount sdk.Int - expectedAmount sdk.Int - swapRate sdk.Dec - }{ - "swapRate 0.5": { - fromAmount: sdk.ZeroInt(), - swapRate: rateDot5, - expectedAmount: sdk.ZeroInt(), - }, - "swapRate 0.333333333333333333": { - fromAmount: sdk.NewInt(3), - swapRate: rateDot3maxPrecision, - expectedAmount: sdk.ZeroInt(), - }, - "swapRate conySwapRate(148.079656 * 10^6) fromAmount(1)": { - fromAmount: sdk.NewInt(1), - swapRate: conySwapRate, - expectedAmount: sdk.NewInt(148079656), - }, - "swapRate conySwapRate(148.079656 * 10^6) fromAmount(3)": { - fromAmount: sdk.NewInt(3), - swapRate: conySwapRate, - expectedAmount: sdk.NewInt(444238968), - }, - "pebSwapRateForCony pebSwapRateForCony(148.079656 * 10^12) fromAmount(1)": { - fromAmount: sdk.NewInt(1), - swapRate: pebSwapRateForCony, - expectedAmount: sdk.NewInt(148079656000000), - }, - "pebSwapRateForCony pebSwapRateForCony(148.079656 * 10^12) fromAmount(3)": { - fromAmount: sdk.NewInt(3), - swapRate: pebSwapRateForCony, - expectedAmount: sdk.NewInt(444238968000000), - }, - } - for name, tc := range testCases { - s.Run(name, func() { - actualAmount := s.keeper.CalcSwap(tc.swapRate, tc.fromAmount) - s.Require().Equal(tc.expectedAmount, actualAmount, fmt.Sprintf("tc.expectedAmount = %v, actualAmount = %v", tc.expectedAmount, actualAmount)) - }) - } -} From 4cdde22776420b8d5206a748dac9a78a189fae63 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 11:34:51 +0900 Subject: [PATCH 34/47] chore: add missing query cmd --- x/fswap/client/cli/query.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/x/fswap/client/cli/query.go b/x/fswap/client/cli/query.go index d5b1a60778..4e5f0db89b 100644 --- a/x/fswap/client/cli/query.go +++ b/x/fswap/client/cli/query.go @@ -24,6 +24,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command { cmd.AddCommand( CmdQuerySwapped(), CmdQueryTotalSwappableAmount(), + CmdQuerySwaps(), ) return cmd } @@ -77,3 +78,28 @@ func CmdQueryTotalSwappableAmount() *cobra.Command { flags.AddQueryFlagsToCmd(cmd) return cmd } + +func CmdQuerySwaps() *cobra.Command { + cmd := &cobra.Command{ + Use: "swaps", + Short: "shows the all the swaps that proposed", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + queryClient := types.NewQueryClient(clientCtx) + + res, err := queryClient.Swaps(cmd.Context(), &types.QuerySwapsRequest{}) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + return cmd +} From d00a3d12089ab57d3a00a99c1bac9677a41beb81 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 15:20:05 +0900 Subject: [PATCH 35/47] chore: fix for proposal cli --- x/fswap/client/cli/tx.go | 21 +++++++++++---------- x/fswap/types/proposal.go | 8 ++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index 98f6f41e48..75bc1dd1d1 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -9,7 +9,6 @@ import ( "github.com/Finschia/finschia-sdk/client" "github.com/Finschia/finschia-sdk/client/flags" "github.com/Finschia/finschia-sdk/client/tx" - "github.com/Finschia/finschia-sdk/codec" sdk "github.com/Finschia/finschia-sdk/types" bank "github.com/Finschia/finschia-sdk/x/bank/types" "github.com/Finschia/finschia-sdk/x/fswap/types" @@ -199,7 +198,7 @@ Example of the content of messages-json: SwapRate: swapRateDec, } - toDenomMetadata, err := parseToDenomMetadata(clientCtx.Codec, args[0]) + toDenomMetadata, err := parseToDenomMetadata(args[0]) if err != nil { return err } @@ -230,21 +229,23 @@ Example of the content of messages-json: cmd.Flags().String(FlagFromDenom, "", "cony") cmd.Flags().String(FlagToDenom, "", "PDT") cmd.Flags().Int64(FlagAmountCapForToDenom, 0, "tbd") - cmd.Flags().Int64(FlagSwapRate, 0, "tbd") + cmd.Flags().String(FlagSwapRate, "0", "tbd") return cmd } -func parseToDenomMetadata(cdc codec.Codec, jsonDenomMetadata string) (bank.Metadata, error) { - cliMsg := json.RawMessage{} - if err := json.Unmarshal([]byte(jsonDenomMetadata), &cliMsg); err != nil { +func parseToDenomMetadata(jsonDenomMetadata string) (bank.Metadata, error) { + type toDenomMeta struct { + Metadata bank.Metadata `json:"metadata"` + } + denomMeta := toDenomMeta{} + if err := json.Unmarshal([]byte(jsonDenomMetadata), &denomMeta); err != nil { return bank.Metadata{}, err } - metadata := bank.Metadata{} - err := cdc.UnmarshalInterfaceJSON(cliMsg, &metadata) - if err != nil { + if err := denomMeta.Metadata.Validate(); err != nil { return bank.Metadata{}, err } - return metadata, nil + + return denomMeta.Metadata, nil } diff --git a/x/fswap/types/proposal.go b/x/fswap/types/proposal.go index 91b2e3413f..6aba55a5c9 100644 --- a/x/fswap/types/proposal.go +++ b/x/fswap/types/proposal.go @@ -8,12 +8,12 @@ import ( ) const ( - ProposalTypeSwap string = "Swap" + ProposalTypeMakeSwap string = "MakeSwap" ) // NewMakeSwapProposal creates a new SwapProposal instance. // Deprecated: this proposal is considered legacy and is deprecated in favor of -// Msg-based gov proposals. See MsgSwap. +// Msg-based gov proposals. See MakeSwapProposal. func NewMakeSwapProposal(title, description string, swap Swap, toDenomMetadata bank.Metadata) *MakeSwapProposal { return &MakeSwapProposal{title, description, swap, toDenomMetadata} } @@ -22,14 +22,14 @@ func NewMakeSwapProposal(title, description string, swap Swap, toDenomMetadata b var _ gov.Content = &MakeSwapProposal{} func init() { - gov.RegisterProposalType(ProposalTypeSwap) + gov.RegisterProposalType(ProposalTypeMakeSwap) } // ProposalRoute gets the proposal's router key func (m *MakeSwapProposal) ProposalRoute() string { return RouterKey } // ProposalType is "Swap" -func (m *MakeSwapProposal) ProposalType() string { return ProposalTypeSwap } +func (m *MakeSwapProposal) ProposalType() string { return ProposalTypeMakeSwap } // String implements the Stringer interface. func (m *MakeSwapProposal) String() string { From 47d684fb20e155b9e07a4b5367d6c7804ff22442 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 15:57:00 +0900 Subject: [PATCH 36/47] chore: fix for init timing --- simapp/app.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index adc724c281..7bb68f2e01 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -343,6 +343,9 @@ func NewSimApp( app.AuthzKeeper = authzkeeper.NewKeeper(keys[authzkeeper.StoreKey], appCodec, app.BaseApp.MsgServiceRouter()) + fswapConfig := fswaptypes.DefaultConfig() + app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], fswapConfig, app.BankKeeper) + // register the proposal types govRouter := govtypes.NewRouter() govRouter.AddRoute(govtypes.RouterKey, govtypes.ProposalHandler). @@ -370,10 +373,6 @@ func NewSimApp( // If evidence needs to be handled for the app, set routes in router here and seal app.EvidenceKeeper = *evidenceKeeper - /**** Phase 1 ****/ - fswapConfig := fswaptypes.DefaultConfig() - app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], fswapConfig, app.BankKeeper) - app.FbridgeKeeper = fbridgekeeper.NewKeeper(appCodec, keys[fbridgetypes.StoreKey], app.AccountKeeper, app.BankKeeper, "stake", authtypes.NewModuleAddress(govtypes.ModuleName).String()) /**** Module Options ****/ From d15f3e5f48bd4b5ecdd01afac1dad83d89e41717 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 16:28:50 +0900 Subject: [PATCH 37/47] chore: fix query cli --- x/fswap/client/cli/query.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/x/fswap/client/cli/query.go b/x/fswap/client/cli/query.go index 4e5f0db89b..46c87aa868 100644 --- a/x/fswap/client/cli/query.go +++ b/x/fswap/client/cli/query.go @@ -31,17 +31,20 @@ func GetQueryCmd(queryRoute string) *cobra.Command { func CmdQuerySwapped() *cobra.Command { cmd := &cobra.Command{ - Use: "swapped", + Use: "swapped [from_denom] [to_denom]", Short: "shows the current swap status, including both old and new coin amount", - Args: cobra.NoArgs, + Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { clientCtx, err := client.GetClientQueryContext(cmd) if err != nil { return err } queryClient := types.NewQueryClient(clientCtx) - - res, err := queryClient.Swapped(cmd.Context(), &types.QuerySwappedRequest{}) + req := &types.QuerySwappedRequest{ + FromDenom: args[0], + ToDenom: args[1], + } + res, err := queryClient.Swapped(cmd.Context(), req) if err != nil { return err } @@ -56,9 +59,9 @@ func CmdQuerySwapped() *cobra.Command { func CmdQueryTotalSwappableAmount() *cobra.Command { cmd := &cobra.Command{ - Use: "total-swappable-amount", + Use: "total-swappable-amount [from_denom] [to_denom]", Short: "shows the current total amount of new coin that're swappable", - Args: cobra.NoArgs, + Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { clientCtx, err := client.GetClientQueryContext(cmd) if err != nil { @@ -66,7 +69,11 @@ func CmdQueryTotalSwappableAmount() *cobra.Command { } queryClient := types.NewQueryClient(clientCtx) - res, err := queryClient.TotalSwappableToCoinAmount(cmd.Context(), &types.QueryTotalSwappableToCoinAmountRequest{}) + req := &types.QueryTotalSwappableToCoinAmountRequest{ + FromDenom: args[0], + ToDenom: args[1], + } + res, err := queryClient.TotalSwappableToCoinAmount(cmd.Context(), req) if err != nil { return err } From 54cce5a306be53272754a8497a1ddc7adadf4b88 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 16:31:41 +0900 Subject: [PATCH 38/47] chore: fix to prevent generating swap more than MaxSwaps --- x/fswap/keeper/proposal.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/x/fswap/keeper/proposal.go b/x/fswap/keeper/proposal.go index 7f202de690..7ebb8a8df9 100644 --- a/x/fswap/keeper/proposal.go +++ b/x/fswap/keeper/proposal.go @@ -9,6 +9,13 @@ import ( ) func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank.Metadata) error { + stats, err := k.getSwapStats(ctx) + if err != nil { + return err + } + if int(stats.SwapCount) >= k.config.MaxSwaps && !k.isUnlimited() { + return types.ErrCanNotHaveMoreSwap.Wrapf("cannot initialize genesis state, there are more than %d swaps", k.config.MaxSwaps) + } isNewSwap := true if _, err := k.getSwap(ctx, swap.FromDenom, swap.ToDenom); err == nil { isNewSwap = false From c0950b6047ba237549699687ed7eece277dc7368 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 16:43:54 +0900 Subject: [PATCH 39/47] fix: add missing call validateBasic for proposal --- x/fswap/types/proposal.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x/fswap/types/proposal.go b/x/fswap/types/proposal.go index 6aba55a5c9..5891f449d0 100644 --- a/x/fswap/types/proposal.go +++ b/x/fswap/types/proposal.go @@ -42,5 +42,8 @@ func (m *MakeSwapProposal) ValidateBasic() error { if err := m.Swap.ValidateBasic(); err != nil { return err } + if err := m.ToDenomMetadata.Validate(); err != nil { + return err + } return gov.ValidateAbstract(m) } From 4b491157f9d0f9329e10385701bb6103f105ab9d Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 16:46:30 +0900 Subject: [PATCH 40/47] fix: --- x/fswap/keeper/proposal.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/x/fswap/keeper/proposal.go b/x/fswap/keeper/proposal.go index 7ebb8a8df9..e9eb99dc23 100644 --- a/x/fswap/keeper/proposal.go +++ b/x/fswap/keeper/proposal.go @@ -14,7 +14,7 @@ func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank. return err } if int(stats.SwapCount) >= k.config.MaxSwaps && !k.isUnlimited() { - return types.ErrCanNotHaveMoreSwap.Wrapf("cannot initialize genesis state, there are more than %d swaps", k.config.MaxSwaps) + return types.ErrCanNotHaveMoreSwap.Wrapf("cannot make more swaps, max swaps is %d", k.config.MaxSwaps) } isNewSwap := true if _, err := k.getSwap(ctx, swap.FromDenom, swap.ToDenom); err == nil { @@ -47,8 +47,7 @@ func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank. } } - err := k.setSwap(ctx, swap) - if err != nil { + if err := k.setSwap(ctx, swap); err != nil { return err } From 7ef5b3fee5aba481f0b78a041c488d63edd9fe75 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 18:10:16 +0900 Subject: [PATCH 41/47] chore: change error type for temporal test-pass --- x/fswap/keeper/proposal_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/fswap/keeper/proposal_test.go b/x/fswap/keeper/proposal_test.go index 4dbe0e63b2..5660470812 100644 --- a/x/fswap/keeper/proposal_test.go +++ b/x/fswap/keeper/proposal_test.go @@ -41,7 +41,7 @@ func (s *KeeperTestSuite) TestMakeSwapProposal() { Symbol: s.toDenomMetadata.Symbol, }, true, - sdkerrors.ErrInvalidRequest, + types.ErrCanNotHaveMoreSwap, // TODO(bjs) check with maxSwap(2) }, } for name, tc := range testCases { From f3d7632dae09bc8bd91524c8e2deb2617295310f Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 18:30:45 +0900 Subject: [PATCH 42/47] chore: fix for make-swap-proposal update --- x/fswap/keeper/proposal.go | 2 +- x/fswap/keeper/proposal_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x/fswap/keeper/proposal.go b/x/fswap/keeper/proposal.go index e9eb99dc23..791c8e51ad 100644 --- a/x/fswap/keeper/proposal.go +++ b/x/fswap/keeper/proposal.go @@ -13,7 +13,7 @@ func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank. if err != nil { return err } - if int(stats.SwapCount) >= k.config.MaxSwaps && !k.isUnlimited() { + if int(stats.SwapCount) > k.config.MaxSwaps && !k.isUnlimited() { return types.ErrCanNotHaveMoreSwap.Wrapf("cannot make more swaps, max swaps is %d", k.config.MaxSwaps) } isNewSwap := true diff --git a/x/fswap/keeper/proposal_test.go b/x/fswap/keeper/proposal_test.go index 5660470812..4dbe0e63b2 100644 --- a/x/fswap/keeper/proposal_test.go +++ b/x/fswap/keeper/proposal_test.go @@ -41,7 +41,7 @@ func (s *KeeperTestSuite) TestMakeSwapProposal() { Symbol: s.toDenomMetadata.Symbol, }, true, - types.ErrCanNotHaveMoreSwap, // TODO(bjs) check with maxSwap(2) + sdkerrors.ErrInvalidRequest, }, } for name, tc := range testCases { From 9854cf3a317706804927e651df4788ec65aba09c Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 18:52:24 +0900 Subject: [PATCH 43/47] chore: fix to check swap count after increase in order not to make more than limit --- x/fswap/keeper/proposal.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/x/fswap/keeper/proposal.go b/x/fswap/keeper/proposal.go index 791c8e51ad..1c726e67a6 100644 --- a/x/fswap/keeper/proposal.go +++ b/x/fswap/keeper/proposal.go @@ -9,13 +9,6 @@ import ( ) func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank.Metadata) error { - stats, err := k.getSwapStats(ctx) - if err != nil { - return err - } - if int(stats.SwapCount) > k.config.MaxSwaps && !k.isUnlimited() { - return types.ErrCanNotHaveMoreSwap.Wrapf("cannot make more swaps, max swaps is %d", k.config.MaxSwaps) - } isNewSwap := true if _, err := k.getSwap(ctx, swap.FromDenom, swap.ToDenom); err == nil { isNewSwap = false @@ -31,6 +24,15 @@ func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank. } } + stats, err := k.getSwapStats(ctx) + if err != nil { + return err + } + + if int(stats.SwapCount) > k.config.MaxSwaps && !k.isUnlimited() { + return types.ErrCanNotHaveMoreSwap.Wrapf("cannot make more swaps, max swaps is %d", k.config.MaxSwaps) + } + if isNewSwap { swapped := types.Swapped{ FromCoinAmount: sdk.Coin{ From 472d5b4b2346069c7734d34660bf1dfd09bc8f4b Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 20:28:34 +0900 Subject: [PATCH 44/47] chore: prevent any proposal with different denom --- x/fswap/types/proposal.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/x/fswap/types/proposal.go b/x/fswap/types/proposal.go index 5891f449d0..99048caa57 100644 --- a/x/fswap/types/proposal.go +++ b/x/fswap/types/proposal.go @@ -3,6 +3,7 @@ package types import ( "gopkg.in/yaml.v2" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" bank "github.com/Finschia/finschia-sdk/x/bank/types" gov "github.com/Finschia/finschia-sdk/x/gov/types" ) @@ -45,5 +46,9 @@ func (m *MakeSwapProposal) ValidateBasic() error { if err := m.ToDenomMetadata.Validate(); err != nil { return err } + if m.Swap.ToDenom != m.ToDenomMetadata.Base { + return sdkerrors.ErrInvalidRequest.Wrapf("denomination does not match %s != %s", m.Swap.ToDenom, m.ToDenomMetadata.Base) + } + return gov.ValidateAbstract(m) } From 70f9499ab1a68e3d7e88b4b919914db5d24270c4 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 21:00:17 +0900 Subject: [PATCH 45/47] chore: change amount cap type --- x/fswap/client/cli/tx.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index 75bc1dd1d1..fcf1d1ce59 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -177,8 +177,11 @@ Example of the content of messages-json: if err != nil { return err } - - amountCap, err := cmd.Flags().GetInt64(FlagAmountCapForToDenom) + amountCapStr, err := cmd.Flags().GetString(FlagAmountCapForToDenom) + if err != nil { + return err + } + amountCap, err := sdk.NewDecFromStr(amountCapStr) if err != nil { return err } @@ -194,7 +197,7 @@ Example of the content of messages-json: swap := types.Swap{ FromDenom: fromDenom, ToDenom: toDenom, - AmountCapForToDenom: sdk.NewInt(amountCap), + AmountCapForToDenom: amountCap.TruncateInt(), SwapRate: swapRateDec, } @@ -228,7 +231,7 @@ Example of the content of messages-json: cmd.Flags().String(govcli.FlagDeposit, "", "deposit of proposal") cmd.Flags().String(FlagFromDenom, "", "cony") cmd.Flags().String(FlagToDenom, "", "PDT") - cmd.Flags().Int64(FlagAmountCapForToDenom, 0, "tbd") + cmd.Flags().String(FlagAmountCapForToDenom, "0", "tbd") cmd.Flags().String(FlagSwapRate, "0", "tbd") return cmd From ebefc77ad41e00bc1b94a521bcbcc6dd80135fcf Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 21:38:35 +0900 Subject: [PATCH 46/47] chore: fix codec-related parts --- x/fswap/codec/codec.go | 18 ++++++++++++++++++ x/fswap/module.go | 8 +------- x/fswap/types/codec.go | 36 +++++++++++++----------------------- x/fswap/types/msgs.go | 11 +++++++++++ 4 files changed, 43 insertions(+), 30 deletions(-) create mode 100644 x/fswap/codec/codec.go diff --git a/x/fswap/codec/codec.go b/x/fswap/codec/codec.go new file mode 100644 index 0000000000..7cbd4f6a69 --- /dev/null +++ b/x/fswap/codec/codec.go @@ -0,0 +1,18 @@ +package codec + +import ( + "github.com/Finschia/finschia-sdk/codec" + cryptocodec "github.com/Finschia/finschia-sdk/crypto/codec" + sdk "github.com/Finschia/finschia-sdk/types" +) + +var ( + Amino = codec.NewLegacyAmino() + ModuleCdc = codec.NewAminoCodec(Amino) +) + +func init() { + cryptocodec.RegisterCrypto(Amino) + codec.RegisterEvidences(Amino) + sdk.RegisterLegacyAminoCodec(Amino) +} diff --git a/x/fswap/module.go b/x/fswap/module.go index 9687b4cff8..d561bbe296 100644 --- a/x/fswap/module.go +++ b/x/fswap/module.go @@ -42,13 +42,7 @@ func (AppModuleBasic) Name() string { return types.ModuleName } -func (AppModuleBasic) RegisterCodec(cdc *codec.LegacyAmino) { - types.RegisterCodec(cdc) -} - -func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - types.RegisterCodec(cdc) -} +func (AppModuleBasic) RegisterLegacyAminoCodec(_ *codec.LegacyAmino) {} // RegisterInterfaces registers the module's interface types func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) { diff --git a/x/fswap/types/codec.go b/x/fswap/types/codec.go index e0419b7755..de8865ec57 100644 --- a/x/fswap/types/codec.go +++ b/x/fswap/types/codec.go @@ -2,48 +2,38 @@ package types import ( "github.com/Finschia/finschia-sdk/codec" + "github.com/Finschia/finschia-sdk/codec/legacy" "github.com/Finschia/finschia-sdk/codec/types" - cryptocodec "github.com/Finschia/finschia-sdk/crypto/codec" sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/types/msgservice" - authzcodec "github.com/Finschia/finschia-sdk/x/authz/codec" - fdncodec "github.com/Finschia/finschia-sdk/x/foundation/codec" + fscodec "github.com/Finschia/finschia-sdk/x/fswap/codec" govcodec "github.com/Finschia/finschia-sdk/x/gov/codec" govtypes "github.com/Finschia/finschia-sdk/x/gov/types" ) -func RegisterCodec(cdc *codec.LegacyAmino) { - // this line is used by starport scaffolding # 2 -} - +// RegisterLegacyAminoCodec registers concrete types on the LegacyAmino codec func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&MakeSwapProposal{}, "finschia-sdk/MakeSwapProposal", nil) + legacy.RegisterAminoMsg(cdc, &MsgSwap{}, "lbm-sdk/MsgSwap") + legacy.RegisterAminoMsg(cdc, &MsgSwapAll{}, "lbm-sdk/MsgSwapAll") + + cdc.RegisterConcrete(&MakeSwapProposal{}, "lbm-sdk/MakeSwapProposal", nil) } func RegisterInterfaces(registry types.InterfaceRegistry) { - // this line is used by starport scaffolding # 3 + registry.RegisterImplementations((*sdk.Msg)(nil), + &MsgSwap{}, + &MsgSwapAll{}, + ) msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) + registry.RegisterImplementations( (*govtypes.Content)(nil), &MakeSwapProposal{}, ) } -var ( - amino = codec.NewLegacyAmino() - ModuleCdc = codec.NewProtoCodec(types.NewInterfaceRegistry()) -) - func init() { - RegisterLegacyAminoCodec(amino) - cryptocodec.RegisterCrypto(amino) - sdk.RegisterLegacyAminoCodec(amino) - - // Register all Amino interfaces and concrete types on the authz and gov Amino codec - // so that this can later be used to properly serialize MsgGrant and MsgExec - // instances. - RegisterLegacyAminoCodec(authzcodec.Amino) RegisterLegacyAminoCodec(govcodec.Amino) - RegisterLegacyAminoCodec(fdncodec.Amino) + RegisterLegacyAminoCodec(fscodec.Amino) } diff --git a/x/fswap/types/msgs.go b/x/fswap/types/msgs.go index 038d8baebd..eae8c6a2c2 100644 --- a/x/fswap/types/msgs.go +++ b/x/fswap/types/msgs.go @@ -3,6 +3,7 @@ package types import ( sdk "github.com/Finschia/finschia-sdk/types" sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + "github.com/Finschia/finschia-sdk/x/fswap/codec" ) var _ sdk.Msg = &MsgSwap{} @@ -38,6 +39,11 @@ func (m *MsgSwap) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{from} } +// GetSignBytes implements the LegacyMsg.GetSignBytes method. +func (m *MsgSwap) GetSignBytes() []byte { + return sdk.MustSortJSON(codec.ModuleCdc.MustMarshalJSON(m)) +} + var _ sdk.Msg = &MsgSwapAll{} // ValidateBasic Implements Msg. @@ -66,3 +72,8 @@ func (m *MsgSwapAll) GetSigners() []sdk.AccAddress { } return []sdk.AccAddress{from} } + +// GetSignBytes implements the LegacyMsg.GetSignBytes method. +func (m *MsgSwapAll) GetSignBytes() []byte { + return sdk.MustSortJSON(codec.ModuleCdc.MustMarshalJSON(m)) +} From 99f9193b7657a602af58a33afedb1e016253b6b5 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 3 May 2024 21:41:24 +0900 Subject: [PATCH 47/47] chore: minor change for convert to-coin-amount-cap --- x/fswap/client/cli/tx.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index fcf1d1ce59..433c8ae33e 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -10,6 +10,7 @@ import ( "github.com/Finschia/finschia-sdk/client/flags" "github.com/Finschia/finschia-sdk/client/tx" sdk "github.com/Finschia/finschia-sdk/types" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" bank "github.com/Finschia/finschia-sdk/x/bank/types" "github.com/Finschia/finschia-sdk/x/fswap/types" govcli "github.com/Finschia/finschia-sdk/x/gov/client/cli" @@ -181,9 +182,9 @@ Example of the content of messages-json: if err != nil { return err } - amountCap, err := sdk.NewDecFromStr(amountCapStr) - if err != nil { - return err + amountCap, ok := sdk.NewIntFromString(amountCapStr) + if !ok { + return sdkerrors.ErrInvalidRequest.Wrapf("failed to parse %s %s", FlagAmountCapForToDenom, amountCap.String()) } swapRate, err := cmd.Flags().GetString(FlagSwapRate) @@ -197,7 +198,7 @@ Example of the content of messages-json: swap := types.Swap{ FromDenom: fromDenom, ToDenom: toDenom, - AmountCapForToDenom: amountCap.TruncateInt(), + AmountCapForToDenom: amountCap, SwapRate: swapRateDec, }