diff --git a/screens/Trading/components/ConfirmMobile.tsx b/screens/Trading/components/ConfirmMobile.tsx index 3c499cf9..290ea272 100644 --- a/screens/Trading/components/ConfirmMobile.tsx +++ b/screens/Trading/components/ConfirmMobile.tsx @@ -20,7 +20,7 @@ import { beautifyPrice } from "../../../utils/beautyNumbet"; import DataSource from "../../../data/datasource"; import { getAccountId } from "../../../redux/accountSelectors"; import { useRouterQuery } from "../../../utils/txhashContract"; -import { handleTransactionHash } from "../../../services/transaction"; +import { handleTransactionHash, handleTransactionResults } from "../../../services/transaction"; import { useToastMessage } from "../../../hooks/hooks"; import { showPositionClose, showPositionFailure } from "../../../components/HashResultModal"; @@ -52,60 +52,112 @@ const ConfirmMobile = ({ open, onClose, action, confirmInfo }) => { : getAssetById(confirmInfo.longInputName?.token_id), ); const cateSymbol = getTokenSymbolOnly(ReduxcategoryAssets1?.metadata?.symbol); + // const confirmOpenPosition = async () => { + // if (Object.values(marginAccountList).length >= max_active_user_margin_position) { + // return showToast("User has exceeded the maximum number of open positions!"); + // } + + // setIsDisabled(true); + // if (action === "Long") { + // try { + // const res: any = await openPosition({ + // token_c_amount: confirmInfo.longInput, + // token_c_id: confirmInfo.longInputName?.token_id, + // token_d_amount: confirmInfo.tokenInAmount, + // token_d_id: confirmInfo.longInputName?.token_id, + // token_p_id: confirmInfo.longOutputName?.token_id, + // min_token_p_amount: confirmInfo.estimateData.min_amount_out, + // swap_indication: confirmInfo.estimateData.swap_indication, + // assets: confirmInfo.assets.data, + // }); + // const k: any = []; + // res.forEach((item: any) => { + // k.push(item.transaction.hash); + // }); + // handleTransactionResults(k); + // // handleTransactionResultsYield(k); + // } catch (error) { + // console.log(error); + // showPositionFailure({ + // title: "Open Position Failed", + // errorMessage: JSON.stringify(error), + // type: action, + // }); + // } finally { + // setIsDisabled(false); + // onClose(); + // } + // } else { + // try { + // const res: any = await openPosition({ + // token_c_amount: confirmInfo.longInput, + // token_c_id: confirmInfo.longInputName?.token_id, + // token_d_amount: confirmInfo.tokenInAmount, + // token_d_id: confirmInfo.longOutputName?.token_id, + // token_p_id: confirmInfo.longInputName?.token_id, + // min_token_p_amount: confirmInfo.estimateData.min_amount_out, + // swap_indication: confirmInfo.estimateData.swap_indication, + // assets: confirmInfo.assets.data, + // }); + // console.log(res); + // const k: any = []; + // res.forEach((item: any) => { + // k.push(item.transaction.hash); + // }); + // handleTransactionResults(k); + // } catch (error) { + // console.log(error); + // showPositionFailure({ + // title: "Open Position Failed", + // errorMessage: JSON.stringify(error), + // type: action, + // }); + // } finally { + // setIsDisabled(false); + // onClose(); + // } + // } + // }; + const confirmOpenPosition = async () => { if (Object.values(marginAccountList).length >= max_active_user_margin_position) { return showToast("User has exceeded the maximum number of open positions!"); } setIsDisabled(true); - if (action === "Long") { - try { - const res = await openPosition({ - token_c_amount: confirmInfo.longInput, - token_c_id: confirmInfo.longInputName?.token_id, - token_d_amount: confirmInfo.tokenInAmount, - token_d_id: confirmInfo.longInputName?.token_id, - token_p_id: confirmInfo.longOutputName?.token_id, - min_token_p_amount: confirmInfo.estimateData.min_amount_out, - swap_indication: confirmInfo.estimateData.swap_indication, - assets: confirmInfo.assets.data, - }); - console.log(res); - } catch (error) { - console.log(error); - showPositionFailure({ - title: "Open Position Failed", - errorMessage: JSON.stringify(error), - type: action, - }); - } finally { - setIsDisabled(false); - onClose(); - } - } else { - try { - const res = await openPosition({ - token_c_amount: confirmInfo.longInput, - token_c_id: confirmInfo.longInputName?.token_id, - token_d_amount: confirmInfo.tokenInAmount, - token_d_id: confirmInfo.longOutputName?.token_id, - token_p_id: confirmInfo.longInputName?.token_id, - min_token_p_amount: confirmInfo.estimateData.min_amount_out, - swap_indication: confirmInfo.estimateData.swap_indication, - assets: confirmInfo.assets.data, - }); - console.log(res); - } catch (error) { - console.log(error); - showPositionFailure({ - title: "Open Position Failed", - errorMessage: JSON.stringify(error), - type: action, - }); - } finally { - setIsDisabled(false); - onClose(); - } + + try { + const openPositionParams = { + token_c_amount: confirmInfo.longInput, + token_c_id: confirmInfo.longInputName?.token_id, + token_d_amount: confirmInfo.tokenInAmount, + token_d_id: + action === "Long" + ? confirmInfo.longInputName?.token_id + : confirmInfo.longOutputName?.token_id, + token_p_id: + action === "Long" + ? confirmInfo.longOutputName?.token_id + : confirmInfo.longInputName?.token_id, + min_token_p_amount: confirmInfo.estimateData.min_amount_out, + swap_indication: confirmInfo.estimateData.swap_indication, + assets: confirmInfo.assets.data, + }; + + const res: any = await openPosition(openPositionParams); + const transactionHashes = res.map( + (item: { transaction: { hash: string } }) => item.transaction.hash, + ); + handleTransactionResults(transactionHashes); + } catch (error) { + showPositionFailure({ + title: "Open Position Failed", + errorMessage: JSON.stringify(error), + type: action, + }); + } finally { + setIsDisabled(false); + onClose(); } }; diff --git a/store/marginActions/openPosition.ts b/store/marginActions/openPosition.ts index 13e2fcb9..fc275b89 100644 --- a/store/marginActions/openPosition.ts +++ b/store/marginActions/openPosition.ts @@ -107,6 +107,5 @@ export async function openPosition({ ], }); - const result = await prepareAndExecuteTransactions(transactions); - return result; + return prepareAndExecuteTransactions(transactions); } diff --git a/store/tokens.ts b/store/tokens.ts index a9c74ffd..a562ff40 100644 --- a/store/tokens.ts +++ b/store/tokens.ts @@ -198,5 +198,5 @@ export const prepareAndExecuteTransactions = async (operations: Transaction[] = } transactions.push(...operations); - await executeMultipleTransactions(transactions); + return executeMultipleTransactions(transactions); }; diff --git a/store/wallet.ts b/store/wallet.ts index e7ef71c3..0df2e635 100644 --- a/store/wallet.ts +++ b/store/wallet.ts @@ -42,10 +42,11 @@ export const executeMultipleTransactions = async (transactions) => { try { const wallet = await selector.wallet(); - await wallet.signAndSendTransactions({ + const result = await wallet.signAndSendTransactions({ transactions: selectorTransactions, }); if (fetchData) fetchData(account.accountId); + return result; } catch (e: any) { if (/reject/.test(e)) { alert("Transaction was rejected in wallet. Please try again!");