Class: PandoBot::Lake::Curve

Inherits:
Object
  • Object
show all
Defined in:
lib/pando_bot/lake/curve.rb

Overview

Curve protocal

Constant Summary collapse

A_CONST =
200
N_COINS =
2
ONE =
1
TWO =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a = A_CONST) ⇒ Curve

Returns a new instance of Curve.



17
18
19
# File 'lib/pando_bot/lake/curve.rb', line 17

def initialize(a = A_CONST)
  @a = a
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



15
16
17
# File 'lib/pando_bot/lake/curve.rb', line 15

def a
  @a
end

Instance Method Details

#d_const(xp = []) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pando_bot/lake/curve.rb', line 21

def d_const(xp = [])
  xp = xp.map(&:to_f)
  sum = xp.sum
  return 0 if sum <= 0

  dp = 0
  d = sum
  ann = a * N_COINS

  255.times do
    _dp = d

    xp.each do |x|
      _dp = _dp * d / (x * N_COINS)
    end

    dp = d

    d1 = (ann - ONE) * d
    d2 = (N_COINS + ONE) * _dp
    d = (((ann * sum) + (_dp * N_COINS)) * d) / (d1 + d2)

    break if (d - dp).floor(0).zero?
  end

  d.floor(0)
end

#price_impact(dx, dy) ⇒ Object



108
109
110
# File 'lib/pando_bot/lake/curve.rb', line 108

def price_impact(dx, dy)
  [(1 - (dy / dx)), 0].max
end

#swap(x, y, dx, d = nil) ⇒ Object

swap A for B x, y is liquidity of A, B dx is supply amount of A



84
85
86
87
88
89
90
91
92
93
# File 'lib/pando_bot/lake/curve.rb', line 84

def swap(x, y, dx, d = nil)
  x *= 1e9
  y *= 1e9
  dx *= 1e9

  _d = d || d_const([x, y])
  _x = x + dx
  _y = y_const(_d, _x)
  (y - _y) / 1e9
end

#swap_reverse(x, y, dy, d = nil) ⇒ Object

swap A for B x, y is liquidity of A, B dy is wanted amount of B



98
99
100
101
102
103
104
105
106
# File 'lib/pando_bot/lake/curve.rb', line 98

def swap_reverse(x, y, dy, d = nil)
  x *= 1e9
  y *= 1e9
  dy *= 1e9
  _d = d || d_const([x, y])
  _y = y - dy
  _x = x_const(_d, _y)
  (_x - x) / 1e9
end

#x_const(d, y) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/pando_bot/lake/curve.rb', line 71

def x_const(d, y)
  d = d.to_f
  y = y.to_f
  ann = a * N_COINS
  k = (d * d * d) / ann / N_COINS / N_COINS
  j = (d / ann) - d + y + y
  n = (y - j) / TWO
  Math.sqrt((k / y) + (n * n)) + n
end

#y_const(d, x) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pando_bot/lake/curve.rb', line 49

def y_const(d, x)
  d = d.to_f
  x = x.to_f

  ann = a * N_COINS
  c = (d * d) / (x * N_COINS)
  c = (c * d) / (ann * N_COINS)

  b = x + (d / ann)

  yp = 0
  y = d

  255.times do
    yp = y
    y = ((y * y) + c) / (y + y + b - d)
    break if (y - yp).floor(0).zero?
  end

  y
end