0109/2018 Ethereum Smart Contacts in Python: a comprehensive(ish) guide
Nick Williams
Astrophysicist, cofounder of @sempo. We're reinventing disaster response to make it efficient,
systematic and transparent.
Apr 13 15 min read
Ethereum Smart Contracts in Python: a
comprehensive(ish) guide
It’s one thing to get a basic smart contract up on Ethereum—just
google “ERC20 Token Tutorial” you'll find plenty of information on how
to do it. Interacting with a contract programmatically is another thing
entirely, and if you're a Python coder, then tutorials are scarce.
One by ry count, andi so000 2077
Fortunately for us, Version 4 of Web3. py has just been released, which.
‘means it's now easier than ever to run a python script and watch as
magical things happen on the blockchain. Spooky.
hitps:ackemoon.comistnereum-smart-contracts-i-pytnon-s-comprehensive-ish-quide-771b03090988 ‘20soroar2018
Ethereum Smart Contracis in Python: a comprehensivelish) guide
A big shout out Piper Merriam, Jason Carver and all the others who've
worked so on hard on Web3.py to make life easy for the rest of us—at
‘Sempo we're using Ethereum to make Disaster Response more
transparent, and it’s only really possible thanks to Web3.py.
Getting Set Up
First we get set up, making sure we have the relevant python libraries
installed and so-forth.
Python libraries everywhere, but what are they for?
‘There are plenty of python libraries related to Ethereum out there, but
there are two that come up a lot when people talk about Ethereum:
‘Web3.py and Pyethereum. At first glance it’s not obvious which one you
should use for what.
Pyethereum
A Python implementation of the Ethereum Virtual Machine (EVM). ‘The
EVM, in turn is the part of the Ethereum protocol that actually runs the
code in Smart Contracts and determines their outputs. So if you wanted
to run an Ethereum node in Python, Pyethereum is a good place to
start,
Even if you're perfectly happy running your Smart Contracts without
running your own node, Pyethereum is still a good library to have
around— it contains a bunch of functions that do useful things like
calculate a user’s address from their private key and so on.
Web3.py
A library for actually interacting with the Ethereum Blockchain, We're
talking about things like transferring Ether between accounts,
publishing Smart Contracts and triggering functions attached existing
Smart Contracts. It’s inspired by the popular javascript library Web3 js,
and it'll be the main library we use in this tutorial.
Ok, less talk, more do!
At first I tried working with a version Python 3.5, but at runtime Tran
into issues, apparently caused by Python's type hinting. Creating a
hitps:ackemoon.comethereum-smar-conracts-n-pytnon-a-comprehensive-ish-quide-77'1b03090988 220soroar2018
Ethereum Smart Contracis in Python: a comprehensivelish) guide
Virtual Environment based off Python 3.6 solved this, so T'd.
recommend doing the same.
Go ahead and pip-install web3 (make sure you get version 4)
Unless you enjoy spending real money for the sake of it, you'll need a
wallet on an Ethereum TestNet such as Ropsten with plenty of Ether to
play with. An easy way to do this is download the Metamask extension
for Chrome, and create a new account from there.
Bw Amloszo A
re Ropsten Test Net’on the lft
Even if you have an existing wallet with real Ether in it, Pd highly
recommend creating a new one for development purposes. We're going
tobe doing some relatively reckless things with private keys, so it can't
be a problem if they accidentally become public (public-private keys?)
Getting test Ether for your newly created wallet is easy: simply go to
fauicet.metamask.io and click on ‘tequest 1 ether from faucet’. This
should be plenty for what we'll be doing.
Finally, because we're going to be working with the Ropsten TestNet
without hosting our own node, we need a provider that we can connect
the Blockchain through. Infura.io works well for this, so go and create a
free account over there. Note down your provider url for the Ropsten
TestNet (looks something like
https://ropsten.infura.io/FE2Gfedem3tfed3).
Deploying a Smart Contract
Using Python to deploy a Smart Contract without running your own
node is pretty difficult, so we're going to cheat on this step. For many
Smart Contract use-cases, you only need to do it once anyway.
hitps:ackemoon.comethereum-smar-conracts-n-pytnon-a-comprehensive-ish-quide-77'1b03090988 3120