Skip to content

Commit

Permalink
feat: try to get colony creation event
Browse files Browse the repository at this point in the history
  • Loading branch information
bassgeta committed Dec 12, 2024
1 parent 4c26eff commit 8172e1e
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 25 deletions.
15 changes: 13 additions & 2 deletions amplify/backend/api/colonycdapp/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,17 @@ type ColonyContributor @model @searchable {
@hasMany(indexName: "byTargetAddress", fields: ["contributorAddress"])
}

type ColonyCreateEvent {
"""
The block number the colony was created at
"""
blockNumber: Int!
"""
The address that signed the transaction
"""
signer: String!
}

"""
Represents a Colony within the Colony Network
"""
Expand All @@ -1546,9 +1557,9 @@ type Colony @model {
"""
name: String! @index(name: "byName", queryField: "getColonyByName")
"""
The block number the colony was created attached
Colony creation data
"""
createdAtBlock: Int
colonyCreateEvent: ColonyCreateEvent
"""
The unique address of the native token of the Colony
"""
Expand Down
126 changes: 126 additions & 0 deletions scripts/getColonyCreatedBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const { graphqlRequest } = require('./utils/graphqlRequest');
const { providers, utils } = require('ethers');

const API_KEY = 'da2-fakeApiId123456';
const GRAPHQL_URI =
process.env.AWS_APPSYNC_GRAPHQL_URL || 'http://localhost:20002/graphql';
const RPC_URL = 'http://localhost:8545';

const getColonies = /* GraphQL */ `
query GetColonies($nextToken: String, $limit: Int) {
listColonies(nextToken: $nextToken, limit: $limit) {
items {
id
colonyCreateEvent {
blockNumber
signer
}
}
}
}
`;

const updateColony = /* GraphQL */ `
mutation UpdateColony($input: UpdateColonyInput!) {
updateColony(input: $input) {
id
}
}
`;

const getAllPages = async (getData, params) => {
let items = [];
let nextToken = null;

do {
const actionsData = await getData({ ...params, nextToken, limit: 1000 });
nextToken = actionsData?.nextToken;
if (actionsData?.items) {
items.push(...actionsData.items);
}
} while (nextToken);

return items;
};

const getColoniesData = async ({ limit, nextToken }) => {
const result = await graphqlRequest(
getColonies,
{
limit,
nextToken,
},
GRAPHQL_URI,
API_KEY,
);

if (!result) {
console.warn('Could not find any colonies in db.');
}

return result.data.listColonies;
};

const getAllColonies = async () => {
return getAllPages(getColoniesData);
};

const updateColonyCreateEvent = async (colonyAddress, blockNumber, signer) => {
await graphqlRequest(
updateColony,
{
input: {
id: colonyAddress,
colonyCreateEvent: {
blockNumber,
signer,
},
},
},
GRAPHQL_URI,
API_KEY,
);
};

const eventSignature = 'ContractCreation(address)';
const eventTopic = utils.id(eventSignature);

async function getColonyCreationBlock() {
try {
const provider = new providers.StaticJsonRpcProvider(RPC_URL);

const allColonies = await getAllColonies();

allColonies.forEach(async (colony) => {
if (colony.colonyCreateEvent) {
return;
}

const topicAddress = '0x' + colony.id.slice(2).padStart(64, '0');

const logs = await provider.getLogs({
fromBlock: 0,
topics: [eventTopic, topicAddress],
});

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

const matchingLog = logs[0];

await updateColonyCreateEvent(
colony.id,
matchingLog.blockNumber,
matchingLog.address,
);
});
} catch (error) {
console.error('Error while getting colony creation block', error);
}
}

getColonyCreationBlock();
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ export const TmpProxyColonyDeploy = () => {
});

const handleClick = async () => {
if (!colony.colonyCreateEvent) {
console.warn('No colony creation data');
return;
}

await createProxyColony({
colonyAddress: colony.colonyAddress,
createdAtBlock: colony.createdAtBlock,
blockNumber: colony.colonyCreateEvent.blockNumber,
signer: colony.colonyCreateEvent.signer,
foreignChainId: chainId,
});
};
Expand Down
5 changes: 4 additions & 1 deletion src/graphql/fragments/colony.graphql
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
fragment Colony on Colony {
colonyAddress: id
name
createdAtBlock
colonyCreateEvent {
blockNumber
signer
}
nativeToken {
...Token
}
Expand Down
49 changes: 31 additions & 18 deletions src/graphql/generated.ts

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/redux/sagas/proxyColonies/createProxyColony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export type CreateProxyColonyPayload =

// @TODO if metatx are enabled sent a metaTx instead of tx
function* createProxyColony({
payload: { colonyAddress, createdAtBlock, foreignChainId },
// eslint-disable-next-line @typescript-eslint/no-unused-vars
payload: { colonyAddress, blockNumber, signer, foreignChainId },
meta,
}: Action<ActionTypes.PROXY_COLONY_CREATE>) {
const batchKey = TRANSACTION_METHODS.CreateProxyColony;
Expand All @@ -40,7 +41,9 @@ function* createProxyColony({

try {
const colonyCreationSalt = yield networkClient.getColonyCreationSalt({
blockTag: createdAtBlock,
blockTag: blockNumber,
// @TODO this doesn't work, we need to parse it differently
// from: signer,
});

const proxyColonyContract: CustomContract<typeof colonyAbi> =
Expand Down
3 changes: 2 additions & 1 deletion src/redux/types/actions/proxyColonies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export type ProxyColoniesActionTypes =
ActionTypes.PROXY_COLONY_CREATE,
{
colonyAddress: Address;
createdAtBlock: number;
blockNumber: number;
signer: Address;
foreignChainId: number;
},
object
Expand Down

0 comments on commit 8172e1e

Please sign in to comment.