Advertisement
Guest User

Untitled

a guest
Apr 9th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Decimal from 'break_infinity.js'
  2.  
  3. let BN = (val) => new Decimal(val)
  4.  
  5. const fcopy = (n) => new BN(n.toFixed());
  6.  
  7. let calc = ({
  8.     N_COINS,
  9.     PRECISION_MUL,
  10.     USE_LENDING,
  11.     LENDING_PRECISION,
  12.     PRECISION,
  13.     A,
  14.     fee,
  15.     admin_fee,
  16.     supply,
  17.     virtual_price,
  18.     timestamp,
  19.     balances,
  20.     rates,
  21. }) => {
  22.  
  23.     const ZERO = BN(0);
  24.     const ONE = BN(1);
  25.  
  26.     const FEE_DENOMINATOR = 10 ** 10;
  27.  
  28.     function _stored_rates() {
  29.         let result = PRECISION_MUL;
  30.         let use_lending = USE_LENDING;
  31.  
  32.     }
  33.  
  34.     function get_D(xp) {
  35.         let S = ZERO
  36.         for (let _x of xp)
  37.             S = S.plus(_x)
  38.         if (S == 0)
  39.             return ZERO
  40.  
  41.         let Dprev = ZERO
  42.         let D = fcopy(S)
  43.         let Ann = BN(A).times(N_COINS)
  44.         let Annsub = Ann.minus(1)
  45.         for (let _i = 0; _i < 255; _i++) {
  46.             let D_P = fcopy(D);
  47.             for (let _x of xp) {
  48.                 let c1 = D_P.times(D);
  49.                 let c2 = _x.times(BN(N_COINS))
  50.                 let c3 = c2.plus(ONE)
  51.                 D_P = c1.div(c3)
  52.                 //D_P = D_P.times(D).div((_x.times(N_COINS).plus(ONE)))
  53.             }
  54.             Dprev = fcopy(D)
  55.             let d1 = Ann.times(S);
  56.             let d2 = D_P.times(BN(N_COINS))
  57.             let d3 = d1.plus(d2);
  58.             let d4 = d3.times(D);
  59.  
  60.             let d5 = Annsub.times(D);
  61.             let d6 = BN(N_COINS + 1).times(D_P)
  62.             let d7 = d5.plus(d6)
  63.             D = d4.div(d7);
  64.             if (D.gt(Dprev))
  65.                 if ((D.minus(Dprev)).lte(BN(1)))
  66.                     break
  67.             else
  68.                 if ((Dprev.minus(D)).lte(BN(1)))
  69.                     break
  70.         }
  71.         return D
  72.     }
  73.  
  74.     function get_y(i, j, x, _xp) {
  75.  
  76.         //if(! ((i != j) && (i >= 0) && (j >= 0) && (i < N_COINS) && (j < N_COINS))) throw new Error('get y assert failed')
  77.  
  78.         let D = get_D(_xp)
  79.         let c = fcopy(D)
  80.         let S_ = ZERO
  81.         let Ann = BN(A).times(BN(N_COINS))
  82.  
  83.         let _x = ZERO
  84.         for (let _i = 0; _i < N_COINS; _i++) {
  85.             if (_i == i)
  86.                 _x = x
  87.             else if (_i != j)
  88.                 _x = _xp[_i]
  89.             else
  90.                 continue
  91.             S_ = S_.plus(_x)
  92.             c = c.times(D)
  93.                 .div(
  94.                         _x.times(BN(N_COINS))
  95.                     )
  96.         }
  97.         c = c.times(D)
  98.             .div(Ann.times(N_COINS))
  99.         let b = S_.plus(
  100.                 D.div(Ann)
  101.             )
  102.         let y_prev = ZERO
  103.         let y = fcopy(D)
  104.         for (let _i = 0; _i < 255; _i++) {
  105.             y_prev = fcopy(y)
  106.             let y1 = y.times(y)
  107.             let y2 = y1.plus(c)
  108.  
  109.             let y3 = BN(2).times(y)
  110.             let y4 = y3.plus(b).minus(D)
  111.  
  112.             y = y2.div(y4)
  113.  
  114.             if (y.gt(y_prev))
  115.                 if ((y.minus(y_prev)).lte(BN(1)))
  116.                     break
  117.             else
  118.                 if ((y_prev.minus(y)).lte(BN(1)))
  119.                     break
  120.         }
  121.         return y
  122.     }
  123.  
  124.     function _xp(rates) {
  125.         let result = rates.map(rate=>BN(rate))
  126.         for(let i = 0; i < N_COINS; i++) {
  127.             result[i] = result[i].times(BN(balances[i])).div(PRECISION);
  128.         }
  129.         return result;
  130.     }
  131.  
  132.     function get_dy(i, j, dx, usefee = false) {
  133.         let precisions = PRECISION_MUL.map(p=>BN(p));
  134.         let currentRates = rates.map((r,i) => BN(r).times(precisions[i]))
  135.         let xp = _xp(currentRates);
  136.         let x = xp[i].plus(BN(dx).times(currentRates[i]).div(PRECISION))
  137.         let y = get_y(i, j, x, xp)
  138.         let dy = (xp[j].minus(y)).times(PRECISION).div(currentRates[j])
  139.         let _fee = BN(fee).times(dy).div(FEE_DENOMINATOR)
  140.         if(!usefee) _fee = ZERO
  141.         console.log(+dy.minus(_fee))
  142.         return dy.minus(_fee);
  143.     }
  144.  
  145.     function get_dy_underlying(i, j, dx, usefee = false) {
  146.         let precisions = PRECISION_MUL.map(p=>BN(p));
  147.         let currentRates = rates.map((r,i) => BN(r).times(precisions[i]))
  148.         let xp = _xp(currentRates);
  149.         let x = xp[i].plus(BN(dx).times(precisions[i]));
  150.         let y = get_y(i, j, x, xp);
  151.         let dy = (xp[j].minus(y)).div(precisions[j])
  152.         let _fee = BN(fee).times(dy).div(FEE_DENOMINATOR)
  153.         if(!usefee) _fee = ZERO
  154.         return dy.minus(_fee);
  155.     }
  156.  
  157.     return {
  158.         get_dy_underlying,
  159.         get_dy,
  160.         _xp,
  161.         //get_dx_underlying,
  162.     }
  163. }
  164.  
  165.  
  166. export default calc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement