@newos/upgradeability-transpiler
Version:
Transpiles Solidity contracts to updgradable versions compatible with NewOs.
58 lines • 2.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const ast_utils_1 = require("../solc/ast-utils");
const get_inheritance_chain_1 = require("../solc/get-inheritance-chain");
// builds an __init call with given arguments, for example
// ERC20DetailedUpgradeable.__init(false, "Gold", "GLD", 18)
function buildSuperCall(args, name, source) {
let superCall = `\n ${name}Upgradeable.__init(false`;
if (args && args.length) {
superCall += args.reduce((acc, arg) => {
const [, , argSource] = ast_utils_1.getNodeSources(arg, source);
return acc + `, ${argSource}`;
}, '');
}
return superCall + ');';
}
// builds all the __init calls for the parent of a given contract, for example
// [ '\n ContextUpgradeable.__init(false);' ]
function buildSuperCalls(node, source, contractsToTranspile, mods) {
const hasInheritance = node.baseContracts.length;
if (hasInheritance) {
return node.baseContracts
.filter(base => contractsToTranspile.map(o => o.contractName).includes(base.baseName.name))
.map(base => {
const mod = mods.find(mod => mod.modifierName.name === base.baseName.name);
if (mod) {
return buildSuperCall(mod.arguments, mod.modifierName.name, source);
}
else {
return buildSuperCall(base.arguments, base.baseName.name, source);
}
});
}
else {
return [];
}
}
// builds all the __init calls a given contract, for example
// ContextUpgradeable.__init(false);
// ERC20DetailedUpgradeable.__init(false, 'Gold', 'GLD', 18);
function buildSuperCallsForChain(contractNode, source, contractsToTranspile, contractsToArtifactsMap) {
const chain = get_inheritance_chain_1.getInheritanceChain(contractNode.name, contractsToArtifactsMap);
const mods = lodash_1.flatten(chain.map(art => {
const node = ast_utils_1.getContract(art);
const constructorNode = ast_utils_1.getConstructor(node);
return constructorNode ? constructorNode.modifiers.filter(mod => ast_utils_1.isModifierInvocation(mod)) : [];
}));
return [
...new Set(lodash_1.flatten(chain.map(base => {
return buildSuperCalls(ast_utils_1.getContract(base), source, contractsToTranspile, mods).reverse();
}))),
]
.reverse()
.join('');
}
exports.buildSuperCallsForChain = buildSuperCallsForChain;
//# sourceMappingURL=build-super-calls-for-chain.js.map