From 6bcdb00e1c9faaf940395b44756d4970919fd58c Mon Sep 17 00:00:00 2001 From: sandbubbles <160503471+sandbubbles@users.noreply.github.com> Date: Mon, 14 Oct 2024 23:55:18 +0200 Subject: [PATCH] feat[lang]: support top level `"abi"` key in json interfaces (#4279) some frameworks produce json ABI files with `"abi"` as a top-level key. this decreases a bit of friction for using those ABI files. --- .../unit/cli/vyper_json/test_compile_json.py | 24 +++++++++++++++++++ vyper/compiler/input_bundle.py | 2 ++ 2 files changed, 26 insertions(+) diff --git a/tests/unit/cli/vyper_json/test_compile_json.py b/tests/unit/cli/vyper_json/test_compile_json.py index ef3284cd15..5da98cf20f 100644 --- a/tests/unit/cli/vyper_json/test_compile_json.py +++ b/tests/unit/cli/vyper_json/test_compile_json.py @@ -295,3 +295,27 @@ def test_relative_import_paths(input_json): input_json["sources"]["contracts/potato/baz/potato.vy"] = {"content": "from . import baz"} input_json["sources"]["contracts/potato/footato.vy"] = {"content": "from baz import baz"} compile_from_input_dict(input_json) + + +def test_compile_json_with_abi_top(make_input_bundle): + stream = """ +{ + "abi": [ + { + "name": "validate", + "inputs": [ + { "name": "creator", "type": "address" }, + { "name": "token", "type": "address" }, + { "name": "amount_per_second", "type": "uint256" }, + { "name": "reason", "type": "bytes" } + ], + "outputs": [{ "name": "max_stream_life", "type": "uint256" }] + } + ] +} + """ + code = """ +from . import stream + """ + input_bundle = make_input_bundle({"stream.json": stream, "code.vy": code}) + vyper.compiler.compile_code(code, input_bundle=input_bundle) diff --git a/vyper/compiler/input_bundle.py b/vyper/compiler/input_bundle.py index a928989393..c9eeded3cf 100644 --- a/vyper/compiler/input_bundle.py +++ b/vyper/compiler/input_bundle.py @@ -52,6 +52,8 @@ class ABIInput(CompilerInput): def try_parse_abi(file_input: FileInput) -> CompilerInput: try: s = json.loads(file_input.source_code) + if isinstance(s, dict) and "abi" in s: + s = s["abi"] return ABIInput(**asdict(file_input), abi=s) except (ValueError, TypeError): return file_input