Giter Club home page Giter Club logo

eth's Introduction

Hex Version Hex docs

ETH

The essential Elixir library for interacting with Ethereum blockchain. You can now create Ethereum wallets, query the blockchain and sign/send transactions, all from Elixir / Erlang Virtual Machine.

Example

  wallet = ETH.Wallet.create
  # %{eth_address: "0x31CF67A272A23C7A11128C97FC3B2F4C13AFD87F",
  #   mnemonic_phrase: "similar bleak naive absorb picnic avoid sponsor canoe inform misery crisp hotel critic have parent couch wrong survey staff primary wet wear velvet horse",
  #   private_key: "C8E2F24A806A422034990C7391B4CEB7133CD3680987FEBB5750555F99F0FC83",
  #   public_key: "04C4AA07F234226CA90FB3E8BB1590D5BEB703E449700FE0B2DF539A948289EA75220CC837CA68F429F3FB3D6677B2D63CF66277888B8209D0B3F3229CE339654C"}

  specific_private_key = "9756371A51D7FC25EDFC95A49DEF3806ED34DF2EBCA2065E543369E708C47374"
  another_wallet = ETH.Wallet.create(specific_private_key)
  # %{eth_address: "0x6A26B49D8046DC5B74D41E29F9A5CA7AD78EEC9B",
  #   mnemonic_phrase: "nuclear random shoot photo lemon base retire naive pig urge sock assist spy hurdle road nephew alien verify art stable identify giraffe soccer mushroom",
  #   private_key: "9756371A51D7FC25EDFC95A49DEF3806ED34DF2EBCA2065E543369E708C47374",
  #   public_key: "04D1F70F6048D1E22FBEBBF3AF462E3356747AB2BB81EC269C600BE6A53C3223472AA336DF0060719C6F3AEC45E40AE57ED39735B61B8F5EF989466D46CA1B72C0"}

  accounts_in_your_client = ETH.get_accounts
  # ["0xfdf5d02f2082753dda0817492d6efff7e76e47aa",
  #  "0xb9906b679aa8edd03fdf7fe396af4d9a77af4108",
  #  "0xae6463bf32efc106ad4300d902e572e1c43e6e9c",
  #  "0xb764c82ae23467be2cf90ab9019ee2464a3946f9",
  #  "0xfc5b5a6cd171f4123439f28fad9986c70572b35f",
  #  "0x7605c8812cfb51a7d2d16e598f521c9302d0ed7f",
  #  "0x7dab29cc88c2ecd69ec216b7d089a82bb95fe1ad",
  #  "0xba3a30f3c4fd2b4a44936b42ceea87ec3e53294a",
  #  "0xb3f4869ce14d6bbd659dc5d2f9a515b58b2765d2",
  #  "0x8c9cec7feacdbbf472ebcc4f61224d83c880896b"]

  first_account_in_your_client = List.first(accounts_in_your_client)
  first_account_private_key = "f121f608500f7e3379c813aa6df62864e35aa0b6cd11a2ff2c20ac84b5771fb2"

  ETH.get_balance("0x31CF67A272A23C7A11128C97FC3B2F4C13AFD87F") # {:ok, 0} # this account holds no ether
  ETH.get_balance!(first_account_in_your_client, :wei) # 1.0e20 -> in this example this address holds 100 ether / 1.0e20 wei

  ETH.get_transaction_count(first_account_in_your_client) # {:ok, 0}

  {:ok, tx_hash} = ETH.send_transaction(%{
    from: first_account_in_your_client, to: wallet[:eth_address], value: 22
  }, first_account_private_key)
  # {:ok, "0x13893d677251ddb9259263490504f3e611a0a7bff23b108641d2cb08b7af21dc"}

  ETH.get_transaction!(tx_hash)
  # %{block_hash: "0xf9917088fc6750677cc1cfb4f7dcab453b21c7de2cb22ed7e6753df058bec5cf",
  #   block_number: 1, from: "0xfdf5d02f2082753dda0817492d6efff7e76e47aa",
  #   gas: 21000, gas_price: 20000000000,
  #   hash: "0x13893d677251ddb9259263490504f3e611a0a7bff23b108641d2cb08b7af21dc",
  #   input: "0", nonce: 0, to: "0x725316bb37d202b0eb203cd83238c31e983a7936",
  #   transaction_index: 0, value: 22}

  ETH.get_balance!(first_account_in_your_client) # 99.99958 # in ether
  ETH.get_balance(wallet[:eth_address], :wei) # {:ok, 22.0} # in wei

Warning: This library uses the Ethereum JSON-RPC under the hood, so you need an ethereum client such as parity/geth or testrpc to use of most of the API.

Credits

  • Izel Nakri - I reverse engineered ethereum JavaScript libraries in Elixir so you don't have to.

Additionally this library wouldnt exist without the libraries below:

  • ExRLP
  • Ethereumex
  • keccakf1600
  • libsecp256k1

NOTE:

To properly use this package, you will need to run rebar compile eunit for libsecp256k1.

TODO:

Ethereum Smart Contracts, small enhancements

Installation

If available in Hex, the package can be installed by adding eth to your list of dependencies in mix.exs:

def deps do
  [
    {:eth, "~> 0.6.5"}
  ]
end

Running the tests

Install a testrpc(now renamed to ganache) to run ethereum locally: npm install -g ganache

Elixir tests automatically spawns and kills the ganache process. All you need to do now is to run the tests in sequence thus --seed 0: mix test --seed 0

The docs can be found at https://hexdocs.pm/eth.

eth's People

Contributors

awoldes avatar ayrat555 avatar baxterjfinch avatar dependabot-preview[bot] avatar dependabot[bot] avatar derekbrown avatar dimitarvp avatar izelnakri avatar lurodrigo avatar ontofractal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eth's Issues

Could not compile dependency :ex_secp256k1

The current master branch from commit 213c8a4

System spec:
Mac OS
Elixir version: Elixir 1.11.4 (compiled with Erlang/OTP 23)
Erlang/OTP version: Erlang/OTP 23 [erts-11.2.2.1]

    Updating crates.io index
error: failed to select a version for the requirement `crypto-mac = "^0.7"`
candidate versions found which didn't match: 0.11.1, 0.11.0, 0.10.1, ...
location searched: crates.io index
required by package `hmac v0.7.0`
    ... which is depended on by `hmac-drbg v0.2.0`
    ... which is depended on by `libsecp256k1 v0.3.5`
    ... which is depended on by `exsecp256k1 v0.1.0 (/Users/barrakuda/Documents/code/eth/deps/ex_secp256k1/native/exsecp256k1)`
could not compile dependency :ex_secp256k1, "mix compile" failed. You can recompile this dependency with "mix deps.compile ex_secp256k1", update it with "mix deps.update ex_secp256k1" or clean it with "mix deps.clean ex_secp256k1"
** (RuntimeError) Rust NIF compile error (rustc exit code 101)
    (rustler 0.21.1) lib/mix/tasks/compile.rustler.ex:68: Mix.Tasks.Compile.Rustler.compile_crate/1
    (elixir 1.11.4) lib/enum.ex:1411: Enum."-map/2-lists^map/1-0-"/2
    (rustler 0.21.1) lib/mix/tasks/compile.rustler.ex:15: Mix.Tasks.Compile.Rustler.run/1
    (mix 1.11.4) lib/mix/task.ex:394: Mix.Task.run_task/3
    (mix 1.11.4) lib/mix/tasks/compile.all.ex:90: Mix.Tasks.Compile.All.run_compiler/2
    (mix 1.11.4) lib/mix/tasks/compile.all.ex:70: Mix.Tasks.Compile.All.compile/4
    (mix 1.11.4) lib/mix/tasks/compile.all.ex:57: Mix.Tasks.Compile.All.with_logger_app/2
    (mix 1.11.4) lib/mix/tasks/compile.all.ex:35: Mix.Tasks.Compile.All.run/1

Issues compiling keccakf1600

Erlang/OTP 24 [erts-12.1.4] installed on M1 Mac running Big Sur.

Dependencies downloaded just fine. Had to export the C_INCLUDE_PATH and make sure it included my Erlang's NIF files. Currently stuck at the following error message when trying to run the elixir program via iex:

Erlang/OTP 24 [erts-12.1.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [dtrace]

 C      decaf-utils.c
 C      keccakf1600_nif.c
 C      shake.c
 LD     keccakf1600.so
ld: warning: directory not found for option '-L/usr/local/Cellar/erlang/23.1.1/lib/erlang/lib/erl_interface-4.0.1/lib'
ld: library not found for -lerl_interface
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/Users/USERNAME/workspace/PROJECT/deps/keccakf1600/c_src/../priv/keccakf1600.so] Error 1
===> Hook for compile failed!

** (Mix) Could not compile dependency :keccakf1600, "/Users/USERNAME/.mix/rebar3 bare compile --paths /Users/USERNAME/workspace/PROJECT/_build/local/lib/*/ebin" command failed. You can recompile this dependency with "mix deps.compile keccakf1600", update it with "mix deps.update keccakf1600" or clean it with "mix deps.clean keccakf1600"

Broken dependency libsecp256k1

Hey

I've just started to integrate with ETH and on calling ETH.wallet.create I get the following error:
image

I've noticed that libsecp256k1 is meant to be pinned to 0.1.3, however recently it was upgraded to 0.1.4. It seems that this version is automatically installed, even with the version being pinned to 0.1.3. Was there a breaking change in this version bump? I can't add an issue on the repo.

Any help would be great for this!

Cheers
Austin

send_transaction fails to format call data

This function sends invalid data to the client:

** (MatchError) no match of right hand side value: {:error, %{"code" => -32602, "message" => "invalid argument 0: json: cannot unmarshal hex string without 0x prefix into Go value of type hexutil.Bytes"}}
    (eth 0.5.0) lib/eth/transaction.ex:88: ETH.Transaction.send_transaction!/2

This is the relevant code. The transaction is signed, then hex encoded.

def send_transaction(params, private_key) do
    set_default_from(params, private_key)
    |> build
    |> sign_transaction(private_key)
    |> Base.encode16()
    |> send
end

By prepending 0x to the transaction, geth is happy.

 def send_transaction(params, private_key) do
    set_default_from(params, private_key)
    |> build
    |> sign_transaction(private_key)
    |> Base.encode16()
    |> (fn transaction -> "0x" <> transaction end).()
    |> send
  end

Right now we are using an infura endpoint. The errors comes from geth in this case.

Are we doing something wrong, or infura, or is this a bug in this library?

We would be happy to supply a PR for the latter.

Thanks in advance!

AV Alert when compiling eth

HI, I was in the process of compiling ETH on Windows when my AV alerted to the binary conftest.exe, it claims it is infected with Win32:Malware-gen

==> mix_erlang_tasks
Compiling 4 files (.ex)
warning: String.to_char_list/1 is deprecated. Use String.to_charlist/1 instead
Found at 2 locations:
lib/util.ex:13: MixErlangTasks.Util.compile_files/2
lib/util.ex:14: MixErlangTasks.Util.compile_files/2

warning: String.to_char_list/1 is deprecated. Use String.to_charlist/1 instead
Found at 2 locations:
lib/mix/tasks/ct.ex:31: Mix.Tasks.Ct.run/1
lib/mix/tasks/ct.ex:32: Mix.Tasks.Ct.run/1

Generated mix_erlang_tasks app
c_src/build_deps.sh
Cloning into 'secp256k1'...
remote: Enumerating objects: 29, done.
remote: Counting objects: 100% (29/29), done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 5919 (delta 9), reused 19 (delta 6), pack-reused 5890
Receiving objects: 100% (5919/5919), 2.48 MiB | 1.70 MiB/s, done.
Resolving deltas: 100% (4142/4142), done.
HEAD is now at d333521 Merge #494: Support OpenSSL versions >= 1.1 for ENABLE_OPENSSL_TESTS
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, build-aux'. libtoolize: copying file build-aux/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIR, build-aux/m4'. libtoolize: copying file build-aux/m4/libtool.m4'
libtoolize: copying file build-aux/m4/ltoptions.m4' libtoolize: copying file build-aux/m4/ltsugar.m4'
libtoolize: copying file build-aux/m4/ltversion.m4' libtoolize: copying file build-aux/m4/lt~obsolete.m4'
configure.ac:26: installing build-aux/compile' configure.ac:5: installing build-aux/config.guess'
configure.ac:5: installing build-aux/config.sub' configure.ac:9: installing build-aux/install-sh'
configure.ac:9: installing build-aux/missing' Makefile.am: installing build-aux/depcomp'
checking build system type... i686-pc-mingw32
checking host system type... i686-pc-mingw32
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking how to print strings... printf
checking for style of include used by make... GNU
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.exe
checking for suffix of executables... .exe
checking whether we are cross compiling... configure: error: in /c/Users/Enrique/IdeaProjects/uzweb2/deps/libsecp256k1/c_src/secp256k1': configure: error: cannot run C compiled programs. If you meant to cross compile, use --host'.
See `config.log' for more details
make: *** [priv/libsecp256k1_nif.so] Error 1
Terminate batch job (Y/N)? y

image

Getting error while trying to call a setter function...

How can I debug this error? The message was sent by Ganache. I can't figure out in which part of send_transaction this code is generated...

Thanks!

Error: %{
"code" => -32700,
"message" => "Cannot wrap string value "F901EB0A8504A817C8008303373C94517B6528CE4DFECB031288B37F5CE910B815316380B90184B3F882400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000A00000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000372F75706C6F6164732F6C6976655F766965775F75706C6F61642D313638363632343033342D3538303938353539363336342D312E7064660000000000000000000000000000000000000000000000000000000000000000000000000000000009446F63756D656E743100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020364332323445464545394145353444383145373942364544344535324442383029A05E6872198F22029840BAFEC13166652BFE4B03F96E854917AF12F7A2EBD2BDEFA013FED17E1367C54749A78829E84D28C111CAD6E937781AE217CF4E09BB01D944" as a json-rpc type; strings must be prefixed with "0x".",

What is the big challenge of implement ETH ABI parser ?

I was discussing with the dev from the Ethereumex and I'm planning to join forces with more developers to create an Eth ABI parser to interprete Smart Contracts and easily interact with them. Any though @izelnakri ? Maybe we can discuss that and start an implementation in another repo.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.