Market Calculations
How to calculate pricing, discount, vesting etc for markets
Pricing & Discount
First, assuming you wish to display USD pricing, you will need to obtain USD prices for your quote and payout tokens. How you do this is up to you, and can vary depending on the token. For example, if both tokens have accurate pricing available on Coingecko or a similar service, you can simply pull the price from their API. In other cases, you may wish to use an oracle or calculate from an LP pair. If your quote token is something like an LP pair or a Balancer Weighted Pool, you will have to manually calculate the value of the token to get accurate market pricing.
You will also need the number of decimals for your payout/quote tokens - if you are simply displaying your markets with a known set of tokens, it makes sense to hardcode these values.
This is a UI issue - the underlying contracts work on the quote/payout ratio, not USD price, so nothing here will affect the actual price at which users can bond, just the usability of your UI. Accordingly, if you wanted to display pricing in EUR, BTC or anything else, you could substitute USD prices for those.
Once you have your pricing information, we recommend the following contract requests:
The next step is to adjust for market scale and get the discounted price:
The price decimal scaling for a market is split between the price value and the scale value in order to be able to support a broader range of inputs. Specifically, half of it is in the scale and half in the price. To normalize the price value for display, we can add the half that is in the scale factor back to it.
Here, quoteTokensPerPayoutToken
is the exchange rate between the two tokens used by the contract to determine the current bond price. Multiplying this by the quote token's USD price gives us the USD price at which a user can purchase from the market.
Next we can calculate the discount:
The 'discount' can also be negative - i.e. purchasing from BondProtocol is currently more expensive than purchasing at market rates. Once the daily capacity has been hit, the discount will be negative until it gradually ticks back to a positive discount.
It is strongly recommended to make this clear in your UI (different colors, warning message etc) to avoid complaints from users who accidentally bonded at a large premium.
Vesting
There are a few options for displaying vesting. First, if you are running an instant swap market, you can just display it as such in the front end. The auctioneer contract has an isInstantSwap(marketId)
function, if you are unsure.
In most cases, your market will either have a fixed vesting date, or a fixed vesting term (e.g. 14 days). You can get the vesting
period value from the auctioneer contract's terms(marketId)
function, which returns 4 values, vesting
(uint48
) being the 3rd. Since this doesn't change after market creation, you could hardcode it.
If you have a fixed expiration market, vesting
will be a timestamp. We use this to create a new JS Date
object for the expiration date, which you can then format as desired, for example:
If you have a fixed term market, vesting
will be a duration in seconds. We use the following function to create a human-readable value:
Capacity, Payout & Allowances
To display the current capacity of your market in a human-readable way, you need to adjust the currentCapacity
value by your capacity token's decimals - the capacity can be in either quote or payout token depending on how you set up the market, this could be hardcoded if you know the decimal value will be the same across all markets you are displaying:
To calculate the max payout for a market:
To calculate the max amount of quote tokens accepted for a single purchase:
We multiply the result of Number(maxAmountAccepted) - Number(maxAmountAccepted)
by 0.005 in order to reduce maxAmountAccepted
by 0.5%.
This is due to the fee being slightly underestimated in the contract function.
See comment on https://github.com/Bond-Protocol/bond-contracts/blob/master/src/bases/BondBaseSDA.sol line 764 for more detail.
Since payout tokens are not held by BondProtocol, it is possible that the maximum payout could be limited by either the payout token balance of your owner address, or the spending allowance. We check these, and if one is insufficient, display an error message showing the exact issue. Should you wish to do the same, it is straightforward:
Finally, you can optionally use isLive
to check the status of your market and hide it from your UI when closed.
Last updated