Skip to content

fix: limit slippage #426

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

Merged
merged 3 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion components/Pages/Trade/Liquidity/DepositForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const DepositForm = ({
name="token2"
control={control}
token={tokenB}
isDisabled={true}
isDisabled={isInputDisabled || !tokenB?.tokenSymbol}
mobile={mobile}
onChange={(value) => {
setReverse(true)
Expand Down
22 changes: 22 additions & 0 deletions components/Pages/Trade/Liquidity/hooks/useDepositTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import useDebounceValue from 'hooks/useDebounceValue'
import { executeAddLiquidity } from 'services/liquidity/index'
import { TxStep } from 'types/common'

import { getPoolInfo } from '../../../../../services/swap'

type Params = {
enabled: boolean
swapAddress: string
Expand Down Expand Up @@ -49,9 +51,29 @@ export const useTransaction = ({
const [buttonLabel, setButtonLabel] = useState<unknown | null>(null)
const queryClient = useQueryClient()

const confirmRatio = async (debouncedMsgFunds:Array<any>) => {
const address = msgs.provide_liquidity ? swapAddress : msgs.deposit.pair_address;
const poolInfo = await getPoolInfo(address, signingClient);
const poolAssets = poolInfo.assets;

const [poolAsset1, poolAsset2] = poolAssets;
const poolRatio = poolAsset1.amount / poolAsset2.amount;

const transactionRatio =
Number(debouncedMsgFunds.find((elem: any) => elem.denom === poolAsset1.info.native_token.denom).amount) /
Number(debouncedMsgFunds.find((elem: any) => elem.denom === poolAsset2.info.native_token.denom).amount);

const slippage = (poolRatio / 100) * 2;

if (!(transactionRatio >= poolRatio - slippage && transactionRatio <= poolRatio + slippage)) {
throw new Error('LP Spread Error');
}
}

const { data: fee } = useQuery<unknown, unknown, any | null>(
['fee', tokenAAmount, tokenBAmount, debouncedMsgs, error],
async () => {
await confirmRatio(debouncedMsgs[0].value.funds)
setError(null)
setTxStep(TxStep.Estimating)
if (!signingClient) {
Expand Down