Skip to content

Commit

Permalink
chore: update proto
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeseung-bae committed Apr 30, 2024
1 parent 6151a0c commit be5f723
Show file tree
Hide file tree
Showing 12 changed files with 461 additions and 179 deletions.
33 changes: 18 additions & 15 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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|

<!-- end services -->

Expand All @@ -12927,41 +12927,44 @@ GenesisState defines the fswap module's genesis state.



<a name="lbm.fswap.v1.MsgSwapAllRequest"></a>
<a name="lbm.fswap.v1.MsgSwap"></a>

### 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) | | |






<a name="lbm.fswap.v1.MsgSwapAllResponse"></a>
<a name="lbm.fswap.v1.MsgSwapAll"></a>

### MsgSwapAllResponse
### MsgSwapAll



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `from_address` | [string](#string) | | holder's address |
| `from_denom` | [string](#string) | | |
| `to_denom` | [string](#string) | | |




<a name="lbm.fswap.v1.MsgSwapRequest"></a>

### MsgSwapRequest

<a name="lbm.fswap.v1.MsgSwapAllResponse"></a>

### 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 |



Expand Down Expand Up @@ -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) | | |

<!-- end services -->

Expand Down
2 changes: 1 addition & 1 deletion proto/lbm/fswap/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}

Expand Down
11 changes: 7 additions & 4 deletions proto/lbm/fswap/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
4 changes: 2 additions & 2 deletions x/fswap/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func CmdTxMsgSwap() *cobra.Command {
return err
}

msg := &types.MsgSwapRequest{
msg := &types.MsgSwap{
FromAddress: clientCtx.GetFromAddress().String(),
FromCoinAmount: coin,
}
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 11 additions & 11 deletions x/fswap/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 77 in x/fswap/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/keeper.go#L77

Added line #L77 was not covered by tests
}

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)

Check warning on line 85 in x/fswap/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/keeper.go#L85

Added line #L85 was not covered by tests
}

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

Check warning on line 91 in x/fswap/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/keeper.go#L91

Added line #L91 was not covered by tests
}
Expand Down Expand Up @@ -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

Check warning on line 131 in x/fswap/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/keeper.go#L131

Added line #L131 was not covered by tests
}
return nil
Expand All @@ -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
Expand Down
16 changes: 14 additions & 2 deletions x/fswap/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,41 +108,47 @@ func (s *KeeperTestSuite) TestSwap() {
testCases := map[string]struct {
from sdk.AccAddress
amountToSwap sdk.Coin
toDenom string
expectedBalanceWithoutMultiply sdk.Int
shouldThrowError bool
expectedError error
}{
"swap some": {
s.accWithFromCoin,
sdk.NewCoin(s.fromDenom, sdk.NewInt(100)),
s.toDenom,
sdk.NewInt(100),
false,
nil,
},
"swap all the balance": {
s.accWithFromCoin,
sdk.NewCoin(s.fromDenom, s.initBalance),
s.toDenom,
s.initBalance,
false,
nil,
},
"swap without holding enough balance": {
s.accWithFromCoin,
sdk.NewCoin(s.fromDenom, sdk.OneInt().Add(s.initBalance)),
s.toDenom,
sdk.ZeroInt(),
true,
sdkerrors.ErrInsufficientFunds,
},
"account holding new coin only": {
s.accWithToCoin,
sdk.NewCoin(s.fromDenom, sdk.NewInt(100)),
s.toDenom,
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.ZeroInt(),
true,
sdkerrors.ErrInvalidRequest,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down
22 changes: 4 additions & 18 deletions x/fswap/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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

Check warning on line 25 in x/fswap/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/msg_server.go#L25

Added line #L25 was not covered by tests
}

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

Check warning on line 40 in x/fswap/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/msg_server.go#L40

Added line #L40 was not covered by tests
}

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
}

Expand Down
Loading

0 comments on commit be5f723

Please sign in to comment.