diff --git a/src/dataLoaders/sqlQueries/getTokens.ts b/src/dataLoaders/sqlQueries/getTokens.ts new file mode 100644 index 0000000..38c4603 --- /dev/null +++ b/src/dataLoaders/sqlQueries/getTokens.ts @@ -0,0 +1,49 @@ +import type { DbSchema } from '../../common/types'; +import { dbConnection } from '../../database/connectToDatabase'; + +export default async function getTokens(dbSchema: DbSchema): Promise { + const distinctGivenTokens = ( + await dbConnection.query( + ` + SELECT DISTINCT ON ("erc20") "erc20" + FROM "${dbSchema}"."GivenEvents" + `, + ) + )[0] as { erc20: string }[]; + + const distinctSplitTokens = ( + await dbConnection.query( + ` + SELECT DISTINCT ON ("erc20") "erc20" + FROM "${dbSchema}"."SplitEvents" + `, + ) + )[0] as { erc20: string }[]; + + const distinctStreamSetTokens = ( + await dbConnection.query( + ` + SELECT DISTINCT ON ("erc20") "erc20" + FROM "${dbSchema}"."StreamsSetEvents" + `, + ) + )[0] as { erc20: string }[]; + + const distinctSqueezedStreamsTokens = ( + await dbConnection.query( + ` + SELECT DISTINCT ON ("erc20") "erc20" + FROM "${dbSchema}"."SqueezedStreamsEvents" + `, + ) + )[0] as { erc20: string }[]; + + return [ + ...new Set([ + ...distinctGivenTokens.map((row: any) => row.erc20), + ...distinctSplitTokens.map((row: any) => row.erc20), + ...distinctStreamSetTokens.map((row: any) => row.erc20), + ...distinctSqueezedStreamsTokens.map((row: any) => row.erc20), + ]), + ]; +} diff --git a/src/user/userResolvers.ts b/src/user/userResolvers.ts index a6b8e87..0c5f7d8 100644 --- a/src/user/userResolvers.ts +++ b/src/user/userResolvers.ts @@ -36,9 +36,8 @@ import toResolverUser from './userUtils'; import { getCrossChainAddressDriverAccountIdByAddress } from '../common/dripsContracts'; import shouldNeverHappen from '../utils/shouldNeverHappen'; import { chainToDbSchema } from '../utils/chainSchemaMappings'; -import getWithdrawableBalancesOnChain, { - getRelevantTokens, -} from '../utils/getWithdrawableBalances'; +import getWithdrawableBalancesOnChain from '../utils/getWithdrawableBalances'; +import getTokens from '../dataLoaders/sqlQueries/getTokens'; const userResolvers = { Query: { @@ -110,27 +109,18 @@ const userResolvers = { const metadata = chainMetadata[userChain]?.metadata ?? {}; - const [assetConfigs, incomingStreams, relevantTokensForIncomingBalance] = - await Promise.all([ - await getAssetConfigs(accountId as AddressDriverId, metadata, [ - userChain, - ]), - streamsDataSource.getUserIncomingStreams( - [userChain], - accountId as AddressDriverId, - ), - getRelevantTokens(accountId as AccountId, userChain), - ]); - - const allTokens = Array.from( - new Set([ - ...assetConfigs[userChain].map((ac) => ac.tokenAddress), - ...relevantTokensForIncomingBalance, + const [assetConfigs, incomingStreams] = await Promise.all([ + await getAssetConfigs(accountId as AddressDriverId, metadata, [ + userChain, ]), - ); + streamsDataSource.getUserIncomingStreams( + [userChain], + accountId as AddressDriverId, + ), + ]); return Promise.all( - allTokens.map(async (tokenAddress) => { + (await getTokens(userChain)).map(async (tokenAddress) => { const outgoingAssetConfig = assetConfigs[userChain].find( (ac) => ac.tokenAddress === tokenAddress, ); diff --git a/src/utils/getWithdrawableBalances.ts b/src/utils/getWithdrawableBalances.ts index 2826872..9be14c4 100644 --- a/src/utils/getWithdrawableBalances.ts +++ b/src/utils/getWithdrawableBalances.ts @@ -1,40 +1,7 @@ import type { AccountId, DbSchema } from '../common/types'; import dripsContracts from '../common/dripsContracts'; -import streamReceiverSeenEventQueries from '../dataLoaders/sqlQueries/streamReceiverSeenEventQueries'; -import streamsSetEventsQueries from '../dataLoaders/sqlQueries/streamsSetEventsQueries'; -import givenEventsQueries from '../dataLoaders/sqlQueries/givenEventsQueries'; -import splitEventsQueries from '../dataLoaders/sqlQueries/splitEventsQueries'; import { dbSchemaToChain } from './chainSchemaMappings'; - -export async function getRelevantTokens(accountId: AccountId, chain: DbSchema) { - const streamReceiverSeenEventsForUser = - await streamReceiverSeenEventQueries.getByAccountId([chain], accountId); - - const [ - incomingStreamTokenAddresses, - incomingGivesTokenAddresses, - incomingSplitEventsTokenAddresses, - ] = await Promise.all([ - streamsSetEventsQueries.getDistinctErc20ByReceiversHashes( - [chain], - streamReceiverSeenEventsForUser.map((event) => event.receiversHash), - ), - givenEventsQueries.getDistinctErc20ByReceiver([chain], accountId), - splitEventsQueries.getDistinctErc20ByReceiver([chain], accountId), - ]); - - return [ - ...incomingStreamTokenAddresses, - ...incomingGivesTokenAddresses, - ...incomingSplitEventsTokenAddresses, - ].reduce((acc, tokenAddress) => { - if (!acc.includes(tokenAddress)) { - return [...acc, tokenAddress]; - } - - return acc; - }, []); -} +import getTokens from '../dataLoaders/sqlQueries/getTokens'; export async function getTokenBalancesOnChain( accountId: AccountId, @@ -60,7 +27,7 @@ export default async function getWithdrawableBalancesOnChain( accountId: AccountId, chain: DbSchema, ) { - const relevantTokenAddresses = await getRelevantTokens(accountId, chain); + const relevantTokenAddresses = await getTokens(chain); const balances: { [tokenAddress: string]: {