Skip to content

loadRemoteVersion is not working with jest test #724

@Damantino

Description

@Damantino

I have made a verification service which verifies smart contracts. I have and endpoint that I'm trying to test. It receives all the solidity files and a few settings and outputs if it matches with code on mainnet.

While doing tests everything was fine until I could not load the remote version, I cannot mock that, it need to be the snapshot.

Here is the code for the test:

it('/verify (POST)', async () => {
    const contractsDir = path.join(__dirname, './contracts');
    const testFolders = fs.readdirSync(contractsDir);

    for (const folder of testFolders) {
      const folderPath = path.join(contractsDir, folder);
      const files = fs.readdirSync(folderPath);

      const req = request(app.getHttpServer()).post('/contracts/verify').set('accept', '*/*').set('Content-Type', 'multipart/form-data');

      let settings: any;

      await Promise.all(
        files.map(async (file) => {
          const ext = path.extname(file);
          const filePath = path.join(folderPath, file);
          if (ext === '.sol') {
            req.attach('files', filePath);
          } else if (ext === '.json') {
            settings = JSON.parse(await fs.promises.readFile(filePath, 'utf-8'));
          }
        }),
      );

      req.field('contractAddress', settings.address);
      req.field('chain', '1');
      req.field('evmVersion', settings.evmVersion);
      req.field('optimizationEnabled', settings.optimizer.enabled);
      req.field('optimizationRuns', settings.optimizer.runs);
      req.field('viaIr', settings.viaIR ?? false);

      const response = await req;
      expect(response.status).toEqual(200);
    }
  });

This works until I arrive to my service, I have a function to load asynchronously the solc snaphot.

async loadSolc(version: string): Promise<any> {
    return await new Promise((resolve, reject) => {
      if (solcCache[version] !== undefined) resolve(solcCache[version]);
      // You may need to adjust the import statement based on your environment
      else {
        loadRemoteVersion(`v${version}`, (error: any, soljson: any) => {
          solcCache[version] = soljson;
          return error ? reject(error) : resolve(soljson);
        });
      }
    });
  }

This is used then on the compile:

async compile(version: string, solcJsonInput: any, forceEmscripten = false): Promise<any> {
    const solc = await this.loadSolc(version);
    const compiledOutput = JSON.parse(solc.compile(JSON.stringify(solcJsonInput)));

    return compiledOutput;
  }

It keeps on loadSolC forever.

Any help would be appreciated.

PD: I have on package json:

"jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": ".",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "moduleNameMapper": {
      "^src/(.*)$": "<rootDir>/src/$1"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }

EDIT: md format

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions