aboutsummaryrefslogtreecommitdiffstats
path: root/packages/migrations/src/utils/artifact_writer.ts
blob: b7522df051e97578d203f7d2c1dcfff0f3b14bf1 (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
import { BaseContract } from '@0xproject/base-contract';
import { ContractArtifact } from '@0xproject/sol-compiler';
import * as fs from 'fs';
import * as path from 'path';

export class ArtifactWriter {
    private readonly _artifactsDir: string;
    private readonly _networkId: number;
    constructor(artifactsDir: string, networkId: number) {
        this._artifactsDir = artifactsDir;
        this._networkId = networkId;
    }
    // This updates the artifact file but does not update the `artifacts` module above. It will not
    // contain the saved artifact changes.
    public saveArtifact(contract: BaseContract): void {
        const contractName = contract.contractName;
        const artifactFile = path.join(this._artifactsDir, `${contractName}.json`);
        const artifact: ContractArtifact = JSON.parse(fs.readFileSync(artifactFile).toString());
        artifact.networks[this._networkId] = {
            address: contract.address,
            links: {},
            constructorArgs: JSON.stringify(contract.constructorArgs),
        };
        fs.writeFileSync(artifactFile, JSON.stringify(artifact, null, '\t'));
    }
}