-
Notifications
You must be signed in to change notification settings - Fork 34
feat: query contracts command #392
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
Draft
fadeev
wants to merge
10
commits into
main
Choose a base branch
from
contracts-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6120006
feat: query contracts command
fadeev cf368f6
split commands
fadeev b8f0f03
refactor
fadeev 73bb5fc
Merge branch 'main' into contracts-cli
fadeev 585c62c
lint
fadeev f33a45a
Remove unused json option from show schema
hernan-clich 3731eeb
Make default value consistent
hernan-clich e6be4f2
merge main
fadeev a814596
lint
fadeev 2f214af
update node version
fadeev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,11 @@ | ||
import { Command } from "commander"; | ||
|
||
import { listCommand } from "./list"; | ||
import { showCommand } from "./show"; | ||
|
||
export const contractsCommand = new Command("contracts") | ||
.alias("c") | ||
.description("Contract registry commands") | ||
.addCommand(listCommand) | ||
.addCommand(showCommand) | ||
.helpCommand(false); |
This file contains hidden or 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,118 @@ | ||
import RegistryABI from "@zetachain/protocol-contracts/abi/Registry.sol/Registry.json"; | ||
import chalk from "chalk"; | ||
import { Command, Option } from "commander"; | ||
import { ethers } from "ethers"; | ||
import ora from "ora"; | ||
import { getBorderCharacters, table } from "table"; | ||
import { z } from "zod"; | ||
|
||
import { CONTRACT_REGISTRY_ADDRESS } from "../../../../../src/constants/addresses"; | ||
import { DEFAULT_EVM_RPC_URL } from "../../../../../src/constants/api"; | ||
import { contractsListOptionsSchema } from "../../../../../src/schemas/commands/contracts"; | ||
import { formatAddress } from "../../../../../utils/addressResolver"; | ||
|
||
type ContractsListOptions = z.infer<typeof contractsListOptionsSchema>; | ||
|
||
interface ContractData { | ||
addressBytes: string; | ||
chainId: ethers.BigNumberish; | ||
contractType: string; | ||
} | ||
|
||
export const fetchContracts = async ( | ||
rpcUrl: string | ||
): Promise<ContractData[]> => { | ||
const provider = new ethers.JsonRpcProvider(rpcUrl); | ||
const contractRegistry = new ethers.Contract( | ||
CONTRACT_REGISTRY_ADDRESS, | ||
RegistryABI.abi, | ||
provider | ||
); | ||
|
||
const contracts = | ||
(await contractRegistry.getAllContracts()) as ContractData[]; | ||
return contracts; | ||
}; | ||
|
||
const formatContractsTable = ( | ||
contracts: ContractData[], | ||
columns: ("type" | "address")[] | ||
): string[][] => { | ||
const headers = ["Chain ID"]; | ||
|
||
if (columns.includes("type")) headers.push("Type"); | ||
if (columns.includes("address")) headers.push("Address"); | ||
|
||
const rows = contracts.map((contract) => { | ||
const baseRow = [contract.chainId.toString()]; | ||
|
||
if (columns.includes("type")) baseRow.push(contract.contractType); | ||
if (columns.includes("address")) | ||
baseRow.push(formatAddress(contract.addressBytes)); | ||
|
||
return baseRow; | ||
}); | ||
|
||
return [headers, ...rows]; | ||
}; | ||
|
||
const main = async (options: ContractsListOptions) => { | ||
const spinner = options.json | ||
? null | ||
: ora("Fetching contracts from registry...").start(); | ||
|
||
try { | ||
const contracts = await fetchContracts(options.rpc); | ||
if (!options.json) { | ||
spinner?.succeed(`Successfully fetched ${contracts.length} contracts`); | ||
} | ||
|
||
const sortedContracts = [...contracts].sort( | ||
(a, b) => parseInt(a.chainId.toString()) - parseInt(b.chainId.toString()) | ||
); | ||
|
||
if (options.json) { | ||
const jsonOutput = sortedContracts.map((c: ContractData) => ({ | ||
address: formatAddress(c.addressBytes), | ||
chainId: c.chainId.toString(), | ||
type: c.contractType, | ||
})); | ||
console.log(JSON.stringify(jsonOutput, null, 2)); | ||
return; | ||
} | ||
|
||
if (contracts.length === 0) { | ||
console.log(chalk.yellow("No contracts found in the registry")); | ||
return; | ||
} | ||
|
||
const tableData = formatContractsTable(sortedContracts, options.columns); | ||
const tableOutput = table(tableData, { | ||
border: getBorderCharacters("norc"), | ||
}); | ||
|
||
console.log(tableOutput); | ||
} catch (error) { | ||
if (!options.json) { | ||
spinner?.fail("Failed to fetch contracts"); | ||
} | ||
console.error(chalk.red("Error details:"), error); | ||
} | ||
}; | ||
|
||
export const listCommand = new Command("list") | ||
.alias("l") | ||
.description("List all contracts from the registry") | ||
.addOption( | ||
new Option("--rpc <url>", "Custom RPC URL").default(DEFAULT_EVM_RPC_URL) | ||
) | ||
.option("--json", "Output contracts as JSON") | ||
.addOption( | ||
new Option("--columns <values...>", "Additional columns to show") | ||
.choices(["type", "address"]) | ||
.default(["type", "address"]) | ||
) | ||
.action(async (options: ContractsListOptions) => { | ||
const validatedOptions = contractsListOptionsSchema.parse(options); | ||
await main(validatedOptions); | ||
}); |
This file contains hidden or 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,81 @@ | ||
import chalk from "chalk"; | ||
import { Command, Option } from "commander"; | ||
import { ethers } from "ethers"; | ||
import { z } from "zod"; | ||
|
||
import { DEFAULT_EVM_RPC_URL } from "../../../../../src/constants/api"; | ||
import { contractsShowOptionsSchema } from "../../../../../src/schemas/commands/contracts"; | ||
import { formatAddress } from "../../../../../utils/addressResolver"; | ||
import { fetchContracts } from "./list"; | ||
|
||
type ContractsShowOptions = z.infer<typeof contractsShowOptionsSchema>; | ||
|
||
interface ContractData { | ||
addressBytes: string; | ||
chainId: ethers.BigNumberish; | ||
contractType: string; | ||
} | ||
|
||
const findContractByChainId = ( | ||
contracts: ContractData[], | ||
chainId: string, | ||
type: string | ||
): ContractData | null => { | ||
const matchingContracts = contracts.filter( | ||
(contract) => contract.chainId.toString() === chainId | ||
); | ||
|
||
return ( | ||
matchingContracts.find( | ||
(contract) => contract.contractType.toLowerCase() === type.toLowerCase() | ||
) || null | ||
); | ||
}; | ||
|
||
const main = async (options: ContractsShowOptions) => { | ||
try { | ||
const contracts = await fetchContracts(options.rpc); | ||
|
||
const contract = findContractByChainId( | ||
contracts, | ||
options.chainId, | ||
options.type | ||
); | ||
|
||
if (!contract) { | ||
console.error( | ||
chalk.red( | ||
`Contract on chain '${options.chainId}' with type '${options.type}' not found` | ||
) | ||
); | ||
console.log(chalk.yellow("Available contracts:")); | ||
const availableContracts = contracts | ||
.map((c) => `${c.chainId.toString()}:${c.contractType}`) | ||
.sort(); | ||
console.log(availableContracts.join(", ")); | ||
process.exit(1); | ||
} | ||
|
||
const address = formatAddress(contract.addressBytes); | ||
console.log(address); | ||
} catch (error) { | ||
console.error(chalk.red("Error details:"), error); | ||
} | ||
}; | ||
|
||
export const showCommand = new Command("show") | ||
.alias("s") | ||
.description("Show contract address for a specific chain and type") | ||
.addOption( | ||
new Option("--rpc <url>", "Custom RPC URL").default(DEFAULT_EVM_RPC_URL) | ||
) | ||
.addOption( | ||
new Option("--chain-id -c <chainId>", "Chain ID").makeOptionMandatory() | ||
) | ||
.addOption( | ||
new Option("--type -t <type>", "Contract type").makeOptionMandatory() | ||
) | ||
.action(async (options: ContractsShowOptions) => { | ||
const validatedOptions = contractsShowOptionsSchema.parse(options); | ||
await main(validatedOptions); | ||
}); |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export const MULTICALL_ADDRESS = "0xca11bde05977b3631167028862be2a173976ca11"; | ||
export const CONTRACT_REGISTRY_ADDRESS = | ||
"0x7cce3eb018bf23e1fe2a32692f2c77592d110394"; |
This file contains hidden or 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,15 @@ | ||
import { z } from "zod"; | ||
|
||
import { DEFAULT_EVM_RPC_URL } from "../../constants/api"; | ||
|
||
export const contractsListOptionsSchema = z.object({ | ||
columns: z.array(z.enum(["type", "address"])).default(["type", "address"]), | ||
json: z.boolean().default(false), | ||
rpc: z.string().default(DEFAULT_EVM_RPC_URL), | ||
}); | ||
|
||
export const contractsShowOptionsSchema = z.object({ | ||
chainId: z.string(), | ||
rpc: z.string().default(DEFAULT_EVM_RPC_URL), | ||
type: z.string(), | ||
}); |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.