0

I have been looking through various tutorials (many of them outdated or with broken links) to figure out how to store and retrieve a simple string or hex value in the ETH blockchain. For my purposes (and intended application I am developing) it would be best to be able to interface with the blockchain via Python.

I have tried using web3 and pyethereum with not much success. The furthest I have gotten is using the following code with the web3 interface.

What pieces am I missing and how can I properly store and retrieve a value on the ETH blockchain?

How do I create a contract and store / retrieve a value from it?

def eth_black_magic(hex_value):

    web3 = Web3(HTTPProvider('http://localhost:8545')) # what URL should I point at?

    user1 = 'xxx' # what goes here?
    user2  = 'xxx' # what goes here?

    transaction = {'from': user2, 'to':user1, 'data': hex_value}

    transaction_hash = web3.eth.sendTransaction(transaction)
  • 1
    Why don't you use a contract with a constant and a getter method ? – Achala Dissanayake Aug 29 '17 at 7:04
  • Are you trying to send data from one person to another? Maybe you want something like Whisper. It sounds like you might want to ask for help with your approach first, rather than the mechanics. What are you trying to accomplish? – carver Aug 29 '17 at 19:28
  • I am not trying to send data from one person to another. I want to store a small string or hex value with a client and retreieve it with the same client. – arcade16 Aug 29 '17 at 19:57
  • Does anyone have an example of how to do a basic contract with web3.py? – arcade16 Aug 29 '17 at 22:23
  • In my answer, It shows how to write a contract in solidity, you'll need a remix like tool to deploy them – Achala Dissanayake Aug 30 '17 at 2:30
3

A general approach could look like:

  1. Create a contract that has a variable, say uint public val
  2. Add a method on the contract that sets that variable, say setVal(uint)
  3. Generate the contract ABI
  4. Initialize Web3(...), with a connection to your client
  5. Create a contract object with the source code
  6. Deploy the contract with contract.deploy()
  7. Use the ABI and deployed address to create a Contract object in web3

remix could be a good option for steps 1-3. You can use web3.py for steps 4-7. Docs and tutorials should exist for all the individual steps.

Now, you are all set up with a deployed contract. You can get and set val in python with:

  • Set the variable with contract.transact().setVal(1337)
  • Get the variable with contract.call().val()
1

Deploy a contract with the value you want to store as constant.

contract C {
    string constant text = "abc";

    function getMystr() constant returns (string) {
        return mystr;
    }
}

You can retrieve the value by calling the getMystr() function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.