Skip to content

Commit

Permalink
fix(trade): pending transaction and re-rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive authored and tomasklim committed Nov 22, 2024
1 parent 402c04d commit a55654c
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,86 +342,100 @@ export const useCoinmarketExchangeForm = ({
}
};

const confirmTrade = async (address: string, extraField?: string, trade?: ExchangeTrade) => {
analytics.report({
type: EventType.CoinmarketConfirmTrade,
payload: {
type,
},
});
const confirmTrade = useCallback(
async (address: string, extraField?: string, trade?: ExchangeTrade) => {
analytics.report({
type: EventType.CoinmarketConfirmTrade,
payload: {
type,
},
});

let ok = false;
const { address: refundAddress } = getUnusedAddressFromAccount(account);
if (!trade) {
trade = selectedQuote;
}
if (!trade || !refundAddress) return false;
let ok = false;
const { address: refundAddress } = getUnusedAddressFromAccount(account);
if (!trade) {
trade = selectedQuote;
}
if (!trade || !refundAddress) return false;

if (trade.isDex && !trade.fromAddress) {
trade = { ...trade, fromAddress: refundAddress };
}
if (trade.isDex && !trade.fromAddress) {
trade = { ...trade, fromAddress: refundAddress };
}

setCallInProgress(true);
const response = await invityAPI.doExchangeTrade({
trade,
receiveAddress: address,
refundAddress,
extraField,
});
setCallInProgress(true);
const response = await invityAPI.doExchangeTrade({
trade,
receiveAddress: address,
refundAddress,
extraField,
});

if (!response) {
dispatch(
notificationsActions.addToast({
type: 'error',
error: 'No response from the server',
}),
);
} else if (
response.error ||
!response.status ||
!response.orderId ||
response.status === 'ERROR'
) {
dispatch(
notificationsActions.addToast({
type: 'error',
error: response.error || 'Error response from the server',
}),
);
dispatch(coinmarketExchangeActions.saveSelectedQuote(response));
} else if (response.status === 'APPROVAL_REQ' || response.status === 'APPROVAL_PENDING') {
dispatch(coinmarketExchangeActions.saveSelectedQuote(response));
setExchangeStep('SEND_APPROVAL_TRANSACTION');
ok = true;
} else if (response.status === 'CONFIRM') {
dispatch(coinmarketExchangeActions.saveSelectedQuote(response));
if (response.isDex) {
if (exchangeStep === 'RECEIVING_ADDRESS' || trade.approvalType === 'ZERO') {
setExchangeStep('SEND_APPROVAL_TRANSACTION');
if (!response) {
dispatch(
notificationsActions.addToast({
type: 'error',
error: 'No response from the server',
}),
);
} else if (
response.error ||
!response.status ||
!response.orderId ||
response.status === 'ERROR'
) {
dispatch(
notificationsActions.addToast({
type: 'error',
error: response.error || 'Error response from the server',
}),
);
dispatch(coinmarketExchangeActions.saveSelectedQuote(response));
} else if (
response.status === 'APPROVAL_REQ' ||
response.status === 'APPROVAL_PENDING'
) {
dispatch(coinmarketExchangeActions.saveSelectedQuote(response));
setExchangeStep('SEND_APPROVAL_TRANSACTION');
ok = true;
} else if (response.status === 'CONFIRM') {
dispatch(coinmarketExchangeActions.saveSelectedQuote(response));
if (response.isDex) {
if (exchangeStep === 'RECEIVING_ADDRESS' || trade.approvalType === 'ZERO') {
setExchangeStep('SEND_APPROVAL_TRANSACTION');
} else {
setExchangeStep('SEND_TRANSACTION');
}
} else {
setExchangeStep('SEND_TRANSACTION');
}
ok = true;
} else {
setExchangeStep('SEND_TRANSACTION');
// CONFIRMING, SUCCESS
dispatch(
coinmarketExchangeActions.saveTrade(
response,
selectedAccount.account,
new Date().toISOString(),
),
);
dispatch(coinmarketExchangeActions.saveTransactionId(response.orderId));
ok = true;
navigateToExchangeDetail();
}
ok = true;
} else {
// CONFIRMING, SUCCESS
dispatch(
coinmarketExchangeActions.saveTrade(
response,
selectedAccount.account,
new Date().toISOString(),
),
);
dispatch(coinmarketExchangeActions.saveTransactionId(response.orderId));
ok = true;
navigateToExchangeDetail();
}
setCallInProgress(false);
setCallInProgress(false);

return ok;
};
return ok;
},
[
account,
selectedQuote,
exchangeStep,
selectedAccount.account,
dispatch,
setCallInProgress,
navigateToExchangeDetail,
],
);

const sendDexTransaction = async () => {
if (
Expand Down Expand Up @@ -592,7 +606,7 @@ export const useCoinmarketExchangeForm = ({
}

checkQuotesTimer(handleChange);
});
}, [quotesRequest, isNotFormPage, navigateToExchangeForm, checkQuotesTimer, handleChange]);

useEffect(() => {
return () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { useTimeoutFn, useUnmount } from 'react-use';

import { ExchangeTrade } from 'invity-api';

import { useDispatch } from 'src/hooks/suite';
import invityAPI from 'src/services/suite/invityAPI';
import { CoinmarketTradeExchangeType } from 'src/types/coinmarket/coinmarket';
import { useDispatch } from 'src/hooks/suite';
import { saveSelectedQuote } from 'src/actions/wallet/coinmarketExchangeActions';

interface CoinmarketUseExchangeWatchSendApprovalProps {
selectedQuote?: ExchangeTrade;
Expand Down Expand Up @@ -49,19 +50,24 @@ export const useCoinmarketExchangeWatchSendApproval = ({
refreshCount,
);

if (!selectedQuote.dexTx) return;
if (!response.status || response.status === selectedQuote.status) return;
if (response.status && response.status !== selectedQuote.status) {
const updatedSelectedQuote = {
...selectedQuote,
status: response.status,
error: response.error,
approvalType: undefined,
};

dispatch(saveSelectedQuote(updatedSelectedQuote));

await confirmTrade(selectedQuote.dexTx.from, undefined, {
...selectedQuote,
status: response.status,
error: response.error,
approvalType: undefined,
});
if (selectedQuote.dexTx) {
await confirmTrade(selectedQuote.dexTx.from, undefined, updatedSelectedQuote);
}
}

resetRefresh();
};

watchTradeAsync();
}, [refreshCount, selectedQuote, cancelRefresh, confirmTrade, resetRefresh, dispatch]);
}, [refreshCount, selectedQuote, cancelRefresh, resetRefresh, dispatch, confirmTrade]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { CoinmarketTradeExchangeType } from 'src/types/coinmarket/coinmarket';
import { useCoinmarketFormContext } from 'src/hooks/wallet/coinmarket/form/useCoinmarketCommonForm';
import { useCoinmarketInfo } from 'src/hooks/wallet/coinmarket/useCoinmarketInfo';
import { useCoinmarketExchangeWatchSendApproval } from 'src/hooks/wallet/coinmarket/form/useCoinmarketExchangeWatchSendApproval';
import { useDispatch } from 'src/hooks/suite';
import { saveSelectedQuote } from 'src/actions/wallet/coinmarketExchangeActions';
import { parseCryptoId } from 'src/utils/wallet/coinmarket/coinmarketUtils';

// add APPROVED means no approval request is necessary
type ExtendedDexApprovalType = DexApprovalType | 'APPROVED';
Expand All @@ -32,6 +35,7 @@ const BreakableValue = styled.span`
`;

export const CoinmarketOfferExchangeSendApproval = () => {
const dispatch = useDispatch();
const {
account,
callInProgress,
Expand Down Expand Up @@ -62,10 +66,11 @@ export const CoinmarketOfferExchangeSendApproval = () => {
exchangeInfo?.providerInfos[exchange]?.companyName || selectedQuote.exchange;

const isFullApproval = !(Number(selectedQuote.preapprovedStringAmount) > 0);
const isToken = selectedQuote.send !== account.symbol.toUpperCase();

if (!selectedQuote.send) return null;

const isToken = parseCryptoId(selectedQuote.send)?.contractAddress !== undefined;

if (isFullApproval && approvalType === 'ZERO') {
setApprovalType('MINIMAL');
}
Expand All @@ -79,20 +84,32 @@ export const CoinmarketOfferExchangeSendApproval = () => {
const selectApprovalValue = async (type: ExtendedDexApprovalType) => {
setApprovalType(type);
if (type !== 'APPROVED') {
await confirmTrade(dexTx.from, undefined, {
const updatedSelectedQuote = {
...selectedQuote,
approvalType: type,
});
};

dispatch(saveSelectedQuote(updatedSelectedQuote));

await confirmTrade(dexTx.from, undefined, updatedSelectedQuote);
}
};

// if the last step was change in approval, we have to recompute the swap request
const proceedToSwap = async () => {
if (selectedQuote.approvalType) {
const confirmedTrade = await confirmTrade(dexTx.from, undefined, {
const updatedSelectedQuote = {
...selectedQuote,
approvalType: undefined,
});
};
dispatch(
saveSelectedQuote({
...selectedQuote,
approvalType: undefined,
}),
);

const confirmedTrade = await confirmTrade(dexTx.from, undefined, updatedSelectedQuote);

if (!confirmedTrade) {
return;
Expand Down

0 comments on commit a55654c

Please sign in to comment.