Bancor In CoinEx Dex Chain

CoinEx Smart Chain
7 min readMay 25, 2020

--

Written by the CoinEx Chain lab, this article talks about the bancor in CoinEx Dex Chain. CoinEx Chain is the world’s first public chain exclusively designed for DEX, and will also include a Smart Chain supporting smart contracts and a Privacy Chain protecting users’ privacy.

Bancor Protocol

The Bancor Protocol enables automatic price determination and an autonomous liquidity mechanism for tokens on smart contract blockchains. These Smart Tokens have one or more connectors to a network that hold balances of other tokens, allowing users to instantly purchase or liquidate a Smart Token for any of its connected tokens directly through the Smart Token’s contract, at a price that is continuously recalculated to balance buy and sell volumes.

As is introduced in the Bancor Protocol white paper, the Bancor protocol emerges against a background where a large number of tokens are confronted with such problems as small market value and difficulties in listing. To solve the long tail of transaction in the Token economy, it is designed to mortgage a certain amount of connector tokens in smart contracts, such as eth and eos, and calculate the real-time exchange price of smart token relative to connector token through a fixed calculation formula. And such price changes with the issuance of smart tokens.

Simply put, this protocol describes such a way of issuing tokens:

· Token issuance is bound with transaction: for smart tokens in the amount of A purchased from the smart token contract, the total tokens issued will increase by A; for smart tokens in the amount of A sold to the smart token contract, the total tokens issued will decrease by A.

· The token price is bound to the volume of issuance: the specific volume of issuance corresponds to the specific price.

· The market value of the token is proportional to the balance of the connector token in the contract, which is the reciprocal of the connector weight.

For example, suppose we create a smart token ABC, with the initial reserve of 100ETH, the initial issuance of 1000 ABC, and the initial price set at 0.2Eth / ABC. Then cw = 100 / (0.2 * 1000) = 0.5, cw to be used as a constant in the future calculation of the transaction price of the contract.

The price calculation formula of the smart token price $$ P = \ sqrt [{\ alpha}] {\ frac {S} {S_0}} P_0 $$

Where P refers to the most current price, P0 to the initial price, S to the issuance of smart token, S0 to the initial issuance, and $$ {\ alpha} = \ frac {1} {CW}-1 $$.

If the user wants to buy a smart token in the amount of T, the amount of connector tokens he needs to pay is E $$ E = R_0 (\ sqrt [CW] {1 + \ frac {T} {S_0}}) $$ where R0 refers to the connector token balance in the contract.

During the transaction with the contract, CW remains constant, and its calculation formula is $$ CW = \ frac {R} {SP}. $$ R refers to the current connector token balance, S to the current smart token issuance, and P to the current price of smart token relative to connector token.

Bancor In CoinEx Dex Chain

In dex 1.0, we provided a bancor implementation where the price curve was a straight line. In such an implementation, the price increased linearly with the increase in issuance.

Three parameters are required to define dex bancor: the initial price of the smart token, the maximum price, and the maximum supply. It should be noted that, unlike the concept in the bancor protocol, dex bancor separates the creation of bancor from the issuance of smart tokens, and only uses bancor to automate market making. dex bancor is more like a pool for storing the smart token and connector token. When creating bancor, it is required to lock the number of smart tokens specified by the user in the pool. This number is also the maximum of smart tokens that can be sold by the bancor. The user does not need to lock any connector token, and, in future trading transactions, the smart token and connector token in the pool will change dynamically. The price changes deterministically as the number of smart tokens changes. In addition, any token issued on dex can be used as a connector token.

Only the owner of the smart token has the right to create a bancor market with the token as stock. Users can choose to buy or sell at any time according to the current price. The transaction occurs immediately, and there is no need to match the order of the buyer and the seller. Each bancor transaction will change the number of smart tokens locked in the current bancor market, thus changing the smart token price according to the public calculation formula. When the bancor market is created, an earliest erasable time will be specified. After that moment on the chain, the creator of bancor has the right to delete the bancor market.

The typical linear price supply relationship is shown in the figure below

To reveal more details about price volatility, in dex 2.0, we provide a price-supply relationship in the exponential curve. Apart from the initial price, maximum price, and maximum supply of smart tokens, we also introduce the maxMoney parameter.

The price calculation formula is as below: $ price = price_ {init} + (price_ {max} -price_ {init}) * (\ frac {supply} {supply_ {max}}) ^ {AR} $$ connector token balance formula: $ $ money = price_ {init} supply + \ frac {price_ {max} -price_ {init}} {AR + 1} (\ frac {supply} {supply_ {max}}) ^ {AR} * supply $$

Among them, the calculation formula of AR (alpha reciprocal) is $$ AR = \ frac {price_ {max} * supply_ {max} -money_ {amx}} {money_ {max} -price_ {init} * supply_ {max}} $ $ AR varies with the combination of the four parameters when bancor is created, and can generate different price curves. Moreover, this formula supports the scenario where the initial price is 0. As you may have noticed, when AR = 1, the curve degenerates into a straight line.

Implementation of exponential operation

The above formula involves exponential operation. Coinex dex chain uses the Dec data type in cosmos sdk to represent the price. Yet Dec does not support exponential operation, and floating-point operation differs among compilers and machines, thus generating various results. That is unacceptable in Block chain application, so we generate the following table on a machine in advance $$ (AR, supply) => (\ frac {supply} {supply_ {max}}) ^ {AR + 1} $ $, simplified as: $$ (AR, S) => k_ {ar, s} $$

The range of AR is [0.0, 6,0], and the range of $$ \ frac {supply} {supply_ {max}} $$ is [0.000, 1.000]. So there are currently 60,000 data in this table.

Among them, the AR calculated according to the formula is rounded down to the data in the table, and $$ \ frac {supply} {supply_ {max}} $$ is rounded up and down respectively, to get the two adjacent values in the table. Then we calculate the real $$ k_ {ar, s} $$ by the linear interpolation method.

For example:

Suppose the current ar = 3.333, and then we round it down to 3.3, $$ \ frac {supply} {supply_ {max}} $$ = 0.5026, and round it up to 0.502 and down to 0.503. After that, we search two values in the table: $$ k_ {3.3, 0.502} $$ and $$ k_ {3.3, 0.503} $$, and then calculate the final k according to the position of 0.5025 on the line determined by the two points 0.502 and 0.503. In this example k is calculated in the way as below: $ k = \ frac {2} {5} * k_ {3.3,0.502} + \ frac {3} {5} * k_ {3.3,0.503} $$. Then we apply k to the price and connector token balance formula abovementioned to calculate the current smart token price and the amount of money in bancor.

The transaction price can be calculated according to the following formula: $$ P_ {mean} = \ frac {M_ {cost}} {S_ {2} -S_ {1}} $$. As $$ \ frac {supply} {supply_ {max}} $$ is rounded up and down for linear interpolation, it is clear that in this cell, the price is a constant, and the whole price curve is actually an exponential curve as a result of fitting of constants. That serves as a trade-off between calculation accuracy and calculation certainty.

The typical exponential price supply relationship is shown in the figure below

For the generation of the above exponential calculation table, the following conversion can also be made according to calculation rule with the exponentiation power of the same base multiplying, the base remaining unchanged, and exponents adding together: $$ x ^ {a.bc} = x ^ {a} * x ^ {0.b} * x ^ {0.0c} $$ This can effectively reduce the number of calculation items in the calculation table. For example, the exponent takes two decimal places, and, with the conversion of the above formula, the number in the table is reduced from $ 6000 * 1000 $ $ to $ 3 * 9 * 1000 $$. But it is also evident that two extra large number multiplications are required in the actual calculation on the chain. The balance of space and time will be our next focus.

Conclusion

Dex chain automates market making with the bancor protocol, providing price-supply relationships in the form of straight lines and curves in succession, and liberalizing the pairing restrictions of the smart token and connector token. As an important supplement to the matching transaction, it has activated the token economy.

--

--

CoinEx Smart Chain
CoinEx Smart Chain

Written by CoinEx Smart Chain

A public chain built for the decentralized exchange. Website: www.coinex.org Telegram: t.me/CoinExChain

No responses yet