Skip to content

Commit

Permalink
feat: index by address instead of symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
Space-Bean committed Jul 14, 2024
1 parent 277910d commit 137e2d9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
6 changes: 3 additions & 3 deletions projects/dex-ui/src/tokens/useAllTokenBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ export const useAllTokensBalance = () => {
for (let i = 0; i < res.length; i++) {
const value = res[i];
const token = tokensToLoad[i];
balances[token.symbol] = token.fromBlockchain(value);
balances[token.address] = token.fromBlockchain(value);

// set the balance in the query cache too
setQueryData(queryKeys.tokenBalance(token.symbol), () => {
return { [token.symbol]: balances[token.symbol] }
setQueryData(queryKeys.tokenBalance(token.address), () => {
return { [token.address]: balances[token.address] }
})
}

Expand Down
16 changes: 7 additions & 9 deletions projects/dex-ui/src/tokens/useSiloBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const useSiloBalance = (token: Token) => {
const sdk = useSdk();

const { data, isLoading, error, refetch, isFetching } = useScopedQuery({
queryKey: queryKeys.siloBalance(token.symbol),
queryKey: queryKeys.siloBalance(token.address),

queryFn: async (): Promise<TokenValue> => {
let balance: TokenValue;
Expand Down Expand Up @@ -40,7 +40,7 @@ export const useSiloBalanceMany = (tokens: Token[]) => {
const setQueryData = useSetScopedQueryData();

const { data, isLoading, error, refetch, isFetching } = useScopedQuery({
queryKey: queryKeys.siloBalanceMany(tokens.map((t) => t.symbol)),
queryKey: queryKeys.siloBalanceMany(tokens.map((t) => t.address)),
queryFn: async () => {
const resultMap: Record<string, TokenValue> = {};
if (!address) return resultMap;
Expand Down Expand Up @@ -70,17 +70,15 @@ export const useSiloBalanceMany = (tokens: Token[]) => {
)
);

console.log("resulst: ", results);

results.forEach((val) => {
resultMap[val.token.symbol] = val.amount;
resultMap[val.token.address] = val.amount;

// merge data into [scope, 'silo', token.symbol]
// merge data into [scope, 'silo', token.address]
setQueryData(queryKeys.siloBalancesAll, (oldData) => {
if (!oldData) return { [val.token.symbol]: val.amount };
return { ...oldData, [val.token.symbol]: val.amount };
if (!oldData) return { [val.token.address]: val.amount };
return { ...oldData, [val.token.address]: val.amount };
});
setQueryData(queryKeys.siloBalance(val.token.symbol), () => {
setQueryData(queryKeys.siloBalance(val.token.address), () => {
return val.amount;
});
});
Expand Down
4 changes: 2 additions & 2 deletions projects/dex-ui/src/tokens/useTokenBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const useTokenBalance = (token: Token | undefined) => {
const setQueryData = useSetScopedQueryData();

const { data, isLoading, error, refetch, isFetching } = useScopedQuery({
queryKey: queryKeys.tokenBalance(token?.symbol),
queryKey: queryKeys.tokenBalance(token?.address),

queryFn: async () => {
if (!token) return;
Expand All @@ -23,7 +23,7 @@ export const useTokenBalance = (token: Token | undefined) => {
}

const result = {
[token.symbol]: balance
[token.address]: balance
};

// Also update the cache of "ALL" token query
Expand Down
9 changes: 4 additions & 5 deletions projects/dex-ui/src/utils/query/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const queryKeys = {
tokenAddress || "invalid",
spender
],
lpSummary: (lpAddresses: string[]) => ["token", "lpSummary", ...lpAddresses],
lpSummaryAll: ["token", "lpSummary"],

// wells
Expand All @@ -24,11 +23,11 @@ export const queryKeys = {

// token balance
tokenBalancesAll: ["token", "balance"],
tokenBalance: (symbol: string | undefined) => ["token", "balance", "external", symbol || "invalid"],
tokenBalanceInternal: (symbol: string | undefined) => ["token", "balance", "internal", symbol || "invalid"],
tokenBalance: (address: string | undefined) => ["token", "balance", "external", address || "invalid"],
tokenBalanceInternal: (address: string | undefined) => ["token", "balance", "internal", address || "invalid"],

siloBalancesAll: ["silo", "balance"],
siloBalance: (symbol: string) => ["silo", "balance", symbol],
siloBalanceMany: (symbols: string[]) => ["silo", "balance", ...symbols],
siloBalance: (address: string) => ["silo", "balance", address],
siloBalanceMany: (addresses: string[]) => ["silo", "balance", ...addresses],
} as const;

0 comments on commit 137e2d9

Please sign in to comment.