-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helper.js
384 lines (340 loc) · 10.9 KB
/
test_helper.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
374
375
376
377
378
379
380
381
382
383
384
/////////////////////////////
// Alexander Trefonas //
// 7/9/2021 //
// Copyright Algodev Inc //
// All Rights Reserved. //
/////////////////////////////
const http = require('http');
const algosdk = require('algosdk');
const axios = require('axios').default;
const algoDelegateTemplate = require('./algo_delegate_template_teal.js');
const asaDelegateTemplate = require('./ASA_delegate_template_teal.js');
const algoOrderBook = require('./dex_teal.js');
const asaOrderBook = require('./asa_dex_teal.js');
//require('./dex_teal.js');
const dexInternal = require('./algodex_internal_api.js');
const algodex = require('./algodex_api.js');
const constants = require('./constants.js');
const transactionGenerator = require('./generate_transaction_types.js');
let ALGO_ESCROW_ORDER_BOOK_ID = -1;
let ASA_ESCROW_ORDER_BOOK_ID = -1;
const ALGOD_SERVER = 'https://testnet-api.algonode.cloud';
const ALGOD_TOKEN = ''; //{ 'X-API-Key': 'VELyABA1dGqGbAVktbew4oACvp0c0298gMgYtYIb' }
const ALGOD_PORT = '';
const TestHelper = {
getLocalClient: function getLocalClientAndEnv() {
//const algodClient = algodex.initAlgodClient("test");
const algodClient = new algosdk.Algodv2(
ALGOD_TOKEN,
ALGOD_SERVER,
ALGOD_PORT
);
return algodClient;
},
getRandomAccount: function getRandomAccount() {
return algosdk.generateAccount();
},
getAccountInfo: async function getAccountInfo(addr) {
return algodex.getAccountInfo(addr);
// This is the old account info that uses algod, not the indexer
// This is necessary for the tests to go faster as the indexer takes time to update.
/*try {
let port = (!!ALGOD_PORT) ? ':' + ALGOD_PORT : '';
const response = await axios.get(ALGOD_SERVER + port + "/v2/accounts/"+addr, {headers: {'X-Algo-API-Token': ALGOD_TOKEN}});
//console.debug(response);
return response.data;
} catch (error) {
console.error(error);
throw new Error("getAccountInfo failed: ", error);
}*/
},
printOuterTransactions: function (outerTxns) {
for (let i = 0; i < outerTxns.length; i++) {
console.log(outerTxns[i]);
}
},
checkFailureType: function (error) {
let hasKnownError = false;
if (error != null && error.response.body.message != null) {
const msg = error.response.body.message;
if (msg.includes('rejected by logic err=')) {
hasKnownError = true;
} else if (msg.includes('TEAL runtime encountered err opcode')) {
hasKnownError = true;
} else {
throw 'Unknown error type: ' + msg;
}
}
return hasKnownError;
},
getOpenAccount: function getOpenAccount() {
//WYWRYK42XADLY3O62N52BOLT27DMPRA3WNBT2OBRT65N6OEZQWD4OSH6PI
let mn =
'mass army warrior number blush distance enroll vivid horse become spend asthma hat desert amazing room asset ivory lucky ridge now deputy erase absorb above';
let openAccount = algosdk.mnemonicToSecretKey(mn);
return openAccount;
},
transferASA: async function (
client,
fromAccount,
toAccount,
amount,
assetId
) {
const asaTransferTxn = await transactionGenerator.getAssetSendTxn(
client,
fromAccount,
toAccount,
amount,
assetId,
false
);
const asaTxnId = asaTransferTxn.txID().toString();
let signedFundTxn = asaTransferTxn.signTxn(fromAccount.sk);
console.log('Signed transaction with txID: %s', asaTxnId);
// Submit the transaction
try {
await client.sendRawTransaction(signedFundTxn).do();
} catch (e) {
console.log(JSON.stringify(e));
}
// Wait for confirmation
await this.checkPending(client, asaTxnId);
},
transferFunds: async function (client, fromAccount, toAccount, amount) {
const fundTxn = await transactionGenerator.getPayTxn(
client,
fromAccount,
toAccount,
amount,
false
);
const fundTxnId = fundTxn.txID().toString();
let signedFundTxn = fundTxn.signTxn(fromAccount.sk);
console.log('Signed transaction with txID: %s', fundTxnId);
// Submit the transaction
try {
await client.sendRawTransaction(signedFundTxn).do();
} catch (e) {
console.log(JSON.stringify(e));
}
// Wait for confirmation
await this.checkPending(client, fundTxnId);
},
sendAndCheckConfirmed: async function (client, signedTxns) {
// Submit the transaction
let txId = null;
try {
let sentTxns = await client.sendRawTransaction(signedTxns).do();
txId = sentTxns.txId;
} catch (e) {
throw e;
}
// Wait for confirmation
await this.waitForConfirmation(client, txId);
},
sendAndCheckPending: async function (client, signedTxns) {
// Submit the transaction
let txId = null;
try {
let sentTxns = await client.sendRawTransaction(signedTxns).do();
txId = sentTxns.txId;
} catch (e) {
throw e;
}
// Wait for confirmation
await this.checkPending(client, txId);
},
getOrderLsig: async function (
algodClient,
makerAccount,
price,
assetId,
isASAEscrow
) {
const orderCreatorAddr = makerAccount.addr;
const numAndDenom = algodex.getNumeratorAndDenominatorFromPrice(price);
const n = numAndDenom.n;
const d = numAndDenom.d;
const escrowSource = algodex.buildDelegateTemplateFromArgs(
0,
assetId,
n,
d,
orderCreatorAddr,
isASAEscrow,
constants.ESCROW_CONTRACT_VERSION
);
const lsig = await algodex.getLsigFromProgramSource(
algosdk,
algodClient,
escrowSource,
constants.DEBUG_SMART_CONTRACT_SOURCE
);
return lsig;
},
getAssetBalance: async function (accountAddr, assetId) {
console.log('checking account info for: ' + accountAddr);
const accountInfo = await this.getAccountInfo(accountAddr);
if (
accountInfo != null &&
accountInfo['assets'] != null &&
accountInfo['assets'].length > 0
) {
for (let i = 0; i < accountInfo['assets'].length; i++) {
let asset = accountInfo['assets'][i];
console.log({ asset });
if (asset['asset-id'] == assetId) {
return asset['amount'];
}
}
}
return null;
},
closeAccount: async function closeAccount(client, fromAccount, toAccount) {
console.log('checking account info for: ' + fromAccount.addr);
const fromAccountInfo = await this.getAccountInfo(fromAccount.addr);
if (
fromAccountInfo != null &&
fromAccountInfo['assets'] != null &&
fromAccountInfo['assets'].length > 0
) {
for (let i = 0; i < fromAccountInfo['assets'].length; i++) {
let asset = fromAccountInfo['assets'][i];
let assetId = asset['asset-id'];
console.log(
'closing asset: ' + assetId + ' for account: ' + fromAccount.addr
);
let txn = await transactionGenerator.getAssetSendTxn(
client,
fromAccount,
toAccount,
0,
assetId,
true
);
let signedTxn = algosdk.signTransaction(txn, fromAccount.sk);
await this.sendAndCheckPending(client, [signedTxn.blob]);
}
}
const fundTxn = await transactionGenerator.getPayTxn(
client,
fromAccount,
toAccount,
0,
true
);
const fundTxnId = fundTxn.txID().toString();
let signedTxn = fundTxn.signTxn(fromAccount.sk);
console.log('Signed transaction with txID: %s', fundTxnId);
// Submit the transaction
try {
await client.sendRawTransaction(signedTxn).do();
} catch (e) {
console.log(JSON.stringify(e));
}
await this.checkPending(client, fundTxnId);
},
deleteApplication: async function deleteApplication(client, sender, appId) {
// create unsigned transaction
let params = await client.getTransactionParams().do();
let txn = algosdk.makeApplicationDeleteTxn(sender.addr, params, appId);
// sign, send, await
let signedTxn = txn.signTxn(sender.sk);
let txId = txn.txID().toString();
console.log('Signed transaction with txID: %s', txId);
// Submit the transaction
try {
await client.sendRawTransaction(signedTxn).do();
} catch (e) {
console.log(JSON.stringify(e));
}
// display results
let transactionResponse = await client
.pendingTransactionInformation(txId)
.do();
console.log('Deleted app-id: ', appId);
},
getExecuteAccount: function getExecuteAccount() {
//UUEUTRNQY7RUXESXRDO7HSYRSJJSSVKYVB4DI7X2HVVDWYOBWJOP5OSM3A
let mn =
'three satisfy build purse lens another idle fashion base equal echo recall proof hill shadow coach early palm act wealth dawn menu portion above mystery';
let executeAccount = algosdk.mnemonicToSecretKey(mn);
return executeAccount;
},
// helper function to await transaction confirmation
waitForConfirmation: async function waitForConfirmation(algodClient, txId) {
let status = await algodClient.status().do();
let lastRound = status['last-round'];
while (true) {
const pendingInfo = await algodClient
.pendingTransactionInformation(txId)
.do();
if (
pendingInfo['confirmed-round'] !== null &&
pendingInfo['confirmed-round'] > 0
) {
//Got the completed Transaction
console.log(
'Transaction ' +
txId +
' confirmed in round ' +
pendingInfo['confirmed-round']
);
break;
}
lastRound++;
await algodClient.statusAfterBlock(lastRound).do();
}
},
// helper function to await transaction confirmation
checkPending: async function checkPending(algodClient, txId) {
while (true) {
const pendingInfo = await algodClient
.pendingTransactionInformation(txId)
.do();
if (
pendingInfo != null &&
pendingInfo.txn != null &&
pendingInfo.txn.txn != null
) {
break;
}
}
},
groupAndSignTransactions: function (outerTxns) {
console.log('inside signAndSend transactions');
let txns = [];
for (let i = 0; i < outerTxns.length; i++) {
txns.push(outerTxns[i].unsignedTxn);
}
const groupID = algosdk.computeGroupID(txns);
for (let i = 0; i < txns.length; i++) {
txns[i].group = groupID;
}
for (let i = 0; i < outerTxns.length; i++) {
let txn = outerTxns[i];
if (txn.lsig != null) {
let signedLsig = algosdk.signLogicSigTransactionObject(
txn.unsignedTxn,
txn.lsig
);
txn.signedTxn = signedLsig.blob;
} else {
let signedTxn = algosdk.signTransaction(
txn.unsignedTxn,
txn.senderAcct.sk
);
txn.signedTxn = signedTxn.blob;
}
}
let signed = [];
for (let i = 0; i < outerTxns.length; i++) {
signed.push(outerTxns[i].signedTxn);
}
console.log('printing transaction debug');
algodex.printTransactionDebug(signed);
return signed;
},
};
module.exports = TestHelper;