Skip to content

Commit

Permalink
Merge pull request #13 from gnosisguild/v2
Browse files Browse the repository at this point in the history
Rearrange Tooling Entrypoints
  • Loading branch information
samepant authored Aug 26, 2024
2 parents 11ab4b9 + 5292710 commit 4413ea3
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 194 deletions.
49 changes: 5 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,62 +199,23 @@ await writeMastercopyFromExplorer({
});
```

#### `readMastercopy`
#### `readMastercopies`

Retrieves the Mastercopy artifact information from the artifacts file. This function is used to access stored data for a specific contract version or the latest available version if no version is specified.
Retrieves a collection of Mastercopy artifacts from a JSON artifacts file. This function allows access to stored mastercopy data, with optional filters for contractName and contractVersion to refine the results. If no filters are provided, all artifacts are returned.

**Inputs**

**`contractName`** - The name of the contract.
**`contractName`** - (Optional) The name of the contract.

**`contractVersion`** - (Optional) The version of the contract. If not provided, the latest version will be used.
**`contractVersion`** - (Optional) The version of the contract or "latest". If not provided, all versions will be retrieved.

**`mastercopyArtifactsFile`** - (Optional) The path to the mastercopy artifacts file. Defaults to defaultMastercopyArtifactsFile().

```ts
import { readMastercopy } from "@gnosis-guild/zodiac-core";
import { readMastercopies } from "@gnosis-guild/zodiac-core";

const artifact = readMastercopy({
contractName: "MyNewMod",
contractVersion: "1.0.0",
});
```

`deployAllMastercopies`

Deploys each Mastercopy listed in the artifacts file using the provided provider. If a Mastercopy is already deployed, it will be skipped.

**Inputs**

**`provider`** - An EIP1193-compliant provider to interact with the blockchain.

**`mastercopyArtifactsFile`** - (Optional) The path to the mastercopy artifacts file. Defaults to defaultMastercopyArtifactsFile().

```ts
import { deployAllMastercopies } from "zodiac-core";

await deployAllMastercopies({
provider, // an EIP1193 compliant provider
});
```

#### `verifyAllMastercopies`

Verifies each Mastercopy in the artifacts file on an Etherscan-compatible block explorer. This function ensures that the deployed contracts are properly verified and visible on public explorers.

**Inputs**

**`apiUrlOrChainId`** - The API URL or Chain ID for the verification service.

**`apiKey`** - The API key used for verification.

**`mastercopyArtifactsFile`** - (Optional) The path to the mastercopy artifacts file. Defaults to `defaultMastercopyArtifactsFile()`.

```ts
import { verifyAllMastercopies } from "zodiac-core";

await verifyAllMastercopies({
apiUrlOrChainId: "1", // or the explorer's API URL
apiKey: "YourEtherscanApiKey",
});
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gnosis-guild/zodiac-core",
"version": "1.1.1",
"version": "2.0.0",
"description": "Zodiac is a composable design philosophy and collection of standards for building DAO ecosystem tooling.",
"author": "Auryn Macmillan <[email protected]>",
"license": "LGPL-3.0+",
Expand Down
63 changes: 0 additions & 63 deletions src/artifact/deployAllMastercopies.ts

This file was deleted.

10 changes: 1 addition & 9 deletions src/artifact/internal/etherscan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function verifySourceCode({
constructorArgs: { types: any[]; values: any[] };
apiUrlOrChainId: string;
apiKey: string;
}): Promise<{ ok: boolean; noop: boolean }> {
}): Promise<{ noop: boolean }> {
const url = resolveApiUrl(apiUrlOrChainId);

if (!(await isLiveUrl(url))) {
Expand All @@ -51,7 +51,6 @@ export async function verifySourceCode({

if (await isVerified(address, { url, apiKey })) {
return {
ok: true,
noop: true,
};
}
Expand Down Expand Up @@ -87,7 +86,6 @@ export async function verifySourceCode({
}

return {
ok: true,
noop: false,
};
}
Expand Down Expand Up @@ -131,12 +129,6 @@ export async function getSourceCode({
result: any;
};

// SourceCode
// ContractName
// ABI
// CompilerVersion
// ConstructorArguments

if (!isOk(status)) {
throw new Error(`Retrieve Error: ${status} ${message}`);
}
Expand Down
138 changes: 138 additions & 0 deletions src/artifact/readMastercopies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import assert from "assert";
import semver from "semver";
import { existsSync, readFileSync } from "fs";

import { defaultMastercopyArtifactsFile } from "./internal/paths";
import { MastercopyArtifact } from "../types";

/**
* Extracts and returns Mastercopy artifact information from the specified artifacts file.
*
* This function allows filtering of artifacts based on the contract name and version.
* If no filter is provided, it returns all artifacts. When `latest` is specified as the
* contract version, only the highest version of each artifact is returned.
*
* @param {Object} params - The parameters for filtering the artifacts.
* @param {string} [params.contractName] - [Optional] The name of the contract to filter by. If not provided, all contract names are considered.
* @param {string} [params.contractVersion] - [Optional] The version of the contract to filter by. If set to `"latest"`, only the most recent version of each contract is returned. If not provided, all versions are considered.
* @param {string} [params.mastercopyArtifactsFile=defaultMastercopyArtifactsFile()] - The path to the mastercopy artifacts file. Defaults to the path returned by `defaultMastercopyArtifactsFile()`.
* @returns {MastercopyArtifact[]} An array of Mastercopy artifacts matching the specified filters.
*
* @throws {Error} Throws an error if the specified artifacts file does not exist.
*
* @example
* // Returns all artifacts
* const allArtifacts = enumMastercopies({});
*
* @example
* // Returns artifacts for a specific contract name
* const specificContractArtifacts = enumMastercopies({ contractName: 'MyContract' });
*
* @example
* // Returns artifacts for a specific contract name and version
* const specificVersionArtifacts = enumMastercopies({ contractName: 'MyContract', contractVersion: '1.0.0' });
*
* @example
* // Returns the latest version of all contracts
* const latestVersionArtifacts = enumMastercopies({ contractVersion: 'latest' });
*/
export default function readMastercopies({
contractName,
contractVersion,
mastercopyArtifactsFile = defaultMastercopyArtifactsFile(),
}: {
contractName?: string;
contractVersion?: string;
mastercopyArtifactsFile?: string;
} = {}): MastercopyArtifact[] {
if (!existsSync(mastercopyArtifactsFile)) {
throw new Error(
`MastercopyArtifacts file not found at ${mastercopyArtifactsFile}`
);
}

const mastercopies = JSON.parse(
readFileSync(mastercopyArtifactsFile, "utf8")
);

const result = [] as MastercopyArtifact[];

for (const nameKey of Object.keys(mastercopies)) {
const _contractVersion =
contractVersion == "latest"
? findLatestVersion({ contractName: nameKey, mastercopies })
: contractVersion;

for (const versionKey of Object.keys(mastercopies[nameKey])) {
if (
filterByContractName({ contractName: nameKey, filter: contractName }) &&
filterByContractVersion({
contractVersion: versionKey,
filter: _contractVersion,
})
) {
result.push(mastercopies[nameKey][versionKey]);
}
}
}

return result;
}

function filterByContractName({
contractName,
filter,
}: {
contractName: string;
filter?: string;
}) {
if (typeof filter != "string") {
return true;
}

return contractName.trim().toLowerCase() == filter.trim().toLowerCase();
}

function filterByContractVersion({
contractVersion,
filter,
}: {
contractVersion: string;
filter?: string;
}) {
if (typeof filter != "string") {
return true;
}

if (!semver.valid(contractVersion)) {
throw new Error(`Invalid Artifact Version ${contractVersion}`);
}

if (!semver.valid(filter)) {
throw new Error(`Invalid Filter Version ${filter}`);
}

return semver.satisfies(contractVersion, filter);
}

function findLatestVersion({
contractName,
mastercopies,
}: {
contractName: string;
mastercopies: Record<string, JSON>;
}) {
const versions = Object.keys(mastercopies[contractName] as JSON);
if (versions.length == 0) {
throw new Error(`MastercopyArtifacts file: no Entries`);
}

const invalid = versions.find((version) => !semver.valid(version));
if (invalid) {
throw new Error(`MastercopyArtifacts file: not a valid version ${invalid}`);
}

const [latest] = versions.sort(semver.compare).reverse();
assert(semver.valid(latest));
return latest;
}
64 changes: 0 additions & 64 deletions src/artifact/readMastercopy.ts

This file was deleted.

Loading

0 comments on commit 4413ea3

Please sign in to comment.