@0xsquid/sdk
Advanced tools
Comparing version 1.14.3 to 1.14.4
@@ -51,3 +51,3 @@ "use strict"; | ||
const baseProperties = (0, exports.parseBaseChain)(data); | ||
const { rest, stakeCurrency, walletUrl, walletUrlForStaking, bip44, alternativeBIP44s, bech32Config, currencies, feeCurrencies, coinType, features, gasPriceStep, chainToAxelarChannelId } = data; | ||
const { rest, stakeCurrency, walletUrl, walletUrlForStaking, bip44, alternativeBIP44s, bech32Config, currencies, feeCurrencies, coinType, features, gasPriceStep, chainToAxelarChannelId, rpcList } = data; | ||
return (0, util_1.removeEmpty)({ | ||
@@ -67,3 +67,4 @@ ...baseProperties, | ||
gasPriceStep, | ||
chainToAxelarChannelId | ||
chainToAxelarChannelId, | ||
rpcList | ||
}); | ||
@@ -70,0 +71,0 @@ }; |
@@ -508,3 +508,7 @@ "use strict"; | ||
const filteredChains = new Set(chains.map(Number).filter(c => !isNaN(c))); | ||
return (0, getEvmBalances_1.getAllEvmTokensBalance)(this.tokens.filter(t => filteredChains.has(Number(t.chainId))), userAddress); | ||
const chainRpcUrls = this.chains.reduce((acc, chain) => ({ | ||
...acc, | ||
[chain.chainId]: chain.rpc | ||
}), {}); | ||
return (0, getEvmBalances_1.getAllEvmTokensBalance)(this.tokens.filter(t => filteredChains.has(Number(t.chainId))), userAddress, chainRpcUrls); | ||
} | ||
@@ -511,0 +515,0 @@ async getAllCosmosBalances({ addresses, chainIds = [] }) { |
import { TokenBalance, TokenData } from "types"; | ||
export declare const getAllEvmTokensBalance: (evmTokens: TokenData[], userAddress: string) => Promise<TokenBalance[]>; | ||
export declare const getAllEvmTokensBalance: (evmTokens: TokenData[], userAddress: string, chainRpcUrls: { | ||
[chainId: string]: string; | ||
}) => Promise<TokenBalance[]>; |
@@ -8,11 +8,6 @@ "use strict"; | ||
const CHAINS_WITHOUT_MULTICALL = [314, 3141]; // Filecoin, & Filecoin testnet | ||
const CHAINS_WITHOUT_MULTICALL_RPC_URLS = { | ||
314: "https://rpc.ankr.com/filecoin", | ||
3141: "https://rpc.ankr.com/filecoin" | ||
}; | ||
const getTokensBalanceSupportingMultiCall = async (tokens, userAddress) => { | ||
const getTokensBalanceSupportingMultiCall = async (tokens, chainRpcUrl, userAddress) => { | ||
if (!userAddress) | ||
return []; | ||
const ETHEREUM_RPC_URL = "https://eth.meowrpc.com"; | ||
const provider = new ethers_1.ethers.providers.JsonRpcProvider(ETHEREUM_RPC_URL); | ||
const provider = new ethers_1.ethers.providers.JsonRpcProvider(chainRpcUrl); | ||
const contractCallContext = tokens.map(token => { | ||
@@ -47,21 +42,29 @@ const isNativeToken = token.address.toLowerCase() === constants_1.NATIVE_EVM_TOKEN_ADDRESS.toLowerCase(); | ||
}); | ||
const { results } = (await multicallInstance.call(contractCallContext)) ?? { | ||
results: {} | ||
}; | ||
const tokenBalances = []; | ||
for (const symbol in results) { | ||
const data = results[symbol].callsReturnContext[0] ?? {}; | ||
const { decimals = 18, address = "0x" } = tokens.find(t => t.symbol === symbol) ?? {}; | ||
const mappedBalance = { | ||
symbol, | ||
address, | ||
decimals, | ||
// balance in wei | ||
balance: parseInt(data.returnValues[0]?.hex ?? "0", 16).toString() | ||
try { | ||
const { results } = (await multicallInstance.call(contractCallContext)) ?? { | ||
results: {} | ||
}; | ||
tokenBalances.push(mappedBalance); | ||
const tokenBalances = []; | ||
for (const symbol in results) { | ||
const data = results[symbol].callsReturnContext[0] ?? {}; | ||
const token = tokens.find(t => t.symbol === symbol); | ||
if (!token) | ||
continue; | ||
const { decimals, address } = token; | ||
const mappedBalance = { | ||
symbol, | ||
address, | ||
decimals, | ||
// balance in wei | ||
balance: parseInt(data.returnValues[0]?.hex ?? "0", 10).toString() | ||
}; | ||
tokenBalances.push(mappedBalance); | ||
} | ||
return tokenBalances; | ||
} | ||
return tokenBalances; | ||
catch (error) { | ||
return []; | ||
} | ||
}; | ||
const getTokensBalanceWithoutMultiCall = async (tokens, userAddress) => { | ||
const getTokensBalanceWithoutMultiCall = async (tokens, userAddress, rpcUrlsPerChain) => { | ||
const balances = await Promise.all(tokens.map(async (t) => { | ||
@@ -73,3 +76,4 @@ let balance; | ||
token: t, | ||
userAddress | ||
userAddress, | ||
rpcUrl: rpcUrlsPerChain[t.chainId] | ||
}); | ||
@@ -80,3 +84,4 @@ } | ||
token: t, | ||
userAddress | ||
userAddress, | ||
rpcUrl: rpcUrlsPerChain[t.chainId] | ||
}); | ||
@@ -93,3 +98,3 @@ } | ||
}; | ||
const getAllEvmTokensBalance = async (evmTokens, userAddress) => { | ||
const getAllEvmTokensBalance = async (evmTokens, userAddress, chainRpcUrls) => { | ||
try { | ||
@@ -109,4 +114,19 @@ // Some tokens don't support multicall, so we need to fetch them with Promise.all | ||
const tokensSupportingMulticall = splittedTokensByMultiCallSupport[1]; | ||
const tokensMulticall = await getTokensBalanceSupportingMultiCall(tokensSupportingMulticall, userAddress); | ||
const tokensNotMultiCall = await getTokensBalanceWithoutMultiCall(tokensNotSupportingMulticall, userAddress); | ||
const tokensByChainId = tokensSupportingMulticall.reduce((groupedTokens, token) => { | ||
if (!groupedTokens[token.chainId]) { | ||
groupedTokens[token.chainId] = []; | ||
} | ||
groupedTokens[token.chainId].push(token); | ||
return groupedTokens; | ||
}, {}); | ||
const tokensMulticall = []; | ||
for (const chainId in tokensByChainId) { | ||
const tokens = tokensByChainId[chainId]; | ||
const rpcUrl = chainRpcUrls[chainId]; | ||
if (!rpcUrl) | ||
continue; | ||
const tokensBalances = await getTokensBalanceSupportingMultiCall(tokens, rpcUrl, userAddress); | ||
tokensMulticall.push(...tokensBalances); | ||
} | ||
const tokensNotMultiCall = await getTokensBalanceWithoutMultiCall(tokensNotSupportingMulticall, userAddress, chainRpcUrls); | ||
return [...tokensMulticall, ...tokensNotMultiCall]; | ||
@@ -120,5 +140,5 @@ } | ||
exports.getAllEvmTokensBalance = getAllEvmTokensBalance; | ||
async function fetchBalance({ token, userAddress }) { | ||
async function fetchBalance({ token, userAddress, rpcUrl }) { | ||
try { | ||
const provider = new ethers_1.ethers.providers.JsonRpcProvider(CHAINS_WITHOUT_MULTICALL_RPC_URLS[Number(token.chainId)]); | ||
const provider = new ethers_1.ethers.providers.JsonRpcProvider(rpcUrl); | ||
const tokenAbi = ["function balanceOf(address) view returns (uint256)"]; | ||
@@ -125,0 +145,0 @@ const tokenContract = new ethers_1.ethers.Contract(token.address ?? "", tokenAbi, provider); |
@@ -124,2 +124,3 @@ import { ethers } from "ethers"; | ||
chainToAxelarChannelId: string; | ||
rpcList: string[]; | ||
}; | ||
@@ -126,0 +127,0 @@ export type ChainData = EvmChain | CosmosChain; |
{ | ||
"name": "@0xsquid/sdk", | ||
"version": "1.14.3", | ||
"version": "1.14.4", | ||
"description": "🛠 An SDK for building applications on top of 0xsquid", | ||
@@ -5,0 +5,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
130065
2304