Skip to content

Commit

Permalink
Merge pull request #277 from JoinColony/negative-balance-logs
Browse files Browse the repository at this point in the history
Temp: Prevent expenditure balance from going below 0, add logs
  • Loading branch information
jakubcolony authored Oct 9, 2024
2 parents 37695cc + 1d00496 commit 3817ae3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/handlers/actions/moveFunds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getUpdatedExpenditureBalances,
transactionHasEvent,
getExpenditureByFundingPot,
verbose,
} from '~utils';
import {
ColonyActionType,
Expand Down Expand Up @@ -85,6 +86,11 @@ export default async (event: ContractEvent): Promise<void> => {

if (targetExpenditure) {
await updateExpenditureBalances(targetExpenditure, tokenAddress, amount);
} else {
// @NOTE: Temporary log until issue with negative balances is resolved
verbose(
`Attempted to find expenditure with funding pot ID: ${toPotId} in colony ${colonyAddress} but it wasn't found. It may be because the transfer was to a domain, or there was a bug.`,
);
}
};

Expand All @@ -94,7 +100,7 @@ const updateExpenditureBalances = async (
amount: string,
): Promise<void> => {
const updatedBalances = getUpdatedExpenditureBalances(
expenditure.balances ?? [],
expenditure,
tokenAddress,
amount,
);
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/expenditures/expenditurePayoutClaimed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default async (event: ContractEvent): Promise<void> => {
).toString();

const updatedBalances = getUpdatedExpenditureBalances(
expenditure.balances ?? [],
expenditure,
tokenAddress,
amountWithFee,
true,
Expand Down
29 changes: 27 additions & 2 deletions src/utils/expenditures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
GetExpenditureByNativeFundingPotIdAndColonyQueryVariables,
} from '~graphql';

import { output } from './logger';
import { insertAtIndex } from './arrays';

export const getExpenditureDatabaseId = (
Expand All @@ -26,18 +27,42 @@ export const getExpenditureDatabaseId = (
};

export const getUpdatedExpenditureBalances = (
balances: ExpenditureBalance[],
expenditure: ExpenditureFragment,
tokenAddress: string,
amount: string,
subtract: boolean = false,
): ExpenditureBalance[] => {
const balances = expenditure.balances ?? [];

const balanceIndex = balances.findIndex(
(balance) => balance.tokenAddress === tokenAddress,
);
const balance = balanceIndex !== -1 ? balances[balanceIndex] : undefined;

const amountToAdd = BigNumber.from(amount).mul(subtract ? -1 : 1);
const updatedAmount = BigNumber.from(balance?.amount ?? 0).add(amountToAdd);
let updatedAmount = BigNumber.from(balance?.amount ?? 0).add(amountToAdd);

if (updatedAmount.lt(0)) {
/**
* @NOTE: This is a temporary fix to prevent negative balances
* The current theory is that when the move funds event is picked up
* straight after the expenditure was created, it does not
* get returned by DynamoDB yet
*/
output(
`Balance of expenditure ${expenditure.id} for token ${tokenAddress} would go negative. This is a bug and needs investigating.`,
);
updatedAmount = BigNumber.from(0);

if (process.env.NODE_ENV !== 'development') {
fetch('https://hooks.zapier.com/hooks/catch/20362233/2mnk4h2', {
method: 'POST',
body: JSON.stringify({
message: `Balance of expenditure ${expenditure.id} for token ${tokenAddress} would go negative. This is a bug and needs investigating.`,
}),
});
}
}

const updatedBalance = {
...balance,
Expand Down

0 comments on commit 3817ae3

Please sign in to comment.