Skip to content

Commit

Permalink
chore: rename fswap to swap
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeseung-bae committed Apr 30, 2024
1 parent 427da10 commit 9b26d2f
Show file tree
Hide file tree
Showing 22 changed files with 249 additions and 264 deletions.
16 changes: 8 additions & 8 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -12750,9 +12750,9 @@ Msg defines the foundation Msg service.



<a name="lbm.fswap.v1.FswapInit"></a>
<a name="lbm.fswap.v1.SwapInit"></a>

### FswapInit
### SwapInit



Expand All @@ -12768,17 +12768,17 @@ Msg defines the foundation Msg service.



<a name="lbm.fswap.v1.FswapInitProposal"></a>
<a name="lbm.fswap.v1.SwapInitProposal"></a>

### FswapInitProposal
### SwapInitProposal
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) | | |
| `swap_init` | [SwapInit](#lbm.fswap.v1.SwapInit) | | |



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


Expand Down
10 changes: 5 additions & 5 deletions proto/lbm/fswap/v1/fswap.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions proto/lbm/fswap/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
4 changes: 2 additions & 2 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ var (
upgradeclient.ProposalHandler,
upgradeclient.CancelProposalHandler,
foundationclient.ProposalHandler,
fswapclient.FswapInitHandler,
fswapclient.SwapInitHandler,
),
params.AppModuleBasic{},
crisis.AppModuleBasic{},
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions x/fswap/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion x/fswap/client/proposal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
16 changes: 8 additions & 8 deletions x/fswap/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 6 additions & 6 deletions x/fswap/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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),
}
}
2 changes: 1 addition & 1 deletion x/fswap/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
78 changes: 37 additions & 41 deletions x/fswap/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -250,56 +246,56 @@ 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
}

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
}

func (k Keeper) getSwapMultiple(ctx sdk.Context) (sdk.Int, error) {

Check failure on line 269 in x/fswap/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / golangci-lint

func `Keeper.getSwapMultiple` is unused (unused)
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
}

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 {
Expand Down Expand Up @@ -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
}
Loading

0 comments on commit 9b26d2f

Please sign in to comment.