Skip to content

Commit

Permalink
added comments where we need to switch out old api
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlee348 committed Nov 15, 2024
1 parent 2ef4f33 commit 84e1e85
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 57 deletions.
2 changes: 2 additions & 0 deletions examples/next/src/app/components/Connected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ export const Connected = () => {
const checkTokenBalancesForFeeOptions = async () => {
if (pendingFeeOptionConfirmation) {
const [account] = await walletClient!.getAddresses()
// ATTENTION: switch to new api
const nativeTokenBalance = await indexerClient.getEtherBalance({ accountAddress: account })

// ATTENTION: switch to new api
const tokenBalances = await indexerClient.getTokenBalances({
accountAddress: account
})
Expand Down
2 changes: 2 additions & 0 deletions examples/react/src/components/Connected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ export const Connected = () => {
const checkTokenBalancesForFeeOptions = async () => {
if (pendingFeeOptionConfirmation && walletClient) {
const [account] = await walletClient.getAddresses()
// ATTENTION: switch to new api
const nativeTokenBalance = await indexerClient.getEtherBalance({ accountAddress: account })

// ATTENTION: switch to new api
const tokenBalances = await indexerClient.getTokenBalances({
accountAddress: account
})
Expand Down
122 changes: 65 additions & 57 deletions packages/kit/src/hooks/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const time = {
}

export const getNativeTokenBalance = async (indexerClient: SequenceIndexer, chainId: number, accountAddress: string) => {
// ATTENTION: switch to new api
const res = await indexerClient.getEtherBalance({ accountAddress })

const tokenBalance: TokenBalance = {
Expand Down Expand Up @@ -47,6 +48,7 @@ interface GetTokenBalancesArgs {
}

export const getTokenBalances = async (indexerClient: SequenceIndexer, args: GetTokenBalancesArgs) => {
// ATTENTION: switch to new api
const res = await indexerClient.getTokenBalances({
accountAddress: args.accountAddress,
includeMetadata: args.includeMetadata ?? true,
Expand All @@ -60,6 +62,7 @@ export const getTokenBalances = async (indexerClient: SequenceIndexer, args: Get
}

export const getBalances = async (indexerClient: SequenceIndexer, chainId: number, args: GetTokenBalancesArgs) => {
// ATTENTION: switch to new api
if (!args.accountAddress) {
return []
}
Expand Down Expand Up @@ -106,6 +109,7 @@ interface UseCoinBalanceArgs extends GetTokenBalancesArgs {
}

export const useCoinBalance = (args: UseCoinBalanceArgs) => {
// ATTENTION: switch to new api
const indexerClient = useIndexerClient(args.chainId)

return useQuery({
Expand Down Expand Up @@ -134,6 +138,7 @@ interface UseCollectibleBalanceArgs {
}

export const useCollectibleBalance = (args: UseCollectibleBalanceArgs) => {
// ATTENTION: switch to new api
const indexerClient = useIndexerClient(args.chainId)

return useQuery({
Expand All @@ -158,6 +163,7 @@ export const useCollectibleBalance = (args: UseCollectibleBalanceArgs) => {
}

export const getCollectionBalance = async (indexerClient: SequenceIndexer, args: UseCollectionBalanceArgs) => {
// ATTENTION: switch to new api
const res = await indexerClient.getTokenBalances({
accountAddress: args.accountAddress,
contractAddress: args.contractAddress,
Expand Down Expand Up @@ -388,7 +394,7 @@ const getSwapPrices = async (
const { withContractInfo, ...swapPricesArgs } = args

const res = await apiClient.getSwapPrices({
...swapPricesArgs,
...swapPricesArgs
})

if (res.swapPrices === null) {
Expand All @@ -399,36 +405,34 @@ const getSwapPrices = async (
if (withContractInfo) {
res?.swapPrices.forEach(price => {
const { currencyAddress: rawCurrencyAddress } = price
const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X)
? zeroAddress
: rawCurrencyAddress
const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X) ? zeroAddress : rawCurrencyAddress
const isNativeToken = compareAddress(currencyAddress, zeroAddress)
if (currencyAddress && !currencyInfoMap.has(currencyAddress)) {
const getNativeTokenInfo = () =>new Promise<ContractInfo>((resolve, reject) => {
resolve({
...network?.nativeToken,
logoURI: network?.logoURI || '',
address: zeroAddress
} as ContractInfo)
})
const getNativeTokenInfo = () =>
new Promise<ContractInfo>((resolve, reject) => {
resolve({
...network?.nativeToken,
logoURI: network?.logoURI || '',
address: zeroAddress
} as ContractInfo)
})

currencyInfoMap.set(
currencyAddress,
isNativeToken ?
getNativeTokenInfo().then(data => {
return data
})
:
metadataClient
.getContractInfo({
chainID: String(args.chainId),
contractAddress: currencyAddress
})
.then(data => {
return ({
...data.contractInfo,
isNativeToken
? getNativeTokenInfo().then(data => {
return data
})
})
: metadataClient
.getContractInfo({
chainID: String(args.chainId),
contractAddress: currencyAddress
})
.then(data => {
return {
...data.contractInfo
}
})
)
}
})
Expand All @@ -437,53 +441,52 @@ const getSwapPrices = async (
const currencyBalanceInfoMap = new Map<string, Promise<Balance>>()
res?.swapPrices.forEach(price => {
const { currencyAddress: rawCurrencyAddress } = price
const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X)
? zeroAddress
: rawCurrencyAddress
const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X) ? zeroAddress : rawCurrencyAddress
const isNativeToken = compareAddress(currencyAddress, zeroAddress)

// ATTENTION: switch to new api
if (currencyAddress && !currencyBalanceInfoMap.has(currencyAddress)) {
currencyBalanceInfoMap.set(
currencyAddress,
isNativeToken ?
indexerClient.getEtherBalance({
accountAddress: args.userAddress
}).then(res => ({
balance: res.balance.balanceWei
}))
:
indexerClient
.getTokenBalances({
accountAddress: args.userAddress,
contractAddress: currencyAddress,
includeMetadata: false,
metadataOptions: {
verifiedOnly: true
}
})
.then(balances => {
return ({
balance: balances.balances?.[0].balance || '0'
})
})
isNativeToken
? indexerClient
.getEtherBalance({
accountAddress: args.userAddress
})
.then(res => ({
balance: res.balance.balanceWei
}))
: indexerClient
.getTokenBalances({
accountAddress: args.userAddress,
contractAddress: currencyAddress,
includeMetadata: false,
metadataOptions: {
verifiedOnly: true
}
})
.then(balances => {
return {
balance: balances.balances?.[0].balance || '0'
}
})
)
}
})

return Promise.all(
res?.swapPrices.map(async price => {
const { currencyAddress: rawCurrencyAddress } = price
const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X)
? zeroAddress
: rawCurrencyAddress
const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X) ? zeroAddress : rawCurrencyAddress

return ({
return {
price: {
...price,
currencyAddress
},
info: (await currencyInfoMap.get(currencyAddress)) || undefined,
balance: (await currencyBalanceInfoMap.get(currencyAddress)) || { balance: '0' }
})
}
}) || []
)
} catch (e) {
Expand All @@ -510,7 +513,12 @@ export const useSwapPrices = (args: UseSwapPricesArgs, options: SwapPricesOption
const indexerClient = useIndexerClient(args.chainId)

const enabled =
!!args.chainId && !!args.userAddress && !!args.buyCurrencyAddress && !!args.buyAmount && args.buyAmount !== '0' && !options.disabled
!!args.chainId &&
!!args.userAddress &&
!!args.buyCurrencyAddress &&
!!args.buyAmount &&
args.buyAmount !== '0' &&
!options.disabled

return useQuery({
queryKey: ['swapPrices', args],
Expand All @@ -527,7 +535,7 @@ interface UseSwapQuoteOptions {
disabled?: boolean
}

export const useSwapQuote = (args:GetSwapQuoteArgs, options: UseSwapQuoteOptions) => {
export const useSwapQuote = (args: GetSwapQuoteArgs, options: UseSwapQuoteOptions) => {
const apiClient = useAPIClient()
const { disabled = false } = options

Expand Down Expand Up @@ -555,4 +563,4 @@ export const useSwapQuote = (args:GetSwapQuoteArgs, options: UseSwapQuoteOptio
staleTime: time.oneMinute * 1,
enabled: !disabled || !args.userAddress || !args.chainId || !args.buyCurrencyAddress
})
}
}
2 changes: 2 additions & 0 deletions packages/wallet/src/hooks/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export const getBalancesAssetsSummary = async (
return []
}

// ATTENTION: switch to new api
return getTokenBalances(indexerClient, {
accountAddress,
contractAddress: asset.contractAddress,
Expand All @@ -112,6 +113,7 @@ export const getBalancesAssetsSummary = async (
await Promise.all([
...indexerClientsArr.map(([chainId, indexerClient]) => getNativeTokenBalance(indexerClient, chainId, accountAddress)),
...indexerClientsArr.map(([_chainId, indexerClient]) =>
// ATTENTION: switch to new api
getTokenBalances(indexerClient, {
accountAddress,
hideCollectibles,
Expand Down

0 comments on commit 84e1e85

Please sign in to comment.