mana-ethereum/ethereumex

Calling smart contracts and executing functions

Closed this issue · 13 comments

I'm curious how you guys are calling smart contracts and executing functions, I'm bit struggling with compiling and functions from smart contracts. I'm working on an oracle service which should use eth_call function and I need to set some params on data params. Any tutorial or advice would be awesome.

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

@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

@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 ?

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.

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

To generate the function signature.

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

@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

@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 ?

@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

@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.

@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

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

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.