Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dib542 committed Dec 13, 2023
1 parent f8b32ff commit 5b18c29
Show file tree
Hide file tree
Showing 29 changed files with 983 additions and 887 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@cosmjs/crypto": "0.31.1",
"@cosmjs/proto-signing": "0.31.1",
"@cosmjs/stargate": "0.31.1",
"@duality-labs/dualityjs": "0.3.3-json-types",
"@duality-labs/dualityjs": "0.4.0",
"@floating-ui/react": "^0.24.5",
"@fortawesome/fontawesome-svg-core": "^6.1.2",
"@fortawesome/free-solid-svg-icons": "^6.1.2",
Expand Down
2 changes: 1 addition & 1 deletion src/components/cards/LimitOrderCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function LimitOrder({
orderType: orderTypeEnum[execution],
// todo: set tickIndex to allow for a tolerance:
// the below function is a tolerance of 0
tickIndex: Long.fromNumber(
tickIndexInToOut: Long.fromNumber(
showLimitPrice
? // set given limit price
displayPriceToTickIndex(
Expand Down
2 changes: 1 addition & 1 deletion src/components/cards/LimitOrderContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useMemo,
useState,
} from 'react';
import { LimitOrderType } from '@duality-labs/dualityjs/types/codegen/dualitylabs/duality/dex/tx';
import { LimitOrderType } from '@duality-labs/dualityjs/types/codegen/duality/dex/tx';

export type LimitOrderTypeKeys = keyof Omit<
typeof LimitOrderType,
Expand Down
73 changes: 38 additions & 35 deletions src/components/cards/PoolsTableCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import { formatAmount, formatCurrency } from '../../lib/utils/number';
import { Token, getDisplayDenomAmount } from '../../lib/web3/utils/tokens';
import useTokenPairs from '../../lib/web3/hooks/useTokenPairs';
import { getPairID } from '../../lib/web3/utils/pairs';

import { UserPositionDepositContext } from '../../lib/web3/hooks/useUserShares';
import {
ValuedUserPositionDepositContext,
useUserPositionsShareValues,
} from '../../lib/web3/hooks/useUserShareValues';
UserValuedReserves,
useEstimatedUserReserves,
} from '../../lib/web3/hooks/useUserReserves';

import './PoolsTableCard.scss';

Expand Down Expand Up @@ -192,24 +190,24 @@ export function MyPoolsTableCard<T extends string | number>({

const tokenList = useTokensWithIbcInfo(useTokens());

const userPositionsShareValues = useUserPositionsShareValues();
const { data: userValuedReserves } = useEstimatedUserReserves();

const myPoolsList = useMemo<
Array<
[
pairID: string,
token0: Token,
token1: Token,
userPositions: UserPositionDepositContext[]
userValuedReserves: UserValuedReserves[]
]
>
>(() => {
// collect positions into token pair groups
const userPositionsShareValueMap = userPositionsShareValues.reduce<{
const userValuedReservesMap = (userValuedReserves || []).reduce<{
[pairID: string]: {
token0: Token;
token1: Token;
userPositions: UserPositionDepositContext[];
userValuedReserves: UserValuedReserves[];
};
}>((map, userPosition) => {
const { token0: token0Address, token1: token1Address } =
Expand All @@ -218,25 +216,29 @@ export function MyPoolsTableCard<T extends string | number>({
const token0 = tokenList.find(matchTokenByDenom(token0Address));
const token1 = tokenList.find(matchTokenByDenom(token1Address));
if (pairID && token0 && token1) {
map[pairID] = map[pairID] || { token0, token1, userPositions: [] };
map[pairID].userPositions.push(userPosition);
map[pairID] = map[pairID] || {
token0,
token1,
userValuedReserves: [],
};
map[pairID].userValuedReserves.push(userPosition);
}
return map;
}, {});

return userPositionsShareValueMap
? Object.entries(userPositionsShareValueMap).map<
[string, Token, Token, UserPositionDepositContext[]]
>(([pairId, { token0, token1, userPositions }]) => {
return [pairId, token0, token1, userPositions];
return userValuedReservesMap
? Object.entries(userValuedReservesMap).map<
[string, Token, Token, UserValuedReserves[]]
>(([pairId, { token0, token1, userValuedReserves }]) => {
return [pairId, token0, token1, userValuedReserves];
})
: [];
}, [userPositionsShareValues, tokenList]);
}, [userValuedReserves, tokenList]);

const filteredPoolTokenList = useFilteredTokenList(tokenList, searchValue);

const filteredPoolsList = useMemo<
Array<[string, Token, Token, UserPositionDepositContext[]]>
Array<[string, Token, Token, UserValuedReserves[]]>
>(() => {
const tokenList = filteredPoolTokenList.map(({ token }) => token);
return myPoolsList.filter(([, token0, token1]) => {
Expand Down Expand Up @@ -266,7 +268,7 @@ export function MyPoolsTableCard<T extends string | number>({
</thead>
<tbody>
{filteredPoolsList.map(
([pairId, token0, token1, userPositions]) => {
([pairId, token0, token1, userValuedReserves]) => {
const onRowClick:
| MouseEventHandler<HTMLButtonElement>
| undefined = onTokenPairClick
Expand All @@ -278,7 +280,7 @@ export function MyPoolsTableCard<T extends string | number>({
key={pairId}
token0={token0}
token1={token1}
userPositions={userPositions}
userValuedReserves={userValuedReserves}
onClick={onRowClick}
actions={userPositionActions}
/>
Expand Down Expand Up @@ -314,7 +316,7 @@ export interface Actions {
action: (action: {
token0: Token;
token1: Token;
userPositions: Array<ValuedUserPositionDepositContext>;
userValuedReserves: Array<UserValuedReserves>;
navigate: NavigateFunction;
}) => void;
};
Expand All @@ -335,30 +337,26 @@ const defaultActions: Actions = {
function PositionRow({
token0,
token1,
userPositions,
userValuedReserves,
onClick,
actions = {},
}: {
token0: Token;
token1: Token;
userPositions: Array<ValuedUserPositionDepositContext>;
userValuedReserves: Array<UserValuedReserves>;
onClick?: MouseEventHandler<HTMLButtonElement>;
actions?: Actions;
}) {
const navigate = useNavigate();

const total0 = userPositions.reduce<BigNumber>((acc, { token0Context }) => {
return acc.plus(token0Context?.userReserves || 0);
}, new BigNumber(0));
const total1 = userPositions.reduce<BigNumber>((acc, { token1Context }) => {
return acc.plus(token1Context?.userReserves || 0);
const total0 = userValuedReserves.reduce<BigNumber>((acc, estimate) => {
return acc.plus(estimate.reserves.reserves0 || 0);
}, new BigNumber(0));

const value0 = userPositions.reduce<BigNumber>((acc, { token0Value }) => {
return acc.plus(token0Value || 0);
const total1 = userValuedReserves.reduce<BigNumber>((acc, estimate) => {
return acc.plus(estimate.reserves.reserves1 || 0);
}, new BigNumber(0));
const value1 = userPositions.reduce<BigNumber>((acc, { token1Value }) => {
return acc.plus(token1Value || 0);
const value = userValuedReserves.reduce<BigNumber>((acc, { value }) => {
return acc.plus(value || 0);
}, new BigNumber(0));

if (total0 && total1) {
Expand All @@ -367,7 +365,7 @@ function PositionRow({
<td>
<TokenPair token0={token0} token1={token1} onClick={onClick} />
</td>
<td>{formatCurrency(value0.plus(value1).toNumber())}</td>
<td>{formatCurrency(value.toNumber())}</td>
<td>
<span className="token-compositions">
{formatAmount(getDisplayDenomAmount(token0, total0) || 0)}
Expand All @@ -387,7 +385,12 @@ function PositionRow({
key={actionKey}
type="button"
onClick={() => {
action({ token0, token1, userPositions, navigate });
action({
token0,
token1,
userValuedReserves,
navigate,
});
}}
className={['button nowrap', className]
.filter(Boolean)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/web3/hooks/useChains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const dualityChain: Chain = {
pretty_name: REACT_APP__CHAIN_NAME,
status: 'upcoming',
network_type: 'testnet',
bech32_prefix: 'cosmos',
bech32_prefix: 'dual',
slip44: 118,
logo_URIs: {
svg: dualityLogo,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/web3/hooks/useIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ export function useIndexerStreamOfDualDataSet<
url,
IndexerStreamAccumulateDualDataSet,
opts
);
) as StaleWhileRevalidateState<[DataSet, DataSet]>;
}

// add higher-level function to fetch multiple pages of data as "one request"
Expand Down
28 changes: 21 additions & 7 deletions src/lib/web3/hooks/useTickLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,39 @@ import { useIndexerStreamOfDualDataSet } from './useIndexer';
import { Token, TokenID } from '../utils/tokens';

type ReserveDataRow = [tickIndex: number, reserves: number];
type ReserveDataSet = Map<ReserveDataRow['0'], ReserveDataRow['1']>;

// add convenience method to fetch ticks in a pair
export function useTokenPairTickLiquidity([tokenIdA, tokenIdB]: [
// add convenience method to fetch liquidity maps of a pair
export function useTokenPairMapLiquidity([tokenIdA, tokenIdB]: [
TokenID?,
TokenID?
]): {
data: [TickInfo[] | undefined, TickInfo[] | undefined];
isValidating: boolean;
error: unknown;
data?: [ReserveDataSet, ReserveDataSet];
error?: unknown;
} {
const [tokenId0, tokenId1] = useOrderedTokenPair([tokenIdA, tokenIdB]) || [];
// stream data from indexer
const { data, error } = useIndexerStreamOfDualDataSet<ReserveDataRow>(
return useIndexerStreamOfDualDataSet<ReserveDataRow>(
tokenIdA && tokenIdB && `/liquidity/pair/${tokenIdA}/${tokenIdB}`,
{
// remove entries of value 0 from the accumulated map, they are not used
mapEntryRemovalValue: 0,
}
);
}

// add convenience method to fetch ticks in a pair
export function useTokenPairTickLiquidity([tokenIdA, tokenIdB]: [
TokenID?,
TokenID?
]): {
data: [TickInfo[] | undefined, TickInfo[] | undefined];
isValidating: boolean;
error: unknown;
} {
const [tokenId0, tokenId1] = useOrderedTokenPair([tokenIdA, tokenIdB]) || [];

// use stream data from indexer
const { data, error } = useTokenPairMapLiquidity([tokenIdA, tokenIdB]);

// add token context into pool reserves
const token0 = useToken(tokenId0);
Expand Down
Loading

0 comments on commit 5b18c29

Please sign in to comment.