Bitshares: Margin Trading and Swap contracts on the DEX discussion

George Harrap
9 min readOct 14, 2018


Bitshares is the longest running decentralised cryptocurrency exchange and one of the pioneers of collateralised stablecoins. The Bitshares DEX provides a sophisticated set of trading tools already but through this article I hope to be able to add to the discussion on adding one key component which I think would very much benefit the ecosystem- Trading on Margin via an active P2P lending market.

Summary TLDR

  • A lending market (ala Bitfinex style) on Bitshares will provide passive income to holders of Smartcoins like BitCNY and BitUSD at a market set interest rate.
  • Increased lending will incentivise a growth in supply of Smartcoins.
  • Increased collateralisation of Smartcoins will increase demand (and price) of Bitshares.
  • Trading on margin will now be available to all Bitshares DEX users increasing volumes
  • Execution of the P2P swap logic on a DEX is fundamentally better than a CEX as it removes the trusted counterparty- a big risk for traders.
  • Swap logic can be separate to existing smartcoin collateralisation logic making it simpler than looking at other options for passive income requiring changes to the underlying Smartcoin settlement mechanisms in bitshares.
  • All swaps are collateralized and accounted for onchain ensuring optimal transparency.
  • This is not something that is beyond the scope of Bitshares capability and on the contrary I think Bitshares is the best suited Blockchain for this due to the speed, scalability and access to stablecoins.


The concept I am discussing today can be called ‘P2P Swap Contracts’, ‘Margin trading and lending’ or ‘CFDs’, terms which I may use interchangeably but essentially I am referring to the concept in operation currently on Bitfinex here. Margin trading is an important addition to efficiently functioning markets.

One of the first attempts at trading on Margin in the Bitcoin world was a trading platform named Bitcoinica in 2011, it was the first time I was exposed to margin trading concepts and after a successful launch and year or two of trading, Bitcoinica subsequently collapsed under suspicious circumstances.

However, from the ashes arose a company we are all familiar with today- Bitfinex. It is purported online that Bitfinex actually started by reverse engineering the leaked Bitcoinica code and again as one of the original users from when Bitfinex was in Beta I can safely say it gained traction due mostly entirely to the lending and margin mechanisms for trading. For those unfamiliar, when Bitfinex first launched in Hong Kong in 2012 it had no liquidity and instead built a proxy orderbook that displayed the Bitstamp orderbook on Bitfinex in addition to ‘Bitfinex’ orders whereby if a trade was executed and it hit a Bitfinex placed order, it would be settled on Bitfinex, however if it hit a Bitstamp order then Bitfinex would execute the trade on Bitstamp and settle balances later. This persisted for almost a year until Bitfinex had somewhat of enough liquidity to remove the need for piggy backing off the Bitstamp orderbook. This helped accelerate the amount of money that was being lent to traders on Bitfinex as now everything could be executed in house on their own orderbook. The big benefit of trading on Bitfinex vs Bitstamp back then was margin trading and lending. When I first started lending USD on the Bitfinex swap contracts, there was a period where daily (yes DAILY) interest was 8.6% on USD as the demand for USD to margin trade with was very large compared to the number of people lending USD.

Margin trading and lending catapulted Bitfinex into the top exchanges due to the fact that people who were unfamiliar with the vagiaries of day trading could instead simply just lend USD to margin traders for a very healthy return for somewhat low risk. Today Bitfinex has hundreds of millions of dollars active in the lending and margin markets on their exchange and users can elect to lend a large number of cryptocurrencies for a daily interest rate. The only problem is trusting Bitfinex, a centralised company which has been hacked twice.


What I would propose consists of the following constituent parts:

  • A lending market
  • Separate Trading at Margin wallet balance
  • A ‘Trade at Margin’ order logic
  • On chain settlement logic

The lending market is a standalone orderbook whereby users can lock an amount of for example BitUSD and lend it to another DEX account for a user defined amount of interest (which can be calculated every block) to be used in their margin trading account. Much like any orderbook there are people on both sides of the orderbook looking to lend bitUSD at X rate (Ask) and looking to borrow bitUSD at Y rate (Bid). The free market will ensure an optimal price for the bitUSD being lent that meets the demand of borrowers and the appetite to lend by lenders.

A screenshot from the Bitfinex lending orderbook

Separate Trading at Margin Wallet

When someone wants to trade at margin, they need to take an Ask from the lending orderbook and apply that directly to the trade on which they’re about to open. Bitfinex ensures that margin trade orders are not commingled with non-margin orders by separating these functions into separate wallets. This ensures that any margin calls will be enacted upon the open orders in the Trading at Margin wallet only and not affect other balances. It is also important that the user cannot get a margin loan then extract the funds and abscond with the money, having a separate wallet ensures that any funding received can only be used to trade on the nominated markets and it can only be used to undertake a few functions (buy and sell limit orders and not withdraws).

Trade at Margin logic

The user should have the option to trade at margin on a given currency pair, for example bitUSD/BTS. In order to do this they need some collateral to ensure that they can cover interest repayments and should the value of their portfolio drop by a certain margin (in Bitfinex this is approximately 15%) then they will be liquidated and lose their collateral while the lender is repaid in full. A full example explanation is provided by Bitfinex here:

The amount of the financing, the term of the financing, and the interest rate are all commercial terms negotiated through the Financing Order Book between Financing Providers and Financing Recipients. For instance, assume that A has $30.00 (in dollars) in her account on the Site. A obtains $70.00 in financing at X interest rate for Y term on the Financing Order Book (thereby becoming a Financing Recipient) from B, a Financing Provider. With that aggregate amount of $100.00, A may purchase $100.00 in bitcoins on the Trading Order Book from C, or from one or more other sellers. A has the right to repay the financing (including any accrued interest) at any time without pre-payment or other penalty. Obtaining financing does not create any obligation to purchase bitcoins on the Trading Order Book. A may also replace financing from B at any time with more favorable financing.

In the above example, the bitcoins purchased by A ($100.00) are subject to a Lien in favor of B up to the total amount of financing secured from B ($70.00 plus any interest component). A may remove any amount of bitcoins from the Site that is not subject to the Lien. If the Financing Recipient’s equity falls to or below 15% — calculated as the quotient (expressed as a percentage) obtained by dividing (a) the excess of (i) the market value of the purchased bitcoins over (ii) the total principal amount (plus accrued and unpaid interest) relating to all financing outstanding by (b) the market value used in (a)(i), above — Bitfinex will force the liquidation of the bitcoins in A’s account without notice to A, return financing to the Financing Provider, with accrued interest, and return the balance to the Financing Recipient. Bitfinex does not make margin calls. For example, if the purchased bitcoins’ value falls from $100.00 to $82.35, the difference between that value and the financing obtained by A would be $82.35 — $70.00, or $12.35. Taken as a percentage of the bitcoins’ value, $12.35 ∕ $82.35 equals 15%. In other words, if the value of the bitcoins fell to $82.35 in aggregate, A’s bitcoins would be liquidated by Bitfinex on the Trading Order Book, B would be repaid, and any remaining difference ($12.35, exclusive of interest) would be A’s to retain.

The user should select a ‘Trade at Margin’ button on the UI, and after calculation of their maximum leverage, the Blockchain would execute the order by taking the lowest Ask on the lending book up to the amount of leverage allowable by the user and add this as one limit order on the BitUSD/BTS market. The users position is now established and the blockchain will use the market value (based on last trade) to determine the margin position of that user to determine if they will be margin called or not.

On Chain Settlement Logic

In the event of a margin call, if the users balance drops below a certain threshold, the blockchain will sell the users active order in market and return 100% of the funds to the lender with any remaining funds being returned to the margin account of the user. This mechanism is the same for either long or short margin positions (in the case of a short you are taking from the Bid on the Lending orderbook).

In the event that a user wishes to exit their position and repay their loan they may do this at any time whereby the blockchain will sell their outstanding balance in the Margin wallet, return the funds to the Margin wallet, and execute a repayment of 100% of funds from that wallet to the Lender with the remainder remaining with the user.

Effect on the DEX

The addition of this functionality in my opinion will massively add to liquidity, volume, smartcoin supply and increase positive price pressure on the BTS token.

Firstly the addition of an active lending market gives holders of stablecoins like bitUSD, BitCNY and others an incentive to hold these coins and lend them out for a return. There has been discussion in the past about how to adequately incentivise smartcoin user adoption with a number of mechanisms discussed usually involving changes to the current Smartcoin collateralization logic. This proposal would keep existing smartcoin creation logic in place and add a separate mechanism that users can opt into should they want to do this.

Secondly, as shown via the growth of Bitfinex and a number of other top exchanges that have implemented this, it has contributed in large part to the growth of these exchanges in terms of volume and liquidity. Bitfinex grew from nothing to where it is today by offering people a low risk high yield way to earn a return by engaging with the platform. This removed the need for Bitfinex to utilise the orderbook of Bitstamp and when a easy option to lend for a return is available this attracts more capital to the orderbooks of the exchange. All funds on a lending orderbook can now directly be utilised to benefit the market depth of the orderbook itself, this is in contrast to today where people A) may not have their funds on the DEX and B) funds which are on the DEX are not on the orderbook and lay dormant in their account. This puts those existing funds to use and attracts new funding to the DEX which is an acceleration function for growth.

Next, the option to lend a smartcoin for a return will not only utilise existing supply better but also attract new money to the DEX as new users want to hold a stable asset and yet earn a (very close to) risk free return. In bear markets, having a stablecoin protects portfolio value and certainly earning interest on that stablecoin puts their capital to good use earning a return even in a downtrend, whereas in a bull market there is an increased demand for stablecoins for long positions which increase lending rates and therefore attract more capital to the DEX.

All of this increased usage of bitshares assets and supply of stablecoins generate new fees for the BTS blockchain in addition to new assets requiring collateralisation to be created forcing new buy orders for BTS influencing the price upwards.

Next steps

The purpose of this post is to generate discussion in the bitshares community on the feasibility of this endeavor and the technical requirements and changes that may be required to support it. Firstly we need to engage the community and secondly it would be great to get input from the Core team on what may be required to make this a BSIP and a reality for Bitshares. Looking forward to hearing the feedback.