Skip to content

Commit

Permalink
Merge branch 'add-memeContract' into add-memeContract-btcWallet
Browse files Browse the repository at this point in the history
  • Loading branch information
naturexie committed Jan 23, 2025
2 parents e877d06 + 3c0dab6 commit 067461a
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 112 deletions.
27 changes: 16 additions & 11 deletions hooks/useEstimateSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,38 +37,43 @@ export const useEstimateSwap = ({
forceUpdate,
});
useEffect(() => {
if (dclEstimateResult?.tag && v1EstimateResult?.tag && validator()) {
if (
dclEstimateResult?.tag &&
v1EstimateResult?.tag &&
validator() &&
!v1Loading &&
!dclLoading
) {
getBestEstimateResult();
setEstimateLoading(false);
}
if (tokenIn_amount == "0" || !tokenIn_amount) {
setEstimateResult((prev: any) => {
if (prev?.amount_out !== "0" || !prev?.loadingComplete) {
return { ...prev, amount_out: "0", loadingComplete: true };
}
return prev;
});
if (!Number(tokenIn_amount || 0)) {
setEstimateLoading(false);
}
}, [
dclEstimateResult?.tag,
v1EstimateResult?.tag,
dclEstimateResult?.amount_out,
v1EstimateResult?.amount_out,
tokenIn_amount,
v1Loading,
dclLoading,
]);
useEffect(() => {
setEstimateLoading(v1Loading || dclLoading);
if (v1Loading || dclLoading) {
setEstimateLoading(true);
}
}, [v1Loading, dclLoading]);
function getBestEstimateResult() {
const { amount_out: dcl_amount_out } = dclEstimateResult!;
const { amount_out: v1_amount_out } = v1EstimateResult!;
console.log(dcl_amount_out, v1_amount_out, "dcl_amount_out, v1_amount_out");
// best is v1
if (new Decimal(v1_amount_out || 0).gt(dcl_amount_out || 0)) {
setEstimateResult({ ...v1EstimateResult, loadingComplete: true });
setEstimateResult(v1EstimateResult);
} else if (new Decimal(dcl_amount_out || 0).gt(v1_amount_out || 0)) {
// best is dcl
setEstimateResult({ ...dclEstimateResult, loadingComplete: true });
setEstimateResult(dclEstimateResult);
}
}
function validator() {
Expand Down
52 changes: 37 additions & 15 deletions hooks/useLiqPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,58 @@ export function useLiqPrice({
const token_c_price = token_c_asset?.price?.usd || 0;
const token_p_price = token_p_asset?.price?.usd || 0;
const token_d_price = token_d_asset?.price?.usd || 0;
const hp_fee_decimals = get_hp_fee({
const hp_fee_amount = get_hp_fee({
debt_cap,
uahpi: token_d_asset.uahpi,
uahpi_at_open,
});
const hp_fee = new Decimal(token_d_price).mul(
shrinkToken(
hp_fee_decimals,
hp_fee_amount,
token_d_asset.metadata.decimals + token_d_asset.config.extra_decimals,
),
);
// (token_c_value + token_p_value) * percent > total_debt + total_hp_fee
if (position_type == "Long") {
const p_liq_price = new Decimal(token_d_amount || 0)
.mul(token_d_price)
// c and d are the same token
// const left = new Decimal(token_c_amount || 0)
// .mul(token_c_price)
// .plus(new Decimal(token_p_amount || 0).mul(token_p_price))
// .mul(safety_buffer);
// const right = new Decimal(token_d_amount || 0).plus(hp_fee_amount || 0).mul(token_c_price);
const liq_price = new Decimal(token_d_amount || 0)
.plus(hp_fee)
.div(safety_buffer)
.minus(new Decimal(token_c_amount || 0).mul(token_c_price))
.div(token_p_amount)
.minus(new Decimal(token_c_amount || 0).mul(safety_buffer))
.div(new Decimal(token_p_amount || 0).mul(safety_buffer))
.toFixed();
return p_liq_price;
// const p_liq_price = new Decimal(token_d_amount || 0)
// .mul(token_d_price)
// .plus(hp_fee)
// .div(safety_buffer)
// .minus(new Decimal(token_c_amount || 0).mul(token_c_price))
// .div(token_p_amount)
// .toFixed();
return liq_price;
} else {
// Short
const d_liq_price = new Decimal(token_c_amount)
.mul(token_c_price)
.plus(new Decimal(token_p_amount).mul(token_p_price))
// Short c and p are the same token
// const left = new Decimal(token_c_amount || 0)
// .mul(token_c_price)
// .plus(new Decimal(token_p_amount || 0).mul(token_c_price))
// .mul(safety_buffer);
// const right = new Decimal(token_d_amount || 0).plus(hp_fee_amount || 0).mul(token_d_price);
const liq_price = new Decimal(token_c_amount || 0)
.plus(token_p_amount || 0)
.mul(safety_buffer)
.minus(hp_fee)
.div(token_d_amount)
.div(new Decimal(token_d_amount || 0).plus(hp_fee))
.toFixed();
return d_liq_price;
// const d_liq_price = new Decimal(token_c_amount)
// .mul(token_c_price)
// .plus(new Decimal(token_p_amount).mul(token_p_price))
// .mul(safety_buffer)
// .minus(hp_fee)
// .div(token_d_amount)
// .toFixed();
return liq_price;
}
}

Expand Down
1 change: 0 additions & 1 deletion interfaces/margin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export interface IEstimateResult {
fee: number;
tag: string;
from: "v1" | "dcl";
loadingComplete?: boolean;
}
export interface IPool {
amount_in?: string;
Expand Down
12 changes: 6 additions & 6 deletions screens/Trading/components/ChangeCollateralMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,13 @@ const ChangeCollateralMobile: FC<ChangeCollateralMobileProps> = ({ open, onClose
{+(inputValue || 0) > 0 ? (
<>
<span className="text-gray-300 mr-2 line-through">
${beautifyPrice(LiqPrice)}
{beautifyPrice(LiqPrice)}
</span>
<RightArrow />
<p className="ml-2">${beautifyPrice(LiqPriceNew)}</p>
<p className="ml-2">{beautifyPrice(LiqPriceNew)}</p>
</>
) : (
<p>${beautifyPrice(LiqPrice)}</p>
<p>{beautifyPrice(LiqPrice)}</p>
)}
</div>
</div>
Expand Down Expand Up @@ -732,13 +732,13 @@ const ChangeCollateralMobile: FC<ChangeCollateralMobileProps> = ({ open, onClose
{+(inputValue || 0) > 0 ? (
<>
<span className="text-gray-300 mr-2 line-through">
${beautifyPrice(LiqPrice)}
{beautifyPrice(LiqPrice)}
</span>
<RightArrow />
<p className="ml-2">${beautifyPrice(LiqPriceNew)}</p>
<p className="ml-2">{beautifyPrice(LiqPriceNew)}</p>
</>
) : (
<p>${beautifyPrice(LiqPrice)}</p>
<p>{beautifyPrice(LiqPrice)}</p>
)}
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion screens/Trading/components/ConfirmMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ const ConfirmMobile: React.FC<IConfirmMobileProps | any> = ({
</div>
<div className="flex items-center justify-between text-sm mb-4">
<div className="text-gray-300">Liq. Price</div>
<div>${beautifyPrice(confirmInfo.LiqPrice)}</div>
<div>{beautifyPrice(confirmInfo.LiqPrice)}</div>
</div>

{isMinTokenPAmount && (
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 @@ -290,7 +290,7 @@ const TradingTable = ({
const sortList = {
close_timestamp: "Close time",
open_timestamp: "Opening time",
pnl: "PNL & ROE",
pnl: "PNL",
};
return (
<div className="flex flex-col items-center justify-center lg:w-full xsm:w-[100vw] xsm:px-2">
Expand Down Expand Up @@ -339,7 +339,7 @@ const TradingTable = ({
<th>Entry Price</th>
<th>Index Price</th>
<th>Liq. Price</th>
<th>PNL & ROE</th>
<th>PNL</th>
<th>
<div
onClick={() => handleSort("open_ts")}
Expand Down Expand Up @@ -438,7 +438,7 @@ const TradingTable = ({
<th>Fee</th>
<th onClick={() => handleSortChange("pnl")}>
<div className="flex items-center cursor-pointer">
PNL & ROE
PNL
<SortHistoryButton
activeColor="rgba(192, 196, 233, 1)"
inactiveColor="rgba(192, 196, 233, 0.5)"
Expand Down
34 changes: 20 additions & 14 deletions screens/Trading/components/Table/HistoryRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ const HistoryRow = ({ key, index, record, assetP, assetD, assetC }) => {
return !isMobile ? (
<tr className="align-text-top">
<td className="py-5 pl-5">
{`${getSymbolById(assetP.token_id, assetP.metadata?.symbol)}/${getSymbolById(
assetD.token_id,
assetD.metadata?.symbol,
)}`}
{record.trend === "long"
? `${getSymbolById(assetP.token_id, assetP.metadata?.symbol)}/${getSymbolById(
assetD.token_id,
assetD.metadata?.symbol,
)}`
: `${getSymbolById(assetD.token_id, assetD.metadata?.symbol)}/${getSymbolById(
assetP.token_id,
assetP.metadata?.symbol,
)}`}
<div
className={
record.trend === "long"
Expand Down Expand Up @@ -49,6 +54,11 @@ const HistoryRow = ({ key, index, record, assetP, assetD, assetC }) => {
),
)
: null}
<span className="ml-0.5">
{record.trend === "long"
? `${getSymbolById(assetP.token_id, assetP.metadata?.symbol)}`
: `${getSymbolById(assetD.token_id, assetD.metadata?.symbol)}`}
</span>
</td>
<td>
{beautifyPrice(
Expand All @@ -74,17 +84,11 @@ const HistoryRow = ({ key, index, record, assetP, assetD, assetC }) => {
) : (
"-"
)}
<p className="text-gray-300 text-xs">
{getSymbolById(assetC.token_id, assetC.metadata?.symbol)}
</p>
</div>
</td>
<td>
<div className="flex items-start flex-col">
{record.price !== "0" ? <span>{beautifyPrice(Number(record.price))}</span> : "-"}
<p className="text-gray-300 text-xs">
{getSymbolById(assetC.token_id, assetC.metadata?.symbol)}
</p>
</div>
</td>
<td>
Expand All @@ -104,9 +108,6 @@ const HistoryRow = ({ key, index, record, assetP, assetD, assetC }) => {
{record.pnl !== "0"
? beautifyPrice(Math.abs(record.pnl * record.c_token_price), false, 3, 3)
: ""}
{/* <p className="text-gray-300 text-xs">
{getSymbolById(assetC.token_id, assetC.metadata?.symbol)}
</p> */}
</td>
<td>
<div className="text-sm">
Expand Down Expand Up @@ -194,6 +195,11 @@ const HistoryRow = ({ key, index, record, assetP, assetD, assetC }) => {
),
)
: null}
<span className="ml-0.5">
{record.trend === "long"
? `${getSymbolById(assetP.token_id, assetP.metadata?.symbol)}`
: `${getSymbolById(assetD.token_id, assetD.metadata?.symbol)}`}
</span>
</p>
</div>
<div className="flex items-center justify-between text-sm mb-[18px]">
Expand Down Expand Up @@ -267,7 +273,7 @@ const HistoryRow = ({ key, index, record, assetP, assetD, assetC }) => {
<p>{record.close_type}</p>
</div>
<div className="bg-dark-100 rounded-2xl flex items-center justify-center text-xs py-1 text-gray-300 mb-4">
PNL & ROE
PNL
<p
className={`ml-1 ${
record.pnl > 0 ? "text-green-150" : record.pnl < 0 ? "text-red-150" : ""
Expand Down
32 changes: 15 additions & 17 deletions screens/Trading/components/Table/PositionRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const PositionRow = ({

const netValue = parseTokenValue(item.token_c_info.balance, decimalsC) * (priceC || 0);
const collateral = parseTokenValue(item.token_c_info.balance, decimalsC);
const assetLabel = positionType.label === "Long" ? symbolD : symbolP;
const assetLabel = positionType.label === "Long" ? symbolP : symbolD;
const indexPrice = positionType.label === "Long" ? priceP / priceD : priceD / priceP;
const openTime = new Date(Number(item.open_ts) / 1e6);
const uahpi: any = isMainStream
Expand Down Expand Up @@ -144,7 +144,10 @@ const PositionRow = ({
</td>
<td>
<div className="flex mr-4 items-start flex-col">
<p className=""> {beautifyPrice(size)}</p>
<p className="">
{" "}
{beautifyPrice(size)} {assetLabel}
</p>
<span className="text-gray-300 text-xs">({beautifyPrice(sizeValue, true, 3, 3)})</span>
</div>
</td>
Expand Down Expand Up @@ -174,16 +177,12 @@ const PositionRow = ({
) : (
<span className="text-gray-500">-</span>
)}
<span className="text-gray-300 text-xs">{assetLabel}</span>
</div>
</td>
<td>
<div className="flex items-start flex-col">
{beautifyPrice(indexPrice)}
<span className="text-gray-300 text-xs">{assetLabel}</span>
</div>
<div className="flex items-start flex-col">{beautifyPrice(indexPrice)}</div>
</td>
<td>${beautifyPrice(LiqPrice)}</td>
<td>{beautifyPrice(LiqPrice)}</td>
<td>
<p className={`${pnl > 0 ? "text-green-150" : pnl < 0 ? "text-red-150" : "text-gray-400"}`}>
{pnl === 0 ? "" : `${pnl > 0 ? `+$` : `-$`}`}
Expand Down Expand Up @@ -258,7 +257,10 @@ const PositionRow = ({
</div>
<div className="text-right">
<div className="flex items-center">
<p className="mr-2"> {beautifyPrice(size)}</p>
<p className="mr-2">
{" "}
{beautifyPrice(size)} {assetLabel}
</p>
<span className="text-gray-300 text-sm">({beautifyPrice(sizeValue, true, 3, 3)})</span>
</div>
<p className="text-xs text-gray-300">Size</p>
Expand Down Expand Up @@ -291,30 +293,26 @@ const PositionRow = ({
<p className="text-gray-300">Entry Price</p>
<p>
{entryPrice !== null ? (
<span>
{beautifyPrice(entryPrice)} {assetLabel}
</span>
<span>{beautifyPrice(entryPrice)}</span>
) : (
<span className="text-gray-500">-</span>
)}
</p>
</div>
<div className="flex items-center justify-between text-sm mb-[18px]">
<p className="text-gray-300">Index Price</p>
<p>
{beautifyPrice(indexPrice)} {assetLabel}
</p>
<p>{beautifyPrice(indexPrice)}</p>
</div>
<div className="flex items-center justify-between text-sm mb-[18px]">
<p className="text-gray-300">Liq. Price</p>
<p>${beautifyPrice(LiqPrice)}</p>
<p>{beautifyPrice(LiqPrice)}</p>
</div>
<div className="flex items-center justify-between text-sm mb-[18px]">
<p className="text-gray-300">Opening time</p>
<p>{new Date(openTime).toLocaleString()}</p>
</div>
<div className="bg-dark-100 rounded-2xl flex items-center justify-center text-xs py-1 text-gray-300 mb-4">
PNL & ROE{" "}
PNL{" "}
<p
className={`ml-1 ${
pnl > 0 ? "text-green-150" : pnl < 0 ? "text-red-150" : "text-gray-400"
Expand Down
Loading

0 comments on commit 067461a

Please sign in to comment.