Bancor AMM DEX formula & calculation example

Bancor Protocol is the world’s first AMM DEX.

AMM DEX is an “Automated Market Maker” Decentralized EXchange comprised of smart contracts called converters. Converters are always available to process token conversions — for instance, to trade tokens AAA for BNT, at some price determined by the reserve ratio of AAA to BNT contained in a “relay” token, which is a type of “smart token” that sits between AAA and BNT, and is generally referred to as AAABNT.

Background:

This article summarizes key aspects of the Bancor Protocol and provides calculations that take place in bancor.cotrader.com — an unrestricted open source portal into Bancor’s decentralized smart contract infrastructure, where new tokens and liquidity pools can be added freely.

For example, anyone can add a token we’ll call AAA, or earn from any trades of it, by acquiring its intermediary “relay” token (AAABNT) that connects it to BNT, and therefore to every other token in the Bancor network — where each token connects to BNT.

BNT is the utility token that has been the trading-pair and reserve currency for all tokens in the Bancor Network, and is essential for supporting voting on the network, and enabling cross-chain trading.

USDB is a new stable token based on the Peg.Network that can be used as a trading-pair (aka reserve), for new tokens added to Bancor. USDB has BNT as its reserve. This connects any USDB based token pairs to any BNT based ones.

The relay tokens that connect any token, such as AAA, to BNT, can be acquired directly in the “relays” tab in the bancor.cotrader.com portal, by direct purchase, or by contributing to the liquidity “pool” for AAA in the “pools” tab. This pool is a reserve of bothf AAA and BNT held in the converter smart contract. The larger the pool, the lower the slippage during trading between these 2 tokens. The smaller the pool, the higher the payout to holders of the relay token, per trade amount. Tokens with such low liquidity “depth” can still have large trade volume relative to depth, especially because their price is moved easily, and this may create more arbitrage opportunities.

For example, AAA (BNT pairing) can be traded to AAA (USDB pairing), using this trade path:

AAA (BNT) => AAABNT => BNT => AAAUSDB => AAA (USDB)

Congratulations! You’ve just learned a lot about how AMM DEX works!

Next learn how pricing works, and how much relay tokens earn:

Formula Calculations:

Say the token AAA has reserves in BNT (the case is the same if the reserve is USDB):

The price of AAA is a function of the changing ratio of AAA to BNT.

Say we start with: AAA / BNT = 3 / 4 and trade 0.2 AAA to get back X BNT.

There will then be 0.2 more AAA and X less BNT.

The new price of AAA after the trade will be:

3.2 / (4 — X)

Q: What is X?: Ie, how much BNT will we get for our 0.2 AAA?

Exact Current Rate (Simple Ratio Price Formula):

An important overlooked fact is that the exact “current” rate is simply the

TO/FROM reserve ratio, or BNT/AAA ratio, or 4/3, in our example case.

BNT return amount = amount of AAA sent times BNT/AAA;

BNT return amount = 0.2 * (4/3) = 0.267

But this is NOT X. To get X, apply slippage of a trade size of 0.2 AAA.

The above current rate is useful to calculate slippage. Note that rate must be compounded for each relay hop — eg 2 times from AAA=>BNT=>CCC.

Slippage:

Slippage is the ratio between the price now, and the average price a trader pays for her traded amount. Note that this slippage isn’t linear with trade size. (advanced note: The WP shows that relay balances grow proportionally to the liquidity pool growth when “connector weight” CW=0.5, which is a typical case. This is a different concept, not too be confused with trade prices)

Slippage factor = 1 — (CurrentRate / RateForTradeSize)

Now we’ll find the BNT returned for our specific trade size of 0.2 AAA using:

Bancor Price Formula:

A: We will first trade AAA to the intermediary “relay” token called AAABNT before hopping from that to get BNT.

So we will trade 2 times:

AAA => AAABNT => BNT.

Let Xrb be the amount of AAABNT we’ll get.

Let X be the amount of BNT we’ll get.

This formula expresses X:

X = _toReserveBalance * (1 — (_fromReserveBalance / (_fromReserveBalance + _amount))

Where, in our case:

_toReserveBalance is 4 — the amount of BNT we have.

_fromReserveBalance is 2 — the amount of AAA we have.

_amount is 0.2 — the amount of AAA we want to trade for BNT.

(advanced note: we simplified the formula a bit by removing a last term that’s only needed in special cases where we don’t want price to move linearly with supply. Ie, where the “connector weight” ratio of tokens A to B isn’t intended to be an equal 0.5: ^ (_fromReserveRatio / _toReserveRatio))

Now, we’ll want to first trade AAA to AAABNT before trading that to BNT, so we’ll need to know how much AAABNT “Balance” is there in the reserve.

Let Rb be this Relay Balance of the AAABNT relay token. This amount is created when creating the AAA/BNT trading pair, and is arbitrary, but typically this relay Balance starts as 2x the BNT reserve value (so 8 = 2*4 = Balance of the AAABNT relay)

Let Xr be the amount of the relay AAABNT we get in the first “hop” in the path

Xr = Rb * ( 1 — ( AAA_supply / ( AAA_supply + AAA_sent )

Xr = 8 * ( 1 — ( 3 / (( 3 + 0.2 ) = 0.5

Now we can make another trade hop along the path, AAABNT=>BNT.

We will use the remaining 7.5 (8–0.5) AAABNT as “to”, and 0.5 as “amount”:

X = 4 * ( 1 — ( 7.5 / ( 7.5 + 0.5 ) = 0.25

X = 0.25

Earning from Pools and Relays:

You can earn from AAA trade fees by depositing an equal value amounts of AAA/BNT into its “liquidity pool”. Ie, you’d maintain the ratio already there.

You receive a “relay” token called AAABNT, in relation to your % of the liquidity pool.

When you return the AAABNT, you get back this same % of the AAA+BNT in the liquidity pool.

The pool’s AAA and BNT both grow by the fee % at each trade.

The fee % simply stays in the pool:

Ie, you don’t get back your full X (0.25 BNT in our case), but rather

Z, which is X minus the converter fee Y:

Z = X — Y%, ie

Z = X * (1-Y)

Eg, if

Y = 0.1% (0.001 parts) per trade, remove that from the 0.25 returned, so

multiply 0.25 by (1–0.001) 2 times, for the 2 hops along the path.

Z= (0.25 * 0.999²)

Z = 0.2495

Z is how much BNT you should receive after the fee, and the collected fee is split between holders of the AAABNT relay. However, such relay tokens are also tradable, and are therefore also speculative assets.

Congrats! If you followed all that, your “midichlorians” count is quite high, and you now very deeply understand one of the most important blockchain Decentralized Finance (DeFi) dApps.

You might next learn more about how these AMM DEXs can be used to prove ROI performance of non-custodial investment funds from the world’s first such marketplace at cotrader.com

Author: Gary Bernstein: Founder of CoTrader