Explaining Vexchange

How the pricing formula and other parts of the exchange work

Oliver Chalk
Monti
7 min readMar 21, 2019

--

This article will start with a high-level overview of the DEX and the pricing formula, before getting into more details and going over the maths and reasoning behind the formula. The different parts are as follows:

  1. Vexchange Overview
  2. Pricing Overview
  3. Vexchange Architecture
  4. Pricing Formula
  5. Risk vs. Reward for Stakers

1. Vexchange Overview

Vexchange is a decentralized exchange running on the VeChain blockchain. Instead of maintaining an expensive on-chain order book, Vexchange uses supply and demand to set the exchange rate for its assets. All markets trade into VET, so all prices are in-terms of VET.

Because trades are instantly settled, Vexchange can be integrated into any dApp to offer seamless swaps for users. This has a wide range of use cases, most notably in user onboarding — dApps can now accept payment in any VIP180 and have it converted to VET with one simple transaction.

2. Pricing Overview

Vexchange adopts a model often seen in prediction markets. Instead of maintaining an order book of buyers and sellers, we set the price for a specific asset based on how much of that asset we have. The exhcnage rate for your trade is set based on how much you are buying/selling. As such, there is no way for a malicious trader to empty one of the reserves, the price will trend towards infinity thanks to their efforts.

What if someone buys/sells and moves the price on the DEX above/below other exchanges?

I hear this question a lot: “if the price on Vexchange is too expensive, won’t people stop trading?” The problem here is that the price on Vexchange will ever only be “expensive” for one side. So if an asset’s price is too high, sellers will be incentivized to sell, and vice-versa. Beyond standard traders, market makers will also have an incentive to buy/sell on Vexchange and then buy/sell those same assets on a different exchange, claiming the margin as profit.

What happens if the exchange runs out of assets because of a sudden price change?

As mentioned earlier, the exchange will never run out of a particular asset. It will simply continue to make that asset more and more expensive to buy as more of it is sold. This prevents anyone from emptying the exchange in one buy and also helps set the price over time as exchange rates naturally move.

If the liquidity stakers are on the other side of every trade, won’t they lose money every time someone arbitrages the DEX?

A common misconception I see is that liquidity provider “lose money” every time someone arbitrages. This mainly stems from people not understanding that the exchange can never empty and that the exchange’s price will move with each trade. What this means is that if someone bought 1,000 VET worth of SHA and then instantly sold the SHA, the exchange rate would not move. The trader would also get their assets back minus the fee they paid.

This also means that any trade that is made on the DEX can be counter-weighted by the opposite trade. So in a lot of cases, arbitragers are actually counter-weighting casual traders that moved the exchange rate. In this case, the arbitrager would be profiting from the trader and the centralized exchange they choose to sell on.

An example would be if we started with 100 VET and 1000 VTHO staked:

  • A trader buys 500 VTHO and the reserves are now 200 VET & 500 VTHO.
  • This would mean a new exchange rate of 1 VET : 2.5 VTHO
  • An arbitrager resets the exchange rate so that 1 VET : 10 VTHO.
  • The balances are now 100 VET & 1000 VTHO again.
  • This example does not include fees paid to the LPs but it shows how the exchange rate can be ‘reset’ without the LP losing any funds.

I will talk more about the risks and rewards in Section 5.

3. Vexchange Architecture

The Vexchange DEX has two main smart-contracts:

  • Exchange.vy — Contains the exchange logic for each market.
  • Factory.vy — Creates and manages all the markets within the DEX.

The exchange.vy contract is deployed on the VeChain blockchain and simply serves as a template for deploying new markets. This ensures that all markets have the exact same code and that deploying new markets is cheap (in terms of gas). Note: market refers to a trading pair (i.e. VTHO/VET, SHA/VET).

The factory.vy contract is deployed after the exchange.vy contract and uses the exchange contract as its template for any market it creates. To then begin trading assets, a user must call the createExchange() function on the factory to create a new exchange.

The create exchange function requires a token address. This token address refers to the token that is traded on that exchange/market. Each token can only have one market and all markets trade against VET. This means that VET must be purchased and staked on all markets within the exchange.

As time goes on, users can continue to permissionlessly add new markets to the DEX. Every market within the DEX can communicate with other markets, meaning any token on the DEX can be traded to another token on the DEX in one transaction.

What happens to VTHO generated by VET staked in the smart-contract?

VTHO generated by the VET in the smart-contract is held by the contract until someone calls the tokenScrape() function. The tokenScrape function will then trade the VTHO for VET using the VTHO market and return the VET to the original contract. Once in the original contract, the share value of the liquidity providers (stakers) will rise.

4. Pricing Formula

Vexchange uses the x * y = k price formula, first implemented in prediction markets and then adopted by Uniswap. This formula has the notion of an invariant, where the invariant is the result of Reserve X * Reserve Y. We can then establish a simple rule, x * y must always equal k (the invariant).dota

What this means is that if you have 10 VET and 100 SHA, your invariant is 1000. If a user wants to buy 50 SHA, the final invariant must still equal 1000. So then the new vet balance will be: invariant / (sha_reserve - sha_bought). So in this scenario the new vet_reserve will be (1000 / (100–50)) = 20 VET. As such, the user will need to pay 10 VET in return for the 50 SHA they purchased.

What you may have noticed, is that as more of a particular asset is bought, the more it will cost. The formula for the price is: (reserve_A / (reserve_B-reserve_B_bought)). What this means is that as you empty reserve_B the price will increase exponentially. As the amount left in reserve_B approaches zero, the price for asset B will approach infinity. This is because you cannot divide by zero (an empty reserve).

4a. Reserve Pricing with Volume

Imagine a market starting with 100 VET and 1000 PLA, the price would be the following based on different buy scenarios:

  • Starting market balances: 100 VET : 1000 PLA. Invariant = 100,000
  • User buys 500 PLA. Balances are now 200 VET : 500 PLA. Invariant = 100,000. User paid 100 VET in.
  • User buys 250 PLA. Balances are now 400 VET : 250 PLA. Invariant = 100,000. User paid 200 VET in. A 4x increase in price compared to the last trade.
  • The user then sells 750 PLA. Balances are now 100 VET : 1000 PLA. Invariant = 100,000. User received 300 VET.

Throughout this flow, the price of VET and PLA change. However, the invariant stays constant throughout. In fact, the only way for the invariant to change is if the LPs earn fees. In this case, the invariant would increase over time.

4b. Showing why reserves can never empty

Another property of this formula is that the DEX can never run out of a particular asset. This is because the price is set by dividing the two reserves and it’s impossible to divide by zero. Let’s go through an example:

Starting balance: 10 VET : 100 VTHO. User A purchases 50 VTHO.

  • Costs 10 VET. 20 VET : 50 VTHO.
  • The price was P = 10 VET / (100–50 VTHO) => 0.2VET / VTHO.

User A purchases 40 VTHO.

  • The price is : 2 VET / VTHO. P = 20 VET / (50–40 VTHO).
  • 100 VET : 10 VTHO

You can see the price jumped 10x for the second trade. If the user tried to buy the last 10 VTHO the equation would look like this:

  • P = 100 VET / (10–10 VTHO).
  • Which is P = 100/0.
  • Dividing by zero is not possible as it would result in infinity.

As the denominator approaches zero, the price will exponentially rise towards infinity.

5. Risk vs. Reward for Traders

Throughout this article, we have established that the reserves can never be emptied and that liquidity providers cannot “lose assets”. However, there is still a degree of risk that we will call Exchange Rate Risk. Exchange rate risk refers to the risk that one of your staked assets changes greatly in price compared to the other assets.

What this means to LPs, is that if they staked two assets and one mooned compared to the other, they would end up with more of the cheaper asset because the DEX always holds 1:1 value of each asset. Let’s go through an example:

Scenario A:

  • Stake 100 VET & 100 DBET
  • Price of DBET doubles compared to VET.
  • You now have ~133 VET and ~67 DBET.
  • If you made 10% in fees you would have ~147 VET and ~73 DBET.
  • If you used the 47 extra VET to purchase DBET you would end up with ~97 DBET and 100 VET.
  • This means you would have lost 3 DBET compared to if you had just held DBET and VET.

This means that you should only stake assets where you believe in both VET and the asset you are staking. If you are able to predict the future or are signficantly more bullish on a particular token, it might make sense to stake a much smaller % of your stake.

The best strategy is usually to stake a % of your total token holdings. This means if one of the tokens rises 100%+ you will be able to sell some of your non-staked holdings (which you probably would have done at that price anyway). In this way, your staked tokens will automatically DCA in and out as the price changes.

Important note: If the exchange rate started moving towards 1:1 again, the staker would still keep their fees but would have more DBET. This means the exchange will always outperform simple holding in cases where the exchange rate doesn’t move majorly, or where you stake for a long enough time.

Disclaimer: Nothing in this article constitutes financial advice, this is simply my attempt to better explain the logic behind Vexchange and its pricing formula.

--

--