Graphing the Melon Protocol

Ivan Herger
Enzyme
Published in
6 min readSep 9, 2019

New Melon Monitoring Tool release, built using The Graph!

The Melon Protocol is complex and creates a lot of data

The Melon Protocol is a fully decentralised protocol for setting up investment funds and managing tokenised assets. The first live version of the Melon Protocol has been deployed to Ethereum mainnet about half a year ago, on March 1st, 2019. Since then, more than a hundred fully decentralised investment funds have been created, investors have invested almost USD 50’000 into those funds, and fund managers have been trading on decentralised exchanges. Some funds (and investors) have been making profits, others have made losses.

The Melon Protocol operates fully on-chain, i.e. all processes are defined in Ethereum smart contracts and all important events are logged on-chain as Ethereum events. In principle, everyone can recreate the complete history of the whole melon network from on-chain data only.

When we set out to analyse and display the history of the melon network, we are confronted with three major difficulties:

  1. The Melon network currently consists of hundreds of smart contracts: a handful of so-called “infrastructure” contracts which are used by all investment funds, and a handful of individual contracts for each fund (so that will be a few hundred contracts for the more than hundred funds that have been created up to now).
  2. Each contract emits events whenever functions are invoked (e.g. when investors invest into the fund, or when a fund trades on a decentralised exchange), so there are currently many thousand events related to the whole network.
  3. The network of smart contracts is not static, new contracts are created whenever a new investment fund is set up.

Building a scalable system to monitor complexity of the protocol and wealth of data it creates is not straight forward, so we would like to use this blog post to show you, how we have set up the monitoring of the web of all smart contracts and events in the Melon Network.

Extracting and processing data with The Graph

Extracting smart contract information and event data from Ethereum can easily be done with the Ethereum Javascript API (web3.js, https://github.com/ethereum/web3.js/).

The first version of the Melon Monitoring Tool has directly queried the Ethereum blockchain using web3.js. In order to improve the user experience, we have used caching (i.e. we made automated queries to the blockchain every 10 minutes and cached the results, so that the user of the front end would see the requested data very quickly, with the small caveat that the data could be up to 10 minutes old). This worked well for the limited purpose of extracting the current data off the blockchain. However, if we wanted to look at historical data, we needed other tools.

Fortunately, the people at The Graph (https://thegraph.com) had realised a while ago that there is a need for fast data extraction from the blockchain, and they have set out to make our monitoring lives considerably easier by building “a decentralised protocol for indexing and querying data from blockchains. It makes it possible to query data that is difficult to query directly.”

The team behind The Graph has chosen an incremental path to decentralisation to provide the ecosystem with a solution that can be used immediately. They launched the hosted service at Graph Day in January and are currently working on the hybrid network, which will introduce a decentralised marketplace for indexing and querying. The Graph is open source and can be built and run by anyone.

Attracted by the Graph hackathon in April 2019, we set out to build the future of the Melon Monitoring Tool with the Graph protocol.

Here is how The Graph works for us:

  • We define data sources, i.e. smart contract that emit events. Data sources can be static (i.e. a contract which has already been deployed at a certain known address, like the Engine Contract) or dynamic (i.e. a template or a contract which will be deployed in the future at an address which is not known yet, e.g. the Vault Contract of a fund that has not been created yet)
  • For each data source, we define event handlers or mappings. Event handlers are called whenever a contract emits an event (e.g. whenever the Version Contract emits the event “NewFund”, the corresponding event handler is called). In an event handler, we can do different things: in the most simple scenario, we just store the event data. In a more complicated scenario, we can do additional processing whenever the event occurs (e.g. we can update the share price of all funds whenever a priceUpdate event emits the current price of all underlying assets).
Excerpt of an event handler
  • We define a GraphQL schema of the entities that we want to store in a decentralised database, including the relations between the different entities, which is very useful for the Melon Monitoring Tool.

These three constituents (data sources, event handlers, GraphQL schema) define a subgraph, and our subgraph is the “Melon Subgraph.” The subgraph definition is stored in IPFS. The Graph Node (the heart of The Graph) continuously scans the Ethereum blockchain for events defined in the subgraph. Whenever an event is detected, Graph Node runs the corresponding event handler and stores data (as defined in the event handler). Querying the subgraph is done through a GraphQL server.

In summary, The Graph allows us to easily extract all kinds of data from the Melon network and store it in a database which we can query using GraphQL.

BTW: the GraphQL endpoint of our Melon subgraph is publicly available at https://thegraph.com/explorer/subgraph/melonproject/melon

The new Melon Monitoring Tool

The new Melon Monitoring Tool (https://monitoring.melon.network) uses the data collected and provided by the Melon Subgraph, and it provides an abundance of information on different levels of the Melon network:

  • Network data, e.g. number of funds, total assets under management
  • Engine data, e.g. current amgu price, MLN burned
  • Fund data, e.g. share price, risk policies, trades
  • Manager data, e.g. funds per manager
  • Investor data, e.g. overview of all investments, return per fund
  • Trading data, e.g. trades per decentralised exchange, trades per fund
  • Asset data e.g. asset prices, overall amount invested into asset
List of Melon funds

A lot of the data stored in the Melon Subgraph is used directly to display information, e.g. in time series charts and tables. In addition, the Monitoring Tool also calculates certain additional values, such fund returns.

The wealth of data provided by the Melon Monitoring Tool is useful for all the stakeholders in the Melon universe.

  • The Melon Council can have a quick overview of the overall state and health of the protocol by looking at the network and engine data, as well as the trading and asset data.
  • Investors can analyse and compare individual funds as an important part of their investment decision.
  • For fund managers, the Melon Monitoring Tool provides a complete overview of the current state and the history of their funds. In addition, fund managers can gain insights about the asset universe and about exchanges.
  • Developers can get a quick understanding about the protocol and can use the Monitoring Tool for debugging, security monitoring and coming up with ideas for protocol improvements.
Examples of time series for each Melon fund.

Where do we go from here?

While we are really happy about the new Melon Monitoring Tool in its current form, we also have some ideas for further improvements:

  • More financial analysis tools to help investors make informed investment decisions
  • “Nerd Mode” for developers, providing additional low-level data
  • Link the Melon Manager Interface with the Melon Monitoring Tool and/or the Melon Subgraph

We would also like to hear from you — fund manager, investor, blockchain guru — on how we can further improve the Melon Protocol and the Melon Monitoring Tool. Let us know through

Thanks

We would like to thank the team at The Graph for their great and super fast support for all our questions regarding our increasingly complex Melon Subgraph. You guys rock!

--

--

Ivan Herger
Enzyme
Writer for

Fullstack engineer for Enzyme Finance. Former theoretical physicist and risk manager in asset management. Also a yoga teacher.