How Spectrum provides best rate deposit to Liquid Pylon Vault

Spectrum Protocol
3 min readDec 27, 2021

One feature that come together with Liquid Pylon Vault is that we also provide best rate deposit to the vault using only UST.

When deposit, app shows how much to swap at Terraswap and how much to deposit via Pylon Gateway

Most of the time, we can get bPsiDP-24m from Terraswap at a discount rate, however, if we swap too many tokens in one transaction, the price of bPsiDP-24m might go above $1.

To ensure that you will always get the best rate for their deposit, we need best rate deposit mechanism for this vault.

Solution overview is simple:

  1. We will find maximum UST amount (Optimal amount) to swap at Terraswap to get bPsiDP-24m with the rate less than $1 first.
  2. Then the remaining amount will deposit directly via Pylon gateway, which rate is fixed at $1.
Calculate swap and direct deposit amounts to ensure best rate deposit (Illustration)

Here are the implementation details (Warning: math ahead!):

In Terraswap, we cannot swap directly from UST to bPsiDP-24m. We need to swap in 2 steps:

  1. From UST to Psi
  2. Then Psi to bPsiDP-24m

For Psi-UST pool: finding UST for a Psi amount

Terraswap swap rate is using this formula

From (1), this is the formula for swapping UST to Psi

From (2), we can shuffle variable to find UST amount

From (3), we can simplify to

For bPsiDP-Psi pool: finding bPsiDP for a Psi amount

This step is easy, from (1), we can get to this formula

And from (5), we can simplify to

Mix 2 pools together

Given that UST amount equal to bPsiDP amount, from (4) and (6), we can come to this formula

Let’s replace Psi amount with X

Now let’s move everything to one side

And simplify to

From here, we can apply quadratic formula

After apply, it will transform into

And simplify into

Now we can find optimal Psi amount. After we get X, use equation (4), we can find optimal UST amount

Here is pseudo code to find optimal UST amount

Conclusion

With best rate deposit to Liquid Pylon Vault, you can simply deposit UST to the vault and we will make sure that you will get the best rate without a hassle of going too many different apps or comparing different exchange rates.

Learn more about Liquid Pylon Vault or explore Spectrum Protocol at https://terra.spec.finance

--

--