Interact with the Ethereum Blockchain using Rivet and Python

Introduction

By the time you finish this tutorial, you’ll be able to query the Ethereum blockchain using Web3.py via your Rivet endpoint.

The Big Picture

  1. You’ll start by setting up a Rivet account if you haven’t already done so. If you have already done so and you are signed into Rivet, your key will appear here:

  2. Next, you’ll install all the necessary dependencies and configure them to use your Rivet endpoint.

  3. Finally, you’ll experiment with some Ethereum calls and see the results in your Python interpreter.

1. Setting up Rivet

My Rivet endpoint: https://YourApiKey.eth.rpc.rivet.cloud/

If you see a URL above that does not contain “YourApiKey”, then you already have a Rivet account set up and you’re logged in. Nice work! Go ahead and skip to step 2.

If you don’t see a URL with your unique key in it, no worries! This will just take a moment:

First, navigate to the Rivet signup page.

When you get there, enter a nickname for your account and a valid email address to which you have access. Select submit.

Web3-savvy?

You can also create an account and top it up with credits via Web3 using the Login with Web3 button on the signup page.

Not sure what this is? Not to worry, just disregard this box for now. Pretty soon, this will make perfect sense.

On the next view, you’ll verify your email address using a code we sent to it when you selected submit. Check your email for a message from Rivet, copy the code, and paste it in.

Got it? Good.

Finally, acknowledge our terms of use—and if you have a promo code, don’t forget to add that too. (If you do forget, contact us at support@rivet.cloud and we’ll hook you up retroactively if you can tell us what the code was supposed to be.

Good to go? Great! Go ahead and hit submit, and voila! Your endpoint URL is:

https://YourApiKey.eth.rpc.rivet.cloud/

Still need help?

If you don’t see it, make sure you’re signed in. If you still don’t see it, please contact us at support@rivet.cloud and we’ll get to the bottom of it. If you recall it, please include the nickname you gave us when you signed up in your message.

Congratulations, you’re 1/3 of the way through this tutorial!

2. The Setup

Web3.py can be installed using pip3 as follows:

$ pip3 install web3

Next, you’ll create a Web3 instance and connect it to your Rivet endpoint. First, launch Python with the following terminal command:

$ pip3 install web3
>>> from web3 import Web3
>>> w3 = Web3(Web3.HTTPProvider('https://YourApiKey.eth.rpc.rivet.cloud/'))

That’s all there is to it! Easy, right?

Ok, now let’s try some queries!

3. The Grand Finale

It’s time to start using Web3.py! Once properly configured, the w3 instance will allow you to interact with the Ethereum blockchain via Rivet. Try getting all the information about the latest block:

>>> w3.eth.getBlock('latest')

Your result should look something like this:

{'difficulty': 1,
 'gasLimit': 6283185,
 'gasUsed': 0,
 'hash': HexBytes('0x53b983fe73e16f6ed8178f6c0e0b91f23dc9dad4cb30d0831f178291ffeb8750'),
 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'),
 'miner': '0x0000000000000000000000000000000000000000',
 'mixHash': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000'),
 'nonce': HexBytes('0x0000000000000000'),
 'number': 0,
 'parentHash': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000'),
 'proofOfAuthorityData': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000dddc391ab2bf6701c74d0c8698c2e13355b2e4150000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'),
 'receiptsRoot': HexBytes('0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421'),
 'sha3Uncles': HexBytes('0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'),
 'size': 622,
 'stateRoot': HexBytes('0x1f5e460eb84dc0606ab74189dbcfe617300549f8f4778c3c9081c119b5b5d1c1'),
 'timestamp': 0,
 'totalDifficulty': 1,
 'transactions': [],
 'transactionsRoot': HexBytes('0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421'),
 'uncles': []}

Now let’s try getting the latest gas price:

>>> w3.eth.gasPrice

The result (in Wei) should look something like:

33000000000

Got it? Great!

Finally, let’s try getting the balance of a particular account. If not specified with the block_identifier argument, the block defaults to ‘latest’. For details about how to form the argument properly, see X.

>>> w3.eth.getBalance('0xd3CdA913deB6f67967B99D67aCDFa1712C293601')

The result:

190135876000049381

Great work! You’re well on your way to becoming a Web3.py ninja!

Conclusion

As you’ve seen in this tutorial, Web3.py can help you read block data, get the latest gas price, and get account balances—but it can do so much more. With it, you can do everything from signing and sending transactions to deploying and interacting with contracts. Basically, anything you want to do with Ethereum, you can do with Web3.py.

In future tutorials, we’ll expand from here to help you grow your creative repertoire. In the meantime, you should check out the resources available in the rest of this doc site, at the Web3.py doc site, and at Ethereum.org and start dreaming up what you want to learn how to BUIDL.

Congratulations!! You’re now an Ethereum BUIDLer—welcome to the community!!

—❤️Rivet