Class: PandoBot::Lake::PairRoutes

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

Overview

swap using paris

Constant Summary collapse

PRECISION =
8
HASH_SALT =
'uniswap routes'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pairs) ⇒ PairRoutes

Returns a new instance of PairRoutes.



17
18
19
20
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
# File 'lib/pando_bot/lake/pair_routes.rb', line 17

def initialize(pairs)
  @uniswap = PandoBot::Lake::Uniswap.new
  @curve = PandoBot::Lake::Curve.new
  @routes = {}.with_indifferent_access
  @pairs =
    pairs.map do |pair|
      pair = pair.with_indifferent_access

      base_amount = pair[:base_amount].to_f
      quote_amount = pair[:quote_amount].to_f
      fill_percent = 1 - pair[:fee_percent].to_f

      d = 0
      d = @curve.d_const([base_amount * 1e9, quote_amount * 1e9]) if pair[:swap_method] == 'curve'

      pair.merge(
        base_amount:,
        quote_amount:,
        fill_percent:,
        K: (base_amount * quote_amount),
        D: d
      )
    end
  @pairs.each do |pair|
    set_asset_route pair[:base_asset_id], pair
    set_asset_route pair[:quote_asset_id], pair
  end
  @pairs.freeze
end

Instance Attribute Details

#curveObject (readonly)

Returns the value of attribute curve.



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

def curve
  @curve
end

#pairsObject (readonly)

Returns the value of attribute pairs.



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

def pairs
  @pairs
end

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

#uniswapObject (readonly)

Returns the value of attribute uniswap.



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

def uniswap
  @uniswap
end

Instance Method Details

#pre_order(input_asset:, output_asset:, input_amount: nil, output_amount: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pando_bot/lake/pair_routes.rb', line 47

def pre_order(input_asset:, output_asset:, input_amount: nil, output_amount: nil)
  funds = input_amount.to_f.ceil(PRECISION)
  amount = output_amount.to_f.floor(PRECISION)

  if input_amount.present?
    raise PandoBot::Lake::SwapError, 'input amount invalid' if input_amount.negative?

    best = best_route input_asset, output_asset, input_amount
    amount = best[:amount]
  elsif output_amount.present?
    raise PandoBot::Lake::SwapError, 'output amount invalid' if output_amount.negative?

    best = best_route_reverse input_asset, output_asset, output_amount
    funds = best[:funds]
  else
    raise PandoBot::Lake::SwapError, 'input or output are needed'
  end

  raise PandoBot::Lake::SwapError, 'no pair route found' if best.blank?

  raise PandoBot::Lake::SwapError, 'swap amount not support' if amount.blank? || funds.blank?

  best
    .merge(
      {
        amount: format("%.#{PRECISION}f", amount),
        funds: format("%.#{PRECISION}f", funds),
        route_assets: best[:route_assets],
        price_impact: best[:price_impact],
        routes: Hashids.new(HASH_SALT).encode(best[:route_ids]),
        pay_asset_id: input_asset,
        fill_asset_id: output_asset,
        pay_amount: format("%.#{PRECISION}f", funds),
        fill_amount: format("%.#{PRECISION}f", amount),
        state: 'Done'
      }
    )
end