forked from burrowfdn/burrow-cash
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
200 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { NearIconMini } from "../../screens/MarginTrading/components/Icon"; | ||
import { CloseIcon } from "../Icons/Icons"; | ||
|
||
interface FailureModalProps { | ||
show: boolean; | ||
onClose: () => void; | ||
title?: string; | ||
errorMessage?: string; | ||
type?: "Long" | "Short"; | ||
} | ||
|
||
const ModalWithFailure = ({ | ||
show, | ||
onClose, | ||
title = "Close Position", | ||
errorMessage = "Operation failed, please try again later", | ||
type = "Long", | ||
}: FailureModalProps) => { | ||
const [isModalVisible, setIsModalVisible] = useState(false); | ||
const [countdown, setCountdown] = useState(10); | ||
const [progress, setProgress] = useState(100); | ||
let countdownTimer; | ||
|
||
useEffect(() => { | ||
if (show) { | ||
setIsModalVisible(true); | ||
startCountdown(); | ||
} else { | ||
setIsModalVisible(false); | ||
clearTimeoutOrInterval(countdownTimer); | ||
} | ||
return () => clearTimeoutOrInterval(countdownTimer); | ||
}, [show]); | ||
|
||
const clearTimeoutOrInterval = (timerId) => { | ||
if (timerId) { | ||
clearInterval(timerId); | ||
countdownTimer = null; | ||
} | ||
}; | ||
|
||
const hideModal = () => { | ||
setIsModalVisible(false); | ||
onClose(); | ||
clearTimeout(countdownTimer); | ||
}; | ||
|
||
const startCountdown = () => { | ||
setCountdown(10); | ||
setProgress(100); | ||
|
||
const timerInterval = 1000; | ||
countdownTimer = setInterval(() => { | ||
setCountdown((prevCountdown) => { | ||
if (prevCountdown <= 1) { | ||
hideModal(); | ||
clearTimeoutOrInterval(countdownTimer); | ||
return 0; | ||
} | ||
return prevCountdown - 1; | ||
}); | ||
setProgress((prevProgress) => Math.max(prevProgress - 10, 0)); | ||
}, timerInterval); | ||
}; | ||
|
||
return ( | ||
<div> | ||
{isModalVisible && ( | ||
<div className="z-50 fixed right-5 bottom-10 w-93 bg-dark-100 text-white border rounded-sm"> | ||
<div className="relative w-full p-6 flex flex-col gap-3"> | ||
<div onClick={onClose} className="absolute" style={{ top: "-6px", right: "-4px" }}> | ||
<CloseIcon /> | ||
</div> | ||
<div className="fc"> | ||
<NearIconMini /> | ||
<span className="font-normal text-base px-2">{title}</span> | ||
<div className="text-toolTipBoxBorderColor text-sm ml-auto">Success</div> | ||
</div> | ||
<div className="w-full h-1 bg-black"> | ||
<div | ||
className="h-full bg-toolTipBoxBorderColor transition-all ease-linear" | ||
style={{ | ||
width: `${progress}%`, | ||
transitionDuration: "950ms", | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ModalWithFailure; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { shrinkToken } from "../store"; | ||
import { getTransactionResult, parsedArgs } from "../utils/txhashContract"; | ||
import { | ||
showPositionResult, | ||
showPositionClose, | ||
showPositionFailure, | ||
} from "../components/HashResultModal"; | ||
|
||
interface TransactionResult { | ||
txHash: string; | ||
result: any; | ||
hasStorageDeposit: boolean; | ||
} | ||
|
||
export const handleTransactionResults = async ( | ||
transactionHashes: string | string[] | undefined, | ||
errorMessage?: string | string[], | ||
) => { | ||
if (transactionHashes) { | ||
try { | ||
const txhash = Array.isArray(transactionHashes) | ||
? transactionHashes | ||
: transactionHashes.split(","); | ||
|
||
const results = await Promise.all( | ||
txhash.map(async (txHash: string): Promise<TransactionResult> => { | ||
const result: any = await getTransactionResult(txHash); | ||
const hasStorageDeposit = result.transaction.actions.some( | ||
(action: any) => action?.FunctionCall?.method_name === "margin_execute_with_pyth", | ||
); | ||
return { txHash, result, hasStorageDeposit }; | ||
}), | ||
); | ||
|
||
results.forEach(({ result, hasStorageDeposit }: TransactionResult) => { | ||
if (hasStorageDeposit) { | ||
const args = parsedArgs(result?.transaction?.actions?.[0]?.FunctionCall?.args || ""); | ||
const { actions } = JSON.parse(args || ""); | ||
const isLong = actions[0]?.OpenPosition?.token_p_id == "wrap.testnet"; | ||
|
||
if (actions[0]?.CloseMTPosition) { | ||
showPositionClose({}); | ||
} else { | ||
showPositionResult({ | ||
title: "Open Position", | ||
type: isLong ? "Long" : "Short", | ||
price: (isLong | ||
? Number(shrinkToken(actions[0]?.OpenPosition?.token_d_amount, 18)) / | ||
Number(shrinkToken(actions[0]?.OpenPosition?.min_token_p_amount, 24)) | ||
: Number(shrinkToken(actions[0]?.OpenPosition?.min_token_p_amount, 18)) / | ||
Number(shrinkToken(actions[0]?.OpenPosition?.token_d_amount, 24)) | ||
).toString(), | ||
transactionHashes: txhash[0], | ||
positionSize: { | ||
amount: isLong | ||
? shrinkToken(actions[0]?.OpenPosition?.min_token_p_amount, 24, 6) | ||
: shrinkToken(actions[0]?.OpenPosition?.token_d_amount, 24, 6), | ||
symbol: "NEAR", | ||
usdValue: "1000", | ||
}, | ||
}); | ||
} | ||
} | ||
}); | ||
} catch (error) { | ||
console.error("Error processing transactions:", error); | ||
} | ||
} | ||
|
||
if (errorMessage) { | ||
showPositionFailure({ | ||
title: "Open Position", | ||
errorMessage: decodeURIComponent(errorMessage as string), | ||
}); | ||
} | ||
}; |