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

Fix: Fixes for fetchDomainBalance lambda edge cases #3665

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const EnvVarsConfig = require('./envVars.js');
const { SupportedNetwork } = require('../consts.js');

const {
ColonyJSNetworkMapping,
Expand All @@ -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],
};
},
};
Expand Down
20 changes: 20 additions & 0 deletions amplify/backend/function/fetchDomainBalance/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional or a leftover 👀 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional, so we can get some more information if there is an error in a specific call

`Date for ${timeframePeriod} timeframe, ` +
`${timeframeType} type, and ` +
`${timeframePeriodEndDate} end date: ` +
`${periodForTimeframe}`,
);

const inOutActions = await getInOutActions(colonyAddress, domainId);
const inOutActionsWithinTimeframe = filterActionsWithinTimeframe(
inOutActions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions amplify/backend/function/fetchDomainBalance/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,34 @@ 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)))),
);
};

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) => {
Expand Down
Loading