Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve partial cancel calculation #15

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Maybe from "easy-maybe/lib";
import { zip } from "ramda";
import { isFloat } from "../../utils/index";
import { withdrawAmount } from "../../utils/twamm-client";

const { withDefault, of } = Maybe;

Expand Down Expand Up @@ -48,10 +49,17 @@ export const format = {
},

userAveragePrice(data: PoolDetails) {
const value = (({ lpAmount, side, withdrawData }) => {
const value = (({ lpAmount, side, withdraw }) => {
const baseTokenIndex = side.sell ? 0 : 1;
const supplTokenIndex = side.sell ? 1 : 0;

const withdrawData = withdrawAmount(
withdraw.orderBalance.lpBalance,
withdraw.tradeSide,
withdraw.orderBalance,
withdraw.tokenPair
);

if (!withdrawData[supplTokenIndex]) return undefined;

const avg =
Expand Down
2 changes: 1 addition & 1 deletion app/src/components/molecules/cancel-order-amount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default memo(({ percentage, onChange, onToggleDetails }: Props) => {
);

const onPercentageChange = useCallback(
(e: Event, value: number | number[]) => {
(_: Event, value: number | number[]) => {
if (Array.isArray(value)) {
onChange(value[0]);
} else {
Expand Down
16 changes: 13 additions & 3 deletions app/src/components/molecules/cancel-order-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import CancelOrderLiquidity from "./cancel-order-liquidity";
import Loading from "../atoms/loading";
import usePrice from "../../hooks/use-price";
import { refreshEach } from "../../swr-options";
import { withdrawAmount as calcWithdraw } from "../../utils/twamm-client";

export interface Props {
data: Voidable<JupTokenData[]>;
details: Voidable<PoolDetails>;
onToggle: () => void;
percentage: number;
}

export default ({ data, details, onToggle }: Props) => {
export default ({ data, details, onToggle, percentage }: Props) => {
const d = Maybe.of(data);

const tokens = Maybe.withDefault(undefined, d);
Expand All @@ -27,10 +29,18 @@ export default ({ data, details, onToggle }: Props) => {
);
const withdrawAmount = Maybe.andMap(([td, det]) => {
const [a, b] = td;
const { withdraw } = det;

const [wda, wdb] = calcWithdraw(
(withdraw.orderBalance.lpBalance * percentage) / 100,
withdraw.tradeSide,
withdraw.orderBalance,
withdraw.tokenPair
);

const withdrawPair = [
det.withdrawData[0] * 10 ** (a.decimals * -1),
det.withdrawData[1] * 10 ** (b.decimals * -1),
wda * 10 ** (a.decimals * -1),
wdb * 10 ** (b.decimals * -1),
];

return withdrawPair;
Expand Down
1 change: 1 addition & 0 deletions app/src/components/molecules/cancel-order-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default ({ data, detailsData, onApprove }: Props) => {
data={tokens.data}
details={details.data}
onToggle={onToggleDetails}
percentage={percentage}
/>
)}
<Box p={2}>
Expand Down
18 changes: 3 additions & 15 deletions app/src/hooks/use-pool-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import useSWR from "swr";

import useJupTokensByMint from "./use-jup-tokens-by-mint";
import usePoolWithPair from "./use-pool-with-pair";
import {
address as addr,
lpAmount,
withdrawAmount,
} from "../utils/twamm-client";
import { address as addr } from "../utils/twamm-client";

const mintsKey = (pair?: TokenPairAccountData) =>
pair
Expand Down Expand Up @@ -48,14 +44,6 @@ export default (

const lastChanged = lastBalanceChangeTime.toNumber();

const withdrawData = withdrawAmount(
Number(order.lpBalance),
tradeSide,
order,
pair
);
const lpAmountData = lpAmount(tradeSide, order);

const next = {
aAddress: configA.mint,
bAddress: configB.mint,
Expand All @@ -66,7 +54,7 @@ export default (
lastBalanceChangeTime: !lastChanged
? undefined
: new Date(lastChanged * 1e3),
lpAmount: lpAmountData,
lpAmount: Number(order.lpBalance),
lpSupply: [
Number(buySide.sourceBalance) / 10 ** configB.decimals +
Number(sellSide.targetBalance) / 10 ** configA.decimals,
Expand All @@ -88,7 +76,7 @@ export default (
],
side,
volume: statsA.orderVolumeUsd + statsB.orderVolumeUsd,
withdrawData,
withdraw: { tradeSide, orderBalance: order, tokenPair: pair },
};

return next;
Expand Down
12 changes: 11 additions & 1 deletion app/src/types/program.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ declare type OrderData = {
unsettledBalance: BN;
};

declare type OrderBalanceData = {
side: OrderTypeStruct;
lpBalance: BN;
tokenDebt: BN;
};

type PoolTradeSideData = {
fillsVolume: BN;
lastBalanceChangeTime: BN;
Expand Down Expand Up @@ -117,7 +123,11 @@ declare type PoolDetails = {
poolAddress: PublicKey;
prices: string[];
volume: number;
withdrawData: number[];
withdraw: {
tradeSide: PoolTradeSideData;
orderBalance: OrderBalanceData;
tokenPair: TokenPairProgramData;
};
};

declare type DetailsData = {
Expand Down
7 changes: 4 additions & 3 deletions app/src/utils/twamm-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ export const lpAmount = (
};

export const withdrawAmount = (
lpBalance: number,
lpBalance: number | BN,
poolSide: PoolTradeSideData,
order: { side: OrderTypeStruct; lpBalance: BN; tokenDebt: BN },
tokenPair: TokenPairProgramData
) => {
const withdrawAmountSource =
(lpBalance * Number(poolSide.sourceBalance)) / Number(poolSide.lpSupply);
(Number(lpBalance) * Number(poolSide.sourceBalance)) /
Number(poolSide.lpSupply);

let withdrawAmountTarget =
(lpBalance *
(Number(lpBalance) *
(Number(poolSide.targetBalance) + Number(poolSide.tokenDebtTotal))) /
Number(poolSide.lpSupply);

Expand Down