I have a Mist wallet and a contract (with its address) that i execute from mist GUI "manually".
I was wondering how i can execute it from command line (bash not geth) or call that execution from a python script.
If this is possible could you provide a toy example or a roadmap?
Thanks in advance!
[Ethereum] Execute a contract from bash or inside a python script
go-ethereummistpython
Best Answer
All of this has already been explored in the comments but I'll summarise as an answer!
Executing/calling from bash
This is explained pretty thoroughly in the Homestead Guide to Accessing Contracts and Transactions.
From the very basics, Ethereum has an RPC (Remote procedure call) interface, as explained excellently by the Wikipedia RPC page. Ethereum's RPC interface uses a subset of the JSON-RPC 2.0 spec, with the following caveats:
Commands with bash
Using the RPC interface, we can execute commands such as
eth_coinbase
, which returns your ETH address:Which prints:
Interacting with deployed contracts with bash
Functions from pre-deployed contracts are interacted with through their function signature, which is found by taking the first 4 bytes of the output of
sha3('functionName(functionParameterTypes)')
. This can be computed from inside geth with, for example,A helpful note here is that even if you've used
uint
orint
within your contract, you'll have to use the real deal like eguint256
.You pad your input (encoded in hex) with zeros, like the following, for an example input of 6:
You do this so the length is a multiple of 32 bytes, but usually I just copy the example and change the end so as not to have to hit the 0 button 8 billion times.
Now quoting straight from the docs, we have:
Combining the function selector and the encoded argument our data will be:
Lets try it:
Since we sent a transaction we got the transaction hash returned. You can have way more exciting examples that return results of function calls.
Calling with Python scripts
I haven't tested a single one of these, but from the comments, links to explore include: