diff --git a/apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex b/apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex index cb23687cda..e9b01836c4 100644 --- a/apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex +++ b/apps/explorer/lib/explorer/smart_contract/solidity/code_compiler.ex @@ -61,6 +61,8 @@ defmodule Explorer.SmartContract.Solidity.CodeCompiler do } """ def run(name, compiler_version, code, optimize, external_libs \\ %{}) do + external_libs_string = Jason.encode!(external_libs) + {response, _status} = System.cmd( "node", @@ -69,16 +71,15 @@ defmodule Explorer.SmartContract.Solidity.CodeCompiler do code, compiler_version, optimize_value(optimize), - @new_contract_name + @new_contract_name, + external_libs_string ] ) with {:ok, contracts} <- Jason.decode(response), %{"abi" => abi, "evm" => %{"deployedBytecode" => %{"object" => bytecode}}} <- get_contract_info(contracts, name) do - bytecode_with_libraries = add_library_addresses(bytecode, external_libs) - - {:ok, %{"abi" => abi, "bytecode" => bytecode_with_libraries, "name" => name}} + {:ok, %{"abi" => abi, "bytecode" => bytecode, "name" => name}} else {:error, %Jason.DecodeError{}} -> {:error, :compilation} @@ -105,16 +106,6 @@ defmodule Explorer.SmartContract.Solidity.CodeCompiler do end end - defp add_library_addresses(bytecode, external_libs) do - Enum.reduce(external_libs, bytecode, fn {library_name, address}, acc -> - placeholder = String.replace(@new_contract_name, ".", "\.") <> ":" <> library_name - regex = Regex.compile!("_+#{placeholder}_+") - address = String.replace(address, "0x", "") - - String.replace(acc, regex, address) - end) - end - def parse_error(%{"error" => error}), do: {:error, [error]} def parse_error(%{"errors" => errors}), do: {:error, errors} def parse_error({:error, _} = error), do: error diff --git a/apps/explorer/priv/compile_solc.js b/apps/explorer/priv/compile_solc.js index 17c2c8bd8e..3c4bd0b78b 100755 --- a/apps/explorer/priv/compile_solc.js +++ b/apps/explorer/priv/compile_solc.js @@ -6,6 +6,7 @@ var sourceCode = process.argv[2]; var version = process.argv[3]; var optimize = process.argv[4]; var newContractName = process.argv[5]; +var externalLibraries = JSON.parse(process.argv[6]) var compiled_code = solc.loadRemoteVersion(version, function (err, solcSnapshot) { if (err) { @@ -24,6 +25,9 @@ var compiled_code = solc.loadRemoteVersion(version, function (err, solcSnapshot) enabled: optimize == '1', runs: 200 }, + libraries: { + [newContractName]: externalLibraries + }, outputSelection: { '*': { '*': ['*'] diff --git a/apps/explorer/priv/repo/migrations/20190208113202_add_unique_index_to_rewards.exs b/apps/explorer/priv/repo/migrations/20190208113202_add_unique_index_to_rewards.exs new file mode 100644 index 0000000000..cd306c7977 --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20190208113202_add_unique_index_to_rewards.exs @@ -0,0 +1,12 @@ +defmodule Explorer.Repo.Migrations.AddUniqueIndexToRewards do + use Ecto.Migration + + def change do + create( + unique_index( + :block_rewards, + [:address_hash, :block_hash, :address_type] + ) + ) + end +end diff --git a/apps/indexer/lib/indexer/block/reward/fetcher.ex b/apps/indexer/lib/indexer/block/reward/fetcher.ex index 7e89166c74..0c190f9719 100644 --- a/apps/indexer/lib/indexer/block/reward/fetcher.ex +++ b/apps/indexer/lib/indexer/block/reward/fetcher.ex @@ -177,14 +177,18 @@ defmodule Indexer.Block.Reward.Fetcher do |> Enum.map(& &1.block_hash) |> Chain.gas_payment_by_block_hash() - Enum.map(beneficiaries_params, fn %{block_hash: block_hash} = beneficiary -> - case gas_payment_by_block_hash do - %{^block_hash => gas_payment} -> - {:ok, minted} = Wei.cast(beneficiary.reward) - %{beneficiary | reward: Wei.sum(minted, gas_payment)} - - _ -> - beneficiary + Enum.map(beneficiaries_params, fn %{block_hash: block_hash, address_type: address_type} = beneficiary -> + if address_type == :validator do + case gas_payment_by_block_hash do + %{^block_hash => gas_payment} -> + {:ok, minted} = Wei.cast(beneficiary.reward) + %{beneficiary | reward: Wei.sum(minted, gas_payment)} + + _ -> + beneficiary + end + else + beneficiary end end) end