-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasa_dex_teal.js
373 lines (319 loc) · 10.1 KB
/
asa_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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/////////////////////////////
// Alexander Trefonas //
// 7/9/2021 //
// Copyright Algodev Inc //
// All Rights Reserved. //
/////////////////////////////
const AsaOrderbookTeal = {
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;
},
getASAOrderBookApprovalProgram : function getASAOrderBookApprovalProgram() {
// stateful DEX contract
// This is for the order book
return `
////////////////////////////////////////////////
// STATEFUL CONTRACT /
// ORDER BOOK FOR ASA ESCROWS (SELL ORDERS) /
////////////////////////////////////////////////
#pragma version 4
// check if the app is being created
// if so save creator
int 0
txn ApplicationID
==
bz not_creation
byte "Creator"
txn Sender
app_global_put
int 1
return
not_creation:
int DeleteApplication
txn OnCompletion
==
bz not_deletion
byte "Creator" // verify creator is deleting app
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
// function to check for ASA opt in transaction
check_asa_optin:
gtxn 2 TypeEnum
int axfer
==
gtxn 2 AssetAmount
int 0
==
&&
gtxn 2 Sender
gtxn 2 AssetReceiver
==
&&
gtxn 2 AssetCloseTo
global ZeroAddress
==
&&
gtxn 2 Sender
txn Sender // Sender must come from the user's wallet, not the escrow
!= // should *not* be originating from escrow
&&
store 0 //this will store the next transaction offset depending if opt in exists
load 0
int 2
+
store 2 // store offset of transaction 2, depending on if opt-in exists
load 0
int 3
+
store 3 // store offset of transaction 3, depending on if opt-in exists
retsub
///////////////////////////////////////////////////////////////////////
// OPEN - ORDER BOOK OPT IN & REGISTRATION
// Placing an ASA Escrow Order. The escrow opts into the order book.
///////////////////////////////////////////////////////////////////////
// TXN 0 - SELLER TO ESCROW: pay transaction into escrow
// TXN 1 - ESCROW TO ORDERBOOK: application opt in
// TXN 2 - ESCROW TO ESCROW: asset opt in
// TXN 3 - SELLER TO ESCROW: asset transfer
open:
int OptIn
txn OnCompletion
==
global GroupSize
int 4
==
&&
assert
gtxn 0 Sender
gtxn 3 Sender
==
assert
int 0 //address index. This is the Sender of this transaction.
txn ApplicationID //current smart contract
txna ApplicationArgs 1 // 2nd txn app arg is order number
app_local_get_ex
bnz ret_success // if the value already exists return without setting anything
pop
int 0 //address index. This is the Sender of this transaction.
txna ApplicationArgs 1 //order number
int 1 // value - just set to 1
app_local_put // store the ordernumber as the key
int 0 //address index. This is the Sender of this transaction.
byte "creator" //creator key
gtxn 0 Sender // store creator as value. This is the sender of the pay transaction
app_local_put
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)
// Partial execution of an ASA escrow, where an ASA balance remains in it
//////////////////////////////////////////////////////////////////////////////
// TXN 0 - ESCROW TO ORDERBOOK: Application call to execute
// TXN 1 - BUYER TO SELLER: Pay transaction (from buyer/executor to escrow owner)
// TXN 2 - BUYER TO BUYER: (Optional) asset opt-in transaction (for buyer/executor)
// TXN 2/3 - ESCROW TO BUYER: Asset transfer (from escrow to buyer/executor)
// TXN 3/4 - BUYER TO ESCROW: Pay transaction for fee refund (from buyer/executor to escrow)
execute:
txn OnCompletion //FIXME check OnCompletion of each individual transaction
int CloseOut
==
txn OnCompletion
int NoOp
==
||
assert
callsub check_asa_optin // this will store transaction offsets into registers if the asa opt-in exists or not
txn Sender
int 0 // foreign asset id 0
asset_holding_get AssetBalance // pushes 1 for success, then asset onto stack
assert //make sure asset exists
load 2
gtxns AssetAmount
> // The asset balance should be greater than or equal to the amount transferred. Otherwise should be with closeout
assert
global GroupSize
int 4
==
global GroupSize //group size can be 5 for asset opt-in
int 5
==
||
assert
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
==
&&
assert
load 2
gtxns TypeEnum //The next transaction must be an asset transfer
int axfer
==
assert
load 3
gtxns TypeEnum
int pay
==
assert
int 0 // Escrow account containing order. This is the Sender of this transaction.
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. This is the Sender of this transaction.
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 WITH CLOSE
// Full order execution where the remaining minimum algo balance is closed to the escrow owner
/////////////////////////////////////////////////////////////////////////////////////////////////
// TXN 0 - ESCROW TO ORDERBOOK: Application call to execute
// TXN 1 - BUYER TO SELLER: Pay transaction (from buyer/executor to escrow owner)
// TXN 2 - BUYER TO BUYER: (Optional) asset opt-in transaction (for buyer/executor)
// TXN 2/3 - ESCROW TO BUYER: Asset transfer (from escrow to buyer/executor)
// - closes out any remaining ASA to seller (escrow owner) as well
// TXN 3/4 - ESCROW TO SELLER: Pay transaction to close out to escrow owner
execute_with_closeout:
txn OnCompletion
int CloseOut
==
bz fail2
callsub check_asa_optin // this will store transaction offsets into registers if the asa opt-in exists or not
txn Sender
int 0 // foreign asset id 0
asset_holding_get AssetBalance // pushes 1 for success, then asset onto stack
assert //make sure asset exists
load 2
gtxns AssetAmount
== // Check we are going to transfer the entire ASA amount to the buyer. Otherwise should be a partial execute
assert
global GroupSize
int 4
==
global GroupSize //group size can be 5 for asset opt-in
int 5
==
||
assert
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
==
&&
assert
load 2
gtxns TypeEnum //The next transaction must be an asset transfer
int axfer
==
assert
load 3 // The last transaction must be a payment transfer
gtxns TypeEnum
int pay
==
assert
int 0 // Escrow account containing order. This is the Sender of this transaction.
txn ApplicationID // Current stateful smart contract
txna ApplicationArgs 1 // 2nd argument is order number
app_local_get_ex
bz fail2 // If the value doesnt exist fail
pop
int 0 // Escrow account containing order. This is the Sender of this transaction.
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. This is the Sender of this transaction.
txn ApplicationID // Current stateful smart contract
byte "version"
app_local_get_ex
assert // If the value doesnt exists fail
pop
global ZeroAddress
gtxn 1 CloseRemainderTo
==
bnz ret_success3
int 0 //escrow account containing order. This is the Sender of this transaction.
txna ApplicationArgs 1 // order details
app_local_del // Delete the ordernumber
int 0 // escrow account containing order
byte "creator"
app_local_del // Delete the creator
int 0 // escrow account containing order
byte "version" // Delete the version
app_local_del
ret_success3:
int 1
return
fail2:
int 0
return
`;
}
};
module.exports = AsaOrderbookTeal;