3

I am having trouble interacting with myetherapi.com using web3.py. I am able to create a few functions that were working, like get_eth_balance() below. But to send transactions like send_eth(), I just keep getting this ValueError:

ValueError: {'code': -32601, 'message': '{eth_sendTransaction} Method not 
found or unavailable', 'data': None}

Any help would be greatly appreciated, what am I missing here?

from web3 import Web3, HTTPProvider

class MyWallet():
    def __init__(self, addr=None, key=None):
        self.addr = addr
        self.key = key
        self.w3 = Web3(HTTPProvider('https://api.myetherapi.com/eth'))

    def get_eth_balance(self):
        return self.w3.eth.getBalance(self.addr)

    def send_eth(self, address, amt):
        transaction = {
            'from': self.addr,
            'to': address,
            'value': Web3.toWei(amt, 'ether')
        }
        return self.w3.eth.sendTransaction(transaction)
0

1 Answer 1

3

eth_sendTransaction is not on the list of Allowed Methods shown at http://www.myetherapi.com/

eth_sendTransaction assumes that the node has access to the private key of the account that you're sending from, but myetherapi does not. So you'll have to sign locally and use sendRawTransaction. The most convenient way to do that (in Python) is with the w3.eth.account section of Web3.py, as of version 4.

You can use w3.eth.account.signTransaction() to sign locally before sending.

>>> transaction = {
        'to': '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
        'value': 1000000000,
        'gas': 2000000,
        'gasPrice': 234567897654321,
        'nonce': 0,
        'chainId': 1
    }
>>> key = '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318'
>>> signed = w3.eth.account.signTransaction(transaction, key)

>>> signed.rawTransaction
HexBytes('0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428')
>>> signed.hash
HexBytes('0xd8f64a42b57be0d565f385378db2f6bf324ce14a594afc05de90436e9ce01f60')
>>> signed.r
4487286261793418179817841024889747115779324305375823110249149479905075174044
>>> signed.s
30785525769477805655994251009256770582792548537338581640010273753578382951464
>>> signed.v
37

# When you run sendRawTransaction, you get back the hash of the transaction:
>>> w3.eth.sendRawTransaction(signed.rawTransaction)  
'0xd8f64a42b57be0d565f385378db2f6bf324ce14a594afc05de90436e9ce01f60'

source

2
  • How/where could I obtain the key? Is key stands for private key? @carver
    – alper
    Apr 4, 2018 at 10:46
  • 1
    Yes, it's the private key for the account you want to send the transaction from
    – carver
    Apr 4, 2018 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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