Skip to content

Commit

Permalink
Merge pull request #48 from realiotech/feature/expect-raw-expanded-am…
Browse files Browse the repository at this point in the history
…ount-transfer-token

Feature/expect raw expanded amount transfer token
  • Loading branch information
jiujiteiro authored Mar 24, 2023
2 parents 7f873e1 + f7a8b4c commit 528abd5
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 7 deletions.
14 changes: 7 additions & 7 deletions x/asset/keeper/msg_server_transfer_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (

"cosmossdk.io/math"

realionetworktypes "github.com/realiotech/realio-network/types"

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

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -39,14 +37,16 @@ func (k msgServer) TransferToken(goCtx context.Context, msg *types.MsgTransferTo
}

if isAuthorizedFrom && isAuthorizedTo {
// normalize into chains 10^18 denomination
totalInt, _ := math.NewIntFromString(msg.Amount)
canonicalAmount := totalInt.Mul(realionetworktypes.PowerReduction)
totalInt, totalIsValid := math.NewIntFromString(msg.Amount)
if !totalIsValid {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "invalid coin amount %s", msg.Amount)
}

baseDenom := fmt.Sprintf("a%s", strings.ToLower(msg.Symbol))
coin := sdk.Coins{{Denom: baseDenom, Amount: canonicalAmount}}
coin := sdk.Coins{{Denom: baseDenom, Amount: totalInt}}
err := k.bankKeeper.SendCoins(ctx, fromAddress, toAddress, coin)
if err != nil {
panic(err)
return nil, err
}
} else {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s transfer not authorized", msg.Symbol)
Expand Down
136 changes: 136 additions & 0 deletions x/asset/keeper/msg_server_transfer_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package keeper_test

import (
"fmt"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/realiotech/realio-network/x/asset/keeper"
"github.com/realiotech/realio-network/x/asset/types"
)

// Prevent strconv unused error
var _ = strconv.IntSize

func (suite *KeeperTestSuite) TestTransferToken() {
suite.SetupTest()

srv := keeper.NewMsgServerImpl(suite.app.AssetKeeper)
wctx := sdk.WrapSDKContext(suite.ctx)

manager := suite.testUser1Address
testUser := suite.testUser2Address

t1 := &types.MsgCreateToken{
Manager: manager,
Symbol: "RST", Total: "1000", AuthorizationRequired: true,
}
_, err := srv.CreateToken(wctx, t1)
suite.Require().NoError(err)

authUserMsg := &types.MsgAuthorizeAddress{
Manager: manager,
Symbol: "RST", Address: manager,
}

_, err = srv.AuthorizeAddress(wctx, authUserMsg)
suite.Require().NoError(err)

authUser2Msg := &types.MsgAuthorizeAddress{
Manager: manager,
Symbol: "RST", Address: testUser,
}

_, err = srv.AuthorizeAddress(wctx, authUser2Msg)
suite.Require().NoError(err)

amount := "50000000000000000000"
expected := &types.MsgTransferToken{Symbol: "RST", From: manager, To: testUser, Amount: amount}

_, err = srv.TransferToken(wctx, expected)
suite.Require().NoError(err)

balance := suite.app.BankKeeper.GetBalance(suite.ctx, suite.testUser2Acc, "arst")
suite.Require().Equal(balance.String(), fmt.Sprintf("%s%s", amount, "arst"))
}

func (suite *KeeperTestSuite) TestTransferTokenInvalidAmount() {
suite.SetupTest()

srv := keeper.NewMsgServerImpl(suite.app.AssetKeeper)
wctx := sdk.WrapSDKContext(suite.ctx)

manager := suite.testUser1Address
testUser := suite.testUser2Address

t1 := &types.MsgCreateToken{
Manager: manager,
Symbol: "RST", Total: "1000", AuthorizationRequired: true,
}
_, err := srv.CreateToken(wctx, t1)
suite.Require().NoError(err)

authUserMsg := &types.MsgAuthorizeAddress{
Manager: manager,
Symbol: "RST", Address: manager,
}

_, err = srv.AuthorizeAddress(wctx, authUserMsg)
suite.Require().NoError(err)

authUser2Msg := &types.MsgAuthorizeAddress{
Manager: manager,
Symbol: "RST", Address: testUser,
}

_, err = srv.AuthorizeAddress(wctx, authUser2Msg)
suite.Require().NoError(err)

// amount is invalid, all amounts should be in base 10^18 amount
amount := "50000000000000000000.00"
expected := &types.MsgTransferToken{Symbol: "RST", From: manager, To: testUser, Amount: amount}

_, err = srv.TransferToken(wctx, expected)
suite.Require().Error(err)
}

func (suite *KeeperTestSuite) TestTransferTokenSenderBalanceToSmall() {
suite.SetupTest()

srv := keeper.NewMsgServerImpl(suite.app.AssetKeeper)
wctx := sdk.WrapSDKContext(suite.ctx)

manager := suite.testUser1Address
testUser := suite.testUser2Address

t1 := &types.MsgCreateToken{
Manager: manager,
Symbol: "RST", Total: "1000", AuthorizationRequired: true,
}
_, err := srv.CreateToken(wctx, t1)
suite.Require().NoError(err)

authUserMsg := &types.MsgAuthorizeAddress{
Manager: manager,
Symbol: "RST", Address: manager,
}

_, err = srv.AuthorizeAddress(wctx, authUserMsg)
suite.Require().NoError(err)

authUser2Msg := &types.MsgAuthorizeAddress{
Manager: manager,
Symbol: "RST", Address: testUser,
}

_, err = srv.AuthorizeAddress(wctx, authUser2Msg)
suite.Require().NoError(err)

// amount is invalid, all amounts should be in base 10^18 amount
amount := "1001000000000000000000"
expected := &types.MsgTransferToken{Symbol: "RST", From: manager, To: testUser, Amount: amount}

_, err = srv.TransferToken(wctx, expected)
suite.Require().Error(err)
suite.Require().Equal(err.Error(), "1000000000000000000000arst is smaller than 1001000000000000000000arst: insufficient funds")
}

0 comments on commit 528abd5

Please sign in to comment.