Skip to content

Commit

Permalink
fix(registry): fix list packages script (#1603)
Browse files Browse the repository at this point in the history
This PR does not change any critical part, just updates the
`script/list-packages` script used to list all the published packages
directly from the registry events.
  • Loading branch information
mjlescano authored Dec 27, 2024
2 parents bc73701 + b518552 commit a3f42a5
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 108 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export function getProvider(expectedAnvilInstance: ChildProcess): typeof anvilPr

export function createProviderProxy(provider: viem.Client): Promise<string> {
return new Promise((resolve) => {
const server = http.createServer(async (req, res) => {
const server = http.createServer(async (req: any, res: any) => {
res.setHeader('Content-Type', 'application/json');
const reqJson = JSON.parse(await streamToString(req));

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/util/on-keypress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ interface Controls {
export default function onKeypress(handleKeyPress: (evt: KeyboardEvent, controls: Controls) => void) {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
input: process.stdin as unknown as any,
escapeCodeTimeout: 50,
});

readline.emitKeypressEvents(process.stdin, rl);
readline.emitKeypressEvents(process.stdin as unknown as any, rl);
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/registry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@typechain/ethers-v5": "10.1.0",
"@typechain/hardhat": "6.1.2",
"@types/mocha": "9.1.1",
"@types/node": "18.0.0",
"@types/node": "22.7.5",
"@usecannon/builder": "workspace:*",
"@usecannon/cli": "workspace:*",
"at-least-node": "^1.0.0",
Expand All @@ -40,6 +40,7 @@
"solidity-coverage": "0.7.21",
"ts-node": "10.8.1",
"typechain": "8.1.0",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"viem": "^2.21.15"
}
}
79 changes: 50 additions & 29 deletions packages/registry/scripts/list-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,82 @@

/* eslint-disable no-console */

import 'dotenv/config';
import { DEFAULT_REGISTRY_ADDRESS } from '@usecannon/builder';
import CannonRegistryAbi from '@usecannon/builder/dist/src/abis/CannonRegistry';
import * as viem from 'viem';
import { mainnet, optimism } from 'viem/chains';

const { PROVIDER_URL } = process.env;
const { ETHEREUM_PROVIDER_URL, OPTIMISM_PROVIDER_URL } = process.env;

// Used only for getting additional publishers
const OPTIMISM_PROVIDER_URL = 'wss://optimism-rpc.publicnode.com';
const ETH_START_BLOCK = 19543644;
const OP_START_BLOCK = 119000000;

// const REGISTRY_DEPLOY_BLOCK = 16493645; // Contract deployment block
const REGISTRY_DEPLOY_BLOCK = 19543644; // First publish event emitted
if (typeof ETHEREUM_PROVIDER_URL !== 'string' || !ETHEREUM_PROVIDER_URL) {
throw new Error('Missing RPC Provider url to use. Needs to have archival node, e.g. Alchemy.');
}

if (typeof PROVIDER_URL !== 'string' || !PROVIDER_URL) {
if (typeof OPTIMISM_PROVIDER_URL !== 'string' || !OPTIMISM_PROVIDER_URL) {
throw new Error('Missing RPC Provider url to use. Needs to have archival node, e.g. Alchemy.');
}

const ownerOrPublisher = process.argv[2] ? viem.getAddress(process.argv[2]) : undefined;

async function main() {
const client = viem.createPublicClient({
const ethClient = viem.createPublicClient({
chain: mainnet,
transport: PROVIDER_URL?.startsWith('wss://') ? viem.webSocket(PROVIDER_URL) : viem.http(PROVIDER_URL),
transport: ETHEREUM_PROVIDER_URL!.startsWith('wss://')
? viem.webSocket(ETHEREUM_PROVIDER_URL)
: viem.http(ETHEREUM_PROVIDER_URL),
});

const opClient = viem.createPublicClient({
chain: optimism,
transport: OPTIMISM_PROVIDER_URL!.startsWith('wss://')
? viem.webSocket(OPTIMISM_PROVIDER_URL)
: viem.http(OPTIMISM_PROVIDER_URL),
});

const contract = viem.getContract({
const ethContract = viem.getContract({
address: DEFAULT_REGISTRY_ADDRESS,
abi: CannonRegistryAbi,
client,
client: ethClient,
});

const opContract = viem.getContract({
address: DEFAULT_REGISTRY_ADDRESS,
abi: CannonRegistryAbi,
client: viem.createPublicClient({
chain: optimism,
transport: OPTIMISM_PROVIDER_URL?.startsWith('wss://')
? viem.webSocket(OPTIMISM_PROVIDER_URL)
: viem.http(OPTIMISM_PROVIDER_URL),
}),
client: opClient,
});

const packageNames = await getPackageNames(client);
const [ethPackageNames, opPackageNames] = await Promise.all([
_getPackageNames(ethClient as viem.PublicClient, ETH_START_BLOCK),
_getPackageNames(opClient as viem.PublicClient, OP_START_BLOCK),
]);

const packageNames = new Set([...Array.from(ethPackageNames), ...Array.from(opPackageNames)]);

console.log('{');
for (const [i, [bytes32name, name]] of Object.entries(packageNames)) {
const owner = await contract.read.getPackageOwner([bytes32name]);
for (const [i, bytes32name] of packageNames.entries()) {
const name = viem.hexToString(bytes32name, { size: 32 });
const [owner, ethPublishers, opPublishers] = await Promise.all([
ethContract.read.getPackageOwner([bytes32name]).then((o: any) => viem.getAddress(o)),
ethContract.read.getAdditionalPublishers([bytes32name]).then((p: any) => p.map((p: any) => viem.getAddress(p))),
opContract.read.getAdditionalPublishers([bytes32name]).then((p: any) => p.map((p: any) => viem.getAddress(p))),
]);

const ethPublishers = await contract.read.getAdditionalPublishers([bytes32name]);
const opPublishers = await opContract.read.getAdditionalPublishers([bytes32name]);
const publishers = Array.from(new Set([...ethPublishers, ...opPublishers]));

const comma = Number.parseInt(i) === packageNames.length - 1 ? '' : ',';
if (ownerOrPublisher && !viem.isAddressEqual(owner, ownerOrPublisher) && !publishers.includes(ownerOrPublisher)) {
continue;
}

const comma = Number.parseInt(i) === packageNames.size - 1 ? '' : ',';
console.log(` "${name}": { "owner:": "${owner}", "publishers": ${JSON.stringify(publishers)} }${comma}`);
}
console.log('}');

process.exit(0);
}

const _packagePublishEvents = viem.parseAbi([
Expand Down Expand Up @@ -93,12 +115,12 @@ const _packagePublishEvents = viem.parseAbi([
* OwnerNominated (address newOwner)
*/

async function getPackageNames(client: viem.PublicClient) {
const names = new Set<[string, string]>();
async function _getPackageNames(client: viem.PublicClient, startBlock: number) {
const names = new Set<viem.Hex>();

const latestBlock = Number((await client.getBlockNumber()).toString());

for (const [fromBlock, toBlock] of batches(REGISTRY_DEPLOY_BLOCK, latestBlock, 50000)) {
for (const [fromBlock, toBlock] of _batches(startBlock, latestBlock, 50000)) {
const filter = await client.createEventFilter({
address: DEFAULT_REGISTRY_ADDRESS,
events: _packagePublishEvents,
Expand All @@ -112,15 +134,14 @@ async function getPackageNames(client: viem.PublicClient) {
console.error(log);
throw new Error('Invalid event');
}
const name = viem.hexToString(log.args.name, { size: 32 });
names.add([log.args.name, name]);
names.add(log.args.name);
}
}

return Array.from(names);
return names;
}

function* batches(start: number, end: number, batchSize: number) {
function* _batches(start: number, end: number, batchSize: number) {
const count = Math.ceil((end - start) / batchSize);
for (let i = 0; i < count; i++) {
const batchStart = start + batchSize * i;
Expand Down
Loading

0 comments on commit a3f42a5

Please sign in to comment.