-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from Levana-Protocol/perp-4191/buy-liquidity
PERP-4191 | Implement liquidity purchase
- Loading branch information
Showing
7 changed files
with
308 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { useActiveWalletType, useCosmWasmSigningClient } from "graz" | ||
import { useMutation, useQueryClient } from "@tanstack/react-query" | ||
import type { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate" | ||
|
||
import { useCurrentAccount } from "@config/chain" | ||
import { useNotifications } from "@config/notifications" | ||
import { querierAwaitCacheAnd, querierBroadcastAndWait } from "@api/querier" | ||
import { MARKET_KEYS, type MarketId } from "@api/queries/Market" | ||
import { POSITIONS_KEYS } from "@api/queries/Positions" | ||
import { BALANCES_KEYS } from "@api/queries/Balances" | ||
import { trackProvideLiquidity } from "@utils/analytics" | ||
import type { Coins } from "@utils/coins" | ||
import { AppError, errorsMiddleware } from "@utils/errors" | ||
|
||
interface ProvideLiquidityRequest { | ||
provide: { | ||
id: number | ||
} | ||
} | ||
|
||
interface ProvideLiquidityArgs { | ||
coinsAmount: Coins | ||
} | ||
|
||
const putProvideLiquidity = ( | ||
address: string, | ||
signer: SigningCosmWasmClient, | ||
marketId: MarketId, | ||
args: ProvideLiquidityArgs, | ||
) => { | ||
const provideMsg: ProvideLiquidityRequest = { | ||
provide: { | ||
id: Number(marketId), | ||
}, | ||
} | ||
|
||
return querierBroadcastAndWait(address, signer, [ | ||
{ | ||
payload: provideMsg, | ||
funds: [ | ||
{ | ||
denom: args.coinsAmount.denom, | ||
amount: args.coinsAmount.units.toFixed(0), | ||
}, | ||
], | ||
}, | ||
]) | ||
} | ||
|
||
const PROVIDE_LIQUIDITY_KEYS = { | ||
all: ["provide_liquidity"] as const, | ||
address: (address: string) => | ||
[...PROVIDE_LIQUIDITY_KEYS.all, address] as const, | ||
market: (address: string, marketId: MarketId) => | ||
[...PROVIDE_LIQUIDITY_KEYS.address(address), marketId] as const, | ||
} | ||
|
||
const useProvideLiquidity = (marketId: MarketId) => { | ||
const account = useCurrentAccount() | ||
const walletName = useActiveWalletType().walletType | ||
const signer = useCosmWasmSigningClient() | ||
const queryClient = useQueryClient() | ||
const notifications = useNotifications() | ||
|
||
const mutation = useMutation({ | ||
mutationKey: PROVIDE_LIQUIDITY_KEYS.market(account.bech32Address, marketId), | ||
mutationFn: (args: ProvideLiquidityArgs) => { | ||
if (signer.data) { | ||
return errorsMiddleware( | ||
"provide", | ||
putProvideLiquidity( | ||
account.bech32Address, | ||
signer.data, | ||
marketId, | ||
args, | ||
), | ||
) | ||
} else { | ||
return Promise.reject() | ||
} | ||
}, | ||
onSuccess: (_, args) => { | ||
notifications.notifySuccess( | ||
`Successfully provided ${args.coinsAmount.toFormat(true)} of liquidity.`, | ||
) | ||
|
||
trackProvideLiquidity({ | ||
marketId: marketId, | ||
coins: args.coinsAmount, | ||
walletName: walletName, | ||
}) | ||
|
||
return querierAwaitCacheAnd( | ||
() => | ||
queryClient.invalidateQueries({ | ||
queryKey: MARKET_KEYS.market(marketId), | ||
}), | ||
() => | ||
queryClient.invalidateQueries({ | ||
queryKey: POSITIONS_KEYS.market(account.bech32Address, marketId), | ||
}), | ||
() => | ||
queryClient.invalidateQueries({ | ||
queryKey: BALANCES_KEYS.address(account.bech32Address), | ||
}), | ||
) | ||
}, | ||
onError: (err, args) => { | ||
notifications.notifyError( | ||
AppError.withCause( | ||
`Failed to provide ${args.coinsAmount.toFormat(true)} of liquidity.`, | ||
err, | ||
), | ||
) | ||
|
||
trackProvideLiquidity( | ||
{ | ||
marketId: marketId, | ||
coins: args.coinsAmount, | ||
walletName: walletName, | ||
}, | ||
err, | ||
) | ||
}, | ||
}) | ||
|
||
return mutation | ||
} | ||
|
||
export { useProvideLiquidity } |
58 changes: 58 additions & 0 deletions
58
frontend/src/features/MarketDetail/components/MarketBetting/ProvideLiquidity/form.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useForm } from "react-hook-form" | ||
import { useQuery } from "@tanstack/react-query" | ||
|
||
import type { Market } from "@api/queries/Market" | ||
import { coinPricesQuery } from "@api/queries/Prices" | ||
import { useProvideLiquidity } from "@api/mutations/ProvideLiquidity" | ||
import { Coins, USD } from "@utils/coins" | ||
|
||
interface ProvideLiquidityFormValues { | ||
liquidityAmount: { | ||
value: string | ||
toggled: boolean | ||
} | ||
} | ||
|
||
const useProvideLiquidityForm = (market: Market) => { | ||
const form = useForm<ProvideLiquidityFormValues>({ | ||
defaultValues: { | ||
liquidityAmount: { | ||
value: "", | ||
toggled: false, | ||
}, | ||
}, | ||
}) | ||
|
||
const denom = market.denom | ||
const provideLiquidity = useProvideLiquidity(market.id) | ||
const prices = useQuery(coinPricesQuery) | ||
|
||
const onSubmit = (formValues: ProvideLiquidityFormValues) => { | ||
const isToggled = formValues.liquidityAmount.toggled | ||
const liquidityAmount = formValues.liquidityAmount.value | ||
const price = prices.data?.get(denom) | ||
|
||
if (liquidityAmount && price) { | ||
const coinsAmount = isToggled | ||
? new USD(liquidityAmount).toCoins(denom, price) | ||
: Coins.fromValue(denom, liquidityAmount) | ||
|
||
return provideLiquidity | ||
.mutateAsync({ coinsAmount: coinsAmount }) | ||
.then(() => { | ||
form.reset() | ||
}) | ||
} else { | ||
return Promise.reject() | ||
} | ||
} | ||
|
||
const canSubmit = | ||
form.formState.isValid && | ||
!form.formState.isSubmitting && | ||
!!prices.data?.has(market.denom) | ||
|
||
return { form, canSubmit, onSubmit } | ||
} | ||
|
||
export { useProvideLiquidityForm } |
51 changes: 51 additions & 0 deletions
51
frontend/src/features/MarketDetail/components/MarketBetting/ProvideLiquidity/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Button, Stack } from "@mui/joy" | ||
import { FormProvider } from "react-hook-form" | ||
import { useQuery } from "@tanstack/react-query" | ||
|
||
import { useCurrentAccount } from "@config/chain" | ||
import type { Market } from "@api/queries/Market" | ||
import { balancesQuery } from "@api/queries/Balances" | ||
import { coinPricesQuery } from "@api/queries/Prices" | ||
import { useProvideLiquidityForm } from "./form" | ||
import { CoinsAmountField } from "../CoinsAmountField" | ||
|
||
const MarketProvideLiquidityForm = (props: { market: Market }) => { | ||
const { market } = props | ||
const account = useCurrentAccount() | ||
const balances = useQuery(balancesQuery(account.bech32Address)) | ||
const price = useQuery(coinPricesQuery).data?.get(market.denom) | ||
|
||
const { form, canSubmit, onSubmit } = useProvideLiquidityForm(market) | ||
|
||
return ( | ||
<FormProvider {...form}> | ||
<Stack | ||
component="form" | ||
onSubmit={form.handleSubmit(onSubmit)} | ||
direction="column" | ||
rowGap={1.5} | ||
> | ||
<CoinsAmountField | ||
name="liquidityAmount" | ||
denom={market.denom} | ||
balance={balances.data?.get(market.denom)} | ||
price={price} | ||
/> | ||
|
||
<Button | ||
aria-label="Provide liquidity" | ||
type="submit" | ||
size="lg" | ||
disabled={!canSubmit} | ||
fullWidth | ||
> | ||
{form.formState.isSubmitting | ||
? "Providing liquidity..." | ||
: "Provide liquidity"} | ||
</Button> | ||
</Stack> | ||
</FormProvider> | ||
) | ||
} | ||
|
||
export { MarketProvideLiquidityForm } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters