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 +4 -4
- data/CHANGELOG.md +1 -0
- data/README.md +2 -0
- data/Rakefile +1 -1
- data/lib/uniswap/PublicMintERC20.rb +27 -29
- data/lib/uniswap/UniswapV2Factory.rb +39 -40
- data/lib/uniswap/UniswapV2Pair.rb +61 -63
- data/lib/uniswap.rb +3 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7d8b1c3465c77d203dd9115bc85450186737da44459e465a822e275f48eb763
|
4
|
+
data.tar.gz: df43be0573452e9a5494fb4b4bec31c3a2d3df87d47fd2d1126d0d2f68a211f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 543cba8830a88c50fbc13b12a9ca836716f24aa16ab4a5388225faa35a235e44689e36ab0fad8184a8b85ce9c14e360c1c17e0443f24e7d6243d437e4d602b1e
|
7
|
+
data.tar.gz: 36d2dfc428ca42558eb60c7664abe94c81265eaa4f977f75f40ea66fd2d00bc36b7e20dd8f8326cb02ad607ca22b22bb3090550692ecbb0e40eb5d01f2f66826
|
data/CHANGELOG.md
CHANGED
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
|
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
|
-
|
2
|
+
class PublicMintERC20 < ERC20
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
uint256 :public, :perMintLimit
|
4
|
+
storage maxSupply: UInt,
|
5
|
+
perMintLimit: UInt
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
2
|
+
class UniswapV2Factory < Contract
|
4
3
|
|
4
|
+
event :PairCreated, token0: Address,
|
5
|
+
token1: Address,
|
6
|
+
pair: Address,
|
7
|
+
pairLength: UInt
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
+
storage feeTo: Address,
|
10
|
+
feeToSetter: Address,
|
11
|
+
getPair: mapping( Address, mapping( Address, Address)),
|
12
|
+
allPairs: array( Address )
|
9
13
|
|
10
|
-
|
11
|
-
|
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
|
-
|
20
|
-
|
19
|
+
sig [], :view, returns: UInt
|
20
|
+
def allPairsLength
|
21
|
+
@allPairs.length
|
21
22
|
end
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
|
24
|
+
sig [], :view, returns: array(Address)
|
25
|
+
def getAllPairs
|
26
|
+
@allPairs
|
25
27
|
end
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
+
sig [Address, Address], returns: Address
|
30
|
+
def createPair( tokenA:, tokenB: )
|
31
|
+
assert tokenA != tokenB, 'Scribeswap: IDENTICAL_ADDRESSES'
|
29
32
|
|
30
|
-
token0, token1 =
|
33
|
+
token0, token1 = uint( tokenA ) < uint( tokenB ) ? [tokenA, tokenB] : [tokenB, tokenA]
|
31
34
|
|
32
|
-
|
33
|
-
|
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
|
-
|
56
|
-
|
58
|
+
@getPair[token0][token1] = pair.__address__
|
59
|
+
@getPair[token1][token0] = pair.__address__
|
57
60
|
|
58
|
-
|
59
|
-
|
61
|
+
@allPairs.push( pair.__address__ )
|
62
|
+
log PairCreated, token0: token0, token1: token1, pair: pair.__address__, pairLength: @allPairs.length
|
60
63
|
|
61
|
-
|
64
|
+
pair.__address__
|
62
65
|
end
|
63
|
-
|
64
66
|
|
65
|
-
|
66
|
-
|
67
|
-
|
67
|
+
sig [Address]
|
68
|
+
def setFeeTo( feeTo: )
|
69
|
+
assert msg.sender == @feeToSetter, "Scribeswap: FORBIDDEN"
|
68
70
|
|
69
|
-
|
70
|
-
|
71
|
-
return nil
|
71
|
+
@feeTo = feeTo
|
72
72
|
end
|
73
73
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
s.feeToSetter = _feeToSetter
|
74
|
+
sig [Address]
|
75
|
+
def setFeeToSetter( feeToSetter: )
|
76
|
+
assert msg.sender == @feeToSetter, "Scribeswap: FORBIDDEN"
|
78
77
|
|
79
|
-
|
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
|
54
|
-
@
|
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
|
-
|
86
|
-
balance0
|
87
|
-
balance1
|
88
|
-
|
89
|
-
|
90
|
-
|
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
|
-
|
95
|
-
timeElapsed =
|
90
|
+
# overflow is desired - why??
|
91
|
+
timeElapsed = block.timestamp - @_blockTimestampLast
|
96
92
|
|
97
|
-
if timeElapsed > 0 &&
|
93
|
+
if timeElapsed > 0 && reserve0 != 0 && reserve1 != 0
|
98
94
|
# * never overflows, and + overflow is desired
|
99
|
-
|
100
|
-
|
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
|
-
|
101
|
+
log PreSwapReserves, reserve0: @_reserve0, reserve1: @_reserve1
|
104
102
|
|
105
|
-
|
106
|
-
|
103
|
+
@_reserve0 = balance0 ## was uint112()
|
104
|
+
@_reserve1 = balance1
|
107
105
|
|
108
|
-
|
109
|
-
|
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
|
-
|
121
|
-
|
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
|
-
|
115
|
+
kLast = @kLast
|
124
116
|
|
125
|
-
|
126
117
|
if feeOn
|
127
|
-
if
|
118
|
+
if kLast != 0
|
128
119
|
## note: sqrt NOT built-in - double check
|
129
|
-
rootK = Integer.sqrt(
|
130
|
-
rootKLast = Integer.sqrt(
|
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
|
139
|
-
|
129
|
+
elsif kLast != 0
|
130
|
+
@kLast = 0
|
140
131
|
end
|
141
132
|
feeOn
|
142
133
|
end
|
143
134
|
|
144
|
-
|
145
|
-
|
135
|
+
sig [Address], returns: UInt
|
136
|
+
def mint( to: )
|
137
|
+
assert @_unlocked == 1, 'ScribeSwap: LOCKED'
|
146
138
|
|
147
|
-
|
139
|
+
reserve0, reserve1, _ = getReserves
|
148
140
|
|
149
|
-
balance0 = ERC20.at(
|
150
|
-
balance1 = ERC20.at(
|
141
|
+
balance0 = ERC20.at( @token0 ).balanceOf( __address__ ) ## address(this)
|
142
|
+
balance1 = ERC20.at( @token1 ).balanceOf( __address__ ) ## address(this)
|
151
143
|
|
152
|
-
amount0 = balance0 -
|
153
|
-
amount1 = balance1 -
|
144
|
+
amount0 = balance0 - reserve0
|
145
|
+
amount1 = balance1 - reserve1
|
154
146
|
|
155
|
-
feeOn = _mintFee(
|
156
|
-
|
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
|
-
|
163
|
-
|
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 *
|
167
|
-
(amount1 *
|
158
|
+
(amount0 * totalSupply).div(reserve0),
|
159
|
+
(amount1 * totalSupply).div(reserve1)
|
168
160
|
].min
|
169
161
|
end
|
170
162
|
|
171
|
-
|
172
|
-
_mint(to, liquidity)
|
163
|
+
assert liquidity > 0, 'ScribeSwap: INSUFFICIENT_LIQUIDITY_MINTED'
|
164
|
+
_mint( to, liquidity )
|
173
165
|
|
174
166
|
|
175
|
-
_update(balance0, balance1,
|
176
|
-
|
167
|
+
_update( balance0, balance1, reserve0, reserve1 )
|
168
|
+
@kLast = @_reserve0 * @_reserve1 if feeOn
|
177
169
|
|
178
|
-
|
170
|
+
log Mint, sender: msg.sender, amount0: amount0, amount1: amount1
|
179
171
|
|
180
|
-
|
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"
|