Skip to content

Commit

Permalink
Add /estimate_coin_sell_all endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
danil-lashin committed Apr 9, 2019
1 parent bc75297 commit f30bb51
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 0.18.0

IMPROVEMENT

- [api] Add `/estimate_coin_sell_all` endpoint

BUG FIXES

- [p2p] Make new addressbook file for each testnet
Expand Down
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var Routes = map[string]*rpcserver.RPCFunc{
"net_info": rpcserver.NewRPCFunc(NetInfo, ""),
"coin_info": rpcserver.NewRPCFunc(CoinInfo, "symbol,height"),
"estimate_coin_sell": rpcserver.NewRPCFunc(EstimateCoinSell, "coin_to_sell,coin_to_buy,value_to_sell,height"),
"estimate_coin_sell_all": rpcserver.NewRPCFunc(EstimateCoinSellAll, "coin_to_sell,coin_to_buy,value_to_sell,gas_price,height"),
"estimate_coin_buy": rpcserver.NewRPCFunc(EstimateCoinBuy, "coin_to_sell,coin_to_buy,value_to_buy,height"),
"estimate_tx_commission": rpcserver.NewRPCFunc(EstimateTxCommission, "tx,height"),
"unconfirmed_txs": rpcserver.NewRPCFunc(UnconfirmedTxs, "limit"),
Expand Down
83 changes: 83 additions & 0 deletions api/estimate_coin_sell_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package api

import (
"github.com/MinterTeam/minter-go-node/core/commissions"
"github.com/MinterTeam/minter-go-node/core/transaction"
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/MinterTeam/minter-go-node/formula"
"github.com/MinterTeam/minter-go-node/rpc/lib/types"
"math/big"
)

type EstimateCoinSellAllResponse struct {
WillGet *big.Int `json:"will_get"`
}

func EstimateCoinSellAll(
coinToSellString string, coinToBuyString string, valueToSell *big.Int, gasPrice uint64, height int) (*EstimateCoinSellAllResponse,
error) {
cState, err := GetStateForHeight(height)
if err != nil {
return nil, err
}

if gasPrice < 1 {
gasPrice = 1
}

coinToSell := types.StrToCoinSymbol(coinToSellString)
coinToBuy := types.StrToCoinSymbol(coinToBuyString)

var result *big.Int

if coinToSell == coinToBuy {
return nil, rpctypes.RPCError{Code: 400, Message: "\"From\" coin equals to \"to\" coin"}
}

if !cState.CoinExists(coinToSell) {
return nil, rpctypes.RPCError{Code: 404, Message: "Coin to sell not exists"}
}

if !cState.CoinExists(coinToBuy) {
return nil, rpctypes.RPCError{Code: 404, Message: "Coin to buy not exists"}
}

commissionInBaseCoin := big.NewInt(commissions.ConvertTx)
commissionInBaseCoin.Mul(commissionInBaseCoin, transaction.CommissionMultiplier)
commission := big.NewInt(0).Set(commissionInBaseCoin)

switch {
case coinToSell == types.GetBaseCoin():
coin := cState.GetStateCoin(coinToBuy).Data()

valueToSell.Sub(valueToSell, commission)
if valueToSell.Cmp(big.NewInt(0)) != 1 {
return nil, rpctypes.RPCError{Code: 400, Message: "Not enough coins to pay commission"}
}

result = formula.CalculatePurchaseReturn(coin.Volume, coin.ReserveBalance, coin.Crr, valueToSell)
case coinToBuy == types.GetBaseCoin():
coin := cState.GetStateCoin(coinToSell).Data()
result = formula.CalculateSaleReturn(coin.Volume, coin.ReserveBalance, coin.Crr, valueToSell)

result.Sub(result, commission)
if result.Cmp(big.NewInt(0)) != 1 {
return nil, rpctypes.RPCError{Code: 400, Message: "Not enough coins to pay commission"}
}
default:
coinFrom := cState.GetStateCoin(coinToSell).Data()
coinTo := cState.GetStateCoin(coinToBuy).Data()
basecoinValue := formula.CalculateSaleReturn(coinFrom.Volume, coinFrom.ReserveBalance, coinFrom.Crr, valueToSell)

basecoinValue.Sub(basecoinValue, commission)
if basecoinValue.Cmp(big.NewInt(0)) != 1 {
return nil, rpctypes.RPCError{Code: 400, Message: "Not enough coins to pay commission"}
}

result = formula.CalculatePurchaseReturn(coinTo.Volume, coinTo.ReserveBalance, coinTo.Crr, basecoinValue)
}

return &EstimateCoinSellAllResponse{
WillGet: result,
}, nil
}

0 comments on commit f30bb51

Please sign in to comment.