Skip to content

Instantly share code, notes, and snippets.

@xseignard
Created August 27, 2019 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xseignard/d1c4ee1922306063b6ee2752cec2153f to your computer and use it in GitHub Desktop.
Save xseignard/d1c4ee1922306063b6ee2752cec2153f to your computer and use it in GitHub Desktop.

Background script general structure

Overview tab

What we need here:

Price

Expressed in DAI

Price can be obtained with the following formula:

ppm * balanceOf(pool, collateral) / (computedSupply * reserveRatio)
  • ppm: in marketMaker contract
  • balanceOf(poolAdress, collateralAddress): adresses available from the background script, call available on the controller contract
  • price can be exprimed with any collateral, just use the collateral address you need
  • computedSupply: for each collateral, you need to calculate a supply responding to the following formula:
computedSupply = bondedToken.totalSupply + bondedToken.tokensToBeMinted + virtualSupply (of current collateral)
  • reserveRatio: on the collateral token you want to express the price

It can also be obtained with the following call on the marketMaker contract: getStaticPrice.

Market cap

Expressed in DAI

Market cap can be obtained with the following formula:

marketCap = price * (totalSupply + tokensToBeMinted)
  • totalSupply and tokensToBeMinted are related to the bondedToken

Trading volume

Expressed in DAI

Sum of the DAI buy and sell orders amounts.

Token supply

totalSupply of the bonded token, minus the tokenToBeMinted

  • totalSupply and tokensToBeMinted are related to the bondedToken

Reserves

Expressed in DAI

  • balanceOf(pool, dai) of the collateral token, minus the tokensToBeClaimed, call available on the controller contract

Monthly allowance

Expressed in DAI/month

Can be found on the tap object, and converted in DAI/month

Orders/My Orders tabs

Orders get their prices and informations about the collateral from the background script. By default, an order is considered as 'pending'

The state of each orders depends on 2 things:

  • if the order can be found on the list of claimed orders, then the order is 'returned' (no need to check if the order is over)
  • if the the current batch ID is greater than the batchId of the order, then the batch is 'over'

This calculation means you need to call the marketMakerContract.getCurrentBatchId() on a regular basis on the frontend.

Recalculate the state of the orders when one of the following occurs:

  • new currentBatchId
  • new order
  • new claim

On the "My orders" tab, filter the orders by the connected user (accessible via the useConnectedAccount() hook of aragon.js)

TODO: This could be optimized, we don't need to recalculate state of already returned orders.

TODO:: This should be refactored when this PR aragon/aragon.js#361 gets merged and published in a new version of aragon.js

Reserve

There's 2 tapped token, but we only care about the DAI.

Monthly allowance

Expressed in DAI/month

Can be found on the tap object, and converted in DAI/month

Floor

Expressed in DAI

Can be found on the tap object

Collateralization ratios

(TODO: Why displaying ANT collateralization ratio ?)

In percents

ratio = reserveRatio / ppm
  • ppm: in marketMaker contract
  • reserveRatio: in the current collateral token

Total supply

totalSupply of the bonded token, minus the tokenToBeMinted

  • totalSupply and tokensToBeMinted are related to the bondedToken

Token

name and symbol of the bonded token

  • name and symbol are related to the bondedToken

Actions

New Order

Place a new buy or sell order with the following calls: openBuyOrder and openSellOrder passing the following arguments:

  • address: the address of the collateral used
  • amount: amount of collateral or token

To calculate the conversion between the collateral and the bonded token, we use calculatePurchaseReturn or calculateSaleReturn calls on the BancorFormula contract with the following arguments:

  • supply: totalSupply of the bonded token + tokensToBeMinted of the bonded token + virtualSupply of the current collateral
  • balance: balanceOf(pool, collateral) the balance of the given collateral in the pool
  • weight: TODO: ????
  • amount: amount of buy or sell

Edit monthly allocation

Update monthly allocation and floor according to the following rules:

  • no more than one increase per month. Check last update with timestamp on the tapped token
  • no restrictions on decrease
  • an increase should fit within the maximumTapIncreasePct
  • no particular rules on the floor (TODO: what about preventing floor increase over reserve balance ?)

Claim

Just a call to claimBuyOrder or claimSellOrder on the controller passing the batchId and collateral address to be claimed.

Withdraw

Just a call to withdraw on the controller passing the collateral address to withdraw.

Charts

Bonding curve

TODO: TBD

History chart

Passing the list of batches where the startingPrice is calculated with the following formula on the background script:

startPrice = (balance * ppm) / (supply * reserveRatio)

All values coming from the event, except ppm which can be found on the background script state.

(TODO: Should we continue to calculate it on the background script ?)

@xseignard
Copy link
Author

export default {
  // boolean, true when the needed data to display the frontend has been loaded
  isReady: true,
  constants: {
    PPM: {}, //
  },
  adresses: {
    marketMaker: '',
    formula: '',
  },
  // no contracts, since we'll use the `useApi()` hook to get the controller, and externals load
  dai: {
    // everything related to the DAI collateral
    // collateralsToBeClaimed updated on the bg script
    // when PR#361 is merged, add balanceOf
  },
  ant: {
    // everything related to the ANT collateral
    // collateralsToBeClaimed updated on the bg script
    // when PR#361 is merged, add balanceOf
  },
  bondedToken: {
    // everything related to the bonded token
    // tokensToBeMinted and totalSupply updated on the bg script
    // computed supply calculated for each collateral
  },
  batches: [
    // data coming from the newBatch event
    // startPrice calculated
    // buyPrice/sellPrice from the updatePricing event
  ],
  orders: [
    // all orders
    // with claim events, handle 'RETURNED' state here
    // other events will have 'PENDING' state, and updated in the front
    // when PR#361 is merged, 'PENDING' to 'OVER' will be computed here
  ],
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment