Skip to content

Commit

Permalink
added withdrawal and liq check
Browse files Browse the repository at this point in the history
  • Loading branch information
worrex committed Dec 20, 2024
1 parent 732e1f3 commit b70c0a4
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 22 deletions.
42 changes: 20 additions & 22 deletions components/Pages/Trade/Migrate/Migrate.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { useEffect, useMemo, useState } from 'react'

import {
VStack,
Text,
useMediaQuery,
Heading,
Box,
Divider,
} from '@chakra-ui/react'
import { Button } from '@chakra-ui/react' // Ensure you import Button if not already
import { Box, Button, Divider, Heading, Text, useMediaQuery, VStack } from '@chakra-ui/react' // Ensure you import Button if not already
import { useChain } from '@cosmos-kit/react-lite'
import DepositForm from 'components/Pages/Trade/Liquidity/DepositForm'
import useProvideLP from 'components/Pages/Trade/Liquidity/hooks/useProvideLP'
import { useFetchStaked } from 'components/Pages/Trade/Migrate/hooks/useFetchStaked'
import useMigrateTx, { MigrateAction } from 'components/Pages/Trade/Migrate/hooks/useMigrateTx'
import { usePoolsListQuery } from 'components/Pages/Trade/Pools/hooks/usePoolsListQuery'
import {
PoolEntityTypeWithLiquidity,
Expand All @@ -28,20 +22,17 @@ import { getDecimals } from 'util/conversion/index'

const Migrate = () => {
const { walletChainName } = useRecoilValue(chainState)
const { isWalletConnected, address } = useChain(walletChainName)
const { isWalletConnected, address, openView } = useChain(walletChainName)
const [isMobile] = useMediaQuery('(max-width: 720px)')

// Mock states and values for demonstration
const [oldPoolLiquidity, setOldPoolLiquidity] = useState<number | null>(500) // Hardcoded for demonstration
const [hasWithdrawn, setHasWithdrawn] = useState(false)
const [hasDeposited, setHasDeposited] = useState(false)
const data = useFetchStaked(address)
const { submit } = useMigrateTx()
const isAmpLp = useMemo(() => data?.denom.includes('amplp'), [data])
const amount = useMemo(() => Number(data?.amount ?? 0) * (10 ** -6), [data])

// States required for DepositForm:
const [reverse, setReverse] = useState(false)
const clearForm = () => {}
const chainId = 'phoenix-1'
const openView = () => {}
const incentivesEnabled = true

const { data: poolData } = usePoolsListQuery()
const { cosmWasmClient } = useClients(walletChainName)
Expand Down Expand Up @@ -144,9 +135,13 @@ const Migrate = () => {
{/* Step 1 */}
<VStack align="flex-start" width="full">
<Heading fontSize="lg" color="white">Step 1: Check Old Pool Liquidity</Heading>
{oldPoolLiquidity !== null && oldPoolLiquidity > 0 ? (
<Text color="gray.200">You have {oldPoolLiquidity} LP tokens in the old USDC/USDT XYK pool.</Text>
) : (
{ isAmpLp && amount > 0 && (
<Text color="gray.200">You have {amount.toFixed(6)} ampLP tokens in the old USDC/USDT XYK pool.</Text>
)}
{!isAmpLp && amount > 0 && (
<Text color="gray.200">You have {amount.toFixed(6)} LP tokens in the old USDC/USDT XYK pool.</Text>
)}
{Number(data?.amount ?? 0) === 0 && (
<Text color="gray.200">You have no liquidity in the old pool.</Text>
)}
</VStack>
Expand All @@ -158,8 +153,11 @@ const Migrate = () => {
<Heading fontSize="lg" color="white">Step 2: Withdraw Liquidity</Heading>
<Text color="gray.200">Withdraw all liquidity from the old XYK pool before migrating.</Text>
<PrimaryButton
label="Withdraw"
onClick={() => setHasWithdrawn(true)}
isDisabled={!(isWalletConnected && (amount > 0)) }
label={isWalletConnected && (amount > 0) ? 'Withdraw' : 'Nothing to withdraw'}
onClick={() => submit(
MigrateAction.Withdraw, amount, data.denom,
)}
/>
</VStack>

Expand Down
61 changes: 61 additions & 0 deletions components/Pages/Trade/Migrate/hooks/deposit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate/build/signingcosmwasmclient'
import { ADV_MEMO } from 'constants/index'
import { createGasFee } from 'services/treasuryService'
import { createExecuteMessage } from 'util/messages/createExecuteMessage'

export const deposit: any = async (
signingClient: SigningCosmWasmClient,
address: string,
denom: string,
amount: number,
) => {
const ampLpPostMessage = {
liquid_stake: {
compounder: 'terra1zly98gvcec54m3caxlqexce7rus6rzgplz7eketsdz7nh750h2rqvu8uzx',
receiver: null,
gauge: 'bluechip',
},
}
const lpPostMessage = {
stake: {
asset_staking: 'terra14mmvqn0kthw6sre75vku263lafn5655mkjdejqjedjga4cw0qx2qlf4arv',
receiver: null,
},
}

const handleMsg1 = {
create_lp: {
min_received: '6307',
assets: [
{
native: 'ibc/2C962DAB9F57FE0921435426AE75196009FAA1981BF86991203C8411F8980FDB',
},
{
native: 'ibc/9B19062D46CAB50361CE9B0A3E6D0A7A53AC9E7CB361F32A73CC733144A9A9E5',
},
],
stage: {
white_whale: {
pair: 'terra17vas9rhxhc6j6f5wrup9cqapxn74jvpft069py7l7l9kr7wx3tnsxrazux',
},
},
post_action: {
stake: {
asset_staking: 'terra14mmvqn0kthw6sre75vku263lafn5655mkjdejqjedjga4cw0qx2qlf4arv',
receiver: null,
},
},
},
}

const execMsg = createExecuteMessage({ senderAddress: address,
contractAddress: 'terra1zly98gvcec54m3caxlqexce7rus6rzgplz7eketsdz7nh750h2rqvu8uzx',
message: handleMsg1,
funds: [{ amount: Math.ceil(amount).toString(),
denom }] })
return await signingClient.signAndBroadcast(
address, [execMsg], await createGasFee(
signingClient, address, [execMsg],
), ADV_MEMO,
)
}
37 changes: 37 additions & 0 deletions components/Pages/Trade/Migrate/hooks/useFetchStaked.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useQuery } from 'react-query'

import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { WalletChainName } from 'constants/index'
import { useClients } from 'hooks/useClients'

const fetchStaked = async (client: CosmWasmClient,
address: string) => {
const lpToken = await client.getBalance(address, 'factory/terra17vas9rhxhc6j6f5wrup9cqapxn74jvpft069py7l7l9kr7wx3tnsxrazux/uLP')
const ampLpToken = await client.getBalance(address, 'factory/terra1zly98gvcec54m3caxlqexce7rus6rzgplz7eketsdz7nh750h2rqvu8uzx/7/bluechip/amplp')

/*
* Const result: JsonObject = await client.queryContractSmart('terra14mmvqn0kthw6sre75vku263lafn5655mkjdejqjedjga4cw0qx2qlf4arv',
* {
* staked_balance: {
* address,
* asset: {
* native: 'factory/terra17vas9rhxhc6j6f5wrup9cqapxn74jvpft069py7l7l9kr7wx3tnsxrazux/uLP',
* },
* },
* })
*/
return Number(lpToken.amount) > 0 ? lpToken : ampLpToken
}
export const useFetchStaked = (address: string): {amount: string, denom: string } => {
const { cosmWasmClient } = useClients(WalletChainName.terra)
const { data } = useQuery({
queryKey: ['fetchStaked', address],
queryFn: () => fetchStaked(cosmWasmClient, address),
enabled: Boolean(cosmWasmClient) && Boolean(address),
staleTime: 30_000, // 30 seconds
cacheTime: 1 * 60 * 1_000, // 5 minutes
});

// Ensure the hook always returns an object with defaults
return data
}
Loading

0 comments on commit b70c0a4

Please sign in to comment.