Introduction
Near the end of 2017, as the markets started to tumble, we began exploring ways to hedge against price risk without moving into fiat. We came up with Vanilla, a trustless way for creating synthetic, leveraged assets on . Not only would it allow users to hedge price risk, it would also make sophisticated financial tools globally accessible.
Today we are making Vanilla fully open-source. While we initially had plans to commercialise the system, along the way we discovered that the regulatory burden of doing so was considerable and so we decided to just put it out there for others to build on or use instead. While it would have been tricky for us to make money by providing this service, there is nothing stopping anyone in the world from creating their own synthetic assets using Vanilla.
We are excited to see how people use Vanilla and would love to help anyone looking to extend or build on it!
How It Works
The Vanilla system allows a user to create a fully collateralised Long/Short contract with leverage, which tracks a specified price feed. The contract is created by the user via a web application and deployed to the on the users behalf.
Vanilla consists of four distinct parts: A , , and the . The Vanilla platform itself is based on the smart contracts and their ability to handle funds, start and close orders, and save currency pair prices on the . The backend, frontend and the price feed services collectively form our implementation of the Vanilla platform, which developers can use as a reference when creating their own implementation (feel free to also copy our code outright). They are responsible for the creation of data that gets saved on the .
The reference implementation functions as follows:
The smart contracts Oracle, OrdersManager and LongShortController are deployed, along with some number of proxy wallets, on the . All the deployed contracts’ addresses are stored in the database, which in our implementation is MongoDB. When a user creates an order for a synthetic asset through the frontend by specifying the desired parameters, one of the proxy wallets gets assigned to the user in question, reserving it for a time period until funds come in and the option order gets created. More proxy wallets get spawned when or before demand exceeds the availability.
The Oracle contract stores key-value pairs of /fiat pairs and their values. Behind the values is a separate process with no API that updates the values based on volume-weighted public API information from 6 or more different exchanges, using Redis as its database. The weighted mean of all the prices, grouped by the currency pair, gets added to a list as a moving average of the last 15 minutes. This is to prevent huge spikes in the price from triggering the contracts prematurely. The latest value then gets stored in the Oracle contract, and the OrdersManager and LongShortController can keep in touch with the latest price.
The backend then works as a contract pinger and as a transaction handler between the smart contracts. Sifting through orders in its database and the OrdersManager contract, it matches up orders, calling openLongShort on LongShortController, moving the funds away from the OrdersManager to the LongShortController along with information of the order’s parameters and the users’ preferred addresses.
From this point forward, the user has information that a synthetic asset has been successfully created. LongShortController is the most independent contract of all in the system, and can be pinged by the users if for some reason the backend is down. When the ping endpoint is called, the smart contract checks if an option with a given hash has enough price fluctuation for a margin call, or exercises the option if its closing date is over expiry. Then the contract itself calculates the rewards and saves them on the . After the rewards have been calculated, a separate withdraw function can be called in the contract to withdraw funds belonging to the given address. A transaction fee is calculated and stored for the maintenance fees of the system, and transferred from time to time to the fee .
Check out the full code .
Published at Wed, 24 Apr 2019 12:23:32 +0000