mento-protocol / mento-sdk

An SDK for integrating applications with the Mento protocol
MIT License
0 stars 3 forks source link

Exchange.getBuyAndSellBuckets equivalent? #49

Open aaronmgdr opened 5 months ago

aaronmgdr commented 5 months ago

The old Exchange Contracts had a getBuyAndSellBuckets view method.

when minReceiveAmount of the token was 0, we had been using this to give warnings if a trade/swap would significantly move the price (see code block below).

function checkNotDangerousExchange() {
  const chainRate = buckets[1].dividedBy(buckets[0])
    let warningMessage
    // XX% difference between rates
    if (Math.abs(oracleMedianRate.dividedBy(chainRate).toNumber() - 1) > deppegedPricePercentage) {
      const warningDeppegedPrice = `Warning ${stableTokenInfo.symbol} price here (i.e. on-chain) is depegged by >${deppegedPricePercentage}% from the oracle prices (i.e. exchange prices).`
      warningMessage = warningDeppegedPrice
    }

    // X% of the bucket
    const bucket = buyBucket ? buckets[1] : buckets[0]
    if (bucket.dividedBy(100 / largeOrderPercentage).isLessThanOrEqualTo(amount)) {
      const warningLargeOrder =
        'Warning you are executing a large order, risk of price slippage i.e. getting an unfavorable exchange rate.'
      warningMessage = warningMessage ? `${warningMessage} ${warningLargeOrder}` : warningLargeOrder
    }  

  }

The questions

  1. Is there an equivalent method on Broker or way you would handle something similar.

  2. since with mentoSDK im fetching a quoted minAmountOut, do i suspect i dont even need to worry about "dangerous" swaps

nvtaveras commented 5 months ago

@aaronmgdr There's no way of fetching the bucket sizes at the moment via the SDK but one way you could achieve a similar slippage check is by calculating the effective price based on the quote from getAmountOut and then comparing that to oracleMedianRate in the same way you are currently doing. I think in that way you probably wouldn't need the second check on X% of the bucket since any potential slippage due to that will be reflected in the first check.

Also it's important for you to set the minAmountOut parameter anyways after getting a quote since otherwise the trade can be frontrun. By looking at the command in the CLI it seems that if people don't provide --forAtLeast it would probably be due to not knowing the exchange rate rather than wanting to force a trade at any execution price.

aaronmgdr commented 5 months ago

thanks for the reply!