Class: UniswapV2Pair

Inherits:
UniswapV2ERC20 show all
Defined in:
lib/uniswap/UniswapV2Pair.rb

Overview

contract :IUniswapV2Factory, abstract: true do

function :feeTo, {}, :external, :view, returns: :address do
end

end

Constant Summary collapse

MINIMUM_LIQUIDITY =

> 1000

10 ** 3

Instance Method Summary collapse

Methods inherited from ERC20

#_burn, #_mint, #approve, #transfer, #transferFrom

Instance Method Details

#_mintFee(reserve0:, reserve1:) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/uniswap/UniswapV2Pair.rb', line 112

def _mintFee( reserve0:, reserve1: )
  feeTo = UniswapV2Factory.at( @factory ).feeTo
  feeOn = feeTo != address(0)
  kLast = @kLast
  
  if feeOn
    if kLast != 0
      ## note: sqrt NOT built-in - double check
      rootK = Integer.sqrt( reserve0 * reserve1 )
      rootKLast = Integer.sqrt( kLast )
      if rootK > rootKLast
        numerator = @totalSupply * (rootK - rootKLast)
        denominator = rootK * 5 + rootKLast
        liquidity = numerator.div(denominator)
        _mint( feeTo, liquidity )   if liquidity > 0
      end
    end
  elsif kLast != 0
    @kLast = 0
  end
  feeOn
end

#_safeTransfer(token:, to:, value:) ⇒ Object



74
75
76
77
78
# File 'lib/uniswap/UniswapV2Pair.rb', line 74

def _safeTransfer( token:, to:, value:)
  result = ERC20.at( token ).transfer(to: to, amount: value)
  
  assert result, "ScribeSwap: TRANSFER_FAILED"
end

#_update(balance0:, balance1:, reserve0:, reserve1:) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/uniswap/UniswapV2Pair.rb', line 82

def _update(
  balance0:,
  balance1:,
  reserve0:,
  reserve1: )
  assert balance0 <= (2 ** 112 - 1) && balance1 <= (2 ** 112 - 1), 'ScribeSwap: OVERFLOW'
  
  # blockTimestamp = uint32(block.timestamp % 2 ** 32)
  # overflow is desired  - why??
  timeElapsed = block.timestamp - @_blockTimestampLast   
  
  if timeElapsed > 0 && reserve0 != 0 && reserve1 != 0
    # * never overflows, and + overflow is desired
    #   up reserve1 from 112 to 224 bit (shift by 112bit)
    #   up reserve2 from 112 to 224 bit (shift by 112bit)
    @price0CumulativeLast +=  (reserve1*(2 ** 112)) / reserve0  * timeElapsed
    @price1CumulativeLast +=  (reserve0*(2 ** 112)) / reserve1  * timeElapsed
  end
  
  log PreSwapReserves, reserve0: @_reserve0, reserve1: @_reserve1
  
  @_reserve0 = balance0   ## was uint112()
  @_reserve1 = balance1
  
  @_blockTimestampLast = block.timestamp
  log Sync, reserve0: @_reserve0, reserve1: @_reserve1
end

#constructorObject



51
52
53
54
55
# File 'lib/uniswap/UniswapV2Pair.rb', line 51

def constructor
  super
  @factory   = msg.sender
  @_unlocked = uint( 1 )
end

#getReservesObject



67
68
69
70
71
# File 'lib/uniswap/UniswapV2Pair.rb', line 67

def getReserves
  return [@_reserve0,
          @_reserve1,
          @_blockTimestampLast]
end

#init(token0:, token1:) ⇒ Object



59
60
61
62
63
64
# File 'lib/uniswap/UniswapV2Pair.rb', line 59

def init( token0:, token1: )
  assert msg.sender == @factory, 'ScribeSwap: FORBIDDEN'

  @token0 = token0
  @token1 = token1    
end

#mint(to:) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/uniswap/UniswapV2Pair.rb', line 136

def mint( to: )
  assert @_unlocked == 1, 'ScribeSwap: LOCKED'
  
  reserve0, reserve1, _ = getReserves
  
  balance0 = ERC20.at( @token0 ).balanceOf( __address__ ) ## address(this)
  balance1 = ERC20.at( @token1 ).balanceOf( __address__ ) ## address(this)
 
  amount0 = balance0 - reserve0
  amount1 = balance1 - reserve1
  
  feeOn = _mintFee( reserve0, reserve1)
  totalSupply = @totalSupply

 
  if totalSupply == 0
    ## note: sqrt NOT built-in - double check
    ##  move "upstream" into contract base (for all) - why? why not?
    liquidity = uint( Integer.sqrt(amount0 * amount1)) - MINIMUM_LIQUIDITY
    _mint( address(0), MINIMUM_LIQUIDITY )
  else
    liquidity = [
      (amount0 * totalSupply).div(reserve0),
      (amount1 * totalSupply).div(reserve1)
    ].min
  end

  assert liquidity > 0, 'ScribeSwap: INSUFFICIENT_LIQUIDITY_MINTED'
  _mint( to, liquidity )
 

  _update( balance0, balance1, reserve0, reserve1 )
  @kLast = @_reserve0 * @_reserve1    if feeOn
  
  log Mint, sender: msg.sender, amount0: amount0, amount1: amount1
  
  liquidity
end