diff --git a/screens/Trading/index.tsx b/screens/Trading/index.tsx
index 2535774b..018b9f02 100644
--- a/screens/Trading/index.tsx
+++ b/screens/Trading/index.tsx
@@ -21,12 +21,13 @@ import { useMarginConfigToken } from "../../hooks/useMarginConfig";
import { setCategoryAssets1, setCategoryAssets2 } from "../../redux/marginTrading";
import { useMarginAccount } from "../../hooks/useMarginAccount";
import { useAccountId, usePortfolioAssets } from "../../hooks/hooks";
-import { useRouterQuery, getTransactionResult } from "../../utils/txhashContract";
+import { useRouterQuery, getTransactionResult, parsedArgs } from "../../utils/txhashContract";
import { showPositionResult } from "../../components/HashResultModal";
init_env("dev");
const Trading = () => {
+ const marginConfig = useAppSelector(getMarginConfig);
const { query } = useRouterQuery();
const accountId = useAccountId();
const { marginAccountList, parseTokenValue, getAssetDetails, getAssetById } = useMarginAccount();
@@ -53,27 +54,6 @@ const Trading = () => {
};
let timer;
- //
- useEffect(() => {
- if (router.query.transactionHashes) {
- //
- showPositionResult({
- title: "Open Position",
- type: "Long", // optional,"Long" 或 "Short"
- price: "100", // optional,
- transactionHashes: Array.isArray(router.query.transactionHashes)
- ? router.query.transactionHashes[0]
- : (router.query.transactionHashes as string),
- positionSize: {
- // optional,
- amount: "10",
- symbol: "NEAR",
- usdValue: "1000",
- },
- });
- }
- }, [router]);
-
// computed currentTokenCate1 dropdown
useEffect(() => {
if (id) {
@@ -168,11 +148,58 @@ const Trading = () => {
const results = await Promise.all(
txhash.map(async (txHash: string) => {
const result: any = await getTransactionResult(txHash);
- console.log(result);
- return { txHash };
+ const hasStorageDeposit = result.transaction.actions.some(
+ (action: any) => action?.FunctionCall?.method_name === "margin_execute_with_pyth",
+ );
+ return { txHash, result, hasStorageDeposit };
}),
);
- console.log(results, ";rrrr");
+ results.forEach(({ txHash, result, hasStorageDeposit }: any) => {
+ if (hasStorageDeposit) {
+ // handleClaimTokenMobile({ level, count });
+ console.log(result);
+
+ const args = parsedArgs(result?.transaction?.actions?.[0]?.FunctionCall?.args || "");
+ const { actions } = JSON.parse(args || "");
+
+ // const ft_on_transfer_id = result?.receipts?.findIndex((r: any) =>
+ // r?.receipt?.Action?.actions?.some(
+ // (a: any) => a?.FunctionCall?.method_name === "margin_execute_with_pyth",
+ // ),
+ // );
+
+ // const ft_on_transfer_logs =
+ // result?.receipts_outcome?.[ft_on_transfer_id]?.outcome?.logs || "";
+ // const ft_on_transfer_log = ft_on_transfer_logs?.[ft_on_transfer_logs?.length - 1];
+ // const idx = ft_on_transfer_log?.indexOf("{");
+
+ // const parsed_ft_on_transfer_log = JSON.parse(ft_on_transfer_log.slice(idx) || "");
+ const isLong = actions[0]?.OpenPosition?.token_p_id == "wrap.testnet";
+ console.log(actions);
+ console.log(marginConfig);
+ 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: Array.isArray(router.query.transactionHashes)
+ ? router.query.transactionHashes[0]
+ : (router.query.transactionHashes as string),
+ positionSize: {
+ // optional,
+ 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);
}
diff --git a/utils/txhashContract.ts b/utils/txhashContract.ts
index 03e97fdd..5e394bb2 100644
--- a/utils/txhashContract.ts
+++ b/utils/txhashContract.ts
@@ -73,3 +73,9 @@ export function useRouterQuery() {
return { query, replaceQuery };
}
+
+export const parsedArgs = (res: any) => {
+ const buff = Buffer.from(res, "base64");
+ const parsedData = buff.toString("ascii");
+ return parsedData;
+};
From 6c77ee6c72d38cefd1c129875e787aad7069427f Mon Sep 17 00:00:00 2001
From: deep-path <476044723@qq.com>
Date: Mon, 18 Nov 2024 17:27:27 +0800
Subject: [PATCH 4/4] fix:margin trading
---
interfaces/margin.ts | 2 +-
redux/marginConfigSlice.ts | 4 +-
redux/marginConfigState.ts | 4 +-
.../components/ChangeCollateralMobile.tsx | 6 +--
screens/Trading/components/Table.tsx | 6 +--
screens/Trading/components/TradingOperate.tsx | 44 ++++++++++++++++---
6 files changed, 48 insertions(+), 18 deletions(-)
diff --git a/interfaces/margin.ts b/interfaces/margin.ts
index daf80479..9be68997 100644
--- a/interfaces/margin.ts
+++ b/interfaces/margin.ts
@@ -4,7 +4,7 @@ export interface IMarginConfig {
max_leverage_rate: number;
pending_debt_scale: number;
max_slippage_rate: number;
- min_safty_buffer: number;
+ min_safety_buffer: number;
margin_debt_discount_rate: number;
open_position_fee_rate: number;
registered_dexes: { [dexId: string]: number };
diff --git a/redux/marginConfigSlice.ts b/redux/marginConfigSlice.ts
index a9325d6f..e901deac 100644
--- a/redux/marginConfigSlice.ts
+++ b/redux/marginConfigSlice.ts
@@ -28,7 +28,7 @@ export const marginConfigSlice = createSlice({
max_leverage_rate,
pending_debt_scale,
max_slippage_rate,
- min_safty_buffer,
+ min_safety_buffer,
margin_debt_discount_rate,
open_position_fee_rate,
registered_dexes,
@@ -38,7 +38,7 @@ export const marginConfigSlice = createSlice({
state.max_leverage_rate = max_leverage_rate;
state.pending_debt_scale = pending_debt_scale;
state.max_slippage_rate = max_slippage_rate;
- state.min_safty_buffer = min_safty_buffer;
+ state.min_safety_buffer = min_safety_buffer;
state.margin_debt_discount_rate = margin_debt_discount_rate;
state.open_position_fee_rate = open_position_fee_rate;
state.registered_dexes = registered_dexes;
diff --git a/redux/marginConfigState.ts b/redux/marginConfigState.ts
index e2300dcc..bb1f85bf 100644
--- a/redux/marginConfigState.ts
+++ b/redux/marginConfigState.ts
@@ -3,7 +3,7 @@ export interface IMarginConfigState {
max_leverage_rate: number;
pending_debt_scale: number;
max_slippage_rate: number;
- min_safty_buffer: number;
+ min_safety_buffer: number;
margin_debt_discount_rate: number;
open_position_fee_rate: number;
registered_dexes: Record
;
@@ -17,7 +17,7 @@ export const initialState: IMarginConfigState = {
max_leverage_rate: 0,
pending_debt_scale: 0,
max_slippage_rate: 0,
- min_safty_buffer: 0,
+ min_safety_buffer: 0,
margin_debt_discount_rate: 0,
open_position_fee_rate: 0,
registered_dexes: {},
diff --git a/screens/Trading/components/ChangeCollateralMobile.tsx b/screens/Trading/components/ChangeCollateralMobile.tsx
index 7130c9f4..fe6c9288 100644
--- a/screens/Trading/components/ChangeCollateralMobile.tsx
+++ b/screens/Trading/components/ChangeCollateralMobile.tsx
@@ -119,13 +119,13 @@ const ChangeCollateralMobile = ({ open, onClose, rowData, collateralTotal }) =>
(rowData.data.debt_cap *
((debt_assets_d?.unit_acc_hp_interest ?? 0) - rowData.data.uahpi_at_open)) /
10 ** 18;
- const decrease_cap = total_cap * (marginConfigTokens.min_safty_buffer / 10000);
- const denominator = sizeValueLong * (1 - marginConfigTokens.min_safty_buffer / 10000);
+ const decrease_cap = total_cap * (marginConfigTokens.min_safety_buffer / 10000);
+ const denominator = sizeValueLong * (1 - marginConfigTokens.min_safety_buffer / 10000);
const LiqPrice =
denominator !== 0
? (total_debt +
total_hp_fee +
- (priceC * leverageC * marginConfigTokens.min_safty_buffer) / 10000 -
+ (priceC * leverageC * marginConfigTokens.min_safety_buffer) / 10000 -
priceC * leverageC) /
denominator
: 0;
diff --git a/screens/Trading/components/Table.tsx b/screens/Trading/components/Table.tsx
index b79b77b8..66808b21 100644
--- a/screens/Trading/components/Table.tsx
+++ b/screens/Trading/components/Table.tsx
@@ -234,14 +234,14 @@ const PositionRow = ({
const total_debt = leverageD * priceD;
const total_hp_fee =
(item.debt_cap * ((debt_assets_d?.unit_acc_hp_interest ?? 0) - item.uahpi_at_open)) / 10 ** 18;
- // const decrease_cap = total_cap * (marginConfigTokens.min_safty_buffer / 10000);
- const denominator = sizeValueLong * (1 - marginConfigTokens.min_safty_buffer / 10000);
+ // const decrease_cap = total_cap * (marginConfigTokens.min_safety_buffer / 10000);
+ const denominator = sizeValueLong * (1 - marginConfigTokens.min_safety_buffer / 10000);
// total_cap - decrease_cap === total_debt + total_hp_fee;
const LiqPrice =
denominator !== 0
? (total_debt +
total_hp_fee +
- (priceC * leverageC * marginConfigTokens.min_safty_buffer) / 10000 -
+ (priceC * leverageC * marginConfigTokens.min_safety_buffer) / 10000 -
priceC * leverageC) /
denominator
: 0;
diff --git a/screens/Trading/components/TradingOperate.tsx b/screens/Trading/components/TradingOperate.tsx
index e8a7eb93..0df51f0e 100644
--- a/screens/Trading/components/TradingOperate.tsx
+++ b/screens/Trading/components/TradingOperate.tsx
@@ -253,7 +253,7 @@ const TradingOperate = () => {
if (rangeMount > 1) {
if (activeTab == "long") {
const k1 = Number(longInput) * rangeMount * (getAssetPrice(ReduxcategoryAssets2) as any);
- const k2 = 1 - marginConfigTokens.min_safty_buffer / 10000;
+ const k2 = 1 - marginConfigTokens.min_safety_buffer / 10000;
liqPriceX = (k1 / k2 - Number(longInput)) / longOutput;
} else {
liqPriceX =
@@ -261,15 +261,41 @@ const TradingOperate = () => {
Number(shrinkToken(estimateData?.min_amount_out, decimalsC))) as any) *
rangeMount *
(getAssetPrice(ReduxcategoryAssets2) as any) *
- (1 - marginConfigTokens.min_safty_buffer / 10000)) /
+ (1 - marginConfigTokens.min_safety_buffer / 10000)) /
shortOutput;
}
}
+ // const total_debt =
+ // (shrinkToken(ReduxcategoryAssets2.margin_debt.balance, decimalsD) as any) * priceD;
+ // const total_hp_fee =
+ // (ReduxcategoryAssets2.margin_debt.debt_cap * (unit_acc_hp_interest - uahpi_at_open)) /
+ // 10 ** 18;
+
+ // const numerator =
+ // total_debt +
+ // total_hp_fee -
+ // (shrinkToken(ReduxcategoryAssets1.margin_debt.balance, decimalsD) as any) *
+ // priceC *
+ // (1 - marginConfigTokens.min_safety_buffer / 10000);
+
+ // const denominator =
+ // estimateData?.min_amount_out * (1 - marginConfigTokens.min_safety_buffer / 10000);
+
+ console.log(marginConfigTokens, ReduxcategoryAssets2, ReduxcategoryAssets1);
+
setLiqPrice(liqPriceX);
}
}, [longOutput, shortOutput]);
+ const Fee = useMemo(() => {
+ console.log(ReduxcategoryAssets1, ReduxcategoryAssets2);
+ return {
+ fee: (Number(longInput || shortInput) * config.open_position_fee_rate) / 10000,
+ price: getAssetPrice(ReduxcategoryAssets1),
+ };
+ }, [longInput, shortInput, ReduxcategoryAssets1]);
+
function getAssetPrice(categoryId) {
return categoryId ? assets.data[categoryId["token_id"]].price?.usd : 0;
}
@@ -458,9 +484,11 @@ const TradingOperate = () => {
Fee
-
0.26
+
{Fee.fee}
NEAR
-
($0.89)
+
+ (${Fee.fee * (Fee.price || 0)})
+
@@ -580,7 +608,7 @@ const TradingOperate = () => {
Minimum received
- {Number(toInternationalCurrencySystem_number(longOutput)) *
+ {Number(toInternationalCurrencySystem_number(shortOutput)) *
(1 - slippageTolerance / 100)}{" "}
NEAR
@@ -601,9 +629,11 @@ const TradingOperate = () => {
Fee
-
0.26
+
{Fee.fee}
NEAR
-
($0.89)
+
+ (${Fee.fee * (Fee.price || 0)})
+