uniswap 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06b64f10df9f1cb893dbf1e22e0c7fbbbc1a3b077c5f362d2c385ec46e321f16
4
- data.tar.gz: edb9d0db8f5edd36a5ac62ff456483f6dad7e480ee7a911377bf48c9d83c12da
3
+ metadata.gz: e7d8b1c3465c77d203dd9115bc85450186737da44459e465a822e275f48eb763
4
+ data.tar.gz: df43be0573452e9a5494fb4b4bec31c3a2d3df87d47fd2d1126d0d2f68a211f2
5
5
  SHA512:
6
- metadata.gz: 7264444379e503aa697de99c6fbd16d39fbcce95411d91a7a4ca053dd761886dc2fcddf9c2efa9fe4678b585dc248b4f0d54dd8eb8910d2fa4b62a46bc257560
7
- data.tar.gz: 6b0f03feb962ad4881c5393239ab7b41f55715e7afe2160e2fcb7c38719b58d6aae903d1c821de8bb2cb3c2135bf4a474ff2c883f7aad4e1f3d3e9ca3602a7e3
6
+ metadata.gz: 543cba8830a88c50fbc13b12a9ca836716f24aa16ab4a5388225faa35a235e44689e36ab0fad8184a8b85ce9c14e360c1c17e0443f24e7d6243d437e4d602b1e
7
+ data.tar.gz: 36d2dfc428ca42558eb60c7664abe94c81265eaa4f977f75f40ea66fd2d00bc36b7e20dd8f8326cb02ad607ca22b22bb3090550692ecbb0e40eb5d01f2f66826
data/CHANGELOG.md CHANGED
@@ -1,3 +1,4 @@
1
+ ### 0.1.0
1
2
  ### 0.0.1 / 2023-11-04
2
3
 
3
4
  * Everything is new. First release
data/README.md CHANGED
@@ -35,8 +35,10 @@ See the [**Facetswap App / Service**](https://facetswap.com)
35
35
  Available contracts include:
36
36
 
37
37
  - [ERC20](lib/uniswap/ERC20.rb)
38
+ - [PublicMintERC20 (is ERC20)](lib/uniswap/PublicMintERC20.rb)
38
39
  - [UniswapV2ERC20 (is ERC20)](lib/uniswap/UniswapV2ERC20.rb)
39
40
  - [UniswapV2Pair (is UniswapV2ERC20)](lib/uniswap/UniswapV2Pair.rb)
41
+ - [UniswapV2Factory](lib/uniswap/UniswapV2Factory.rb)
40
42
  - ...
41
43
 
42
44
  And so on. To be continued ...
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require 'hoe'
3
3
 
4
4
 
5
5
  Hoe.spec 'uniswap' do
6
- self.version = '0.0.1' # Rubidity::Module::Uniswap::VERSION
6
+ self.version = '0.1.0' # Rubidity::Module::Uniswap::VERSION
7
7
 
8
8
  self.summary = 'uniswap - core uniswap v2 (dumb) contracts for ruby (rubidity) for layer 1 (l1) with "off-chain" indexer'
9
9
  self.description = summary
@@ -1,39 +1,37 @@
1
- pragma :rubidity, "1.0.0"
2
1
 
3
- import 'ERC20'
2
+ class PublicMintERC20 < ERC20
4
3
 
5
- contract :PublicMintERC20, is: :ERC20 do
6
- uint256 :public, :maxSupply
7
- uint256 :public, :perMintLimit
4
+ storage maxSupply: UInt,
5
+ perMintLimit: UInt
8
6
 
9
- constructor(
10
- name: :string,
11
- symbol: :string,
12
- maxSupply: :uint256,
13
- perMintLimit: :uint256,
14
- decimals: :uint8
15
- ) do
16
- super(name: name, symbol: symbol, decimals: decimals)
17
- s.maxSupply = maxSupply
18
- s.perMintLimit = perMintLimit
7
+ sig [String, String, UInt, UInt, UInt]
8
+ def constructor(
9
+ name:,
10
+ symbol:,
11
+ maxSupply:,
12
+ perMintLimit:,
13
+ decimals:
14
+ )
15
+ super( name: name, symbol: symbol, decimals: decimals )
16
+ @maxSupply = maxSupply
17
+ @perMintLimit = perMintLimit
19
18
  end
20
19
 
21
-
22
- function :mint, { amount: :uint256 }, :public do
23
- require(amount > 0, 'Amount must be positive')
24
- require(amount <= s.perMintLimit, 'Exceeded mint limit')
25
-
26
- require(s.totalSupply + amount <= s.maxSupply, 'Exceeded max supply')
20
+ sig [UInt]
21
+ def mint( amount: )
22
+ assert amount > 0, 'Amount must be positive'
23
+ assert amount <= @perMintLimit, 'Exceeded mint limit'
24
+ assert @totalSupply + amount <= @maxSupply, 'Exceeded max supply'
27
25
 
28
- _mint(to: msg.sender, amount: amount)
26
+ _mint( to: msg.sender, amount: amount )
29
27
  end
30
28
 
31
- function :airdrop, { to: :address, amount: :uint256 }, :public do
32
- require(amount > 0, 'Amount must be positive')
33
- require(amount <= s.perMintLimit, 'Exceeded mint limit')
34
-
35
- require(s.totalSupply + amount <= s.maxSupply, 'Exceeded max supply')
29
+ sig [Address, UInt]
30
+ def airdrop( to:, amount: )
31
+ assert amount > 0, 'Amount must be positive'
32
+ assert amount <= @perMintLimit, 'Exceeded mint limit'
33
+ assert @totalSupply + amount <= @maxSupply, 'Exceeded max supply'
36
34
 
37
- _mint(to: to, amount: amount)
35
+ _mint( to: to, amount: amount )
38
36
  end
39
- end
37
+ end # class PublicMintERC20
@@ -1,36 +1,39 @@
1
- pragma :rubidity, "1.0.0"
2
1
 
3
- import 'UniswapV2Pair'
2
+ class UniswapV2Factory < Contract
4
3
 
4
+ event :PairCreated, token0: Address,
5
+ token1: Address,
6
+ pair: Address,
7
+ pairLength: UInt
5
8
 
6
- contract :UniswapV2Factory do
7
- address :public, :feeTo
8
- address :public, :feeToSetter
9
+ storage feeTo: Address,
10
+ feeToSetter: Address,
11
+ getPair: mapping( Address, mapping( Address, Address)),
12
+ allPairs: array( Address )
9
13
 
10
- mapping ({ address: mapping({ address: :address })}), :public, :getPair
11
- array :address, :public, :allPairs
12
-
13
- event :PairCreated, { token0: :address, token1: :address, pair: :address, pairLength: :uint256 }
14
-
15
- constructor(_feeToSetter: :address) do
16
- s.feeToSetter = _feeToSetter
14
+ sig [Address]
15
+ def constructor( feeToSetter: )
16
+ @feeToSetter = feeToSetter
17
17
  end
18
18
 
19
- function :allPairsLength, {}, :public, :view, returns: :uint256 do
20
- return s.allPairs.length
19
+ sig [], :view, returns: UInt
20
+ def allPairsLength
21
+ @allPairs.length
21
22
  end
22
-
23
- function :getAllPairs, {}, :public, :view, returns: [:address] do
24
- return s.allPairs
23
+
24
+ sig [], :view, returns: array(Address)
25
+ def getAllPairs
26
+ @allPairs
25
27
  end
26
28
 
27
- function :createPair, { tokenA: :address, tokenB: :address }, :public, returns: :address do
28
- require(tokenA != tokenB, 'Scribeswap: IDENTICAL_ADDRESSES')
29
+ sig [Address, Address], returns: Address
30
+ def createPair( tokenA:, tokenB: )
31
+ assert tokenA != tokenB, 'Scribeswap: IDENTICAL_ADDRESSES'
29
32
 
30
- token0, token1 = uint256( tokenA ) < uint256( tokenB ) ? [tokenA, tokenB] : [tokenB, tokenA]
33
+ token0, token1 = uint( tokenA ) < uint( tokenB ) ? [tokenA, tokenB] : [tokenB, tokenA]
31
34
 
32
- require(token0 != address(0), "Scribeswap: ZERO_ADDRESS");
33
- require(s.getPair[token0][token1] == address(0), "Scribeswap: PAIR_EXISTS");
35
+ assert token0 != address(0), "Scribeswap: ZERO_ADDRESS"
36
+ assert @getPair[token0][token1] == address(0), "Scribeswap: PAIR_EXISTS"
34
37
 
35
38
  =begin
36
39
  salt = keccak256(abi.encodePacked(token0, token1))
@@ -52,30 +55,26 @@ contract :UniswapV2Factory do
52
55
  pair = UniswapV2Pair.construct
53
56
  pair.init( token0, token1 )
54
57
 
55
- s.getPair[token0][token1] = pair.__address__
56
- s.getPair[token1][token0] = pair.__address__
58
+ @getPair[token0][token1] = pair.__address__
59
+ @getPair[token1][token0] = pair.__address__
57
60
 
58
- s.allPairs.push( pair.__address__ )
59
- emit :PairCreated, token0: token0, token1: token1, pair: pair.__address__, pairLength: s.allPairs.length
61
+ @allPairs.push( pair.__address__ )
62
+ log PairCreated, token0: token0, token1: token1, pair: pair.__address__, pairLength: @allPairs.length
60
63
 
61
- return pair.__address__
64
+ pair.__address__
62
65
  end
63
-
64
66
 
65
-
66
- function :setFeeTo, { _feeTo: :address }, :public do
67
- require(msg.sender == feeToSetter, "Scribeswap: FORBIDDEN")
67
+ sig [Address]
68
+ def setFeeTo( feeTo: )
69
+ assert msg.sender == @feeToSetter, "Scribeswap: FORBIDDEN"
68
70
 
69
- s.feeTo = _feeTo
70
-
71
- return nil
71
+ @feeTo = feeTo
72
72
  end
73
73
 
74
- function :setFeeToSetter, { _feeToSetter: :address }, :public do
75
- require(msg.sender == feeToSetter, "Scribeswap: FORBIDDEN")
76
-
77
- s.feeToSetter = _feeToSetter
74
+ sig [Address]
75
+ def setFeeToSetter( feeToSetter: )
76
+ assert msg.sender == @feeToSetter, "Scribeswap: FORBIDDEN"
78
77
 
79
- return nil
78
+ @feeToSetter = feeToSetter
80
79
  end
81
- end
80
+ end # class UniswapV2Factory
@@ -50,8 +50,8 @@ class UniswapV2Pair < UniswapV2ERC20
50
50
  sig []
51
51
  def constructor
52
52
  super
53
- @factory = msg.sender
54
- @unlocked = uint256( 1 )
53
+ @factory = msg.sender
54
+ @_unlocked = uint( 1 )
55
55
  end
56
56
 
57
57
  # note: can't name method initialize because of ruby
@@ -77,109 +77,107 @@ class UniswapV2Pair < UniswapV2ERC20
77
77
  assert result, "ScribeSwap: TRANSFER_FAILED"
78
78
  end
79
79
 
80
- end # class UniswapV2Pair
81
-
82
- __END__
83
80
 
84
-
85
- function :_update, {
86
- balance0: :uint256,
87
- balance1: :uint256,
88
- _reserve0: :uint112,
89
- _reserve1: :uint112
90
- }, :private do
91
- require(balance0 <= (2 ** 112 - 1) && balance1 <= (2 ** 112 - 1), 'ScribeSwap: OVERFLOW')
81
+ sig [UInt, UInt, UInt, UInt]
82
+ def _update(
83
+ balance0:,
84
+ balance1:,
85
+ reserve0:,
86
+ reserve1: )
87
+ assert balance0 <= (2 ** 112 - 1) && balance1 <= (2 ** 112 - 1), 'ScribeSwap: OVERFLOW'
92
88
 
93
89
  # blockTimestamp = uint32(block.timestamp % 2 ** 32)
94
- blockTimestamp = block.timestamp
95
- timeElapsed = blockTimestamp - s.blockTimestampLast # overflow is desired
90
+ # overflow is desired - why??
91
+ timeElapsed = block.timestamp - @_blockTimestampLast
96
92
 
97
- if timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0
93
+ if timeElapsed > 0 && reserve0 != 0 && reserve1 != 0
98
94
  # * never overflows, and + overflow is desired
99
- s.price0CumulativeLast += uint256(uqdiv(encode(_reserve1), _reserve0)) * timeElapsed
100
- s.price1CumulativeLast += uint256(uqdiv(encode(_reserve0), _reserve1)) * timeElapsed
95
+ # up reserve1 from 112 to 224 bit (shift by 112bit)
96
+ # up reserve2 from 112 to 224 bit (shift by 112bit)
97
+ @price0CumulativeLast += (reserve1*(2 ** 112)) / reserve0 * timeElapsed
98
+ @price1CumulativeLast += (reserve0*(2 ** 112)) / reserve1 * timeElapsed
101
99
  end
102
100
 
103
- emit :PreSwapReserves, reserve0: s.reserve0, reserve1: s.reserve1
101
+ log PreSwapReserves, reserve0: @_reserve0, reserve1: @_reserve1
104
102
 
105
- s.reserve0 = uint112(balance0)
106
- s.reserve1 = uint112(balance1)
103
+ @_reserve0 = balance0 ## was uint112()
104
+ @_reserve1 = balance1
107
105
 
108
- s.blockTimestampLast = blockTimestamp
109
- emit :Sync, reserve0: s.reserve0, reserve1: s.reserve1
110
- end
111
-
112
- function :encode, { y: :uint112 }, :internal, :pure, returns: :uint224 do
113
- return uint224(y) * (2 ** 112)
114
- end
115
-
116
- function :uqdiv, { x: :uint224, y: :uint112 }, :internal, :pure, returns: :uint224 do
117
- return x / uint224(y)
106
+ @_blockTimestampLast = block.timestamp
107
+ log Sync, reserve0: @_reserve0, reserve1: @_reserve1
118
108
  end
119
109
 
120
- function :_mintFee, { _reserve0: :uint112, _reserve1: :uint112 }, :private, returns: :bool do
121
- feeTo = UniswapV2Factory.at( s.factory ).feeTo
110
+
111
+ sig [UInt, UInt], returns: Bool ## was uint112, uint112
112
+ def _mintFee( reserve0:, reserve1: )
113
+ feeTo = UniswapV2Factory.at( @factory ).feeTo
122
114
  feeOn = feeTo != address(0)
123
- _kLast = s.kLast
115
+ kLast = @kLast
124
116
 
125
-
126
117
  if feeOn
127
- if _kLast != 0
118
+ if kLast != 0
128
119
  ## note: sqrt NOT built-in - double check
129
- rootK = Integer.sqrt(_reserve0 * _reserve1)
130
- rootKLast = Integer.sqrt(_kLast)
120
+ rootK = Integer.sqrt( reserve0 * reserve1 )
121
+ rootKLast = Integer.sqrt( kLast )
131
122
  if rootK > rootKLast
132
- numerator = totalSupply * (rootK - rootKLast)
123
+ numerator = @totalSupply * (rootK - rootKLast)
133
124
  denominator = rootK * 5 + rootKLast
134
125
  liquidity = numerator.div(denominator)
135
- _mint(feeTo, liquidity) if liquidity > 0
126
+ _mint( feeTo, liquidity ) if liquidity > 0
136
127
  end
137
128
  end
138
- elsif _kLast != 0
139
- s.kLast = 0
129
+ elsif kLast != 0
130
+ @kLast = 0
140
131
  end
141
132
  feeOn
142
133
  end
143
134
 
144
- function :mint, { to: :address }, :public, returns: :uint256 do
145
- require(s.unlocked == 1, 'ScribeSwap: LOCKED')
135
+ sig [Address], returns: UInt
136
+ def mint( to: )
137
+ assert @_unlocked == 1, 'ScribeSwap: LOCKED'
146
138
 
147
- _reserve0, _reserve1, _ = getReserves
139
+ reserve0, reserve1, _ = getReserves
148
140
 
149
- balance0 = ERC20.at(s.token0).balanceOf( __address__ ) ## address(this)
150
- balance1 = ERC20.at(s.token1).balanceOf( __address__ ) ## address(this)
141
+ balance0 = ERC20.at( @token0 ).balanceOf( __address__ ) ## address(this)
142
+ balance1 = ERC20.at( @token1 ).balanceOf( __address__ ) ## address(this)
151
143
 
152
- amount0 = balance0 - _reserve0
153
- amount1 = balance1 - _reserve1
144
+ amount0 = balance0 - reserve0
145
+ amount1 = balance1 - reserve1
154
146
 
155
- feeOn = _mintFee(_reserve0, _reserve1)
156
- _totalSupply = s.totalSupply
147
+ feeOn = _mintFee( reserve0, reserve1)
148
+ totalSupply = @totalSupply
157
149
 
158
150
 
159
-
160
- if _totalSupply == 0
151
+ if totalSupply == 0
161
152
  ## note: sqrt NOT built-in - double check
162
- liquidity = uint256( Integer.sqrt(amount0 * amount1)) - s.MINIMUM_LIQUIDITY
163
- _mint(address(0), s.MINIMUM_LIQUIDITY)
153
+ ## move "upstream" into contract base (for all) - why? why not?
154
+ liquidity = uint( Integer.sqrt(amount0 * amount1)) - MINIMUM_LIQUIDITY
155
+ _mint( address(0), MINIMUM_LIQUIDITY )
164
156
  else
165
157
  liquidity = [
166
- (amount0 * _totalSupply).div(_reserve0),
167
- (amount1 * _totalSupply).div(_reserve1)
158
+ (amount0 * totalSupply).div(reserve0),
159
+ (amount1 * totalSupply).div(reserve1)
168
160
  ].min
169
161
  end
170
162
 
171
- require(liquidity > 0, 'ScribeSwap: INSUFFICIENT_LIQUIDITY_MINTED')
172
- _mint(to, liquidity)
163
+ assert liquidity > 0, 'ScribeSwap: INSUFFICIENT_LIQUIDITY_MINTED'
164
+ _mint( to, liquidity )
173
165
 
174
166
 
175
- _update(balance0, balance1, _reserve0, _reserve1)
176
- s.kLast = s.reserve0 * s.reserve1 if feeOn
167
+ _update( balance0, balance1, reserve0, reserve1 )
168
+ @kLast = @_reserve0 * @_reserve1 if feeOn
177
169
 
178
- emit :Mint, sender: msg.sender, amount0: amount0, amount1: amount1
170
+ log Mint, sender: msg.sender, amount0: amount0, amount1: amount1
179
171
 
180
- return liquidity
172
+ liquidity
181
173
  end
174
+ end # class UniswapV2Pair
175
+
176
+ __END__
177
+
182
178
 
179
+
180
+
183
181
  function :burn, { to: :address }, :external, :lock, returns: { amount0: :uint256, amount1: :uint256 } do
184
182
  require(s.unlocked == 1, 'ScribeSwap: LOCKED')
185
183
 
data/lib/uniswap.rb CHANGED
@@ -5,7 +5,10 @@ require 'rubidity'
5
5
 
6
6
  ## our own code
7
7
  require_relative 'uniswap/ERC20'
8
+ require_relative 'uniswap/PublicMintERC20'
8
9
  require_relative 'uniswap/UniswapV2ERC20'
9
10
  require_relative 'uniswap/UniswapV2Pair'
11
+ require_relative 'uniswap/UniswapV2Factory'
12
+
10
13
 
11
14
  puts "bye"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uniswap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer