-
Notifications
You must be signed in to change notification settings - Fork 0
/
dex_teal.js
297 lines (259 loc) · 7.61 KB
/
dex_teal.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/////////////////////////////
// Alexander Trefonas //
// 7/9/2021 //
// Copyright Algodev Inc //
// All Rights Reserved. //
/////////////////////////////
const AlgoOrderbookTeal = {
getClearProgram : function getClearProgram() {
const clearProgram =
`
#pragma version 2
// This program clears program state.
// This will clear local state for any escrow accounts that call this from a ClearState transaction,
// the logic of which is contained within the stateless contracts as part of a Close Out (Cancel Order) operation.
// We use ClearState instead of CloseOut so that the order book cannot prevent an escrow from closing out.
int 1
`
;
return clearProgram;
},
getAlgoOrderBookApprovalProgram : function getAlgoOrderBookApprovalProgram() {
// stateful DEX contract
// This is for the order book
return `
//////////////////////////////////////////////////
// STATEFUL CONTRACT /
// ORDER BOOK FOR ALGO ESCROWS (BUY ORDERS) /
//////////////////////////////////////////////////
#pragma version 4
// STATEFUL APP CREATION
// check if the app is being created
// if so save creator
int 0
txn ApplicationID // check if there is no application ID - means it hasn't been created
==
bz not_creation
byte "Creator"
txn Sender
app_global_put
int 1
return
not_creation:
int DeleteApplication // check if this is deletion transaction
txn OnCompletion
==
bz not_deletion
byte "Creator"
app_global_get
txn Sender
==
assert
int 1
return
not_deletion:
int UpdateApplication // check if this is update
txn OnCompletion
==
bz not_update
byte "Creator" // verify that the creator is making the call
app_global_get
txn Sender
==
assert
int 1
return
not_update:
txna ApplicationArgs 0
byte "open"
==
bnz open
txna ApplicationArgs 0
byte "execute"
==
bnz execute
txna ApplicationArgs 0
byte "execute_with_closeout"
==
bnz execute_with_closeout
err
///////////////////////////
/// OPEN - ORDER BOOK OPT IN & REGISTRATION
// Placing an Algo Escrow Order
//////////////////////////
// TXN 0 - BUYER TO ESCROW: Pay from order creator to escrow account
// TXN 1 - ESCROW TO ORDERBOOK: Stateful app opt-in to order book
// TXN 2 - BUYER TO BUYER: (Optional) ASA opt-in for the order creator's original wallet account.
// This is the situation when a new escrow contract and order book contract is created
// These are for Algo-only escrow accounts, i.e. buy orders to buy ASAs
// The orders are unique per users
open:
gtxn 0 Amount // pay transaction must be over 1 algo
int 500000 // 0.5 algo. Also checked in the escrow contract
>=
assert
global GroupSize
int 2
==
global GroupSize
int 3
==
||
assert
int OptIn
txn OnCompletion
==
assert
int 0 //sender
txn ApplicationID //current smart contract
txna ApplicationArgs 1 // 2nd arg is order number
app_local_get_ex // if the value already exists return without setting anything
bnz ret_success
pop
int 0 //address index
txna ApplicationArgs 1 //order number
int 1 // value - just set to 1
app_local_put // Store the order number as the key
int 0 //address index
byte "creator" //creator key
gtxn 0 Sender // The sender of the pay transaction
app_local_put // Store creator as value.
int 0 //address index. This is the Sender of this transaction.
byte "version" //store version
txna ApplicationArgs 2 //version
int 0
getbyte
app_local_put // store the version
ret_success:
int 1
return
///////////////////////////////////
// EXECUTE
// (PARTIAL ORDER EXECUTION)
/////////////////////////////////
// TXN 0 - ESCROW TO ORDERBOOK: Transaction must be a call to a stateful contract
// TXN 1 - ESCROW TO SELLER: Payment transaction from this escrow to seller
// TXN 2 - SELLER TO BUYER: Asset transfer from seller to owner of this escrow (buyer)
// TXN 3 - SELLER TO ESCROW: Pay fee refund transaction
execute:
txn OnCompletion
int NoOp
==
assert
global GroupSize
int 4
==
gtxn 0 TypeEnum // First Transaction must be a call to a stateful contract
int appl
==
&&
gtxn 1 TypeEnum // The second transaction must be a payment transaction
int pay
==
&&
gtxn 2 TypeEnum // The third transaction must be an asset transfer
int axfer
==
&&
gtxn 3 TypeEnum // The fourth transaction must be a payment transaction to refund escrow fees
int pay
==
&&
assert
txn Sender
balance
gtxn 1 Amount
-
int 499000
>= // after subtracting the amount, over 0.499 algo must remain (0.5 - original txn fee from maker order)
assert
int 0 // Escrow account containing order
txn ApplicationID // Current stateful smart contract
txna ApplicationArgs 1 // 2nd argument is order number
app_local_get_ex
assert // If the value doesnt exists fail
pop
int 0 // Escrow account containing order
txn ApplicationID // Current stateful smart contract
byte "creator"
app_local_get_ex // returns if it exists and the creator
assert // If the value doesnt exist fail
txna Accounts 1 // account arg is order creator
==
assert
global ZeroAddress
gtxn 1 CloseRemainderTo
==
assert
int 1
return
///////////////////////////////
// EXECUTE (ORDER EXECUTION)
// WITH CLOSEOUT
/////////////////////////////////
// TXN 0 - ESCROW TO ORDERBOOK: transaction must be a call to a stateful contract
// TXN 1 - ESCROW TO SELLER: Payment transaction from this escrow to seller, with closeout to owner (buyer)
// TXN 2 - SELLER TO BUYER: Asset transfer from seller to owner of this escrow (buyer)
execute_with_closeout:
txn Sender
balance
gtxn 1 Amount
-
int 500000
< // after subtracting the amount, less than 0.5 algo must remain (to be closed out)
assert
txn OnCompletion
int CloseOut
==
global GroupSize // Must be three transactions
int 3
==
&&
gtxn 0 TypeEnum // First Transaction must be a call to a stateful contract
int appl
==
&&
gtxn 1 TypeEnum // The second transaction must be a payment transaction
int pay
==
&&
gtxn 2 TypeEnum // The third transaction must be an asset transfer
int axfer
==
&&
assert
int 0 // Escrow account containing order
txn ApplicationID // Current stateful smart contract
txna ApplicationArgs 1 // 2nd argument is order number
app_local_get_ex
assert // If the value doesnt exists fail
pop
int 0 // Escrow account containing order
txn ApplicationID // Current stateful smart contract
byte "creator"
app_local_get_ex // returns if it exists and the creator
assert // If the value doesnt exist fail
txna Accounts 1 // account arg is order creator
==
assert
int 0 // Escrow account containing order
txn ApplicationID // Current stateful smart contract
byte "version"
app_local_get_ex
assert // If the value doesnt exists fail
pop
int 0 //escrow account containing order
txna ApplicationArgs 1 // order details
app_local_del // Delete the order details from the order book
int 0
byte "creator"
app_local_del // Delete creator from order book
int 0
byte "version" // Delete version from order book
app_local_del
int 1
return
`;
}
}
module.exports = AlgoOrderbookTeal;