TTDex
The contract manages Token/Token pairs and fully implements TZIP-12 so that pool shares are FA2 assets. The contract manages pools for FA1.2/FA1.2, FA2/FA2 and FA2/FA1.2 tokens. The only instance of the pool for the same pair exists.
The pair is always ordered. The token with the lower address must be provided first. If in the case of FA2 tokens the addresses match the asset with the lowest token id goes first.

Code

quipuswap-core/TTDex.ligo at master · madfish-solutions/quipuswap-core
GitHub

State-Changing Functions

setDexFunction(func, index)

1
(pair %setDexFunction
2
(lambda %func
3
(pair
4
(pair
5
(or
6
(or
7
(pair %addPair
8
(pair %pair
9
(pair
10
(pair (address %token_a_address) (nat %token_a_id))
11
(pair (or %token_a_type (unit %fa12) (unit %fa2))
12
(address %token_b_address)))
13
(pair (nat %token_b_id)
14
(or %token_b_type (unit %fa12) (unit %fa2))))
15
(pair (nat %token_a_in) (nat %token_b_in)))
16
(pair %divest
17
(pair %pair
18
(pair
19
(pair (address %token_a_address) (nat %token_a_id))
20
(pair (or %token_a_type (unit %fa12) (unit %fa2))
21
(address %token_b_address)))
22
(pair (nat %token_b_id)
23
(or %token_b_type (unit %fa12) (unit %fa2))))
24
(pair (nat %min_token_a_out)
25
(pair (nat %min_token_b_out) (nat %shares)))))
26
(or
27
(pair %invest
28
(pair %pair
29
(pair
30
(pair (address %token_a_address) (nat %token_a_id))
31
(pair (or %token_a_type (unit %fa12) (unit %fa2))
32
(address %token_b_address)))
33
(pair (nat %token_b_id)
34
(or %token_b_type (unit %fa12) (unit %fa2))))
35
(pair (nat %token_a_in) (nat %token_b_in)))
36
(pair %swap
37
(list %swaps (pair (or %operation (unit %buy) (unit %sell))
38
(pair %pair
39
(pair
40
(pair (address %token_a_address)
41
(nat %token_a_id))
42
(pair
43
(or %token_a_type (unit %fa12)
44
(unit %fa2))
45
(address %token_b_address)))
46
(pair (nat %token_b_id)
47
(or %token_b_type (unit %fa12)
48
(unit %fa2))))))
49
(pair (nat %amount_in)
50
(pair (nat %min_amount_out) (address %receiver))))))
51
(pair
52
(pair
53
(pair (bool %entered)
54
(big_map %ledger (pair address nat)
55
(pair (set %allowances address)
56
(nat %balance))))
57
(pair
58
(big_map %pairs nat
59
(pair
60
(pair (nat %token_a_pool)
61
(nat %token_b_pool))
62
(nat %total_supply)))
63
(nat %pairs_count)))
64
(pair (big_map %token_to_id bytes nat)
65
(big_map %tokens nat
66
(pair
67
(pair
68
(pair (address %token_a_address)
69
(nat %token_a_id))
70
(pair
71
(or %token_a_type (unit %fa12)
72
(unit %fa2))
73
(address %token_b_address)))
74
(pair (nat %token_b_id)
75
(or %token_b_type (unit %fa12)
76
(unit %fa2))))))))
77
address)
78
(pair (list operation)
79
(pair
80
(pair
81
(pair (bool %entered)
82
(big_map %ledger (pair address nat)
83
(pair (set %allowances address)
84
(nat %balance))))
85
(pair
86
(big_map %pairs nat
87
(pair
88
(pair (nat %token_a_pool)
89
(nat %token_b_pool))
90
(nat %total_supply)))
91
(nat %pairs_count)))
92
(pair (big_map %token_to_id bytes nat)
93
(big_map %tokens nat
94
(pair
95
(pair
96
(pair (address %token_a_address)
97
(nat %token_a_id))
98
(pair
99
(or %token_a_type (unit %fa12)
100
(unit %fa2))
101
(address %token_b_address)))
102
(pair (nat %token_b_id)
103
(or %token_b_type
104
(unit %fa12)
105
(unit %fa2)))))))))
106
(nat %index))))
Copied!
The method is used to set the lambdas to the storage after the contract deployment.
Parameter
Type
Description
func
complex type
The lambda
index
nat
Index in lambda big map

setTokenFunction(func, index)

1
(pair %setTokenFunction
2
(lambda %func
3
(pair
4
(pair
5
(or
6
(or
7
(pair %iBalance_of
8
(list %requests (pair (address %owner) (nat %token_id)))
9
(contract %callback (list (pair
10
(pair %request (address %owner)
11
(nat %token_id))
12
(nat %balance)))))
13
(list %iTransfer (pair (address %from_)
14
(list %txs (pair (address %to_)
15
(pair (nat %token_id)
16
(nat %amount)))))))
17
(list %iUpdate_operators (or
18
(pair %add_operator (address %owner)
19
(pair
20
(address %operator)
21
(nat %token_id)))
22
(pair %remove_operator
23
(address %owner)
24
(pair (address %operator)
25
(nat %token_id))))))
26
(pair
27
(pair
28
(pair (bool %entered)
29
(big_map %ledger (pair address nat)
30
(pair (set %allowances address)
31
(nat %balance))))
32
(pair
33
(big_map %pairs nat
34
(pair
35
(pair (nat %token_a_pool)
36
(nat %token_b_pool))
37
(nat %total_supply)))
38
(nat %pairs_count)))
39
(pair (big_map %token_to_id bytes nat)
40
(big_map %tokens nat
41
(pair
42
(pair
43
(pair (address %token_a_address)
44
(nat %token_a_id))
45
(pair
46
(or %token_a_type (unit %fa12)
47
(unit %fa2))
48
(address %token_b_address)))
49
(pair (nat %token_b_id)
50
(or %token_b_type (unit %fa12)
51
(unit %fa2))))))))
52
address)
53
(pair (list operation)
54
(pair
55
(pair
56
(pair (bool %entered)
57
(big_map %ledger (pair address nat)
58
(pair (set %allowances address)
59
(nat %balance))))
60
(pair
61
(big_map %pairs nat
62
(pair
63
(pair (nat %token_a_pool)
64
(nat %token_b_pool))
65
(nat %total_supply)))
66
(nat %pairs_count)))
67
(pair (big_map %token_to_id bytes nat)
68
(big_map %tokens nat
69
(pair
70
(pair
71
(pair (address %token_a_address)
72
(nat %token_a_id))
73
(pair
74
(or %token_a_type (unit %fa12)
75
(unit %fa2))
76
(address %token_b_address)))
77
(pair (nat %token_b_id)
78
(or %token_b_type
79
(unit %fa12)
80
(unit %fa2)))))))))
81
(nat %index))
Copied!
The method is used to set the lambdas to the storage after the contract deployment.
Parameter
Type
Description
func
complex type
The lambda
index
nat
Index in lambda big map

addPair(token_a_address, token_a_id, token_a_type, token_b_address, token_b_id, token_b_type, token_a_in, token_b_in)

1
(pair %addPair
2
(pair %pair
3
(pair (pair (address %token_a_address) (nat %token_a_id))
4
(pair (or %token_a_type (unit %fa12) (unit %fa2))
5
(address %token_b_address)))
6
(pair (nat %token_b_id)
7
(or %token_b_type (unit %fa12) (unit %fa2))))
8
(pair (nat %token_a_in) (nat %token_b_in)))
Copied!
Setup the new exchange or relaunch the exchange after all the liquidity was drained. amount_a_in and amount_b_in of tokens must be approved. Initial liquidity should be non-zero. All the storage parameters are reset to default values. The sender receives shares equal to a minimum of amount_a_in and amount_b_in. Addresses of the tokens must be ordered in ascending order. If the addresses are equal the token ids must be in ascending order.
Parameter
Type
Description
token_a_address
address
The address of the first token
token_a_id
nat
Token id of the first asset; 0 in case of fa1.2 token
token_a_type
"fa12" || "fa2"
Token standard
token_b_address
address
The address of the second token
token_b_id
nat
Identifier of the second asset; 0 in case of fa1.2 token
token_b_type
"fa12" || "fa2"
Token standard
token_a_in
nat
Amount of the first token
token_b_in
nat
Amount of the second token

swap(token_a_address, token_a_id, token_a_type, token_b_address, token_b_id, token_b_type, operation, amount_in, min_amount_out, receiver)

1
(pair %swap
2
(list %swaps (pair (or %operation (unit %buy) (unit %sell))
3
(pair %pair
4
(pair
5
(pair (address %token_a_address)
6
(nat %token_a_id))
7
(pair
8
(or %token_a_type (unit %fa12) (unit %fa2))
9
(address %token_b_address)))
10
(pair (nat %token_b_id)
11
(or %token_b_type (unit %fa12)
12
(unit %fa2))))))
13
(pair (nat %amount_in)
14
(pair (nat %min_amount_out) (address %receiver))))
Copied!
Swaps the token to another token. "Sell" operation is used to exchange token_a_address to token_b_address. "Buy" performs the opposite swap from token_b_address to token_a_address. The amount_in of the token will be charged from the user. If the received amount is smaller than min_amount_out then the transaction is reverted. Addresses of the tokens must be ordered in ascending order. If the addresses are equal the token ids must be in ascending order.
Parameter
Type
Description
swaps
array of data
Swaps to perform; each swap consist of standard, token_a_address,token_a_id,token_b_address,token_b_id,operation
token_a_address(per swap)
address
The address of the first token
token_a_id(per swap)
nat
Token id of the first asset; 0 in case of fa1.2 token
token_a_type
"fa12" || "fa2"
Token standard
token_b_address(per swap)
address
The address of the second token
token_b_id(per swap)
nat
Identifier of the second asset; 0 in case of fa1.2 token
token_b_type
"fa12" || "fa2"
Token standard
operation(per swap)
"sell"|| "buy"
Name of the operation
amount_in
nat
Amount of the token to swap
min_amount_out
nat
The minimal amount of the token to receive
receiver
address
Receiver's address

invest(token_a_address, token_a_id, token_a_type, token_b_address, token_b_id, token_b_type, token_a_in, token_b_in)

1
(pair %invest
2
(pair %pair
3
(pair (pair (address %token_a_address) (nat %token_a_id))
4
(pair (or %token_a_type (unit %fa12) (unit %fa2))
5
(address %token_b_address)))
6
(pair (nat %token_b_id)
7
(or %token_b_type (unit %fa12) (unit %fa2))))
8
(pair (nat %token_a_in) (nat %token_b_in)))
Copied!
Adds new liquidity to the exchange. amount_a_in and amount_b_in of tokens must be approved. Initial liquidity should be non-zero. The sender spends amount_a_in and amount_b_in at most otherwise, the transaction fails. Addresses of the tokens must be ordered in ascending order. If the addresses are equal the token ids must be in ascending order.
Parameter
Type
Description
token_a_address
address
The address of the first token
token_a_id
nat
Token id of the first asset; 0 in case of fa1.2 token
token_a_type
"fa12" || "fa2"
Token standard
token_b_address
address
The address of the second token
token_b_id
nat
Identifier of the second asset; 0 in case of fa1.2 token
token_b_type
"fa12" || "fa2"
Token standard
token_a_in
nat
Amount of the first token
token_b_in
nat
Amount of the second token

divest(token_a_address, token_a_id, token_a_type, token_b_address, token_b_id, token_b_type, min_token_a_out, min_token_b_out, shares)

1
(pair %divest
2
(pair %pair
3
(pair (pair (address %token_a_address) (nat %token_a_id))
4
(pair (or %token_a_type (unit %fa12) (unit %fa2))
5
(address %token_b_address)))
6
(pair (nat %token_b_id)
7
(or %token_b_type (unit %fa12) (unit %fa2))))
8
(pair (nat %min_token_a_out)
9
(pair (nat %min_token_b_out) (nat %shares))))
Copied!
Burns shares and sends tokens to the owner; operation is reverted if the amount of appropriate divested tokens is smaller than min_token_a_out or min_token_b_out. . All parameters are non-zero values. Addresses of the tokens must be ordered in ascending order. If the addresses are equal the token ids must be in ascending order.
Parameter
Type
Description
token_a_address
address
The address of the first token
token_a_id
nat
Token id of the first asset; 0 in case of fa1.2 token
token_a_type
"fa12" || "fa2"
Token standard
token_b_address
address
The address of the second token
token_b_id
nat
Identifier of the secod asset; 0 in case of fa1.2 token
token_b_type
"fa12" || "fa2"
Token standard
min_token_a_out
nat
Minimal amount of the first token to receive
min_token_b_out
nat
Minimal amount of the second token to receive
shares
nat
The amount of the shares to burn

API

1
parameter (or
2
(or
3
(or
4
(pair %balance_of
5
(list %requests (pair (address %owner) (nat %token_id)))
6
(contract %callback (list (pair
7
(pair %request (address %owner)
8
(nat %token_id))
9
(nat %balance)))))
10
(unit %close))
11
(or
12
(pair %get_reserves (nat %pair_id) (contract %receiver (pair nat nat)))
13
(pair %setDexFunction
14
(lambda %func
15
(pair
16
(pair
17
(or
18
(or
19
(pair %addPair
20
(pair %pair
21
(pair
22
(pair (address %token_a_address) (nat %token_a_id))
23
(pair (or %token_a_type (unit %fa12) (unit %fa2))
24
(address %token_b_address)))
25
(pair (nat %token_b_id)
26
(or %token_b_type (unit %fa12) (unit %fa2))))
27
(pair (nat %token_a_in) (nat %token_b_in)))
28
(pair %divest
29
(pair %pair
30
(pair
31
(pair (address %token_a_address) (nat %token_a_id))
32
(pair (or %token_a_type (unit %fa12) (unit %fa2))
33
(address %token_b_address)))
34
(pair (nat %token_b_id)
35
(or %token_b_type (unit %fa12) (unit %fa2))))
36
(pair (nat %min_token_a_out)
37
(pair (nat %min_token_b_out) (nat %shares)))))
38
(or
39
(pair %invest
40
(pair %pair
41
(pair
42
(pair (address %token_a_address) (nat %token_a_id))
43
(pair (or %token_a_type (unit %fa12) (unit %fa2))
44
(address %token_b_address)))
45
(pair (nat %token_b_id)
46
(or %token_b_type (unit %fa12) (unit %fa2))))
47
(pair (nat %token_a_in) (nat %token_b_in)))
48
(pair %swap
49
(list %swaps (pair (or %operation (unit %buy) (unit %sell))
50
(pair %pair
51
(pair
52
(pair (address %token_a_address)
53
(nat %token_a_id))
54
(pair
55
(or %token_a_type (unit %fa12)
56
(unit %fa2))
57
(address %token_b_address)))
58
(pair (nat %token_b_id)
59
(or %token_b_type (unit %fa12)
60
(unit %fa2))))))
61
(pair (nat %amount_in)
62
(pair (nat %min_amount_out) (address %receiver))))))
63
(pair
64
(pair
65
(pair (bool %entered)
66
(big_map %ledger (pair address nat)
67
(pair (set %allowances address)
68
(nat %balance))))
69
(pair
70
(big_map %pairs nat
71
(pair
72
(pair (nat %token_a_pool)
73
(nat %token_b_pool))
74
(nat %total_supply)))
75
(nat %pairs_count)))
76
(pair (big_map %token_to_id bytes nat)
77
(big_map %tokens nat
78
(pair
79
(pair
80
(pair (address %token_a_address)
81
(nat %token_a_id))
82
(pair
83
(or %token_a_type (unit %fa12)
84
(unit %fa2))
85
(address %token_b_address)))
86
(pair (nat %token_b_id)
87
(or %token_b_type (unit %fa12)
88
(unit %fa2))))))))
89
address)
90
(pair (list operation)
91
(pair
92
(pair
93
(pair (bool %entered)
94
(big_map %ledger (pair address nat)
95
(pair (set %allowances address)
96
(nat %balance))))
97
(pair
98
(big_map %pairs nat
99
(pair
100
(pair (nat %token_a_pool)
101
(nat %token_b_pool))
102
(nat %total_supply)))
103
(nat %pairs_count)))
104
(pair (big_map %token_to_id bytes nat)
105
(big_map %tokens nat
106
(pair
107
(pair
108
(pair (address %token_a_address)
109
(nat %token_a_id))
110
(pair
111
(or %token_a_type (unit %fa12)
112
(unit %fa2))
113
(address %token_b_address)))
114
(pair (nat %token_b_id)
115
(or %token_b_type
116
(unit %fa12)
117
(unit %fa2)))))))))
118
(nat %index))))
119
(or
120
(or
121
(pair %setTokenFunction
122
(lambda %func
123
(pair
124
(pair
125
(or
126
(or
127
(pair %iBalance_of
128
(list %requests (pair (address %owner) (nat %token_id)))
129
(contract %callback (list (pair
130
(pair %request (address %owner)
131
(nat %token_id))
132
(nat %balance)))))
133
(list %iTransfer (pair (address %from_)
134
(list %txs (pair (address %to_)
135
(pair (nat %token_id)
136
(nat %amount)))))))
137
(list %iUpdate_operators (or
138
(pair %add_operator (address %owner)
139
(pair
140
(address %operator)
141
(nat %token_id)))
142
(pair %remove_operator
143
(address %owner)
144
(pair (address %operator)
145
(nat %token_id))))))
146
(pair
147
(pair
148
(pair (bool %entered)
149
(big_map %ledger (pair address nat)
150
(pair (set %allowances address)
151
(nat %balance))))
152
(pair
153
(big_map %pairs nat
154
(pair
155
(pair (nat %token_a_pool)
156
(nat %token_b_pool))
157
(nat %total_supply)))
158
(nat %pairs_count)))
159
(pair (big_map %token_to_id bytes nat)
160
(big_map %tokens nat
161
(pair
162
(pair
163
(pair (address %token_a_address)
164
(nat %token_a_id))
165
(pair
166
(or %token_a_type (unit %fa12)
167
(unit %fa2))
168
(address %token_b_address)))
169
(pair (nat %token_b_id)
170
(or %token_b_type (unit %fa12)
171
(unit %fa2))))))))
172
address)
173
(pair (list operation)
174
(pair
175
(pair
176
(pair (bool %entered)
177
(big_map %ledger (pair address nat)
178
(pair (set %allowances address)
179
(nat %balance))))
180
(pair
181
(big_map %pairs nat
182
(pair
183
(pair (nat %token_a_pool)
184
(nat %token_b_pool))
185
(nat %total_supply)))
186
(nat %pairs_count)))
187
(pair (big_map %token_to_id bytes nat)
188
(big_map %tokens nat
189
(pair
190
(pair
191
(pair (address %token_a_address)
192
(nat %token_a_id))
193
(pair
194
(or %token_a_type (unit %fa12)
195
(unit %fa2))
196
(address %token_b_address)))
197
(pair (nat %token_b_id)
198
(or %token_b_type
199
(unit %fa12)
200
(unit %fa2)))))))))
201
(nat %index))
202
(list %transfer (pair (address %from_)
203
(list %txs (pair (address %to_)
204
(pair (nat %token_id) (nat %amount)))))))
205
(or
206
(list %update_operators (or
207
(pair %add_operator (address %owner)
208
(pair (address %operator)
209
(nat %token_id)))
210
(pair %remove_operator (address %owner)
211
(pair
212
(address %operator)
213
(nat %token_id)))))
214
(or %use
215
(or
216
(pair %addPair
217
(pair %pair
218
(pair (pair (address %token_a_address) (nat %token_a_id))
219
(pair (or %token_a_type (unit %fa12) (unit %fa2))
220
(address %token_b_address)))
221
(pair (nat %token_b_id)
222
(or %token_b_type (unit %fa12) (unit %fa2))))
223
(pair (nat %token_a_in) (nat %token_b_in)))
224
(pair %divest
225
(pair %pair
226
(pair (pair (address %token_a_address) (nat %token_a_id))
227
(pair (or %token_a_type (unit %fa12) (unit %fa2))
228
(address %token_b_address)))
229
(pair (nat %token_b_id)
230
(or %token_b_type (unit %fa12) (unit %fa2))))
231
(pair (nat %min_token_a_out)
232
(pair (nat %min_token_b_out) (nat %shares)))))
233
(or
234
(pair %invest
235
(pair %pair
236
(pair (pair (address %token_a_address) (nat %token_a_id))
237
(pair (or %token_a_type (unit %fa12) (unit %fa2))
238
(address %token_b_address)))
239
(pair (nat %token_b_id)
240
(or %token_b_type (unit %fa12) (unit %fa2))))
241
(pair (nat %token_a_in) (nat %token_b_in)))
242
(pair %swap
243
(list %swaps (pair (or %operation (unit %buy) (unit %sell))
244
(pair %pair
245
(pair
246
(pair (address %token_a_address)
247
(nat %token_a_id))
248
(pair
249
(or %token_a_type (unit %fa12) (unit %fa2))
250
(address %token_b_address)))
251
(pair (nat %token_b_id)
252
(or %token_b_type (unit %fa12)
253
(unit %fa2))))))
254
(pair (nat %amount_in)
255
(pair (nat %min_amount_out) (address %receiver)))))))));
Copied!
Last modified 5mo ago