[Disclaimer: The code here is under the MIT license, delivered for educational and illustrative purposes for the sole purpose of demonstrating how to obtain price feeds. It is not investment advice. As always, trade at your own risk.]
The Ethereum network represents a robust ecosystem of smart contracts that power numerous tokens, decentralized applications, and protocols. These apps and protocols take on a variety of forms, including decentralized exchanges (DEX).
In a DEX setting, buyers and sellers interact directly with one another and the market itself. This means no signups and no deposits. It also means that, through direct interactions with the blockchain, users can generate their own price feeds and automated trading strategies.
Metronome, a fully autonomous DeFi asset, launched in 2018. Below, we’ll show step-by-step how to hook up Bloq Connect with Web3 and Metronome’s API to create a price feed that grabs prices across Metronome’s daily auctions and its built-in MET/ETH DEX.
Initial Setup
In order to retrieve MET/ETH price data, we need three tools: Bloq Connect, Web3.js, and the Metronome contracts.
First-time Bloq users can see the Getting Started guide. Beyond that, a “project id” is required for ETH/ETC Connect. With an account created, generate a project id here.
You’ll also need to install the web3.js library. See the Web3 on Bloq Connect article for installation info.
Lastly, install the Metronome Contracts in the same directory as web3 with npm install --save metronome-contracts
.
Now we can start coding. Create a Javascript file in the same directory as the above installations. We’re going to create a web3 object that connects to, well, Bloq Connect. Then a Metronome object that utilizes the web3 object.
Make sure to replace the ‘projectId’ value with your own project id.
Retrieve MET Auction Price
The Metronome Contract API installed above gives us a number of methods we can call to grab information related to the Metronome smart contracts. There are four:
- The Ledger contract
- The Auctions contract
- The Proceeds contract
- The MET DEX
For the purposes of this tutorial, we’ll be interacting with the Auctions and MET DEX contracts.
To start, let’s use the currentPrice()
method to see what the most recent MET/ETH Auction price is.
The above code is going to return a very large number. That’s because the price is in wei, the smallest unit of ETH. To translate to ETH, divide the price by 1*1018. Let’s also use toFixed()
to truncate some of the decimal places.
The currentPrice()
method will return the current price if there is an auction currently live. If not, it will return the final price from the previous auction. We can also determine the starting price for the next auction as double the last auction’s final price.
The currentMintable()
method returns how many tokens are currently available via auction. We can use this to check whether or not there is a live auction (one starts every midnight UTC), and then split the code into two functions that will report relevant information for whether we see the live auction price or the last auction price.
Retrieve MET DEX Price
With an auction price feed in place, let’s also grab the current buy/sell prices from the MET DEX.
The getMetForEthResult()
method returns how much MET can be bought with an input amount of ETH through the MET DEX. Similarly, getEthForMetResult()
gives rates for the inverse: how much ETH is awarded when an input amount of MET is sold to the MET DEX.
Like the above commands, the ETH input is reflected in wei. One ETH represented as wei is too large for Javascript to handle natively and will return an error.
We can alleviate this by using the BigNumber API. Install to the same directory with npm install bignumber.js
. We’ll create a ‘bignumber.js’ value and also create a BigNumber that represents one ETH.
Now, plug in the ‘oneEth’ value to getMetForEthResult()
to return how much MET a one ETH purchase from the MET DEX would return. Like before, the method returns an amount of MET in wei, which may return an error. With the ‘oneEth’ value, we can divide the output to get the proper representation. Use toFixed()
as well to truncate some of the superfluous decimals.
This returns the number of MET we can expect to receive from the MET DEX for one ether. With some additional arithmetic, we can also grab the ETH price per single MET.
Now we have the MET/ETH price and sum of MET that can be purchased for one ETH. Lastly, let’s replicate the internal code of the above method into a getEthForMetResult()
that will show us the price for MET sales instead. Because the MET DEX has minor slippage, there will be some nominal difference between buy and sell values.
For uniformity, place the sell method within a function that takes a ‘met’ value, then place a function call in the buy method we just created. Once again, these methods operate in terms of wei, so supply the ‘token’ value before it’s been divided by ‘oneEth’ to the sell function.
One final note: We’re supplying a MET value, not an ETH value, for the sell function, which means we also need to record one more value: the amount of ETH expected when the input sum of MET is sold.
Next Steps
The above code is a fully-functional price feed that returns current MET prices from the MET DEX and the Daily Auctions. Immediately, this represents a rudimentary “arbitrage alerts” system. Call the function and you can see if there is profit to be made buying from the auction then immediately selling to the MET DEX.
Of course, a more comprehensive program would feature a loop that re-calls the method each minute during a live auction, for a live price feed. Beyond that, a savvy programmer could pair the program with a trading bot that automatically executes an auctions purchase then MET DEX sale when a price disparity of a certain percentage is achieved.
Keep in mind that Ethereum accounts selling MET through these contracts for the first time will need to authorize the contract with a separate transaction before any outgoing MET transactions can broadcast successfully.
Additionally, the Metronome Contracts API is similar to what other decentralized exchanges employ. This project could additionally be used as part of a larger Arbitrage bot that trades between Metronome’s Uniswap or Balancer pool and the MET DEX, for example, or for market pairings of other ERC-20s.