sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Feature request: return explicit error if getBalancesFromProvider failed

Open olekon opened this issue 2 years ago • 1 comments

Now it is almost impossible to know if getBalancesFromProvider failed, as it just swallows the error, prints console warning and returns amount: 0.

const getBalancesFromProvider = async (
  walletAddress: string,
  tokens: Token[]
): Promise<TokenAmount[]> => {
  const chainId = tokens[0].chainId
  const rpc = await getRpcProvider(chainId)
  const tokenAmountPromises: Promise<TokenAmount>[] = tokens.map(
    async (token): Promise<TokenAmount> => {
      let amount = '0'
      let blockNumber

      try {
        const balance = await getBalanceFromProvider(
          walletAddress,
          token.address,
          chainId,
          rpc
        )
        amount = new BigNumber(balance.amount.toString())
          .shiftedBy(-token.decimals)
          .toString()
        blockNumber = balance.blockNumber
      } catch (e) {
        // eslint-disable-next-line no-console
        console.warn(e)
      }

      return {
        ...token,
        amount,
        blockNumber,
      }
    }
  )
  return Promise.all(tokenAmountPromises)
}

Can we somehow indicate that there was an error? For example return Error object or set amount to null in that case?

olekon avatar May 15 '23 07:05 olekon

Hi, this was made not to throw whenever RPCs have some issue because the next try can be successful, but maybe we should reconsider how we handle this.

chybisov avatar May 16 '23 11:05 chybisov

The SDK v3 has been released. Please see the documentation here https://docs.li.fi/integrate-li.fi-sdk/li.fi-sdk-overview

chybisov avatar Jun 27 '24 14:06 chybisov