Skip to content

Commit

Permalink
feat: refactor external-prs to use oss-directory types (#1586)
Browse files Browse the repository at this point in the history
* oss-directory now exposes BlockchainTag and BlockchainNetwork
* Use these types to make sure that we are using the correct strings in
  validation logic
  • Loading branch information
ryscheng authored Jun 4, 2024
1 parent 08f8a06 commit 57d88f3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion ops/external-prs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"lodash": "^4.17.21",
"mustache": "^4.2.0",
"octokit": "^3.1.0",
"oss-directory": "^0.0.13",
"oss-directory": "^0.0.14",
"tmp-promise": "^3.0.3",
"ts-dedent": "^2.2.0",
"winston": "^3.11.0",
Expand Down
57 changes: 32 additions & 25 deletions ops/external-prs/src/ossd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
loadData,
Project,
Collection,
BlockchainAddress,
BlockchainNetwork,
BlockchainTag,
} from "oss-directory";
import duckdb from "duckdb";
import _ from "lodash";
Expand All @@ -29,15 +30,6 @@ import {
import { GithubOutput } from "../github.js";
import { CheckConclusion, CheckStatus } from "../checks.js";

// This is only used to get the type of `networks`
const EXAMPLE_ADDRESS: BlockchainAddress = {
address: "0x123",
networks: ["mainnet"],
tags: ["eoa"],
};
type NetworkTuple = typeof EXAMPLE_ADDRESS.networks;
type Network = NetworkTuple[number];

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Should map to the tables field in ./metadata/databases/databases.yaml
Expand Down Expand Up @@ -321,7 +313,7 @@ class OSSDirectoryPullRequest {
private db: duckdb.Database;
private args: OSSDirectoryPullRequestArgs;
private changes: ChangeSummary;
private validators: Partial<Record<Network, EVMNetworkValidator>>;
private validators: Partial<Record<BlockchainNetwork, EVMNetworkValidator>>;

static async init(args: OSSDirectoryPullRequestArgs) {
const pr = new OSSDirectoryPullRequest(args);
Expand Down Expand Up @@ -650,33 +642,48 @@ class OSSDirectoryPullRequest {
for (const item of this.changes.artifacts.toValidate.blockchain) {
const address = item.address;
for (const network of item.networks) {
const validator = this.validators[network as Network];
const validator = this.validators[network as BlockchainNetwork];
if (!validator) {
logger.error({
message: "no validator found for network",
network: network,
});
throw new Error(`No validator found for network "${network}"`);
}
const createValidatorMapping = (
validator: EVMNetworkValidator,
): Partial<
Record<BlockchainTag, (name: string) => Promise<boolean>>
> => {
const mapping = {
eoa: validator.isEOA,
contract: validator.isContract,
deployer: validator.isDeployer,
};
return mapping;
};
const validatorMappings = createValidatorMapping(validator);

logger.info({
message: "validating address",
address: address,
network: network,
tags: item.tags,
});
if (item.tags.indexOf("eoa") !== -1) {
if (!(await validator.isEOA(address))) {
addErrorToResult(address, "is not an EOA");
}
}
if (item.tags.indexOf("contract") !== -1) {
if (!(await validator.isContract(address))) {
addErrorToResult(address, "is not a Contract");
}
}
if (item.tags.indexOf("deployer") !== -1) {
if (!(await validator.isDeployer(address))) {
addErrorToResult(address, "is not a Deployer");

for (const [tag, validatorFn] of Object.entries(validatorMappings)) {
if (item.tags.includes(tag)) {
if (!validatorFn) {
logger.error(
`ERROR: missing validator for ${tag} on network=${network}`,
);
} else if (!(await validatorFn(address))) {
addErrorToResult(address, `is not a '${tag}' on ${network}`);
} else {
logger.debug(
`validation okay: ${address} is a ${tag} on ${network}`,
);
}
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 57d88f3

Please sign in to comment.