Skip to content

Commit

Permalink
fix:margin trading
Browse files Browse the repository at this point in the history
  • Loading branch information
deep-path committed Nov 18, 2024
1 parent 1d76006 commit 6c77ee6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion interfaces/margin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
4 changes: 2 additions & 2 deletions redux/marginConfigSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions redux/marginConfigState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number>;
Expand All @@ -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: {},
Expand Down
6 changes: 3 additions & 3 deletions screens/Trading/components/ChangeCollateralMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions screens/Trading/components/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 37 additions & 7 deletions screens/Trading/components/TradingOperate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,23 +253,49 @@ 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 =
(((Number(shortInput) +
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;
}
Expand Down Expand Up @@ -458,9 +484,11 @@ const TradingOperate = () => {
<div className="flex items-center justify-between text-sm mb-4">
<div className="text-gray-300">Fee</div>
<div className="flex items-center justify-center">
<p className="border-b border-dashed border-dark-800">0.26</p>
<p className="border-b border-dashed border-dark-800">{Fee.fee}</p>
NEAR
<span className="text-xs text-gray-300 ml-1.5">($0.89)</span>
<span className="text-xs text-gray-300 ml-1.5">
(${Fee.fee * (Fee.price || 0)})
</span>
</div>
</div>
<div className="flex items-baseline justify-between text-sm mb-4">
Expand Down Expand Up @@ -580,7 +608,7 @@ const TradingOperate = () => {
<div className="flex items-center justify-between text-sm mb-4">
<div className="text-gray-300">Minimum received</div>
<div className="text-right">
{Number(toInternationalCurrencySystem_number(longOutput)) *
{Number(toInternationalCurrencySystem_number(shortOutput)) *
(1 - slippageTolerance / 100)}{" "}
NEAR
</div>
Expand All @@ -601,9 +629,11 @@ const TradingOperate = () => {
<div className="flex items-center justify-between text-sm mb-4">
<div className="text-gray-300">Fee</div>
<div className="flex items-center justify-center">
<p className="border-b border-dashed border-dark-800">0.26</p>
<p className="border-b border-dashed border-dark-800">{Fee.fee}</p>
NEAR
<span className="text-xs text-gray-300 ml-1.5">($0.89)</span>
<span className="text-xs text-gray-300 ml-1.5">
(${Fee.fee * (Fee.price || 0)})
</span>
</div>
</div>
<div className="flex items-baseline justify-between text-sm mb-4">
Expand Down

0 comments on commit 6c77ee6

Please sign in to comment.