Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add missing functions in abstract contracts #54

Merged
merged 4 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions solidity/contracts/utils/ContractBAbstract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma solidity ^0.8.0;

import {IContractBAbstract} from '../../interfaces/IContractBAbstract.sol';
import {IContractB2Abstract} from '../../interfaces/IContractB2Abstract.sol';

abstract contract ContractBAbstract is IContractBAbstract, IContractB2Abstract {
uint256 public uintVariable;

constructor(uint256 _uintVariable) {
uintVariable = _uintVariable;
}

function setVariablesA(uint256 _newValue) public returns (bool _result) {
uintVariable = _newValue;
_result = true;
}
}
5 changes: 5 additions & 0 deletions solidity/interfaces/IContractB2Abstract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity ^0.8.0;

interface IContractB2Abstract {
function undefinedInterfaceFunc2(uint256 _someNumber) external returns (bool _result);
}
6 changes: 6 additions & 0 deletions solidity/interfaces/IContractBAbstract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pragma solidity ^0.8.0;

interface IContractBAbstract {
function undefinedInterfaceFunc(uint256 _someNumber) external returns (bool _result);
function uintVariable() external view returns (uint256 _uintVariable);
}
5 changes: 5 additions & 0 deletions src/smock-foundry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getSourceUnits,
smockableNode,
compileSolidityFilesFoundry,
renderAbstractUnimplementedFunctions,
} from './utils';
import path from 'path';
import { ensureDir } from 'fs-extra';
Expand Down Expand Up @@ -44,6 +45,10 @@ export async function generateMockContracts(
// Libraries are not mocked
if (contract.kind === 'library') continue;

if (contract.abstract) {
mockContent += await renderAbstractUnimplementedFunctions(contract);
}

for (const node of contract.children) {
if (!smockableNode(node)) continue;
mockContent += await renderNodeMock(node);
Expand Down
48 changes: 46 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import Handlebars from 'handlebars';
import path from 'path';
import { glob } from 'fast-glob';
import { VariableDeclaration, FunctionDefinition, ImportDirective, ASTNode, ASTKind, ASTReader, SourceUnit, compileSol } from 'solc-typed-ast';
import {
VariableDeclaration,
FunctionDefinition,
ImportDirective,
ASTNode,
ASTKind,
ASTReader,
SourceUnit,
compileSol,
ContractDefinition,
} from 'solc-typed-ast';
import { userDefinedTypes, explicitTypes } from './types';
import { readFileSync } from 'fs'; // TODO: Replace with fs/promises
import { ensureDir, emptyDir } from 'fs-extra';
Expand Down Expand Up @@ -44,13 +54,13 @@
* Registers the nested templates
* @returns The content of the template
*/
export function getContractTemplate(): HandlebarsTemplateDelegate<any> {

Check warning on line 57 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

Unexpected any. Specify a different type
const templatePath = path.resolve(__dirname, 'templates', 'contract-template.hbs');
const templateContent = readFileSync(templatePath, 'utf8');
return Handlebars.compile(templateContent);
}

export function getSmockHelperTemplate(): HandlebarsTemplateDelegate<any> {

Check warning on line 63 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

Unexpected any. Specify a different type
const templatePath = path.resolve(__dirname, 'templates', 'helper-template.hbs');
const templateContent = readFileSync(templatePath, 'utf8');
return Handlebars.compile(templateContent);
Expand Down Expand Up @@ -210,7 +220,7 @@
const regex = /remappings[\s|\n]*=[\s\n]*\[(?<remappings>[^\]]+)]/;
const matches = foundryConfigContent.match(regex);
if (matches) {
return matches

Check warning on line 223 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

Forbidden non-null assertion
.groups!.remappings.split(',')
.map((line) => line.trim())
.map((line) => line.replace(/["']/g, ''))
Expand Down Expand Up @@ -261,7 +271,8 @@
includePath: [rootPath],
});

const sourceUnits = new ASTReader().read(compiledFiles.data, ASTKind.Any, compiledFiles.files)
const sourceUnits = new ASTReader()
.read(compiledFiles.data, ASTKind.Any, compiledFiles.files)
// Skip source units that are not in the contracts directories
.filter((sourceUnit) => contractsDirectories.some((directory) => sourceUnit.absolutePath.includes(directory)));

Expand All @@ -281,3 +292,36 @@

return true;
}

/**
* Renders the abstract functions that are not implemented in the current contract
* @param contract The contract to render the abstract functions from
* @returns The content of the functions
*/
export async function renderAbstractUnimplementedFunctions(contract: ContractDefinition): Promise<string> {
let content = '';

const existingSelectors = [...contract.vStateVariables, ...contract.vFunctions].map((node) => node.raw?.functionSelector);
const functions = [];

for (const base of contract.vLinearizedBaseContracts) {
// Skip the first contract, which is the current contract
if (base.id === contract.id) continue;

for (const baseFunction of base.vFunctions) {
// Skip the functions that are already implemented in the current contract
if (existingSelectors.includes(baseFunction.raw?.functionSelector)) continue;

// If the function is already in the new functions array, skip it
if (functions.some((func) => func.raw?.functionSelector === baseFunction.raw?.functionSelector)) continue;

functions.push(baseFunction);
}
}

for (const func of functions) {
content += await renderNodeMock(func);
}

return content;
}
Loading