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

Add support for MoveFunds action #22

Merged
merged 2 commits into from
Mar 6, 2023
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
6 changes: 6 additions & 0 deletions src/eventProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
handleExtensionInitialised,
handleCreateDomainAction,
handleTokenUnlockedAction,
handleMoveFundsAction,
} from './handlers';

dotenv.config();
Expand Down Expand Up @@ -128,6 +129,11 @@ export default async (event: ContractEvent): Promise<void> => {
return;
}

case ContractEventsSignatures.ColonyFundsMovedBetweenFundingPots: {
await handleMoveFundsAction(event);
return;
}

default: {
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/handlers/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { default as handleMintTokensAction } from './mintTokens';
export { default as handlePaymentAction } from './payment';
export { default as handleCreateDomainAction } from './createDomain';
export { default as handleTokenUnlockedAction } from './unlockToken';
export { default as handleMoveFundsAction } from './moveFunds';
58 changes: 58 additions & 0 deletions src/handlers/actions/moveFunds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
AnyColonyClient,
ColonyClientV1,
ColonyClientV2,
ColonyClientV3,
ColonyClientV4,
} from '@colony/colony-js';
import { BigNumber } from 'ethers';
import networkClient from '~/networkClient';
import { ColonyActionType, ContractEvent } from '~/types';
import { toNumber, writeActionFromEvent } from '~/utils';
import { getDatabaseDomainId } from '~/utils/domains';

/**
* The handler makes use of colonyClient getDomainFromFundingPot method which is only
* available on ColonyClientV5 and above. The following type predicate allows to check
* we're dealing with a client that supports this method
*/
type SupportedColonyClient = Exclude<
AnyColonyClient,
ColonyClientV1 | ColonyClientV2 | ColonyClientV3 | ColonyClientV4
>;
const isSupportedColonyClient = (
colonyClient: AnyColonyClient,
): colonyClient is SupportedColonyClient =>
(colonyClient as SupportedColonyClient).getDomainFromFundingPot !== undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

alternatively:

'getDomainFromFundingPot' in colonyClient

Then you don't need to cast colonyClient

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think explicit casting might be better here since it will alert you if the property you're checking against doesn't exist/got removed
https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates


export default async (event: ContractEvent): Promise<void> => {
const { contractAddress: colonyAddress } = event;
const {
agent: initiatorAddress,
token: tokenAddress,
amount,
fromPot,
toPot,
} = event.args;

const colonyClient = await networkClient.getColonyClient(colonyAddress);
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we could optimize the ingestor somewhat if we were to store all known instances of the colony client.

I see us re-instantiating the client for a lot of tracked events.

But I actually don't know what this impact would be real-life.

let fromDomainId: BigNumber | undefined;
let toDomainId: BigNumber | undefined;
if (isSupportedColonyClient(colonyClient)) {
fromDomainId = await colonyClient.getDomainFromFundingPot(fromPot);
toDomainId = await colonyClient.getDomainFromFundingPot(toPot);
}

await writeActionFromEvent(event, colonyAddress, {
type: ColonyActionType.MoveFunds,
initiatorAddress,
tokenAddress,
amount: amount.toString(),
fromDomainId: fromDomainId
? getDatabaseDomainId(colonyAddress, toNumber(fromDomainId))
: undefined,
toDomainId: toDomainId
? getDatabaseDomainId(colonyAddress, toNumber(toDomainId))
: undefined,
});
};
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export enum ContractEventsSignatures {
OneTxPaymentMade = 'OneTxPaymentMade(address,uint256,uint256)',
DomainAdded = 'DomainAdded(address,uint256)',
TokenUnlocked = 'TokenUnlocked(address)',
ColonyFundsMovedBetweenFundingPots = 'ColonyFundsMovedBetweenFundingPots(address,uint256,uint256,uint256,address)',
}

/*
Expand All @@ -69,4 +70,5 @@ export enum ColonyActionType {
Payment = 'PAYMENT',
CreateDomain = 'CREATE_DOMAIN',
UnlockToken = 'UNLOCK_TOKEN',
MoveFunds = 'MOVE_FUNDS',
}
4 changes: 4 additions & 0 deletions src/utils/actions/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ export const addActionEventListeners = async (
ContractEventsSignatures.TokenUnlocked,
colonyAddress,
);
await addColonyEventListener(
ContractEventsSignatures.ColonyFundsMovedBetweenFundingPots,
colonyAddress,
);
};