From a7d69ac849106bdb780a11937f4dff6ef297ebf9 Mon Sep 17 00:00:00 2001 From: DaevMithran Date: Sun, 27 Oct 2024 02:45:41 +0400 Subject: [PATCH] Format code --- app/app.go | 5 +- app/decorators/msg_block_ibc.go | 6 +- x/fractionnft/autocli.go | 56 +++++----- x/fractionnft/depinject.go | 4 +- x/fractionnft/keeper/keeper.go | 156 +++++++++++++-------------- x/fractionnft/keeper/keeper_test.go | 4 +- x/fractionnft/keeper/msg_server.go | 3 - x/fractionnft/keeper/query_server.go | 6 +- x/fractionnft/module.go | 12 +-- 9 files changed, 122 insertions(+), 130 deletions(-) diff --git a/app/app.go b/app/app.go index e6600076..60c3d0b2 100644 --- a/app/app.go +++ b/app/app.go @@ -193,7 +193,7 @@ var maccPerms = map[string][]string{ feemarkettypes.ModuleName: {authtypes.Burner}, feemarkettypes.FeeCollectorName: {authtypes.Burner}, oracletypes.ModuleName: nil, - fractionnfttypes.ModuleName: {authtypes.Minter, authtypes.Burner}, + fractionnfttypes.ModuleName: {authtypes.Minter, authtypes.Burner}, } var ( @@ -260,7 +260,7 @@ type App struct { // MANTRAChain keepers TokenFactoryKeeper tokenfactorykeeper.Keeper TaxKeeper taxkeeper.Keeper - FractionnftKeeper fractionnftkeeper.Keeper + FractionnftKeeper fractionnftkeeper.Keeper // the module manager ModuleManager *module.Manager @@ -788,7 +788,6 @@ func New( tax.NewAppModule(appCodec, app.TaxKeeper), feemarket.NewAppModule(appCodec, *app.FeeMarketKeeper), fractionnft.NewAppModule(appCodec, app.FractionnftKeeper, app.AccountKeeper, app.NFTKeeper, app.BankKeeper), - ) // BasicModuleManager defines the module BasicManager is in charge of setting up basic, diff --git a/app/decorators/msg_block_ibc.go b/app/decorators/msg_block_ibc.go index 6a5b09d6..9ca57cea 100644 --- a/app/decorators/msg_block_ibc.go +++ b/app/decorators/msg_block_ibc.go @@ -22,9 +22,7 @@ type MsgFilterDecorator struct { // - decorators.FilterDecorator(&banktypes.MsgSend{}) // This would block any MsgSend messages from being included in a transaction if set in ante.go func FilterDecorator() MsgFilterDecorator { - return MsgFilterDecorator{ - - } + return MsgFilterDecorator{} } func (mfd MsgFilterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { @@ -54,7 +52,7 @@ func (mfd MsgFilterDecorator) HasDisallowedMessage(ctx sdk.Context, msgs []sdk.M return true } } - + } return false diff --git a/x/fractionnft/autocli.go b/x/fractionnft/autocli.go index ea33a388..d755b8b6 100644 --- a/x/fractionnft/autocli.go +++ b/x/fractionnft/autocli.go @@ -12,51 +12,51 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { Service: modulev1.Query_ServiceDesc.ServiceName, RpcCommandOptions: []*autocliv1.RpcCommandOptions{ { - RpcMethod: "GetNftToken", - Use: "get ", - Short: "Get nfttoken", - PositionalArgs: []*autocliv1.PositionalArgDescriptor{ - {ProtoField: "class_id"}, + RpcMethod: "GetNftToken", + Use: "get ", + Short: "Get nfttoken", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "class_id"}, {ProtoField: "nft_id"}, - }, - }, + }, + }, }, }, Tx: &autocliv1.ServiceCommandDescriptor{ Service: modulev1.Msg_ServiceDesc.ServiceName, RpcCommandOptions: []*autocliv1.RpcCommandOptions{ { - RpcMethod: "MsgTokenizeNFT", - Use: "tokenize ", - Short: "Tokenize nft", - PositionalArgs: []*autocliv1.PositionalArgDescriptor{ - {ProtoField: "class_id"}, + RpcMethod: "MsgTokenizeNFT", + Use: "tokenize ", + Short: "Tokenize nft", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "class_id"}, {ProtoField: "nft_id"}, {ProtoField: "token_supply"}, {ProtoField: "timeout_height"}, - }, - }, + }, + }, { - RpcMethod: "MsgRemintNFT", - Use: "remint ", - Short: "Remint nfttoken", - PositionalArgs: []*autocliv1.PositionalArgDescriptor{ - {ProtoField: "class_id"}, + RpcMethod: "MsgRemintNFT", + Use: "remint ", + Short: "Remint nfttoken", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "class_id"}, {ProtoField: "nft_id"}, - }, - }, + }, + }, { - RpcMethod: "MsgMintNFT", - Use: "mint ", - Short: "Mint nfttoken", - PositionalArgs: []*autocliv1.PositionalArgDescriptor{ - {ProtoField: "name"}, + RpcMethod: "MsgMintNFT", + Use: "mint ", + Short: "Mint nfttoken", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "name"}, {ProtoField: "id"}, {ProtoField: "description"}, {ProtoField: "image"}, {ProtoField: "category"}, - }, - }, + }, + }, }, }, } diff --git a/x/fractionnft/depinject.go b/x/fractionnft/depinject.go index 51fe0fce..84d74b1f 100644 --- a/x/fractionnft/depinject.go +++ b/x/fractionnft/depinject.go @@ -50,8 +50,8 @@ type ModuleInputs struct { SlashingKeeper slashingkeeper.Keeper accountKepper authkeeper.AccountKeeper - nftKeeper nftkeeper.Keeper - bankKeeper bankkeeper.Keeper + nftKeeper nftkeeper.Keeper + bankKeeper bankkeeper.Keeper } type ModuleOutputs struct { diff --git a/x/fractionnft/keeper/keeper.go b/x/fractionnft/keeper/keeper.go index 23f3ed35..b531f4dd 100644 --- a/x/fractionnft/keeper/keeper.go +++ b/x/fractionnft/keeper/keeper.go @@ -29,7 +29,7 @@ import ( ) type Keeper struct { - cdc codec.BinaryCodec + cdc codec.BinaryCodec addressCodec address.Codec logger log.Logger @@ -44,8 +44,8 @@ type Keeper struct { NFTMapping collections.Map[string, types.TokenizedNFT] AccountKeeper authkeeper.AccountKeeper - Nftkeeper nftkeeper.Keeper - BankKeeper bankkeeper.Keeper + Nftkeeper nftkeeper.Keeper + BankKeeper bankkeeper.Keeper } // NewKeeper creates a new Keeper instance @@ -77,12 +77,10 @@ func NewKeeper( panic(err) } - - k := Keeper{ - cdc: cdc, + cdc: cdc, addressCodec: addressCodec, - logger: logger, + logger: logger, Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)), OrmDB: store, @@ -92,8 +90,8 @@ func NewKeeper( NFTMapping: collections.NewMap(sb, collections.NewPrefix(1), "nft_mapping", collections.StringKey, codec.CollValue[types.TokenizedNFT](cdc)), AccountKeeper: accountKepper, - Nftkeeper: nftKeeper, - BankKeeper: bankKeeper, + Nftkeeper: nftKeeper, + BankKeeper: bankKeeper, } schema, err := sb.Build() @@ -134,37 +132,37 @@ func (k *Keeper) ExportGenesis(ctx context.Context) *types.GenesisState { func (k *Keeper) TokenizeNFT(ctx context.Context, classId string, nftId string, sender sdk.AccAddress, tokenSupply int64, timeoutHeight int64) error { if !k.Nftkeeper.HasClass(ctx, classId) { - return fmt.Errorf("NFT class not found: %s", classId) + return fmt.Errorf("NFT class not found: %s", classId) } if !k.Nftkeeper.HasNFT(ctx, classId, nftId) { - return fmt.Errorf("NFT not found: %s", nftId) + return fmt.Errorf("NFT not found: %s", nftId) } sdkCtx := sdk.UnwrapSDKContext(ctx) blockHeight := sdkCtx.BlockHeight() // Restricting timeout: timeout should be alteast greater than 3 blocks - if blockHeight + 3 >= timeoutHeight { + if blockHeight+3 >= timeoutHeight { return fmt.Errorf("timeout should be greater than curent blockHeight %d", blockHeight) } - denom := "fractionNFT-"+classId+"-"+nftId - + denom := "fractionNFT-" + classId + "-" + nftId + // validate if NFT is tokenized already - has, err := k.NFTMapping.Has(ctx, denom) - if err != nil { - return err - } - if has { - return fmt.Errorf("NFT is tokenized already : %s", denom) - } + has, err := k.NFTMapping.Has(ctx, denom) + if err != nil { + return err + } + if has { + return fmt.Errorf("NFT is tokenized already : %s", denom) + } // Lock the NFT in module address // Note: Owner validation is done within the Send function - _, err = k.Nftkeeper.Send(ctx, &nft.MsgSend { - ClassId: classId, - Id: nftId, - Sender: sender.String(), + _, err = k.Nftkeeper.Send(ctx, &nft.MsgSend{ + ClassId: classId, + Id: nftId, + Sender: sender.String(), Receiver: authtypes.NewModuleAddress(types.ModuleName).String(), }) if err != nil { @@ -172,7 +170,7 @@ func (k *Keeper) TokenizeNFT(ctx context.Context, classId string, nftId string, } coins := sdk.NewCoins(sdk.NewCoin(denom, math.NewInt(tokenSupply))) - + // tokenize NFT to create new coins err = k.BankKeeper.MintCoins(ctx, types.ModuleName, coins) if err != nil { @@ -186,19 +184,19 @@ func (k *Keeper) TokenizeNFT(ctx context.Context, classId string, nftId string, } return k.NFTMapping.Set(ctx, denom, types.TokenizedNFT{ - Owner: sender.String(), + Owner: sender.String(), TimeoutHeight: timeoutHeight, }) } -func(k *Keeper) RemintNFT(ctx context.Context, classId string, nftId string, sender sdk.AccAddress) error { - denom := "fractionNFT-"+classId+"-"+nftId - +func (k *Keeper) RemintNFT(ctx context.Context, classId string, nftId string, sender sdk.AccAddress) error { + denom := "fractionNFT-" + classId + "-" + nftId + // validate if NFT is tokenized already - tokenizedNFT, err := k.NFTMapping.Get(ctx, denom) - if err != nil { - return err - } + tokenizedNFT, err := k.NFTMapping.Get(ctx, denom) + if err != nil { + return err + } // validate block height sdkCtx := sdk.UnwrapSDKContext(ctx) @@ -207,40 +205,40 @@ func(k *Keeper) RemintNFT(ctx context.Context, classId string, nftId string, sen fmt.Printf("senderStr %s\n", senderStr) fmt.Printf("moduleAddr %s", authtypes.NewModuleAddress(types.ModuleName).String()) switch senderStr { - case authtypes.NewModuleAddress(types.ModuleName).String(): - if blockHeight < tokenizedNFT.TimeoutHeight { - return fmt.Errorf("remint can be performed on timeout: %s", denom) - } - // Burn tokens in the denom - err = k.BurnAllTokensOfDenom(sdkCtx, denom) - if err != nil { - return err - } - case tokenizedNFT.Owner: - supply := k.BankKeeper.GetSupply(ctx, denom) - balance := k.BankKeeper.GetBalance(ctx, sender, denom) - - if !supply.Amount.Equal(balance.Amount) { - return fmt.Errorf("remint can be called by owner only if they own all the tokens: %s", denom) - } - - coins := sdk.NewCoins(balance) - - err = k.BankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, coins) - if err != nil { - return err - } - - k.BankKeeper.BurnCoins(ctx, types.ModuleName, coins) - default: - return fmt.Errorf("unauthorized sender: %s", senderStr) + case authtypes.NewModuleAddress(types.ModuleName).String(): + if blockHeight < tokenizedNFT.TimeoutHeight { + return fmt.Errorf("remint can be performed on timeout: %s", denom) + } + // Burn tokens in the denom + err = k.BurnAllTokensOfDenom(sdkCtx, denom) + if err != nil { + return err + } + case tokenizedNFT.Owner: + supply := k.BankKeeper.GetSupply(ctx, denom) + balance := k.BankKeeper.GetBalance(ctx, sender, denom) + + if !supply.Amount.Equal(balance.Amount) { + return fmt.Errorf("remint can be called by owner only if they own all the tokens: %s", denom) + } + + coins := sdk.NewCoins(balance) + + err = k.BankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, coins) + if err != nil { + return err + } + + k.BankKeeper.BurnCoins(ctx, types.ModuleName, coins) + default: + return fmt.Errorf("unauthorized sender: %s", senderStr) } // Transfer NFT back to the owner - _, err = k.Nftkeeper.Send(ctx, &nft.MsgSend { - ClassId: classId, - Id: nftId, - Sender: authtypes.NewModuleAddress(types.ModuleName).String(), + _, err = k.Nftkeeper.Send(ctx, &nft.MsgSend{ + ClassId: classId, + Id: nftId, + Sender: authtypes.NewModuleAddress(types.ModuleName).String(), Receiver: tokenizedNFT.Owner, }) if err != nil { @@ -253,30 +251,30 @@ func(k *Keeper) RemintNFT(ctx context.Context, classId string, nftId string, sen func (k Keeper) BurnAllTokensOfDenom(ctx sdk.Context, denom string) error { fmt.Printf("Burning All tokens of Denom") - owners, err := k.BankKeeper.DenomOwners(ctx, &banktypes.QueryDenomOwnersRequest { + owners, err := k.BankKeeper.DenomOwners(ctx, &banktypes.QueryDenomOwnersRequest{ Denom: denom, }) if err != nil { return err } - for _, owner:= range owners.DenomOwners { - addr, err :=sdk.AccAddressFromBech32(owner.Address) + for _, owner := range owners.DenomOwners { + addr, err := sdk.AccAddressFromBech32(owner.Address) if err != nil { return err } k.BankKeeper.SendCoinsFromAccountToModule(ctx, addr, types.ModuleName, sdk.NewCoins(owner.Balance)) } - coin :=k.BankKeeper.GetBalance(ctx, authtypes.NewModuleAddress(types.ModuleName), denom) + coin := k.BankKeeper.GetBalance(ctx, authtypes.NewModuleAddress(types.ModuleName), denom) k.BankKeeper.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(coin)) - return nil + return nil } func (k Keeper) MintNFT(ctx context.Context, owner sdk.AccAddress, category string, id string, name string, description string, image string) error { if !k.Nftkeeper.HasClass(ctx, category) { - k.Nftkeeper.SaveClass(ctx, nft.Class{ Id: category, Name: category }) + k.Nftkeeper.SaveClass(ctx, nft.Class{Id: category, Name: category}) } if k.Nftkeeper.HasNFT(ctx, category, id) { @@ -285,23 +283,23 @@ func (k Keeper) MintNFT(ctx context.Context, owner sdk.AccAddress, category stri coins := sdk.NewCoins(sdk.NewCoin("uom", math.NewInt(int64(5)))) // receive payment - err :=k.BankKeeper.SendCoinsFromAccountToModule(ctx, owner, types.ModuleName, coins) - if err !=nil { + err := k.BankKeeper.SendCoinsFromAccountToModule(ctx, owner, types.ModuleName, coins) + if err != nil { return err } - data, err := codectypes.NewAnyWithValue(&types.NFTData { - Name: name, + data, err := codectypes.NewAnyWithValue(&types.NFTData{ + Name: name, Description: description, - Image: image, + Image: image, }) if err != nil { return err } - return k.Nftkeeper.Mint(ctx, nft.NFT { + return k.Nftkeeper.Mint(ctx, nft.NFT{ ClassId: category, - Id: id, - Data: data, + Id: id, + Data: data, }, owner) } diff --git a/x/fractionnft/keeper/keeper_test.go b/x/fractionnft/keeper/keeper_test.go index 1a464576..14a01da3 100644 --- a/x/fractionnft/keeper/keeper_test.go +++ b/x/fractionnft/keeper/keeper_test.go @@ -36,7 +36,7 @@ var maccPerms = map[string][]string{ stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking}, minttypes.ModuleName: {authtypes.Minter}, govtypes.ModuleName: {authtypes.Burner}, - types.ModuleName: {authtypes.Minter, authtypes.Burner}, + types.ModuleName: {authtypes.Minter, authtypes.Burner}, } type testFixture struct { @@ -49,7 +49,7 @@ type testFixture struct { appModule *module.AppModule accountkeeper authkeeper.AccountKeeper - nftkeeper nftkeeper.Keeper + nftkeeper nftkeeper.Keeper bankkeeper bankkeeper.BaseKeeper stakingKeeper *stakingkeeper.Keeper mintkeeper mintkeeper.Keeper diff --git a/x/fractionnft/keeper/msg_server.go b/x/fractionnft/keeper/msg_server.go index 680310d1..b4709cc1 100644 --- a/x/fractionnft/keeper/msg_server.go +++ b/x/fractionnft/keeper/msg_server.go @@ -44,7 +44,6 @@ func (ms msgServer) MsgTokenizeNFT(ctx context.Context, msg *types.MsgTokenizeNF return &types.MsgTokenizeNFTResponse{}, nil } - // MsgTokenizeNFT implements types.MsgServer. func (ms msgServer) MsgRemintNFT(ctx context.Context, msg *types.MsgRemintNFTParams) (*types.MsgRemintNFTResponse, error) { owner, err := ms.k.addressCodec.StringToBytes(msg.Owner) @@ -59,7 +58,6 @@ func (ms msgServer) MsgRemintNFT(ctx context.Context, msg *types.MsgRemintNFTPar return &types.MsgRemintNFTResponse{}, nil } - func (ms msgServer) MsgMintNFT(ctx context.Context, msg *types.MsgMintNFTParams) (*types.MsgMintNFTResponse, error) { owner, err := ms.k.addressCodec.StringToBytes(msg.Owner) if err != nil { @@ -72,4 +70,3 @@ func (ms msgServer) MsgMintNFT(ctx context.Context, msg *types.MsgMintNFTParams) } return &types.MsgMintNFTResponse{}, nil } - diff --git a/x/fractionnft/keeper/query_server.go b/x/fractionnft/keeper/query_server.go index f289faac..68de036a 100644 --- a/x/fractionnft/keeper/query_server.go +++ b/x/fractionnft/keeper/query_server.go @@ -30,14 +30,14 @@ func (k Querier) Params(c context.Context, req *types.QueryParamsRequest) (*type } func (k Querier) GetNftToken(goCtx context.Context, req *types.QueryGetNftTokenRequest) (*types.QueryNftTokenResponse, error) { - denom := "fractionNFT-"+req.ClassId+"-"+req.NftId - nftres, err := k.Keeper.NFTMapping.Get(goCtx, denom) + denom := "fractionNFT-" + req.ClassId + "-" + req.NftId + nftres, err := k.Keeper.NFTMapping.Get(goCtx, denom) if err != nil { return nil, err } return &types.QueryNftTokenResponse{ - Owner: nftres.Owner, + Owner: nftres.Owner, TimeoutHeight: nftres.TimeoutHeight, }, nil diff --git a/x/fractionnft/module.go b/x/fractionnft/module.go index a0d93ce3..07b3be44 100644 --- a/x/fractionnft/module.go +++ b/x/fractionnft/module.go @@ -52,8 +52,8 @@ type AppModule struct { keeper keeper.Keeper accountKeeper authkeeper.AccountKeeper - nftKeeper nftkeeper.Keeper - bankKeeper bankkeeper.Keeper + nftKeeper nftkeeper.Keeper + bankKeeper bankkeeper.Keeper } // EndBlock implements module.HasABCIEndBlock. @@ -75,7 +75,7 @@ func (am AppModule) EndBlock(ctx context.Context) ([]abci.ValidatorUpdate, error if v.Value.TimeoutHeight == blockHeight { splitted := strings.Split(v.Key, "-") am.keeper.RemintNFT(ctx, splitted[1], splitted[2], authtypes.NewModuleAddress(types.ModuleName)) - } + } } return nil, nil } @@ -119,9 +119,9 @@ func NewAppModule( return &AppModule{ AppModuleBasic: AppModuleBasic{cdc: cdc}, keeper: keeper, - accountKeeper: accountKeeper, - nftKeeper: nftKeeper, - bankKeeper: bankKeeper, + accountKeeper: accountKeeper, + nftKeeper: nftKeeper, + bankKeeper: bankKeeper, } }