Future Swaps on DeFiChain: A Technical Discussion and Tutorial

solros
DeFiChain Insights
Published in
5 min readApr 11, 2022

With the Fort Canning Road update that went live on April 11, 2022 with block 1,785,960, DeFiChain introduced “future swaps”. In this post, I want to explain and discuss some of the technical details.

What are future swaps?

Future swaps allow you to buy or sell a decentralized asset at the oracle price plus (in the case of buying) or minus (in the case of selling) 5% approximately once per week. The first such swap happened at on April 14, 2022 at block height 1,794,240. You can always find the countdown to the next one here.

Let us consider an example: If dTSLA trades at 1100 DUSD on the DEX while the oracle price (which is determined from the price of the “real” TSLA stock) is 1000 USD, the decentralized one is 10% more expensive and we say that it has a 10% premium. (The premium is calculated by dividing the DEX price by the oracle price. In this case: 1100/1000 = 1.1 = 110%)

The new future swaps allow you to buy dTSLA at only 5% above the oracle price — but only at certain points in time, and the price is calculated with the then active oracle price.

Similarly, if TSLA were to trade at only 900 DUSD on the DEX while the oracle price is still at 1000 USD, it would be at a 10% discount (900/1000 = 0.9 = 90%) and of course this would be bad news for someone wanting to sell dTSLA. For such cases, there is a second kind of future swap with which you can sell it for only a 5% discount.

How do future swaps work?

A future swap contract says something like “I want to buy dTSLA for 1000 DUSD.” or “I want to sell 5AAPL.” You cannot specify the price at which you want to buy or sell though.

You can create them at any time, but they will only be executed at the next future swap block, which occurs every 20,160 = (7*2,880) blocks, which is about once per week. The swap will then be executed using the oracle price that becomes active at this block and hence you know the price at which your swap will be execute 120 blocks (about one hour) before.

Once you create the contract, your input (DUSD for buying or some dToken for selling) will be transferred from your address into the smart contract. When the swap is executed, the smart contract burns the input, mints the output (dToken when buying and DUSD when selling) and you will get the result back into your address.

How can I make a future swap via the CLI?

So far, future swaps are only available via the CLI (command-line interface).

The command to create a future swap contract is called futureswap. To buy dTSLA for 1000 DUSD, you would do:

futureswap <your_address> 1000@DUSD TSLA

In order to sell 5 dAAPL, you would do:

futureswap <your_address> 5@AAPL

Before the future swap block, you can get out of the contract at any time if you change your mind. The command is withdrawfutureswap. The syntax is the same as for creating the contract. For example, to withdraw from the TSLA contract above, you would do:

withdrawfutureswap <your_address> 1000@DUSD TSLA

When you withdraw from the contract, you will immediately get your input funds back into your address. (Also note that you don’t have to withdraw the full amount; you can also just withdraw a part.)

You can get the block height of the next future swap block using getfutureswapblock.

Finally, there are two commands to get information about existing pending future swaps: getpendingfutureswaps <address> gives you the list of pending future swap contracts for a given address; listpendingfutureswaps returns the full list of all pending future swap contracts.

The command getpendingfutureswaps <address> will return something like this:

{
“owner”: “<your_address>”,
“values”: [
{
“source”: “1000@DUSD”,
“destination”: “TSLA”
},
{
“source”: “5@AAPL”,
“destination”: “DUSD”
}
]
}

How can I profit and do I need a bot?

If a token has a premium of more than 5%, you can use the future swaps to make arbitrage gains by selling a token for a high premium on the DEX and then buying it back for only 5% premium via a future swap.

Conversely, if a token is trading at a discount of more than 5%, you can buy it cheap on the DEX and then sell it for a higher price via a future swap.

Assume as before that the dTSLA price on the DEX is 1100 DUSD while the oracle price is 1000 DUSD. Then you can mint dTSLA and sell it for 1100 DUSD on the DEX. You can then use a future swap to buy back dTSLA and settle your loan at only 5% above the oracle price. If, at the future swap block, the oracle price is still at 1000 DUSD, the swap will be executed at 1050 DUSD and you will make a profit of 50 DUSD (minus pool fee for the TSLA-DUSD swap and some interest).

Note, however, that the oracle price at which the swap will be executed may change until execution. You know the actual price after the last price block (120 blocks) before the future swap block: The price that becomes the “next” oracle price then, will become the “active” oracle price at the future swap block and hence the basis for the future swap.

It is important to note that it does not make any difference for the execution price at which time you enter the future swap contract. You know the swap price 120 blocks before — or a little earlier at times when the oracle prices aren’t changing (such as on weekends and holidays). But as I’ve already mentioned above, as soon as you create the contract, your input (DUSD or dToken) will be withdrawn from your address.

So there is no rush to get into those contracts and thus I don’t think it makes sense to enter the swap contract very early. On the other hand, since you have the full price information at least one hour before the swap, you should have ample time to create the contract then (unless you are sleeping or otherwise occupied at swap time, in which case it might make sense to create the contract a bit earlier). In any case, except for the fact that computers don’t need sleep or breaks, I don’t think a bot will have any advantage over a human for this type of arbitrage.

Further Discussion — and “About Me”

I am just someone who is very interested in and fascinated by DeFiChain. A mathematician by profession, I have a very technical view on things and want to share some of my insights here. This is my first post about DeFiChain and I plan to write more.

Please feel free to give feedback and let me know if you have any questions or suggestions for future posts!

Contents distributed by Learn.Block6.tech

👉 Twitter | Telegram | LinkTr.ee 👈

--

--

solros
DeFiChain Insights

Mathematician with a passion for optimization, Python, and blockchain. Likes to teach technical things since that’s the best way to learn them yourself.