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

Feature/lp stake #3255

Merged
merged 7 commits into from
Jan 30, 2025
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,9 +1,11 @@
import type { BoxProps } from '@mui/material/Box';

import { forwardRef } from 'react';
import { Icon } from '@iconify/react';

import Box, { BoxProps } from '@mui/material/Box';
import Box from '@mui/material/Box';

import { IconifyProps } from './types';
import type { IconifyProps } from './types';

// ----------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IconifyIcon } from '@iconify/react';
import type { IconifyIcon } from '@iconify/react';

// ----------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@











Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { StackProps } from '@mui/material/Stack';

import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import { alpha } from '@mui/material/styles';
import Typography from '@mui/material/Typography';
import Stack, { StackProps } from '@mui/material/Stack';

// ----------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Theme, SxProps } from '@mui/material/styles';

import TableRow from '@mui/material/TableRow';
import TableCell from '@mui/material/TableCell';
import { Theme, SxProps } from '@mui/material/styles';

import EmptyContent from './empty-content';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Theme, SxProps } from '@mui/material/styles';
import type { TablePaginationProps } from '@mui/material/TablePagination';

import Box from '@mui/material/Box';
import Switch from '@mui/material/Switch';
import { Theme, SxProps } from '@mui/material/styles';
import FormControlLabel from '@mui/material/FormControlLabel';
import TablePagination, { TablePaginationProps } from '@mui/material/TablePagination';
import TablePagination from '@mui/material/TablePagination';

// ----------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useCallback } from 'react';

import { TableProps } from './types';
import type { TableProps } from './types';

// ----------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default function SelectTokenPair({ onLoading, onCallback }: SelectTokenPa
type: yCoin.coin_type,
icon: yCoin.icon_url || undefined,
symbol: yCoin.symbol,
amount: yCount,
amount: fixdYCount.toString(),
decimal: yCoin.decimals,
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import type { AnnotatedMoveStructView} from '@roochnetwork/rooch-sdk';

import { useMemo } from 'react';
import {
useRoochClient,
useCurrentAddress,
useRoochClientQuery,
} from '@roochnetwork/rooch-sdk-kit';

import { useNetworkVariable } from 'src/hooks/use-networks';

export type AllLiquidityItemType = {
id: string;
createAt: number;
x: {
id: string;
symbol: string;
type: string;
};
y: {
id: string;
symbol: string;
type: string;
};
lpTokenId: string;
creator: string;
};

export type UseAllLiquidityReturn = {
lpTokens: AllLiquidityItemType[];
isPending: boolean;
};

export function useAllLiquidity(): UseAllLiquidityReturn {
const currentAddress = useCurrentAddress();
const dex = useNetworkVariable('dex');
const client = useRoochClient();

const { data: tokenPairs, isPending } = useRoochClientQuery('queryObjectStates', {
filter: {
object_type: `${dex.address}::swap::TokenPair`,
},
queryOption: {
showDisplay: true,
},
});

const resolvedTokenPairs = useMemo(() => {
if (!tokenPairs) {
return [];
}

const rowItme: AllLiquidityItemType[] = tokenPairs!.data.map((item) => {
const xView = item.decoded_value!.value.balance_x as AnnotatedMoveStructView;
let xType = xView.type.replace('0x2::object::Object<0x3::coin_store::CoinStore<', '');
xType = xType.replace('>>', '');
const xName = xType.split('::');
const yView = item.decoded_value!.value.balance_y as AnnotatedMoveStructView;
let yType = yView.type.replace('0x2::object::Object<0x3::coin_store::CoinStore<', '');
yType = yType.replace('>>', '');
const yName = yType.split('::');
const lpView = item.decoded_value!.value.coin_info as AnnotatedMoveStructView;
return {
id: item.id,
creator: item.decoded_value!.value.creator as string,
createAt: Number(item.created_at),
lpTokenId: lpView.value.id as string,
x: {
id: xView.value.id as string,
symbol: xName[xName.length - 1].replace('>>', ''),
type: xType,
},
y: {
id: yView.value.id as string,
symbol: yName[xName.length - 1].replace('>>', ''),
type: yType,
},
};
});

return rowItme;
}, [tokenPairs]);

return {
lpTokens: resolvedTokenPairs,
isPending,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { BalanceInfoView } from '@roochnetwork/rooch-sdk';

import { useMemo } from 'react';
import { useCurrentAddress, useRoochClientQuery } from '@roochnetwork/rooch-sdk-kit';

import { useNetworkVariable } from 'src/hooks/use-networks';

export type OwnerLiquidityItemType = {
x: {
name: string;
type: string;
};
y: {
name: string;
type: string;
};
} & BalanceInfoView;

export type UseOwnerLiquidityReturn = {
lpTokens: OwnerLiquidityItemType[];
isPending: boolean;
};

export function useOwnerLiquidity(): UseOwnerLiquidityReturn {
const currentAddress = useCurrentAddress();
const dex = useNetworkVariable('dex');

const { data: assetsList, isPending } = useRoochClientQuery('getBalances', {
owner: currentAddress?.toStr() || '',
});

const lpTokens = useMemo(() => {
if (!assetsList) {
return [];
}

const tokens: OwnerLiquidityItemType[] = assetsList!.data
.filter((item) => item.symbol.startsWith('RDexLP'))
.map((item) => {
const t = item.coin_type
.replaceAll(' ', '')
.replace(`${dex.address}::swap::LPToken<`, '')
.split(',');
const x = t[0];
const y = t[1].substring(0, t[1].length - 1);
const xName = x.split('::');
const yName = y.split('::');
return {
...item,
x: {
type: x,
name: xName[xName.length - 1],
},
y: {
type: y,
name: yName[yName.length - 1],
},
};
})
.sort((a, b) => b.fixedBalance - a.fixedBalance);
return tokens;
}, [assetsList, dex.address]);

return {
lpTokens,
isPending,
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { BalanceInfoView, AnnotatedMoveStructView } from '@roochnetwork/rooch-sdk';

import { toast } from 'sonner';
import BigNumber from 'bignumber.js';
import { useDebounce } from 'react-use';
import { Args, Transaction } from '@roochnetwork/rooch-sdk';
Expand Down Expand Up @@ -35,6 +34,8 @@ import { useNetworkVariable } from 'src/hooks/use-networks';
import { formatCoin } from 'src/utils/format-number';
import { toDust, bigNumberToBigInt } from 'src/utils/number';

import { toast } from 'src/components/snackbar';

import Icon from '../components/icon';

import type { AllLiquidityItemType } from './all-liquidity-row-item';
Expand Down Expand Up @@ -221,7 +222,9 @@ export default function AddLiquidityModal({
const y = BigNumber(yBalance).multipliedBy(xRate);

if (y.toNumber() > Number(assetsMap.get(row.y.type)?.balance || 0)) {
setYLabelError(`Insufficient`);
setYLabelError('Insufficient');
} else {
setYLabelError(undefined);
}
setYAmount(y.toFixed(0, 1));
}, [xAmount, reserveX, reserveY, row.x.type, row.y.type, assetsMap]);
Expand Down Expand Up @@ -257,7 +260,7 @@ export default function AddLiquidityModal({
inputMode="decimal"
autoComplete="off"
onChange={(e) => {
const value = e.target.value;
const {value} = e.target;
if (/^\d*\.?\d*$/.test(value) === false) {
return;
}
Expand Down
Loading
Loading