From 9305353a966f4fc8a4f5bfe76d382006890c6e9b Mon Sep 17 00:00:00 2001 From: nikitosing Date: Fri, 25 Jun 2021 14:14:18 +0700 Subject: [PATCH] Fix getsourcecode bug --- CHANGELOG.md | 1 + .../views/api/rpc/contract_view.ex | 27 +++++++++++++++++++ apps/explorer/lib/explorer/chain.ex | 3 ++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12c622880c..0d04ea4d2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - [#4067](https://github.com/blockscout/blockscout/pull/4067) - Display LP tokens USD value and custom metadata in tokens dropdown at address page ### Fixes +- [#4320](https://github.com/blockscout/blockscout/pull/4320) - Fix absence of imported smart-contracts' source code in `getsourcecode` api method - [#4274](https://github.com/blockscout/blockscout/pull/4302) - Fix search token-autocomplete - [#4316](https://github.com/blockscout/blockscout/pull/4316) - Fix `/decompiled-contracts` bug - [#4310](https://github.com/blockscout/blockscout/pull/4310) - Fix logo URL redirection, set font-family defaults for chart.js diff --git a/apps/block_scout_web/lib/block_scout_web/views/api/rpc/contract_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api/rpc/contract_view.ex index 41a0b7734d..3f958dedd6 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api/rpc/contract_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api/rpc/contract_view.ex @@ -1,7 +1,9 @@ defmodule BlockScoutWeb.API.RPC.ContractView do use BlockScoutWeb, :view + alias BlockScoutWeb.AddressView alias BlockScoutWeb.API.RPC.RPCView + alias Explorer.Chain alias Explorer.Chain.{Address, DecompiledSmartContract, SmartContract} defguardp is_empty_string(input) when input == "" or input == nil @@ -140,9 +142,34 @@ defmodule BlockScoutWeb.API.RPC.ContractView do |> Map.put_new(:CompilerVersion, Map.get(contract, :compiler_version, "")) |> Map.put_new(:OptimizationUsed, contract_optimization) |> Map.put_new(:EVMVersion, Map.get(contract, :evm_version, "")) + |> insert_additional_sources(address) end end + defp insert_additional_sources(output, address) do + additional_sources_from_twin = Chain.get_address_verified_twin_contract(address.hash).additional_sources + + additional_sources = + if AddressView.smart_contract_verified?(address), + do: address.smart_contract_additional_sources, + else: additional_sources_from_twin + + additional_sources_array = + if additional_sources, + do: + Enum.map(additional_sources, fn src -> + %{ + Filename: src.file_name, + SourceCode: SmartContract.add_submitted_comment(src.contract_source_code, src.inserted_at) + } + end), + else: [] + + if additional_sources_array == [], + do: output, + else: Map.put_new(output, :AdditionalSources, additional_sources_array) + end + defp prepare_contract(%Address{ hash: hash, smart_contract: nil diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index ccc72d88a8..83e6e6fc06 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -3537,7 +3537,8 @@ defmodule Explorer.Chain do nil address -> - address_with_smart_contract = Repo.preload(address, [:smart_contract, :decompiled_smart_contracts]) + address_with_smart_contract = + Repo.preload(address, [:smart_contract, :decompiled_smart_contracts, :smart_contract_additional_sources]) if address_with_smart_contract.smart_contract do formatted_code = format_source_code_output(address_with_smart_contract.smart_contract)