Merge pull request #1 from Permissionless-Software-Foundation/ct-unstable

fix(orders): Fixing bug with sats to bch calculation
This commit is contained in:
Chris Troutner
2022-05-02 08:31:56 -07:00
committed by GitHub
3 changed files with 23 additions and 20 deletions
+7 -6
View File
@@ -1,14 +1,14 @@
import React from 'react'
import { Row, Col, Content, Box, DataTable, Button, Inputs } from 'adminlte-2-react'
import { Row, Col, Content, Box, DataTable, Button } from 'adminlte-2-react'
import Details from './details'
import './orders.css'
import Spinner from '../../../images/loader.gif'
const { Text } = Inputs
// const { Text } = Inputs
const axios = require('axios').default
const siteConfig = require('../../site-config')
let SERVER = siteConfig.bchDexUrl
const SERVER = siteConfig.bchDexUrl
// const EXPLORER_URL = 'https://explorer.bitcoin.com/bch/tx/'
@@ -189,8 +189,8 @@ class Orders extends React.Component {
const data = []
// console.log(`dataArr: ${JSON.stringify(dataArr, null, 2)}`)
if(!this.props.bchWallet) {
console.log(`BCH wallet is not initialized. Can not calculate price of BCH or tokens.`)
if (!this.props.bchWallet) {
console.log('BCH wallet is not initialized. Can not calculate price of BCH or tokens.')
return
}
@@ -203,7 +203,8 @@ class Orders extends React.Component {
const order = dataArr[i]
// console.log(`order: ${JSON.stringify(order, null, 2)}`)
const satsTotal = order.numTokens * order.rateInBaseUnit
const satsTotal = Math.ceil(order.numTokens * order.rateInBaseUnit)
console.log(`satsTotal: ${satsTotal}`)
let usdTotal = bchSpotPrice * this.props.bchWallet.bchjs.BitcoinCash.toBitcoinCash(satsTotal)
usdTotal = `$${this.props.bchWallet.bchjs.Util.floor8(usdTotal)}`
+10 -10
View File
@@ -242,7 +242,7 @@ class SellModal extends React.Component {
}
// Update the quantity of tokens to sell
handleSellQty(event) {
handleSellQty (event) {
const value = event.target.value
// console.log('value: ', value)
@@ -253,7 +253,7 @@ class SellModal extends React.Component {
// console.log('_this.state.sellQty: ', _this.state.sellQty)
}
handlePricePerToken(event) {
handlePricePerToken (event) {
const value = event.target.value
// console.log('value: ', value)
@@ -289,11 +289,11 @@ class SellModal extends React.Component {
// This is called when the user clicks the 'Sell' button to place their order.
handleConfirm () {
// Verify that inputs are valid.
if(isNaN(_this.state.sellQty)) {
if (isNaN(_this.state.sellQty)) {
alert('Token quantity must be a number.')
return
}
if(isNaN(_this.state.pricePerToken)) {
if (isNaN(_this.state.pricePerToken)) {
alert('Price per token must be a number.')
return
}
@@ -317,7 +317,7 @@ class SellModal extends React.Component {
}, 200)
}
async sellTokens(isConfirmed) {
async sellTokens (isConfirmed) {
try {
// Dismiss
if (!isConfirmed) {
@@ -336,7 +336,7 @@ class SellModal extends React.Component {
console.log('token: ', token)
// Error if trying to sell more tokens than the wallet holds.
if(token.qty < _this.state.sellQty) {
if (token.qty < _this.state.sellQty) {
alert(`Error: The wallet has ${token.qty} tokens, which is less than the ${_this.state.sellQty} tokens you are trying to sell.`)
}
@@ -345,7 +345,7 @@ class SellModal extends React.Component {
// Convert the dollar amount of tokens to sats.
const bchSpotPrice = await bchWallet.getUsd()
console.log('bchSpotPrice: ', bchSpotPrice)
const bchPerToken = _this.state.pricePerToken/bchSpotPrice
const bchPerToken = _this.state.pricePerToken / bchSpotPrice
console.log('bchPerToken: ', bchPerToken)
const satsPerToken = Math.floor(bchWallet.bchjs.BitcoinCash.toSatoshi(bchPerToken))
console.log('satsPerToken: ', satsPerToken)
@@ -359,12 +359,12 @@ class SellModal extends React.Component {
buyOrSell: 'sell',
numTokens: _this.state.sellQty,
rateInBaseUnit: satsPerToken,
minUnitsToExchange: satsPerToken*_this.state.sellQty,
minUnitsToExchange: satsPerToken * _this.state.sellQty
}
}
const result = await axios.post(`${siteConfig.bchDexUrl}/order`, orderObj)
console.log(`result.data: `, result.data)
console.log('result.data: ', result.data)
// console.log('starting sleep')
// await bchWallet.bchjs.Util.sleep(5000)
@@ -375,7 +375,7 @@ class SellModal extends React.Component {
txId: result.data.hash,
inFetch: false
})
} catch(error) {
} catch (error) {
console.warn(error)
_this.setState({
errMsg: error.message,
+6 -4
View File
@@ -36,10 +36,12 @@ const config = {
}
// Attempt to auto-detect the URL for the bch-dex.
if(window && window.document && window.document.domain) {
if(window.document.domain.includes('192.168') ||
window.document.domain.includes('localhost')) {
config.bchDexUrl = `http://${window.document.domain}:5700`
if (typeof window !== 'undefined') {
if (window && window.document && window.document.domain) {
if (window.document.domain.includes('192.168') ||
window.document.domain.includes('localhost')) {
config.bchDexUrl = `http://${window.document.domain}:5700`
}
}
}