From 4c51a862a866faf91b15629d8b96a96a314269fb Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Fri, 26 May 2023 16:10:17 +0300 Subject: [PATCH] Return contract type in address view --- CHANGELOG.md | 2 ++ .../views/api/v2/address_view.ex | 18 +++++++++++++++++- .../api/v2/address_controller_test.exs | 3 ++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7939109636..6fdec21574 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Current ### Features + - [#7513](https://github.com/blockscout/blockscout/pull/7513) - Add Polygon Edge support - [#7532](https://github.com/blockscout/blockscout/pull/7532) - Handle empty id in json rpc responses @@ -10,6 +11,7 @@ ### Fixes +- [#7564](https://github.com/blockscout/blockscout/pull/7564) - Return contract type in address view - [#7562](https://github.com/blockscout/blockscout/pull/7562) - Remove fallback from Read methods - [#7537](https://github.com/blockscout/blockscout/pull/7537), [#7553](https://github.com/blockscout/blockscout/pull/7553) - Withdrawals fixes and improvements - [#7546](https://github.com/blockscout/blockscout/pull/7546) - API v2: fix today coin price (use in-memory or cached in DB value) diff --git a/apps/block_scout_web/lib/block_scout_web/views/api/v2/address_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api/v2/address_view.ex index e044cf7ffb..90d85355e2 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api/v2/address_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api/v2/address_view.ex @@ -108,7 +108,8 @@ defmodule BlockScoutWeb.API.V2.AddressView do "has_tokens" => Chain.check_if_tokens_at_address(address.hash, @api_true), "has_token_transfers" => Chain.check_if_token_transfers_at_address(address.hash, @api_true), "watchlist_address_id" => Chain.select_watchlist_address_id(get_watchlist_id(conn), address.hash), - "has_beacon_chain_withdrawals" => Chain.check_if_withdrawals_at_address(address.hash, @api_true) + "has_beacon_chain_withdrawals" => Chain.check_if_withdrawals_at_address(address.hash, @api_true), + "contract_type" => contract_type(address) }) end @@ -170,4 +171,19 @@ defmodule BlockScoutWeb.API.V2.AddressView do TokenView.render("token_instance.json", %{token_instance: token_instance, token: token}) end + + @spec contract_type(term) :: nil | :solidity | :vyper | :yul + def contract_type(%Address{smart_contract: nil}), do: nil + + def contract_type(%Address{smart_contract: %SmartContract{}} = address) do + if address.smart_contract.is_vyper_contract do + :vyper + else + if address.smart_contract.abi do + :solidity + else + :yul + end + end + end end diff --git a/apps/block_scout_web/test/block_scout_web/controllers/api/v2/address_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/api/v2/address_controller_test.exs index b414a917bc..0902bf47eb 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/api/v2/address_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/api/v2/address_controller_test.exs @@ -67,7 +67,8 @@ defmodule BlockScoutWeb.API.V2.AddressControllerTest do "has_tokens" => false, "has_token_transfers" => false, "watchlist_address_id" => nil, - "has_beacon_chain_withdrawals" => false + "has_beacon_chain_withdrawals" => false, + "contract_type" => nil } request = get(conn, "/api/v2/addresses/#{Address.checksum(address.hash)}")