Skip to content
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
4 changes: 0 additions & 4 deletions src/components/TdexOrderInput/TradeRowInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ const TradeRowInput: React.FC<Props> = ({
setInputValue(stringAmount === '0' ? '' : stringAmount);
const satoshis = toSatoshi(stringAmount, assetSelected.precision, unitLBTC).toNumber();
onChangeSats(satoshis);
if (type === 'send' && satoshis > (balance ? balance.amount : 0)) {
// only check balance in case of `send` input
throw new Error(`amount greater than balance`);
}
};

const handleInputChange = (e: CustomEvent) => {
Expand Down
7 changes: 6 additions & 1 deletion src/components/TdexOrderInput/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useState } from 'react';
import type { TradeOrder } from 'tdex-sdk';

import type { TDEXMarket } from '../../redux/actionTypes/tdexActionTypes';
import type { BalanceInterface } from '../../redux/actionTypes/walletActionTypes';
import type { AssetConfig, LbtcDenomination } from '../../utils/constants';
import { defaultPrecision } from '../../utils/constants';
import { NoMarketsAvailableForAllPairsError } from '../../utils/errors';
Expand Down Expand Up @@ -39,7 +40,7 @@ function useAssetSats(initialAssetHash?: string) {
}

// eslint-disable-next-line
export function useTradeState(markets: TDEXMarket[]) {
export function useTradeState(markets: TDEXMarket[], balances: BalanceInterface[]) {
const [sendAsset, sendSats, setSendAsset, setSendSats] = useAssetSats(
markets.length > 0 ? markets[0].baseAsset : undefined
);
Expand Down Expand Up @@ -79,6 +80,8 @@ export function useTradeState(markets: TDEXMarket[]) {
if (receiveLoader || !sendAsset || (focus === 'receive' && !hasBeenSwapped && !sendAssetHasChanged)) return;
try {
setReceiveLoader(true);
const sendBalance = balances.find((b) => b.assetHash === sendAsset)?.amount;
if (newSendSats > (sendBalance ?? 0)) throw new Error(`not enough balance`);
const bestOrder = await discoverBestOrder(markets, sendAsset, receiveAsset)(newSendSats ?? 0, sendAsset);
const assetSats = await marketPriceRequest(bestOrder, newSendSats ?? 0, sendAsset);
setReceiveSats(assetSats.sats);
Expand All @@ -100,6 +103,8 @@ export function useTradeState(markets: TDEXMarket[]) {
setSendLoader(true);
const bestOrder = await discoverBestOrder(markets, sendAsset, receiveAsset)(newReceiveSats ?? 0, receiveAsset);
const assetSats = await marketPriceRequest(bestOrder, newReceiveSats ?? 0, receiveAsset);
const sendBalance = balances.find((b) => b.assetHash === sendAsset)?.amount;
if (assetSats.sats > (sendBalance ?? 0)) throw new Error(`not enough balance`);
setSendSats(assetSats.sats);
setBestOrder(bestOrder);
resetErrors();
Expand Down
8 changes: 6 additions & 2 deletions src/pages/Exchange/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ import type { TdexOrderInputResult } from '../../components/TdexOrderInput';
import TdexOrderInput from '../../components/TdexOrderInput';
import { useTradeState } from '../../components/TdexOrderInput/hooks';
import type { TDEXMarket, TDEXProvider } from '../../redux/actionTypes/tdexActionTypes';
import type { BalanceInterface } from '../../redux/actionTypes/walletActionTypes';
import { setIsFetchingUtxos } from '../../redux/actions/appActions';
import { updateMarkets } from '../../redux/actions/tdexActions';
import { addErrorToast, addSuccessToast } from '../../redux/actions/toastActions';
import { watchTransaction } from '../../redux/actions/transactionsActions';
import { unlockUtxos, updateUtxos } from '../../redux/actions/walletActions';
import { useTypedDispatch } from '../../redux/hooks';
import { lastUsedIndexesSelector, unlockedUtxosSelector } from '../../redux/reducers/walletReducer';
import { balancesSelector, lastUsedIndexesSelector, unlockedUtxosSelector } from '../../redux/reducers/walletReducer';
import type { RootState } from '../../redux/types';
import { routerLinks } from '../../routes';
import { PIN_TIMEOUT_FAILURE, PIN_TIMEOUT_SUCCESS } from '../../utils/constants';
Expand All @@ -55,6 +56,7 @@ import type { PreviewData } from '../TradeSummary';
import ExchangeErrorModal from './ExchangeErrorModal';

export interface ExchangeConnectedProps {
balances: BalanceInterface[];
explorerLiquidAPI: string;
isFetchingMarkets: boolean;
lastUsedIndexes: StateRestorerOpts;
Expand All @@ -68,6 +70,7 @@ export interface ExchangeConnectedProps {
type Props = RouteComponentProps & ExchangeConnectedProps;

const Exchange: React.FC<Props> = ({
balances,
explorerLiquidAPI,
history,
isFetchingMarkets,
Expand Down Expand Up @@ -161,7 +164,7 @@ const Exchange: React.FC<Props> = ({
setHasBeenSwapped,
setSendAssetHasChanged,
setReceiveAssetHasChanged,
] = useTradeState(getAllMarketsFromNotExcludedProviders());
] = useTradeState(getAllMarketsFromNotExcludedProviders(), balances);

const getIdentity = async (pin: string) => {
try {
Expand Down Expand Up @@ -445,6 +448,7 @@ const Exchange: React.FC<Props> = ({

const mapStateToProps = (state: RootState) => {
return {
balances: balancesSelector(state),
explorerLiquidAPI: state.settings.explorerLiquidAPI,
isFetchingMarkets: state.app.isFetchingMarkets,
lastUsedIndexes: lastUsedIndexesSelector(state),
Expand Down