Skip to content

Commit

Permalink
feat(x/fswap): introduce new event(MakeSwapProposal) (#1363)
Browse files Browse the repository at this point in the history
* feat(x/fswap): introduce new event(MakeSwapProposal)

* chore: lint proto

* chore: update proto-doc

* chore: update changelog

* chore: newline

* chore: update proto

* chore: make the event two separate events
  • Loading branch information
jaeseung-bae authored May 8, 2024
1 parent 8ad5585 commit 5e2cd7f
Show file tree
Hide file tree
Showing 6 changed files with 490 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (server) [\#1175](https://github.com/Finschia/finschia-sdk/pull/1175) Use go embed for swagger
* (x/collection) [\#1287](https://github.com/Finschia/finschia-sdk/pull/1287) add nft id validation to MsgSendNFT
* (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
* chore(deps) [\#1141](https://github.com/Finschia/finschia-sdk/pull/1141) Bump github.com/cosmos/ledger-cosmos-go from 0.12.2 to 0.13.2 to fix ledger signing issue
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 @@ -950,15 +950,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 @@ -14120,39 +14122,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 @@ -14226,6 +14195,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

0 comments on commit 5e2cd7f

Please sign in to comment.