Class: Bancor::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/bancor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(eth:, rate:, crr:) ⇒ Protocol

Returns a new instance of Protocol.



7
8
9
10
11
12
# File 'lib/bancor.rb', line 7

def initialize(eth:, rate:, crr:)
  @total_supply = eth * rate
  @reserved_token = @total_supply * crr
  @price = @reserved_token / (@total_supply * crr)
  @crr = crr
end

Instance Attribute Details

#priceObject (readonly)

Returns the value of attribute price.



5
6
7
# File 'lib/bancor.rb', line 5

def price
  @price
end

#reserved_tokenObject (readonly)

Returns the value of attribute reserved_token.



5
6
7
# File 'lib/bancor.rb', line 5

def reserved_token
  @reserved_token
end

#total_supplyObject (readonly)

Returns the value of attribute total_supply.



5
6
7
# File 'lib/bancor.rb', line 5

def total_supply
  @total_supply
end

Instance Method Details

#buy(quantity) ⇒ Object



14
15
16
17
18
19
# File 'lib/bancor.rb', line 14

def buy(quantity)
  token = @total_supply * (((1 + (quantity / @reserved_token)) ** @crr) - 1)
  @reserved_token = @reserved_token + quantity
  @total_supply = @total_supply + token
  @price = @reserved_token / (@total_supply * @crr)
end

#sell(quantity) ⇒ Object



21
22
23
24
25
26
# File 'lib/bancor.rb', line 21

def sell(quantity)
  token = @reserved_token * (1 - ((1 - (quantity / @total_supply)) ** (1/@crr)))
  @reserved_token = @reserved_token - token
  @total_supply = @total_supply - quantity
  @price = @reserved_token / (@total_supply * @crr)
end