UNPKG

@openzeppelin/upgradeability-transpiler

Version:

Transpiles Solidity contracts to updgradable versions compatible with OpenZeppelin SDK.

83 lines 3.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const ast_utils_1 = require("./solc/ast-utils"); const transpiler_1 = require("./transpiler"); const index_1 = require("./transformations/index"); const get_inheritance_chain_1 = require("./solc/get-inheritance-chain"); function transpileContracts(contracts, artifacts) { // check that we have valid ast tree for (const art of artifacts) { ast_utils_1.throwIfInvalidNode(art.ast); } // create contract name | id to artifact map for quick access to artifacts const contractsToArtifactsMap = artifacts.reduce((acc, art) => { acc[art.contractName] = art; const contract = ast_utils_1.getContract(art); acc[contract.id] = art; return acc; }, {}); // build a list of all contracts to transpile const contractsToTranspile = [ ...new Set(lodash_1.flatten(contracts.map(contract => get_inheritance_chain_1.getInheritanceChain(contract, contractsToArtifactsMap)))), ].filter(art => { const contractNode = ast_utils_1.getContract(art); return ast_utils_1.isContract(contractNode); }); // build a array of transformations per Solidity file const fileTrans = contractsToTranspile.reduce((acc, art) => { const contractName = art.contractName; const source = art.source; const contractNode = ast_utils_1.getContract(art); if (!acc[art.fileName]) { const directive = `\nimport "@openzeppelin/upgrades/contracts/Initializable.sol";`; acc[art.fileName] = { transformations: [ index_1.appendDirective(art.ast, directive), ...index_1.fixImportDirectives(art, artifacts, contractsToTranspile), ...index_1.purgeExceptContracts(art.ast, contractsToTranspile), ], source: '', }; } acc[art.fileName].transformations = [ ...acc[art.fileName].transformations, index_1.prependBaseClass(contractNode, source, 'Initializable'), ...index_1.transformParentsNames(contractNode, source, contractsToTranspile), ...index_1.transformConstructor(contractNode, source, contractsToTranspile, contractsToArtifactsMap), ...index_1.purgeVarInits(contractNode, source), index_1.transformContractName(contractNode, source, `${contractName}Upgradable`), ]; return acc; }, {}); // build a final array of files to return return contractsToTranspile.reduce((acc, art) => { const contractName = art.contractName; const source = art.source; const fileTran = fileTrans[art.fileName]; if (!fileTran.source) { fileTran.source = transpiler_1.transpile(source, fileTran.transformations); } const entry = acc.find(o => o.fileName === art.fileName); if (!entry) { const path = art.sourcePath.replace('.sol', 'Upgradable.sol'); let patchedFilePath = path; if (path.startsWith('contracts')) { patchedFilePath = path.replace('contracts/', ''); } patchedFilePath = `./contracts/__upgradable__/${patchedFilePath}`; acc.push({ source: fileTran.source, path: patchedFilePath, fileName: art.fileName, contracts: [contractName], }); } else { entry.contracts.push(contractName); } return acc; }, []); } exports.transpileContracts = transpileContracts; //# sourceMappingURL=index.js.map