Skip to content

Commit

Permalink
fix: pail price select
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveVodrazka committed Nov 19, 2024
1 parent 625cbf1 commit 5d660f5
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 17 deletions.
7 changes: 7 additions & 0 deletions src/components/Navigation/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ export const Navigation = () => {
badge={<NewBadge />}
isActive={current === "battlecharts"}
/>
<Nav
title="PAIL"
path="pail"
icon={ShieldPlus}
badge={<NewBadge />}
isActive={current === "pail"}
/>
</ul>
</nav>
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/components/PAIL/Buy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { Pair } from "../../classes/Pair";
import { PAIL_ADDRESS } from "../../constants/amm";
import { LoadingAnimation } from "../Loading/Loading";
import { math64toDecimal } from "../../utils/units";
import { decimalToMath64, math64toDecimal } from "../../utils/units";
import { longInteger } from "../../utils/computations";
import toast from "react-hot-toast";
import { TokenBadge } from "../TokenBadge";
Expand All @@ -15,16 +15,17 @@ type Props = {
tokenPair: Pair;
expiry: number;
notional: bigint;
priceAt: number;
};

export const Buy = ({ tokenPair, expiry, notional }: Props) => {
export const Buy = ({ tokenPair, expiry, notional, priceAt }: Props) => {
const { provider } = useProvider();
const args = [
notional.toString(10),
tokenPair.quoteToken.address,
tokenPair.baseToken.address,
expiry.toString(10),
0,
decimalToMath64(priceAt),
0,
];
const { isLoading, isError, error, data } = useReadContract({
Expand Down Expand Up @@ -124,7 +125,6 @@ export const Buy = ({ tokenPair, expiry, notional }: Props) => {
],
};

console.log(call);
await sendAsync([approveQuote, approveBase, call])
.then(({ transaction_hash }) => {
toast.promise(provider.waitForTransaction(transaction_hash), {
Expand Down
5 changes: 3 additions & 2 deletions src/components/PAIL/Owned.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const Owned = () => {
if (isLoading) {
return <LoadingAnimation />;
}

if (isError || !data) {
console.error(error);
return <p>Something went wrong</p>;
Expand Down Expand Up @@ -48,7 +49,7 @@ export const Owned = () => {
return (
<div style={{ display: "flex", flexFlow: "column" }}>
<h3>Live</h3>
{data.live.length === 0 ? (
{data?.live?.length === 0 ? (
<p>No live PAIL</p>
) : (
data.live.map((id, i) => (
Expand All @@ -67,7 +68,7 @@ export const Owned = () => {
))
)}
<h3>Expired</h3>
{data.expired.length === 0 ? (
{data?.expired?.length === 0 ? (
<p>No expired PAIL</p>
) : (
data.expired.map((id, i) => (
Expand Down
83 changes: 73 additions & 10 deletions src/components/PAIL/Pail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export const Pail = () => {
const [amountText, setAmountText] = useState<string>("1");
const tokenPair = Pair.pairByKey(pair);

const [priceAt, setPriceAt] = useState<number>(0);
const [priceAtCurrent, setPriceAtCurrent] = useState<boolean>(true);

const handlePairChange = (event: SelectChangeEvent) => {
setPair(event.target.value as PairKey);
};
Expand All @@ -39,6 +42,13 @@ export const Pail = () => {
}
);

const handlePriceAtChange = (e: any) => {
try {
const parsed = parseFloat(e?.target?.value);
setPriceAt(parsed);
} catch {}
};

if (isLoading) {
return <LoadingAnimation />;
}
Expand Down Expand Up @@ -75,13 +85,16 @@ export const Pail = () => {
const minPut = puts.at(0);

const priceRange =
!!maxCall && !!minPut ? minPut.strike + " - " + maxCall.strike : undefined;
!!maxCall && !!minPut ? [minPut.strike, maxCall.strike] : undefined;

const formatTimestamp = (timestamp: number) => {
const date = new Date(timestamp * 1000);
return date.toLocaleDateString("en-US", { day: "numeric", month: "short" });
};

const isPriceWithinRange =
priceRange && priceAt >= priceRange[0] && priceAt <= priceRange[1];

return (
<div className={styles.container}>
<div>
Expand Down Expand Up @@ -131,7 +144,7 @@ export const Pail = () => {
</div>
<div>
<div>
<span>size</span>
<h3>Size</h3>
</div>
<div>
<input
Expand All @@ -142,14 +155,64 @@ export const Pail = () => {
/>
</div>
</div>
{!!priceRange && <h4>Price range: {priceRange}</h4>}
{!!tokenPair && !!maturity && (
<Buy
tokenPair={Pair.pairByKey(pair)}
expiry={maturity}
notional={longInteger(amount, tokenPair.baseToken.decimals)}
/>
)}
<div>
<div
style={{
display: "flex",
flexFlow: "column",
gap: "5px",
}}
>
<h3>Price at</h3>
<p>Price at which you want to protect against impermanent loss.</p>
<p>
If you check current, price at the time of execution will be used.
</p>
{!!priceRange && (
<h4>
Price range: {priceRange[0]} - {priceRange[1]}
</h4>
)}
</div>
<div
style={{
display: "flex",
alignItems: "center",
}}
>
<input
type="checkbox"
checked={priceAtCurrent}
onChange={() => setPriceAtCurrent((prev) => !prev)}
/>
<input
onChange={handlePriceAtChange}
type="number"
placeholder="price at"
value={priceAt}
style={{
border:
priceRange &&
(priceAt < priceRange[0] || priceAt > priceRange[1])
? "2px solid red"
: "",
}}
/>
</div>
</div>

{!!tokenPair &&
!!maturity &&
(priceAtCurrent || isPriceWithinRange ? (
<Buy
tokenPair={Pair.pairByKey(pair)}
expiry={maturity}
notional={longInteger(amount, tokenPair.baseToken.decimals)}
priceAt={priceAtCurrent ? 0 : priceAt}
/>
) : (
<p>Either check current price or use price withing range</p>
))}
<div className={"divider"} />
<Owned />
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/usePailTokenIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const usePailTokenIds = () => {
const { address } = useAccount();

return useQuery({
queryKey: [QueryKeys.position, address],
queryKey: [QueryKeys.pail, address],
queryFn: async () => getUserPailTokens(address!),
enabled: !!address,
});
Expand Down
1 change: 1 addition & 0 deletions src/queries/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export enum QueryKeys {
userDomain = "USER_DOMAIN",
poolData = "POOL_DATA",
apy = "APY",
pail = "PAIL",
}

0 comments on commit 5d660f5

Please sign in to comment.