Skip to content

Commit

Permalink
refactor: change get tokens query logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jtourkos committed Jan 9, 2025
1 parent ef80b44 commit f6fb763
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 56 deletions.
49 changes: 49 additions & 0 deletions src/dataLoaders/sqlQueries/getTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { DbSchema } from '../../common/types';
import { dbConnection } from '../../database/connectToDatabase';

export default async function getTokens(dbSchema: DbSchema): Promise<string[]> {
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),
]),
];
}
32 changes: 11 additions & 21 deletions src/user/userResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
);
Expand Down
37 changes: 2 additions & 35 deletions src/utils/getWithdrawableBalances.ts
Original file line number Diff line number Diff line change
@@ -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<string[]>((acc, tokenAddress) => {
if (!acc.includes(tokenAddress)) {
return [...acc, tokenAddress];
}

return acc;
}, []);
}
import getTokens from '../dataLoaders/sqlQueries/getTokens';

export async function getTokenBalancesOnChain(
accountId: AccountId,
Expand All @@ -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]: {
Expand Down

0 comments on commit f6fb763

Please sign in to comment.