Bringing True Composability and Robustness to Decentralized Finance

Vishakh
7 min readDec 9, 2020

--

Blockchain finance is really taking off, so much so that people are seriously thinking about blockchain-centric capital markets. DeFi has been growing a lot and there is much talk about composability and money legos. Still, DeFi applications mainly only trade tokens in volatile synthetic assets and are composable only at the application level. How then does blockchain finance reach greater maturity with a more sophisticated set of financial instruments and institutional grade risk management? This is where a twenty year old research paper comes into the picture.

Composable Contracts

It’s remarkable how a paper published by Jones, Eber and Seward at a functional programming conference two decades ago has a direct and productive use in cutting edge finance today. The paper describes a simple framework for building arbitrarily complex instruments using simple building blocks. The results can be used not just to represent the behavior of financial instruments such as loans and options but also to generate valuation models for them. With a little bit of further application, we also get the ability to measure and manage financial risk.

Let’s consider a trivial example to consider how these composable contracts work (with apologies to the authors of the paper). We begin with this simple contract which represents me giving you a dollar immediately:

I now apply a scale combinator so you actually get ten dollars:

We are done with the first leg of the instrument. Now let’s create a second leg along the same lines so you have to pay me $11!

We use the ON combinator to put off the $11 payment to December 31:

Our second leg is now complete so we can combine the two legs with the AND combinator:

The net effect is that I give you ten dollars today and then, on December 31, you hand me back $11. And voila, we have a loan!

Using a few more types of observables (1 USDC, 1 DAI, LIBOR, the EUR/USD exchange rate) and combinators (OR, IF, UNTIL, etc) we can create a rich array of financial instruments. The paper contains a nice and elegant construction of equity options. In fact, with just a small set of observables and combinators, we can represent instruments as complex as swaptions and credit default swaps. Using contracts themselves as observables, we can have complex compositions between different apps on the same blockchain.

Anyone familiar with functional programming sees a very obvious translation here. We can use well-written, well-tested functions to represent observables and combinators and then simply compose over them to create functions representing the behavior and life cycles of complex financial instruments. If the basic functions are sound, the compositions over them will be sound as well.

The boffins among us might also see many possibilities for going from a visual representation to generated code or simple domain-specific languages for generating smart contract code where the structurers of the instrument don’t even have to be coders and can use a collaborative UI tool. On platforms such as Tezos where functional constructs are natively supported, such translations are even easier to do.

Without having yet considered the fine grained details, one gets the sense that it should be easier to generate formal proofs of correctness for these instruments due to their composable nature.

Standardizing Blockchain Finance

Smart contracts for blockchain finance are purpose built for specific applications. Even if they use components from open source libraries such as OpenZeppelin, they need to be individually examined and vetted. Instead, if we build a standard library for composable smart contracts with simple observables and combinators, we will save ourselves a lot of coding and auditing time. Since the behavior of the ensuing smart contracts will be standardized, tools such as wallets and indexers could also just work out of box for all apps which use the library.

Composability is a buzzword in DeFi right now but it refers to a very narrow and limited sort of composability. Synthetic asset contracts can work with decentralized exchange and lending contracts, for example wBTC with Uniswap and Compound on Ethereum, but interactions between these apps have to be built on an ad-hoc basis. There is no guarantee that financial instruments created by one app will play nicely with another app. If, on the other hand, multiple apps used the same composable framework to issue financial instruments, they could be guaranteed to work with each other, with applications being able to build higher-level instruments on top of others’ instruments. For example, one app could issue hundreds of thousands of peer-to-peer loans at the retail level while another could make large macro bets on them using tranched baskets at the institutional level. Furthermore, with the transparency that blockchains bring, we could avoid the opaque chains of ownership and nebulousness of counterparty risk which almost brought down the legacy financial system in 2008.

Always be hedged!

Back when I started working with credit derivatives amidst rows of arrayed screens in midtown Manhattan, I noticed there was as much focus on making money as understanding why the money had been made. Anyone with ICO-crazy friends from 2017 quickly realized even people with a basic ability to install Metamask and a compulsive desire to drown their existential ennui in constant streams of adrenalin could make a killing during a bull run. Equally, scamcoin cowboys could lose more money in a week than a rogue Asia-Pacific employee at Barings Bank in the 1990s. This is why if Defi is to ever work at an institutional scale it has to operate with institutional rigor. For this, we need price transparency and disciplined risk management.

We are in the infancy of the era of decentralized finance. In the grand scheme of things, most DeFi assets are trivial first degree instruments whose prices are determined on open markets. For DeFi to take the next steps towards robust capital markets, higher order derivative instruments like swaptions will be needed for both speculation and hedging. While the /r/wallstreetbets vanguard are content making massively unidirectional leveraged bets using BitMex options, mature capital markets will require disciplined trading based on sound risk management. For this, we need to be able to analytically value a plethora of financial instruments and calculate risk to a variety of factors, including conventional ones such as benchmark interest rates, FX rates of BTC and ETH and unconventional inputs like CDP liquidation rates.

Once again, composable contracts come in very handy. The Jones paper shows how, given a composable representation of a financial instrument, it’s relatively straightforward to generate code to value it. We can even swap in and out alternative models for discounting cash flows, pricing options, etc. If everyone uses the same types of valuation methodologies for similar instruments, we overall end up with price transparency for even relatively esoteric instruments. Once we know we can price an instrument using market data inputs, it’s relatively straightforward to see how the price changes in response to small perturbations of some individual market data. For example, we can tweak the current price of ETH by a fraction of a percent and observe the change in valuations across our entire portfolio to understand our overall exposure to the price of Ethereum. We have in effect calculated a type of FX risk. We can similarly compute various types of interest rate risks, credit risks and so on in order to predict the market value impact of specific events, explain our daily P&L by attributing it across changes in multiple market inputs and thus hedge our overall exposure to the market like good traders through space and time. This is the sort of institutional discipline that can eventually build decentralized and fleshed out capital markets on blockchains.

Wrapping Up

Since you made it this far, perhaps you are the sort of person who is expectantly reading on to see how all this concretely works on blockchains. I’m afraid that requires a lot of detail and is likely to drive off the people at the end of their ten minute attention spans. Suffice to say, with a few adjustments to the core concepts, it’s quite straightforward to implement composable instruments using smart contracts in a way that also works with most standard token types. If this post garners any traction at all, I will follow up with another post on this topic which lays out all the gory implementation details. As the kids say, like and subscribe for more.

There have also been many follow-up efforts in the past twenty years, with a litany showing up on Google Scholar. There are likely many wonderful supplemental ideas which also need to be incorporated in any serious effort to implement composable contacts. Perhaps I will also do an overview here in the future.

So is this post merely a general call to arms for DeFi builders or am I not entirely a disinterested party in these matters? I have long wanted to use these ideas to build industrial grade blockchain finance apps. Who knows, maybe someday soon we might have a launch announcement or two out there. In the meanwhile, let a thousand flowers bloom and may composable contracts finally fulfill their promise in this new and exciting decentralized realm!

I’d like to thank my friend Sanjay Venkat for introducing me to the wonderful composable contracts paper.

“Favela Cantagalo” by alobos life is licensed with CC BY-NC-ND 2.0. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-nd/2.0/

--

--

No responses yet