Class: Bancor::Protocol
- Inherits:
-
Object
- Object
- Bancor::Protocol
- Defined in:
- lib/bancor.rb
Instance Attribute Summary collapse
-
#price ⇒ Object
readonly
Returns the value of attribute price.
-
#reserved_token ⇒ Object
readonly
Returns the value of attribute reserved_token.
-
#total_supply ⇒ Object
readonly
Returns the value of attribute total_supply.
Instance Method Summary collapse
- #buy(quantity) ⇒ Object
-
#initialize(eth:, rate:, crr:) ⇒ Protocol
constructor
A new instance of Protocol.
- #sell(quantity) ⇒ Object
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
#price ⇒ Object (readonly)
Returns the value of attribute price.
5 6 7 |
# File 'lib/bancor.rb', line 5 def price @price end |
#reserved_token ⇒ Object (readonly)
Returns the value of attribute reserved_token.
5 6 7 |
# File 'lib/bancor.rb', line 5 def reserved_token @reserved_token end |
#total_supply ⇒ Object (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 |