@merkl/api 0.10.240 → 0.10.241
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
| 
         @@ -6,6 +6,6 @@ type ReturnType = { 
     | 
|
| 
       6 
6 
     | 
    
         
             
                    decimals: number;
         
     | 
| 
       7 
7 
     | 
    
         
             
                };
         
     | 
| 
       8 
8 
     | 
    
         
             
            };
         
     | 
| 
       9 
     | 
    
         
            -
            export declare function getOnlyUserBalance(chainId: number, userAddress: string, tokenAddresses: string[]): Promise< 
     | 
| 
      
 9 
     | 
    
         
            +
            export declare function getOnlyUserBalance(chainId: number, userAddress: string, tokenAddresses: string[]): Promise<ReturnType>;
         
     | 
| 
       10 
10 
     | 
    
         
             
            export declare function getUserBalances(user: string, chainId: number, tokenAddresses?: string[]): Promise<UncachedResult<ReturnType>>;
         
     | 
| 
       11 
11 
     | 
    
         
             
            export {};
         
     | 
| 
         @@ -1,42 +1,43 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            import { Erc20__factory } from "@sdk";
         
     | 
| 
      
 1 
     | 
    
         
            +
            import { ChainId, ChainInteractionService, Erc20__factory, EthOnZKSync_INTERFACE, NETWORK_LABELS, } from "@sdk";
         
     | 
| 
       2 
2 
     | 
    
         
             
            import { log } from "../../utils/logger";
         
     | 
| 
       3 
3 
     | 
    
         
             
            import { getTokensListWithCache } from "../getTokensList";
         
     | 
| 
       4 
4 
     | 
    
         
             
            export async function getOnlyUserBalance(chainId, userAddress, tokenAddresses) {
         
     | 
| 
       5 
5 
     | 
    
         
             
                const calls = [];
         
     | 
| 
       6 
6 
     | 
    
         
             
                const ERC20_Interface = Erc20__factory.createInterface();
         
     | 
| 
       7 
7 
     | 
    
         
             
                for (const tokenAddress of tokenAddresses) {
         
     | 
| 
       8 
     | 
    
         
            -
                     
     | 
| 
       9 
     | 
    
         
            -
                         
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
      
 8 
     | 
    
         
            +
                    if (chainId === ChainId.ZKSYNC && tokenAddress === "0x000000000000000000000000000000000000800A") {
         
     | 
| 
      
 9 
     | 
    
         
            +
                        calls.push({
         
     | 
| 
      
 10 
     | 
    
         
            +
                            allowFailure: true,
         
     | 
| 
      
 11 
     | 
    
         
            +
                            callData: EthOnZKSync_INTERFACE.encodeFunctionData("balanceOf", [userAddress]),
         
     | 
| 
      
 12 
     | 
    
         
            +
                            target: tokenAddress,
         
     | 
| 
      
 13 
     | 
    
         
            +
                        });
         
     | 
| 
      
 14 
     | 
    
         
            +
                    }
         
     | 
| 
      
 15 
     | 
    
         
            +
                    else {
         
     | 
| 
      
 16 
     | 
    
         
            +
                        calls.push({
         
     | 
| 
      
 17 
     | 
    
         
            +
                            allowFailure: true,
         
     | 
| 
      
 18 
     | 
    
         
            +
                            callData: ERC20_Interface.encodeFunctionData("balanceOf", [userAddress]),
         
     | 
| 
      
 19 
     | 
    
         
            +
                            target: tokenAddress,
         
     | 
| 
      
 20 
     | 
    
         
            +
                        });
         
     | 
| 
      
 21 
     | 
    
         
            +
                    }
         
     | 
| 
       13 
22 
     | 
    
         
             
                }
         
     | 
| 
       14 
     | 
    
         
            -
                 
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
       16 
     | 
    
         
            -
             
     | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
       18 
     | 
    
         
            -
             
     | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
             
     | 
| 
       21 
     | 
    
         
            -
             
     | 
| 
       22 
     | 
    
         
            -
             
     | 
| 
       23 
     | 
    
         
            -
             
     | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
       29 
     | 
    
         
            -
             
     | 
| 
       30 
     | 
    
         
            -
             
     | 
| 
       31 
     | 
    
         
            -
             
     | 
| 
       32 
     | 
    
         
            -
                                    decimals: 0,
         
     | 
| 
       33 
     | 
    
         
            -
                                    symbol: "",
         
     | 
| 
       34 
     | 
    
         
            -
                                };
         
     | 
| 
       35 
     | 
    
         
            -
                            }
         
     | 
| 
       36 
     | 
    
         
            -
                            return res;
         
     | 
| 
       37 
     | 
    
         
            -
                        },
         
     | 
| 
       38 
     | 
    
         
            -
                    },
         
     | 
| 
       39 
     | 
    
         
            -
                };
         
     | 
| 
      
 23 
     | 
    
         
            +
                const result = await ChainInteractionService(chainId).fetchState(calls);
         
     | 
| 
      
 24 
     | 
    
         
            +
                const res = {};
         
     | 
| 
      
 25 
     | 
    
         
            +
                for (let j = 0; j < tokenAddresses.length; j++) {
         
     | 
| 
      
 26 
     | 
    
         
            +
                    const tokenAddress = tokenAddresses[j];
         
     | 
| 
      
 27 
     | 
    
         
            +
                    let balance = "0";
         
     | 
| 
      
 28 
     | 
    
         
            +
                    try {
         
     | 
| 
      
 29 
     | 
    
         
            +
                        balance = ERC20_Interface.decodeFunctionResult("balanceOf", result[j].returnData)[0]?.toString();
         
     | 
| 
      
 30 
     | 
    
         
            +
                    }
         
     | 
| 
      
 31 
     | 
    
         
            +
                    catch (_error) {
         
     | 
| 
      
 32 
     | 
    
         
            +
                        log.local(`❌ Failed to call balanceOf for ${tokenAddress} on ${NETWORK_LABELS[chainId]}`);
         
     | 
| 
      
 33 
     | 
    
         
            +
                    }
         
     | 
| 
      
 34 
     | 
    
         
            +
                    res[tokenAddress] = {
         
     | 
| 
      
 35 
     | 
    
         
            +
                        balance: balance,
         
     | 
| 
      
 36 
     | 
    
         
            +
                        decimals: 0,
         
     | 
| 
      
 37 
     | 
    
         
            +
                        symbol: "",
         
     | 
| 
      
 38 
     | 
    
         
            +
                    };
         
     | 
| 
      
 39 
     | 
    
         
            +
                }
         
     | 
| 
      
 40 
     | 
    
         
            +
                return res;
         
     | 
| 
       40 
41 
     | 
    
         
             
            }
         
     | 
| 
       41 
42 
     | 
    
         
             
            export async function getUserBalances(user, chainId, tokenAddresses) {
         
     | 
| 
       42 
43 
     | 
    
         
             
                const tokens = (await getTokensListWithCache())?.[chainId];
         
     | 
| 
         @@ -1,6 +1,5 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            import { getTokensListWithCache } from "../../../libs/getTokensList";
         
     | 
| 
       2 
2 
     | 
    
         
             
            import { getOnlyUserBalance } from "../../../libs/tokens/balances";
         
     | 
| 
       3 
     | 
    
         
            -
            import { executeSimple } from "../../../utils/execute";
         
     | 
| 
       4 
3 
     | 
    
         
             
            import { log } from "../../../utils/logger";
         
     | 
| 
       5 
4 
     | 
    
         
             
            import { apiDbClient } from "../../../utils/prisma";
         
     | 
| 
       6 
5 
     | 
    
         
             
            import { Prisma } from "../../../../database/api/.generated";
         
     | 
| 
         @@ -22,7 +21,7 @@ export class TokenService { 
     | 
|
| 
       22 
21 
     | 
    
         
             
                 * Fetches balances of provided tokens
         
     | 
| 
       23 
22 
     | 
    
         
             
                 */
         
     | 
| 
       24 
23 
     | 
    
         
             
                static async fetchBalances(chainId, userAddress, tokens) {
         
     | 
| 
       25 
     | 
    
         
            -
                    const balances = await  
     | 
| 
      
 24 
     | 
    
         
            +
                    const balances = await getOnlyUserBalance(chainId, userAddress, tokens.map(t => t.address));
         
     | 
| 
       26 
25 
     | 
    
         
             
                    const tokensWithBalances = tokens.map(t => {
         
     | 
| 
       27 
26 
     | 
    
         
             
                        const balance = balances[t.address] ??
         
     | 
| 
       28 
27 
     | 
    
         
             
                            balances[Object.keys(balances).find(a => a?.toLowerCase() === t.address?.toLowerCase()) ?? ""];
         
     |