Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QA Dashboard] Total funds bug #3555

Closed
mmioana opened this issue Oct 30, 2024 · 4 comments · Fixed by #3614 or #3640
Closed

[QA Dashboard] Total funds bug #3555

mmioana opened this issue Oct 30, 2024 · 4 comments · Fixed by #3614 or #3640
Assignees
Labels
bug Something isn't working

Comments

@mmioana
Copy link
Contributor

mmioana commented Oct 30, 2024

Steps to reproduce

  1. Login on QA to the colony with address 0x997B915CaC293D69f14a8a85e660EE87f1523FcD - Jakub Colony

  2. On dashboard you'll see the balance has a small negative value
    Screenshot 2024-10-30 at 12 37 53

  3. However, if you switch to the finances pages, on the balance you'll notice the total ETH is positive
    Screenshot 2024-10-30 at 12 38 04
    But there is no incoming funds - in the past once an incoming funds was claimed, the entry was deleted
    Screenshot 2024-10-30 at 12 38 17

  4. Also, another bug related to the funds, was found while scraping through the getDomainBalance query: if for the timeframePeriod you enter a number of days > than the number of days in a month (considering you selected TimeframeType.DAILY), as well as if you enter a number of months > than the number of months in a year (considering you selected TimeframeType.MONTHLY), you will not have an entry for each expected key.
    So the solution would be to use the entire date yyyy-mm-dd as key and then adjust the displayed key in the UI.

Screen.Recording.2024-10-30.at.11.46.59.mov

Implementation details

Given the incoming funds entries are not available for prior to May 2024 - previously when an incoming funds action was claimed, the entry got deleted, we need to change a bit our approach to the balances.

So, in order to get our data right, at least for the overall amounts shown in the Total cards we need to make use of the actual network balance at colony level or domain level.

For this please make use of the following code put in place by @Nortsova in the UI

const calculateTotalFunds = async (
  balances: ColonyBalances,
  currency: SupportedCurrencies,
  selectedDomainNativeId: number | undefined,
): Promise<Decimal | null> => {
  let isError = false;
  const funds = balances.items
    ?.filter(notNull)
    .filter(({ domain }) =>
      !selectedDomainNativeId
        ? domain === null
        : domain?.nativeId === selectedDomainNativeId,
    )
    .reduce(
      async (total, { balance, token: { tokenAddress, decimals } }) => {
        const currentPrice = await fetchCurrentPrice({
          contractAddress: tokenAddress,
          conversionDenomination: currency,
        });

        if (currentPrice == null) {
          isError = true;
        }

        const balanceInWeiToEth = new Decimal(balance).div(10 ** decimals);

        return (await total).add(
          new Decimal(balanceInWeiToEth).mul(currentPrice ?? 0),
        );
      },
      Promise.resolve(new Decimal(0)),
    );

  if (isError) {
    return null;
  }

  return (await funds) ?? new Decimal(0);
};

export const useTotalFunds = () => {
  const selectedDomain = useGetSelectedDomainFilter();
  const { colony } = useColonyContext();
  const { currency } = useCurrencyContext();
  const { balances: colonyBalances } = colony || {};
  const [totalFunds, setTotalFunds] = useState<Decimal | null>(new Decimal(0));

  useEffect(() => {
    const getTotalFunds = async (balances: ColonyBalances) => {
      const funds = await calculateTotalFunds(
        balances,
        currency,
        selectedDomain?.nativeId,
      );
      setTotalFunds(funds);
    };

    if (colonyBalances) {
      getTotalFunds(colonyBalances);
    }
  }, [colonyBalances, currency, selectedDomain]);

  return totalFunds;
};

Also, for the caching of the total funds we should make a call to the network and estimate the block for which we want to retrieve the data. So in the cacheDomainBalance for the Total entry we should make use of this code

const networkClient = getColonyNetworkClient(network, provider, {
      networkAddress,
    });

const colonyClient = await networkClient.getColonyClient(colonyAddress);

For domain level balance, use the nativeFundingPotId

const { nativeId, nativeFundingPotId } = domain;


const domainBalance = await colonyClient.getFundingPotBalance(
          nativeFundingPotId,
          AddressZero,
        );

To estimate the block number from a given point in time to retrieve the total balance value, we should consider that each chain has a constant block time (0.25s for Arbitrum), so to find a block n seconds ago the following formula can be used:
currentBlockNumber - n/0.25

Now for the total in/out balance given we want to cache the value for the timeframe 60 days - 30 days, we can leave the logic how it is, at least for the moment.

And if we're touching all the domain balances, we should also fix the issue with the timeframe keys that can get overridden.

Expected behaviour

  • We need to get the correct total values
  • Also the getDomainBalance query should retrieve the timeframe containing all relevant keys - this however doesn't or shouldn't impact the values for the moment, as we don't query more than 30 days or more than 4 months

Actual behaviour

  • We don't get the right total values
  • We don't get all relevant timeframe keys if we were to change our timeframePeriod query input
@mmioana mmioana added bug Something isn't working needs more info The issue requires more information in order to be resolved. labels Oct 30, 2024
@mmioana
Copy link
Contributor Author

mmioana commented Oct 30, 2024

I'll schedule a brain storming session with @jakubcolony first to see what's the best approach for retrieving these values

@mmioana mmioana added priority and removed needs more info The issue requires more information in order to be resolved. labels Oct 31, 2024
@rdig
Copy link
Member

rdig commented Nov 1, 2024

What came of the call here? How are you proceeding?

Since this is a bug that affects Dashboard it needs to be prioritized

@mmioana
Copy link
Contributor Author

mmioana commented Nov 1, 2024

@rdig I put all the comments from our session in the Implementation details

@mmioana mmioana self-assigned this Nov 1, 2024
@rdig
Copy link
Member

rdig commented Nov 1, 2024

Ah. Missed those. All good then! 👍

@rdig rdig linked a pull request Nov 5, 2024 that will close this issue
1 task
@mmioana mmioana linked a pull request Nov 5, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants