Skip to content

Commit

Permalink
Merge pull request #307 from terra-money/update/tf
Browse files Browse the repository at this point in the history
update: tf
  • Loading branch information
javiersuweijie authored May 30, 2024
2 parents db9b997 + f95fe95 commit d2810c5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
6 changes: 4 additions & 2 deletions app/custom_queriers/custom_queriers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package custom_queriers
import (
"encoding/json"
"fmt"
"strings"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
alliancebindings "github.com/terra-money/alliance/x/alliance/bindings"
alliancekeeper "github.com/terra-money/alliance/x/alliance/keeper"
tokenfactorybindings "github.com/terra-money/core/v2/x/tokenfactory/bindings"
tokenfactorykeeper "github.com/terra-money/core/v2/x/tokenfactory/keeper"
"strings"

bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down
1 change: 1 addition & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func NewTerraAppKeepers(
)
keepers.TokenFactoryKeeper = tokenfactorykeeper.NewKeeper(
keys[tokenfactorytypes.StoreKey],
maccPerms,
keepers.AccountKeeper,
&keepers.BankKeeper,
keepers.DistrKeeper,
Expand Down
1 change: 0 additions & 1 deletion x/tokenfactory/bindings/wasm.go
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
package bindings

27 changes: 27 additions & 0 deletions x/tokenfactory/keeper/bankactions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package keeper

import (
"sort"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

sdk "github.com/cosmos/cosmos-sdk/types"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -71,6 +76,28 @@ func (k Keeper) forceTransfer(ctx sdk.Context, amount sdk.Coin, fromAddr string,
return err
}

fromAcc, err := sdk.AccAddressFromBech32(fromAddr)
if err != nil {
return err
}

sortedPermAddrs := make([]string, 0, len(k.permAddrs))
for moduleName := range k.permAddrs {
sortedPermAddrs = append(sortedPermAddrs, moduleName)
}
sort.Strings(sortedPermAddrs)

for _, moduleName := range sortedPermAddrs {
account := k.accountKeeper.GetModuleAccount(ctx, moduleName)
if account == nil {
return status.Errorf(codes.NotFound, "account %s not found", moduleName)
}

if account.GetAddress().Equals(fromAcc) {
return status.Errorf(codes.Internal, "send from module acc not available")
}
}

fromSdkAddr, err := sdk.AccAddressFromBech32(fromAddr)
if err != nil {
return err
Expand Down
7 changes: 5 additions & 2 deletions x/tokenfactory/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (

type (
Keeper struct {
storeKey storetypes.StoreKey
storeKey storetypes.StoreKey
permAddrs map[string][]string

accountKeeper types.AccountKeeper
bankKeeper *customtypes.Keeper
Expand All @@ -33,6 +34,7 @@ type (
// NewKeeper returns a new instance of the x/tokenfactory keeper
func NewKeeper(
storeKey storetypes.StoreKey,
permAddrs map[string][]string,
accountKeeper types.AccountKeeper,
bankKeeper *customtypes.Keeper,
communityPoolKeeper types.CommunityPoolKeeper,
Expand All @@ -41,7 +43,8 @@ func NewKeeper(
) Keeper {

return Keeper{
storeKey: storeKey,
storeKey: storeKey,
permAddrs: permAddrs,

accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
Expand Down
22 changes: 22 additions & 0 deletions x/tokenfactory/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"testing"

govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/stretchr/testify/suite"
"github.com/terra-money/core/v2/x/tokenfactory/types"

Expand Down Expand Up @@ -187,6 +189,26 @@ func (s *KeeperTestSuite) TestBurnDenomMsg() {
}
}

func (s *KeeperTestSuite) TestForceTransferMsgFromModuleAcc() {
// Create a denom
res, _ := s.msgServer.CreateDenom(sdk.WrapSDKContext(s.Ctx), types.NewMsgCreateDenom(s.TestAccs[0].String(), "bitcoin"))
defaultDenom := res.GetNewTokenDenom()

s.Run(fmt.Sprintf("test force transfer"), func() {
mintAmt := sdk.NewInt64Coin(defaultDenom, 10)

_, err := s.msgServer.Mint(sdk.WrapSDKContext(s.Ctx), types.NewMsgMint(s.TestAccs[0].String(), mintAmt))

govModAcc := s.App.Keepers.AccountKeeper.GetModuleAccount(s.Ctx, govtypes.ModuleName)

err = s.App.Keepers.BankKeeper.SendCoins(s.Ctx, s.TestAccs[0], govModAcc.GetAddress(), sdk.NewCoins(mintAmt))
s.Require().NoError(err)

_, err = s.msgServer.ForceTransfer(s.Ctx, types.NewMsgForceTransfer(s.TestAccs[0].String(), mintAmt, govModAcc.GetAddress().String(), s.TestAccs[1].String()))
s.Require().ErrorContains(err, "send from module acc not available")
})
}

// TestCreateDenomMsg tests TypeMsgCreateDenom message is emitted on a successful denom creation
func (s *KeeperTestSuite) TestCreateDenomMsg() {
for _, tc := range []struct {
Expand Down

0 comments on commit d2810c5

Please sign in to comment.