Skip to content

Commit

Permalink
Feat: Add create event details to colony model
Browse files Browse the repository at this point in the history
Feat: Add create event details to colony model
  • Loading branch information
mmioana committed Dec 12, 2024
1 parent 6b1d83e commit 754ae12
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
11 changes: 10 additions & 1 deletion apps/main-chain/src/handlers/colonies/colonyAdded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getColonyContributorId } from '~utils/contributors';
import { tryFetchGraphqlQuery } from '~utils/graphql';
import { createUniqueColony } from './helpers/createUniqueColony';
import { output } from '@joincolony/utils';
import { getCreateSaltEventFromTransaction } from '~utils/colony';

export default async (event: ContractEvent): Promise<void> => {
const { transactionHash, args, blockNumber } = event;
Expand Down Expand Up @@ -77,6 +78,11 @@ export default async (event: ContractEvent): Promise<void> => {
args: { user: '' },
};

const createSaltEvent = await getCreateSaltEventFromTransaction(
blockNumber,
colonyAddress,
);

try {
/*
* Create the colony entry in the database
Expand All @@ -86,7 +92,10 @@ export default async (event: ContractEvent): Promise<void> => {
tokenAddress: utils.getAddress(tokenAddress),
transactionHash,
initiatorAddress: utils.getAddress(colonyFounderAddress),
createdAtBlock: blockNumber,
colonyCreateEvent: {
blockNumber,
salt: createSaltEvent?.args?.salt,
},
});
} catch (error) {
console.error(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
GetTokenFromEverywhereDocument,
GetTokenFromEverywhereQuery,
GetTokenFromEverywhereQueryVariables,
ColonyCreateEvent,
} from '@joincolony/graphql';
import rpcProvider from '~provider';
import { getCachedColonyClient } from '~utils/clients/colony';
Expand All @@ -47,13 +48,13 @@ export const createUniqueColony = async ({
tokenAddress,
transactionHash,
initiatorAddress,
createdAtBlock,
colonyCreateEvent,
}: {
colonyAddress: string;
tokenAddress: string;
transactionHash: string;
initiatorAddress: string;
createdAtBlock: number;
colonyCreateEvent: ColonyCreateEvent;
}): Promise<void> => {
/*
* Validate Colony and Token addresses
Expand Down Expand Up @@ -217,7 +218,7 @@ export const createUniqueColony = async ({
chainMetadata: {
chainId,
},
createdAtBlock,
colonyCreateEvent,
version: version.toNumber(),
status: {
nativeToken: {
Expand Down
37 changes: 37 additions & 0 deletions apps/main-chain/src/utils/colony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import {
ListColoniesWithRootPermissionHoldersQuery,
ListColoniesWithRootPermissionHoldersQueryVariables,
} from '@joincolony/graphql';
import {
ContractCreateSaltEvents,
ContractEvent,
ContractEventsSignatures,
} from '@joincolony/blocks';
import { notNull } from './arrays';
import { getAllPagesOfData, GetDataFn } from './graphql';
import { mapLogToContractEvent } from './events';
import rpcProvider from '~provider';
import { utils } from 'ethers';

const getColoniesData: GetDataFn<
ColonyWithRootRolesFragment,
Expand All @@ -29,3 +37,32 @@ export const getAllColoniesWithRootPermissionHolders = async (): Promise<

return allColonies.filter(notNull);
};

export const getCreateSaltEventFromTransaction = async (
blockNumber: number,
colonyAddress: string,
): Promise<ContractEvent | undefined> => {
const createSaltLogs = await rpcProvider.getProviderInstance().getLogs({
fromBlock: blockNumber,
toBlock: blockNumber,
topics: [utils.id(ContractEventsSignatures.Create3ProxyContractCreation)],
});

if (createSaltLogs.length === 0) {
console.error(
`Couldn't fetch colony salt creation event for colonyAddress: ${colonyAddress}`,
);
return;
}

const event = await mapLogToContractEvent(
createSaltLogs[0],
ContractCreateSaltEvents,
);

/*
* Typecasting since apparently TS doesn't realize we are actually filtering
* to ensure that the Array only contains proper events
*/
return event as ContractEvent;
};
4 changes: 4 additions & 0 deletions packages/blocks/src/events/eventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export const ProxyColonyEvents = new utils.Interface([
'event LogMessagePublished(address indexed sender,uint64 sequence,uint32 nonce,bytes payload,uint8 consistencyLevel)',
]);

export const ContractCreateSaltEvents = new utils.Interface([
`event Create3ProxyContractCreation(address indexed newContract, bytes32 indexed salt)`,
]);

export class EventManager {
private listeners: EventListener[] = [];
private readonly rpcProvider: RpcProvider;
Expand Down
3 changes: 3 additions & 0 deletions packages/blocks/src/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export enum ContractEventsSignatures {
ProxyColonyDeployed = 'ProxyColonyDeployed(address)',
LogMessagePublished = 'LogMessagePublished(address,uint64,uint32,bytes,uint8)',
WormholeMessageReceived = 'WormholeMessageReceived(uint16,bytes32,uint64)',

// Contract create events
Create3ProxyContractCreation = 'Create3ProxyContractCreation(address,bytes32)',
}

/*
Expand Down
24 changes: 17 additions & 7 deletions packages/graphql/src/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ export type Colony = {
chainFundsClaim?: Maybe<ColonyChainFundsClaim>;
/** Metadata related to the chain of the Colony */
chainMetadata: ChainMetadata;
/** Colony creation data */
colonyCreateEvent?: Maybe<ColonyCreateEvent>;
/**
* The main member invite object
* It is possible to create multiple member invites for a given colony
Expand All @@ -310,8 +312,6 @@ export type Colony = {
/** ID of the main member invite object */
colonyMemberInviteCode?: Maybe<Scalars['ID']>;
createdAt: Scalars['AWSDateTime'];
/** The block number the colony was created attached */
createdAtBlock?: Maybe<Scalars['Int']>;
domains?: Maybe<ModelDomainConnection>;
expenditures?: Maybe<ModelExpenditureConnection>;
/** Global claim delay for expenditures (in seconds) */
Expand Down Expand Up @@ -835,6 +835,19 @@ export type ColonyContributorRolesArgs = {
sortDirection?: InputMaybe<ModelSortDirection>;
};

export type ColonyCreateEvent = {
__typename?: 'ColonyCreateEvent';
/** The block number the colony was created at */
blockNumber: Scalars['Int'];
/** The address that signed the transaction */
salt: Scalars['String'];
};

export type ColonyCreateEventInput = {
blockNumber: Scalars['Int'];
salt: Scalars['String'];
};

export type ColonyDecision = {
__typename?: 'ColonyDecision';
action?: Maybe<ColonyAction>;
Expand Down Expand Up @@ -1542,8 +1555,8 @@ export type CreateColonyInput = {
balances?: InputMaybe<ColonyBalancesInput>;
chainFundsClaim?: InputMaybe<ColonyChainFundsClaimInput>;
chainMetadata: ChainMetadataInput;
colonyCreateEvent?: InputMaybe<ColonyCreateEventInput>;
colonyMemberInviteCode?: InputMaybe<Scalars['ID']>;
createdAtBlock?: InputMaybe<Scalars['Int']>;
expendituresGlobalClaimDelay?: InputMaybe<Scalars['String']>;
id?: InputMaybe<Scalars['ID']>;
lastUpdatedContributorsWithReputation?: InputMaybe<Scalars['AWSDateTime']>;
Expand Down Expand Up @@ -2983,7 +2996,6 @@ export type ModelColonyActionTypeInput = {
export type ModelColonyConditionInput = {
and?: InputMaybe<Array<InputMaybe<ModelColonyConditionInput>>>;
colonyMemberInviteCode?: InputMaybe<ModelIdInput>;
createdAtBlock?: InputMaybe<ModelIntInput>;
expendituresGlobalClaimDelay?: InputMaybe<ModelStringInput>;
lastUpdatedContributorsWithReputation?: InputMaybe<ModelStringInput>;
name?: InputMaybe<ModelStringInput>;
Expand Down Expand Up @@ -3110,7 +3122,6 @@ export type ModelColonyExtensionFilterInput = {
export type ModelColonyFilterInput = {
and?: InputMaybe<Array<InputMaybe<ModelColonyFilterInput>>>;
colonyMemberInviteCode?: InputMaybe<ModelIdInput>;
createdAtBlock?: InputMaybe<ModelIntInput>;
expendituresGlobalClaimDelay?: InputMaybe<ModelStringInput>;
id?: InputMaybe<ModelIdInput>;
lastUpdatedContributorsWithReputation?: InputMaybe<ModelStringInput>;
Expand Down Expand Up @@ -4369,7 +4380,6 @@ export type ModelSubscriptionColonyExtensionFilterInput = {
export type ModelSubscriptionColonyFilterInput = {
and?: InputMaybe<Array<InputMaybe<ModelSubscriptionColonyFilterInput>>>;
colonyMemberInviteCode?: InputMaybe<ModelSubscriptionIdInput>;
createdAtBlock?: InputMaybe<ModelSubscriptionIntInput>;
expendituresGlobalClaimDelay?: InputMaybe<ModelSubscriptionStringInput>;
id?: InputMaybe<ModelSubscriptionIdInput>;
lastUpdatedContributorsWithReputation?: InputMaybe<ModelSubscriptionStringInput>;
Expand Down Expand Up @@ -9447,8 +9457,8 @@ export type UpdateColonyInput = {
balances?: InputMaybe<ColonyBalancesInput>;
chainFundsClaim?: InputMaybe<ColonyChainFundsClaimInput>;
chainMetadata?: InputMaybe<ChainMetadataInput>;
colonyCreateEvent?: InputMaybe<ColonyCreateEventInput>;
colonyMemberInviteCode?: InputMaybe<Scalars['ID']>;
createdAtBlock?: InputMaybe<Scalars['Int']>;
expendituresGlobalClaimDelay?: InputMaybe<Scalars['String']>;
id: Scalars['ID'];
lastUpdatedContributorsWithReputation?: InputMaybe<Scalars['AWSDateTime']>;
Expand Down

0 comments on commit 754ae12

Please sign in to comment.