@gnus.ai/upgrade-safe-transpiler-diamond
Version:
Solidity preprocessor used to generate OpenZeppelin Contracts Upgrade Safe using Diamond Pattern (EIP-2535).
140 lines • 6.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addPublicGetters = void 0;
const utils_1 = require("solidity-ast/utils");
const new_function_position_1 = require("./utils/new-function-position");
const upgrades_overrides_1 = require("../utils/upgrades-overrides");
const get_var_storage_name_1 = require("./utils/get-var-storage-name");
const rename_1 = require("../rename");
function* addPublicGetters(sourceUnit, tools) {
const { resolver } = tools;
for (const contractNode of (0, utils_1.findAll)('ContractDefinition', sourceUnit)) {
if (contractNode.contractKind !== 'contract') {
continue;
}
const publicVariables = [...(0, utils_1.findAll)('VariableDeclaration', contractNode)].filter(v => v.stateVariable &&
v.visibility === 'public' &&
!v.constant &&
!(0, upgrades_overrides_1.hasOverride)(v, 'state-variable-assignment') &&
v.functionSelector);
if (publicVariables.length > 0) {
let getterFuncsText = '';
for (const varDecl of publicVariables) {
const argumentsText = [];
// VariableDeclaration nodes for function parameters or state variables will always
// have their typeName fields defined.
let nextType = varDecl.typeName;
let returnType;
while (true) {
if (nextType.nodeType === 'Mapping') {
const canonicalType = canonicalAbiTypeForElementaryOrUserDefinedTypes(nextType.keyType);
argumentsText.push(`${canonicalType} arg${argumentsText.length.toString()}`);
nextType = nextType.valueType;
}
else {
if (nextType.nodeType === 'ArrayTypeName') {
argumentsText.push(`uint256 arg${argumentsText.length.toString()}`);
nextType = nextType.baseType;
}
if (nextType.nodeType === 'UserDefinedTypeName') {
const userVarDecl = resolver.resolveScope(nextType.referencedDeclaration).node;
returnType = nextType.pathNode.name;
if (['ArrayDefinition', 'StructDefinition'].indexOf(userVarDecl.nodeType) > -1) {
if (('scope' in userVarDecl) && (userVarDecl.scope !== contractNode.id)) {
returnType = (0, rename_1.renamePath)(returnType);
}
returnType += ' memory';
}
else if (userVarDecl.nodeType === 'ContractDefinition') {
returnType = (0, rename_1.renameContract)(returnType);
}
}
else {
returnType = canonicalAbiTypeForElementaryOrUserDefinedTypes(nextType);
if (returnType === 'string') {
returnType += ' memory';
}
}
break;
}
}
const arrayAccess = argumentsText.map((arg, index) => `[arg${index.toString()}]`).join('');
getterFuncsText += '\n' +
` // generated getter for ${varDecl.name}\n` +
` function ${varDecl.name}(${argumentsText.join(',')}) public view returns(${returnType}) {\n` +
` return ${(0, get_var_storage_name_1.getVarStorageName)(varDecl, tools)}${varDecl.name}${arrayAccess};\n` +
` }\n`;
}
// grab first line of contract.
const braceOffset = (0, new_function_position_1.newFunctionPosition)(contractNode, tools, true);
yield {
start: braceOffset,
length: 0,
kind: 'add-public-getters',
text: getterFuncsText,
};
}
}
}
exports.addPublicGetters = addPublicGetters;
function isContractType(param) {
var _a, _b;
return ((((_a = param.typeName) === null || _a === void 0 ? void 0 : _a.nodeType) === 'UserDefinedTypeName' ||
(param === null || param === void 0 ? void 0 : param.nodeType) === 'UserDefinedTypeName') &&
((_b = param.typeDescriptions) === null || _b === void 0 ? void 0 : _b.typeString) !== undefined &&
param.typeDescriptions.typeString.startsWith('contract '));
}
function isEnumType(param) {
var _a, _b;
return ((((_a = param.typeName) === null || _a === void 0 ? void 0 : _a.nodeType) === 'UserDefinedTypeName' ||
(param === null || param === void 0 ? void 0 : param.nodeType) === 'UserDefinedTypeName') &&
((_b = param.typeDescriptions) === null || _b === void 0 ? void 0 : _b.typeString) !== undefined &&
param.typeDescriptions.typeString.startsWith("enum "));
}
function canonicalAbiTypeForElementaryOrUserDefinedTypes(typeName) {
var _a, _b, _c, _d;
if (typeName.nodeType === 'ElementaryTypeName') {
return toCanonicalAbiType(typeName.name);
}
if (isEnumType(typeName)) {
return renamePathNode(((_b = (_a = typeName.typeDescriptions) === null || _a === void 0 ? void 0 : _a.typeString) === null || _b === void 0 ? void 0 : _b.substring(5)) || '');
}
if (isContractType(typeName)) {
return (0, rename_1.renameContract)(((_d = (_c = typeName.typeDescriptions) === null || _c === void 0 ? void 0 : _c.typeString) === null || _d === void 0 ? void 0 : _d.substring(8)) || '');
}
return undefined;
}
function toCanonicalAbiType(type) {
if (type.startsWith('int[')) {
return `int256${type.slice(3)}`;
}
if (type === 'int') {
return 'int256';
}
if (type.startsWith('uint[')) {
return `uint256${type.slice(4)}`;
}
if (type === 'uint') {
return 'uint256';
}
if (type.startsWith('fixed[')) {
return `fixed128x128${type.slice(5)}`;
}
if (type === 'fixed') {
return 'fixed128x128';
}
if (type.startsWith('ufixed[')) {
return `ufixed128x128${type.slice(6)}`;
}
if (type === 'ufixed') {
return 'ufixed128x128';
}
return type;
}
function renamePathNode(pathNode) {
if (pathNode.indexOf('.') > -1) {
return (0, rename_1.renamePath)(pathNode);
}
return pathNode;
}
//# sourceMappingURL=add-public-getters.js.map