Blockchain development using Python and Ganache

Smriti Bajaj
5 min readJul 1, 2021

Disclaimer- This part focuses more on the theoretical aspects and etherscan.io.

I know I’m a sweet-toothed, fan of ganache but this “Ganache” tastes a little code-like. It’s easy to get and use but we’ll see it in the next part because first, Blockchain. Blockchain is among the hippest technology gossips out there in terms of numbers, buzzing cryptocurrency, accounts, blocks, gas, gas price and other jargon. So, let’s witness the inevitable.

Blockchain is a chain of blocks containing transactions. It is a decentralized database that keeps a permanent record of all transactions. Once a record is written to a blockchain, it can’t be altered. The core concepts of blockchain include:

  1. Block: A block itself has three primary components. The block is made up of the information for the current transaction, a cryptographic hash of the previous block, and a timestamp. Each block uses an RPC protocol to communicate.
  2. Network: Using a peer-to-peer network helps make blockchain fault-tolerant, transparent and distributed.
  3. Consensus mechanism: It lets all the members know that an update is valid and that the update was made by a member of the blockchain. It also sets the standard for how members prove to the rest of the chain that an update is valid. A consensus mechanism makes blockchain, distributed and immutable.
  4. Smart contract: A smart contract is a contract that executes without any additional action required by a third party. With a smart contract, you establish the terms and conditions of the contract. Once the terms and conditions of the contract are met on the blockchain, the contract automatically executes — without you needing to take any action.

Wait, can a contract constitute multiple transactions? Absolutely. A “smart contract” is simply a program — a collection of code (its functions) and data (its state) that resides at a specific address on the blockchain. And what currency do we trade in? It’s cryptocurrency— your digital asset. Well, the reason blockchain is so important is that it prevents cryptocurrency to get duplicated.

Pro tip: Blockchain and bitcoin aren’t synonymous.

Photo by Thought Catalog on Unsplash

A blockchain framework is the set of standards on which a blockchain operates. Some frameworks are built specifically for cryptocurrency creation and tracking. Other frameworks are focused on collaboration and sharing of knowledge. One of the commonly used frameworks is Ethereum.

Ethereum is a permission-less, or public, cryptocurrency blockchain that provides a broad set of capabilities for using smart contracts, running applications, and executing code. Ethereum also has its programming language (Solidity) that allows you to create applications within the Ethereum Virtual Machine (EVM). Within the EVM, you can write, build, and run applications. You can even establish fees or charges, in Ether (Ethereum’s cryptocurrency) for running an application. (Miners do charge that 😉)

For our code here, we’ll connect to the Ethereum API to access an Ethereum network via web3, an Ethereum library in Python. To interact with the Ethereum blockchain, we’ll use infura. We’ll have to sign up and check our dashboard for an API key to access the endpoint “https://mainnet.infura.io/v3/<your-key>”.

This will return with a True value if the connection is established.

Now, to get the basic idea of a block of the blockchain, use the following code.

print(web3.eth.get_block(‘latest’))

This will give you lots of information and we’ll talk about it in the next part. Let’s now go to etherscan. Etherscan is the leading blockchain platform for Ethereum that provides APIs, analytics and more.

After navigating to the website, choose any token and you can spot a contract address, copy it. There, you can see all the transactions, holders, and many other fruitful pieces of information like specific to contract- total supply, name, symbol, etc. One can use this information to publish on their website or for analysis through the code ahead.

Now with that copied contract address and an Application Binary Interface(ABI) which is the description of the contract, we have our contract information with us.

However, to get the ABI there are two ways, one using the API and the other through my favorite etherscan.io UI. In the above snippet, the commented ABI was extracted in the latter way wherein you’ll have to click on the contract address, click on the Contract tab and in the code below, you’ll see the Contract ABI.

For the ABI-API way, one can refer to the following code.

It simply uses the requests package and gets the required ABI based on the address passed. Finally, ABI is loaded as a JSON to the contract method along with the address.

I know your great eye has noticed a call() in the previous code, yes, that’s how we interact with the functions of the contract when reading from them. In the next part, we’ll see how to create our own contract in solidity and even use parameters to play with like, transact(), an alternative of call().

Moreover, there is an alien term fromWei that returns the value in wei converted to the given currency. Wei refers to the smallest denomination of ether (ETH). The value is returned as a decimal to ensure precision down to the wei.

For details check out-https://web3py.readthedocs.io/en/stable/web3.eth.html

Woohoo! You now know how to access the blocks of a blockchain, how to analyze complex numbers via Etherscan. I just feel it in my bones that you want to create a contract, a transaction, and then call more methods. Well, then stay tuned for the next part!

Note- Since it’s been a long gap and I don’t have anything saved in my drafts, part 2 is not coming. Sorry guys, I couldn’t keep me in touch with Blockchain.

If you liked the content, please clap. It’s a really good exercise :)

Thanks for reading! ❤ #CodeEveryday

--

--