Skip to content

Commit

Permalink
Move useGetCoins to core (MystenLabs#12020)
Browse files Browse the repository at this point in the history
## Description 

Describe the changes or additions included in this PR.

- Move useGetCoins for core
- Explorer and Wallet, useGetCoins  from core 
- Update wallet to keep calling `fetchNextPage` until hasNextPage is
false

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
Jibz-Mysten authored May 23, 2023
1 parent 1c0c6f2 commit 19ad963
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { useRpcClient } from '@mysten/core';
import { useRpcClient } from '../api/RpcClientContext';
import { useInfiniteQuery } from '@tanstack/react-query';

import type { SuiAddress } from '@mysten/sui.js';

const MAX_COINS_PER_REQUEST = 10;

// Fetch all coins for an address, this will keep calling the API until all coins are fetched
export function useGetCoins(coinType: string, address?: SuiAddress | null) {
export function useGetCoins(
coinType: string,
address?: SuiAddress | null,
maxCoinsPerRequest = MAX_COINS_PER_REQUEST
) {
const rpc = useRpcClient();
return useInfiniteQuery(
['get-coins', address, coinType],
async ({ pageParam }) =>
await rpc.getCoins({
['get-coins', address, coinType, maxCoinsPerRequest],
({ pageParam }) =>
rpc.getCoins({
owner: address!,
coinType,
cursor: pageParam ? pageParam.cursor : null,
limit: MAX_COINS_PER_REQUEST,
limit: maxCoinsPerRequest,
}),
{
getNextPageParam: (lastPage) =>
lastPage?.hasNextPage
getNextPageParam: ({ hasNextPage, nextCursor }) =>
hasNextPage
? {
cursor: lastPage.nextCursor,
cursor: nextCursor,
}
: false,
enabled: !!address,
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ export * from './hooks/useGetOwnedObjects';
export * from './hooks/useCopyToClipboard';
export * from './hooks/useGetOriginByteKioskContents';
export * from './hooks/useAppsBackend';
export * from './hooks/useGetCoins';
3 changes: 1 addition & 2 deletions apps/explorer/src/components/OwnedCoins/OwnedCoinsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { useOnScreen } from '@mysten/core';
import { useOnScreen, useGetCoins } from '@mysten/core';
import { useEffect, useRef } from 'react';

import CoinItem from './CoinItem';

import { useGetCoins } from '~/hooks/useGetCoins';
import { LoadingSpinner } from '~/ui/LoadingSpinner';

type CoinsPanelProps = {
Expand Down
2 changes: 1 addition & 1 deletion apps/wallet/src/ui/app/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export { useGetTransferAmount } from './useGetTransferAmount';
export { useGetCoinBalance } from './useGetCoinBalance';
export { useGetAllBalances } from './useGetAllBalances';
export { useOwnedNFT } from './useOwnedNFT';
export { useGetCoins } from './useGetCoins';
export * from './useSigner';
export * from './useTransactionData';
export * from './useActiveAddress';
export * from './useGetAllCoins';
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import type { SuiAddress, PaginatedCoins, CoinStruct } from '@mysten/sui.js';
const MAX_COINS_PER_REQUEST = 100;

// Fetch all coins for an address, this will keep calling the API until all coins are fetched
export function useGetCoins(coinType: string, address?: SuiAddress | null) {
export function useGetAllCoins(coinType: string, address?: SuiAddress | null) {
const rpc = useRpcClient();
return useQuery({
queryKey: ['get-coins', address, coinType],
queryKey: ['get-all-coins', address, coinType],
queryFn: async () => {
let cursor: string | null = null;
const allData: CoinStruct[] = [];
Expand All @@ -36,5 +36,6 @@ export function useGetCoins(coinType: string, address?: SuiAddress | null) {
return allData;
},
enabled: !!address,
initialData: [],
});
}
25 changes: 9 additions & 16 deletions apps/wallet/src/ui/app/pages/home/transfer-coin/SendTokenForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { AddressInput } from '_components/address-input';
import Alert from '_components/alert';
import Loading from '_components/loading';
import { parseAmount } from '_helpers';
import { useTransactionGasBudget, useGetCoins } from '_hooks';
import { useTransactionGasBudget, useGetAllCoins } from '_hooks';
import { GAS_SYMBOL } from '_src/ui/app/redux/slices/sui-objects/Coin';
import { InputWithAction } from '_src/ui/app/shared/InputWithAction';

Expand Down Expand Up @@ -107,40 +107,33 @@ export function SendTokenForm({
}: SendTokenFormProps) {
const activeAddress = useActiveAddress();
// Get all coins of the type
const { data: coinsData, isLoading: coinsIsLoading } = useGetCoins(
const { data: coinsData, isLoading: coinsIsLoading } = useGetAllCoins(
coinType,
activeAddress!
);

const { data: suiCoinsData, isLoading: suiCoinsIsLoading } = useGetCoins(
const { data: suiCoinsData, isLoading: suiCoinsIsLoading } = useGetAllCoins(
SUI_TYPE_ARG,
activeAddress!
);

const suiCoins = suiCoinsData;
const coins = coinsData;
const coinBalance = CoinAPI.totalBalance(coins || []);
const suiBalance = CoinAPI.totalBalance(suiCoinsData || []);
const coinBalance = CoinAPI.totalBalance(coins);
const suiBalance = CoinAPI.totalBalance(suiCoins);

const coinSymbol = (coinType && CoinAPI.getCoinSymbol(coinType)) || '';
const coinMetadata = useCoinMetadata(coinType);
const coinDecimals = coinMetadata.data?.decimals ?? 0;

const validationSchemaStepOne = useMemo(
() =>
createValidationSchemaStepOne(
coinBalance,
coinSymbol,
coinDecimals
),
[coinBalance, coinSymbol, coinDecimals]
);

const [tokenBalance, symbol, queryResult] = useFormatCoin(
coinBalance,
coinType,
CoinFormat.FULL
);
const validationSchemaStepOne = useMemo(
() => createValidationSchemaStepOne(coinBalance, symbol, coinDecimals),
[coinBalance, symbol, coinDecimals]
);

// remove the comma from the token balance
const formattedTokenBalance = tokenBalance.replace(/,/g, '');
Expand Down

0 comments on commit 19ad963

Please sign in to comment.