Skip to content

Commit

Permalink
Merge pull request #474 from klim0v/v1.2.1
Browse files Browse the repository at this point in the history
refactor v1.2.1
  • Loading branch information
danil-lashin authored Dec 11, 2020
2 parents 4eb6506 + 1f6aa64 commit cac664b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 53 deletions.
16 changes: 1 addition & 15 deletions api/v2/service/estimate_coin_buy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package service

import (
"context"
"fmt"
"github.com/MinterTeam/minter-go-node/core/code"
"github.com/MinterTeam/minter-go-node/core/commissions"
"github.com/MinterTeam/minter-go-node/core/transaction"
Expand Down Expand Up @@ -69,19 +68,6 @@ func (s *Service) EstimateCoinBuy(ctx context.Context, req *pb.EstimateCoinBuyRe
coinTo := cState.Coins().GetCoin(coinToBuy)

if !coinToSell.IsBaseCoin() {
if coinFrom.Reserve().Cmp(commissionInBaseCoin) < 0 {
return nil, s.createError(
status.New(codes.InvalidArgument, fmt.Sprintf("Coin reserve balance is not sufficient for transaction. Has: %s, required %s",
coinFrom.Reserve().String(), commissionInBaseCoin.String())),
transaction.EncodeError(code.NewCoinReserveNotSufficient(
coinFrom.GetFullSymbol(),
coinFrom.ID().String(),
coinFrom.Reserve().String(),
commissionInBaseCoin.String(),
)),
)
}

commission = formula.CalculateSaleAmount(coinFrom.Volume(), coinFrom.Reserve(), coinFrom.Crr(), commissionInBaseCoin)
}

Expand All @@ -95,10 +81,10 @@ func (s *Service) EstimateCoinBuy(ctx context.Context, req *pb.EstimateCoinBuyRe
}

if !coinToSell.IsBaseCoin() {
value = formula.CalculateSaleAmount(coinFrom.Volume(), coinFrom.Reserve(), coinFrom.Crr(), value)
if errResp := transaction.CheckReserveUnderflow(coinFrom, value); errResp != nil {
return nil, s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
}
value = formula.CalculateSaleAmount(coinFrom.Volume(), coinFrom.Reserve(), coinFrom.Crr(), value)
}

return &pb.EstimateCoinBuyResponse{
Expand Down
16 changes: 1 addition & 15 deletions api/v2/service/estimate_coin_sell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package service

import (
"context"
"fmt"
"github.com/MinterTeam/minter-go-node/core/code"
"github.com/MinterTeam/minter-go-node/core/commissions"
"github.com/MinterTeam/minter-go-node/core/transaction"
Expand Down Expand Up @@ -69,19 +68,6 @@ func (s *Service) EstimateCoinSell(ctx context.Context, req *pb.EstimateCoinSell
coinTo := cState.Coins().GetCoin(coinToBuy)

if !coinToSell.IsBaseCoin() {
if coinFrom.Reserve().Cmp(commissionInBaseCoin) < 0 {
return nil, s.createError(
status.New(codes.InvalidArgument, fmt.Sprintf("Coin reserve balance is not sufficient for transaction. Has: %s, required %s",
coinFrom.Reserve().String(), commissionInBaseCoin.String())),
transaction.EncodeError(code.NewCoinReserveNotSufficient(
coinFrom.GetFullSymbol(),
coinFrom.ID().String(),
coinFrom.Reserve().String(),
commissionInBaseCoin.String(),
)),
)
}

commission = formula.CalculateSaleAmount(coinFrom.Volume(), coinFrom.Reserve(), coinFrom.Crr(), commissionInBaseCoin)
}

Expand All @@ -95,10 +81,10 @@ func (s *Service) EstimateCoinSell(ctx context.Context, req *pb.EstimateCoinSell
}

if !coinToBuy.IsBaseCoin() {
value = formula.CalculatePurchaseReturn(coinTo.Volume(), coinTo.Reserve(), coinTo.Crr(), value)
if errResp := transaction.CheckForCoinSupplyOverflow(coinTo, value); errResp != nil {
return nil, s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
}
value = formula.CalculatePurchaseReturn(coinTo.Volume(), coinTo.Reserve(), coinTo.Crr(), value)
}

res := &pb.EstimateCoinSellResponse{
Expand Down
25 changes: 5 additions & 20 deletions api/v2/service/estimate_coin_sell_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package service

import (
"context"
"fmt"
"github.com/MinterTeam/minter-go-node/core/code"
"github.com/MinterTeam/minter-go-node/core/commissions"
"github.com/MinterTeam/minter-go-node/core/transaction"
Expand Down Expand Up @@ -75,34 +74,20 @@ func (s *Service) EstimateCoinSellAll(ctx context.Context, req *pb.EstimateCoinS
value := valueToSell

if !coinToSell.IsBaseCoin() {
if coinFrom.Reserve().Cmp(commissionInBaseCoin) < 0 {
return nil, s.createError(
status.New(codes.InvalidArgument, fmt.Sprintf("Coin reserve balance is not sufficient for transaction. Has: %s, required %s",
coinFrom.Reserve().String(), commissionInBaseCoin.String())),
transaction.EncodeError(code.NewCoinReserveNotSufficient(
coinFrom.GetFullSymbol(),
coinFrom.ID().String(),
coinFrom.Reserve().String(),
commissionInBaseCoin.String(),
)),
)
}

value = formula.CalculateSaleReturn(coinFrom.Volume(), coinFrom.Reserve(), coinFrom.Crr(), valueToSell)
if errResp := transaction.CheckReserveUnderflow(coinFrom, value); errResp != nil {
return nil, s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
}
}

value.Sub(value, commissionInBaseCoin)
if value.Sign() != 1 {
return nil, status.New(codes.FailedPrecondition, "Not enough coins to pay commission").Err()
}
if !coinToBuy.IsBaseCoin() {
value = formula.CalculatePurchaseReturn(coinTo.Volume(), coinTo.Reserve(), coinTo.Crr(), value)
if errResp := transaction.CheckForCoinSupplyOverflow(coinTo, value); errResp != nil {
return nil, s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
}
value.Sub(value, valueToSell)
if value.Sign() != 1 {
return nil, status.New(codes.FailedPrecondition, "Not enough coins to pay commission").Err()
}
value = formula.CalculatePurchaseReturn(coinTo.Volume(), coinTo.Reserve(), coinTo.Crr(), value)
}

return &pb.EstimateCoinSellAllResponse{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/stretchr/testify v1.6.1
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca
github.com/tendermint/go-amino v0.15.1
github.com/tendermint/iavl v0.14.3-0.20201112111120-f5ab75a110eb
github.com/tendermint/iavl v0.14.3
github.com/tendermint/tendermint v0.33.8
github.com/tendermint/tm-db v0.5.2
github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c/go.mod h1:ahpPrc7
github.com/tendermint/go-amino v0.14.1/go.mod h1:i/UKE5Uocn+argJJBb12qTZsCDBcAYMbR92AaJVmKso=
github.com/tendermint/go-amino v0.15.1 h1:D2uk35eT4iTsvJd9jWIetzthE5C0/k2QmMFkCN+4JgQ=
github.com/tendermint/go-amino v0.15.1/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
github.com/tendermint/iavl v0.14.3-0.20201112111120-f5ab75a110eb h1:RKHi/zIW55JQ1TXFvwMsERCC3TYZV3aB7dV3mFdqSr8=
github.com/tendermint/iavl v0.14.3-0.20201112111120-f5ab75a110eb/go.mod h1:vHLYxU/zuxBmxxr1v+5Vnd/JzcIsyK17n9P9RDubPVU=
github.com/tendermint/iavl v0.14.3 h1:tuiUAqJdA3OOyPU/9P3pMYnAcd+OL7BUdzNiE3ytUwQ=
github.com/tendermint/iavl v0.14.3/go.mod h1:vHLYxU/zuxBmxxr1v+5Vnd/JzcIsyK17n9P9RDubPVU=
github.com/tendermint/tendermint v0.33.5 h1:jYgRd9ImkzA9iOyhpmgreYsqSB6tpDa6/rXYPb8HKE8=
github.com/tendermint/tendermint v0.33.5/go.mod h1:0yUs9eIuuDq07nQql9BmI30FtYGcEC60Tu5JzB5IezM=
github.com/tendermint/tendermint v0.33.8 h1:Xxu4QhpqcomSE0iQDw1MqLgfsa8fqtPtWFJK6zZOVso=
Expand Down

0 comments on commit cac664b

Please sign in to comment.