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 15, 2025
2 parents a76ab70 + 8cf7a00 commit 04a4f44
Show file tree
Hide file tree
Showing 11 changed files with 686 additions and 574 deletions.
1 change: 1 addition & 0 deletions interfaces/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface IAssetConfig {
can_borrow: boolean;
net_tvl_multiplier: number;
holding_position_fee_rate: string;
min_borrowed_amount?: string;
}

export interface IAssetEntry {
Expand Down
10 changes: 6 additions & 4 deletions screens/Trading/components/ClosePositionMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useAppSelector } from "../../../redux/hooks";
import { Wrapper } from "../../../components/Modal/style";
import { DEFAULT_POSITION } from "../../../utils/config";
import { CloseIcon } from "../../../components/Modal/svg";
import { RefLogoIcon, RightArrow, MaxPositionIcon } from "./TradingIcon";
import { RightArrow, MaxPositionIcon } from "./TradingIcon";
import { toInternationalCurrencySystem_number, toDecimal } from "../../../utils/uiNumber";
import { closePosition } from "../../../store/marginActions/closePosition";
import { useEstimateSwap } from "../../../hooks/useEstimateSwap";
Expand All @@ -22,15 +22,14 @@ import {
YellowSolidSubmitButton as YellowSolidButton,
RedSolidSubmitButton as RedSolidButton,
} from "../../../components/Modal/button";
import { showPositionClose } from "../../../components/HashResultModal";
import { beautifyPrice } from "../../../utils/beautyNumber";
import { findPathReserve } from "../../../api/get-swap-path";
import { getAssets, getAssetsMEME } from "../../../redux/assetsSelectors";
import { useMarginAccount } from "../../../hooks/useMarginAccount";
import { IClosePositionMobileProps } from "../comInterface";
import { getMarginConfig, getMarginConfigMEME } from "../../../redux/marginConfigSelectors";
import { handleTransactionHash } from "../../../services/transaction";
import DataSource from "../../../data/datasource";
import { showPositionFailure } from "../../../components/HashResultModal";
import { useRegisterTokenType } from "../../../hooks/useRegisterTokenType";

export const ModalContext = createContext(null) as any;
Expand Down Expand Up @@ -204,7 +203,10 @@ const ClosePositionMobile: React.FC<IClosePositionMobileProps> = ({
await handleTransactionHash(transactionHashes);
}
} catch (error) {
console.error("Failed to close position:", error);
showPositionFailure({
title: "Transactions error",
errorMessage: error instanceof Error ? error.message : JSON.stringify(error),
});
} finally {
setIsDisabled(false);
}
Expand Down
41 changes: 8 additions & 33 deletions screens/Trading/components/ConfirmMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ import {
} from "../../../components/Modal/button";
import { shrinkToken, expandToken } from "../../../store";
import { beautifyPrice } from "../../../utils/beautyNumber";
import { getAccountId } from "../../../redux/accountSelectors";
import { handleTransactionHash } from "../../../services/transaction";
import { showPositionFailure } from "../../../components/HashResultModal";
import { getBurrow } from "../../../utils";
import { getSymbolById } from "../../../transformers/nearSymbolTrans";
import { IConfirmMobileProps } from "../comInterface";
import { useRegisterTokenType } from "../../../hooks/useRegisterTokenType";
Expand All @@ -31,20 +29,6 @@ const ConfirmMobile: React.FC<IConfirmMobileProps | any> = ({
action,
confirmInfo,
}) => {
const [burrowData, setBurrowData] = useState<{
selector?: {
wallet: () => Promise<{ id: string }>;
};
} | null>(null);

useEffect(() => {
const initBurrow = async () => {
const data: any = await getBurrow();
setBurrowData(data);
};
initBurrow();
}, []);
const accountId = useAppSelector(getAccountId);
const theme = useTheme();
const [selectedCollateralType, setSelectedCollateralType] = useState(DEFAULT_POSITION);
const { ReduxcategoryAssets1 } = useAppSelector((state) => state.category);
Expand Down Expand Up @@ -193,26 +177,17 @@ const ConfirmMobile: React.FC<IConfirmMobileProps | any> = ({
: minAmountOutForPopUp / confirmInfo.tokenInAmount,
}),
);

const wallet = await burrowData?.selector?.wallet();
if (wallet?.id && ["my-near-wallet", "mintbase-wallet", "bitte-wallet"].includes(wallet.id)) {
await openPosition(openPositionParams);
return;
}

const res: any = await openPosition(openPositionParams);
if (!res || !Array.isArray(res)) {
throw new Error("Invalid response from openPosition");
if (res) {
const transactionHashes = res.map((item) => {
if (!item?.transaction?.hash) {
throw new Error("Invalid transaction hash");
}
return item.transaction.hash;
});
await handleTransactionHash(transactionHashes);
}
const transactionHashes = res.map((item) => {
if (!item?.transaction?.hash) {
throw new Error("Invalid transaction hash");
}
return item.transaction.hash;
});
await handleTransactionHash(transactionHashes);
} catch (error) {
console.error("Open position error:", error);
showPositionFailure({
title: "Transactions error",
errorMessage: error instanceof Error ? error.message : JSON.stringify(error),
Expand Down
48 changes: 24 additions & 24 deletions screens/Trading/components/RangeSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,18 @@ interface RangeSliderProps {
}

const RangeSlider: React.FC<RangeSliderProps> = ({ defaultValue, action, setRangeMount }) => {
const [value, setValue] = useState(defaultValue);
const [splitList, setSplitList] = useState<number[]>([]);
const [matchValue, setMatchValue] = useState(value);
const router = useRouter();
const dispatch = useAppDispatch();
const { filteredTokenTypeMap } = useRegisterTokenType();
const router = useRouter();
const { marginConfigTokens, marginConfigTokensMEME } = useMarginConfigToken();
const valueRef = useRef<HTMLInputElement>(null);
const { id }: any = router.query;
const isMainStream = filteredTokenTypeMap.mainStream.includes(id);

const generateArithmeticSequence = (value: number) => {
const increment = (value - 1) / 4;
const sequence: number[] = [];

for (let i = 0; i <= 4; i++) {
sequence.push(+(1 + i * increment).toFixed(2));
}

return sequence;
};
const { marginConfigTokens, marginConfigTokensMEME } = useMarginConfigToken();
const marginConfigTokensCombined = isMainStream ? marginConfigTokens : marginConfigTokensMEME;

const [value, setValue] = useState(defaultValue);
const [splitList, setSplitList] = useState<number[]>([]);
const [matchValue, setMatchValue] = useState(value);
const valueRef = useRef<HTMLInputElement>(null);
const [selectedItem, setSelectedItem] = useState(defaultValue);

useEffect(() => {
const newAllowedValues = generateArithmeticSequence(
marginConfigTokensCombined["max_leverage_rate"],
Expand All @@ -46,10 +33,14 @@ const RangeSlider: React.FC<RangeSliderProps> = ({ defaultValue, action, setRang
}, [marginConfigTokensCombined["max_leverage_rate"]]);
// init value
useEffect(() => {
if (splitList.length > 0 && !splitList.includes(value)) {
changeValue(splitList[0]);
if (splitList.length > 0) {
if (!splitList.includes(value)) {
changeValue(splitList[0]);
} else if (value !== defaultValue) {
changeValue(defaultValue);
}
}
}, [value, splitList]);
}, [value, splitList, defaultValue]);
useEffect(() => {
if (valueRef.current && splitList.length > 0) {
const nearestValue = splitList.reduce((prev, curr) => {
Expand All @@ -68,10 +59,19 @@ const RangeSlider: React.FC<RangeSliderProps> = ({ defaultValue, action, setRang
return Math.abs(curr - numValue) < Math.abs(prev - numValue) ? curr : prev;
});
setValue(nearestValue);
setSelectedItem(nearestValue);
setRangeMount(nearestValue);
dispatch(setReduxRangeMount(nearestValue));
}
const generateArithmeticSequence = (value: number) => {
const increment = (value - 1) / 4;
const sequence: number[] = [];

for (let i = 0; i <= 4; i++) {
sequence.push(+(1 + i * increment).toFixed(2));
}

return sequence;
};
const actionShowRedColor = action === "Long";
return (
<div className="mt-5 pb-5 border-b border-dark-700 -mx-4 px-4">
Expand Down Expand Up @@ -100,7 +100,7 @@ const RangeSlider: React.FC<RangeSliderProps> = ({ defaultValue, action, setRang
className={twMerge(
`flex items-center justify-center text-xs text-gray-400 w-11 py-0.5 border border-transparent hover:border-v3LiquidityRemoveBarColor
rounded-lg`,
p === selectedItem && "bg-black bg-opacity-20",
p === value && "bg-black bg-opacity-20",
)}
>
{p}X
Expand Down
1 change: 0 additions & 1 deletion screens/Trading/components/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ const TradingTable = ({
setStateSelectedTab("positions");
}
}, [storeSelectedTab]);
// TODOXX
useEffect(() => {
if (query?.transactionHashes && accountId) {
handleTransactionHash(query?.transactionHashes);
Expand Down
19 changes: 6 additions & 13 deletions screens/Trading/components/Table/PositionRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,15 @@ const PositionRow = ({
</div>
</div>
</td>
<td
title={entryPrice !== null && entryPrice !== undefined ? `$${entryPrice}` : ""}
className="cursor-default"
>
<td>
{entryPrice !== null && entryPrice !== undefined ? (
<span>${beautifyPrice(entryPrice)}</span>
) : (
<span className="text-gray-500">-</span>
)}
</td>
<td title={`$${indexPrice?.toString()}`} className="cursor-default">
${beautifyPrice(indexPrice)}
</td>
<td title={`$${LiqPrice?.toString()}`} className="cursor-default">
${beautifyPrice(LiqPrice)}
</td>
<td>${beautifyPrice(indexPrice)}</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 @@ -280,7 +273,7 @@ const PositionRow = ({
</div>
<div className="flex items-center justify-between text-sm mb-[18px]">
<p className="text-gray-300">Entry Price</p>
<p title={entryPrice !== null && entryPrice !== undefined ? `$${entryPrice}` : ""}>
<p>
{entryPrice !== null ? (
<span>${beautifyPrice(entryPrice)}</span>
) : (
Expand All @@ -290,11 +283,11 @@ const PositionRow = ({
</div>
<div className="flex items-center justify-between text-sm mb-[18px]">
<p className="text-gray-300">Index Price</p>
<p title={indexPrice?.toString()}>${beautifyPrice(indexPrice)}</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 title={LiqPrice?.toString()}>${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>
Expand Down
1 change: 0 additions & 1 deletion screens/Trading/components/TradingIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ export const MaxPositionIcon = (props: any) => {
viewBox="0 0 16 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className="mt-[2px]"
>
<path
d="M8 3.11531e-06C12.411 3.50093e-06 16 3.58847 16 8C16 12.411 12.411 16 8 16C3.58846 16 -3.50098e-06 12.4115 -3.11532e-06 8C-2.72965e-06 3.58846 3.58846 2.72965e-06 8 3.11531e-06ZM8 14.8521C11.7784 14.8521 14.8521 11.7784 14.8521 8C14.8521 4.22213 11.7784 1.14794 8 1.14794C4.22212 1.14794 1.14794 4.22155 1.14794 8C1.14794 11.7784 4.22212 14.8521 8 14.8521ZM8.86095 11.7308C8.86095 11.5025 8.77025 11.2835 8.60879 11.122C8.44733 10.9606 8.22834 10.8699 8 10.8699C7.77166 10.8699 7.55267 10.9606 7.39121 11.122C7.22975 11.2835 7.13904 11.5025 7.13904 11.7308C7.13904 11.9591 7.22975 12.1781 7.39121 12.3396C7.55267 12.5011 7.77166 12.5918 8 12.5918C8.22834 12.5918 8.44733 12.5011 8.60879 12.3396C8.77025 12.1781 8.86095 11.9591 8.86095 11.7308ZM8 3.40824C8.31683 3.40824 8.57397 3.6648 8.57397 3.98221L8.57397 9.14794C8.57397 9.46477 8.31683 9.72191 8 9.72191C7.68317 9.72191 7.42603 9.46477 7.42603 9.14794L7.42603 3.98221C7.42603 3.6648 7.68317 3.40824 8 3.40824Z"
Expand Down
Loading

0 comments on commit 04a4f44

Please sign in to comment.