Fix getsourcecode bug

pull/4320/head
nikitosing 3 years ago
parent 59e4e1d707
commit 9305353a96
  1. 1
      CHANGELOG.md
  2. 27
      apps/block_scout_web/lib/block_scout_web/views/api/rpc/contract_view.ex
  3. 3
      apps/explorer/lib/explorer/chain.ex

@ -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 - [#4067](https://github.com/blockscout/blockscout/pull/4067) - Display LP tokens USD value and custom metadata in tokens dropdown at address page
### Fixes ### 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 - [#4274](https://github.com/blockscout/blockscout/pull/4302) - Fix search token-autocomplete
- [#4316](https://github.com/blockscout/blockscout/pull/4316) - Fix `/decompiled-contracts` bug - [#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 - [#4310](https://github.com/blockscout/blockscout/pull/4310) - Fix logo URL redirection, set font-family defaults for chart.js

@ -1,7 +1,9 @@
defmodule BlockScoutWeb.API.RPC.ContractView do defmodule BlockScoutWeb.API.RPC.ContractView do
use BlockScoutWeb, :view use BlockScoutWeb, :view
alias BlockScoutWeb.AddressView
alias BlockScoutWeb.API.RPC.RPCView alias BlockScoutWeb.API.RPC.RPCView
alias Explorer.Chain
alias Explorer.Chain.{Address, DecompiledSmartContract, SmartContract} alias Explorer.Chain.{Address, DecompiledSmartContract, SmartContract}
defguardp is_empty_string(input) when input == "" or input == nil 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(:CompilerVersion, Map.get(contract, :compiler_version, ""))
|> Map.put_new(:OptimizationUsed, contract_optimization) |> Map.put_new(:OptimizationUsed, contract_optimization)
|> Map.put_new(:EVMVersion, Map.get(contract, :evm_version, "")) |> Map.put_new(:EVMVersion, Map.get(contract, :evm_version, ""))
|> insert_additional_sources(address)
end end
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{ defp prepare_contract(%Address{
hash: hash, hash: hash,
smart_contract: nil smart_contract: nil

@ -3537,7 +3537,8 @@ defmodule Explorer.Chain do
nil nil
address -> 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 if address_with_smart_contract.smart_contract do
formatted_code = format_source_code_output(address_with_smart_contract.smart_contract) formatted_code = format_source_code_output(address_with_smart_contract.smart_contract)

Loading…
Cancel
Save