Skip to content

Commit

Permalink
feat: excute open position
Browse files Browse the repository at this point in the history
  • Loading branch information
deep-path committed Nov 25, 2024
1 parent 428e258 commit 906b8ba
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
5 changes: 3 additions & 2 deletions data/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class DataSource {
}

// eslint-disable-next-line class-methods-use-this,default-param-last
async callAPI(endPoint, method = "GET", queryObject, requestBody, host, authentication) {
async callAPI(endPoint, method, queryObject, requestBody, host, authentication) {
method = method || "GET";
const url = URLForEndpoint(endPoint, queryObject, host);
const headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("pragma", "no-cache");
// headers.append("pragma", "no-cache");
headers.append("cache-control", "no-cache");
if (authentication) {
headers.append("Authentication", getAuthenticationHeaders(endPoint));
Expand Down
20 changes: 7 additions & 13 deletions screens/Trading/components/ConfirmMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ import {
} from "../../../components/Modal/button";
import { shrinkToken } from "../../../store";
import { beautifyPrice } from "../../../utils/beautyNumbet";
import DataSource from "../../../data/datasource";
import { getAccountId } from "../../../redux/accountSelectors";
import { useRouterQuery } from "../../../utils/txhashContract";
import { handleTransactionHash } from "../../../services/transaction";

const getTokenSymbolOnly = (assetId) => {
return assetId === "wNEAR" ? "NEAR" : assetId || "";
};

export const ModalContext = createContext(null) as any;
const ConfirmMobile = ({ open, onClose, action, confirmInfo }) => {
const { query } = useRouterQuery();
const accountId = useAppSelector(getAccountId);
const dispatch = useAppDispatch();
const theme = useTheme();
const [selectedCollateralType, setSelectedCollateralType] = useState(DEFAULT_POSITION);
Expand All @@ -43,18 +49,6 @@ const ConfirmMobile = ({ open, onClose, action, confirmInfo }) => {
);
const cateSymbol = getTokenSymbolOnly(ReduxcategoryAssets1.metadata.symbol);
const confirmOpenPosition = async () => {
// console.log(
// action == "Long"
// ? getAssetById(confirmInfo.longOutputName?.token_id)
// : getAssetById(confirmInfo.longInputName?.token_id),
// );

// return console.log(
// parseTokenValue(confirmInfo.estimateData.min_amount_out, decimalsP) >
// confirmInfo.tokenInAmount /
// (confirmInfo.indexPrice * (1 - marginConfigTokens.max_slippage_rate / 10000)),
// );

setIsDisabled(true);
if (action === "Long") {
try {
Expand All @@ -67,7 +61,7 @@ const ConfirmMobile = ({ open, onClose, action, confirmInfo }) => {
min_token_p_amount: confirmInfo.estimateData.min_amount_out,
swap_indication: confirmInfo.estimateData.swap_indication,
assets: confirmInfo.assets.data,
}).finally(() => {
}).finally(async () => {
setIsDisabled(false);
});
} catch (error) {
Expand Down
21 changes: 20 additions & 1 deletion screens/Trading/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
showPositionFailure,
showPositionClose,
} from "../../components/HashResultModal";
import { handleTransactionResults } from "../../services/transaction";
import { handleTransactionResults, handleTransactionHash } from "../../services/transaction";
import DataSource from "../../data/datasource";

init_env("dev");

Expand Down Expand Up @@ -143,6 +144,24 @@ const Trading = () => {

useEffect(() => {
handleTransactionResults(query?.transactionHashes, query?.errorMessage);
if (query?.transactionHashes) {
(async () => {
const txHash = await handleTransactionHash(query?.transactionHashes);
txHash
.filter((item) => item.hasStorageDeposit)
.forEach(async (item) => {
try {
await DataSource.shared.getMarginTradingPosition({
addr: accountId,
process_type: "open",
tx_hash: item.txHash,
});
} catch (error) {
console.error("Failed to get margin trading position:", error);
}
});
})();
}
}, [query?.transactionHashes, query?.errorMessage]);

//
Expand Down
28 changes: 28 additions & 0 deletions services/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,31 @@ export const handleTransactionResults = async (
});
}
};

export const handleTransactionHash = async (
transactionHashes: string | string[] | undefined,
): Promise<TransactionResult[]> => {
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 };
}),
);

return results;
} catch (error) {
console.error("Error processing transactions:", error);
return [];
}
}
return [];
};

0 comments on commit 906b8ba

Please sign in to comment.