ERC-1155: fix indexer

pull/3902/head
Viktor Baranov 4 years ago
parent 85ddac18aa
commit 0418483b9f
  1. 10
      apps/explorer/lib/explorer/token/balance_reader.ex
  2. 4
      apps/indexer/lib/indexer/token_balances.ex
  3. 124
      apps/indexer/test/indexer/token_balances_test.exs

@ -65,9 +65,13 @@ defmodule Explorer.Token.BalanceReader do
|> Enum.map(&format_balance_request/1) |> Enum.map(&format_balance_request/1)
end end
formatted_balances_requests if Enum.count(formatted_balances_requests) > 0 do
|> Reader.query_contracts(abi) formatted_balances_requests
|> Enum.map(&format_balance_result/1) |> Reader.query_contracts(abi)
|> Enum.map(&format_balance_result/1)
else
[]
end
end end
defp format_balance_request(%{ defp format_balance_request(%{

@ -70,13 +70,13 @@ defmodule Indexer.TokenBalances do
requested_regular_token_balances = requested_regular_token_balances =
regular_token_balances regular_token_balances
|> BalanceReader.get_balances_of() |> BalanceReader.get_balances_of()
|> Stream.zip(token_balances) |> Stream.zip(regular_token_balances)
|> Enum.map(fn {result, token_balance} -> set_token_balance_value(result, token_balance) end) |> Enum.map(fn {result, token_balance} -> set_token_balance_value(result, token_balance) end)
requested_erc1155_token_balances = requested_erc1155_token_balances =
erc1155_token_balances erc1155_token_balances
|> BalanceReader.get_balances_of_with_abi(@erc1155_balance_function_abi) |> BalanceReader.get_balances_of_with_abi(@erc1155_balance_function_abi)
|> Stream.zip(token_balances) |> Stream.zip(erc1155_token_balances)
|> Enum.map(fn {result, token_balance} -> set_token_balance_value(result, token_balance) end) |> Enum.map(fn {result, token_balance} -> set_token_balance_value(result, token_balance) end)
requested_token_balances = requested_regular_token_balances ++ requested_erc1155_token_balances requested_token_balances = requested_regular_token_balances ++ requested_erc1155_token_balances

@ -49,6 +49,42 @@ defmodule Indexer.TokenBalancesTest do
} = List.first(result) } = List.first(result)
end end
test "fetches balances of ERC-1155 tokens" do
address = insert(:address, hash: "0x609991ca0ae39bc4eaf2669976237296d40c2f31")
address_hash_string = Hash.to_string(address.hash)
token_contract_address_hash = "0xf7f79032fd395978acb7069c74d21e5a53206559"
contract_address = insert(:address, hash: token_contract_address_hash)
token = insert(:token, contract_address: contract_address)
data = [
%{
token_contract_address_hash: Hash.to_string(token.contract_address_hash),
address_hash: address_hash_string,
block_number: 1_000,
token_id: 5,
token_type: "ERC-1155"
}
]
get_erc1155_balance_from_blockchain()
{:ok, result} = TokenBalances.fetch_token_balances_from_blockchain(data)
assert [
%{
value: 2,
token_contract_address_hash: ^token_contract_address_hash,
address_hash: ^address_hash_string,
block_number: 1_000,
value_fetched_at: _
}
] = result
end
test "fetches multiple balances of tokens" do test "fetches multiple balances of tokens" do
address_1 = insert(:address, hash: "0xecba3c9ea993b0e0594e0b0a0d361a1f9596e310") address_1 = insert(:address, hash: "0xecba3c9ea993b0e0594e0b0a0d361a1f9596e310")
address_2 = insert(:address, hash: "0x609991ca0ae39bc4eaf2669976237296d40c2f31") address_2 = insert(:address, hash: "0x609991ca0ae39bc4eaf2669976237296d40c2f31")
@ -60,11 +96,18 @@ defmodule Indexer.TokenBalancesTest do
token_1_contract_address_hash = "0x57e93bb58268de818b42e3795c97bad58afcd3fe" token_1_contract_address_hash = "0x57e93bb58268de818b42e3795c97bad58afcd3fe"
token_2_contract_address_hash = "0xe0d0b1dbbcf3dd5cac67edaf9243863fd70745da" token_2_contract_address_hash = "0xe0d0b1dbbcf3dd5cac67edaf9243863fd70745da"
token_3_contract_address_hash = "0x22c1f6050e56d2876009903609a2cc3fef83b415"
token_4_contract_address_hash = "0xf7f79032fd395978acb7069c74d21e5a53206559"
contract_address_1 = insert(:address, hash: token_1_contract_address_hash) contract_address_1 = insert(:address, hash: token_1_contract_address_hash)
contract_address_2 = insert(:address, hash: token_2_contract_address_hash) contract_address_2 = insert(:address, hash: token_2_contract_address_hash)
contract_address_3 = insert(:address, hash: token_3_contract_address_hash)
contract_address_4 = insert(:address, hash: token_4_contract_address_hash)
token_1 = insert(:token, contract_address: contract_address_1) token_1 = insert(:token, contract_address: contract_address_1)
token_2 = insert(:token, contract_address: contract_address_2) token_2 = insert(:token, contract_address: contract_address_2)
token_3 = insert(:token, contract_address: contract_address_3)
token_4 = insert(:token, contract_address: contract_address_4)
data = [ data = [
%{ %{
@ -81,6 +124,20 @@ defmodule Indexer.TokenBalancesTest do
token_id: nil, token_id: nil,
token_type: "ERC-20" token_type: "ERC-20"
}, },
%{
token_contract_address_hash: Hash.to_string(token_3.contract_address_hash),
address_hash: address_2_hash_string,
block_number: 1_000,
token_id: 42,
token_type: "ERC-721"
},
%{
token_contract_address_hash: Hash.to_string(token_4.contract_address_hash),
address_hash: address_2_hash_string,
block_number: 1_000,
token_id: 5,
token_type: "ERC-1155"
},
%{ %{
token_contract_address_hash: Hash.to_string(token_2.contract_address_hash), token_contract_address_hash: Hash.to_string(token_2.contract_address_hash),
address_hash: Hash.to_string(token_2.contract_address_hash), address_hash: Hash.to_string(token_2.contract_address_hash),
@ -105,6 +162,7 @@ defmodule Indexer.TokenBalancesTest do
] ]
get_multiple_balances_from_blockchain() get_multiple_balances_from_blockchain()
get_erc1155_balance_from_blockchain()
{:ok, result} = TokenBalances.fetch_token_balances_from_blockchain(data) {:ok, result} = TokenBalances.fetch_token_balances_from_blockchain(data)
@ -123,6 +181,13 @@ defmodule Indexer.TokenBalancesTest do
block_number: 1_000, block_number: 1_000,
value_fetched_at: _ value_fetched_at: _
}, },
%{
value: 1,
token_contract_address_hash: ^token_3_contract_address_hash,
address_hash: ^address_2_hash_string,
block_number: 1_000,
value_fetched_at: _
},
%{ %{
value: 6_000_000_000_000_000_000_000_000_000, value: 6_000_000_000_000_000_000_000_000_000,
token_contract_address_hash: ^token_2_contract_address_hash, token_contract_address_hash: ^token_2_contract_address_hash,
@ -143,6 +208,13 @@ defmodule Indexer.TokenBalancesTest do
address_hash: ^token_2_contract_address_hash, address_hash: ^token_2_contract_address_hash,
block_number: 1_000, block_number: 1_000,
value_fetched_at: _ value_fetched_at: _
},
%{
value: 2,
token_contract_address_hash: ^token_4_contract_address_hash,
address_hash: ^address_2_hash_string,
block_number: 1_000,
value_fetched_at: _
} }
] = result ] = result
end end
@ -266,6 +338,41 @@ defmodule Indexer.TokenBalancesTest do
) )
end end
defp get_erc1155_balance_from_blockchain() do
expect(
EthereumJSONRPC.Mox,
:json_rpc,
fn requests, _options ->
{:ok,
requests
|> Enum.map(fn
%{
id: id,
method: "eth_call",
params: [
%{
data:
"0x00fdd58e000000000000000000000000609991ca0ae39bc4eaf2669976237296d40c2f310000000000000000000000000000000000000000000000000000000000000005",
to: "0xf7f79032fd395978acb7069c74d21e5a53206559"
},
_
]
} ->
%{
id: id,
jsonrpc: "2.0",
result: "0x0000000000000000000000000000000000000000000000000000000000000002"
}
req ->
IO.inspect("Gimme req")
IO.inspect(req)
end)
|> Enum.shuffle()}
end
)
end
defp get_multiple_balances_from_blockchain() do defp get_multiple_balances_from_blockchain() do
expect( expect(
EthereumJSONRPC.Mox, EthereumJSONRPC.Mox,
@ -298,6 +405,23 @@ defmodule Indexer.TokenBalancesTest do
result: "0x000000000000000000000000000000000000000009b18ab5df7180b6b8000000" result: "0x000000000000000000000000000000000000000009b18ab5df7180b6b8000000"
} }
%{
id: id,
method: "eth_call",
params: [
%{
data: "0x70a08231000000000000000000000000609991ca0ae39bc4eaf2669976237296d40c2f31",
to: "0x22c1f6050e56d2876009903609a2cc3fef83b415"
},
_
]
} ->
%{
id: id,
jsonrpc: "2.0",
result: "0x0000000000000000000000000000000000000000000000000000000000000001"
}
%{ %{
id: id, id: id,
method: "eth_call", method: "eth_call",

Loading…
Cancel
Save