Giter Site home page Giter Site logo

Comments (13)

KristerV avatar KristerV commented on September 18, 2024 1

sorry for necrobump, but i found this place trying to call balanceOf for a contract. it's been quite a headache. finally found the solution in a tutorial, thought i'd cross-post here for future people.

# prepare account address
{:ok, address } = 
  "0xaeaf89a26ce2986e7a948dc3adf0166301ec5c4b"
  |> String.slice(2..-1)
  |> Base.decode16(case: :mixed)

# prepare function to be called
data = ABI.encode("balanceOf(address)", [address])
       |> Base.encode16(case: :lower)

# perform call
contract_hash = "0x88dF592F8eb5D7Bd38bFeF7dEb0fBc02cf3778a0"
{:ok, result} = Ethereumex.HttpClient.eth_call( %{ data: "0x" <> data, to: contract_hash}, "latest")

# decode result
result
|> String.slice(2..-1)
|> Base.decode16!(case: :lower)
|> :binary.decode_unsigned

# results in balance integer.

from ethereumex.

ayrat555 avatar ayrat555 commented on September 18, 2024

@eduardonunesp I will be working on it in the next few weeks. You can help if you want.

from ethereumex.

eduardonunesp avatar eduardonunesp commented on September 18, 2024

@ayrat555 for sure man, I'm very curious to interact within smart contracts using that lib, if you have any PR or any other repo will be awesome

from ethereumex.

eduardonunesp avatar eduardonunesp commented on September 18, 2024

@ayrat555 congratulations for your job in the PR #7 I think the next step is to create functions or helpers to call the functions on smart contract to get and alter states on smart contracts. Do you think is possible to wrap up this within that PR ?

from ethereumex.

eduardonunesp avatar eduardonunesp commented on September 18, 2024

To call smart contracts functions from json api is necessary to call send_transaction and pass some parameters like the address of the smart contract, also is necessary to compile the solidity ABI in the data parameter, so it's important to follow the doc (https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI). I was working on that and using the module {:keccak, "~> 2.0"} to compile the function name to sha3.

from ethereumex.

eduardonunesp avatar eduardonunesp commented on September 18, 2024

Found a module called hexate which looks interesting to generate the params for the function calls Hexate.encode(69, 64) which generates `"0x" <> "0000000000000000000000000000000000000000000000000000000000000045"

from ethereumex.

eduardonunesp avatar eduardonunesp commented on September 18, 2024

To generate the function signature.

{:ok, %{"result" => result}} = Ethereumex.HttpClient.web3_sha3(["baz(uint32,bool)"])
String.slice(result, 0, 12) # "0xcdcd77c0"

from ethereumex.

ayrat555 avatar ayrat555 commented on September 18, 2024

@eduardonunesp thanks, that's the idea

I will take a look at abi.
But we had problems with keccak library implementing merkle patricia tree. It's bettter to use keccakf1600_orig

from ethereumex.

eduardonunesp avatar eduardonunesp commented on September 18, 2024

@ayrat555 based on the code from web3.js which parses the Smart Contract ABI, do you think is in the scope of the project to have an ABI parser and functions to Smart Contract Functions and listen to Smart Contract Events ?

from ethereumex.

ayrat555 avatar ayrat555 commented on September 18, 2024

@eduardonunesp why not? We can do that. but there is library by @izelnakri called eth, maybe it has this functionality - https://github.com/izelnakri/eth

from ethereumex.

izelnakri avatar izelnakri commented on September 18, 2024

@eduardonunesp currently I haven't built this functionality but would be more than happy to review your pull request if you are willing to contribute! Also I would appreciate if this isn't a seperate package, I'm already using several dependencies on https://github.com/izelnakri/eth, so I would like to keep it low as well.

from ethereumex.

ayrat555 avatar ayrat555 commented on September 18, 2024

@eduardonunesp I just found out that smart contract compilation using Json Rpc api is deprecated in both geth and parity.
I used old version of parity in #7

from ethereumex.

eduardonunesp avatar eduardonunesp commented on September 18, 2024

@ayrat555 I started to read and understand the web3.js is the ultimate source as base for Elixir port

from ethereumex.

Related Issues (20)

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.