Skip to content

Commit 36a5c76

Browse files
authored
Merge pull request #296 from algodex/295-randomly-generate-the-asset-before-testing
295 randomly generate the asset before testing
2 parents a8ff23f + 6eaf571 commit 36a5c76

14 files changed

+277
-69
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const algosdk = require('algosdk');
2+
const ALGOD_SERVER = 'https://node.testnet.algoexplorerapi.io';
3+
const ALGOD_TOKEN = ''; // { 'X-API-Key': 'VELyABA1dGqGbAVktbew4oACvp0c0298gMgYtYIb' }
4+
const ALGOD_PORT = '';
5+
6+
const client = new algosdk.Algodv2(
7+
ALGOD_TOKEN,
8+
ALGOD_SERVER,
9+
ALGOD_PORT,
10+
);
11+
/**
12+
*
13+
* @param {object} assetCreator
14+
* @param {string} assetId
15+
* @return {Promise<void>}
16+
*/
17+
async function destroyAsset(assetCreator, assetId) {
18+
const params = await client.getTransactionParams().do();
19+
20+
const dtxn = algosdk.makeAssetDestroyTxnWithSuggestedParams(
21+
assetCreator.addr,
22+
undefined,
23+
assetId,
24+
params);
25+
26+
const rawSignedTxn = dtxn.signTxn(assetCreator.sk);
27+
const txn = (await client.sendRawTransaction(rawSignedTxn).do());
28+
const confirmedTxn = await algosdk.waitForConfirmation(client, txn.txId, 4);
29+
console.log(confirmedTxn);
30+
31+
console.log('asset destroyed');
32+
}
33+
34+
module.exports = destroyAsset;
35+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const algosdk = require('algosdk');
2+
const ALGOD_SERVER = 'https://node.testnet.algoexplorerapi.io';
3+
const ALGOD_TOKEN = ''; // { 'X-API-Key': 'VELyABA1dGqGbAVktbew4oACvp0c0298gMgYtYIb' }
4+
const ALGOD_PORT = '';
5+
6+
const client = new algosdk.Algodv2(
7+
ALGOD_TOKEN,
8+
ALGOD_SERVER,
9+
ALGOD_PORT,
10+
);
11+
/**
12+
*
13+
* @param {number} length
14+
* @return {String}
15+
*/
16+
function makeid(length) {
17+
let result = '';
18+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
19+
const charactersLength = characters.length;
20+
for ( let i = 0; i < length; i++ ) {
21+
result += characters.charAt(Math.floor(Math.random() *
22+
charactersLength));
23+
}
24+
return result;
25+
}
26+
/**
27+
*
28+
* @param {object} assetCreator
29+
* @param {number} decimals
30+
* @return {Promise<void>}
31+
*/
32+
async function generateAsset(assetCreator, decimals=6) {
33+
const params = await client.getTransactionParams().do();
34+
35+
const createAssetTxn = algosdk.makeAssetCreateTxnWithSuggestedParams(
36+
assetCreator.addr,
37+
undefined, // no note for time being
38+
10000000, // hardCoded issuance for time being
39+
decimals, // hardCoded decimals for time
40+
false,
41+
assetCreator.addr,
42+
undefined,
43+
assetCreator.addr,
44+
assetCreator.addr,
45+
makeid(7),
46+
makeid(7),
47+
undefined,
48+
undefined,
49+
params);
50+
51+
const rawSignedTxn = createAssetTxn.signTxn(assetCreator.sk);
52+
const txn = (await client.sendRawTransaction(rawSignedTxn).do());
53+
const ptx = await algosdk.waitForConfirmation(client, txn.txId, 4);
54+
const assetId = ptx['asset-index'];
55+
56+
return assetId;
57+
}
58+
59+
module.exports = generateAsset;
60+

lib/order/__tests__/TealAlgoEscrowCancel.spec.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,40 @@ const placeAlgoOrderTest = require('./teal_tests/placeAlgoEscrowOrder.js');
44
const closeAlgoOrderTest = require('./teal_tests/closeAlgoEscrowOrder.js');
55
const JEST_MINUTE_TIMEOUT = 60 * 1000;
66
const accountSetup = require('./accountSetup.js');
7+
const accountCleanup = require('./accountCleanup');
8+
const generateAsset = require('./GenerateAsset');
9+
const destroyAsset = require('./DestroyAsset');
710

8-
describe('ALGO ESCROW CANCEL', () => {
11+
12+
describe(`ALGO ESCROW CANCEL`, () => {
913
const price = 1.2;
1014
const amount = 0.8;
11-
const note = `
12-
Testing: TealAlgoEscrowCancel
13-
Open Account: ${config.openAccount.addr}
14-
Creator Account: ${config.creatorAccount.addr}
15-
16-
Creator Account is buying ${amount} LAMPC at price: ${price} Algo
17-
`;
1815

1916
beforeAll(async () => {
17+
const assetId = await generateAsset(config.openAccount);
18+
console.log(assetId);
19+
20+
const note = `
21+
Testing: TealAlgoEscrowCancel
22+
assetId: ${assetId}
23+
Open Account: ${config.openAccount.addr}
24+
Creator Account: ${config.creatorAccount.addr}
25+
26+
Creator Account is buying ${amount} LAMPC at price: ${price} Algo
27+
`;
28+
config.assetId = assetId;
29+
2030
await accountSetup(config, 'buy', true, false, note);
2131
await timeout(7000); // Eliminates race condition where future indexer calls occur before setUp step fully propogates but after it succeeds
2232
}, JEST_MINUTE_TIMEOUT);
2333

24-
// Create App
34+
afterAll(async () => {
35+
await timeout(4000);
36+
await accountCleanup(config, 'buy', true);
37+
await destroyAsset(config.openAccount, config.assetId);
38+
}, JEST_MINUTE_TIMEOUT);
39+
40+
2541
test(
2642
'Place Algo Escrow Order',
2743
async () => {

lib/order/__tests__/TealAlgoEscrowPayCloseout.spec.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,38 @@ const executeAlgoEscrowOrderTest = require('./teal_tests/executeAlgoEscrowOrder'
66
const JEST_MINUTE_TIMEOUT = 60 * 1000;
77
const config = require('./TealConfig');
88
const takerAccountCleanup = require('./takerAccountCleanup');
9+
const generateAsset = require('./GenerateAsset');
10+
const destroyAsset = require('./DestroyAsset');
911

1012
//
1113
describe('Algo ESCROW PAY CLOSEOUT', () => {
1214
const asaAmount = 0.4;
1315
const price = 1.25;
1416
const executorAmount = 0.4;
1517

16-
const note = `
17-
Testing: TealAlgoEscrowPayCloseout
18-
Open Account: ${config.openAccount.addr}
19-
Creator Account: ${config.creatorAccount.addr}
20-
Executor Account: ${config.executorAccount.addr}
21-
22-
Creator Account is buying ${asaAmount} LAMPC at price: ${price} Algo
23-
Executor Account is selling ${executorAmount} LAMPC
24-
`;
2518

2619
beforeAll(async () => {
20+
const assetId = await generateAsset(config.openAccount);
21+
console.log(assetId);
22+
23+
const note = `
24+
Testing: TealAlgoEscrowPayCloseout
25+
assetId: ${assetId}
26+
Open Account: ${config.openAccount.addr}
27+
Creator Account: ${config.creatorAccount.addr}
28+
29+
Creator Account is buying ${asaAmount} LAMPC at price: ${price} Algo
30+
Executor Account is selling ${executorAmount} LAMPC
31+
`;
32+
config.assetId = assetId;
2733
await accountSetup(config, 'buy', true, true, note);
2834
await timeout(7000); // Eliminates race condition where future indexer calls occur before setUp step fully propogates but after it succeeds
29-
}, JEST_MINUTE_TIMEOUT);
35+
}, JEST_MINUTE_TIMEOUT*3);
3036

3137
afterAll(async () => {
3238
await timeout(4000);
3339
await takerAccountCleanup(config, 'buy', executorAmount, true);
40+
await destroyAsset(config.openAccount, config.assetId);
3441
}, JEST_MINUTE_TIMEOUT);
3542

3643
test(

lib/order/__tests__/TealAlgoEscrowPayPartial.spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,40 @@ const executeAlgoEscrowOrderTest = require('./teal_tests/executeAlgoEscrowOrder'
66
const JEST_MINUTE_TIMEOUT = 60 * 1000;
77
const config = require('./TealConfig');
88
const takerAccountCleanup = require('./takerAccountCleanup');
9+
const generateAsset = require('./GenerateAsset');
10+
const destroyAsset = require('./DestroyAsset');
911

1012
//
1113
describe('Algo ESCROW PAY PARTIAL', () => {
1214
const asaAmount = 0.4;
1315
const price = 1.25;
1416
const executorAmount = 0.2;
1517

18+
beforeAll(async () => {
19+
const assetId = await generateAsset(config.openAccount);
20+
console.log(assetId);
1621

17-
const note = `
22+
const note = `
1823
Testing: TealAlgoEscrowPayPartial
24+
assetId: ${assetId}
1925
Open Account: ${config.openAccount.addr}
2026
Creator Account: ${config.creatorAccount.addr}
2127
Executor Account: ${config.executorAccount.addr}
2228
2329
Creator Account is buying ${asaAmount} LAMPC at price: ${price} Algo
2430
Executor Account is selling ${executorAmount} LAMPC
2531
`;
32+
config.assetId = assetId;
33+
2634

27-
beforeAll(async () => {
2835
await accountSetup(config, 'buy', true, true, note);
2936
await timeout(7000); // Eliminates race condition where future indexer calls occur before setUp step fully propogates but after it succeeds
3037
}, JEST_MINUTE_TIMEOUT);
3138

3239
afterAll(async () => {
3340
await timeout(4000);
3441
await takerAccountCleanup(config, 'buy', executorAmount);
42+
await destroyAsset(config.openAccount, config.assetId);
3543
}, JEST_MINUTE_TIMEOUT);
3644

3745
test(

lib/order/__tests__/TealAlgoEscrowPlaceNoOptInTxn.spec.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,35 @@ const config = require('./TealConfig');
55
const {timeout} = require('../../teal/utils');
66
const closeAlgoOrderTest = require('./teal_tests/closeAlgoEscrowOrder.js');
77
const accountCleanup = require('./accountCleanup.js');
8+
const generateAsset = require('./GenerateAsset');
9+
const destroyAsset = require('./DestroyAsset');
810

911
describe('ALGO ESCROW PLACE NO OPT IN TXN', () => {
1012
const amount = 0.8;
1113
const price = 1.2;
1214

13-
const note = `
14-
Testing: TealAlgoEscrowPlaceNoOptInTxn
15-
Open Account: ${config.openAccount.addr}
16-
Creator Account: ${config.creatorAccount.addr}
1715

18-
Creator Account is buying ${amount} LAMPC at price: ${price} Algo
19-
`;
2016
beforeAll(async () => {
17+
const assetId = await generateAsset(config.openAccount);
18+
console.log(assetId);
19+
const note = `
20+
Testing: TealAlgoEscrowPlaceNoOptInTxn
21+
assetId: ${assetId}
22+
23+
Open Account: ${config.openAccount.addr}
24+
Creator Account: ${config.creatorAccount.addr}
25+
26+
Creator Account is buying ${amount} LAMPC at price: ${price} Algo
27+
`;
28+
config.assetId = assetId;
2129
await accountSetup(config, 'buy', true, false, note); // optIn in the setUp phase to test sdk no optIn
2230
await timeout(7000); // Eliminates race condition where future indexer calls occur before setUp step fully propogates but after it succeeds
2331
}, JEST_MINUTE_TIMEOUT);
2432

2533
afterAll(async () => {
2634
await timeout(4000);
2735
await accountCleanup(config, 'buy', true);
36+
await destroyAsset(config.openAccount, config.assetId);
2837
}, JEST_MINUTE_TIMEOUT);
2938

3039
test(

lib/order/__tests__/TealAlgoEscrowPlaceWithOptInTxn.spec.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,36 @@ const config = require('./TealConfig');
55
const {timeout} = require('../../teal/utils');
66
const closeAlgoOrderTest = require('./teal_tests/closeAlgoEscrowOrder.js');
77
const accountCleanup = require('./accountCleanup.js');
8+
const generateAsset = require('./GenerateAsset');
9+
const destroyAsset = require('./DestroyAsset');
810

911
describe('ALGO ESCROW PLACE WITH OPT IN TXN', () => {
1012
const amount = 0.8;
1113
const price = 1.2;
1214

13-
const note = `
15+
beforeAll(async () => {
16+
const assetId = await generateAsset(config.openAccount);
17+
console.log(assetId);
18+
19+
const note = `
1420
Testing: TealAlgoEscrowPlaceWithOptInTxn
21+
assetId: ${assetId}
22+
1523
Open Account: ${config.openAccount.addr}
1624
Creator Account: ${config.creatorAccount.addr}
1725
1826
Creator Account is buying ${amount} LAMPC at price: ${price} Algo
1927
`;
28+
config.assetId = assetId;
2029

21-
beforeAll(async () => {
2230
await accountSetup(config, 'buy', false, false, note);
23-
await timeout(7000); // Eliminates race condition where future indexer calls occur before setUp step fully propogates but after it succeeds
31+
await timeout(10000); // Eliminates race condition where future indexer calls occur before setUp step fully propogates but after it succeeds
2432
}, JEST_MINUTE_TIMEOUT);
2533

2634
afterAll(async () => {
2735
await timeout(4000);
2836
await accountCleanup(config, 'buy', false);
37+
await destroyAsset(config.openAccount, config.assetId);
2938
}, JEST_MINUTE_TIMEOUT);
3039

3140
test(

lib/order/__tests__/TealAsaEscrowCancel.spec.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,42 @@ const closeASAOrderTest = require('./teal_tests/closeASAEscrowOrder.js');
44
const JEST_MINUTE_TIMEOUT = 60 * 1000;
55
const placeASAOrderTest = require('./teal_tests/placeASAEscrowOrder.js');
66
const accountSetup = require('./accountSetup.js');
7+
const accountCleanup = require('./accountCleanup');
8+
const generateAsset = require('./GenerateAsset');
9+
const destroyAsset = require('./DestroyAsset');
710

811

912
describe('ASA ESCROW CANCEL', () => {
1013
const price = 1.2;
1114
const amount = 0.8;
12-
const note = `
13-
Testing: TealAsaEscrowCancel
14-
Open Account: ${config.openAccount.addr}
15-
Creator Account: ${config.creatorAccount.addr}
16-
17-
Creator Account is buying ${amount} LAMPC at price: ${price} Algo
18-
`;
15+
1916

2017
// Create App
2118
beforeAll(async () => {
22-
// await accountSetup(config, "close")
23-
await accountSetup(config, 'sell', true, true, note);
19+
const assetId = await generateAsset(config.openAccount);
20+
console.log(assetId);
21+
22+
const note = `
23+
Testing: TealAsaEscrowCancel
24+
assetId: ${assetId}
2425
26+
Open Account: ${config.openAccount.addr}
27+
Creator Account: ${config.creatorAccount.addr}
28+
29+
Creator Account is buying ${amount} LAMPC at price: ${price} Algo
30+
`;
31+
config.assetId = assetId;
32+
33+
await accountSetup(config, 'sell', true, true, note);
2534
await timeout(7000);
2635
}, JEST_MINUTE_TIMEOUT * 10);
2736

37+
afterAll(async () => {
38+
await timeout(4000);
39+
await accountCleanup(config, 'sell', true);
40+
await destroyAsset(config.openAccount, config.assetId);
41+
}, JEST_MINUTE_TIMEOUT);
42+
2843
test(
2944
'Place asa escrow order',
3045
async () => {

0 commit comments

Comments
 (0)