Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

0x01 Web3 APIs

docs_livebook/0x01_web3_apis.livemd

0x01 Web3 APIs

0x01 Ethereumex Config

config.exs:

config :ethereumex,
  url: "https://ropsten.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161" # provider
  # tips: u can get provider from metamask rpc setting!

0x02 Type Translator

Impl Type Translator to decode data, the funcs will be call in the bellow section:

defmodule Utils.TypeTranslator do

  def data_to_int(raw) do
    raw
    |> hex_to_bin()
    |> ABI.TypeDecoder.decode_raw([{:uint, 256}])
    |> List.first()
  end

  def data_to_str(raw) do
    raw
    |> hex_to_bin()
    |> ABI.TypeDecoder.decode_raw([:string])
    |> List.first()
  end

  def data_to_addr(raw) do
    addr_bin =
      raw
      |> hex_to_bin()
      |> ABI.TypeDecoder.decode_raw([:address])
      |> List.first()

    "0x" <> Base.encode16(addr_bin, case: :lower)
  end

  def hex_to_bin(hex) do
    hex
    |> String.slice(2..-1)
    |> Base.decode16!(case: :lower)
  end

  def hex_to_int(hex) do
    hex
    |> String.slice(2..-1)
    |> String.to_integer(16)
  end
  def hex_to_bytes(hex) do
    hex
    |> String.slice(2..-1)
    |> Base.decode16(case: :mixed)
  end

  @spec addr_to_bin(String.t()) :: Binary.t()
  def addr_to_bin(addr_str) do
    addr_str
    |> String.replace("0x", "")
    |> Base.decode16!(case: :mixed)
  end
end

0x03 APIs about Fetch Data

It’s ok to use another provider to override the default provider,just add url: "provider_uri" as the last param of the function. > attention: > some func has optional param, such as getBalance it can be search in: > https://web3js.readthedocs.io/en/v1.7.0/web3-eth.html?highlight=Balance#getbalance > and see in: > https://github.com/mana-ethereum/ethereumex/blob/1edc90411736a1c1dfb919fda3522f158c1af2b8/lib/ethereumex/client/behaviour.ex

2.1 get block number

Ethereumex.HttpClient.eth_block_number()
{:ok, "0xb1ca1b"}

The Result is hex, so we should translate it.

{:ok, payload} = Ethereumex.HttpClient.eth_block_number()
IO.puts("the newest block number is : #{Utils.TypeTranslator.hex_to_int(payload)}")
the newest block number is : 11651621
:ok

2.2 get block by number

Params:

Ethereumex.HttpClient.eth_get_block_by_number(block_number_in_hex, full?)
Ethereumex.HttpClient.eth_get_block_by_number("0xb1ca1b", false)
{:ok,
 %{
   "baseFeePerGas" => "0x8",
   "difficulty" => "0x3fcbcdcd1",
   "extraData" => "0x746869732069732074686520776179",
   "gasLimit" => "0x7a1200",
   "gasUsed" => "0x5768d1",
   "hash" => "0xbdeff7f69d8c97fb7d32e112ba231a75892ff1743837518762306c0b9b331132",
   "logsBloom" => "0x000002000000200000000000810000000080000240010000008100020000010000002000000010003c00400000000000000000000008020010100201502000000400000008080002800000083800000000010000040000000000240080000001000400000620000000000000080008100000000000080000000000100000004000000080000004200040100000000000008004090000800800004000004000010200000000100100000000102000000000000000000100c0000000080800000000000006100000000404048000420000000000800900001000000000200020000418010200100080020040000000000000042024002000c01000000000000000",
   "miner" => "0x68268f12253f69f66b188c95b8106b2f847859fb",
   "mixHash" => "0x6250b56cf6f0d9bd2719246e1087b486d1583470c1047dbf78b297f61708be36",
   "nonce" => "0xb20673dd5f813282",
   "number" => "0xb1ca1b",
   "parentHash" => "0x442fd4bb0a2f6d4f51dc3d191a1e30d71a22dd5fe210c0d1bcfb1322c69ef7ba",
   "receiptsRoot" => "0xaba50abb1315e952fa4bce09dce308a51070656b30964c3821f62bc49a15abcd",
   "sha3Uncles" => "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
   "size" => "0x5d82",
   "stateRoot" => "0xcdcf44b949a34a208b68589d05ccc7f0c5d1d7551877e5a73fb47b39d0981b75",
   "timestamp" => "0x61bf5048",
   "totalDifficulty" => "0x8477b617b675a9",
   "transactions" => ["0x441c008ff96cbe95ca3b9889cf044c837b90e3001b9eba24fa430f6757a1c046",
    "0x591ae8d8e3b320822c9efe35c6dd9ec286a3884a6476760cc623ffc5fb46f874",
    "0x3f2e5b6e3950a7493559f17cc36031139ee71e8f69a020812fba1aaf99213d0e",
    "0x112263ff0705dcb5b03b538ef6cd5a3f2d0e9fcb3c81385b8200d200b84e7822",
    "0x202c88fa0acda9ffc5a1ec55a0a3f25c7addbe8071d4b200ad22951fefffa0f9",
    "0xc893445253b61fda394fa94af7b73f31daef1e8a4c2c7ac30193c87ebb4c438e",
    "0x5258a3a6fdef760e3874624875af1ecc37e4a3f7bf452d0f518fa4a80b19173d",
    "0x63e271ece48957e77f8cfed7a63f4200aba527a244fda951675d36bebc4224f9",
    "0x9b98fbeecea48fd725343456b63cd05e33e32d3596fc8eee9f6a931573102e5a",
    "0x4d24c6bc0b5eaa6b678b9f1815cac9fa4a44c9524bebac0ca409e22e840c8591",
    "0x8b702784a79ca9da90811140ada02a791b173926a2e0ed09fe2f0035cea045b3"],
   "transactionsRoot" => "0x67485c3ef83cd5bfc580fc87f40b476ad576031033f814af25783c4335299ca6",
   "uncles" => []
 }}

2.3 get transaction by hash

Ethereumex.HttpClient.eth_get_transaction_by_hash(
  "0x441c008ff96cbe95ca3b9889cf044c837b90e3001b9eba24fa430f6757a1c046"
)
{:ok,
 %{
   "blockHash" => "0xbdeff7f69d8c97fb7d32e112ba231a75892ff1743837518762306c0b9b331132",
   "blockNumber" => "0xb1ca1b",
   "from" => "0x57d8493313acbfb4f788e8d74e36ea90118435aa",
   "gas" => "0x249f0",
   "gasPrice" => "0x9f81246c",
   "hash" => "0x441c008ff96cbe95ca3b9889cf044c837b90e3001b9eba24fa430f6757a1c046",
   "input" => "0x2505c3d9012604f30fea84d281698f6a8aab8a6b5ab54c4d0f4a38382a918d10fd768e3b02c04d8b650f44092278a7cb1e1028c82025dff622db96c934b611b84cc8de5a000000000000000000000000000000000000000000000000000000000000a068000000000000000000000000000000000000000000000000000000746a528800",
   "nonce" => "0x185d6",
   "r" => "0xce8adae8a39094a455df386ec19d30a4ea8ee4b7d87f8922740a268c0cf7ab78",
   "s" => "0xd36f5efc3d2a5e9bb083085846162997da7aa952002e5d1cb0992e4b4b50fb5",
   "to" => "0x014f738ead8ec6c50bcd456a971f8b84cd693bbe",
   "transactionIndex" => "0x0",
   "type" => "0x0",
   "v" => "0x2a",
   "value" => "0x0"
 }}

2.4 get transaction receipt

Ethereumex.HttpClient.eth_get_transaction_receipt(
  "0x441c008ff96cbe95ca3b9889cf044c837b90e3001b9eba24fa430f6757a1c046"
)
{:ok,
 %{
   "blockHash" => "0xbdeff7f69d8c97fb7d32e112ba231a75892ff1743837518762306c0b9b331132",
   "blockNumber" => "0xb1ca1b",
   "contractAddress" => nil,
   "cumulativeGasUsed" => "0x168ae",
   "effectiveGasPrice" => "0x9f81246c",
   "from" => "0x57d8493313acbfb4f788e8d74e36ea90118435aa",
   "gasUsed" => "0x168ae",
   "logs" => [
     %{
       "address" => "0x8707a5bf4c2842d46b31a405ba41b858c0f876c4",
       "blockHash" => "0xbdeff7f69d8c97fb7d32e112ba231a75892ff1743837518762306c0b9b331132",
       "blockNumber" => "0xb1ca1b",
       "data" => "0x000000000000000000000000000000000000000000000000000000746a528800",
       "logIndex" => "0x0",
       "removed" => false,
       "topics" => ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
        "0x00000000000000000000000057d8493313acbfb4f788e8d74e36ea90118435aa",
        "0x000000000000000000000000014f738ead8ec6c50bcd456a971f8b84cd693bbe"],
       "transactionHash" => "0x441c008ff96cbe95ca3b9889cf044c837b90e3001b9eba24fa430f6757a1c046",
       "transactionIndex" => "0x0"
     },
     %{
       "address" => "0x8707a5bf4c2842d46b31a405ba41b858c0f876c4",
       "blockHash" => "0xbdeff7f69d8c97fb7d32e112ba231a75892ff1743837518762306c0b9b331132",
       "blockNumber" => "0xb1ca1b",
       "data" => "0xffffffffffffffffffffffffffffffffffffffffffffffffffa60bfff91c79ff",
       "logIndex" => "0x1",
       "removed" => false,
       "topics" => ["0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
        "0x00000000000000000000000057d8493313acbfb4f788e8d74e36ea90118435aa",
        "0x000000000000000000000000014f738ead8ec6c50bcd456a971f8b84cd693bbe"],
       "transactionHash" => "0x441c008ff96cbe95ca3b9889cf044c837b90e3001b9eba24fa430f6757a1c046",
       "transactionIndex" => "0x0"
     },
     %{
       "address" => "0x014f738ead8ec6c50bcd456a971f8b84cd693bbe",
       "blockHash" => "0xbdeff7f69d8c97fb7d32e112ba231a75892ff1743837518762306c0b9b331132",
       "blockNumber" => "0xb1ca1b",
       "data" => "0x00000000000000000000000057d8493313acbfb4f788e8d74e36ea90118435aa012604f30fea84d281698f6a8aab8a6b5ab54c4d0f4a38382a918d10fd768e3b000000000000000000000000000000000000000000000000000000000000a06802c04d8b650f44092278a7cb1e1028c82025dff622db96c934b611b84cc8de5a000000000000000000000000000000000000000000000000000000746a528800000000000000000000000000000000000000000000000000000000746a528800",
       "logIndex" => "0x2",
       "removed" => false,
       "topics" => ["0x06724742ccc8c330a39a641ef02a0b419bd09248360680bb38159b0a8c2635d6"],
       "transactionHash" => "0x441c008ff96cbe95ca3b9889cf044c837b90e3001b9eba24fa430f6757a1c046",
       "transactionIndex" => "0x0"
     }
   ],
   "logsBloom" => "0x00000000000000000000000000000000000000000000000000000000000000000000000000001000000040000000000000000000000000000000000000200000000000000000000280000008180000000000000000000000000004000000000000000000020000000000000000000000000000000000000000000010000000000000000000000000000010000000000000000400000000000000000000000001020000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000800000000000000000000000000410000000000000000000000000000000040000000000800000000000000000",
   "status" => "0x1",
   "to" => "0x014f738ead8ec6c50bcd456a971f8b84cd693bbe",
   "transactionHash" => "0x441c008ff96cbe95ca3b9889cf044c837b90e3001b9eba24fa430f6757a1c046",
   "transactionIndex" => "0x0",
   "type" => "0x0"
 }}

2.5 get balance

Ethereumex.HttpClient.eth_get_balance("0x73c7448760517E3E6e416b2c130E3c6dB2026A1d", "latest")
{:ok, "0x0"}