This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3481 from trufflesuite/revert-3478-revert-3392-im…
…port-abi-json Allow importing ABI JSON files in Solidity
- Loading branch information
Showing
6 changed files
with
460 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.