top of page

Middleware Sprint Publications

Abstract

The following article is a collection of information from a research sprint into middleware. More specifically, the team focused on enterprise-grade approaches to middleware services connecting developers to the Ethereum blockchain, and investigated the challenges they face and how they overcome them. We first discuss what middleware is, how it works, and why it is important. Then, we cover a few Ethereum middleware solutions. Lastly, we explore the fundamentals of implementing your own middleware solution linked to the Ethereum network.

The Participants

Gavin Stein: Researcher/Editor

William Godfrey: Researcher

Jack Lodge: Researcher

Stephanie Shen: Researcher

Cameron: Researcher

What is Middleware?

Middleware is a type of software that provides a common service or method of communication between two or more applications within a distributed network. It simplifies the process of trying to connect applications which weren’t designed to communicate with one another in turn helping streamline app development and reducing the time a product is brought to market.

How it Works

Middleware solutions allow applications to be developed without the need of having to create custom integrations whenever you want to communicate between different or foreign application components. To do this, they allow applications to connect by utilizing messaging frameworks such as REST, JSON or XML to name a few.

In addition to this, middleware also provides services that let components which can be written in multiple programming languages to communicate with one another.

Types of Middleware

Middleware comes in many different implementations. To name a few:

1. APIs

  • Developers are provided tools to create and manage APIs for application use, allowing other devs to be able to connect to them.

2. Databases

  • Makes the process of accessing and interacting with a back end database more simplified.

3. Message Oriented

  • Application components whether that be data sources or devices can exchange messages by using different messaging protocols.

4. Remote Procedure Call

  • An application is able to trigger a procedure within another application on a different network as if they both running on the same computer.

 

Within Enterprise

One use case for middleware is within industrial enterprises. It allows an organization to connect all the business processes, back end data sources and application components within an integration hub. It can also be used for intelligent automation, helping a business to improve resource management and overall efficiency as it eliminates the manual processes.

An Enterprise Service Bus used to be the most commonly used middleware serving as an integration hub. In the present day however, cloud based models are used more and allows a business to connect all their services in a cloud environment. This is more efficient, eliminating the costs used for installation and maintenance of the middleware hardware within their own data center.

Why Is it Important?

 

To summarize what was said so far, middleware helps businesses manage the challenges of using different tools, frameworks, programming languages or wanting to deploy an application across multiple platforms in which an architecture can vary. The key thing to remember is that it helps make application development cost-effective, efficient and helps to support environments, ensuring services will run consistently across a distributed network.

Existing Middleware Services for Ethereum

The following are a few different types of middleware solutions that currently exist to help you connect with the Ethereum blockchain depending on your needs. This is by no means an exhaustive list however.

Nansen.ai

Nansen.ai is an analytics platform for Ethereum. They combine blockchain data with wallet labels. Their blockchain data comes from an open source platform Ethereum ETL. This platform allows you to download up-to-date blockchain data in CSV formats via command line or you can access the latest database in Google BigQuery with SQL queries.

Nansen.ai does more than just provide a nice interface for blockchain data analytics. It also provides labels for crypto wallets. An example of the label could be “Millionaire”, indicating that the wallet handles more than 1 million USD worth of cryptocurrencies. These labels are not originally a feature in the blockchain data. Over 99 percent of the labels are inferred with high precision from methodologies like machine learning. Nansen.ai currently tracks over 90 million labeled Ethereum wallets and their activities.

MIDDLEWARE SPRINT PUBLICATIONS
Abstract
The Participants
What is Middleware?
How it Works
Types of Middleware
Within Enterprise
Why Is it Important?
Existing Middleware Services for Ethereum
Nansen.ai
SettleMint

Developing your own blockchain solution from scratch is hard. SettleMint aims to bridge the gap between companies and blockchain by offering minimized configuration APIs. It can help you by offering a layer three solution, from providing smart contract templates and dAPIs to a clean and informative user interface (dAPP).

Chainlink

Oracles

Oracles are becoming an increasingly foundational 3rd party of the decentralized financial ecosystem (DeFi), providing off-chain data for blockchain smart contracts. Oracles address crucial problems:

1. Blockchains struggle to allow native communication to external network systems off-chain.

2. Oracle networks must be decentralized in order not to nullify the benefits of the blockchain.

 

Enter Chainlink

Chainlink is a decentralized oracle network established by Steve Ellis and Sergey Nazarov in June 2017. Chainlink functions off-chain as a network of oracle nodes connecting to on-chain protocols. On-chain, Chainlink functions a series of smart contracts. The Chainlink process begins with a requesting contract that triggers an event and concurrently three other contracts:

1. Reputation contract (verifies the reputability of nodes)

2. Order-matching contract (matches smart contract orders)

3. Aggregating contract (collects data from oracle node)

Together these three pieces make up the Service Level Agreement (SLA) initiated when a requesting contract is made on Chainlink. Contract holders that would like to request a smart contract for a task pay LINK tokens. The rates established by the Chainlink node operator are dependent on the market relevance of the information given. Furthermore, node operators will stake on the network and deposit LINK as proof of their commitment. Node operators are penalized for malicious behavior through a tax on the stake of the node.

Chainlink Adoption and Use Case

Chainlink’s products and uses are currently being utilized by a diverse range of protocols to facilitate certain tasks. These tasks include, empowering decentralized lending and borrowing applications, derivative markets, price feeds to settle trades, insurance, pegging assets, connecting real world assets, payments, asset management and even NFT’s and Verifiable Random Function (VRF). More recently Chainlink has been deployed within Binance Smart Chain (BSC) to provide VRF. Another recent project is Chainlink being used by yearn.finance for the v2 LINK yvault (yvLINK). Chainlink provides the yearn protocol with a decentralized oracle network to deploy three strategies:

1. Supplying LINK to Vesper Finance LINK Pool to earn rewards

2. Lending LINK to AAVE and gaining interest while staking the accumulated AAVE

3. Locking LINK at MakerDAO then using minted DAI as a deposit for earning yield.

Chainlink 2.0

Chainlink Labs issued the Chainlink 2.0 white paper in April 2021, outlining increased network capabilities and possibilities. Scalable off-chain computing, explicit staking, and privacy-preserving features all contributed to Chainlink Lab’s long-term goal of creating a Decentralized Meta layer. The key functions were as follows: hybrid smart contracts, abstraction of complexity, scalability, secrecy, order-fairness, minimization, and incentive-based crypto-economic security, as stated in the white paper.

Implementing Your Own Middleware Solution

Implementations exist across multiple languages such as Python, Javascript, Java, Swift (for iOS), and more. This article will focus on a Python implementation. You should become familiar with the following tools/concepts which are used in a variety of implementations.

Web3.py

Web3.py is a python module that simplifies interactions with the Ethereum blockchain for developers. With Web3.py you can create and send transactions, query smart contracts, and gather all sorts of on-chain data. If you are using google colab for any dev work, you will need to install jsonschema version 3.2.0.

Infura.io

Infura.io is a service that runs a node on the Ethereum blockchain and interacts with the network. This allows users of this service to connect to an Ethereum node over http. While this service has been criticized for introducing centralization to a decentralized environment, for our purposes it is ideal.

GraphQL

Graph Query Language is an open-source language for data querying and manipulation within APIs.

Application Binary Interface (ABI)

ABI is a format defined for interacting with contracts and describes how data should be encoded, decoded for executable bytecode on the Ethereum Virtual Machine. It is represented in JSON format. The ABI will tell the EVM the function to call, what parameters to pass in, and outputs to receive, amongst other things.

ERC20 Standard

Refer to the official documentation for more information, however this standard sets a minimum level of functionality for complying tokens.

First and foremost, any implementation needs to be able to connect to the blockchain, in this case Ethereum. While you could run your own node and connect that way, for R&D and small scale operations, this is too laborious and difficult. Instead, you could use a service such as Infura.io.

Now that we have the ability to connect with the Ethereum blockchain, we need a way to interact with it. That is where Web3.jy comes in. This python library gives you much more simple functionality with the Ethereum blockchain.

With both Infura.io and Web3.py, we have all the functionality to implement a middleware solution. However, there is one more piece of information that anyone implementing a middleware solution should be comfortable with; Application Binary Interface (ABI).

Using all of this information, below are some code examples of using Web3.py, Infura and the graph.

Implementation Example(s)

# Install web3 module

Settlemint
Chainlink
Oracles
Enter Chainlink
Chainlink Adoption and Use case
Chainlink 2.0
Implementing your own Middleware Solution
Web3.py
Infura.io
GraphQL
Application Binary Interface (ABI)
ERC20 Standard

pip install web3
# Force jsonschema version to 3.2.0
!pip install — force-reinstall jsonschema==3.2.0
# Import Web3
from web3 import Web3
# Insert Infura API to Web3 HTTP provider
w3 = Web3(Web3.HTTPProvider(‘https://mainnet.infura.io/v3/8b72421f59bd4bb684bfee189f398d4b'))
# Get information about the latest Ethereum block
w3.eth.getBlock(‘latest’)
# Get the ETH balance of an address https://etherscan.io/address/0x2d7a6adeb37dcb6e0a796dc6b15d3e8889092cd9 (random address for example)
# Checksum on address for error avoidance
address = Web3.toChecksumAddress(‘0x2d7a6ADeB37dcb6E0A796Dc6b15d3e8889092cD9’)
# Get balance in wei (1/10¹⁸ ETH)
wei = w3.eth.getBalance(address)
# Convert to ETH
ether = wei / 1000000000000000000
ether
# Import json module
import json
# Establish ABI for handling smart contract inputs and outputs
ABI = json.loads(‘[{“constant”:true,”inputs”:[{“name”:”_owner”,”type”:”address”}],”name”:”balanceOf”,”outputs”:[{“name”:””,”type”:”uint256"}],”payable”:false,”stateMutability”:”view”,”type”:”function”}]’)
# Checksum wallet address of your choice
wallet_address = ‘ADDRESS_HERE’
wallet_address = Web3.toChecksumAddress(wallet_address)
token_contract_address = ‘0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f’
token_contract_address = Web3.toChecksumAddress(token_contract_address)
# define contract
contract = w3.eth.contract(token_contract_address, abi=ABI)
# call contract and get data from balanceOf for argument wallet_address
raw_balance = contract.functions.balanceOf(wallet_address).call()
# convert the value from Wei to Ether
synthetix_value = Web3.fromWei(raw_balance, ‘ether’)
synthetix_value
# Install and import gql, Client and RequestsHTTPTransport
pip install gql
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
# Using the graph to grab price data, SNX in DAI
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
sample_transport=RequestsHTTPTransport(
url=’https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2',
verify=True,
retries=5,
)
client = Client(
transport=sample_transport
)
# Get the value of SNX/ETH
query = gql(‘’’
query {
pair(id: “0x43ae24960e5534731fc831386c07755a2dc33d47”){
reserve0
reserve1
}
}
‘’’)
response = client.execute(query)
snx_eth_pair = response[‘pair’]
eth_value = float(snx_eth_pair[‘reserve1’]) / float(snx_eth_pair[‘reserve0’])
# Get the value of ETH/DAI
query = gql(‘’’
query {
pair(id: “0xa478c2975ab1ea89e8196811f51a7b7ade33eb11”){
reserve0
reserve1
}
}
‘’’)
response = client.execute(query)
eth_dai_pair = response[‘pair’]
dai_value = float(eth_dai_pair[‘reserve0’]) / float(eth_dai_pair[‘reserve1’])
snx_dai_value = eth_value * dai_value
snx_dai_value

References
bottom of page