Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3481 from trufflesuite/revert-3478-revert-3392-im…
Browse files Browse the repository at this point in the history
…port-abi-json

Allow importing ABI JSON files in Solidity
  • Loading branch information
gnidan authored Nov 3, 2020
2 parents 2281ef7 + 147fd49 commit f67b577
Show file tree
Hide file tree
Showing 6 changed files with 460 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/resolver/lib/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const expect = require("@truffle/expect");
const provision = require("@truffle/provisioner");

import { ResolverSource } from "./source";
import { EthPMv1, NPM, GlobalNPM, FS, Truffle } from "./sources";
import { EthPMv1, NPM, GlobalNPM, FS, Truffle, ABI } from "./sources";

export class Resolver {
options: any;
Expand All @@ -17,6 +17,7 @@ export class Resolver {
new EthPMv1(options.working_directory),
new NPM(options.working_directory),
new GlobalNPM(),
new ABI(options.working_directory, options.contracts_build_directory),
new FS(options.working_directory, options.contracts_build_directory)
];

Expand Down
49 changes: 49 additions & 0 deletions packages/resolver/lib/sources/abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import path from "path";
import { ContractObject } from "@truffle/contract-schema/spec";
import { generateSolidity } from "abi-to-sol";

import { FS } from "./fs";

export class ABI extends FS {
// requiring artifacts is out of scope for this ResolverSource
// just return `null` here and let another ResolverSource handle it
require(): ContractObject | null {
return null;
}

async resolve(importPath: string, importedFrom: string = "") {
let filePath: string | undefined;
let body: string | undefined;

if (!importPath.endsWith(".json")) {
return { filePath, body };
}

const resolution = await super.resolve(importPath, importedFrom);
if (!resolution) {
return { filePath, body };
}

({ filePath, body } = resolution);

// extract basename twice to support .json and .abi.json
const name = path.basename(path.basename(filePath, ".json"), ".abi");

try {
const abi = JSON.parse(body);

const soliditySource = generateSolidity({
name,
abi,
license: "MIT" // as per the rest of Truffle
});

return {
filePath,
body: soliditySource
};
} catch (e) {
return { filePath, body };
}
}
}
1 change: 1 addition & 0 deletions packages/resolver/lib/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { NPM } from "./npm";
export { GlobalNPM } from "./globalnpm";
export { FS } from "./fs";
export { Truffle } from "./truffle";
export { ABI } from "./abi";
1 change: 1 addition & 0 deletions packages/resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@truffle/contract-sources": "^0.1.8",
"@truffle/expect": "^0.0.15",
"@truffle/provisioner": "^0.2.10",
"abi-to-sol": "^0.2.0",
"debug": "^4.1.1",
"detect-installed": "^2.0.4",
"get-installed-path": "^4.0.8",
Expand Down
12 changes: 7 additions & 5 deletions packages/truffle/test/scenarios/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("truffle build [ @standalone ]", () => {
let config, project;

describe("when there is no build script in config", () => {
beforeEach("set up sandbox", function() {
beforeEach("set up sandbox", function () {
this.timeout(10000);
project = path.join(
__dirname,
Expand Down Expand Up @@ -37,7 +37,7 @@ describe("truffle build [ @standalone ]", () => {
});

describe("when there is a proper build config", () => {
beforeEach("set up sandbox", function() {
beforeEach("set up sandbox", function () {
this.timeout(10000);
project = path.join(
__dirname,
Expand All @@ -48,15 +48,16 @@ describe("truffle build [ @standalone ]", () => {
config.logger = logger;
});
});
it("runs the build script", async () => {
it("runs the build script", async function () {
this.timeout(10000);
await CommandRunner.run("build", config);
const output = logger.contents();
assert(output.includes("'this is the build script'"));
});
});

describe("when there is an object in the build config", () => {
beforeEach("set up sandbox", function() {
beforeEach("set up sandbox", function () {
this.timeout(10000);
project = path.join(
__dirname,
Expand All @@ -67,7 +68,8 @@ describe("truffle build [ @standalone ]", () => {
config.logger = logger;
});
});
it("tells the user it shouldn't use an object", async () => {
it("tells the user it shouldn't use an object", async function () {
this.timeout(10000);
try {
await CommandRunner.run("build", config);
assert(false, "The process should have exited with code 1");
Expand Down
Loading

0 comments on commit f67b577

Please sign in to comment.