Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When the baseFee of the token is an external asset, issue&mint Token error #103

Merged
merged 8 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions modules/token/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions modules/token/keeper/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 21 additions & 8 deletions modules/token/keeper/fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))

Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions modules/token/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 0 additions & 1 deletion modules/token/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +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.BurnToken(ctx, msg.Symbol, msg.Amount, owner); err != nil {
return nil, err
}
Expand Down
11 changes: 9 additions & 2 deletions modules/token/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
11 changes: 9 additions & 2 deletions modules/token/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions modules/token/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)