-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: try to get colony creation event
- Loading branch information
Showing
7 changed files
with
188 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters