diff --git a/routes/dao_coin_exchange_with_fees.go b/routes/dao_coin_exchange_with_fees.go index 2c8162a6..f812041a 100644 --- a/routes/dao_coin_exchange_with_fees.go +++ b/routes/dao_coin_exchange_with_fees.go @@ -1600,6 +1600,8 @@ func (fes *APIServer) HandleMarketOrder( // Again ONLY when deso is the quote currency do we need to do this extra step. if IsDesoPkid(req.QuoteCurrencyPublicKeyBase58Check) { bigLimitAmount = big.NewInt(0).Div(bigLimitAmount, big.NewInt(int64(lib.NanosPerUnit))) + } else if IsDesoPkid(req.BaseCurrencyPublicKeyBase58Check) { + bigLimitAmount = big.NewInt(0).Mul(bigLimitAmount, big.NewInt(int64(lib.NanosPerUnit))) } uint256LimitAmount, overflow := uint256.FromBig(bigLimitAmount) if overflow { @@ -1731,30 +1733,24 @@ func (fes *APIServer) HandleMarketOrder( // The receive amount is the buying coin quantity filled because that's how we set up the order. executionReceiveAmount := newOrderRes.SimulatedExecutionResult.BuyingCoinQuantityFilled executionReceiveAmountCurrencyType := CurrencyTypeBase - executionReceiveAmountBaseUnits, err := CalculateBaseUnitsFromStringDecimalAmountSimple( - req.BaseCurrencyPublicKeyBase58Check, executionReceiveAmount) if err != nil { return nil, fmt.Errorf("HandleMarketOrder: Problem calculating base currency received: %v", err) } // The price per token the user is getting, expressed as a decimal float - // - quoteAmountSpentTotal / baseAmountReceived - // - = (quoteAmountSpentTotal * BaseUnitsPerCoin / baseAmountReceived) / BaseUnitsPerCoin + // - executionAmount / executionReceiveAmount executionPriceInQuoteCurrency := "" - if !executionReceiveAmountBaseUnits.IsZero() { - priceQuotePerBase := big.NewInt(0).Mul( - quoteCurrencyExecutedPlusFeesBaseUnits.ToBig(), lib.BaseUnitsPerCoin.ToBig()) - priceQuotePerBase = big.NewInt(0).Div( - priceQuotePerBase, executionReceiveAmountBaseUnits.ToBig()) - uint256PriceQuotePerBase, overflow := uint256.FromBig(priceQuotePerBase) - if overflow { - return nil, fmt.Errorf("HandleMarketOrder: Overflow calculating price: %v", err) - } - executionPriceInQuoteCurrency, err = CalculateStringDecimalAmountFromBaseUnitsSimple( - req.QuoteCurrencyPublicKeyBase58Check, uint256PriceQuotePerBase) - if err != nil { - return nil, fmt.Errorf("HandleMarketOrder: Problem calculating price: %v", err) - } + executionAmountFloat, err := strconv.ParseFloat(executionAmount, 64) + if err != nil { + return nil, fmt.Errorf("HandleMarketOrder: Problem parsing execution amount: %v", err) + } + executionReceiveAmountFloat, err := strconv.ParseFloat(executionReceiveAmount, 64) + if err != nil { + return nil, fmt.Errorf("HandleMarketOrder: Problem parsing execution receive amount: %v", err) + } + if executionReceiveAmountFloat > 0.0 { + executionPriceInQuoteCurrencyFloat := executionAmountFloat / executionReceiveAmountFloat + executionPriceInQuoteCurrency = fmt.Sprintf("%0.9f", executionPriceInQuoteCurrencyFloat) } // Compute the percentage of the amount spent that went to fees @@ -1849,6 +1845,8 @@ func (fes *APIServer) HandleMarketOrder( // Again ONLY when deso is the quote currency do we need to do this extra step. if IsDesoPkid(req.QuoteCurrencyPublicKeyBase58Check) { bigLimitAmount = big.NewInt(0).Div(bigLimitAmount, big.NewInt(int64(lib.NanosPerUnit))) + } else if IsDesoPkid(req.BaseCurrencyPublicKeyBase58Check) { + bigLimitAmount = big.NewInt(0).Mul(bigLimitAmount, big.NewInt(int64(lib.NanosPerUnit))) } uint256LimitAmount, overflow := uint256.FromBig(bigLimitAmount) if overflow { @@ -1890,6 +1888,8 @@ func (fes *APIServer) HandleMarketOrder( // = (1e9) * limitAmountQuote / scaledPrice if IsDesoPkid(req.QuoteCurrencyPublicKeyBase58Check) { bigLimitReceiveAmount = big.NewInt(0).Mul(bigLimitReceiveAmount, big.NewInt(int64(lib.NanosPerUnit))) + } else if IsDesoPkid(req.BaseCurrencyPublicKeyBase58Check) { + bigLimitReceiveAmount = big.NewInt(0).Div(bigLimitReceiveAmount, big.NewInt(int64(lib.NanosPerUnit))) } uint256LimitReceiveAmount, overflow := uint256.FromBig(bigLimitReceiveAmount) if overflow { @@ -2021,21 +2021,18 @@ func (fes *APIServer) HandleMarketOrder( } // The price per token the user is getting, expressed as a decimal float // - quoteAmountReceived / baseAmountSpent - // - = (quoteAmountReceived * BaseUnitsPerCoin / baseAmountReceived) / BaseUnitsPerCoin finalPriceStr := "0.0" if !baseAmountSpentBaseUnits.IsZero() { - priceQuotePerBase := big.NewInt(0).Mul( - quoteAmountReceivedBaseUnits.ToBig(), lib.BaseUnitsPerCoin.ToBig()) - priceQuotePerBase = big.NewInt(0).Div( - priceQuotePerBase, baseAmountSpentBaseUnits.ToBig()) - uint256PriceQuotePerBase, overflow := uint256.FromBig(priceQuotePerBase) - if overflow { - return nil, fmt.Errorf("HandleMarketOrder: Overflow calculating price: %v", err) + quoteAmountReceivedFloat, err := strconv.ParseFloat(quoteAmountReceivedStr, 64) + if err != nil { + return nil, fmt.Errorf("HandleMarketOrder: Problem parsing quote amount received: %v", err) } - finalPriceStr, err = CalculateStringDecimalAmountFromBaseUnitsSimple( - req.QuoteCurrencyPublicKeyBase58Check, uint256PriceQuotePerBase) + baseAmountSpentFloat, err := strconv.ParseFloat(baseAmountSpentStr, 64) if err != nil { - return nil, fmt.Errorf("HandleMarketOrder: Problem calculating price: %v", err) + return nil, fmt.Errorf("HandleMarketOrder: Problem parsing base amount spent: %v", err) + } + if baseAmountSpentFloat != 0 { + finalPriceStr = fmt.Sprintf("%0.9f", quoteAmountReceivedFloat/baseAmountSpentFloat) } } @@ -2131,6 +2128,8 @@ func (fes *APIServer) HandleMarketOrder( // - 1e9 * quoteAmount / scaledPriceBasePerQuote if IsDesoPkid(req.QuoteCurrencyPublicKeyBase58Check) { bigLimitAmount = big.NewInt(0).Mul(bigLimitAmount, big.NewInt(int64(lib.NanosPerUnit))) + } else if IsDesoPkid(req.BaseCurrencyPublicKeyBase58Check) { + bigLimitAmount = big.NewInt(0).Div(bigLimitAmount, big.NewInt(int64(lib.NanosPerUnit))) } uint256LimitAmount, overflow := uint256.FromBig(bigLimitAmount) if overflow { @@ -2179,6 +2178,8 @@ func (fes *APIServer) HandleMarketOrder( // = 1/1e9 * limitAmountBaseUnits * scaledPriceQuotePerBase if IsDesoPkid(req.QuoteCurrencyPublicKeyBase58Check) { bigLimitReceiveAmount = big.NewInt(0).Div(bigLimitReceiveAmount, big.NewInt(int64(lib.NanosPerUnit))) + } else if IsDesoPkid(req.BaseCurrencyPublicKeyBase58Check) { + bigLimitReceiveAmount = big.NewInt(0).Mul(bigLimitReceiveAmount, big.NewInt(int64(lib.NanosPerUnit))) } uint256LimitReceiveAmount, overflow := uint256.FromBig(bigLimitReceiveAmount) if overflow {