diff --git a/amplify/backend/function/fetchDomainBalance/src/config/networkConfig.js b/amplify/backend/function/fetchDomainBalance/src/config/networkConfig.js index 389cabeabf..2e98c645c3 100644 --- a/amplify/backend/function/fetchDomainBalance/src/config/networkConfig.js +++ b/amplify/backend/function/fetchDomainBalance/src/config/networkConfig.js @@ -1,4 +1,5 @@ const EnvVarsConfig = require('./envVars.js'); +const { SupportedNetwork } = require('../consts.js'); const { ColonyJSNetworkMapping, @@ -10,11 +11,14 @@ const NetworkConfig = (() => { return { getConfig: async () => { const { network } = await EnvVarsConfig.getEnvVars(); - const supportedNetwork = ColonyJSNetworkMapping[network]; + const supportedNetwork = ColonyJSNetworkMapping[network] || network; return { - DEFAULT_NETWORK_TOKEN: TOKEN_DATA[supportedNetwork], - DEFAULT_NETWORK_INFO: NETWORK_DATA[supportedNetwork], + DEFAULT_NETWORK_TOKEN: + TOKEN_DATA[supportedNetwork] ?? TOKEN_DATA[SupportedNetwork.Ganache], + DEFAULT_NETWORK_INFO: + NETWORK_DATA[supportedNetwork] ?? + NETWORK_DATA[SupportedNetwork.Ganache], }; }, }; diff --git a/amplify/backend/function/fetchDomainBalance/src/index.js b/amplify/backend/function/fetchDomainBalance/src/index.js index 8508199ffd..341c4cbadd 100644 --- a/amplify/backend/function/fetchDomainBalance/src/index.js +++ b/amplify/backend/function/fetchDomainBalance/src/index.js @@ -32,12 +32,32 @@ exports.handler = async (event) => { timeframeType, timeframePeriodEndDate, } = event.arguments?.input || {}; + + /** + * We want to early return if there is no positive period for which to compute values + */ + if (timeframePeriod <= 0) { + return { + totalIn: 0, + totalOut: 0, + total: 0, + timeframe: [], + }; + } + const periodForTimeframe = getPeriodFor( timeframePeriod, timeframeType, timeframePeriodEndDate, ); + console.log( + `Date for ${timeframePeriod} timeframe, ` + + `${timeframeType} type, and ` + + `${timeframePeriodEndDate} end date: ` + + `${periodForTimeframe}`, + ); + const inOutActions = await getInOutActions(colonyAddress, domainId); const inOutActionsWithinTimeframe = filterActionsWithinTimeframe( inOutActions, diff --git a/amplify/backend/function/fetchDomainBalance/src/services/actions.js b/amplify/backend/function/fetchDomainBalance/src/services/actions.js index e02f53d7c4..d227ae7b8a 100644 --- a/amplify/backend/function/fetchDomainBalance/src/services/actions.js +++ b/amplify/backend/function/fetchDomainBalance/src/services/actions.js @@ -40,7 +40,9 @@ const getInOutActions = async (colonyAddress, domainId) => { ...getFormattedIncomingFunds(incomingFunds, domainId), ...getFormattedActions(actions, domainId), ...getFormattedExpenditures(filteredExpenditures, domainId, tokensDecimals), - ]; + ].filter( + (action) => !!action.token && (!!action.amount || !!action.networkFee), + ); }; const getTokensDatesMap = (actions) => { @@ -109,6 +111,11 @@ const groupBalanceByPeriod = ( actions.forEach((action) => { const period = getFullPeriodFormat(action.finalizedDate, timeframeType); + if (!balance[period]) { + console.warn(`No balance entry configured for period ${period}`); + return; + } + // If we are at colony level and the action has a type // The action is among the acceptedColonyActionTypes and must be an outgoing source of funds if (!domainId && action.type) { diff --git a/amplify/backend/function/fetchDomainBalance/src/utils.js b/amplify/backend/function/fetchDomainBalance/src/utils.js index 27c89280e0..3e93bd1102 100644 --- a/amplify/backend/function/fetchDomainBalance/src/utils.js +++ b/amplify/backend/function/fetchDomainBalance/src/utils.js @@ -69,17 +69,17 @@ const subtractYearsFor = (numberOfYears, timeframePeriodEndDate) => { const now = timeframePeriodEndDate ? new Date(timeframePeriodEndDate) : new Date(); - return startOfDay( - new Date(startOfYear(new Date(subYears(now, numberOfYears)))), - ); + const timeOffset = numberOfYears > 0 ? numberOfYears - 1 : 0; + return startOfDay(new Date(startOfYear(new Date(subYears(now, timeOffset))))); }; const subtractMonthsFor = (numberOfMonths, timeframePeriodEndDate) => { const now = timeframePeriodEndDate ? new Date(timeframePeriodEndDate) : new Date(); + const timeOffset = numberOfMonths > 0 ? numberOfMonths - 1 : 0; return startOfDay( - new Date(startOfMonth(new Date(subMonths(now, numberOfMonths)))), + new Date(startOfMonth(new Date(subMonths(now, timeOffset)))), ); }; @@ -87,16 +87,16 @@ const subtractWeeksFor = (numberOfWeeks, timeframePeriodEndDate) => { const now = timeframePeriodEndDate ? new Date(timeframePeriodEndDate) : new Date(); - return startOfDay( - new Date(startOfWeek(new Date(subWeeks(now, numberOfWeeks)))), - ); + const timeOffset = numberOfWeeks > 0 ? numberOfWeeks - 1 : 0; + return startOfDay(new Date(startOfWeek(new Date(subWeeks(now, timeOffset))))); }; const subtractDaysFor = (numberOfDays, timeframePeriodEndDate) => { const now = timeframePeriodEndDate ? new Date(timeframePeriodEndDate) : new Date(); - return startOfDay(new Date(subDays(now, numberOfDays))); + const timeOffset = numberOfDays > 0 ? numberOfDays - 1 : 0; + return startOfDay(new Date(subDays(now, timeOffset))); }; const getFullPeriodFormat = (date, timeframeType) => {