Bid Calculation Functions

The auction system includes several utility functions for calculating bid-related values.

The bidAmountToRequestedSize function calculates the requested stake size based on bid amount and APY:

// src/oada/actions.ts
// Converts a bid amount to the requested stake size based on the APR
// Formula: requestedStakeSize = floor (1000 * 73 * bidAmount / bidApy)
export const bidAmountToRequestedSize = (bidAmount: Big, bidApr: Big) => {
  return bidAmount.mul(1000).mul(73).div(bidApr);
};

The bidAmountToClearingRate function calculates the clearing rate for a bid:

// src/oada/actions.ts
// Performs binary search to find the clearing rate that matches the bid APR
export const bidAmountToClearingRate = (
  bidAmount: Big, // Amount being bid
  sr: Big, // Staked reserves
  r: Big, // Total reserves
  t: Big, // Time parameter [0-100]
  limit: number, // Maximum iterations for binary search
  initMinBidApr: Big, // Initial minimum bid APR
  initMaxBidApr: Big, // Initial maximum bid APR
  initBidApr: Big // Initial bid APR
) => {
  // Implementation of binary search algorithm to find clearing rate
  let prevMinBidApr = initMinBidApr;
  let prevMaxBidApr = initMaxBidApr;
  let prevBidApr = initBidApr;
  const prevRequestedBidStakeSize = bidAmountToRequestedSize(
    bidAmount,
    initBidApr
  );
  let prevPotentialStakedReserves = sr.add(prevRequestedBidStakeSize);
  const potentialReserves = r;
  let prevCr = Big(1000).mul(
    calcClearingRate(
      oadaNetwork.baseRate,
      oadaNetwork.projectedRate,
      oadaNetwork.sigmoidScalar,
      prevPotentialStakedReserves,
      potentialReserves,
      t
    )
  );

  let count = 0;
  while (
    count < limit &&
    !(prevCr.gte(prevBidApr) && prevCr.sub(prevBidApr).lte("1e-6"))
  ) {
    // Binary search implementation
    if (prevCr.gt(prevBidApr)) {
      const nextMinBidApr = prevBidApr;
      const nextMaxBidApr = prevMaxBidApr;
      const nextBidApr = prevBidApr.add(prevMaxBidApr.sub(prevBidApr).div(2));
      const nextRequestedBidStakeSize = bidAmountToRequestedSize(
        bidAmount,
        nextBidApr
      );
      prevMinBidApr = nextMinBidApr;
      prevMaxBidApr = nextMaxBidApr;
      prevBidApr = nextBidApr;
      prevPotentialStakedReserves = sr.add(nextRequestedBidStakeSize);
    } else {
      const nextMinBidApr = prevMinBidApr;
      const nextMaxBidApr = prevBidApr;
      const nextBidApr = prevBidApr.sub(prevBidApr.sub(prevMinBidApr).div(2));
      const nextRequestedBidStakeSize = bidAmountToRequestedSize(
        bidAmount,
        nextBidApr
      );
      prevMinBidApr = nextMinBidApr;
      prevMaxBidApr = nextMaxBidApr;
      prevBidApr = nextBidApr;
      prevPotentialStakedReserves = sr.add(nextRequestedBidStakeSize);
    }
    prevCr = Big(1000).mul(
      calcClearingRate(
        oadaNetwork.baseRate,
        oadaNetwork.projectedRate,
        oadaNetwork.sigmoidScalar,
        prevPotentialStakedReserves,
        potentialReserves,
        t
      )
    );
    ++count;
  }
  if (count >= limit) {
    console.log("Clearing rate search limit reached");
  }

  return prevCr.round(0, Big.roundUp);
};

Last updated