From 6f9122cf4afa80fe1f295825e5593652b2a2dd80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E5=BC=BA?= Date: Tue, 26 Jan 2021 18:35:38 +0800 Subject: [PATCH 1/6] Optimized token accuracy conversion --- modules/token/types/token.go | 30 +++---- modules/token/types/token_test.go | 135 ++++++++++++++++++++++++++---- 2 files changed, 129 insertions(+), 36 deletions(-) diff --git a/modules/token/types/token.go b/modules/token/types/token.go index 41f1243c..720b2e45 100644 --- a/modules/token/types/token.go +++ b/modules/token/types/token.go @@ -2,7 +2,7 @@ package types import ( "encoding/json" - "math" + "math/big" "strconv" "github.com/gogo/protobuf/proto" @@ -12,6 +12,12 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) +var ( + _ proto.Message = &Token{} + tenInt = big.NewInt(10) +) + +// TokenI define a interface for Token type TokenI interface { GetSymbol() string GetName() string @@ -26,8 +32,6 @@ type TokenI interface { ToMinCoin(coin sdk.DecCoin) (sdk.Coin, error) } -var _ proto.Message = &Token{} - // NewToken constructs a new Token instance func NewToken( symbol string, @@ -115,15 +119,9 @@ func (t Token) ToMainCoin(coin sdk.Coin) (sdk.DecCoin, error) { return sdk.NewDecCoin(coin.Denom, coin.Amount), nil } - precision := math.Pow10(int(t.Scale)) - precisionStr := strconv.FormatFloat(precision, 'f', 0, 64) - precisionDec, err := sdk.NewDecFromStr(precisionStr) - if err != nil { - return sdk.DecCoin{}, err - } - + precision := new(big.Int).Exp(tenInt, big.NewInt(int64(t.Scale)), nil) // dest amount = src amount / 10^(scale) - amount := sdk.NewDecFromInt(coin.Amount).Quo(precisionDec) + amount := sdk.NewDecFromInt(coin.Amount).Quo(sdk.NewDecFromBigInt(precision)) return sdk.NewDecCoinFromDec(t.Symbol, amount), nil } @@ -137,15 +135,9 @@ func (t Token) ToMinCoin(coin sdk.DecCoin) (newCoin sdk.Coin, err error) { return sdk.NewCoin(coin.Denom, coin.Amount.TruncateInt()), nil } - precision := math.Pow10(int(t.Scale)) - precisionStr := strconv.FormatFloat(precision, 'f', 0, 64) - precisionDec, err := sdk.NewDecFromStr(precisionStr) - if err != nil { - return sdk.Coin{}, err - } - + precision := new(big.Int).Exp(tenInt, big.NewInt(int64(t.Scale)), nil) // dest amount = src amount * 10^(dest scale) - amount := coin.Amount.Mul(precisionDec) + amount := coin.Amount.Mul(sdk.NewDecFromBigInt(precision)) return sdk.NewCoin(t.MinUnit, amount.TruncateInt()), nil } diff --git a/modules/token/types/token_test.go b/modules/token/types/token_test.go index cb46c7ed..67c40176 100644 --- a/modules/token/types/token_test.go +++ b/modules/token/types/token_test.go @@ -1,15 +1,15 @@ package types import ( + "fmt" + "reflect" "testing" - "github.com/stretchr/testify/require" - sdk "github.com/cosmos/cosmos-sdk/types" ) -func TestToken_ToMinCoin(t *testing.T) { - token := Token{ +var ( + token = Token{ Symbol: "iris", Name: "irisnet", Scale: 18, @@ -19,19 +19,7 @@ func TestToken_ToMinCoin(t *testing.T) { Mintable: true, Owner: "", } - - amt, err := sdk.NewDecFromStr("1.500000000000000001") - require.NoError(t, err) - coin := sdk.NewDecCoinFromDec(token.Symbol, amt) - - c, err := token.ToMinCoin(coin) - require.NoError(t, err) - require.Equal(t, "1500000000000000001atto", c.String()) - - coin1, err := token.ToMainCoin(c) - require.NoError(t, err) - require.Equal(t, coin, coin1) -} +) func TestCheckKeywords(t *testing.T) { type args struct { @@ -61,3 +49,116 @@ func TestCheckKeywords(t *testing.T) { }) } } + +func TestToken_ToMinCoin(t *testing.T) { + type args struct { + coin sdk.DecCoin + } + + for i := uint32(0); i <= MaximumScale; i++ { + token.Scale = i + tests := []struct { + name string + args args + want sdk.Coin + wantErr bool + success bool + }{ + { + name: fmt.Sprintf("Main Coin to Min Coin,scale=%d", i), + wantErr: false, + args: args{coin: sdk.NewDecCoin(token.Symbol, sdk.NewInt(10))}, + want: sdk.NewCoin(token.MinUnit, sdk.NewIntWithDecimal(10, int(token.Scale))), + success: true, + }, + { + name: fmt.Sprintf("Main Coin to Min Coin Failed,scale=%d", i), + wantErr: false, + args: args{coin: sdk.NewDecCoin(token.Symbol, sdk.NewInt(10))}, + want: sdk.NewCoin(token.MinUnit, sdk.NewInt(10)), + success: (i == 0), + }, + { + name: fmt.Sprintf("Min Coin to Min Coin Success,scale=%d", i), + wantErr: false, + args: args{coin: sdk.NewDecCoin(token.MinUnit, sdk.NewInt(10))}, + want: sdk.NewCoin(token.MinUnit, sdk.NewInt(10)), + success: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tr := Token{ + Symbol: token.Symbol, + Scale: token.Scale, + MinUnit: token.MinUnit, + } + got, err := tr.ToMinCoin(tt.args.coin) + if (err != nil) != tt.wantErr { + t.Errorf("Token.ToMainCoin() error = %v, wantErr %v", err, tt.wantErr) + return + } + if tt.success != reflect.DeepEqual(got, tt.want) { + t.Errorf("Token.ToMainCoin() = %v, want %v", got, tt.want) + } + }) + } + } +} + +func TestToken_ToMainCoin(t *testing.T) { + type args struct { + coin sdk.Coin + } + + for i := uint32(0); i <= MaximumScale; i++ { + token.Scale = i + tests := []struct { + name string + args args + want sdk.DecCoin + wantErr bool + success bool + }{ + { + name: "Main Coin to Main Coin", + wantErr: false, + args: args{coin: sdk.NewCoin(token.Symbol, sdk.NewInt(10))}, + want: sdk.NewInt64DecCoin(token.Symbol, 10), + success: true, + }, + { + name: "Min Coin to Main Coin Failed", + wantErr: false, + args: args{coin: sdk.NewCoin(token.MinUnit, sdk.NewInt(10))}, + want: sdk.NewInt64DecCoin(token.Symbol, 10), + success: (i == 0), + }, + { + name: "Min Coin to Main Coin Success", + wantErr: false, + args: args{coin: sdk.NewCoin(token.MinUnit, sdk.NewInt(10))}, + want: sdk.NewDecCoinFromDec(token.Symbol, + sdk.NewDecWithPrec(1, int64(token.Scale)).MulInt64(10)), + success: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tr := Token{ + Symbol: token.Symbol, + Scale: token.Scale, + MinUnit: token.MinUnit, + } + got, err := tr.ToMainCoin(tt.args.coin) + if (err != nil) != tt.wantErr { + t.Errorf("Token.ToMainCoin() error = %v, wantErr %v", err, tt.wantErr) + return + } + if tt.success != reflect.DeepEqual(got, tt.want) { + t.Errorf("Token.ToMainCoin() = %v, want %v", got, tt.want) + } + }) + } + } +} From 46ae580cd3454d30fabfbcab8d55f9cc8a31d44b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E5=BC=BA?= Date: Thu, 28 Jan 2021 16:31:02 +0800 Subject: [PATCH 2/6] mint&edit&burn only accept symbol as denom --- modules/token/keeper/keeper.go | 18 ++++++------------ modules/token/keeper/msg_server.go | 5 ----- modules/token/keeper/token.go | 6 +++--- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/modules/token/keeper/keeper.go b/modules/token/keeper/keeper.go index c0b59e99..3e58efd2 100644 --- a/modules/token/keeper/keeper.go +++ b/modules/token/keeper/keeper.go @@ -96,13 +96,11 @@ func (k Keeper) EditToken( owner sdk.AccAddress, ) error { // get the destination token - tokenI, err := k.GetToken(ctx, symbol) + token, err := k.getTokenBySymbol(ctx, symbol) if err != nil { return err } - token := tokenI.(*types.Token) - if owner.String() != token.Owner { return sdkerrors.Wrapf(types.ErrInvalidOwner, "the address %s is not the owner of the token %s", owner, symbol) } @@ -131,7 +129,7 @@ func (k Keeper) EditToken( token.Mintable = mintable.ToBool() } - k.setToken(ctx, *token) + k.setToken(ctx, token) return nil } @@ -143,13 +141,11 @@ func (k Keeper) TransferTokenOwner( srcOwner sdk.AccAddress, dstOwner sdk.AccAddress, ) error { - tokenI, err := k.GetToken(ctx, symbol) + token, err := k.getTokenBySymbol(ctx, symbol) if err != nil { return err } - token := tokenI.(*types.Token) - if srcOwner.String() != token.Owner { return sdkerrors.Wrapf(types.ErrInvalidOwner, "the address %s is not the owner of the token %s", srcOwner, symbol) } @@ -157,7 +153,7 @@ func (k Keeper) TransferTokenOwner( token.Owner = dstOwner.String() // update token - k.setToken(ctx, *token) + k.setToken(ctx, token) // reset all indices k.resetStoreKeyForQueryToken(ctx, token.Symbol, srcOwner, dstOwner) @@ -174,13 +170,11 @@ func (k Keeper) MintToken( recipient sdk.AccAddress, owner sdk.AccAddress, ) error { - tokenI, err := k.GetToken(ctx, symbol) + token, err := k.getTokenBySymbol(ctx, symbol) if err != nil { return err } - token := tokenI.(*types.Token) - if owner.String() != token.Owner { return sdkerrors.Wrapf(types.ErrInvalidOwner, "the address %s is not the owner of the token %s", owner, symbol) } @@ -228,7 +222,7 @@ func (k Keeper) BurnToken( amount uint64, owner sdk.AccAddress, ) error { - token, err := k.GetToken(ctx, symbol) + token, err := k.getTokenBySymbol(ctx, symbol) if err != nil { return err } diff --git a/modules/token/keeper/msg_server.go b/modules/token/keeper/msg_server.go index 9189e59d..7ac4e7a1 100644 --- a/modules/token/keeper/msg_server.go +++ b/modules/token/keeper/msg_server.go @@ -139,11 +139,6 @@ func (m msgServer) BurnToken(goCtx context.Context, msg *types.MsgBurnToken) (*t } ctx := sdk.UnwrapSDKContext(goCtx) - - if err := m.Keeper.DeductMintTokenFee(ctx, owner, msg.Symbol); err != nil { - return nil, err - } - if err := m.Keeper.BurnToken(ctx, msg.Symbol, msg.Amount, owner); err != nil { return nil, err } diff --git a/modules/token/keeper/token.go b/modules/token/keeper/token.go index f4d00d94..48f15761 100644 --- a/modules/token/keeper/token.go +++ b/modules/token/keeper/token.go @@ -50,7 +50,7 @@ func (k Keeper) GetTokens(ctx sdk.Context, owner sdk.AccAddress) (tokens []types func (k Keeper) GetToken(ctx sdk.Context, denom string) (types.TokenI, error) { store := ctx.KVStore(k.storeKey) - if token, err := k.getToken(ctx, denom); err == nil { + if token, err := k.getTokenBySymbol(ctx, denom); err == nil { return &token, nil } @@ -62,7 +62,7 @@ func (k Keeper) GetToken(ctx sdk.Context, denom string) (types.TokenI, error) { var symbol gogotypes.StringValue k.cdc.MustUnmarshalBinaryBare(bz, &symbol) - token, err := k.getToken(ctx, symbol.Value) + token, err := k.getTokenBySymbol(ctx, symbol.Value) if err != nil { return nil, err } @@ -208,7 +208,7 @@ func (k Keeper) setToken(ctx sdk.Context, token types.Token) { store.Set(types.KeySymbol(token.Symbol), bz) } -func (k Keeper) getToken(ctx sdk.Context, symbol string) (token types.Token, err error) { +func (k Keeper) getTokenBySymbol(ctx sdk.Context, symbol string) (token types.Token, err error) { store := ctx.KVStore(k.storeKey) bz := store.Get(types.KeySymbol(symbol)) From 74c63740635a21431cb73e8e9d710d85da33f1fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E5=BC=BA?= Date: Thu, 28 Jan 2021 17:17:04 +0800 Subject: [PATCH 3/6] revert code --- modules/token/keeper/msg_server.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/modules/token/keeper/msg_server.go b/modules/token/keeper/msg_server.go index 7ac4e7a1..e4b4399f 100644 --- a/modules/token/keeper/msg_server.go +++ b/modules/token/keeper/msg_server.go @@ -139,6 +139,15 @@ func (m msgServer) BurnToken(goCtx context.Context, msg *types.MsgBurnToken) (*t } ctx := sdk.UnwrapSDKContext(goCtx) + + if err := m.Keeper.DeductMintTokenFee(ctx, owner, msg.Symbol); err != nil { + return nil, err + } + + if err := m.Keeper.DeductMintTokenFee(ctx, owner, msg.Symbol); err != nil { + return nil, err + } + if err := m.Keeper.BurnToken(ctx, msg.Symbol, msg.Amount, owner); err != nil { return nil, err } From c4cd5f7df4eebba7b0e215809a9ab19690c5c3fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E5=BC=BA?= Date: Thu, 28 Jan 2021 17:19:04 +0800 Subject: [PATCH 4/6] revert code --- modules/token/keeper/msg_server.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/modules/token/keeper/msg_server.go b/modules/token/keeper/msg_server.go index e4b4399f..d702c09a 100644 --- a/modules/token/keeper/msg_server.go +++ b/modules/token/keeper/msg_server.go @@ -143,11 +143,6 @@ func (m msgServer) BurnToken(goCtx context.Context, msg *types.MsgBurnToken) (*t if err := m.Keeper.DeductMintTokenFee(ctx, owner, msg.Symbol); err != nil { return nil, err } - - if err := m.Keeper.DeductMintTokenFee(ctx, owner, msg.Symbol); err != nil { - return nil, err - } - if err := m.Keeper.BurnToken(ctx, msg.Symbol, msg.Amount, owner); err != nil { return nil, err } From 614aa774360b8c5cefc98ce24611f8ab08c891eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E5=BC=BA?= Date: Thu, 28 Jan 2021 17:52:52 +0800 Subject: [PATCH 5/6] handle error --- modules/token/handler_test.go | 7 +++++-- modules/token/keeper/ante.go | 12 +++++++++-- modules/token/keeper/fees.go | 29 +++++++++++++++++++------- modules/token/keeper/grpc_query.go | 11 ++++++++-- modules/token/keeper/querier.go | 11 ++++++++-- modules/token/simulation/operations.go | 11 ++++++++-- modules/token/types/errors.go | 1 + 7 files changed, 64 insertions(+), 18 deletions(-) diff --git a/modules/token/handler_test.go b/modules/token/handler_test.go index 122c3f79..dfe1bae9 100644 --- a/modules/token/handler_test.go +++ b/modules/token/handler_test.go @@ -91,7 +91,8 @@ func (suite *HandlerSuite) TestIssueToken() { nativeTokenAmt2 := suite.bk.GetBalance(suite.ctx, owner, denom).Amount - fee := suite.keeper.GetTokenIssueFee(suite.ctx, msg.Symbol) + fee, err := suite.keeper.GetTokenIssueFee(suite.ctx, msg.Symbol) + suite.NoError(err) suite.Equal(nativeTokenAmt1.Sub(fee.Amount), nativeTokenAmt2) @@ -121,7 +122,9 @@ func (suite *HandlerSuite) TestMintToken() { mintBtcAmt := sdk.NewIntWithDecimal(int64(msgMintToken.Amount), int(token.Scale)) suite.Equal(beginBtcAmt.Add(mintBtcAmt), endBtcAmt) - fee := suite.keeper.GetTokenMintFee(suite.ctx, token.Symbol) + fee, err := suite.keeper.GetTokenMintFee(suite.ctx, token.Symbol) + suite.NoError(err) + endNativeAmt := suite.bk.GetBalance(suite.ctx, token.GetOwner(), denom).Amount suite.Equal(beginNativeAmt.Sub(fee.Amount), endNativeAmt) diff --git a/modules/token/keeper/ante.go b/modules/token/keeper/ante.go index e9c502b5..735efe37 100644 --- a/modules/token/keeper/ante.go +++ b/modules/token/keeper/ante.go @@ -32,14 +32,22 @@ func (dtf ValidateTokenFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu switch msg := msg.(type) { case *types.MsgIssueToken: - fee := dtf.k.GetTokenIssueFee(ctx, msg.Symbol) + fee, err := dtf.k.GetTokenIssueFee(ctx, msg.Symbol) + if err != nil { + return ctx, sdkerrors.Wrap(types.ErrInvalidBaseFee, err.Error()) + } + if fe, ok := feeMap[msg.Owner]; ok { feeMap[msg.Owner] = fe.Add(fee) } else { feeMap[msg.Owner] = fee } case *types.MsgMintToken: - fee := dtf.k.GetTokenMintFee(ctx, msg.Symbol) + fee, err := dtf.k.GetTokenMintFee(ctx, msg.Symbol) + if err != nil { + return ctx, sdkerrors.Wrap(types.ErrInvalidBaseFee, err.Error()) + } + if fe, ok := feeMap[msg.Owner]; ok { feeMap[msg.Owner] = fe.Add(fee) } else { diff --git a/modules/token/keeper/fees.go b/modules/token/keeper/fees.go index ab47ea27..03edf5d5 100644 --- a/modules/token/keeper/fees.go +++ b/modules/token/keeper/fees.go @@ -19,19 +19,25 @@ const ( // DeductIssueTokenFee performs fee handling for issuing token func (k Keeper) DeductIssueTokenFee(ctx sdk.Context, owner sdk.AccAddress, symbol string) error { // get the required issuance fee - fee := k.GetTokenIssueFee(ctx, symbol) + fee, err := k.GetTokenIssueFee(ctx, symbol) + if err != nil { + return err + } return feeHandler(ctx, k, owner, fee) } // DeductMintTokenFee performs fee handling for minting token func (k Keeper) DeductMintTokenFee(ctx sdk.Context, owner sdk.AccAddress, symbol string) error { // get the required minting fee - fee := k.GetTokenMintFee(ctx, symbol) + fee, err := k.GetTokenMintFee(ctx, symbol) + if err != nil { + return err + } return feeHandler(ctx, k, owner, fee) } // GetTokenIssueFee returns the token issurance fee -func (k Keeper) GetTokenIssueFee(ctx sdk.Context, symbol string) sdk.Coin { +func (k Keeper) GetTokenIssueFee(ctx sdk.Context, symbol string) (sdk.Coin, error) { // get params params := k.GetParamSet(ctx) issueTokenBaseFee := params.IssueTokenBaseFee @@ -43,20 +49,27 @@ func (k Keeper) GetTokenIssueFee(ctx sdk.Context, symbol string) sdk.Coin { } // GetTokenMintFee returns the token minting fee -func (k Keeper) GetTokenMintFee(ctx sdk.Context, symbol string) sdk.Coin { +func (k Keeper) GetTokenMintFee(ctx sdk.Context, symbol string) (sdk.Coin, error) { // get params params := k.GetParamSet(ctx) mintTokenFeeRatio := params.MintTokenFeeRatio // compute the insurance and minting fees - issueFee := k.GetTokenIssueFee(ctx, symbol) + issueFee, err := k.GetTokenIssueFee(ctx, symbol) + if err != nil { + return sdk.Coin{}, err + } mintFee := sdk.NewDecFromInt(issueFee.Amount).Mul(mintTokenFeeRatio) return k.truncateFee(ctx, issueFee.Denom, mintFee) } -func (k Keeper) truncateFee(ctx sdk.Context, denom string, feeAmt sdk.Dec) sdk.Coin { - token, _ := k.GetToken(ctx, denom) +func (k Keeper) truncateFee(ctx sdk.Context, denom string, feeAmt sdk.Dec) (sdk.Coin, error) { + token, err := k.GetToken(ctx, denom) + if err != nil { + return sdk.Coin{}, err + } + precision := sdk.NewIntWithDecimal(1, int(token.GetScale())) feeNativeToken := feeAmt.Quo(sdk.NewDecFromInt(precision)) @@ -66,7 +79,7 @@ func (k Keeper) truncateFee(ctx sdk.Context, denom string, feeAmt sdk.Dec) sdk.C } else { amount = sdk.NewInt(1).Mul(precision) } - return sdk.NewCoin(token.GetMinUnit(), amount) + return sdk.NewCoin(token.GetMinUnit(), amount), nil } // feeHandler handles the fee of token diff --git a/modules/token/keeper/grpc_query.go b/modules/token/keeper/grpc_query.go index 19ed44e2..61e1efe1 100644 --- a/modules/token/keeper/grpc_query.go +++ b/modules/token/keeper/grpc_query.go @@ -114,8 +114,15 @@ func (k Keeper) Fees(c context.Context, req *types.QueryFeesRequest) (*types.Que return nil, status.Errorf(codes.InvalidArgument, err.Error()) } - issueFee := k.GetTokenIssueFee(ctx, req.Symbol) - mintFee := k.GetTokenMintFee(ctx, req.Symbol) + issueFee, err := k.GetTokenIssueFee(ctx, req.Symbol) + if err != nil { + return nil, status.Error(codes.Internal, err.Error()) + } + + mintFee, err := k.GetTokenMintFee(ctx, req.Symbol) + if err != nil { + return nil, status.Error(codes.Internal, err.Error()) + } return &types.QueryFeesResponse{ Exist: k.HasToken(ctx, req.Symbol), diff --git a/modules/token/keeper/querier.go b/modules/token/keeper/querier.go index 351e720f..0e5f71f5 100644 --- a/modules/token/keeper/querier.go +++ b/modules/token/keeper/querier.go @@ -62,8 +62,15 @@ func queryFees(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuer return nil, err } - issueFee := keeper.GetTokenIssueFee(ctx, params.Symbol) - mintFee := keeper.GetTokenMintFee(ctx, params.Symbol) + issueFee, err := keeper.GetTokenIssueFee(ctx, params.Symbol) + if err != nil { + return nil, err + } + + mintFee, err := keeper.GetTokenMintFee(ctx, params.Symbol) + if err != nil { + return nil, err + } fees := types.QueryFeesResponse{ Exist: keeper.HasToken(ctx, params.Symbol), diff --git a/modules/token/simulation/operations.go b/modules/token/simulation/operations.go index 1e59ec89..bc699524 100644 --- a/modules/token/simulation/operations.go +++ b/modules/token/simulation/operations.go @@ -298,7 +298,11 @@ func selectOneToken( return t, nil } - mintFee := k.GetTokenMintFee(ctx, t.GetSymbol()) + mintFee, err := k.GetTokenMintFee(ctx, t.GetSymbol()) + if err != nil { + panic(err) + } + account := ak.GetAccount(ctx, t.GetOwner()) spendable := bk.SpendableCoins(ctx, account.GetAddress()) spendableStake := spendable.AmountOf(nativeToken.MinUnit) @@ -333,7 +337,10 @@ func genToken(ctx sdk.Context, token = randToken(r, accs) } - issueFee := k.GetTokenIssueFee(ctx, token.Symbol) + issueFee, err := k.GetTokenIssueFee(ctx, token.Symbol) + if err != nil { + panic(err) + } account, maxFees := filterAccount(ctx, r, ak, bk, accs, issueFee) token.Owner = account.String() diff --git a/modules/token/types/errors.go b/modules/token/types/errors.go index da1b97c9..73c6ef22 100644 --- a/modules/token/types/errors.go +++ b/modules/token/types/errors.go @@ -21,4 +21,5 @@ var ( ErrNotMintable = sdkerrors.Register(ModuleName, 13, "token is not mintable") ErrNotFoundTokenAmt = sdkerrors.Register(ModuleName, 14, "burn token amount not found") ErrInvalidAmount = sdkerrors.Register(ModuleName, 15, "invalid amount") + ErrInvalidBaseFee = sdkerrors.Register(ModuleName, 16, "invalid base fee") ) From e77a13a051c448fbc1e795ed87bde0df89c4725c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E5=BC=BA?= Date: Thu, 28 Jan 2021 18:09:15 +0800 Subject: [PATCH 6/6] revert --- modules/token/keeper/keeper.go | 18 ++++++++++++------ modules/token/keeper/token.go | 6 +++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/modules/token/keeper/keeper.go b/modules/token/keeper/keeper.go index 3e58efd2..c0b59e99 100644 --- a/modules/token/keeper/keeper.go +++ b/modules/token/keeper/keeper.go @@ -96,11 +96,13 @@ func (k Keeper) EditToken( owner sdk.AccAddress, ) error { // get the destination token - token, err := k.getTokenBySymbol(ctx, symbol) + tokenI, err := k.GetToken(ctx, symbol) if err != nil { return err } + token := tokenI.(*types.Token) + if owner.String() != token.Owner { return sdkerrors.Wrapf(types.ErrInvalidOwner, "the address %s is not the owner of the token %s", owner, symbol) } @@ -129,7 +131,7 @@ func (k Keeper) EditToken( token.Mintable = mintable.ToBool() } - k.setToken(ctx, token) + k.setToken(ctx, *token) return nil } @@ -141,11 +143,13 @@ func (k Keeper) TransferTokenOwner( srcOwner sdk.AccAddress, dstOwner sdk.AccAddress, ) error { - token, err := k.getTokenBySymbol(ctx, symbol) + tokenI, err := k.GetToken(ctx, symbol) if err != nil { return err } + token := tokenI.(*types.Token) + if srcOwner.String() != token.Owner { return sdkerrors.Wrapf(types.ErrInvalidOwner, "the address %s is not the owner of the token %s", srcOwner, symbol) } @@ -153,7 +157,7 @@ func (k Keeper) TransferTokenOwner( token.Owner = dstOwner.String() // update token - k.setToken(ctx, token) + k.setToken(ctx, *token) // reset all indices k.resetStoreKeyForQueryToken(ctx, token.Symbol, srcOwner, dstOwner) @@ -170,11 +174,13 @@ func (k Keeper) MintToken( recipient sdk.AccAddress, owner sdk.AccAddress, ) error { - token, err := k.getTokenBySymbol(ctx, symbol) + tokenI, err := k.GetToken(ctx, symbol) if err != nil { return err } + token := tokenI.(*types.Token) + if owner.String() != token.Owner { return sdkerrors.Wrapf(types.ErrInvalidOwner, "the address %s is not the owner of the token %s", owner, symbol) } @@ -222,7 +228,7 @@ func (k Keeper) BurnToken( amount uint64, owner sdk.AccAddress, ) error { - token, err := k.getTokenBySymbol(ctx, symbol) + token, err := k.GetToken(ctx, symbol) if err != nil { return err } diff --git a/modules/token/keeper/token.go b/modules/token/keeper/token.go index 48f15761..f4d00d94 100644 --- a/modules/token/keeper/token.go +++ b/modules/token/keeper/token.go @@ -50,7 +50,7 @@ func (k Keeper) GetTokens(ctx sdk.Context, owner sdk.AccAddress) (tokens []types func (k Keeper) GetToken(ctx sdk.Context, denom string) (types.TokenI, error) { store := ctx.KVStore(k.storeKey) - if token, err := k.getTokenBySymbol(ctx, denom); err == nil { + if token, err := k.getToken(ctx, denom); err == nil { return &token, nil } @@ -62,7 +62,7 @@ func (k Keeper) GetToken(ctx sdk.Context, denom string) (types.TokenI, error) { var symbol gogotypes.StringValue k.cdc.MustUnmarshalBinaryBare(bz, &symbol) - token, err := k.getTokenBySymbol(ctx, symbol.Value) + token, err := k.getToken(ctx, symbol.Value) if err != nil { return nil, err } @@ -208,7 +208,7 @@ func (k Keeper) setToken(ctx sdk.Context, token types.Token) { store.Set(types.KeySymbol(token.Symbol), bz) } -func (k Keeper) getTokenBySymbol(ctx sdk.Context, symbol string) (token types.Token, err error) { +func (k Keeper) getToken(ctx sdk.Context, symbol string) (token types.Token, err error) { store := ctx.KVStore(k.storeKey) bz := store.Get(types.KeySymbol(symbol))