Skip to content

Commit

Permalink
Merge pull request #211 from CarmineOptions/feature/rework-price-guard
Browse files Browse the repository at this point in the history
Feature/rework price guard
  • Loading branch information
DaveVodrazka authored Aug 15, 2024
2 parents 6d2ddfe + cd57b4b commit 93abb66
Show file tree
Hide file tree
Showing 16 changed files with 757 additions and 655 deletions.
2 changes: 2 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<link
href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700&display=swap"
rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Condensed:wght@400;500;600;700&display=swap"
rel="stylesheet">
<!--GTAG_PLACEHOLDER-->
</head>

Expand Down
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import StakingExplainedPage from "./pages/stakeInfo";
import LeaderboardPage from "./pages/leaderboard";
import StarknetRewards from "./pages/starknetRewards";
import BattlechartsPage from "./pages/battlecharts";
import PriceGuard from "./pages/priceGuard";
import PriceGuardPage from "./pages/priceGuard";

import "./style/base.css";

Expand Down Expand Up @@ -73,7 +73,7 @@ const App = () => {
<Route path="/leaderboard" element={<LeaderboardPage />} />
<Route path="/rewards" element={<StarknetRewards />} />
<Route path="/battlecharts" element={<BattlechartsPage />} />
<Route path="/priceguard" element={<PriceGuard />} />
<Route path="/priceprotect" element={<PriceGuardPage />} />
<Route path="*" element={<NotFound />} />
</Routes>
</Router>
Expand Down
93 changes: 90 additions & 3 deletions src/calls/tradeOpen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import { AccountInterface } from "starknet";
import { Option } from "../classes/Option";
import { debug } from "../utils/debugger";
import { getToApprove, shortInteger } from "../utils/computations";
import AmmAbi from "../abi/amm_abi.json";
import LpAbi from "../abi/lptoken_abi.json";
import { afterTransaction } from "../utils/blockchain";
import { invalidatePositions } from "../queries/client";
import { TransactionAction } from "../redux/reducers/transactions";
import { ToastType } from "../redux/reducers/ui";
import { math64ToInt } from "../utils/units";
import { math64toDecimal, math64ToInt } from "../utils/units";
import { apiUrl } from "../api";
import { isMainnet } from "../constants/amm";

Expand Down Expand Up @@ -126,3 +124,92 @@ export const approveAndTradeOpen = async (

return true;
};

export const approveAndTradeOpenNew = async (
account: AccountInterface,
option: Option,
size: number,
premiaMath64: bigint,
balance: bigint,
updateTradeState: (
state: "initial" | "processing" | "fail" | "success"
) => void,
isPriceGuard = false
) => {
if (size === 0) {
showToast("Cannot open position with size 0", ToastType.Warn);
return;
}
updateTradeState("processing");
const premiaNum = math64toDecimal(premiaMath64);
const premiaTokenCount = math64ToInt(premiaMath64, option.digits);
const toApprove = getToApprove(option, size, BigInt(premiaTokenCount));
const toApproveNumber = shortInteger(toApprove, option.digits);

if (balance < toApprove) {
const [has, needs] = [
shortInteger(balance.toString(10), option.digits),
toApproveNumber,
];
showToast(
`To open this position you need ${option.symbol}\u00A0${Number(
needs
).toFixed(4)}, but you only have ${option.symbol}\u00A0${has.toFixed(4)}`,
ToastType.Warn
);
updateTradeState("fail");
return;
}

const approve = option.underlying.approveCalldata(toApprove);
const tradeOpen = option.tradeOpenCalldata(size, premiaMath64);

option.sendBeginCheckoutEvent(size, premiaNum, isPriceGuard);
const res = await account.execute([approve, tradeOpen]).catch((e) => {
debug("Trade open rejected or failed", e.message);
});

if (res === undefined) {
updateTradeState("fail");
return;
}

option.sendPurchaseEvent(size, premiaNum, isPriceGuard);

if (isPriceGuard && isMainnet) {
const options = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
user_address: account.address,
calldata: tradeOpen.calldata,
}),
};

fetch(apiUrl("priceGuard-event"), options)
.then((response) => {
debug("PriceGuard event sent", response);
})
.catch((err) => {
debug("PriceGuard event failed", err);
console.error(err);
});
}

const hash = res.transaction_hash;
addTx(hash, option.optionId, TransactionAction.TradeOpen);
afterTransaction(
hash,
() => {
markTxAsDone(hash);
invalidatePositions();
updateTradeState("success");
showToast("Successfully opened position", ToastType.Success);
},
() => {
markTxAsFailed(hash);
updateTradeState("fail");
showToast("Failed to open position", ToastType.Error);
}
);
};
4 changes: 2 additions & 2 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const navLinks = [
link: "/staking",
},
{
title: "Price Guard",
link: "/priceguard",
title: "Price Protect",
link: "/priceprotect",
},
{
title: "Trade",
Expand Down
6 changes: 0 additions & 6 deletions src/components/MultiDialog/MultiDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { Close } from "@mui/icons-material";
import { ClosePosition } from "../ClosePosition/ClosePosition";
import { WalletInfo } from "../WalletInfo/WalletInfo";
import { ReactNode } from "react";
import { BuyPriceGuardModal } from "../PriceGuard/BuyPriceGuardModal";
import { TransferDialog } from "../Transfer";
import { BraavosDialog } from "./BraavosDialog";

Expand Down Expand Up @@ -171,11 +170,6 @@ export const MultiDialog = () => {
<ClosePosition />
</Border>
)}
{dialogContent === DialogContentElem.BuyPriceGuard && (
<Border>
<BuyPriceGuardModal />
</Border>
)}
{dialogContent === DialogContentElem.Account && (
<Border>
<WalletInfo />
Expand Down
111 changes: 0 additions & 111 deletions src/components/PriceGuard/ActivePriceGuard.tsx

This file was deleted.

Loading

0 comments on commit 93abb66

Please sign in to comment.