-
Notifications
You must be signed in to change notification settings - Fork 24
/
l0k_factory.cairo
176 lines (144 loc) · 4.01 KB
/
l0k_factory.cairo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
174
175
176
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.bool import FALSE
from starkware.cairo.common.math_cmp import is_le_felt
from starkware.cairo.common.math import assert_nn, assert_not_equal, assert_not_zero
from starkware.starknet.common.syscalls import get_caller_address, deploy
from starkware.cairo.common.hash import hash2
from interfaces.Il0kPair import Il0kPair
from libraries.l0k_library import l0kLibrary
#
# Events
#
@event
func PairCreated(token0 : felt, token1 : felt, pair : felt, index : felt):
end
#
# Storage
#
@storage_var
func _pairClass() -> (pairClass : felt):
end
@storage_var
func _feeTo() -> (feeTo : felt):
end
@storage_var
func _feeToSetter() -> (feeToSetter : felt):
end
@storage_var
func _getPair(token0 : felt, token1 : felt) -> (pair : felt):
end
@storage_var
func _allPairs(index : felt) -> (pair : felt):
end
@storage_var
func _allPairsLength() -> (length : felt):
end
#
# Constructor
#
@constructor
func constructor{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
pairClass : felt, feeToSetter : felt
):
_pairClass.write(pairClass)
_feeToSetter.write(feeToSetter)
return ()
end
#
# Getters
#
@view
func feeTo{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (feeTo : felt):
let (value) = _feeTo.read()
return (feeTo=value)
end
@view
func feeToSetter{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (
feeToSetter : felt
):
let (value) = _feeToSetter.read()
return (feeToSetter=value)
end
@view
func getPair{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
token0 : felt, token1 : felt
) -> (pair : felt):
let (value) = _getPair.read(token0, token1)
return (pair=value)
end
@view
func allPairs{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(index : felt) -> (
pair : felt
):
let (value) = _allPairs.read(index)
return (pair=value)
end
@view
func allPairsLength{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (
length : felt
):
let (value) = _allPairsLength.read()
return (length=value)
end
#
# Externals
#
@external
func createPair{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
tokenA : felt, tokenB : felt
) -> (pair : felt):
alloc_locals
let (token0, token1) = l0kLibrary.sortTokens(tokenA, tokenB)
# Pair exists
with_attr error_message("10kSwap: PE"):
let (pair) = _getPair.read(token0, token1)
assert pair = 0
end
let (pairClass) = _pairClass.read()
let (salt) = hash2{hash_ptr=pedersen_ptr}(token0, token1)
# https://www.cairo-lang.org/docs/hello_starknet/deploying_from_contracts.html
let (newPair) = deploy(
class_hash=pairClass,
contract_address_salt=salt,
constructor_calldata_size=0,
constructor_calldata=cast(new (), felt*),
deploy_from_zero=FALSE,
)
Il0kPair.initialize(contract_address=newPair, token0=token0, token1=token1)
_getPair.write(token0, token1, newPair)
_getPair.write(token1, token0, newPair)
let (length) = _allPairsLength.read()
_allPairs.write(length, newPair)
let newLength = length + 1
_allPairsLength.write(newLength)
PairCreated.emit(token0, token1, newPair, newLength)
return (newPair)
end
@external
func setFeeTo{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(feeTo : felt) -> (
):
_onlyFeeToSetter()
_feeTo.write(feeTo)
return ()
end
@external
func setFeeToSetter{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
feeToSetter : felt
) -> ():
_onlyFeeToSetter()
_feeToSetter.write(feeToSetter)
return ()
end
#
# Internal
#
func _onlyFeeToSetter{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}():
let (caller) = get_caller_address()
let (feeToSetter) = _feeToSetter.read()
# Forbidden
with_attr error_message("10kSwap: FB"):
assert feeToSetter = caller
end
return ()
end