@gnosis-guild/zodiac-core
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -1,2 +0,2 @@ | ||
import { BuildArtifact, MastercopyArtifact } from "../../types"; | ||
import { BuildArtifact } from "../../types"; | ||
/** | ||
@@ -11,17 +11,2 @@ * Retrieves the build artifact for a specified contract. | ||
export default function getBuildArtifact(_contractName: string, buildDirPath: string): BuildArtifact; | ||
/** | ||
* Replaces library references in the bytecode with actual deployed addresses. | ||
* | ||
* This function scans the bytecode and replaces placeholder references | ||
* to libraries with their actual on-chain addresses. It ensures that | ||
* the library addresses are valid and properly formatted. | ||
* | ||
* @param {string} bytecode - The bytecode that may contain library references. | ||
* @param {Record<string, any>} linkReferences - References to libraries, as returned by the compiler. | ||
* @param {Record<string, string>} libraryAddresses - A map of library names to their deployed addresses. | ||
* @returns {string} - The updated bytecode with library references replaced by actual addresses. | ||
* | ||
* @throws {Error} - Throws if a library address is missing or incorrectly formatted. | ||
*/ | ||
export declare function resolveLinksInBytecode(contractVersion: string, artifact: BuildArtifact, mastercopies: Record<string, Record<string, MastercopyArtifact>>): string; | ||
export declare function sourcePathFromSourceCode(compilerInput: any, contractName: string): string | null; |
@@ -7,7 +7,4 @@ "use strict"; | ||
exports.default = getBuildArtifact; | ||
exports.resolveLinksInBytecode = resolveLinksInBytecode; | ||
exports.sourcePathFromSourceCode = sourcePathFromSourceCode; | ||
const assert_1 = __importDefault(require("assert")); | ||
const path_1 = __importDefault(require("path")); | ||
const ethers_1 = require("ethers"); | ||
const fs_1 = require("fs"); | ||
@@ -37,39 +34,2 @@ /** | ||
/** | ||
* Replaces library references in the bytecode with actual deployed addresses. | ||
* | ||
* This function scans the bytecode and replaces placeholder references | ||
* to libraries with their actual on-chain addresses. It ensures that | ||
* the library addresses are valid and properly formatted. | ||
* | ||
* @param {string} bytecode - The bytecode that may contain library references. | ||
* @param {Record<string, any>} linkReferences - References to libraries, as returned by the compiler. | ||
* @param {Record<string, string>} libraryAddresses - A map of library names to their deployed addresses. | ||
* @returns {string} - The updated bytecode with library references replaced by actual addresses. | ||
* | ||
* @throws {Error} - Throws if a library address is missing or incorrectly formatted. | ||
*/ | ||
function resolveLinksInBytecode(contractVersion, artifact, mastercopies) { | ||
let bytecode = artifact.bytecode; | ||
for (const libraryPath of Object.keys(artifact.linkReferences)) { | ||
for (const libraryName of Object.keys(artifact.linkReferences[libraryPath])) { | ||
console.log(`libraryPath ${libraryPath} libraryName ${libraryName}`); | ||
if (!mastercopies[libraryName] || | ||
!mastercopies[libraryName][contractVersion]) { | ||
throw new Error(`Could not link ${libraryName} for ${artifact.contractName}`); | ||
} | ||
let { address: libraryAddress } = mastercopies[libraryName][contractVersion]; | ||
(0, assert_1.default)((0, ethers_1.isAddress)(libraryAddress)); | ||
for (const { length, start: offset } of artifact.linkReferences[libraryPath][libraryName]) { | ||
(0, assert_1.default)(length == 20); | ||
// the offset is in bytes, and does not account for the trailing 0x | ||
const left = 2 + offset * 2; | ||
const right = left + length * 2; | ||
bytecode = `${bytecode.slice(0, left)}${libraryAddress.slice(2).toLowerCase()}${bytecode.slice(right)}`; | ||
console.log(`Replaced library reference at ${offset} with address ${libraryAddress}`); | ||
} | ||
} | ||
} | ||
return bytecode; | ||
} | ||
/** | ||
* Resolves the paths to the artifact and build info files for a specified contract. | ||
@@ -76,0 +36,0 @@ * |
@@ -0,1 +1,2 @@ | ||
import { MastercopyArtifact } from "../types"; | ||
/** | ||
@@ -30,2 +31,2 @@ * Extracts and stores current Mastercopy result from current contract build, and stores it in the artifacts file. | ||
mastercopyArtifactsFile?: string; | ||
}): void; | ||
}): MastercopyArtifact; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -35,3 +12,4 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const paths_1 = require("./internal/paths"); | ||
const getBuildArtifact_1 = __importStar(require("./internal/getBuildArtifact")); | ||
const getBuildArtifact_1 = __importDefault(require("./internal/getBuildArtifact")); | ||
const linkBuildArtifact_1 = __importDefault(require("./internal/linkBuildArtifact")); | ||
/** | ||
@@ -62,3 +40,8 @@ * Extracts and stores current Mastercopy result from current contract build, and stores it in the artifacts file. | ||
} | ||
const bytecode = (0, getBuildArtifact_1.resolveLinksInBytecode)(contractVersion, buildArtifact, mastercopies); | ||
const artifact = (0, linkBuildArtifact_1.default)({ | ||
artifact: buildArtifact, | ||
contractVersion, | ||
minimalCompilerInput, | ||
mastercopies, | ||
}); | ||
const mastercopyArtifact = { | ||
@@ -72,11 +55,11 @@ contractName, | ||
factory, | ||
bytecode, | ||
bytecode: artifact.bytecode, | ||
constructorArgs, | ||
salt, | ||
}), | ||
bytecode, | ||
bytecode: artifact.bytecode, | ||
constructorArgs, | ||
salt, | ||
abi: buildArtifact.abi, | ||
compilerInput: minimalCompilerInput || buildArtifact.compilerInput, | ||
compilerInput: artifact.compilerInput, | ||
}; | ||
@@ -93,2 +76,3 @@ const nextMastercopies = Object.assign(Object.assign({}, mastercopies), { [contractName]: Object.assign(Object.assign({}, (mastercopies[contractName] || {})), { [contractVersion]: mastercopyArtifact }) }); | ||
(0, fs_1.writeFileSync)(mastercopyArtifactsFile, JSON.stringify(sortedMastercopies, null, 2), "utf8"); | ||
return mastercopyArtifact; | ||
} |
@@ -0,1 +1,2 @@ | ||
import { MastercopyArtifact } from "../types"; | ||
/** | ||
@@ -34,2 +35,2 @@ * Extracts and stores the Mastercopy result from a contract deployed on the blockchain by querying an Etherscan-like explorer. | ||
mastercopyArtifactsFile?: string; | ||
}): Promise<void>; | ||
}): Promise<MastercopyArtifact>; |
@@ -69,2 +69,3 @@ "use strict"; | ||
(0, fs_1.writeFileSync)(mastercopyArtifactsFile, JSON.stringify(sortedMastercopies, null, 2), "utf8"); | ||
return mastercopyArtifact; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { BuildArtifact, MastercopyArtifact } from "../../types"; | ||
import { BuildArtifact } from "../../types"; | ||
/** | ||
@@ -11,17 +11,2 @@ * Retrieves the build artifact for a specified contract. | ||
export default function getBuildArtifact(_contractName: string, buildDirPath: string): BuildArtifact; | ||
/** | ||
* Replaces library references in the bytecode with actual deployed addresses. | ||
* | ||
* This function scans the bytecode and replaces placeholder references | ||
* to libraries with their actual on-chain addresses. It ensures that | ||
* the library addresses are valid and properly formatted. | ||
* | ||
* @param {string} bytecode - The bytecode that may contain library references. | ||
* @param {Record<string, any>} linkReferences - References to libraries, as returned by the compiler. | ||
* @param {Record<string, string>} libraryAddresses - A map of library names to their deployed addresses. | ||
* @returns {string} - The updated bytecode with library references replaced by actual addresses. | ||
* | ||
* @throws {Error} - Throws if a library address is missing or incorrectly formatted. | ||
*/ | ||
export declare function resolveLinksInBytecode(contractVersion: string, artifact: BuildArtifact, mastercopies: Record<string, Record<string, MastercopyArtifact>>): string; | ||
export declare function sourcePathFromSourceCode(compilerInput: any, contractName: string): string | null; |
@@ -1,4 +0,2 @@ | ||
import assert from "assert"; | ||
import path from "path"; | ||
import { isAddress } from "ethers"; | ||
import { readdirSync, readFileSync, statSync } from "fs"; | ||
@@ -28,39 +26,2 @@ /** | ||
/** | ||
* Replaces library references in the bytecode with actual deployed addresses. | ||
* | ||
* This function scans the bytecode and replaces placeholder references | ||
* to libraries with their actual on-chain addresses. It ensures that | ||
* the library addresses are valid and properly formatted. | ||
* | ||
* @param {string} bytecode - The bytecode that may contain library references. | ||
* @param {Record<string, any>} linkReferences - References to libraries, as returned by the compiler. | ||
* @param {Record<string, string>} libraryAddresses - A map of library names to their deployed addresses. | ||
* @returns {string} - The updated bytecode with library references replaced by actual addresses. | ||
* | ||
* @throws {Error} - Throws if a library address is missing or incorrectly formatted. | ||
*/ | ||
export function resolveLinksInBytecode(contractVersion, artifact, mastercopies) { | ||
let bytecode = artifact.bytecode; | ||
for (const libraryPath of Object.keys(artifact.linkReferences)) { | ||
for (const libraryName of Object.keys(artifact.linkReferences[libraryPath])) { | ||
console.log(`libraryPath ${libraryPath} libraryName ${libraryName}`); | ||
if (!mastercopies[libraryName] || | ||
!mastercopies[libraryName][contractVersion]) { | ||
throw new Error(`Could not link ${libraryName} for ${artifact.contractName}`); | ||
} | ||
let { address: libraryAddress } = mastercopies[libraryName][contractVersion]; | ||
assert(isAddress(libraryAddress)); | ||
for (const { length, start: offset } of artifact.linkReferences[libraryPath][libraryName]) { | ||
assert(length == 20); | ||
// the offset is in bytes, and does not account for the trailing 0x | ||
const left = 2 + offset * 2; | ||
const right = left + length * 2; | ||
bytecode = `${bytecode.slice(0, left)}${libraryAddress.slice(2).toLowerCase()}${bytecode.slice(right)}`; | ||
console.log(`Replaced library reference at ${offset} with address ${libraryAddress}`); | ||
} | ||
} | ||
} | ||
return bytecode; | ||
} | ||
/** | ||
* Resolves the paths to the artifact and build info files for a specified contract. | ||
@@ -67,0 +28,0 @@ * |
@@ -0,1 +1,2 @@ | ||
import { MastercopyArtifact } from "../types"; | ||
/** | ||
@@ -30,2 +31,2 @@ * Extracts and stores current Mastercopy result from current contract build, and stores it in the artifacts file. | ||
mastercopyArtifactsFile?: string; | ||
}): void; | ||
}): MastercopyArtifact; |
@@ -6,3 +6,4 @@ import { existsSync, readFileSync, writeFileSync } from "fs"; | ||
import { defaultBuildDir, defaultMastercopyArtifactsFile, } from "./internal/paths"; | ||
import getBuildArtifact, { resolveLinksInBytecode, } from "./internal/getBuildArtifact"; | ||
import getBuildArtifact from "./internal/getBuildArtifact"; | ||
import linkBuildArtifact from "./internal/linkBuildArtifact"; | ||
/** | ||
@@ -33,3 +34,8 @@ * Extracts and stores current Mastercopy result from current contract build, and stores it in the artifacts file. | ||
} | ||
const bytecode = resolveLinksInBytecode(contractVersion, buildArtifact, mastercopies); | ||
const artifact = linkBuildArtifact({ | ||
artifact: buildArtifact, | ||
contractVersion, | ||
minimalCompilerInput, | ||
mastercopies, | ||
}); | ||
const mastercopyArtifact = { | ||
@@ -43,11 +49,11 @@ contractName, | ||
factory, | ||
bytecode, | ||
bytecode: artifact.bytecode, | ||
constructorArgs, | ||
salt, | ||
}), | ||
bytecode, | ||
bytecode: artifact.bytecode, | ||
constructorArgs, | ||
salt, | ||
abi: buildArtifact.abi, | ||
compilerInput: minimalCompilerInput || buildArtifact.compilerInput, | ||
compilerInput: artifact.compilerInput, | ||
}; | ||
@@ -64,2 +70,3 @@ const nextMastercopies = Object.assign(Object.assign({}, mastercopies), { [contractName]: Object.assign(Object.assign({}, (mastercopies[contractName] || {})), { [contractVersion]: mastercopyArtifact }) }); | ||
writeFileSync(mastercopyArtifactsFile, JSON.stringify(sortedMastercopies, null, 2), "utf8"); | ||
return mastercopyArtifact; | ||
} |
@@ -0,1 +1,2 @@ | ||
import { MastercopyArtifact } from "../types"; | ||
/** | ||
@@ -34,2 +35,2 @@ * Extracts and stores the Mastercopy result from a contract deployed on the blockchain by querying an Etherscan-like explorer. | ||
mastercopyArtifactsFile?: string; | ||
}): Promise<void>; | ||
}): Promise<MastercopyArtifact>; |
@@ -73,3 +73,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
writeFileSync(mastercopyArtifactsFile, JSON.stringify(sortedMastercopies, null, 2), "utf8"); | ||
return mastercopyArtifact; | ||
}); | ||
} |
{ | ||
"name": "@gnosis-guild/zodiac-core", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Zodiac is a composable design philosophy and collection of standards for building DAO ecosystem tooling.", | ||
@@ -5,0 +5,0 @@ "author": "Auryn Macmillan <auryn@gnosisguild.org>", |
274599
123
4240