-
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 #15 from Levana-Protocol/perp-4037/better-ux
PERP-4037 | Better UX
- Loading branch information
Showing
38 changed files
with
1,331 additions
and
181 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
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
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,83 @@ | ||
import { useCosmWasmSigningClient } from 'graz' | ||
import BigNumber from 'bignumber.js' | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate' | ||
|
||
import { useCurrentAccount } from '@config/chain' | ||
import { useNotifications } from '@config/notifications' | ||
import { querierAwaitCacheAnd, querierBroadcastAndWait } from '@api/querier' | ||
import { MARKET_KEYS, MarketId, OutcomeId } from '@api/queries/Market' | ||
import { POSITIONS_KEYS } from '@api/queries/Positions' | ||
import { BALANCE_KEYS } from '@api/queries/NtrnBalance' | ||
import { AppError, errorsMiddleware } from '@utils/errors' | ||
|
||
interface CancelBetRequest { | ||
withdraw: { | ||
id: number, | ||
outcome: number, | ||
tokens: string, | ||
}, | ||
} | ||
|
||
interface CancelBetArgs { | ||
outcomeId: OutcomeId, | ||
tokensAmount: BigNumber, | ||
} | ||
|
||
const putCancelBet = (address: string, signer: SigningCosmWasmClient, marketId: MarketId, args: CancelBetArgs) => { | ||
const msg: CancelBetRequest = { | ||
withdraw: { | ||
id: Number(marketId), | ||
outcome: Number(args.outcomeId), | ||
tokens: args.tokensAmount.toFixed(), | ||
}, | ||
} | ||
|
||
return querierBroadcastAndWait( | ||
address, | ||
signer, | ||
{ payload: msg }, | ||
) | ||
} | ||
|
||
const CANCEL_BET_KEYS = { | ||
all: ["cancel_bet"] as const, | ||
address: (address: string) => [...CANCEL_BET_KEYS.all, address] as const, | ||
market: (address: string, marketId: MarketId) => [...CANCEL_BET_KEYS.address(address), marketId] as const, | ||
} | ||
|
||
const useCancelBet = (marketId: MarketId) => { | ||
const account = useCurrentAccount() | ||
const signer = useCosmWasmSigningClient() | ||
const queryClient = useQueryClient() | ||
const notifications = useNotifications() | ||
|
||
const mutation = useMutation({ | ||
mutationKey: CANCEL_BET_KEYS.market(account.bech32Address, marketId), | ||
mutationFn: (args: CancelBetArgs) => { | ||
if (signer.data) { | ||
return errorsMiddleware("sell", putCancelBet(account.bech32Address, signer.data, marketId, args)) | ||
} else { | ||
return Promise.reject() | ||
} | ||
}, | ||
onSuccess: (_, args) => { | ||
notifications.notifySuccess(`Successfully cancelled bet of ${args.tokensAmount.toFixed(3)} tokens.`) | ||
|
||
return querierAwaitCacheAnd( | ||
() => queryClient.invalidateQueries({ queryKey: MARKET_KEYS.market(marketId)}), | ||
() => queryClient.invalidateQueries({ queryKey: POSITIONS_KEYS.market(account.bech32Address, marketId)}), | ||
() => queryClient.invalidateQueries({ queryKey: BALANCE_KEYS.address(account.bech32Address)}), | ||
) | ||
}, | ||
onError: (err, args) => { | ||
notifications.notifyError( | ||
AppError.withCause(`Failed to cancel bet of ${args.tokensAmount.toFixed(3)} tokens.`, err) | ||
) | ||
}, | ||
}) | ||
|
||
return mutation | ||
} | ||
|
||
export { useCancelBet } |
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,72 @@ | ||
import { useCosmWasmSigningClient } from 'graz' | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate' | ||
|
||
import { useCurrentAccount } from '@config/chain' | ||
import { useNotifications } from '@config/notifications' | ||
import { querierAwaitCacheAnd, querierBroadcastAndWait } from '@api/querier' | ||
import { MarketId } from '@api/queries/Market' | ||
import { POSITIONS_KEYS } from '@api/queries/Positions' | ||
import { AppError, errorsMiddleware } from '@utils/errors' | ||
import { BALANCE_KEYS } from '@api/queries/NtrnBalance' | ||
|
||
interface ClaimEarningsRequest { | ||
collect: { | ||
id: number, | ||
}, | ||
} | ||
|
||
const putClaimEarnings = (address: string, signer: SigningCosmWasmClient, marketId: MarketId) => { | ||
const msg: ClaimEarningsRequest = { | ||
collect: { | ||
id: Number(marketId), | ||
}, | ||
} | ||
|
||
return querierBroadcastAndWait( | ||
address, | ||
signer, | ||
{ payload: msg }, | ||
) | ||
} | ||
|
||
const CLAIM_EARNINGS_KEYS = { | ||
all: ["claim_earnings"] as const, | ||
address: (address: string) => [...CLAIM_EARNINGS_KEYS.all, address] as const, | ||
market: (address: string, marketId: MarketId) => [...CLAIM_EARNINGS_KEYS.address(address), marketId] as const, | ||
} | ||
|
||
const useClaimEarnings = (marketId: MarketId) => { | ||
const account = useCurrentAccount() | ||
const signer = useCosmWasmSigningClient() | ||
const queryClient = useQueryClient() | ||
const notifications = useNotifications() | ||
|
||
const mutation = useMutation({ | ||
mutationKey: CLAIM_EARNINGS_KEYS.market(account.bech32Address, marketId), | ||
mutationFn: () => { | ||
if (signer.data) { | ||
return errorsMiddleware("claim", putClaimEarnings(account.bech32Address, signer.data, marketId)) | ||
} else { | ||
return Promise.reject() | ||
} | ||
}, | ||
onSuccess: () => { | ||
notifications.notifySuccess("Successfully claimed earnings.") | ||
|
||
return querierAwaitCacheAnd( | ||
() => queryClient.invalidateQueries({ queryKey: POSITIONS_KEYS.market(account.bech32Address, marketId)}), | ||
() => queryClient.invalidateQueries({ queryKey: BALANCE_KEYS.address(account.bech32Address)}), | ||
) | ||
}, | ||
onError: (err) => { | ||
notifications.notifyError( | ||
AppError.withCause("Failed to claim earnings.", err) | ||
) | ||
}, | ||
}) | ||
|
||
return mutation | ||
} | ||
|
||
export { useClaimEarnings } |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.