Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(x/fswap): introduce new event(MakeSwapProposal) (backport #1363) #1364

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements
* (types) [\#1314](https://github.com/Finschia/finschia-sdk/pull/1314) replace IsEqual with Equal
* (x/fswap) [\#1363](https://github.com/Finschia/finschia-sdk/pull/1363) introduce new event for MakeSwapProposal

### Bug Fixes
* (x/auth) [#1281](https://github.com/Finschia/finschia-sdk/pull/1281) `ModuleAccount.Validate` now reports a nil `.BaseAccount` instead of panicking. (backport #1274)
Expand Down
104 changes: 68 additions & 36 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -973,15 +973,17 @@

- [Msg](#lbm.foundation.v1.Msg)

- [lbm/fswap/v1/event.proto](#lbm/fswap/v1/event.proto)
- [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)
- [SwapStats](#lbm.fswap.v1.SwapStats)
- [Swapped](#lbm.fswap.v1.Swapped)

- [lbm/fswap/v1/event.proto](#lbm/fswap/v1/event.proto)
- [EventAddDenomMetadata](#lbm.fswap.v1.EventAddDenomMetadata)
- [EventMakeSwap](#lbm.fswap.v1.EventMakeSwap)
- [EventSwapCoins](#lbm.fswap.v1.EventSwapCoins)

- [lbm/fswap/v1/genesis.proto](#lbm/fswap/v1/genesis.proto)
- [GenesisState](#lbm.fswap.v1.GenesisState)

Expand Down Expand Up @@ -14481,39 +14483,6 @@ Msg defines the foundation Msg service.



<a name="lbm/fswap/v1/event.proto"></a>
<p align="right"><a href="#top">Top</a></p>

## lbm/fswap/v1/event.proto



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

### EventSwapCoins



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `address` | [string](#string) | | holder's address |
| `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 |





<!-- end messages -->

<!-- end enums -->

<!-- end HasExtensions -->

<!-- end services -->



<a name="lbm/fswap/v1/fswap.proto"></a>
<p align="right"><a href="#top">Top</a></p>

Expand Down Expand Up @@ -14587,6 +14556,69 @@ From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk base



<!-- end messages -->

<!-- end enums -->

<!-- end HasExtensions -->

<!-- end services -->



<a name="lbm/fswap/v1/event.proto"></a>
<p align="right"><a href="#top">Top</a></p>

## lbm/fswap/v1/event.proto



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

### EventAddDenomMetadata



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `metadata` | [cosmos.bank.v1beta1.Metadata](#cosmos.bank.v1beta1.Metadata) | | |






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

### EventMakeSwap



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `swap` | [Swap](#lbm.fswap.v1.Swap) | | |






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

### EventSwapCoins



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `address` | [string](#string) | | holder's address |
| `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 |





<!-- end messages -->

<!-- end enums -->
Expand Down
11 changes: 11 additions & 0 deletions proto/lbm/fswap/v1/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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";
import "lbm/fswap/v1/fswap.proto";

message EventSwapCoins {
// holder's address
Expand All @@ -16,3 +18,12 @@ message EventSwapCoins {
cosmos.base.v1beta1.Coin to_coin_amount = 3
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"];
}

message EventMakeSwap {
Swap swap = 1 [(gogoproto.nullable) = false];
}

message EventAddDenomMetadata {
cosmos.bank.v1beta1.Metadata metadata = 1
[(gogoproto.moretags) = "yaml:\"denom_metadata\"", (gogoproto.nullable) = false];
}
8 changes: 7 additions & 1 deletion x/fswap/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,25 @@ func (k Keeper) MakeSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank.
}
}

eventManager := ctx.EventManager()
if err := k.setSwap(ctx, swap); err != nil {
return err
}
if err := eventManager.EmitTypedEvent(&types.EventMakeSwap{Swap: swap}); err != nil {
panic(err)
}

existingMetadata, ok := k.GetDenomMetaData(ctx, swap.ToDenom)
if !ok {
k.SetDenomMetaData(ctx, toDenomMetadata)
if err := eventManager.EmitTypedEvent(&(types.EventAddDenomMetadata{Metadata: toDenomMetadata})); err != nil {
panic(err)
}
return nil
}
if !denomMetadataEqual(existingMetadata, toDenomMetadata) {
return errors.ErrInvalidRequest.Wrap("changing existing metadata not allowed")
}

return nil
}

Expand Down
33 changes: 31 additions & 2 deletions x/fswap/keeper/proposal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper_test

import (
abci "github.com/tendermint/tendermint/abci/types"

sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
bank "github.com/Finschia/finschia-sdk/x/bank/types"
Expand All @@ -13,6 +15,7 @@ func (s *KeeperTestSuite) TestMakeSwapProposal() {
toDenomMeta bank.Metadata
existingMetadata bool
expectedError error
expectedEvents sdk.Events
}{
"valid": {
types.Swap{
Expand All @@ -24,6 +27,28 @@ func (s *KeeperTestSuite) TestMakeSwapProposal() {
s.toDenomMetadata,
false,
nil,
sdk.Events{
sdk.Event{
Type: "lbm.fswap.v1.EventMakeSwap",
Attributes: []abci.EventAttribute{
{
Key: []byte("swap"),
Value: []uint8{0x7b, 0x22, 0x66, 0x72, 0x6f, 0x6d, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x66, 0x72, 0x6f, 0x6d, 0x44, 0x22, 0x2c, 0x22, 0x74, 0x6f, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x74, 0x6f, 0x44, 0x22, 0x2c, 0x22, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x5f, 0x63, 0x61, 0x70, 0x5f, 0x66, 0x6f, 0x72, 0x5f, 0x74, 0x6f, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x31, 0x22, 0x2c, 0x22, 0x73, 0x77, 0x61, 0x70, 0x5f, 0x72, 0x61, 0x74, 0x65, 0x22, 0x3a, 0x22, 0x31, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22, 0x7d},
Index: false,
},
},
},
sdk.Event{
Type: "lbm.fswap.v1.EventAddDenomMetadata",
Attributes: []abci.EventAttribute{
{
Key: []byte("metadata"),
Value: []uint8{0x7b, 0x22, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3a, 0x22, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x2d, 0x63, 0x6f, 0x69, 0x6e, 0x22, 0x2c, 0x22, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x5f, 0x75, 0x6e, 0x69, 0x74, 0x73, 0x22, 0x3a, 0x5b, 0x7b, 0x22, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x74, 0x6f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x2c, 0x22, 0x65, 0x78, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x22, 0x3a, 0x30, 0x2c, 0x22, 0x61, 0x6c, 0x69, 0x61, 0x73, 0x65, 0x73, 0x22, 0x3a, 0x5b, 0x5d, 0x7d, 0x5d, 0x2c, 0x22, 0x62, 0x61, 0x73, 0x65, 0x22, 0x3a, 0x22, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x22, 0x2c, 0x22, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x22, 0x3a, 0x22, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x63, 0x6f, 0x69, 0x6e, 0x22, 0x2c, 0x22, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3a, 0x22, 0x44, 0x55, 0x4d, 0x4d, 0x59, 0x22, 0x2c, 0x22, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x22, 0x3a, 0x22, 0x44, 0x55, 0x4d, 0x22, 0x7d},
Index: false,
},
},
},
},
},
"to-denom metadata change not allowed": {
types.Swap{
Expand All @@ -42,6 +67,7 @@ func (s *KeeperTestSuite) TestMakeSwapProposal() {
},
true,
sdkerrors.ErrInvalidRequest,
sdk.Events{},
},
}
for name, tc := range testCases {
Expand All @@ -51,9 +77,12 @@ func (s *KeeperTestSuite) TestMakeSwapProposal() {
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().ErrorIs(err, tc.expectedError)
events := ctx.EventManager().Events()
s.Require().Equal(tc.expectedEvents, events)
})
}
}
Expand Down
Loading
Loading