aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-cov/src/artifact_adapters/0x.ts
blob: 87d23b0aa9cc044fbc9b0df8f4c84e96b4e83780 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import * as fs from 'fs';
import * as glob from 'glob';
import * as _ from 'lodash';
import * as path from 'path';

import { ContractData } from '../types';

import { AbstractArtifactAdapter } from './abstract';

export class ZeroExArtifactAdapter extends AbstractArtifactAdapter {
    private _artifactsPath: string;
    private _sourcesPath: string;
    constructor(artifactsPath: string, sourcesPath: string) {
        super();
        this._artifactsPath = artifactsPath;
        this._sourcesPath = sourcesPath;
    }
    public async collectContractsDataAsync(): Promise<ContractData[]> {
        const artifactsGlob = `${this._artifactsPath}/**/*.json`;
        const artifactFileNames = glob.sync(artifactsGlob, { absolute: true });
        const contractsData: ContractData[] = [];
        for (const artifactFileName of artifactFileNames) {
            const artifact = JSON.parse(fs.readFileSync(artifactFileName).toString());
            let sources = _.keys(artifact.sources);
            sources = _.map(sources, relativeFilePath => path.resolve(this._sourcesPath, relativeFilePath));
            const contractName = artifact.contractName;
            // We don't compute coverage for dependencies
            const sourceCodes = _.map(sources, (source: string) => fs.readFileSync(source).toString());
            const contractData = {
                sourceCodes,
                sources,
                bytecode: artifact.compilerOutput.evm.bytecode.object,
                sourceMap: artifact.compilerOutput.evm.bytecode.sourceMap,
                runtimeBytecode: artifact.compilerOutput.evm.deployedBytecode.object,
                sourceMapRuntime: artifact.compilerOutput.evm.deployedBytecode.sourceMap,
            };
            contractsData.push(contractData);
        }
        return contractsData;
    }
}