Skip to content

Commit 1f89143

Browse files
authored
Merge pull request #233 from algodex/231-acceptance-refactor-generate-transaction-types-to-user-sdk-v2-transaction-generators
231 acceptance refactor generate transaction types to user sdk v2 transaction generators
2 parents 2940384 + 9a0c29e commit 1f89143

File tree

13 files changed

+1702
-1118
lines changed

13 files changed

+1702
-1118
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const withPlaceAssetTxns = require('../txns/sell/withPlaceAssetTxns');
2+
const makePlaceAlgoTxns = require('../txns/buy/makePlaceAlgoTxns');
3+
const compile = require('../compile/compile');
4+
const AlgodexApi = require('../../AlgodexApi');
5+
6+
const tesnet = {
7+
config: {
8+
algod: {
9+
uri: 'https://node.testnet.algoexplorerapi.io',
10+
token: '',
11+
},
12+
indexer: {
13+
uri: 'https://algoindexer.testnet.algoexplorerapi.io',
14+
token: '',
15+
},
16+
explorer: {
17+
uri: 'https://indexer.testnet.algoexplorerapi.io',
18+
port: '',
19+
},
20+
dexd: {
21+
uri: 'https://api-testnet-public.algodex.com/algodex-backend',
22+
token: '',
23+
},
24+
},
25+
};
26+
27+
const algodexApi = new AlgodexApi(tesnet);
28+
29+
const TransactionGenerator = {
30+
getPlaceASAEscrowOrderTxns: async function (order, optIn) {
31+
return await withPlaceAssetTxns(
32+
await compile({
33+
...order,
34+
appId:
35+
typeof order.appId === 'undefined'
36+
? await algodexApi.getAppId(order)
37+
: order.appId,
38+
version: 6,
39+
indexer: algodexApi.indexer,
40+
wallet: {
41+
...order.wallet,
42+
...(await algodexApi.http.indexer.fetchAccountInfo(order.wallet)),
43+
},
44+
})
45+
);
46+
},
47+
getPlaceAlgoEscrowOrderTxns: async function (order, optIn) {
48+
return await makePlaceAlgoTxns(
49+
await compile({
50+
...order,
51+
appId:
52+
typeof order.appId === 'undefined'
53+
? await algodexApi.getAppId(order)
54+
: order.appId,
55+
version: 6,
56+
indexer: algodexApi.indexer,
57+
wallet: {
58+
...order.wallet,
59+
...(await algodexApi.http.indexer.fetchAccountInfo(order.wallet)),
60+
},
61+
})
62+
);
63+
},
64+
};
65+
66+
module.exports = TransactionGenerator;
Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
it('should fix these tests', ()=>{
1+
it('should fix these tests', () => {
22
const isBroken = true;
33
expect(isBroken).toEqual(true);
44
});
55
const testHelper = require('./setup.js');
6-
const createAppTest = require('./teal_tests/createAppTest.js');
7-
const deleteAppTest = require('./teal_tests/deleteAppTest.js');
8-
const executeAlgoOrderTest = require('./teal_tests/executeAlgoEscrowOrder.js');
6+
const connector = require('../../wallet/connectors/AlgoSDK');
7+
8+
// const deleteAppTest = require('./teal_tests/deleteAppTest.js');
9+
910
const placeASAOrderTest = require('./teal_tests/placeASAEscrowOrder.js');
10-
const closeASAOrderTest = require('./teal_tests/closeASAEscrowOrder.js');
11+
// const closeASAOrderTest = require('./teal_tests/closeASAEscrowOrder.js');
1112
const constants = require('./constants.js');
13+
const setup = require('./beforeAll.js');
1214
const JEST_MINUTE_TIMEOUT = 60 * 1000;
1315

1416
const config = {
@@ -18,12 +20,15 @@ const config = {
1820
openAccount: testHelper.getOpenAccount(),
1921
maliciousAccount: testHelper.getRandomAccount(),
2022
client: testHelper.getLocalClient(),
23+
connector: connector,
2124
assetId: 66711302,
2225
};
2326

24-
console.log('DEBUG_SMART_CONTRACT_SOURCE is: ' + constants.DEBUG_SMART_CONTRACT_SOURCE);
27+
console.log(
28+
'DEBUG_SMART_CONTRACT_SOURCE is: ' + constants.DEBUG_SMART_CONTRACT_SOURCE
29+
);
30+
2531

26-
const textEncoder = new TextEncoder();
2732
//
2833
// // TODO: The negative tests need to be implemented. The commented ones out are examples but will not work with
2934
// // this transaction type.
@@ -54,48 +59,72 @@ const textEncoder = new TextEncoder();
5459
//
5560
//
5661
describe('ASA ESCROW ORDER BOOK', () => {
57-
test('Create asa escrow order book', async () => {
58-
config.creatorAccount = testHelper.getRandomAccount();
59-
config.executorAccount = testHelper.getRandomAccount();
60-
config.maliciousAccount = testHelper.getRandomAccount();
61-
config.appId = await createAppTest.runTest(config, false);
62-
global.ASA_ESCROW_APP_ID = config.appId;
63-
expect(config.appId).toBeGreaterThan(0);
62+
function timeout(ms) {
63+
return new Promise(resolve => setTimeout(resolve, ms));
64+
}
65+
beforeAll(async () => {
66+
await setup(config, 'sell', true)
67+
await timeout(7000) // Eliminates race condition where future indexer calls occur before setUp step fully propogates but after it succeeds
68+
6469
}, JEST_MINUTE_TIMEOUT);
70+
// test(
71+
// 'Create asa escrow order book',
72+
// async () => {
73+
// config.creatorAccount = testHelper.getRandomAccount();
74+
// config.executorAccount = testHelper.getRandomAccount();
75+
// config.maliciousAccount = testHelper.getRandomAccount();
76+
// config.appId = await createAppTest.runTest(config, false);
77+
// global.ASA_ESCROW_APP_ID = config.appId;
78+
// expect(config.appId).toBeGreaterThan(0);
79+
// },
80+
// JEST_MINUTE_TIMEOUT,
81+
// );
6582

66-
test('Place asa escrow order', async () => {
67-
const asaAmount = 400000;
68-
const price = 1.25;
69-
const result = await placeASAOrderTest.runTest(config, asaAmount, price);
70-
expect(result).toBeTruthy();
71-
}, JEST_MINUTE_TIMEOUT);
72-
//
73-
// negTests.map( (negTestTxnConfig) => {
74-
// const testName = `Negative algo full execution order test: txnNum: ${negTestTxnConfig.txnNum} field: ${negTestTxnConfig.field} val: ${negTestTxnConfig.val}`;
75-
// test(testName, async () => {
76-
// if (negTestTxnConfig.negTxn) {
77-
// negTestTxnConfig.negTxn.unsignedTxn = await negTestTxnConfig.negTxn.unsignedTxnPromise;
78-
// }
79-
// const outerTxns = await executeAlgoOrderTest.runFullExecTest(config, true);
80-
// outerTxns.map( (txn) => {
81-
// const unsignedTxn = txn.unsignedTxn;
82-
// // console.log({unsignedTxn});
83-
// });
84-
// const result = await testHelper.runNegativeTest(config, config.client, outerTxns, negTestTxnConfig);
85-
// expect(result).toBeTruthy();
86-
// }, JEST_MINUTE_TIMEOUT);
87-
// });
88-
//
89-
//
90-
test('Close asa escrow order', async () => {
91-
const price = 1.25;
92-
const result = await closeASAOrderTest.runTest(config, price);
93-
expect(result).toBeTruthy();
94-
}, JEST_MINUTE_TIMEOUT);
83+
test(
84+
'Place asa escrow order',
85+
async () => {
86+
const asaAmount = 0.4;
87+
const price = 1.25;
88+
const result = await placeASAOrderTest.runTest(config, asaAmount, price);
89+
expect(result).toBeTruthy();
90+
},
91+
JEST_MINUTE_TIMEOUT
92+
);
93+
//
94+
// negTests.map( (negTestTxnConfig) => {
95+
// const testName = `Negative algo full execution order test: txnNum: ${negTestTxnConfig.txnNum} field: ${negTestTxnConfig.field} val: ${negTestTxnConfig.val}`;
96+
// test(testName, async () => {
97+
// if (negTestTxnConfig.negTxn) {
98+
// negTestTxnConfig.negTxn.unsignedTxn = await negTestTxnConfig.negTxn.unsignedTxnPromise;
99+
// }
100+
// const outerTxns = await executeAlgoOrderTest.runFullExecTest(config, true);
101+
// outerTxns.map( (txn) => {
102+
// const unsignedTxn = txn.unsignedTxn;
103+
// // console.log({unsignedTxn});
104+
// });
105+
// const result = await testHelper.runNegativeTest(config, config.client, outerTxns, negTestTxnConfig);
106+
// expect(result).toBeTruthy();
107+
// }, JEST_MINUTE_TIMEOUT);
108+
// });
109+
//
110+
//
111+
// test(
112+
// 'Close asa escrow order',
113+
// async () => {
114+
// const price = 1.25;
115+
// const result = await closeASAOrderTest.runTest(config, price);
116+
// expect(result).toBeTruthy();
117+
// },
118+
// JEST_MINUTE_TIMEOUT
119+
// );
95120

96-
test('Delete asa escrow order book', async () => {
97-
const result = await deleteAppTest.runTest(config);
98-
expect(result).toBeTruthy();
99-
}, JEST_MINUTE_TIMEOUT);
121+
// test(
122+
// 'Delete asa escrow order book',
123+
// async () => {
124+
// const result = await deleteAppTest.runTest(config);
125+
// expect(result).toBeTruthy();
126+
// },
127+
// JEST_MINUTE_TIMEOUT
128+
// );
100129
});
101130
//

lib/order/__tests__/asa_dex_teal.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
/////////////////////////////
1+
// ///////////////////////////
22
// Alexander Trefonas //
33
// 7/9/2021 //
44
// Copyright Algodev Inc //
55
// All Rights Reserved. //
6-
/////////////////////////////
6+
// ///////////////////////////
77

88
const AsaOrderbookTeal = {
99

10-
getClearProgram : function getClearProgram() {
11-
const clearProgram =
10+
getClearProgram: function getClearProgram() {
11+
const clearProgram =
1212
`
1313
#pragma version 2
1414
// This program clears program state.
@@ -19,12 +19,12 @@ const AsaOrderbookTeal = {
1919
`
2020
;
2121
return clearProgram;
22-
},
23-
24-
getASAOrderBookApprovalProgram : function getASAOrderBookApprovalProgram() {
25-
// stateful DEX contract
26-
// This is for the order book
27-
return `
22+
},
23+
24+
getASAOrderBookApprovalProgram: function getASAOrderBookApprovalProgram() {
25+
// stateful DEX contract
26+
// This is for the order book
27+
return `
2828
////////////////////////////////////////////////
2929
// STATEFUL CONTRACT /
3030
// ORDER BOOK FOR ASA ESCROWS (SELL ORDERS) /
@@ -327,8 +327,7 @@ const AsaOrderbookTeal = {
327327
328328
329329
`;
330-
331-
}
332-
};
333-
334-
module.exports = AsaOrderbookTeal;
330+
},
331+
};
332+
333+
module.exports = AsaOrderbookTeal;

lib/order/__tests__/beforeAll.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const AlgodexApi = require('../../AlgodexApi');
44
const apiConfig = require('../../../config.json');
55
const initAccounts = require('../../teal/test/initAccounts');
66
const logger = require('../../logger');
7+
8+
9+
710
/**
811
*
912
* @param {TestConfig} config Test Configuration
@@ -19,31 +22,32 @@ async function beforeAll(config, type, optIn = false, newCreator = false) {
1922
}
2023

2124
// Initalize API Client
22-
await config.init(AlgodexApi, [apiConfig]);
25+
// await config.init(AlgodexApi, [apiConfig]);
26+
const api = new AlgodexApi(apiConfig)
2327

2428
// Initialize Accounts and Configuration
2529
await initAccounts(config, optIn, newCreator);
2630

27-
const {client, creatorAccount} = config;
28-
const createTxn = await txns.makeApplicationCreateTxn(
29-
client,
30-
type,
31-
creatorAccount,
32-
config.suggestedParams,
33-
);
34-
const txId = createTxn.txID().toString();
31+
// const {client, creatorAccount} = config;
32+
// const createTxn = await txns.makeApplicationCreateTxn(
33+
// client,
34+
// type,
35+
// creatorAccount,
36+
// config.suggestedParams,
37+
// );
38+
// const txId = createTxn.txID().toString();
3539

36-
const signedTxn = createTxn.signTxn(creatorAccount.sk);
40+
// const signedTxn = createTxn.signTxn(creatorAccount.sk);
3741

38-
await client.sendRawTransaction(signedTxn).do();
42+
// await client.sendRawTransaction(signedTxn).do();
3943

40-
// Wait for confirmation
41-
await algosdk.waitForConfirmation(client, txId, 10);
44+
// // Wait for confirmation
45+
// await algosdk.waitForConfirmation(client, txId, 10);
4246

43-
// display results
44-
const transactionResponse = await client.pendingTransactionInformation(txId).do();
47+
// // display results
48+
// const transactionResponse = await client.pendingTransactionInformation(txId).do();
4549

46-
config.setAppIndex(transactionResponse['application-index']);
50+
// config.setAppIndex(transactionResponse['application-index']);
4751
}
4852

4953
module.exports = beforeAll;

0 commit comments

Comments
 (0)